-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.py
More file actions
46 lines (37 loc) · 1.51 KB
/
Copy pathutils.py
File metadata and controls
46 lines (37 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from typing import Callable
import numpy as np
from tinygrad import Tensor, dtypes
def get_activation_fn(activation: str) -> Callable:
"""Return an activation function given a string."""
if activation == "relu":
return Tensor.relu
if activation == "gelu":
return Tensor.gelu
raise RuntimeError(f"activation should be relu/gelu/glu, not {activation}.")
def clip_grad_norm_(parameters, max_norm) -> Tensor:
if isinstance(parameters, Tensor):
parameters = [parameters]
max_norm = float(max_norm)
if len(parameters) == 0:
return Tensor(0.0)
# For Metal, we need this: Metal can only support 32 memory buffers
is_32 = 0
total_norm = Tensor.zeros((), dtype=dtypes.float32)
for p in parameters:
if p.grad is not None:
param_norm = p.grad.flatten().square().sum().realize()
total_norm += param_norm
is_32 += 1
if is_32 > 29:
total_norm = total_norm.realize()
is_32 = 0
# We need one extra realize here: make sure there's nothing left in the parameter calc
total_norm = (total_norm.realize() + 1e-12).sqrt()
print(f'total_norm: {total_norm.numpy()}')
clip_coef = max_norm / (total_norm + 1e-6)
clip_coef = Tensor.minimum(clip_coef, Tensor.ones_like(clip_coef)).contiguous().realize().item()
print(f'clip_coef: {clip_coef}')
for p in parameters:
if p.grad is not None:
p.grad *= clip_coef
return total_norm