-
Notifications
You must be signed in to change notification settings - Fork 240
/
model_scope.py
374 lines (365 loc) · 13.4 KB
/
model_scope.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
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
# Copyright (c) 2024 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.
import copy
from typing import Dict, List
import nncf
from nncf import ModelType
from nncf import QuantizationPreset
from nncf.parameters import CompressWeightsMode
from nncf.parameters import SensitivityMetric
from nncf.quantization.advanced_parameters import AdvancedQuantizationParameters
from nncf.quantization.advanced_parameters import AdvancedSmoothQuantParameters
from tests.post_training.pipelines.base import ALL_PTQ_BACKENDS
from tests.post_training.pipelines.base import NNCF_PTQ_BACKENDS
from tests.post_training.pipelines.base import BackendType
from tests.post_training.pipelines.causal_language_model import CausalLMHF
from tests.post_training.pipelines.image_classification_timm import ImageClassificationTimm
from tests.post_training.pipelines.lm_weight_compression import LMWeightCompression
from tests.post_training.pipelines.masked_language_modeling import MaskedLanguageModelingHF
QUANTIZATION_MODELS = [
# HF models
{
"reported_name": "hf/bert-base-uncased",
"model_id": "bert-base-uncased",
"pipeline_cls": MaskedLanguageModelingHF,
"compression_params": {
"preset": QuantizationPreset.MIXED,
"model_type": ModelType.TRANSFORMER,
"subset_size": 2,
},
"backends": ALL_PTQ_BACKENDS + [BackendType.OPTIMUM],
"is_batch_size_supported": False,
},
{
"reported_name": "hf/hf-internal-testing/tiny-random-GPTNeoXForCausalLM",
"model_id": "hf-internal-testing/tiny-random-GPTNeoXForCausalLM",
"pipeline_cls": CausalLMHF,
"compression_params": {
"preset": QuantizationPreset.MIXED,
"model_type": ModelType.TRANSFORMER,
"subset_size": 2,
},
"backends": [BackendType.OPTIMUM],
"is_batch_size_supported": False,
},
# Timm models
{
"reported_name": "timm/crossvit_9_240",
"model_id": "crossvit_9_240",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
"model_type": ModelType.TRANSFORMER,
"advanced_parameters": AdvancedQuantizationParameters(smooth_quant_alpha=-1.0),
},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/darknet53",
"model_id": "darknet53",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/deit3_small_patch16_224",
"model_id": "deit3_small_patch16_224",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
"model_type": ModelType.TRANSFORMER,
"advanced_parameters": AdvancedQuantizationParameters(
smooth_quant_alphas=AdvancedSmoothQuantParameters(matmul=-1)
),
},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/dla34",
"model_id": "dla34",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/dpn68",
"model_id": "dpn68",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/efficientnet_b0",
"model_id": "efficientnet_b0",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/efficientnet_b0_BC",
"model_id": "efficientnet_b0",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
"fast_bias_correction": False,
},
"backends": [BackendType.ONNX, BackendType.OV],
},
{
"reported_name": "timm/efficientnet_lite0",
"model_id": "efficientnet_lite0",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/hrnet_w18",
"model_id": "hrnet_w18",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/inception_resnet_v2",
"model_id": "inception_resnet_v2",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {},
"backends": NNCF_PTQ_BACKENDS,
},
{
"reported_name": "timm/levit_128",
"model_id": "levit_128",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
"model_type": ModelType.TRANSFORMER,
"advanced_parameters": AdvancedQuantizationParameters(
smooth_quant_alphas=AdvancedSmoothQuantParameters(matmul=0.05)
),
},
"backends": NNCF_PTQ_BACKENDS,
"is_batch_size_supported": False, # Issue is raised during export with dynamich shape.
},
{
"reported_name": "timm/mobilenetv2_050",
"model_id": "mobilenetv2_050",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/mobilenetv2_050_BC",
"model_id": "mobilenetv2_050",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
"fast_bias_correction": False,
},
"backends": [BackendType.ONNX, BackendType.OV],
},
{
"reported_name": "timm/mobilenetv3_small_050",
"model_id": "mobilenetv3_small_050",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/mobilenetv3_small_050_BC",
"model_id": "mobilenetv3_small_050",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
"fast_bias_correction": False,
},
"backends": [BackendType.ONNX, BackendType.OV],
},
{
"reported_name": "timm/regnetx_002",
"model_id": "regnetx_002",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/resnest14d",
"model_id": "resnest14d",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/resnet18",
"model_id": "resnet18",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/swin_base_patch4_window7_224",
"model_id": "swin_base_patch4_window7_224",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
"model_type": ModelType.TRANSFORMER,
},
"backends": [BackendType.OV],
},
{
"reported_name": "timm/swin_base_patch4_window7_224_no_sq",
"model_id": "swin_base_patch4_window7_224",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
"model_type": ModelType.TRANSFORMER,
"advanced_parameters": AdvancedQuantizationParameters(
smooth_quant_alphas=AdvancedSmoothQuantParameters(matmul=-1)
),
},
"backends": [BackendType.TORCH, BackendType.CUDA_TORCH, BackendType.ONNX],
},
{
"reported_name": "timm/tf_inception_v3",
"model_id": "tf_inception_v3",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/vgg11",
"model_id": "vgg11",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {},
"backends": NNCF_PTQ_BACKENDS,
},
{
"reported_name": "timm/visformer_small",
"model_id": "visformer_small",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
"model_type": ModelType.TRANSFORMER,
},
"backends": ALL_PTQ_BACKENDS,
},
{
"reported_name": "timm/wide_resnet50_2",
"model_id": "wide_resnet50_2",
"pipeline_cls": ImageClassificationTimm,
"compression_params": {
"preset": QuantizationPreset.MIXED,
},
"backends": ALL_PTQ_BACKENDS,
},
]
WEIGHT_COMPRESSION_MODELS = [
{
"reported_name": "tinyllama_data_free",
"model_id": "tinyllama/tinyllama-1.1b-step-50k-105b",
"pipeline_cls": LMWeightCompression,
"compression_params": {
"group_size": 64,
"ratio": 0.8,
"mode": CompressWeightsMode.INT4_SYM,
"sensitivity_metric": SensitivityMetric.WEIGHT_QUANTIZATION_ERROR,
},
"backends": [BackendType.OV],
"is_batch_size_supported": False,
},
{
"reported_name": "tinyllama_data_aware",
"model_id": "tinyllama/tinyllama-1.1b-step-50k-105b",
"pipeline_cls": LMWeightCompression,
"compression_params": {"group_size": 64, "ratio": 0.8, "mode": CompressWeightsMode.INT4_SYM},
"backends": [BackendType.OV],
"is_batch_size_supported": False,
},
{
"reported_name": "tinyllama_data_aware_awq",
"model_id": "tinyllama/tinyllama-1.1b-step-50k-105b",
"pipeline_cls": LMWeightCompression,
"compression_params": {"group_size": 64, "ratio": 0.8, "mode": CompressWeightsMode.INT4_SYM, "awq": True},
"backends": [BackendType.OV],
"is_batch_size_supported": False,
},
{
"reported_name": "tinyllama_data_aware_awq_stateful",
"model_id": "tinyllama/tinyllama-1.1b-step-50k-105b",
"pipeline_cls": LMWeightCompression,
"compression_params": {"group_size": 64, "ratio": 0.8, "mode": CompressWeightsMode.INT4_SYM, "awq": True},
"params": {"is_stateful": True},
"backends": [BackendType.OV],
"is_batch_size_supported": False,
},
{
"reported_name": "tinyllama_int8_data_free",
"model_id": "tinyllama/tinyllama-1.1b-step-50k-105b",
"pipeline_cls": LMWeightCompression,
"compression_params": {
"mode": CompressWeightsMode.INT8_ASYM,
"all_layers": None,
"awq": None,
"sensitivity_metric": None,
},
"backends": [BackendType.TORCH],
},
]
def generate_tests_scope(models_list: List[Dict]) -> Dict[str, dict]:
"""
Generate tests by names "{reported_name}_backend_{backend}"
"""
reported_name_to_model_id_mapping = {mc["reported_name"]: mc["model_id"] for mc in models_list}
tests_scope = {}
fp32_models = set()
for test_model_param in models_list:
for backend in test_model_param["backends"] + [BackendType.FP32]:
model_param = copy.deepcopy(test_model_param)
if "is_batch_size_supported" not in model_param: # Set default value of is_batch_size_supported.
model_param["is_batch_size_supported"] = True
reported_name = model_param["reported_name"]
model_id = reported_name_to_model_id_mapping[reported_name]
if backend == BackendType.FP32:
# Some test cases may share the same model_id, therefore fp32 test case is added only once for model_id.
if model_id not in fp32_models:
fp32_models.add(model_id)
else:
continue
test_case_name = f"{reported_name}_backend_{backend.value}"
model_param["backend"] = backend
model_param.pop("backends")
if test_case_name in tests_scope:
raise nncf.ValidationError(f"{test_case_name} already in tests_scope")
tests_scope[test_case_name] = model_param
return tests_scope
PTQ_TEST_CASES = generate_tests_scope(QUANTIZATION_MODELS)
WC_TEST_CASES = generate_tests_scope(WEIGHT_COMPRESSION_MODELS)