forked from pytorch/executorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_selective_quantization.py
More file actions
349 lines (269 loc) · 9.97 KB
/
Copy pathtest_selective_quantization.py
File metadata and controls
349 lines (269 loc) · 9.97 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# Copyright 2025-2026 Arm Limited and/or its affiliates.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
from typing import Dict
import torch
from executorch.backends.arm.quantizer import (
get_symmetric_a16w8_quantization_config,
get_symmetric_quantization_config,
TOSAQuantizer,
)
from executorch.backends.arm.quantizer.quantization_config import QuantizationConfig
from executorch.backends.arm.test import common
from executorch.backends.arm.test.tester.test_pipeline import QuantizationPipeline
from executorch.backends.arm.tosa import TosaSpecification
from executorch.backends.cortex_m.test.tester import ramp_tensor
from executorch.backends.test.harness.stages import StageType
from torchao.quantization.pt2e.quantizer.quantizer import Q_ANNOTATION_KEY
from torchvision import models, transforms # type: ignore[import-untyped]
from torchvision.ops.misc import Conv2dNormActivation # type: ignore[import-untyped]
def get_quantizer(use_composable_quantizer: bool = False):
tosa_spec = TosaSpecification.create_from_string("TOSA-1.0+INT")
quantizer = TOSAQuantizer(
tosa_spec, use_composable_quantizer=use_composable_quantizer
)
quantizer.set_global(get_symmetric_quantization_config())
return quantizer
def get_selective_quantizer_by_module(
module_types: Dict[torch.nn.Module, QuantizationConfig]
):
quantizer = get_quantizer()
quantizer.set_global(get_symmetric_quantization_config())
for module_type, config in module_types.items():
quantizer.set_module_type(module_type, config)
return quantizer
def get_selective_quantizer_by_module_name(module_names: Dict[str, QuantizationConfig]):
quantizer = get_quantizer()
quantizer.set_global(get_symmetric_quantization_config())
for module_name, config in module_names.items():
quantizer.set_module_name(module_name, config)
return quantizer
class Add(torch.nn.Module):
def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
return x + y
class Cat(torch.nn.Module):
def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
return torch.cat((x, y), dim=1)
class LinearGraphTail(torch.nn.Module):
def __init__(self):
super().__init__()
self.linear = torch.nn.Linear(10, 10)
def forward(self, x: torch.Tensor) -> torch.Tensor:
x = self.linear(x)
x = torch.relu(x)
x = torch.sigmoid(x)
return torch.neg(x)
class AddSoftmaxAdd(torch.nn.Module):
module_names = {"add_0": None, "add_1": None}
module_types = {
Add: None,
}
quantized_aten_targets = {"aten.relu.default": 1}
non_quantized_aten_targets = {"aten.add.Tensor": 2}
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.softmax = torch.nn.Softmax(dim=-1)
self.relu = torch.nn.ReLU()
self.add_0 = Add()
self.add_1 = Add()
def get_inputs(self):
return (torch.randn(1, 10), torch.randn(1, 10))
def forward(self, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
z = self.add_0(x, y)
z = self.relu(z)
z = self.softmax(z)
return self.add_1(z, y)
test_models = {
"add_softmax_add": AddSoftmaxAdd,
}
@common.parametrize("model", test_models)
def test_selective_quant_module_name_tosa_INT(model):
model = model()
inputs = model.get_inputs()
quantzed_aten_targets = model.quantized_aten_targets
non_quantized_aten_targets = model.non_quantized_aten_targets
quantization_annotations = {}
for target, count in quantzed_aten_targets.items():
quantization_annotations[target] = {
get_symmetric_quantization_config().output_activation: count
}
for target, count in non_quantized_aten_targets.items():
quantization_annotations[target] = {None: count}
pipeline = QuantizationPipeline[tuple[torch.Tensor, torch.Tensor]](
model,
inputs,
quantizer=get_selective_quantizer_by_module_name(model.module_names),
qspecs=quantization_annotations,
)
pipeline.run()
@common.parametrize("model", test_models)
def test_selective_quant_module_type_tosa_INT(model):
model = model()
inputs = model.get_inputs()
quantzed_aten_targets = model.quantized_aten_targets
non_quantized_aten_targets = model.non_quantized_aten_targets
quantization_annotations = {}
for target, count in quantzed_aten_targets.items():
quantization_annotations[target] = {
get_symmetric_quantization_config().output_activation: count
}
for target, count in non_quantized_aten_targets.items():
quantization_annotations[target] = {None: count}
pipeline = QuantizationPipeline[tuple[torch.Tensor, torch.Tensor]](
model,
inputs,
quantizer=get_selective_quantizer_by_module(model.module_types),
qspecs=quantization_annotations,
)
pipeline.run()
def test_selective_quant_cat_node_target_none_tosa_INT():
model = Cat()
inputs = (torch.randn(1, 2, 4), torch.randn(1, 3, 4))
quantizer = get_quantizer(use_composable_quantizer=True)
quantizer.set_node_target(torch.ops.aten.cat.default, None)
pipeline = QuantizationPipeline[tuple[torch.Tensor, torch.Tensor]](
model,
inputs,
quantizer=quantizer,
qspecs={
"aten.cat.default": {
None: 1,
},
},
)
pipeline.run()
def test_composable_io_none_skips_global_tosa_INT():
model = Add()
inputs = (torch.randn(1, 10), torch.randn(1, 10))
quantizer = get_quantizer(use_composable_quantizer=True)
quantizer.set_io(None)
pipeline = QuantizationPipeline[tuple[torch.Tensor, torch.Tensor]](
model,
inputs,
quantizer=quantizer,
input_qspecs={None: 2},
output_qspecs={None: 1},
)
pipeline.run()
def test_composable_global_none_linear_graph_tail_tosa_INT():
model = LinearGraphTail()
inputs = (torch.randn(1, 10),)
quantizer = get_quantizer(use_composable_quantizer=True)
quantizer.set_global(None)
pipeline = QuantizationPipeline[tuple[torch.Tensor]](
model,
inputs,
quantizer=quantizer,
qspecs={
"aten.linear.default": {None: 1},
"aten.relu.default": {None: 1},
"aten.sigmoid.default": {None: 1},
"aten.neg.default": {None: 1},
},
)
pipeline.run()
graph = pipeline.tester.get_graph(StageType.QUANTIZE)
unannotated_nodes = [
node.name
for node in graph.nodes
if node.op == "call_function" and Q_ANNOTATION_KEY not in node.meta
]
assert not unannotated_nodes
mv3 = models.mobilenet_v3_small(weights=models.MobileNet_V3_Small_Weights)
mv3.eval()
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
class SharedBufferEmbeddingLinearConstantFold(torch.nn.Module):
def __init__(self):
super().__init__()
self.shared = torch.nn.Embedding(4, 4)
self.lm_head = torch.nn.Linear(4, 4, bias=False)
self.lm_head.weight = self.shared.weight
def forward(self, ids, x):
y0 = self.shared(ids).sum(dim=1)
z = self.lm_head(x)
return y0 + z
def test_mv3_selective_quant_int16_tosa_INT():
model = mv3
inputs = (normalize(torch.randn(1, 3, 224, 224)),)
a16w8_config = get_symmetric_a16w8_quantization_config()
quantization_annotations = {
"aten.conv2d.default": {
a16w8_config.output_activation: 34,
},
"aten.hardswish_.default": {
a16w8_config.output_activation: 18,
},
"aten.relu_.default": {
a16w8_config.output_activation: 5,
},
}
pipeline = QuantizationPipeline[tuple[torch.Tensor]](
model,
inputs,
quantizer=get_selective_quantizer_by_module(
{
Conv2dNormActivation: a16w8_config,
}
),
qspecs=quantization_annotations,
)
pipeline.run()
def test_mv3_selective_quant_float32_tosa_INT():
model = mv3
inputs = (normalize(torch.randn(1, 3, 224, 224)),)
quantization_annotations = {
"aten.conv2d.default": {
None: 14,
},
}
pipeline = QuantizationPipeline[tuple[torch.Tensor]](
model,
inputs,
quantizer=get_selective_quantizer_by_module_name(
{
"conv2d_3": None,
}
),
qspecs=quantization_annotations,
)
pipeline.run()
def test_mv3_io_quant_tosa_INT():
model = mv3
inputs = (normalize(torch.randn(1, 3, 224, 224)),)
quantizer = get_quantizer()
# Workaround to disable quantization for all modules
quantizer.set_module_type(torch.nn.Module, None)
# Only quantize IO
quantizer.set_io(get_symmetric_quantization_config())
pipeline = QuantizationPipeline[tuple[torch.Tensor]](
model,
inputs,
quantizer=quantizer,
input_qspecs={get_symmetric_quantization_config().input_activation: 1},
output_qspecs={get_symmetric_quantization_config().output_activation: 1},
)
pipeline.run()
def test_multiple_folded_get_attr():
"""In torchao/quantization/pt2e/constant_fold.py:constant_fold, get_attr
node targets are deleted as soon as there is one get_attr node w/o users
using the target.
If there are multiple get_attr nodes referring the same target such as in
this test, the function crashes if no workaround is present.
"""
model = SharedBufferEmbeddingLinearConstantFold()
example_inputs = (
torch.tensor([[0, 1]], dtype=torch.long),
ramp_tensor(-2, 2, (1, 4)),
)
quantizer = get_quantizer()
quantizer.set_module_type(torch.nn.Embedding, None)
pipeline = QuantizationPipeline(
model,
example_inputs,
quantizer=quantizer,
qspecs=None,
input_qspecs=None,
output_qspecs=None,
)
pipeline.run()