-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_act.py
More file actions
282 lines (223 loc) · 11.1 KB
/
Copy pathplot_act.py
File metadata and controls
282 lines (223 loc) · 11.1 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from numpy.core.fromnumeric import nonzero
from numpy.lib.function_base import delete
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from models.dss_util import round_muldiv, round_divmul, Thresh, floor_muldiv, floor_divmul
import matplotlib.pyplot as plt
import math
class FunLSQ(torch.autograd.Function):
@staticmethod
def forward(ctx, weight, alpha, g, Qn, Qp):
assert alpha > 0, 'alpha = {}'.format(alpha)
ctx.save_for_backward(weight, alpha)
ctx.other = g, Qn, Qp
q_w = (weight / alpha).round().clamp(Qn, Qp)
w_q = q_w * alpha
return w_q
@staticmethod
def backward(ctx, grad_weight):
weight, alpha = ctx.saved_tensors
g, Qn, Qp = ctx.other
q_w = weight / alpha
indicate_small = (q_w < Qn).float()
indicate_big = (q_w > Qp).float()
# indicate_middle = torch.ones(indicate_small.shape).to(indicate_small.device) - indicate_small - indicate_big
indicate_middle = 1.0 - indicate_small - indicate_big # Thanks to @haolibai
grad_alpha = ((indicate_small * Qn + indicate_big * Qp + indicate_middle * (-q_w + q_w.round())) * grad_weight * g).sum().unsqueeze(dim=0)
grad_weight = indicate_middle * grad_weight
return grad_weight, grad_alpha, None, None, None
def plot_tanhshrink():
v = torch.linspace(-2, 2, 2000)
v = v.detach()
lambd = 5e-3
scale = 0.5/lambd
m1 = nn.Tanhshrink()
m2 = lambda x: F.softshrink(x, lambd=0.5)
y1 = m1(v)
y2 = m2(v)
fig = plt.figure(21, figsize=(32,32),dpi=300)
ax = plt.subplot(111)
ax.plot(v, y1.squeeze().detach().numpy(), label=r'$\bar{v}$', color='g', linewidth=1.5)
ax.plot(v, y2.squeeze().detach().numpy(), label=r'$\bar{v}$', color='r', linewidth=1.5)
ax.grid()
# plt.ylim([-3,7])
ax.axis('equal')
fig.savefig('resources/tanhshrink.png')
# fig.savefig('run_act_test/HSwish.pdf')
def plot_act1():
act_name = 'Swish'
act_swish = get_act('swish', 1)
act_hswish = get_act('hswish', 1)
v = torch.linspace(-4, 7, 2000)
v0 = v.clone()
v.requires_grad=True
s = torch.tensor([1.0/4.0])
s.requires_grad=True
v_bar = Thresh.apply(v, s)
# Swish
v_hat_swish = act_swish(v_bar).squeeze()
func_act_swish = lambda x: act_swish(Thresh.apply(x, s)).squeeze()
jac_swish = [torch.autograd.grad(func_act_swish(v_), s)[0] for v_ in v]
jac_swish = torch.cat(jac_swish)
# H-Swish
v_hat_hswish = act_hswish(v_bar).squeeze()
func_act_hswish = lambda x: act_hswish(Thresh.apply(x, s)).squeeze()
jac_hswish = [torch.autograd.grad(func_act_hswish(v_), s)[0] for v_ in v]
jac_hswish = torch.cat(jac_hswish)
fig = plt.figure(21, figsize=(32,32),dpi=300)
ax = plt.subplot(111)
ax.plot(v0, v_bar.squeeze().detach().numpy(), label=r'$\bar{v}$', color='g', linewidth=1.5)
ax.plot(v0, v_hat_swish.squeeze().detach().numpy(), label=r'$\hat{v}$(Swish)', color='r', linewidth=1.5)
ax.plot(v0, v_hat_hswish.squeeze().detach().numpy(), label=r'$\hat{v}$(H-Swish)', color='m', linewidth=1.5)
ax.plot(v0, jac_swish.squeeze().detach().numpy(), label=r'$\frac{\partial \hat{v}}{\partial s}$(Swish)', color='b', linewidth=1.5)
ax.plot(v0, jac_hswish.squeeze().detach().numpy(), label=r'$\frac{\partial \hat{v}}{\partial s}$(H-Swish)', color='c', linewidth=1.5)
ax.set_xlabel(r'$v$', fontsize=38)
ax.set_ylabel(r'$\bar{v},\hat{v}, \frac{\partial \hat{v}}{\partial s}$', fontsize=38)
ax.legend(loc='best', fontsize=38)
ax.grid()
# plt.ylim([-3,7])
ax.axis('equal')
fig.savefig('run_act_test/(H)Swish(2).png')
# fig.savefig('run_act_test/HSwish.pdf')
def plot_act2():
fontsize = 15
Qp = 9.0
Qn = 0.0
act_name = 'relu_'
# act = get_act('relu')
act = lambda x: F.relu(x, inplace=False)
# act = FunLSQ.apply(v_i, th, g, 0, 6)
g = 1.0 / math.sqrt(1 * Qp*0.1)
v = torch.linspace(-2, Qp, 2000)
v0 = v.clone()
v.requires_grad=True
use_log = False
th_val = 9.0/5.0
if use_log:
th1 = torch.tensor([np.log(th_val)])
th2 = torch.tensor([np.log(1/th_val).item()])
else:
th1 = torch.tensor([th_val])
th2 = torch.tensor([1/th_val])
th1.requires_grad=True
th2.requires_grad=True
ax = plt.subplot(111)
dv__ds_DSS1 = [torch.autograd.grad(round_divmul(act(v_i), th1, round_only=True, use_log=use_log), th1)[0] for v_i in v]
dv__ds_LSQ1 = [torch.autograd.grad(round_divmul(act(v_i), th1, round_only=False, use_log=use_log), th1)[0] for v_i in v]
dv__ds_LSQ1 = torch.cat(dv__ds_LSQ1)
dv__ds_DSS1 = torch.cat(dv__ds_DSS1)
dv__ds_DSS2 = [torch.autograd.grad(round_muldiv(act(v_i), th2, round_only=True, use_log=use_log), th2)[0] for v_i in v]
dv__ds_LSQ2 = [torch.autograd.grad(round_muldiv(act(v_i), th2, round_only=False, use_log=use_log), th2)[0] for v_i in v]
dv__ds_LSQ2 = torch.cat(dv__ds_LSQ2)
dv__ds_DSS2 = torch.cat(dv__ds_DSS2)
v_hat = round_divmul(act(v.detach()), th1, round_only=False, use_log=use_log)
fig = plt.figure(figsize=(9, 12))
ax = plt.subplot(111)
ax.set_xlim([-3,7])
ax.set_ylim([-5,6.5])
ax.plot(v0.detach().numpy(), (v0.detach()).numpy(), label=r'$x$', color='k', linewidth=1.3, linestyle='-.')
ax.plot(v0.detach().numpy(), v_hat.detach().numpy(), label=r'$\hat{x}$', color='b', linewidth=1.5)
ax.plot(v0.detach().numpy(), dv__ds_LSQ1.detach().numpy(), label=r'$\frac{\partial \hat{x}}{\partial s}$(LSQ)', color='r', linewidth=1.5,linestyle='dashed')
ax.plot(v0.detach().numpy(), dv__ds_DSS1.detach().numpy(), label=r'$\frac{\partial \hat{x}}{\partial s}$(MG)', color='g', linewidth=1.5,linestyle='dashed')
# ax.plot(v0.detach().numpy(), dv__ds_LSQ2.detach().numpy(), label=r'$\frac{\partial \hat{x}}{\partial s}$(LSQ, mul->div)', color='r', linewidth=1.3)
# ax.plot(v0.detach().numpy(), dv__ds_DSS2.detach().numpy(), label=r'$\frac{\partial \bar{x}}{\partial s}$(mul->div)', color='g', linewidth=1.3)
ax.set_xlabel(r'$x$', fontsize=fontsize)
ax.set_ylabel(r'$x, \hat{x}, \frac{\partial \hat{x}}{\partial s}$', fontsize=fontsize)
# ax.set_ylabel(r'$v, \bar{v}, \hat{v}, \frac{\partial \hat{v}}{\partial s},\frac{\partial \bar{v}}{\partial s}$', fontsize=fontsize)
ax.legend(loc='upper left', fontsize=fontsize)
ax.grid()
ax.axis('equal')
fig.savefig('resources/' + 'LSQ_vs_DSS_log_' + str(use_log) + '.png')
fig.savefig('resources/' + 'LSQ_vs_DSS_log_' + str(use_log) + '.pdf')
def plot_act3():
fontsize = 15
Qp = 9.0
Qn = 0.0
act_name = 'relu_'
# act = get_act('relu')
act = lambda x: F.relu(x, inplace=False)
g = 1.0 / math.sqrt(1 * Qp*0.1)
v = torch.linspace(-2, Qp, 2000)
v0 = v.clone()
v.requires_grad=True
use_log = False
th_val = 9.0/5.0
if use_log:
th1 = torch.tensor([np.log(th_val)])
th2 = torch.tensor([np.log(1/th_val).item()])
else:
th1 = torch.tensor([th_val])
th2 = torch.tensor([1/th_val])
th1.requires_grad=True
th2.requires_grad=True
ax = plt.subplot(111)
dv__ds_DSS1 = [torch.autograd.grad(round_divmul(act(v_i), th1, round_only=True, use_log=use_log), th1)[0] for v_i in v]
dv__ds_LSQ1 = [torch.autograd.grad(round_divmul(act(v_i), th1, round_only=False, use_log=use_log), th1)[0] for v_i in v]
dv__ds_LSQ1 = torch.cat(dv__ds_LSQ1)
dv__ds_DSS1 = torch.cat(dv__ds_DSS1)
dv__ds_DSS2 = [torch.autograd.grad(floor_divmul(act(v_i), th2, round_only=True, use_log=use_log), th2)[0] for v_i in v]
dv__ds_LSQ2 = [torch.autograd.grad(floor_divmul(act(v_i), th2, round_only=False, use_log=use_log), th2)[0] for v_i in v]
dv__ds_LSQ2 = torch.cat(dv__ds_LSQ2)
dv__ds_DSS2 = torch.cat(dv__ds_DSS2)
v_hat1 = round_divmul(act(v.detach()), th1, round_only=False, use_log=use_log)
v_hat2 = floor_divmul(act(v.detach()), th1, round_only=False, use_log=use_log)
fig = plt.figure(figsize=(9, 12))
ax = plt.subplot(111)
ax.set_xlim([-3,7])
ax.set_ylim([-5,6.5])
ax.plot(v0.detach().numpy(), (v0.detach()).numpy(), label=r'$x$', color='k', linewidth=1.3, linestyle='-.')
ax.plot(v0.detach().numpy(), v_hat1.detach().numpy(), label=r'$\hat{x}1$', color='b', linewidth=1.5,linestyle='dashed')
ax.plot(v0.detach().numpy(), v_hat2.detach().numpy(), label=r'$\hat{x}2$', color='b', linewidth=1.5)
ax.plot(v0.detach().numpy(), dv__ds_LSQ1.detach().numpy(), label=r'$\frac{\partial \hat{x}}{\partial s}$(LSQ)', color='r', linewidth=1.5,linestyle='dashed')
ax.plot(v0.detach().numpy(), dv__ds_DSS1.detach().numpy(), label=r'$\frac{\partial \hat{x}}{\partial s}$(MG)', color='g', linewidth=1.5,linestyle='dashed')
ax.plot(v0.detach().numpy(), dv__ds_LSQ2.detach().numpy(), label=r'$\frac{\partial \hat{x}}{\partial s}$(LSQ-f)', color='r', linewidth=1.3)
ax.plot(v0.detach().numpy(), dv__ds_DSS2.detach().numpy(), label=r'$\frac{\partial \bar{x}}{\partial s}$(MG-f)', color='g', linewidth=1.3)
ax.set_xlabel(r'$x$', fontsize=fontsize)
ax.set_ylabel(r'$x, \hat{x}, \frac{\partial \hat{x}}{\partial s}$', fontsize=fontsize)
# ax.set_ylabel(r'$v, \bar{v}, \hat{v}, \frac{\partial \hat{v}}{\partial s},\frac{\partial \bar{v}}{\partial s}$', fontsize=fontsize)
ax.legend(loc='upper left', fontsize=fontsize)
ax.grid()
ax.axis('equal')
fig.savefig('resources/' + 'LSQ_vs_DSS_flooe_' + str(use_log) + '.png')
fig.savefig('resources/' + 'LSQ_vs_DSS_flooe_' + str(use_log) + '.pdf')
def plot_act4():
fontsize = 15
Qp = 6.0
Qn = 0.0
# act = get_act('relu')
v_i_3p3 = torch.tensor([2.3])
v_i_3p0 = torch.tensor([2.4])
th = torch.linspace(1/1024., 5.5, 2000)
# ste_round(v_i, torch.tensor([3.0]))
use_floor = True
if use_floor:
v_hat_3p3 = [floor_divmul(v_i_3p3, th_i, round_only=True, use_log=False) for th_i in th]
v_hat_3p0 = [floor_divmul(v_i_3p0, th_i, round_only=True, use_log=False) for th_i in th]
else:
v_hat_3p3 = [round_divmul(v_i_3p3, th_i, round_only=True, use_log=False) for th_i in th]
v_hat_3p0 = [round_divmul(v_i_3p0, th_i, round_only=True, use_log=False) for th_i in th]
v_hat_3p3 = torch.cat(v_hat_3p3)
v_hat_3p0 = torch.cat(v_hat_3p0)
fig = plt.figure(figsize=(9, 12))
# fig = plt.figure(figsize=(11, 13),dpi=200)
ax = plt.subplot(111)
ax.set_xlim([-0.1,5.5])
# ax.set_ylim([-5,6.5])
ax.plot(th.detach().numpy(), v_hat_3p3.detach().numpy(), label=r'$\hat{x}_{2.3}$', color='c', linewidth=1.5)
ax.plot(th.detach().numpy(), v_hat_3p0.detach().numpy(), label=r'$\hat{x}_{2.4}$', color='m', linewidth=1.5)
ax.plot(th.detach().numpy(), v_hat_3p3.sub(v_hat_3p0).detach().numpy(), label=r'$\hat{x}_{2.3}-\hat{x}_{2.4}$', color='gray', linewidth=1.5, linestyle='dashed')
ax.set_xlabel(r'$s$', fontsize=fontsize)
ax.set_ylabel(r'$\hat{x}, \Delta \hat{x}$', fontsize=fontsize)
ax.legend(loc='upper left', fontsize=fontsize)
ax.grid()
ax.axis('equal')
fig.savefig('resources/' + 's_vs_vhat_round.png')
fig.savefig('resources/' + 's_vs_vhat_round.pdf')
if __name__ == "__main__":
# plot_act1()
# plot_act2()
# plot_act3()
# plot_act4()
plot_tanhshrink()