-
Notifications
You must be signed in to change notification settings - Fork 38
/
compression_utils.py
152 lines (102 loc) · 3.46 KB
/
compression_utils.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
from functools import partial
import torch
import numpy as np
import copy
device = 'cuda' if torch.cuda.is_available() else 'cpu'
def approx_v(T, p, frac):
if frac < 1.0:
n_elements = T.numel()
n_sample = min(int(max(np.ceil(n_elements * frac), np.ceil(100/p))), n_elements)
n_top = int(np.ceil(n_sample*p))
if n_elements == n_sample:
i = 0
else:
i = np.random.randint(n_elements-n_sample)
topk, _ = torch.topk(T.flatten()[i:i+n_sample], n_top)
if topk[-1] == 0.0 or topk[-1] == T.max():
return approx_v(T, p, 1.0)
else:
n_elements = T.numel()
n_top = int(np.ceil(n_elements*p))
topk, _ = torch.topk(T.flatten(), n_top)
return topk[-1], topk
def none(T, hp):
'''
Identity
'''
return T
def dgc(T, hp):
'''
"Deep Gradient Compression: Reducing the communication Bandwidth for Distributed Training, Lin et al."
'''
hp_ = {'p' : 0.001, 'approx' : 1.0}
hp_.update(hp)
if hp_['p'] >= 1.0:
return T
T_abs = torch.abs(T)
v, _ = approx_v(T_abs, hp_["p"], hp_["approx"])
out = torch.where(T_abs >= v, T, torch.Tensor([0.0]).to(device))
return out
def stc(T, hp):
'''
"Sparse Binary Compression: Towards Distributed Deep Learning with minimal Communication, Sattler et al."
'''
hp_ = {'p' : 0.001, 'approx' : 1.0}
hp_.update(hp)
T_abs = torch.abs(T)
v, topk = approx_v(T_abs, hp_["p"], hp_["approx"])
mean = torch.mean(topk)
out_ = torch.where(T >= v, mean, torch.Tensor([0.0]).to(device))
out = torch.where(T <= -v, -mean, out_)
return out
def signsgd(T, hp):
"""
signSGD: Compressed Optimisation for non-convex Problems, Bernstein et al.
"""
return T.sign()
def compression_function(name, hp=None):
'''
Returns a function that maps a tensor to a tensor of the same shape
'''
return partial(globals()[name], hp=hp)
###############################################################################################
# COUNTING BITS
###############################################################################################
def get_bits(T, compression_method, approx=False):
"""
Returns the number of bits that are required to communicate the Tensor T, which was compressed with compresion_method
"""
B_val = {"none" : 32, "dgc" : 32, "stc" : 1, "signsgd" : 1}[compression_method]
# dense methods
if compression_method in ["none", "signsgd"]:
k = T.numel()
B_pos = 0
# sparse methods non-optimal encoding
elif compression_method in ["dgc"]:
k = torch.sum(T!=0.0).item()
B_pos = 16
# sparse methods golomb encoding
elif compression_method in ["stc"]:
k = torch.sum(T!=0.0).item()
N = T.numel()
q = (k+1)/(N+1)
golden = (np.sqrt(5)+1)/2
if q == 1:
return B_val*T.numel()
if q == 0:
return 0
b_star = 1 + np.floor(np.log2(np.log(golden-1)/np.log(1-q)))
if approx:
B_pos = b_star + 1/(1-(1-q)**(2**b_star)) + 1
else:
idc = torch.nonzero(T.view(-1))
distances = idc[:]-torch.cat([torch.Tensor([[-1]]).long().to("cuda"),idc[:-1]])
B_pos = torch.mean(torch.ceil(distances.float()/2**b_star)).item()+(b_star+1)
b_total = (B_pos+B_val)*k
return b_total
def get_update_size(dW, compression_method):
"""
Returns the number of bits that are required to communicate the entire network dW, which was compressed with compresion_method
"""
update_size = sum([get_bits(T, compression_method[0]) for T in dW.values()])
return update_size