-
Notifications
You must be signed in to change notification settings - Fork 2
/
grad_check.py
112 lines (92 loc) · 3.15 KB
/
grad_check.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import torch
from hgru import Hgru1d
n = 512
b = 1
d = 32
dtype = torch.bfloat16
# dtype = torch.float16
# dtype = torch.float32
model = Hgru1d(d, use_triton=False).cuda().to(dtype)
x = torch.randn(n, b, d).to(dtype).cuda().requires_grad_()
y1 = model(x)
y2 = model.forward_naive(x)
y3 = model.forward_triton(x)
print(torch.norm(y1 - y2), torch.norm(y1 - y3))
print("====================")
res = []
if x.grad != None:
x.grad.data.zero_()
if model.input_proj.weight.grad != None:
model.input_proj.weight.grad.data.zero_()
if model.lambda_proj.weight.grad != None:
model.lambda_proj.weight.grad.data.zero_()
if model.theta.grad != None:
model.theta.grad.data.zero_()
if model.gate.weight.grad != None:
model.gate.weight.grad.data.zero_()
if model.out_proj.weight.grad != None:
model.out_proj.weight.grad.data.zero_()
if model.norm.weight.grad != None:
model.norm.weight.grad.data.zero_()
loss = (y1**2).sum()
loss.backward()
res.append(x.grad.data.clone())
res.append(model.input_proj.weight.grad.data.clone())
res.append(model.lambda_proj.weight.grad.data.clone())
res.append(model.theta.grad.data.clone())
res.append(model.gate.weight.grad.data.clone())
res.append(model.out_proj.weight.grad.data.clone())
res.append(model.norm.weight.grad.data.clone())
if x.grad != None:
x.grad.data.zero_()
if model.input_proj.weight.grad != None:
model.input_proj.weight.grad.data.zero_()
if model.lambda_proj.weight.grad != None:
model.lambda_proj.weight.grad.data.zero_()
if model.theta.grad != None:
model.theta.grad.data.zero_()
if model.gate.weight.grad != None:
model.gate.weight.grad.data.zero_()
if model.out_proj.weight.grad != None:
model.out_proj.weight.grad.data.zero_()
if model.norm.weight.grad != None:
model.norm.weight.grad.data.zero_()
loss = (y2**2).sum()
loss.backward()
res.append(x.grad.data.clone())
res.append(model.input_proj.weight.grad.data.clone())
res.append(model.lambda_proj.weight.grad.data.clone())
res.append(model.theta.grad.data.clone())
res.append(model.gate.weight.grad.data.clone())
res.append(model.out_proj.weight.grad.data.clone())
res.append(model.norm.weight.grad.data.clone())
if x.grad != None:
x.grad.data.zero_()
if model.input_proj.weight.grad != None:
model.input_proj.weight.grad.data.zero_()
if model.lambda_proj.weight.grad != None:
model.lambda_proj.weight.grad.data.zero_()
if model.theta.grad != None:
model.theta.grad.data.zero_()
if model.gate.weight.grad != None:
model.gate.weight.grad.data.zero_()
if model.out_proj.weight.grad != None:
model.out_proj.weight.grad.data.zero_()
if model.norm.weight.grad != None:
model.norm.weight.grad.data.zero_()
loss = (y3**2).sum()
loss.backward()
res.append(x.grad.data.clone())
res.append(model.input_proj.weight.grad.data.clone())
res.append(model.lambda_proj.weight.grad.data.clone())
res.append(model.theta.grad.data.clone())
res.append(model.gate.weight.grad.data.clone())
res.append(model.out_proj.weight.grad.data.clone())
res.append(model.norm.weight.grad.data.clone())
c = 7
for i in range(c):
print(
torch.norm(res[i] - res[i + c]),
torch.norm(res[i] - res[i + 2 * c]),
torch.norm(res[i]),
)