Skip to content

Commit

Permalink
torch 2 inf
Browse files Browse the repository at this point in the history
  • Loading branch information
michalwols committed Aug 30, 2023
1 parent 7a02e18 commit 27a5f35
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions yann/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,11 @@ def grad_norm(parameters, norm_type: float = 2.0):
if len(parameters) == 0:
return torch.tensor(0.)
device = parameters[0].grad.device
from torch._six import inf
try:
from torch import inf
except ImportError:
from torch._six import inf

if norm_type == inf:
norms = [p.grad.detach().abs().max().to(device) for p in parameters]
norm = norms[0] if len(norms) == 1 else torch.max(torch.stack(norms))
Expand All @@ -471,7 +475,10 @@ def param_norm(parameters, norm_type: float = 2.0):
if len(parameters) == 0:
return torch.tensor(0.)
device = parameters[0].device
from torch._six import inf
try:
from torch import inf
except ImportError:
from torch._six import inf
if norm_type == inf:
norms = [p.detach().abs().max().to(device) for p in parameters]
norm = norms[0] if len(norms) == 1 else torch.max(torch.stack(norms))
Expand Down
2 changes: 1 addition & 1 deletion yann/callbacks/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def decorated(func):
return decorated

@callback
def on_init(self, trainer=None):
def on_init(self, trainer=None, kwargs=None):
pass

@callback
Expand Down

0 comments on commit 27a5f35

Please sign in to comment.