-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompressors.py
826 lines (609 loc) · 25.8 KB
/
compressors.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
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
814
815
816
817
818
819
820
821
822
823
824
825
826
import torch
import struct
import numpy as np
# Following imports require the installation of custom PyTorch extensions.
# import bitpacking
# import gpu_bitpacking
class NoneCompressor:
"""
No compression.
"""
def __init__(self, device):
self._device = device
def compress(self, tensor):
compressed_tensor = tensor
compressed_tensor_size = torch.tensor(compressed_tensor.size(), device=self._device)
return compressed_tensor, compressed_tensor_size
def decompress(self, compressed_tensor, compressed_tensor_size):
unpadded_compressed_tensor = compressed_tensor[:compressed_tensor_size]
return unpadded_compressed_tensor
class QSGDCompressor:
"""
QSGD Compressor with Elias coding.
Code: Elias coded string is represented in 64 bit integers.
"""
def __init__(self, device, quantization_level=8):
self._device = device
self._quantization_level = quantization_level
self._sign_int_bit = 62
self._encode_dict = self.elias_dict()
def elias_dict(self):
s = (1 << self._quantization_level) - 1
keys = set(np.arange(0, s))
encode_dict = dict.fromkeys(keys)
for key in encode_dict:
encode_dict[key] = self.elias_encode(key)
return encode_dict
def compress(self, tensor):
s = (1 << self._quantization_level) - 1
norm = torch.norm(tensor)
sign_array = torch.sign(tensor)
sign_array *= -1
sign_array[sign_array == -1] = 0
sign_array = sign_array.to(dtype=torch.int8)
l_array = torch.abs(tensor) / norm * s
l_array_floored = l_array.to(dtype=torch.int)
prob_array = l_array - l_array_floored
prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
mask = torch.bernoulli(prob_array).to(torch.int)
xi_array = l_array_floored + mask
norm = norm / s
code = ""
code += self.float_to_bin(norm)
for sign, xi in zip(sign_array, xi_array):
code += str(sign.item())
code += self._encode_dict[xi.item()]
code_int_list = []
for i in range(len(code) // self._sign_int_bit + 1):
code_chunk = "1" + code[i * self._sign_int_bit : (i + 1) * self._sign_int_bit]
code_int_list.append(int(code_chunk, 2))
compressed_tensor = torch.tensor(code_int_list, dtype=torch.int64, device=self._device)
compressed_tensor_size = torch.tensor(compressed_tensor.size(), device=self._device)
return compressed_tensor, compressed_tensor_size
def decompress(self, compressed_tensor, compressed_tensor_size):
s = (1 << self._quantization_level) - 1
unpadded_compressed_tensor = compressed_tensor[:compressed_tensor_size]
code_int_list = unpadded_compressed_tensor.tolist()
code = ""
for ind, code_int in enumerate(code_int_list):
if ind == len(code_int_list) - 1:
code += bin(code_int)[3:]
continue
code += bin(code_int)[3:].zfill(self._sign_int_bit)
norm = self.bin_to_float(code[:32])
code = code[32:]
xi_list = []
sign_list = []
while code != "":
sign = int(code[0])
xi, code = self.elias_decode(code[1:])
sign_list.append(sign)
xi_list.append(xi)
norm = torch.tensor(norm) / s
sign_array = torch.tensor(sign_list)
xi_array = torch.tensor(xi_list)
sign_array[sign_array == 1] = -1
sign_array[sign_array == 0] = 1
return norm * sign_array * xi_array
def float_to_bin(self, num):
return format(struct.unpack("!I", struct.pack("!f", num))[0], "032b")
def bin_to_float(self, binary):
return struct.unpack("!f", struct.pack("!I", int(binary, 2)))[0]
def elias_encode(self, n):
elias_code = "0"
while n > 1:
binary = bin(n)[2:]
elias_code = binary + elias_code
n = len(binary) - 1
return elias_code
def elias_decode(self, elias_code):
n = 1
while elias_code[0] != "0":
m = int(elias_code[: n + 1], 2)
elias_code = elias_code[n + 1 :]
n = m
elias_code = elias_code[1:]
return n, elias_code
class QSGDWECCompressor:
"""
QSGD Compressor without Elias coding.
Code: norm, sign array, xi array.
"""
def __init__(self, device, quantization_level=8):
self._device = device
self._quantization_level = quantization_level
if quantization_level < 8:
self._dtype = torch.int8
else:
self._dtype = torch.int32
def compress(self, tensor):
s = (1 << self._quantization_level) - 1
# norm = torch.norm(tensor)
norm = tensor.abs().max()
sign_array = torch.sign(tensor).to(dtype=torch.int8)
l_array = torch.abs(tensor) / norm * s
l_array_floored = l_array.to(dtype=torch.int)
prob_array = l_array - l_array_floored
prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
mask = torch.bernoulli(prob_array)
xi_array = l_array_floored + mask
xi_array = xi_array.to(dtype=self._dtype)
norm = norm / s
return norm, sign_array, xi_array
def decompress(self, norm, sign_array, xi_array):
return norm * sign_array * xi_array
class QSGDWECModCompressor:
"""
Modified QSGD Compressor without Elias coding.
Code: norm, sign array * xi array.
"""
def __init__(self, device, quantization_level=8):
self._device = device
self._quantization_level = quantization_level
if quantization_level < 8:
self._dtype = torch.int8
else:
self._dtype = torch.int32
def compress(self, tensor):
s = (1 << self._quantization_level) - 1
# norm = torch.norm(tensor)
norm = tensor.abs().max()
sign_array = torch.sign(tensor).to(dtype=torch.int8)
l_array = torch.abs(tensor) / norm * s
l_array_floored = l_array.to(dtype=torch.int)
prob_array = l_array - l_array_floored
prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
mask = torch.bernoulli(prob_array)
xi_array = l_array_floored + mask
xi_array = xi_array.to(dtype=torch.int32)
sign_xi_array = (sign_array * xi_array).to(dtype=self._dtype, device=self._device)
norm = norm / s
return norm, sign_xi_array
def decompress(self, norm, sign_xi_array):
return norm * sign_xi_array
class TernGradCompressor:
"""
TernGrad Compressor.
Code: norm, sign array, b array.
"""
def __init__(self, device):
self._device = device
def compress(self, tensor):
scaler = tensor.abs().max()
sign_array = torch.sign(tensor).to(dtype=torch.int8)
prob_array = torch.abs(tensor) / scaler
prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
b_array = torch.bernoulli(prob_array).to(torch.int8)
return scaler, sign_array, b_array
def decompress(self, scaler, sign_array, b_array):
return scaler * sign_array * b_array
class TernGradModCompressor:
"""
TernGrad Compressor.
Code: norm, sign array * b array.
"""
def __init__(self, device):
self._device = device
def compress(self, tensor):
scaler = tensor.abs().max()
sign_array = torch.sign(tensor).to(dtype=torch.int8)
prob_array = torch.abs(tensor) / scaler
prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
b_array = torch.bernoulli(prob_array).to(torch.int8)
sign_b_array = sign_array * b_array
return scaler, sign_b_array
def decompress(self, scaler, sign_b_array):
return scaler * sign_b_array
class QSGDMaxNormCompressor:
"""
Modified QSGD Compressor without Elias coding.
Normalizing with max norm among thw workers.
Code: sign array * xi array.
"""
def __init__(self, device, quantization_level=8):
self._device = device
self._quantization_level = quantization_level
if quantization_level < 8:
self._dtype = torch.int8
else:
self._dtype = torch.int32
def compress(self, norm, tensor):
s = (1 << self._quantization_level) - 1
sign_array = torch.sign(tensor).to(dtype=torch.int8)
l_array = torch.abs(tensor) / norm * s
l_array_floored = l_array.to(dtype=torch.int32)
prob_array = l_array - l_array_floored
prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
mask = torch.bernoulli(prob_array)
xi_array = l_array_floored + mask
xi_array = xi_array.to(dtype=torch.int32)
sign_xi_array = (sign_array * xi_array).to(dtype=self._dtype, device=self._device)
return sign_xi_array
def decompress(self, norm, sign_xi_array):
s = (1 << self._quantization_level) - 1
return norm / s * sign_xi_array
"""
QSGDBPCompressor and QSGDBPAllReduceCompressor requires installation of custom PyTorch extensions
"""
# class QSGDBPCompressor:
# """
# Modified QSGD Compressor without Elias coding.
# Bit packing greedily in four modes.
# Code: norm, sign_packed, xi_packed, xi_size
# """
#
# def __init__(self, device, quantization_level=8):
# self._device = device
# self._quantization_level = quantization_level
#
# def compress(self, tensor):
# s = (1 << self._quantization_level) - 1
#
# # norm = torch.norm(tensor)
# norm = tensor.abs().max()
#
# sign_array = torch.sign(tensor).to(dtype=torch.int32)
# sign_array *= -1
# sign_array[sign_array == -1] = 0
#
# l_array = torch.abs(tensor) / norm * s
# l_array_floored = l_array.to(dtype=torch.int)
# prob_array = l_array - l_array_floored
# prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
#
# mask = torch.bernoulli(prob_array)
# xi_array = l_array_floored + mask
# xi_array = xi_array.to(dtype=torch.int32)
#
# sign_packed = bitpacking.packing(sign_array.to("cpu")).to(device=self._device)
# xi_packed = bitpacking.packing(xi_array.to("cpu")).to(device=self._device)
# xi_size = torch.tensor(xi_packed.size(), device=self._device)
#
# # sign_packed = gpu_bitpacking.packing(sign_array)
# # xi_packed = gpu_bitpacking.packing(xi_array)
# # xi_size = torch.tensor(xi_packed.size(), device=self._device)
#
# norm = norm / s
#
# return norm, sign_packed, xi_packed, xi_size
#
# def decompress(self, norm, sign_packed, xi_packed, tensor_size):
# sign_array = bitpacking.unpacking(sign_packed.to("cpu")).to(device=self._device)
# sign_array = sign_array[:tensor_size]
# xi_array = bitpacking.unpacking(xi_packed.to("cpu")).to(device=self._device)
# xi_array = xi_array[:tensor_size]
#
# sign_array[sign_array == 1] = -1
# sign_array[sign_array == 0] = 1
#
# return norm * sign_array * xi_array
#
#
# class QSGDBPAllReduceCompressor:
# """
# Modified QSGD Compressor without Elias coding.
# Normalizing with max norm among thw workers.
# Bit packing eight ints in 64bits to allreduce.
# Code: sign_xi_packed
# """
#
# def __init__(self, device, quantization_level=8):
# self._device = device
# self._quantization_level = quantization_level
#
# def compress(self, norm, tensor):
# s = (1 << self._quantization_level) - 1
#
# sign_array = torch.sign(tensor).to(dtype=torch.int8)
#
# l_array = torch.abs(tensor) / norm * s
# l_array_floored = l_array.to(dtype=torch.int32)
# prob_array = l_array - l_array_floored
# prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
#
# mask = torch.bernoulli(prob_array)
# xi_array = l_array_floored + mask
# xi_array = xi_array.to(dtype=torch.int32)
#
# sign_xi_array = sign_array * xi_array
# sign_xi_packed = bitpacking.packing(sign_xi_array.to("cpu")).to(device=self._device)
#
# return sign_xi_packed
#
# def decompress(self, norm, sign_xi_array):
# s = (1 << self._quantization_level) - 1
# sign_xi_unpacked = bitpacking.unpacking(sign_xi_array.to("cpu")).to(device=self._device)
#
# return norm / s * sign_xi_unpacked
class GlobalRandKMaxNormCompressor:
"""
RandK compressor with max norm.
Normalizing with max norm among thw workers.
Code: sign array * xi array.
"""
def __init__(self, device, quantization_level=8):
self._device = device
self._quantization_level = quantization_level
if quantization_level < 8:
self._dtype = torch.int8
else:
self._dtype = torch.int32
def compress(self, norm, tensor):
s = (1 << self._quantization_level) - 1
sign_array = torch.sign(tensor).to(dtype=torch.int8)
l_array = torch.abs(tensor) / norm * s
l_array_floored = l_array.to(dtype=torch.int32)
prob_array = l_array - l_array_floored
prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
mask = torch.bernoulli(prob_array)
xi_array = l_array_floored + mask
xi_array = xi_array.to(dtype=torch.int32)
sign_xi_array = (sign_array * xi_array).to(dtype=self._dtype, device=self._device)
return sign_xi_array
def decompress(self, norm, sign_xi_array):
s = (1 << self._quantization_level) - 1
return norm / s * sign_xi_array
class NUQSGDModCompressor:
"""
Non uniform QSGD Compressor without encoding.
Code: norm, sign array * xi array.
"""
def __init__(self, device, quantization_level=8):
self._device = device
self._quantization_level = quantization_level
if quantization_level < 8:
self._dtype = torch.int8
else:
self._dtype = torch.int32
def compress(self, tensor):
s = 1 << self._quantization_level
norm = torch.norm(tensor)
sign_array = torch.sign(tensor).to(dtype=torch.int8)
r_array = torch.abs(tensor) / norm * s
floored_log2 = torch.floor(torch.log2(r_array))
floored_log2[floored_log2 < 0] = -float("inf")
lsr = torch.pow(2, floored_log2)
lsr_1 = torch.pow(2, floored_log2 + 1)
lsr_1[lsr_1 == 0] = 1
prob_array = (r_array - lsr) / (lsr_1 - lsr)
prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
mask = torch.bernoulli(prob_array)
h_array = (1 - mask) * lsr + mask * lsr_1
h_array = h_array.to(dtype=torch.int32)
sign_h_array = (sign_array * h_array).to(dtype=self._dtype, device=self._device)
norm = norm / s
return norm, sign_h_array
def decompress(self, norm, sign_h_array):
return norm * sign_h_array
class NUQSGDMaxNormCompressor:
"""
Modified Non uniform QSGD Compressor without encoding.
Normalizing with max norm among thw workers.
Code: sign array * xi array.
"""
def __init__(self, device, quantization_level=8):
self._device = device
self._quantization_level = quantization_level
if quantization_level < 8:
self._dtype = torch.int8
else:
self._dtype = torch.int32
def compress(self, norm, tensor):
s = 1 << self._quantization_level
sign_array = torch.sign(tensor).to(dtype=torch.int8)
r_array = torch.abs(tensor) / norm * s
floored_log2 = torch.floor(torch.log2(r_array))
floored_log2[floored_log2 < 0] = -float("inf")
lsr = torch.pow(2, floored_log2)
lsr_1 = torch.pow(2, floored_log2 + 1)
lsr_1[lsr_1 == 0] = 1
prob_array = (r_array - lsr) / (lsr_1 - lsr)
prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
mask = torch.bernoulli(prob_array)
h_array = (1 - mask) * lsr + mask * lsr_1
h_array = h_array.to(dtype=torch.int32)
sign_h_array = (sign_array * h_array).to(dtype=self._dtype, device=self._device)
return sign_h_array
def decompress(self, norm, sign_h_array):
s = 1 << self._quantization_level
return norm / s * sign_h_array
class QSGDMaxNormBiasedCompressor:
"""
Modified QSGD Compressor without Elias coding and randomized rounding.
Normalizing with max norm among thw workers.
Code: sign array * xi array.
"""
def __init__(self, device, quantization_level=8):
self._device = device
self._quantization_level = quantization_level
if quantization_level < 8:
self._dtype = torch.int8
else:
self._dtype = torch.int32
def compress(self, norm, tensor):
s = (1 << self._quantization_level) - 1
l_array = tensor / norm * s
l_array_floored = l_array.to(dtype=self._dtype, device=self._device)
return l_array_floored
def decompress(self, norm, l_array_floored):
s = (1 << self._quantization_level) - 1
return norm / s * l_array_floored
class NUQSGDMaxNormBiasedCompressor:
"""
Modified Non uniform QSGD Compressor without encoding.
Normalizing with max norm among thw workers.
Code: sign array * xi array.
"""
def __init__(self, device, quantization_level=8):
self._device = device
self._quantization_level = quantization_level
if quantization_level < 8:
self._dtype = torch.int8
else:
self._dtype = torch.int32
def compress(self, norm, tensor):
s = 1 << self._quantization_level
sign_array = torch.sign(tensor).to(dtype=torch.int8)
r_array = torch.abs(tensor) / norm * s
floored_log2 = torch.floor(torch.log2(r_array))
floored_log2[floored_log2 < 0] = -float("inf")
lsr = torch.pow(2, floored_log2)
l_array_floored = (sign_array * lsr).to(dtype=self._dtype, device=self._device)
return l_array_floored
def decompress(self, norm, l_array_floored):
s = 1 << self._quantization_level
return norm / s * l_array_floored
class QSGDMaxNormTwoScaleCompressor:
"""
QSGD MaxNorm Compressor with two scale compression.
Normalizing with max norm among thw workers.
Calculates common low resolution masks, and returns two scale vector
Code: sign array * xi array.
"""
def __init__(self, device, lower_quantization_level=6, higher_quantization_level=10):
self._device = device
self._lower_quantization_level = lower_quantization_level
self._higher_quantization_level = higher_quantization_level
if lower_quantization_level < 8:
self._dtype = torch.int8
else:
self._dtype = torch.int32
def compress_lower(self, norm, tensor):
s = (1 << self._lower_quantization_level) - 1
sign_array = torch.sign(tensor).to(dtype=torch.int8)
l_array = torch.abs(tensor) / norm * s
l_array_floored = l_array.to(dtype=torch.int32)
prob_array = l_array - l_array_floored
prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
mask = torch.bernoulli(prob_array)
xi_array = l_array_floored + mask
xi_array = xi_array.to(dtype=torch.int32)
sign_xi_array = (sign_array * xi_array).to(dtype=self._dtype, device=self._device)
return sign_xi_array
def compress_higher(self, norm, tensor):
s_lower = (1 << self._lower_quantization_level) - 1
s_higher = (1 << self._higher_quantization_level) - 1
sign_array = torch.sign(tensor).to(dtype=torch.int8)
l_array = torch.abs(tensor) / norm * s_higher
l_array_floored = l_array.to(dtype=torch.int32)
prob_array = l_array - l_array_floored
prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
mask = torch.bernoulli(prob_array)
xi_array = l_array_floored + mask
xi_array = xi_array.to(dtype=torch.int32)
higher_resolution_mask = (xi_array <= s_lower).to(torch.int8)
sign_xi_array = (sign_array * xi_array).to(dtype=self._dtype, device=self._device)
return sign_xi_array, higher_resolution_mask
def decompress(self, norm, sign_xi_array, higher_resolution_mask):
s_lower = (1 << self._lower_quantization_level) - 1
s_higher = (1 << self._higher_quantization_level) - 1
decompressed_lower_scale = norm / s_lower * sign_xi_array
decompressed_higher_scale = norm / s_higher * sign_xi_array
decompressed_tensor = (
higher_resolution_mask * decompressed_higher_scale
+ (1 - higher_resolution_mask) * decompressed_lower_scale
)
return decompressed_tensor
class GlobalRandKMaxNormTwoScaleCompressor:
"""
Global RandK MaxNorm Compressor with two scale compression.
Normalizing with max norm among thw workers.
Calculates common low resolution masks, and returns two scale vector
Code: sign array * xi array.
"""
def __init__(self, device, lower_quantization_level=6, higher_quantization_level=10):
self._device = device
self._lower_quantization_level = lower_quantization_level
self._higher_quantization_level = higher_quantization_level
if lower_quantization_level < 8:
self._dtype = torch.int8
else:
self._dtype = torch.int32
def compress_lower(self, norm, tensor):
s = (1 << self._lower_quantization_level) - 1
sign_array = torch.sign(tensor).to(dtype=torch.int8)
l_array = torch.abs(tensor) / norm * s
l_array_floored = l_array.to(dtype=torch.int32)
prob_array = l_array - l_array_floored
prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
mask = torch.bernoulli(prob_array)
xi_array = l_array_floored + mask
xi_array = xi_array.to(dtype=torch.int32)
sign_xi_array = (sign_array * xi_array).to(dtype=self._dtype, device=self._device)
return sign_xi_array
def compress_higher(self, norm, tensor):
s_lower = (1 << self._lower_quantization_level) - 1
s_higher = (1 << self._higher_quantization_level) - 1
sign_array = torch.sign(tensor).to(dtype=torch.int8)
l_array = torch.abs(tensor) / norm * s_higher
l_array_floored = l_array.to(dtype=torch.int32)
prob_array = l_array - l_array_floored
prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
mask = torch.bernoulli(prob_array)
xi_array = l_array_floored + mask
xi_array = xi_array.to(dtype=torch.int32)
higher_resolution_mask = (xi_array <= s_lower).to(torch.int8)
sign_xi_array = (sign_array * xi_array).to(dtype=self._dtype, device=self._device)
return sign_xi_array, higher_resolution_mask
def decompress(self, norm, sign_xi_array, higher_resolution_mask):
s_lower = (1 << self._lower_quantization_level) - 1
s_higher = (1 << self._higher_quantization_level) - 1
decompressed_lower_scale = norm / s_lower * sign_xi_array
decompressed_higher_scale = norm / s_higher * sign_xi_array
decompressed_tensor = (
higher_resolution_mask * decompressed_higher_scale
+ (1 - higher_resolution_mask) * decompressed_lower_scale
)
return decompressed_tensor
class QSGDMaxNormMultiScaleCompressor:
"""
QSGD MaxNorm Compressor with Multi scale compression.
Normalizing with max norm among thw workers.
Calculates common low resolution masks, and returns two scale vector
Code: sign array * xi array.
"""
def __init__(self, device, quantization_levels=None):
self._device = device
if not quantization_levels:
quantization_levels = [6, 10]
quantization_levels.sort()
self._quantization_levels = quantization_levels
if quantization_levels[0] < 8:
self._dtype = torch.int8
else:
self._dtype = torch.int32
self._cache = None
def compress_cache(self, norm, tensor):
if not self._cache:
self._cache = torch.zeros(len(self._quantization_levels), tensor.size(0), device=self._device)
for ind, quantization_level in enumerate(self._quantization_levels):
s = (1 << quantization_level) - 1
sign_array = torch.sign(tensor).to(dtype=torch.int8)
l_array = torch.abs(tensor) / norm * s
l_array_floored = l_array.to(dtype=torch.int32)
prob_array = l_array - l_array_floored
prob_array = torch.clamp(prob_array, min=0.0, max=1.0)
mask = torch.bernoulli(prob_array)
xi_array = l_array_floored + mask
xi_array = xi_array.to(dtype=torch.int32)
sign_xi_array = sign_array * xi_array # ).to(device=self._device)
self._cache[ind] = sign_xi_array
def compress_mask(self, norm, tensor):
MAX_VAL = 2 ** self._quantization_levels[0] - 1
self.compress_cache(norm, tensor)
resolution_mask = torch.zeros_like(tensor, dtype=torch.int8)
for ind in range(len(self._quantization_levels)):
resolution_mask[self._cache[ind].abs() <= MAX_VAL] = ind
return resolution_mask
def compress(self, resolution_mask):
sign_xi_array = torch.zeros_like(resolution_mask, dtype=torch.float32)
for ind in range(len(self._quantization_levels)):
sign_xi_array[resolution_mask == ind] = self._cache[ind][resolution_mask == ind]
sign_xi_array = sign_xi_array.to(dtype=self._dtype, device=self._device)
return sign_xi_array
def decompress(self, norm, sign_xi_array, resolution_mask):
decompressed_tensor = torch.zeros_like(self._cache[0], dtype=torch.float32)
for ind, quantization_level in enumerate(self._quantization_levels):
s = (1 << quantization_level) - 1
decompressed_tensor[resolution_mask == ind] = sign_xi_array[resolution_mask == ind] * norm / s
return decompressed_tensor