Skip to content

Commit 0c44e0b

Browse files
committed
clean tests
1 parent 54ce18f commit 0c44e0b

File tree

3 files changed

+68
-20
lines changed

3 files changed

+68
-20
lines changed

tests/neural_compressor/test_ipex.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2023 The HuggingFace Team. All rights reserved.
1+
# Copyright 2024 The HuggingFace Team. All rights reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

tests/neural_compressor/test_optimization.py

+22-18
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
pipeline,
4545
set_seed,
4646
)
47-
from utils_tests import SEED, INCTestMixin, _generate_dataset
47+
from utils_tests import MODEL_NAMES, SEED, INCTestMixin, _generate_dataset
4848
from optimum.intel.utils.import_utils import is_torch_version, is_intel_extension_for_transformers_available
4949

5050

@@ -71,22 +71,23 @@
7171

7272
class QuantizationTest(INCTestMixin):
7373
SUPPORTED_ARCHITECTURES_WITH_EXPECTED_QUANTIZED_MATMULS = (
74-
("text-classification", "hf-internal-testing/tiny-random-BertForSequenceClassification", 21),
75-
("text-generation", "hf-internal-testing/tiny-random-BloomForCausalLM", 21),
74+
("text-classification", "bert", 21),
75+
# ("text-generation", "bloom", 21),
7676
)
7777

7878
SUPPORTED_ARCHITECTURES_DYNAMIC = SUPPORTED_ARCHITECTURES_WITH_EXPECTED_QUANTIZED_MATMULS + (
79-
("fill-mask", "hf-internal-testing/tiny-random-BertForMaskedLM", 22),
80-
("token-classification", "hf-internal-testing/tiny-random-AlbertForTokenClassification", 26),
79+
("fill-mask", "bert", 22),
80+
("token-classification", "albert", 26),
8181
)
8282

8383
TEXT_GENERATION_SUPPORTED_ARCHITECTURES = (
84-
"hf-internal-testing/tiny-random-BloomForCausalLM",
85-
"hf-internal-testing/tiny-random-GPTNeoForCausalLM",
84+
"bloom",
85+
"gpt_neo",
8686
)
8787

8888
@parameterized.expand(SUPPORTED_ARCHITECTURES_DYNAMIC)
89-
def test_dynamic_quantization(self, task, model_name, expected_quantized_matmuls):
89+
def test_dynamic_quantization(self, task, model_arch, expected_quantized_matmuls):
90+
model_name = MODEL_NAMES[model_arch]
9091
quantization_config = PostTrainingQuantConfig(approach="dynamic")
9192
model_class = ORT_SUPPORTED_TASKS[task]["class"][0]
9293
tokenizer = AutoTokenizer.from_pretrained(model_name)
@@ -121,8 +122,9 @@ def test_dynamic_quantization(self, task, model_name, expected_quantized_matmuls
121122
)
122123

123124
@parameterized.expand(SUPPORTED_ARCHITECTURES_WITH_EXPECTED_QUANTIZED_MATMULS)
124-
def test_static_quantization(self, task, model_name, expected_quantized_matmuls):
125+
def test_static_quantization(self, task, model_arch, expected_quantized_matmuls):
125126
num_samples = 10
127+
model_name = MODEL_NAMES[model_arch]
126128
model_class = ORT_SUPPORTED_TASKS[task]["class"][0]
127129
tokenizer = AutoTokenizer.from_pretrained(model_name)
128130
if tokenizer.pad_token is None:
@@ -245,7 +247,8 @@ def test_dynamic_diffusion_model(self):
245247
self.assertTrue(np.allclose(loaded_pipe_outputs, outputs, atol=1e-4))
246248

247249
@parameterized.expand(TEXT_GENERATION_SUPPORTED_ARCHITECTURES)
248-
def test_quantize_text_generate_model(self, model_id):
250+
def test_quantize_text_generate_model(self, model_arch):
251+
model_id = MODEL_NAMES[model_arch]
249252
set_seed(42)
250253
model = AutoModelForCausalLM.from_pretrained(model_id)
251254
tokenizer = AutoTokenizer.from_pretrained(model_id)
@@ -274,13 +277,11 @@ def calibration_fn(p_model):
274277

275278

276279
class TrainingOptimizationTest(INCTestMixin):
277-
SUPPORTED_ARCHITECTURES_WITH_EXPECTED_QUANTIZED_MATMULS = (
278-
("text-classification", "hf-internal-testing/tiny-random-BertForSequenceClassification", 21),
279-
("text-generation", "hf-internal-testing/tiny-random-BloomForCausalLM", 21),
280-
)
280+
SUPPORTED_ARCHITECTURES_WITH_EXPECTED_QUANTIZED_MATMULS = (("text-classification", "bert", 21),)
281281

282282
@parameterized.expand(SUPPORTED_ARCHITECTURES_WITH_EXPECTED_QUANTIZED_MATMULS)
283-
def test_aware_training_quantization(self, task, model_name, expected_quantized_matmuls):
283+
def test_aware_training_quantization(self, task, model_arch, expected_quantized_matmuls):
284+
model_name = MODEL_NAMES[model_arch]
284285
quantization_config = QuantizationAwareTrainingConfig()
285286
save_onnx_model = False
286287

@@ -303,7 +304,8 @@ def test_aware_training_quantization(self, task, model_name, expected_quantized_
303304
)
304305

