-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_mish.py
More file actions
170 lines (135 loc) · 5.05 KB
/
test_mish.py
File metadata and controls
170 lines (135 loc) · 5.05 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
'''
test_mish.py
Test Mish() implementation
# Author: Ty Nguyen
# Contact: tynguyen.tech@gmail.com
'''
import gc
import torch
import torch.nn as nn
import numpy as np
import torch.nn.functional as F
from sklearn.datasets import make_classification
from mish import Mish
def get_model(mish_module):
# Deliberately make the model very large
width = 2 ** 19
return nn.Sequential(
nn.Linear(256, width),
mish_module(inplace=True),
nn.BatchNorm1d(width),
nn.Linear(width, 1)
)
def print_parameter_count(model):
print("# of parameters: {:,d}".format(
np.sum(list(p.numel() for p in model.parameters()))))
print("# of trainable parameters: {:,d}".format(
np.sum(list(p.numel() for p in model.parameters() if p.requires_grad))))
# Plain Mish
def mish(input):
return input * torch.tanh(F.softplus(input))
class PlainMish(nn.Module):
def __init__(self, **kwargs):
super().__init__()
pass
def forward(self, input_tensor):
return mish(input_tensor)
############################################################################################################
def test_mish_forward_backward():
torch.manual_seed(0)
import numpy as np
np.random.seed(0)
total_test = 20
n_correct = 0
for t in range(total_test):
np_input = np.random.randn(20, 30)
plain_input = torch.tensor([np_input], requires_grad=True)
eff_input = torch.tensor([np_input], requires_grad=True)
plain_mish = PlainMish()
eff_mish = Mish()
plain_output = plain_mish(plain_input).sum()
eff_output = eff_mish(eff_input).sum()
print("=======================\nForward:")
assert plain_output - eff_output < 1e-9, "Outpus must match!"
print("=======================\nBackward:")
eff_output.backward()
eff_grad = eff_input.grad
#print("Eff grad:\n", eff_grad)
print("----------------")
plain_output.backward()
plain_grad = plain_input.grad
#print("Plain grad:\n", plain_grad)
#print("----------------")
delta_grad = torch.abs(plain_grad - eff_grad).max()
print("Max delta grad:\n", delta_grad)
assert delta_grad < 1e-15, "Grads must match!"
print("==========================\n")
print("Successful!")
n_correct += 1
print("==========================\n")
print("Completed! Succeeded %d/%d!"%(n_correct, total_test))
def test_mish_memory():
X, y = make_classification(
n_samples=1024,
n_features=256,
n_informative=128,
n_redundant=0,
n_repeated=0,
n_classes=2,
n_clusters_per_class=2,
flip_y=0.01,
class_sep=1.0,
hypercube=True,
shuffle=True,
random_state=42
)
criterion = nn.BCEWithLogitsLoss()
batch_size = 128
model = get_model(PlainMish).cuda()
print_parameter_count(model)
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
optimizer.zero_grad()
print("begin:", torch.cuda.memory_allocated() / 1024 ** 2)
for i in range(0, 1024, batch_size):
Xt, yt = torch.tensor(X[i:i+batch_size], dtype=torch.float).cuda(), torch.tensor(y[i:i+batch_size], dtype=torch.float).cuda()
print("data:", torch.cuda.memory_allocated() / 1024 ** 2)
pred = model(Xt)[:, 0]
print("forw:", torch.cuda.memory_allocated() / 1024 ** 2)
loss = criterion(pred, yt)
# print(loss)
print("loss:", torch.cuda.memory_allocated() / 1024 ** 2)
loss.backward()
print("back:", torch.cuda.memory_allocated() / 1024 ** 2)
optimizer.step()
optimizer.zero_grad()
print("step:", torch.cuda.memory_allocated() / 1024 ** 2)
print("=" * 20)
del optimizer, model, Xt, yt, loss, pred
gc.collect()
print("end:", torch.cuda.memory_allocated() / 1024 ** 2)
print("===============================================")
print("Custom Mish")
model = get_model(Mish).cuda()
optimizer = torch.optim.SGD(model.parameters(), lr=0.1)
optimizer.zero_grad()
torch.cuda.memory_allocated() / 1024
for i in range(0, 1024, batch_size):
Xt, yt = torch.tensor(X[i:i+batch_size], dtype=torch.float).cuda(), torch.tensor(y[i:i+batch_size], dtype=torch.float).cuda()
print("data:", torch.cuda.memory_allocated() / 1024 ** 2)
pred = model(Xt)[:, 0]
print("forw:", torch.cuda.memory_allocated() / 1024 ** 2)
loss = criterion(pred, yt)
# print(loss)
print("loss:", torch.cuda.memory_allocated() / 1024 ** 2)
loss.backward()
print("back:", torch.cuda.memory_allocated() / 1024 ** 2)
optimizer.step()
optimizer.zero_grad()
print("step:", torch.cuda.memory_allocated() / 1024 ** 2)
print("=" * 20)
del optimizer, model, Xt, yt, loss, pred
gc.collect()
print("end:", torch.cuda.memory_allocated() / 1024 ** 2)
if __name__=="__main__":
test_mish_memory()
test_mish_forward_backward()