-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbgemm_linear.py
More file actions
813 lines (637 loc) · 28.7 KB
/
Copy pathbgemm_linear.py
File metadata and controls
813 lines (637 loc) · 28.7 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
import torch
import torch.nn as nn
import lowbit_kernel
import math
import numpy as np
__all__ = ['BNNLinear', 'BGEMMLinear', 'BGEMMLinear_elastic_signed', 'bgemm_matmul', 'bgemm_attn_matmul', 'BGEMMLinear_bireal', 'NNLinear_elastic_signed']
class bgemm_matmul(torch.autograd.Function):
@staticmethod
def forward(ctx, matA, matB, instruction='xor'):
"""
:param matA: matA to be binarized
:param matB: matB to be binarized
:param instruction: ['and', 'xor'], 'and' for {0,1} and 'xor' for {-1,1}
:return: calculated result
"""
matA = matA.to(torch.half)
matB = matB.to(torch.half)
assert matA.dtype == torch.half and matB.dtype == torch.half
ctx.save_for_backward(matA, matB)
INSTRUCTION = 0 if instruction=='and' else 1 # 1 by default
out = lowbit_kernel.bgemm_linear_forward_cuda(matA, matB.transpose(-1, -2), 1, INSTRUCTION)
return out
@staticmethod
def backward(ctx, grad_output):
matA, matB = ctx.saved_tensors
matA, matB = matA.sign(), matB.sign()
bs = matA.shape[0]
assert matB.dim() == 2, "matB dim must be 2, now is %d" % matB.dim()
if matA.dim() == 3:
matB_grad = torch.bmm(matA.transpose(1, 2), grad_output).transpose(1, 2) # checked
matA_grad = torch.bmm(grad_output, matB.repeat(bs, 1, 1)) # checked
elif matA.dim() == 2:
matB_grad = torch.mm(matA.T, grad_output).T # checked
matA_grad = torch.mm(grad_output, matB) # checked
return matA_grad, matB_grad, None
class bgemm_attn_matmul(torch.autograd.Function): # noqa
@staticmethod
def forward(ctx, input1, input2, instruction="attn_mm"): # attn, value
"""
:param input2: input2 to be binarized
:param input1: input1 to be binarized
:param instruction: ['and', 'xor'], 'and' for {0,1} and 'xor' for {-1,1}
:return: calculated result
"""
input1 = input1.to(torch.half)
input2 = input2.to(torch.half)
ctx.save_for_backward(input1, input2)
assert input2.dtype == torch.half and input1.dtype == torch.half
out = lowbit_kernel.bgemm_linear_forward_cuda(input2.transpose(-1, -2).contiguous(), input1, 1, 2).T # value, attn
return out
@staticmethod
def backward(ctx, grad_output):
grad_output = grad_output
input1, input2 = ctx.saved_tensors
input1 = (input1.sign() + 1)/2
input1 = torch.where(input1==0, torch.ones_like(input1), input1)
input2 = input2.sign()
bs = input2.shape[0]
assert input1.dim() == 2, "input1 dim must be 2, now is %d" % input1.dim()
# import pdb; pdb.set_trace()
if input2.dim() == 3:
input1_grad = torch.bmm(grad_output, input2.T).transpose(1, 2) / 2
input2_grad = torch.bmm(input1.transpose(1, 2).repeat(bs, 1, 1), grad_output)
elif input2.dim() == 2:
input1_grad = torch.mm(grad_output, input2.T) / 2
input2_grad = torch.mm(input1.T, grad_output)
return input1_grad, input2_grad, None
class mm(torch.autograd.Function): # noqa
@staticmethod
def forward(ctx, matB, matA, instruction="attn_mm"): # x, w
"""
:param matA: matA to be binarized
:param matB: matB to be binarized
:param instruction: ['and', 'xor'], 'and' for {0,1} and 'xor' for {-1,1}
:return: calculated result
"""
# matA = matA.to(torch.half)
# matB = matB.to(torch.half)
# assert matA.dtype == torch.half and matB.dtype == torch.half
# out = lowbit_kernel.bgemm_linear_forward_cuda(matA.transpose(-1, -2).contiguous(), matB, 1, 2).T # feat, (attn)
ctx.save_for_backward(matA, matB)
out = matB @ matA
return out
@staticmethod
def backward(ctx, grad_output):
grad_output = grad_output.float()
matA, matB = ctx.saved_tensors
bs = matA.shape[0]
assert matB.dim() == 2, "matB dim must be 2, now is %d" % matB.dim()
# import pdb; pdb.set_trace()
if matA.dim() == 3:
matB_grad = torch.bmm(matA.transpose(1, 2), grad_output).transpose(1, 2)
matA_grad = torch.bmm(grad_output, matB.repeat(bs, 1, 1))
elif matA.dim() == 2:
matA_grad = torch.mm(matB.T, grad_output)
matB_grad = torch.mm(grad_output, matA.T)
return matB_grad, matA_grad, None
class bgemm_linear(torch.autograd.Function):
@staticmethod
def forward(ctx, input, weight, instruction='xor'):
"""
:param input: input to be binarized
:param weight: weight to be binarized
:param instruction: ['and', 'xor'], 'and' for {0,1} and 'xor' for {-1,1}
:return: calculated result
"""
input = input.to(torch.half)
weight = weight.to(torch.half)
assert input.dtype == torch.half and weight.dtype == torch.half
ctx.save_for_backward(input, weight)
INSTRUCTION = 0 if instruction=='and' else 1 # 1 by default
out = lowbit_kernel.bgemm_linear_forward_cuda(input, weight, 1, INSTRUCTION)
return out
@staticmethod
def backward(ctx, grad_output):
input_, weight = ctx.saved_tensors
input_, weight = input_.sign(), weight.sign()
bs = input_.shape[0]
assert weight.dim() == 2, "weight dim must be 2, now is %d" % weight.dim()
# import pdb; pdb.set_trace()
if input_.dim() == 3:
weight_grad = torch.bmm(input_.transpose(1, 2), grad_output).transpose(1, 2) # checked
feat_grad = torch.bmm(grad_output, weight.repeat(bs, 1, 1)) # checked
elif input_.dim() == 2:
feat_grad = torch.mm(grad_output, weight)
weight_grad = torch.mm(input_.T, grad_output).T
return feat_grad, weight_grad, None
class bgemm_linear_bireal(torch.autograd.Function):
@staticmethod
def forward(ctx, input, weight, instruction='xor'):
"""
:param input: input to be binarized
:param weight: weight to be binarized
:param instruction: ['and', 'xor'], 'and' for {0,1} and 'xor' for {-1,1}
:return: calculated result
"""
input = input.to(torch.half)
weight = weight.to(torch.half)
assert input.dtype == torch.half and weight.dtype == torch.half
ctx.save_for_backward(input, weight)
INSTRUCTION = 0 if instruction=='and' else 1 # 1 by default
out = lowbit_kernel.bgemm_linear_forward_cuda(input, weight, 1, INSTRUCTION)
return out
@staticmethod
def backward(ctx, grad_output):
input, weight = ctx.saved_tensors
input_sign, weight_sign = input.sign(), weight.sign()
bs = input.shape[0]
assert weight.dim() == 2, "weight dim must be 2, now is %d" % weight.dim()
# import pdb; pdb.set_trace()
if input_sign.dim() == 3:
weight_grad = torch.bmm(input_sign.transpose(1, 2), grad_output).transpose(1, 2) # checked
feat_grad = torch.bmm(grad_output, weight_sign.repeat(bs, 1, 1)) # checked
elif input_sign.dim() == 2:
feat_grad = torch.mm(grad_output, weight_sign)
weight_grad = torch.mm(input_sign.T, grad_output).T
# STE
# feat_grad, weight_grad = feat_grad*1, weight_grad*1
# bireal
'''
dx = 2+2x -1<x<0
2-2x 0<x<1
0 otherwise
'''
x = input.abs()
x = torch.where(x > 1, torch.ones_like(x), x) # clip
dx = 2 - 2 * x
# np.save("/disk2/results/bibert/save_npy/save_backward/x_0.npy", input.detach().cpu().numpy())
# np.save("/disk2/results/bibert/save_npy/save_backward/dx_0.npy", dx.detach().cpu().numpy())
# print("saved x, dx in /disk2/results/bibert/save_npy/save_backward.")
feat_grad = feat_grad * dx
return feat_grad, weight_grad, None
def ternary(x, is_act):
# # norm = ((x - x.min())/ (x.max() - x.min())).to(torch.float32)
# # import pdb; pdb.set_trace()
# x = x.to(torch.float32)
# # quantile_0 = torch.quantile(x, 0.9)
# # quantile_1 = torch.quantile(x, 0.1)
# if is_act:
# quantile_0, quantile_1 = 1e-1, -1e-1
# else:
# quantile_0, quantile_1 = 1e-1, -1e-1
# # print("range: %.5f, %.5f" %(quantile_0, quantile_1))
# x = torch.where(x>=quantile_0, torch.ones_like(x), x)
# x = torch.where(x<=quantile_1, torch.ones_like(x)-2, x)
# x = torch.where(x.abs()!=1, torch.zeros_like(x), x)
# return torch.where(x.abs()<1e-3, torch.zeros_like(x), x.sign())
# return x
return x.sign()
class nn_linear_elastic_signed(torch.autograd.Function):
@staticmethod
def forward(ctx, input, weight, sa, sw, grad_scale, instruction="xor"):
input = input.clone()
weight = weight.clone()
# input -= 0.5
input -= input.mean().item()
weight -= weight.mean().item()
# eps = torch.tensor(0.00001).float().to(sa.device)
# sa = torch.where(sa > eps, sa, eps)
# sw = torch.where(sw > eps, sw, eps)
# import pdb; pdb.set_trace()
# assert sa > 0 and sw > 0, 'sa = {:.6f}, sw = {:.6f} '.format(sa, sw)
ctx.other = grad_scale
input = input / sa
# weight = weight / sw
ctx.save_for_backward(input, weight)
input = input.to(torch.half)
weight = weight.to(torch.half)
INSTRUCTION = 0 if instruction=='and' else 1 # 1 by default
# out = lowbit_kernel.bgemm_linear_forward_cuda(input, weight, 1, INSTRUCTION)
# input, weight = input.sign(), weight.sign()
input, weight = ternary(input, True), ternary(weight, False)
out = input @ weight.T
out = out * sa * sw
return out
@staticmethod
def backward(ctx, grad_output):
input, weight = ctx.saved_tensors
grad_scale = ctx.other
# input_sign, weight_sign = input.sign(), weight.sign()
input_sign, weight_sign = ternary(input, True), ternary(weight, False)
bs = input.shape[0]
assert weight.dim() == 2, "weight dim must be 2, now is %d" % weight.dim()
if input_sign.dim() == 3:
weight_grad = torch.bmm(input_sign.transpose(1, 2), grad_output).transpose(1, 2) # checked
feat_grad = torch.bmm(grad_output, weight_sign.repeat(bs, 1, 1)) # checked
elif input_sign.dim() == 2:
weight_grad = torch.mm(input_sign.T, grad_output).T # checked
feat_grad = torch.mm(grad_output, weight_sign) # checked
grad_input, grad_sa = calculate_gradient_signed(input, feat_grad, grad_scale, True)
_, grad_sw = calculate_gradient_signed(weight, weight_grad, grad_scale, False)
# import pdb; pdb.set_trace()
'''
# Constant function (STE)
feat_grad, weight_grad = feat_grad*1, weight_grad*1
'''
def ste(x):
return torch.where(x==1, torch.zeros_like(x), torch.ones_like(x))
'''
# Linear function
# dx = 2+2x -1<x<0
# 2-2x 0<x<1
# 0 otherwise
'''
def f_lin(x): # clip must be 1
return 2 - 2 * x
'''
# Quadratic function
# dx = -3/2 x^2 + 3/2 -1<x<1
# 0 otherwise
'''
def f_quad(x): # clip must be 1
return -3/2 * x.pow(2) + 3/2
def f1(x): # pass
# 向x正半轴和x负半轴收敛到0。关于y轴对称。经过(0,2)点。函数图像与x轴围成的面积存在极限,且极限为4
return 2 * torch.exp(-torch.abs(x))
def f2(x):
# 在x=0附近斜率为0,函数图像与x轴围成的面积存在极限,且极限为2√pi约为3.55
return 2 * (torch.exp(-x**2))
def sech2(x):
# tanh的导数
return 2 / torch.cosh(x)**2
clip = 1
x = input.abs()
x = torch.where(x > clip, torch.ones_like(x) * clip, x) # clip
dx = f2(x)
grad_input = grad_input * dx
x = weight.abs()
x = torch.where(x > clip, torch.ones_like(x) * clip, x) # clip
dx = f2(x)
weight_grad = weight_grad * dx
# np.save("/disk2/results/bibert/save_npy/save_backward/x_0.npy", input.detach().cpu().numpy())
# np.save("/disk2/results/bibert/save_npy/save_backward/dx_0.npy", dx.detach().cpu().numpy())
# print("saved x, dx in /disk2/results/bibert/save_npy/save_backward.")
# import pdb; pdb.set_trace()
return grad_input, weight_grad, grad_sa, grad_sw, None
# return feat_grad, weight_grad, None, None, None
class bgemm_linear_elastic_signed(torch.autograd.Function):
@staticmethod
def forward(ctx, input, weight, sa, sw, grad_scale, instruction="xor"):
input = input.clone()
weight = weight.clone()
# input -= 0.5
input -= input.mean().item()
weight -= weight.mean().item()
# eps = torch.tensor(0.00001).float().to(sa.device)
# sa = torch.where(sa > eps, sa, eps)
# sw = torch.where(sw > eps, sw, eps)
# import pdb; pdb.set_trace()
# assert sa > 0 and sw > 0, 'sa = {:.6f}, sw = {:.6f} '.format(sa, sw)
ctx.other = grad_scale
input = input / sa
# weight = weight / sw
ctx.save_for_backward(input, weight)
input = input.to(torch.half)
weight = weight.to(torch.half)
INSTRUCTION = 0 if instruction=='and' else 1 # 1 by default
out = lowbit_kernel.bgemm_linear_forward_cuda(input, weight, 1, INSTRUCTION)
# input, weight = input.sign(), weight.sign()
# out = input @ weight.T
out = out * sa * sw
return out
@staticmethod
def backward(ctx, grad_output):
input, weight = ctx.saved_tensors
grad_scale = ctx.other
input_sign, weight_sign = input.sign(), weight.sign()
bs = input.shape[0]
assert weight.dim() == 2, "weight dim must be 2, now is %d" % weight.dim()
if input_sign.dim() == 3:
weight_grad = torch.bmm(input_sign.transpose(1, 2), grad_output).transpose(1, 2) # checked
feat_grad = torch.bmm(grad_output, weight_sign.repeat(bs, 1, 1)) # checked
elif input_sign.dim() == 2:
weight_grad = torch.mm(input_sign.T, grad_output).T # checked
feat_grad = torch.mm(grad_output, weight_sign) # checked
grad_input, grad_sa = calculate_gradient_signed(input, feat_grad, grad_scale)
_, grad_sw = calculate_gradient_signed(weight, weight_grad, grad_scale)
# import pdb; pdb.set_trace()
'''
# Constant function (STE)
feat_grad, weight_grad = feat_grad*1, weight_grad*1
'''
'''
# Linear function
# dx = 2+2x -1<x<0
# 2-2x 0<x<1
# 0 otherwise
x = input.abs()
x = torch.where(x > 1, torch.ones_like(x), x) # clip
dx = 2 - 2 * x
grad_input = grad_input * dx
'''
def f_lin(x):
return 2 - 2 * x
'''
# Quadratic function
# dx = -3/2 x^2 + 3/2 -1<x<1
# 0 otherwise
x = input
x = torch.where(x.abs() > 1, torch.ones_like(x), x) # clip
dx = -3/2 * x.pow(2) + 3/2
grad_input = grad_input * dx
'''
def f_quad(x): # clip must be 1
return -3/2 * x.pow(2) + 3/2
def f1(x):
# 向x正半轴和x负半轴收敛到0。关于y轴对称。经过(0,2)点。函数图像与x轴围成的面积存在极限,且极限为4
return 2 * torch.exp(-torch.abs(x))
def f2(x):
# 在x=0附近斜率为0,函数图像与x轴围成的面积存在极限,且极限为2√pi约为3.55
return 2 * (torch.exp(-x**2))
def sech2(x):
# 与x轴围成的面积是4
return 2 / torch.cosh(x)**2
clip = 4
x = input.abs()
x = torch.where(x > clip, torch.ones_like(x) * clip, x) # clip
dx = sech2(x)
grad_input = grad_input * dx
x = weight.abs()
x = torch.where(x > clip, torch.ones_like(x) * clip, x) # clip
dx = sech2(x)
weight_grad = weight_grad * dx
# np.save("/disk2/results/bibert/save_npy/save_backward/x_0.npy", input.detach().cpu().numpy())
# np.save("/disk2/results/bibert/save_npy/save_backward/dx_0.npy", dx.detach().cpu().numpy())
# print("saved x, dx in /disk2/results/bibert/save_npy/save_backward.")
# import pdb; pdb.set_trace()
return grad_input, weight_grad, grad_sa, grad_sw, None
# return feat_grad, weight_grad, None, None, None
class BGEMMLinear(nn.Linear):
def __init__(self, in_channels, out_channels, bias=False):
super(BGEMMLinear, self).__init__(in_channels, out_channels, bias)
self.initialized = False
def _initialize(self, x, w):
assert not self.initialized, 'already initialized.'
self.sw = nn.Parameter(w.norm(1, 1).div(w.nelement()), requires_grad=True)
self.sa = nn.Parameter(2 * x.abs().mean() , requires_grad=True)
self.zpw = torch.mean(w).detach() # nn.Parameter(torch.mean(w), requires_grad=False)
self.zpa = torch.mean(x).detach() # nn.Parameter(torch.mean(x), requires_grad=False)
self.initialized = True
def forward(self, x):
assert x.shape[0] % 32 == 0, "x.shape[0] is %d" % x.shape[0]
if not self.initialized:
self._initialize(x, self.weight)
w = self.weight - self.zpw
x = x - self.zpa
# import pdb; pdb.set_trace()
out = bgemm_linear.apply(x, w) * self.sw * self.sa
if not self.bias is None:
out += self.bias.view(1, -1).expand_as(out)
return out
class BGEMMLinear_bireal(nn.Linear):
def __init__(self, in_channels, out_channels, bias=False):
super(BGEMMLinear_bireal, self).__init__(in_channels, out_channels, bias)
self.initialized = False
def _initialize(self, x, w):
assert not self.initialized, 'already initialized.'
self.sw = nn.Parameter(w.norm(1, 1).div(w.nelement()), requires_grad=True)
self.sa = nn.Parameter(2 * x.abs().mean() , requires_grad=True)
self.zpw = torch.mean(w).detach() # nn.Parameter(torch.mean(w), requires_grad=False)
self.zpa = torch.mean(x).detach() # nn.Parameter(torch.mean(x), requires_grad=False)
self.initialized = True
def forward(self, x):
assert x.shape[0] % 32 == 0, "x.shape[0] is %d" % x.shape[0]
if not self.initialized:
self._initialize(x, self.weight)
w = self.weight - self.zpw
x = x - self.zpa
out = bgemm_linear_bireal.apply(x, w) * self.sw * self.sa
if not self.bias is None:
out += self.bias.view(1, -1).expand_as(out)
return out
def calculate_gradient_signed(qx, grad, grad_scale, is_act):
# qx = x / sx
Qn, Qp = -1, 1
indicate_small = (qx < Qn).float()
indicate_big = (qx > Qp).float()
indicate_middle = 1.0 - indicate_small - indicate_big # this is more cpu-friendly than torch.ones(input_.shape)
# # import pdb; pdb.set_trace()
# grad_sx = ((indicate_small * Qn + indicate_big * Qp + indicate_middle * (-qx + qx.round())) * grad * grad_scale).sum(dim=-1).unsqueeze(dim=0)
# grad_sx = ((indicate_small * Qn + indicate_big * Qp + indicate_middle * (-qx + ternary(qx))) * grad * grad_scale).sum(dim=-1).unsqueeze(dim=0)
# grad_sx = ((qx.sign()) * grad * grad_scale).sum(dim=-1).unsqueeze(dim=0)
if not is_act:
grad_sx = (ternary(qx, is_act) * grad * grad_scale).sum(dim=-1).unsqueeze(dim=0)
else:
grad_sx = ((indicate_small * Qn + indicate_big * Qp + indicate_middle * (-qx + ternary(qx, is_act))) * grad * grad_scale).sum(dim=-1).unsqueeze(dim=0)
# grad_x = indicate_middle * grad
grad_x = grad
return grad_x, grad_sx
class BGEMMLinear_elastic_signed(nn.Linear):
def __init__(self, in_channels, out_channels, bias=True):
super(BGEMMLinear_elastic_signed, self).__init__(in_channels, out_channels, bias)
self.initialized = False
def _initialize(self, x, w):
assert not self.initialized, 'already initialized.'
self.sw = nn.Parameter(w.norm(1, 1).div(w.nelement()), requires_grad=True)
self.sa = nn.Parameter(2 * x.abs().mean() , requires_grad=True)
self.grad_scale = 1.0 / math.sqrt(x.numel())
self.zpw = torch.mean(w).detach() # nn.Parameter(torch.mean(w), requires_grad=False)
self.zpa = torch.mean(x).detach() # nn.Parameter(torch.mean(x), requires_grad=False)
self.initialized = True
def forward(self, x):
assert x.shape[0] % 32 == 0, "x.shape[0] is %d" % x.shape[0]
if not self.initialized:
self._initialize(x, self.weight)
# import pdb; pdb.set_trace()
out = bgemm_linear_elastic_signed.apply(x, self.weight, self.sa, self.sw, self.grad_scale)
if not self.bias is None:
out += self.bias.view(1, -1).expand_as(out)
return out
class NNLinear_elastic_signed(nn.Linear):
def __init__(self, in_channels, out_channels, bias=True):
super(NNLinear_elastic_signed, self).__init__(in_channels, out_channels, bias)
self.initialized = False
def _initialize(self, x, w):
assert not self.initialized, 'already initialized.'
self.sw = nn.Parameter(w.norm(1, 1).div(w.nelement()), requires_grad=True)
self.sa = nn.Parameter(2 * x.abs().mean() , requires_grad=True)
self.grad_scale = 1.0 / math.sqrt(x.numel())
self.zpw = torch.mean(w).detach() # nn.Parameter(torch.mean(w), requires_grad=False)
self.zpa = torch.mean(x).detach() # nn.Parameter(torch.mean(x), requires_grad=False)
self.initialized = True
def forward(self, x):
assert x.shape[0] % 32 == 0, "x.shape[0] is %d" % x.shape[0]
if not self.initialized:
self._initialize(x, self.weight)
# import pdb; pdb.set_trace()
out = nn_linear_elastic_signed.apply(x, self.weight, self.sa, self.sw, self.grad_scale)
if not self.bias is None:
out += self.bias.view(1, -1).expand_as(out)
return out
class BinActive(torch.autograd.Function):
'''
Binarize the input activations for ***** BNN and XNOR *****.
'''
@staticmethod
def forward(ctx, input):
# ctx.save_for_backward(input)
input = input.sign()
input = torch.where(input==0, torch.ones_like(input), input)
return input
@staticmethod
def backward(ctx, grad_output):
# input, = ctx.saved_tensors
grad_input = grad_output.clone()
# grad_input[input.ge(1)] = 0
# grad_input[input.le(-1)] = 0
return grad_input
class BNNLinear(nn.Linear):
def __init__(self, in_channels, out_channels, bias=False):
super(BNNLinear, self).__init__(in_channels, out_channels, bias)
self.initialized = False
def _initialize(self, x, w):
assert not self.initialized, 'already initialized.'
self.sw = nn.Parameter(w.norm(1, 1).div(w.nelement()), requires_grad=True)
self.sa = nn.Parameter(2 * x.abs().mean() , requires_grad=True)
self.zpw = torch.mean(w).detach() # nn.Parameter(torch.mean(w), requires_grad=False)
self.zpa = torch.mean(x).detach() # nn.Parameter(torch.mean(x), requires_grad=False)
self.initialized = True
def forward(self, x):
if not self.initialized:
self._initialize(x, self.weight)
w = self.weight - self.zpw
x = x - self.zpa
bw = BinActive().apply(w)
bx = BinActive().apply(x)
out = nn.functional.linear(bx, bw, self.bias) * self.sw * self.sa
# out = bx @ bw.T + self.bias
return out
class test_linear_fn(torch.autograd.Function):
@staticmethod
def forward(ctx, input, weight, instruction='xor'):
"""
:param input: input to be binarized
:param weight: weight to be binarized
:param instruction: ['and', 'xor'], 'and' for {0,1} and 'xor' for {-1,1}
:return: calculated result
"""
ctx.save_for_backward(input, weight)
INSTRUCTION = 0 if instruction=='and' else 1 # 1 by default
# out = lowbit_kernel.bgemm_linear_forward_cuda(input, weight, 1, INSTRUCTION)
out = input @ weight.T
return out
@staticmethod
def backward(ctx, grad_output):
input_, weight = ctx.saved_tensors
bs = input_.shape[0]
assert weight.dim() == 2, "weight dim must be 2, now is %d" % weight.dim()
if input_.dim() == 3:
weight_grad = torch.bmm(input_.transpose(1, 2), grad_output).transpose(1, 2) # checked
feat_grad = torch.bmm(grad_output, weight.repeat(bs, 1, 1)) # checked
elif input_.dim() == 2:
weight_grad = torch.mm(input_.T, grad_output).T # checked
feat_grad = torch.mm(grad_output, weight) # checked
return feat_grad, weight_grad, None
class TestLinear(nn.Linear):
def __init__(self, in_channels, out_channels, bias=False):
super(TestLinear, self).__init__(in_channels, out_channels, bias)
def forward(self, x):
w = self.weight
# bgemm
# out = bgemm_linear.apply(x, w)
# battn = BinActive.apply(x)
# bv = BinActive.apply(w)
# battn = (battn+1)/2
# battn = torch.where(battn == -1., torch.zeros_like(battn), battn)
# out = attn_matmul.apply(w.cuda(), x.cuda())
matA = w.cuda()
matB = x.cuda()
matA = BinActive.apply(matA)
matB = BinActive.apply(matB)
matB = (matB+1) / 2
# matB = torch.where(matB==0.5, torch.zeros_like(matB)+0.5, matB)
# out = matB @ matA
out = mm.apply(matB, matA)
return out
class TestLinear2(nn.Linear):
def __init__(self, in_channels, out_channels, bias=False):
super(TestLinear2, self).__init__(in_channels, out_channels, bias)
def forward(self, x):
w = self.weight
# bgemm
# out = bgemm_linear.apply(x, w)
out = bgemm_linear_bireal.apply(x.cuda(), w.cuda())
# out = torch.matmul(battn, bv).cuda()
# bnn
# x = BinActive.apply(x)
# w = BinActive.apply(w)
# out = nn.functional.linear(x, w)
# import pdb; pdb.set_trace()
return out
def test_linear():
torch.manual_seed(0)
x = torch.randn(32, 128).cuda()
y = torch.randn(32).cuda()
criterion = nn.BCEWithLogitsLoss()
lin1 = nn.Sequential(
nn.Linear(128, 128, False),
BNNLinear(128, 32, False),
)
lin1 = lin1.cuda()
out1 = lin1(x)
loss1 = criterion(out1.sum(dim=-1), y)
loss1.backward()
lin2 = nn.Sequential(
nn.Linear(128, 128, False),
BGEMMLinear_bireal(128, 32, False),
)
lin2 = lin2.cuda()
lin2[0].weight = nn.Parameter(lin1[0].weight.clone(), requires_grad=True)
lin2[1].weight = nn.Parameter(lin1[1].weight.clone(), requires_grad=True)
# lin2[2].weight = nn.Parameter(lin1[2].weight.clone(), requires_grad=True)
out2 = lin2(x)
loss2 = criterion(out2.sum(dim=-1), y)
# import pdb; pdb.set_trace()
loss2.backward()
import pdb; pdb.set_trace()
def test_matmul():
torch.manual_seed(0)
q = torch.randn(32, 768).cuda()
k = torch.randn(32, 768).cuda()
bq = torch.sign(q)
bk = torch.sign(k)
attn1 = torch.matmul(bq, bk.transpose(-1, -2))
attn2 = bgemm_matmul.apply(q, k.transpose(-1, -2))
import pdb; pdb.set_trace()
def test_attn():
torch.manual_seed(0)
criterion = nn.BCEWithLogitsLoss()
y = torch.randn(128).cuda()
x = torch.randn(128, 32).cuda()
lin1 = nn.Sequential(
nn.Linear(32, 128, False),
# nn.ReLU(),
nn.Identity(),
TestLinear(256, 128, False),
)
lin1.cuda()
res1 = lin1(x)
loss1 = criterion(res1.sum(dim=-1), y)
loss1.backward()
lin2 = nn.Sequential(
nn.Linear(32, 128, False),
# nn.ReLU(),
nn.Identity(),
TestLinear2(256, 128, False),
)
lin2.cuda()
lin2[0].weight = nn.Parameter(lin1[0].weight.clone(), requires_grad=True)
lin2[2].weight = nn.Parameter(lin1[2].weight.clone(), requires_grad=True)
res2 = lin2(x)
loss2 = criterion(res2.sum(dim=-1), y)
loss2.backward()
import pdb; pdb.set_trace()
# test_linear()
# test_matmul()
# test_attn()