forked from openvinotoolkit/nncf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_binarize_layers.py
267 lines (226 loc) · 8.91 KB
/
benchmark_binarize_layers.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
"""
Copyright (c) 2022 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from typing import Any
import torch
from torch import nn
from nncf.torch.binarization.layers import XNORBinarize, DOREFABinarize, ActivationBinarizationScaleThreshold
from tools.benchmark import run_profile
from nncf.torch.quantization.layers import get_per_channel_scale_shape
NBITS = 8
GPU_RUNS_LOW_BATCH = 10000
GPU_RUNS_HIGH_BATCH = 100
CPU_RUNS = 100
LOW_BATCH_INPUT_SIZE = [1, 96, 112, 112]
HIGH_BATCH_INPUT_SIZE = [128, 96, 112, 112]
TEST_PARAMS_STRUCT = [("low batch", LOW_BATCH_INPUT_SIZE, GPU_RUNS_LOW_BATCH),
("high batch", HIGH_BATCH_INPUT_SIZE, GPU_RUNS_HIGH_BATCH)]
# reference impl
# pylint:disable=abstract-method
class ReferenceXNORBinarize(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
norm = x.abs().mean([1, 2, 3], keepdim=True)
sign = ((x > 0).type(x.dtype) * 2 - 1)
output = sign * norm
return output
@staticmethod
def backward(ctx: Any, *grad_outputs: Any) -> Any:
return grad_outputs[0]
# pylint:disable=abstract-method
class ReferenceDOREFABinarize(torch.autograd.Function):
@staticmethod
def forward(ctx, x):
norm = x.abs().mean()
sign = ((x > 0).type(x.dtype) * 2 - 1)
output_flat = sign * norm
return output_flat.view_as(x)
@staticmethod
def backward(ctx: Any, *grad_outputs: Any) -> Any:
return grad_outputs[0]
# pylint:disable=abstract-method
class ReferenceActivationBinarize(torch.autograd.Function):
@staticmethod
def forward(ctx, input_, scale, threshold):
shape = [1 for s in input_.shape]
shape[1] = input_.shape[1]
t = (threshold*scale).view(shape)
output = (input_ > t).type(input_.dtype) * scale
ctx.save_for_backward(input_, scale, output)
return output
@staticmethod
def backward(ctx: Any, *grad_outputs: Any) -> Any:
grad_output = grad_outputs[0]
input_, scale, output = ctx.saved_variables
# calc gradient for input
mask_lower = (input_ <= scale).type(input_.dtype)
grad_input = grad_output * (input_ >= 0).type(input_.dtype) * mask_lower
# calc gradient for scale
err = (output - input_) * scale.reciprocal()
grad_scale = grad_output * (mask_lower * err + (1 - mask_lower))
grad_scale = grad_scale.sum().view(1)
# calc gradient for threshold
grad_threshold = -grad_output * (input_ > 0).type(input_.dtype) * (input_ < scale).type(input_.dtype)
for idx, _ in enumerate(input_.shape):
if idx != 1: # sum over all dims except activations channel
grad_threshold = grad_threshold.sum(idx, keepdim=True)
return grad_input, grad_scale, grad_threshold
class ReferenceWeightBinarizationModule(nn.Module):
def __init__(self, mode='xnor'):
super().__init__()
self.mode = mode
if self.mode == 'xnor':
self.binarize = ReferenceXNORBinarize.apply
elif self.mode == 'dorefa':
self.binarize = ReferenceDOREFABinarize.apply
def forward(self, input_):
return self.binarize(input_)
def get_test_scale(num_channels):
torch.manual_seed(0)
retval = torch.Tensor(num_channels)
retval.random_(0, 1)
return retval
def get_test_threshold(input_shape):
torch.manual_seed(0)
threshold_shape = get_per_channel_scale_shape(input_shape, is_weights=False)
retval = torch.Tensor(torch.zeros(threshold_shape))
retval.random_(-10, 10)
return retval
class ReferenceActivationBinarizationModule(nn.Module):
def __init__(self, input_shape):
super().__init__()
self.input_shape = input_shape
self.scale = torch.nn.Parameter(get_test_scale(num_channels=1))
self.threshold = torch.nn.Parameter(get_test_threshold(input_shape))
def forward(self, input_):
return ReferenceActivationBinarize.apply(input_, self.scale, self.threshold)
if __name__ == '__main__':
for input_name, input_size, gpu_runs in TEST_PARAMS_STRUCT:
print()
print("CUDA " + input_name)
print("------------------------------------------------")
print("Pytorch XNOR weight binarization (cuda 0) impl:")
print("input size: {0}".format(input_size))
run_profile(
ReferenceWeightBinarizationModule('xnor').cuda(),
input_size,
'cuda',
gpu_runs,
forward_only=True)
print()
print("Custom XNOR weight binarization (cuda 0) impl:")
print("input size: {0}".format(input_size))
run_profile(
XNORBinarize(enabled=True).cuda(),
input_size,
'cuda',
gpu_runs,
forward_only=True)
print()
print("Pytorch DoReFa weight binarization (cuda 0) impl:")
print("input size: {0}".format(input_size))
run_profile(
ReferenceWeightBinarizationModule('dorefa').cuda(),
input_size,
'cuda',
gpu_runs,
forward_only=True)
print()
print("Custom DoReFa weight binarization (cuda 0) impl:")
print("input size: {0}".format(input_size))
run_profile(
DOREFABinarize(enabled=True).cuda(),
input_size,
'cuda',
gpu_runs,
forward_only=True)
print()
print("Pytorch scale/threshold activation binarization (cuda 0) impl:")
print("input size: {0}".format(input_size))
run_profile(
ReferenceActivationBinarizationModule(input_shape=LOW_BATCH_INPUT_SIZE).cuda(),
input_size,
'cuda',
gpu_runs)
print()
print("Custom scale/threshold activation binarization (cuda 0) impl:")
print("input size: {0}".format(input_size))
# Init the scales now so that it does not affect the benchmark
act_bin_module = ActivationBinarizationScaleThreshold(input_shape=LOW_BATCH_INPUT_SIZE, enabled=True)
act_bin_module.scale = torch.nn.Parameter(get_test_scale(1))
act_bin_module.threshold = torch.nn.Parameter(get_test_threshold(LOW_BATCH_INPUT_SIZE))
act_bin_module.is_scale_initialized = True
run_profile(
act_bin_module.cuda(),
input_size,
'cuda',
gpu_runs)
# CPU low batch
print()
print("CPU low batch")
print("------------------------------------------------")
print("Pytorch XNOR weight binarization (cpu) impl:")
print("input size: {0}".format(LOW_BATCH_INPUT_SIZE))
run_profile(
ReferenceWeightBinarizationModule('xnor'),
LOW_BATCH_INPUT_SIZE,
'cpu',
CPU_RUNS,
forward_only=True)
print()
print("Custom XNOR weight binarization (cpu) impl:")
print("input size: {0}".format(LOW_BATCH_INPUT_SIZE))
run_profile(
XNORBinarize(enabled=True),
LOW_BATCH_INPUT_SIZE,
'cpu',
CPU_RUNS,
forward_only=True)
print()
print("Pytorch DoReFa weight binarization (cpu) impl:")
print("input size: {0}".format(LOW_BATCH_INPUT_SIZE))
run_profile(
ReferenceWeightBinarizationModule('dorefa'),
LOW_BATCH_INPUT_SIZE,
'cpu',
CPU_RUNS,
forward_only=True)
print()
print("Custom DoReFa weight binarization (cpu) impl:")
print("input size: {0}".format(LOW_BATCH_INPUT_SIZE))
run_profile(
DOREFABinarize(enabled=True),
LOW_BATCH_INPUT_SIZE,
'cpu',
CPU_RUNS,
forward_only=True)
print()
print("Pytorch scale/threshold activation binarization (cpu) impl:")
print("input size: {0}".format(LOW_BATCH_INPUT_SIZE))
run_profile(
ReferenceActivationBinarizationModule(input_shape=LOW_BATCH_INPUT_SIZE),
LOW_BATCH_INPUT_SIZE,
'cpu',
CPU_RUNS)
print()
print("Custom scale/threshold activation binarization (cpu) impl:")
print("input size: {0}".format(LOW_BATCH_INPUT_SIZE))
# Init the scales now so that it does not affect the benchmark
act_bin_module = ActivationBinarizationScaleThreshold(input_shape=LOW_BATCH_INPUT_SIZE, enabled=True)
act_bin_module.scale = torch.nn.Parameter(get_test_scale(1))
act_bin_module.threshold = torch.nn.Parameter(get_test_threshold(LOW_BATCH_INPUT_SIZE))
act_bin_module.is_scale_initialized = True
run_profile(
act_bin_module,
LOW_BATCH_INPUT_SIZE,
'cpu',
CPU_RUNS)