305306
@parameterized.expand(SUPPORTED_ARCHITECTURES_WITH_EXPECTED_QUANTIZED_MATMULS)
306-
def test_aware_training_quantization_pruning(self, task, model_name, expected_quantized_matmuls):
307+
def test_aware_training_quantization_pruning(self, task, model_arch, expected_quantized_matmuls):
308+
model_name = MODEL_NAMES[model_arch]
307309
quantization_config = QuantizationAwareTrainingConfig()
308310
target_sparsity = 0.9
309311
pruning_config = WeightPruningConfig(
@@ -335,7 +337,8 @@ def test_aware_training_quantization_pruning(self, task, model_name, expected_qu
335337
)
336338

337339
@parameterized.expand(SUPPORTED_ARCHITECTURES_WITH_EXPECTED_QUANTIZED_MATMULS)
338-
def test_magnitude_pruning(self, task, model_name, expected_quantized_matmuls):
340+
def test_magnitude_pruning(self, task, model_arch, expected_quantized_matmuls):
341+
model_name = MODEL_NAMES[model_arch]
339342
target_sparsity = 0.9
340343
# end_step should be training_args.num_train_epochs * (len(train_dataset) // training_args.per_device_train_batch_size)
341344
pruning_config = WeightPruningConfig(
@@ -374,7 +377,8 @@ def test_magnitude_pruning(self, task, model_name, expected_quantized_matmuls):
374377
self.assertEqual(inc_config.pruning["pattern"], "4x1")
375378

376379
@parameterized.expand(SUPPORTED_ARCHITECTURES_WITH_EXPECTED_QUANTIZED_MATMULS)
377-
def test_distillation(self, task, model_name, expected_quantized_matmuls):
380+
def test_distillation(self, task, model_arch, expected_quantized_matmuls):
381+
model_name = MODEL_NAMES[model_arch]
378382
teacher_model = ORT_SUPPORTED_TASKS[task]["class"][0].auto_model_class.from_pretrained(model_name)
379383
distillation_config = DistillationConfig(teacher_model=teacher_model)
380384
save_onnx_model = True

tests/neural_compressor/utils_tests.py

+45-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,50 @@
6565
}
6666

6767

68+
MODEL_NAMES = {
69+
"albert": "hf-internal-testing/tiny-random-albert",
70+
"beit": "hf-internal-testing/tiny-random-BeitForImageClassification",
71+
"bert": "hf-internal-testing/tiny-random-bert",
72+
"bart": "hf-internal-testing/tiny-random-bart",
73+
"blenderbot-small": "hf-internal-testing/tiny-random-BlenderbotModel",
74+
"blenderbot": "hf-internal-testing/tiny-random-BlenderbotModel",
75+
"bloom": "hf-internal-testing/tiny-random-BloomModel",
76+
"convbert": "hf-internal-testing/tiny-random-ConvBertForSequenceClassification",
77+
"codegen": "hf-internal-testing/tiny-random-CodeGenForCausalLM",
78+
"convnext": "hf-internal-testing/tiny-random-convnext",
79+
"distilbert": "hf-internal-testing/tiny-random-distilbert",
80+
"electra": "hf-internal-testing/tiny-random-electra",
81+
"flaubert": "hf-internal-testing/tiny-random-flaubert",
82+
"gpt_bigcode": "hf-internal-testing/tiny-random-GPTBigCodeModel",
83+
"gpt2": "hf-internal-testing/tiny-random-gpt2",
84+
"gpt_neo": "hf-internal-testing/tiny-random-GPTNeoModel",
85+
"gpt_neox": "hf-internal-testing/tiny-random-GPTNeoXForCausalLM",
86+
"gptj": "hf-internal-testing/tiny-random-GPTJModel",
87+
"levit": "hf-internal-testing/tiny-random-LevitModel",
88+
"llama": "fxmarty/tiny-llama-fast-tokenizer",
89+
"llama2": "Jiqing/tiny_random_llama2",
90+
"marian": "sshleifer/tiny-marian-en-de",
91+
"mbart": "hf-internal-testing/tiny-random-mbart",
92+
"mistral": "echarlaix/tiny-random-mistral",
93+
"mobilenet_v1": "google/mobilenet_v1_0.75_192",
94+
"mobilenet_v2": "hf-internal-testing/tiny-random-MobileNetV2Model",
95+
"mobilevit": "hf-internal-testing/tiny-random-mobilevit",
96+
"mpt": "hf-internal-testing/tiny-random-MptForCausalLM",
97+
"mt5": "stas/mt5-tiny-random",
98+
"opt": "hf-internal-testing/tiny-random-OPTModel",
99+
"phi": "echarlaix/tiny-random-PhiForCausalLM",
100+
"resnet": "hf-internal-testing/tiny-random-resnet",
101+
"roberta": "hf-internal-testing/tiny-random-roberta",
102+
"roformer": "hf-internal-testing/tiny-random-roformer",
103+
"squeezebert": "hf-internal-testing/tiny-random-squeezebert",
104+
"t5": "hf-internal-testing/tiny-random-t5",
105+
"unispeech": "hf-internal-testing/tiny-random-unispeech",
106+
"vit": "hf-internal-testing/tiny-random-vit",
107+
"wav2vec2": "anton-l/wav2vec2-random-tiny-classifier",
108+
"xlm": "hf-internal-testing/tiny-random-xlm",
109+
}
110+
111+
68112
def num_quantized_matmul_onnx_model(onnx_model):
69113
num_quantized_matmul = 0
70114
for node in onnx_model.graph.node:
@@ -145,7 +189,7 @@ def check_model_outputs(
145189
ort_model = ORT_SUPPORTED_TASKS[task]["class"][0].from_pretrained(save_directory, **model_kwargs)
146190
ort_outputs = ort_model(**tokens)
147191
self.assertTrue("logits" in ort_outputs)
148-
if task != "fill-mask":
192+
if task not in {"fill-mask", "text-generation"}:
149193
self.assertTrue(torch.allclose(ort_outputs.logits, outputs, atol=1e-2))
150194

151195
@staticmethod

0 commit comments

Comments
 (0)