-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquantizer.py
37 lines (27 loc) · 1.15 KB
/
quantizer.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
from utils import *
prune = True
class quantizer:
def __init__(self, model, bits, uniform):
self.quantizedModel = self.quantizeModel(model, bits, uniform)
def quantizeModel(self, model, bits, uniform):
for layer in model.layers:
if not layer.weights:
continue
else:
length = 1
shape = layer.weights[0].shape
for l in shape:
length = l * length
w_vec = np.reshape(layer.weights[0].numpy(), (1, length))[0]
mean = np.mean(w_vec)
if prune:
w_vec[np.where((w_vec < 0.2) * (w_vec > 0))[0]] = 0
w_vec[np.where((w_vec > -0.2) * (w_vec < 0))[0]] = 0
partition, codebook = partition_codebook(w_vec, bits, uniform)
i, quantized = quantize(w_vec, partition, codebook)
w = np.reshape(quantized, shape)
if True:
mean_diff = np.mean(w) - mean
w = w - mean_diff
layer.set_weights([w, np.array(layer.weights[1].numpy())])
return model