PTQ-Level Efficiency, QAT-Level Accuracy | 训练后量化的效率,量化感知训练的精度
Allow non-MLLM calibration datasets when not quantizing vision (#2065)
Fabrizio del Tin63dbc8ba27ef
diff --git a/auto_round/calibration/mllm.py b/auto_round/calibration/mllm.pyindex 6ea2d786e..4eb3ea80b 100644--- a/auto_round/calibration/mllm.py+++ b/auto_round/calibration/mllm.py@@ -108,27 +108,49 @@ class MLLMCalibrator(LLMCalibrator):" will use liuhaotian/llava_conv_58k with default config as an alternative.")dataset = "liuhaotian/llava_conv_58k"- orig_bs = self.batch_size- (- self.dataloader,- self.batch_size,- self.seqlen,- ) = get_mllm_dataloader(- template=template_obj,- model=self.model,- tokenizer=tokenizer,- processor=self.processor,- image_processor=image_processor,- dataset=dataset,- extra_data_dir=self.extra_data_dir,- seqlen=self.seqlen,- bs=bs,- seed=self.seed,- nsamples=nsamples,- quant_nontext_module=self.quant_nontext_module,- )- if orig_bs != 1 and self.batch_size == 1:- self.is_only_supported_bs1 = True+ from auto_round.compressors.mllm.dataset import MLLM_DATASET++ if not self.quant_nontext_module and dataset not in MLLM_DATASET:+ # A local text dataset (file/dir/HF+ # text set) is valid calibration for a VLM whose non-text+ # modules are NOT being quantized -- the full-model forward+ # runs text-only and the generic-dict branch of the loop below+ # feeds model(**batch). Upstream instead falls through to+ # get_mllm_dataloader, which KeyErrors on any dataset not in+ # its MLLM_DATASET registry (its `os.path.isfile(dataset) or+ # dataset in MLLM_DATASET` guard still indexes MLLM_DATASET+ # with the file path).+ from auto_round.calib_dataset import get_dataloader++ logger.info(+ f"Multimodal model with non-MLLM calibration dataset {dataset!r} and "+ "quant_nontext_module=False: using the standard text dataloader "+ "(vision/audio towers are not being quantized, so text-only "+ "calibration through the full-model forward is sufficient)."+ )+ self.dataloader = get_dataloader(tokenizer, self.seqlen, dataset, self.seed, bs, nsamples)+ else:+ orig_bs = self.batch_size+ (+ self.dataloader,+ self.batch_size,+ self.seqlen,+ ) = get_mllm_dataloader(+ template=template_obj,+ model=self.model,+ tokenizer=tokenizer,+ processor=self.processor,+ image_processor=image_processor,+ dataset=dataset,+ extra_data_dir=self.extra_data_dir,+ seqlen=self.seqlen,+ bs=bs,+ seed=self.seed,+ nsamples=nsamples,+ quant_nontext_module=self.quant_nontext_module,+ )+ if orig_bs != 1 and self.batch_size == 1:+ self.is_only_supported_bs1 = Trueelse:self.dataloader = self.datasetdiff --git a/test/test_cpu/models/test_mllm_text_only_calib.py b/test/test_cpu/models/test_mllm_text_only_calib.pynew file mode 100644index 000000000..40e3cf0de--- /dev/null+++ b/test/test_cpu/models/test_mllm_text_only_calib.py@@ -0,0 +1,71 @@+# Copyright (c) 2026 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.++"""+Regression test for calibrating a multimodal (VLM) checkpoint with a+non-MLLM (plain text) local calibration dataset when quant_nontext_module+is False.++Before this fix, any string dataset unconditionally fell through to+``get_mllm_dataloader``, which indexes ``MLLM_DATASET`` with the dataset's+value even when ``os.path.isfile(dataset)`` is what made it enter that+branch (a local file path is never actually a key in that registry) --+``KeyError: '<the local file path>'``. Since the vision/audio towers aren't+being quantized in this scenario, plain text-only calibration through the+standard (non-MLLM) dataloader is sufficient and now used instead.+"""++import json++import pytest++from auto_round import AutoRound+++class TestMllmTextOnlyCalibration:+ @pytest.fixture(autouse=True)+ def setup_save_dir(self, tmp_path):+ self.save_dir = str(tmp_path / "saved")+ yield+ import shutil++ shutil.rmtree(self.save_dir, ignore_errors=True)++ @pytest.fixture+ def local_text_calib_file(self, tmp_path):+ data = [{"text": "The quick brown fox jumps over the lazy dog. " * 40}] * 20+ path = tmp_path / "calib.json"+ with open(path, "w") as f:+ json.dump(data, f)+ return str(path)++ def test_local_text_dataset_with_quant_nontext_module_false(self, tiny_qwen_vl_model_path, local_text_calib_file):+ """A local (non-MLLM-registered) text file used to KeyError inside+ get_mllm_dataloader; it must now route through the standard text+ dataloader instead, since the vision tower isn't being quantized."""+ ar = AutoRound(+ model=tiny_qwen_vl_model_path,+ scheme="W4A16",+ iters=1,+ nsamples=1,+ seqlen=32,+ dataset=local_text_calib_file,+ quant_nontext_module=False,+ )+ _, layer_config = ar.quantize()++ quantized_language_layers = [+ name for name, cfg in layer_config.items() if "bits" in cfg and "language_model" in name+ ]+ assert quantized_language_layers, "expected language-model layers to be quantized"