Skip to content

Commit c3e4fef

Browse files
Add tests
1 parent b275cff commit c3e4fef

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

optimum/intel/openvino/quantization.py

+6
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,12 @@ def _prepare_visual_causal_lm_dataset(self, config: OVWeightQuantizationConfig,
737737
"You have entered a string value for dataset. You can only choose between"
738738
f"{list(PREDEFINED_VISUAL_LM_DATASETS.keys())}, but the {dataset_name} was found"
739739
)
740+
if config.processor is None:
741+
raise ValueError(
742+
"`processor` must be specified in order to run data-aware weight compression. "
743+
"Please provide it as a model id, or a path to a directory containing all the required "
744+
"configuration files."
745+
)
740746

741747
dataset_metadata = PREDEFINED_VISUAL_LM_DATASETS[dataset_name]
742748
dataset = datasets.load_dataset(dataset_metadata["name"], split=dataset_metadata["split"]).shuffle(seed=0)

tests/openvino/test_quantization.py

+13-5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
OVWeightQuantizationConfig,
6464
OVDynamicQuantizationConfig,
6565
OVModelOpenCLIPForZeroShotImageClassification,
66+
OVModelForVisualCausalLM,
6667
)
6768
from optimum.intel.openvino.configuration import (
6869
OVQuantizationMethod,
@@ -298,6 +299,7 @@ class OVWeightCompressionTest(unittest.TestCase):
298299
(OVStableDiffusionPipeline, "stable-diffusion"),
299300
(OVStableDiffusionXLPipeline, "stable-diffusion-xl"),
300301
(OVModelOpenCLIPForZeroShotImageClassification, "open-clip"),
302+
(OVModelForVisualCausalLM, "llava"),
301303
)
302304

303305
SUPPORTED_ARCHITECTURES_WITH_HYBRID_QUANTIZATION = (
@@ -439,6 +441,9 @@ def test_ovmodel_load_with_compressed_weights(self, model_cls, model_type):
439441
models.append(model.text_encoder if model_type == "stable-diffusion" else model.text_encoder_2)
440442
elif model_type == "open-clip":
441443
models = [model.text_model, model.visual_model]
444+
elif model.export_feature == "image-text-to-text":
445+
models = [model.lm_model, model.vision_embeddings_model, model.text_embeddings_model]
446+
models += [getattr(model, part) for part in model.additional_parts]
442447
else:
443448
models = [model]
444449

@@ -528,16 +533,16 @@ def test_ovmodel_4bit_auto_compression_with_config(
528533
# TODO: Check that AWQ was actually applied
529534
pass
530535

531-
tokenizer = AutoTokenizer.from_pretrained(model_id)
532-
if tokenizer.pad_token is None:
533-
tokenizer.pad_token = tokenizer.eos_token
536+
ov_model = model
537+
if model_cls == OVModelForVisualCausalLM:
538+
ov_model = model.lm_model
534539

535-
_, num_weight_nodes = get_num_quantized_nodes(model)
540+
_, num_weight_nodes = get_num_quantized_nodes(ov_model)
536541
expected_num_weight_nodes.update({k: 0 for k in set(num_weight_nodes) - set(expected_num_weight_nodes)})
537542
self.assertEqual(expected_num_weight_nodes, num_weight_nodes)
538543
model.save_pretrained(tmp_dir)
539544

540-
wc_rt_info = model.model.get_rt_info()["nncf"]["weight_compression"]
545+
wc_rt_info = ov_model.get_rt_info()["nncf"]["weight_compression"]
541546
self.assertEqual(quantization_config.quant_method.lower() == "awq", wc_rt_info["awq"].value == "True")
542547
self.assertEqual(
543548
quantization_config.scale_estimation or False, wc_rt_info["scale_estimation"].value == "True"
@@ -568,6 +573,9 @@ def test_ovmodel_load_with_uncompressed_weights(self, model_cls, model_type):
568573
models.append(model.text_encoder if model_type == "stable-diffusion" else model.text_encoder_2)
569574
elif model_type == "open-clip":
570575
models = [model.text_model, model.visual_model]
576+
elif model.export_feature == "image-text-to-text":
577+
models = [model.lm_model, model.vision_embeddings_model, model.text_embeddings_model]
578+
models += [getattr(model, part) for part in model.additional_parts]
571579
else:
572580
models = [model]
573581

tests/openvino/utils_tests.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414

1515
import numpy as np
16+
import openvino as ov
1617
import torch
1718

1819

@@ -170,18 +171,20 @@
170171
"stable-diffusion-xl": (366, 34, 42, 66),
171172
"stable-diffusion-xl-refiner": (366, 34, 42, 66),
172173
"open-clip": (20, 28),
174+
"llava": (30, 18, 2),
173175
}
174176

175177

176-
def get_num_quantized_nodes(ov_model):
178+
def get_num_quantized_nodes(model):
177179
num_fake_quantize = 0
178180
num_weight_nodes = {
179181
"int8": 0,
180182
"int4": 0,
181183
"f4e2m1": 0,
182184
"f8e8m0": 0,
183185
}
184-
for elem in ov_model.model.get_ops():
186+
ov_model = model if isinstance(model, ov.Model) else model.model
187+
for elem in ov_model.get_ops():
185188
if "FakeQuantize" in elem.name:
186189
num_fake_quantize += 1
187190
for i in range(elem.get_output_size()):

0 commit comments

Comments
 (0)