-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompression.py
More file actions
192 lines (154 loc) · 4.61 KB
/
Copy pathcompression.py
File metadata and controls
192 lines (154 loc) · 4.61 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
#Ivan Dimitrov
#Compression Techniques
import torch
import numpy as np
def examine_model(model):
num_child = 0
for child in model.children():
print ("Child: ", num_child)
print (child)
num_child +=1
def examine_parameter_structure(model):
num_child = 0
for child in model.children():
print ("Child: ", num_child)
print (child)
num_child +=1
def conv_weights_structure(model):
num_child = 0
for child in model.children():
if num_child == 4:
print (child)
for param in child.parameters():
print (param)
print ("Size of Param", param.size())
num_child +=1
def prune_model(model):
num_child = 0
for child in model.children():
if num_child == 4:
for param in child.parameters():
temp_size = list(param.size())
# print (temp_size)
if temp_size == [1024]:
#print (param)
simple_prune(param, 1024)
#print (param)
num_child +=1
def simple_prune(param, n):
print ("in simple prune")
#param.data[param.data < 0] = 0
acc = 0
for i in range(n):
if param.data[i] < .01 and param.data[i] > .01:
param.data[i] = 0
acc+=1
print ("Acc is: ", acc)
def collect_stats(model, collect_parama):
num_child = 0
t = 0
for child in model.children():
if num_child == 4:
for param in child.parameters():
print (t, flush=True)
t+=1
temp_size = list(param.size())
if (len(temp_size) == 1):
for i in range(temp_size[0]):
collect_parama.append(param.data[i])
else:
for i in range(temp_size[0]):
for j in range(temp_size[1]):
for k in range(temp_size[2]):
collect_parama.append(param.data[i][j][k])
if t == 20:
return collect_parama
num_child +=1
return collect_parama
def full_prune(model, threshold):
num_child = 0
num_of_parameters = 0
changed_params = 0
for child in model.children():
if num_child == 4:
num_of_parameters += sum(p.numel() for p in child.parameters() if p.requires_grad)
for param in child.parameters():
param.data[np.absolute(param.data) < threshold] = 0
changed_params += (param.numel() - param.nonzero().size(0))
num_child +=1
print ("Total Parameters: ", num_of_parameters)
print ("Changed Parameters: ", changed_params)
def layer_prune(model, threshold, layers):
""" Each conv block as weights + bias, that why we pretend there are 30 layers when really there are 15 in this model
"""
num_child = 0
num_of_parameters = 0
changed_params = 0
for child in model.children():
if num_child == 4:
curr_place = 0
num_of_parameters += sum(p.numel() for p in child.parameters() if p.requires_grad)
for param in child.parameters():
if curr_place in layers:
param.data[np.absolute(param.data) < threshold] = 0
changed_params += (param.numel() - param.nonzero().size(0))
curr_place+=1
num_child +=1
print ("Total Parameters: ", num_of_parameters)
print ("Changed Parameters: ", changed_params)
def mask_generation(model, threshold):
num_child = 0
for child in model.children():
if num_child == 4:
curr_place = 0
for param in child.parameters():
param.data[np.absolute(param.data) < threshold] = 0
temp_ten = param.clone()
temp_ten.detach()
temp_ten[temp_ten != 0] = 1
print (curr_place)
print (param)
print (param.size())
if curr_place%2 == 0:
model.convolutions[curr_place//2].bias_mask = temp_ten
else:
model.convolutions[curr_place//2].weight_mask = temp_ten
curr_place +=1
num_child +=1
def binarize_weights(model):
"""doesn't actually binarize -> but a side effect since almost all weights [-1, 1]
"""
num_child = 0
encoder = None
ecoder = None
for child in model.children():
if num_child == 0:
encoder = child
elif num_child == 1:
decoder = child
num_child+=1
for child in encoder.children():
for param in child.parameters():
param.data = torch.tensor(torch.tensor(param.data, dtype=torch.short), dtype=torch.float).cuda()
for child in decoder.children():
for param in child.parameters():
param.data = torch.tensor(torch.tensor(param.data, dtype=torch.short), dtype=torch.float).cuda()
def half_point_floating_point(model):
num_child = 0
encoder = None
decoder = None
for child in model.children():
if num_child == 0:
encoder = child
elif num_child == 1:
decoder = child
num_child+=1
num_child = 0
for child in encoder.children():
num_child += 1
for param in child.parameters():
param.data = torch.tensor(param.data, dtype=torch.half, requires_grad=True).cuda()
num_child = 0
for child in decoder.children():
for param in child.parameters():
param.data = torch.tensor(param.data, dtype=torch.half, requires_grad=True).cuda()