-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
195 lines (175 loc) · 9.51 KB
/
test.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
##### test functions #####
import matplotlib.pyplot as plt
def test_k_means_quantize(
test_tensor=torch.tensor([
[-0.3747, 0.0874, 0.3200, -0.4868, 0.4404],
[-0.0402, 0.2322, -0.2024, -0.4986, 0.1814],
[ 0.3102, -0.3942, -0.2030, 0.0883, -0.4741],
[-0.1592, -0.0777, -0.3946, -0.2128, 0.2675],
[ 0.0611, -0.1933, -0.4350, 0.2928, -0.1087]]),
bitwidth=2):
def plot_matrix(tensor, ax, title, cmap=ListedColormap(['white'])):
ax.imshow(tensor.cpu().numpy(), vmin=-0.5, vmax=0.5, cmap=cmap)
ax.set_title(title)
ax.set_yticklabels([])
ax.set_xticklabels([])
for i in range(tensor.shape[1]):
for j in range(tensor.shape[0]):
text = ax.text(j, i, f'{tensor[i, j].item():.2f}',
ha="center", va="center", color="k")
fig, axes = plt.subplots(1,2, figsize=(8, 12))
ax_left, ax_right = axes.ravel()
print(test_tensor)
plot_matrix(test_tensor, ax_left, 'original tensor')
num_unique_values_before_quantization = test_tensor.unique().numel()
k_means_quantize(test_tensor, bitwidth=bitwidth)
num_unique_values_after_quantization = test_tensor.unique().numel()
print('* Test k_means_quantize()')
print(f' target bitwidth: {bitwidth} bits')
print(f' num unique values before k-means quantization: {num_unique_values_before_quantization}')
print(f' num unique values after k-means quantization: {num_unique_values_after_quantization}')
assert num_unique_values_after_quantization == min((1 << bitwidth), num_unique_values_before_quantization)
print('* Test passed.')
plot_matrix(test_tensor, ax_right, f'{bitwidth}-bit k-means quantized tensor', cmap='tab20c')
fig.tight_layout()
plt.show()
def test_linear_quantize(
test_tensor=torch.tensor([
[ 0.0523, 0.6364, -0.0968, -0.0020, 0.1940],
[ 0.7500, 0.5507, 0.6188, -0.1734, 0.4677],
[-0.0669, 0.3836, 0.4297, 0.6267, -0.0695],
[ 0.1536, -0.0038, 0.6075, 0.6817, 0.0601],
[ 0.6446, -0.2500, 0.5376, -0.2226, 0.2333]]),
quantized_test_tensor=torch.tensor([
[-1, 1, -1, -1, 0],
[ 1, 1, 1, -2, 0],
[-1, 0, 0, 1, -1],
[-1, -1, 1, 1, -1],
[ 1, -2, 1, -2, 0]], dtype=torch.int8),
real_min=-0.25, real_max=0.75, bitwidth=2, scale=1/3, zero_point=-1):
def plot_matrix(tensor, ax, title, vmin=0, vmax=1, cmap=ListedColormap(['white'])):
ax.imshow(tensor.cpu().numpy(), vmin=vmin, vmax=vmax, cmap=cmap)
ax.set_title(title)
ax.set_yticklabels([])
ax.set_xticklabels([])
for i in range(tensor.shape[0]):
for j in range(tensor.shape[1]):
datum = tensor[i, j].item()
if isinstance(datum, float):
text = ax.text(j, i, f'{datum:.2f}',
ha="center", va="center", color="k")
else:
text = ax.text(j, i, f'{datum}',
ha="center", va="center", color="k")
quantized_min, quantized_max = get_quantized_range(bitwidth)
fig, axes = plt.subplots(1,3, figsize=(10, 32))
plot_matrix(test_tensor, axes[0], 'original tensor', vmin=real_min, vmax=real_max)
_quantized_test_tensor = linear_quantize(
test_tensor, bitwidth=bitwidth, scale=scale, zero_point=zero_point)
_reconstructed_test_tensor = scale * (_quantized_test_tensor.float() - zero_point)
print('* Test linear_quantize()')
print(f' target bitwidth: {bitwidth} bits')
print(f' scale: {scale}')
print(f' zero point: {zero_point}')
assert _quantized_test_tensor.equal(quantized_test_tensor)
print('* Test passed.')
plot_matrix(_quantized_test_tensor, axes[1], f'2-bit linear quantized tensor',
vmin=quantized_min, vmax=quantized_max, cmap='tab20c')
plot_matrix(_reconstructed_test_tensor, axes[2], f'reconstructed tensor',
vmin=real_min, vmax=real_max, cmap='tab20c')
fig.tight_layout()
plt.show()
def test_quantized_fc(
input=torch.tensor([
[0.6118, 0.7288, 0.8511, 0.2849, 0.8427, 0.7435, 0.4014, 0.2794],
[0.3676, 0.2426, 0.1612, 0.7684, 0.6038, 0.0400, 0.2240, 0.4237],
[0.6565, 0.6878, 0.4670, 0.3470, 0.2281, 0.8074, 0.0178, 0.3999],
[0.1863, 0.3567, 0.6104, 0.0497, 0.0577, 0.2990, 0.6687, 0.8626]]),
weight=torch.tensor([
[ 1.2626e-01, -1.4752e-01, 8.1910e-02, 2.4982e-01, -1.0495e-01,
-1.9227e-01, -1.8550e-01, -1.5700e-01],
[ 2.7624e-01, -4.3835e-01, 5.1010e-02, -1.2020e-01, -2.0344e-01,
1.0202e-01, -2.0799e-01, 2.4112e-01],
[-3.8216e-01, -2.8047e-01, 8.5238e-02, -4.2504e-01, -2.0952e-01,
3.2018e-01, -3.3619e-01, 2.0219e-01],
[ 8.9233e-02, -1.0124e-01, 1.1467e-01, 2.0091e-01, 1.1438e-01,
-4.2427e-01, 1.0178e-01, -3.0941e-04],
[-1.8837e-02, -2.1256e-01, -4.5285e-01, 2.0949e-01, -3.8684e-01,
-1.7100e-01, -4.5331e-01, -2.0433e-01],
[-2.0038e-01, -5.3757e-02, 1.8997e-01, -3.6866e-01, 5.5484e-02,
1.5643e-01, -2.3538e-01, 2.1103e-01],
[-2.6875e-01, 2.4984e-01, -2.3514e-01, 2.5527e-01, 2.0322e-01,
3.7675e-01, 6.1563e-02, 1.7201e-01],
[ 3.3541e-01, -3.3555e-01, -4.3349e-01, 4.3043e-01, -2.0498e-01,
-1.8366e-01, -9.1553e-02, -4.1168e-01]]),
bias=torch.tensor([ 0.1954, -0.2756, 0.3113, 0.1149, 0.4274, 0.2429, -0.1721, -0.2502]),
quantized_bias=torch.tensor([ 3, -2, 3, 1, 3, 2, -2, -2], dtype=torch.int32),
shifted_quantized_bias=torch.tensor([-1, 0, -3, -1, -3, 0, 2, -4], dtype=torch.int32),
calc_quantized_output=torch.tensor([
[ 0, -1, 0, -1, -1, 0, 1, -2],
[ 0, 0, -1, 0, 0, 0, 0, -1],
[ 0, 0, 0, -1, 0, 0, 0, -1],
[ 0, 0, 0, 0, 0, 1, -1, -2]], dtype=torch.int8),
bitwidth=2, batch_size=4, in_channels=8, out_channels=8):
def plot_matrix(tensor, ax, title, vmin=0, vmax=1, cmap=ListedColormap(['white'])):
ax.imshow(tensor.cpu().numpy(), vmin=vmin, vmax=vmax, cmap=cmap)
ax.set_title(title)
ax.set_yticklabels([])
ax.set_xticklabels([])
for i in range(tensor.shape[0]):
for j in range(tensor.shape[1]):
datum = tensor[i, j].item()
if isinstance(datum, float):
text = ax.text(j, i, f'{datum:.2f}',
ha="center", va="center", color="k")
else:
text = ax.text(j, i, f'{datum}',
ha="center", va="center", color="k")
output = torch.nn.functional.linear(input, weight, bias)
quantized_weight, weight_scale, weight_zero_point = \
linear_quantize_weight_per_channel(weight, bitwidth)
quantized_input, input_scale, input_zero_point = \
linear_quantize_feature(input, bitwidth)
_quantized_bias, bias_scale, bias_zero_point = \
linear_quantize_bias_per_output_channel(bias, weight_scale, input_scale)
assert _quantized_bias.equal(_quantized_bias)
_shifted_quantized_bias = \
shift_quantized_linear_bias(quantized_bias, quantized_weight, input_zero_point)
assert _shifted_quantized_bias.equal(shifted_quantized_bias)
quantized_output, output_scale, output_zero_point = \
linear_quantize_feature(output, bitwidth)
_calc_quantized_output = quantized_linear(
quantized_input, quantized_weight, shifted_quantized_bias,
bitwidth, bitwidth,
input_zero_point, output_zero_point,
input_scale, weight_scale, output_scale)
assert _calc_quantized_output.equal(calc_quantized_output)
reconstructed_weight = weight_scale * (quantized_weight.float() - weight_zero_point)
reconstructed_input = input_scale * (quantized_input.float() - input_zero_point)
reconstructed_bias = bias_scale * (quantized_bias.float() - bias_zero_point)
reconstructed_calc_output = output_scale * (calc_quantized_output.float() - output_zero_point)
fig, axes = plt.subplots(3,3, figsize=(15, 12))
quantized_min, quantized_max = get_quantized_range(bitwidth)
plot_matrix(weight, axes[0, 0], 'original weight', vmin=-0.5, vmax=0.5)
plot_matrix(input.t(), axes[1, 0], 'original input', vmin=0, vmax=1)
plot_matrix(output.t(), axes[2, 0], 'original output', vmin=-1.5, vmax=1.5)
plot_matrix(quantized_weight, axes[0, 1], f'{bitwidth}-bit linear quantized weight',
vmin=quantized_min, vmax=quantized_max, cmap='tab20c')
plot_matrix(quantized_input.t(), axes[1, 1], f'{bitwidth}-bit linear quantized input',
vmin=quantized_min, vmax=quantized_max, cmap='tab20c')
plot_matrix(calc_quantized_output.t(), axes[2, 1], f'quantized output from quantized_linear()',
vmin=quantized_min, vmax=quantized_max, cmap='tab20c')
plot_matrix(reconstructed_weight, axes[0, 2], f'reconstructed weight',
vmin=-0.5, vmax=0.5, cmap='tab20c')
plot_matrix(reconstructed_input.t(), axes[1, 2], f'reconstructed input',
vmin=0, vmax=1, cmap='tab20c')
plot_matrix(reconstructed_calc_output.t(), axes[2, 2], f'reconstructed output',
vmin=-1.5, vmax=1.5, cmap='tab20c')
print('* Test quantized_fc()')
print(f' target bitwidth: {bitwidth} bits')
print(f' batch size: {batch_size}')
print(f' input channels: {in_channels}')
print(f' output channels: {out_channels}')
print('* Test passed.')
fig.tight_layout()
plt.show()