forked from Lightning-AI/pytorch-lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove .item which causes sync issues (Lightning-AI#1254)
* remove .item which causes sync issues * fixed gradient acc sched * fixed gradient acc sched
- Loading branch information
1 parent
1742734
commit 52118a1
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import torch | ||
|
||
|
||
class TensorRunningMean(object): | ||
""" | ||
Tracks a running mean without graph references. | ||
Round robbin for the mean | ||
""" | ||
def __init__(self, window_length): | ||
self.window_length = window_length | ||
self.reset() | ||
self.last_idx = 0 | ||
|
||
def reset(self): | ||
self.memory = torch.Tensor(self.window_length) | ||
self.current_idx = 0 | ||
|
||
def last(self): | ||
return self.memory[self.last_idx] | ||
|
||
def append(self, x): | ||
# map proper type for memory if they don't match | ||
if self.memory.type() != x.type(): | ||
self.memory.type_as(x) | ||
|
||
# store without grads | ||
with torch.no_grad(): | ||
self.memory[self.current_idx] = x | ||
self.last_idx = self.current_idx | ||
|
||
# increase index | ||
self.current_idx += 1 | ||
|
||
# reset index when hit limit of tensor | ||
if self.current_idx >= self.window_length: | ||
self.current_idx = 0 | ||
|
||
def mean(self): | ||
return self.memory.mean() |