Repository navigation
fix(models): route every VLM prepare_dataset through the shared calibration helper (#4633) - #4648
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4633
TL;DR
Every vision-language
prepare_datasetoverride now delegates to one sharedhelper,
prepare_vlm_calibration_datasetingptqmodel/models/definitions/_vlm_calibration.py. Twenty-three overrides thathad copied the conversation loop (and dropped the #4228/#4308 split) were
replaced; the eight that already used the shared helpers are unchanged. Net
+420/-215 across 25 definition files (the helper plus 24 definitions), one
96-node contract test, and a docs page.
1. The problem
Definitions added after #4228/#4308 copied
batched_conversationsdirectly:flowchart TD A["definition.prepare_dataset"] --> B["batched_conversations"] B --> C["prepare_inputs_for_conversations"] C --> D["processor.apply_chat_template"] A --> E["del kwargs"] D -->|"pre-tokenized row"| F["empty prompt or literal 'input_ids'<br/>calibrates on boilerplate"] E -->|"concat / sort / min_length"| G["silently ignored"] C -->|"unequal lengths, no padding"| H["ValueError at batch_size > 1"]cohere_compass.py:374del kwargs; nopadding=Trueinkling.py:132del kwargs; nopadding=Truehunyuan_vl.py:110del kwargs"input_ids"muse_glimmer.py:83del kwargs"input_ids"ernie4_5_vl_moe.py:399contentminimax_m3_vl.py:114intnot subscriptable)interns1.py:127lfm3_vl.py:143,lfm3_omni.py:139intern_s2_preview.py:187del kwargsdeepseek_vl.py:111del kwargsdiffusion_gemma.py:344del kwargsapertus.py:130del kwargsdeepseek_ocr2.py:124del kwargsdeepseek_vl_v2.py:171del kwargslocateanything.py:98del kwargsopenvla.py:180del kwargsovis.py:129ovis2_5.py:102unlimited_ocr.py:255del kwargszdtaichu5.py:283The silent cases are the bad ones: the caller passes
[{"input_ids": ids, "attention_mask": mask}](ortokenizer(texts)) andevery calibration sample is replaced by a template rendered around a dict with
no
content, or around the literal key names. GPTQ then calibrates on 1 to 9tokens of boilerplate, the exact #4308 failure.
2. The new flow
BaseQModel.quantizestill callsself.prepare_dataset(...); each definitionoverride is now a short delegation that supplies only its processor hook.
flowchart TD A["BaseQModel.quantize"] --> B["definition.prepare_dataset"] B --> C["prepare_vlm_calibration_dataset"] C --> D["split_calibration_dataset"] D -->|"pre-tokenized rows / columnar mapping"| E["prepare_calibration_dataset<br/>base text path"] D -->|"mixed ids + conversations"| F["ValueError: mixes pre-tokenized rows"] D -->|"multimodal columns in an input_ids row"| G["ValueError: non-text columns"] D -->|"chat conversations"| H["pop_conversation_calibration_options"] H -->|"calibration_concat_size set"| I["ValueError: concat not supported"] H --> J["batched_conversations<br/>preprocess_func"] J --> K["prepare_inputs_for_conversations<br/>or prepare_conversations callback"] K --> L["finalize_conversation_batches<br/>sort / min_length / shuffle"] E --> M["calibration batches"] L --> MTwo per-model hooks remain, and both delegate:
prepare_inputs_for_conversations(processor, conversations)prepares oneprocessor batch. It is the default, and every definition whose processor call
is a plain
apply_chat_templatesupplies only this method.prepare_conversations(rows)receives the raw non-chat rows and returns theprepared batches (OCR
{"image", "text"}rows, Ovis{"conversations", "image"}rows, OpenVLA prompt rows).Processors are resolved with
self.processorfirst andload_processor()onlywhen none is loaded, so a pre-tokenized dataset never loads a multimodal
processor.
3. Audit: every VLM
prepare_datasetoverrideDiscovered with
"prepare_dataset" in cls.__dict__overgptqmodel.models.definitions(31 exported classes). Status after this PR:Apertus1p5QModelprepare_inputs_for_conversationsCohereCompassQModelpadding=TrueDeepSeekOCR2QModel_prepare_calibration_batchesDeepSeekVLQModelDeepSeekVLV2QModel_prepare_calibration_batchesDiffusionGemmaQModelprepare_inputs_for_conversationsErnie4_5_VLMoeQModel_prepare_calibration_batchHunYuanVLQModelprepare_inputs_for_conversationsInklingMMQModelpadding=TrueInternS1QModel_prepare_calibration_batch;padding=TrueInternS2PreviewQModelprepare_inputs_for_conversationsInternVLChatQModelInternVLQModelLFM2VLQModelLFM3OmniQModelpadding=TrueLFM3VLQModelpadding=TrueLlavaQModelLocateAnythingQModel_prepare_calibration_batchesMiniCPMOQModelMiniCPMV4_6QModelMiniCPMVQModelMiniMaxM3VLGPTQ_prepare_calibration_batchesMuseGlimmerQModelOpenVLAQModel_prepare_calibration_batchesOvis2_5QModel_prepare_calibration_batchesOvisQModel_prepare_calibration_batchesQwen2AudioGPTQ_prepare_calibration_batchQwen3OmniMoeGPTQ_prepare_calibration_batchesSmolVLMQModelUnlimitedOCRQModel_prepare_calibration_batchesZDTaichu5QModelOverrides that are not exported through the definitions package but already
share the helpers:
BaseQwen2VLGPTQ(base_qwen2_vl.py:345),BaseQwen2_5_OmniGPTQ(base_qwen2_5_omni.py:293),Gemma3ForConditionalGenerationGPTQ(gemma3.py:299),Ovis2QModel(ovis2.py:103).Deliberate exceptions (documented in
docs/vlm-calibration-input-normalization.md):BaseQwen3VLGPTQ(base_qwen3_vl.py:563) buffers a pre-tokenized prefix andapplies the same options/finalizer itself. It does not call
split_calibration_dataset, so it rejects columnar mappings that the sharedsplitter accepts. Converting it changes the mixed-dataset behavior it was
written for; tracked as a follow-up, not part of [BUG] VLM prepare_dataset overrides added after #4228/#4308 bypass the shared calibration helper: pre-tokenized rows silently become an empty chat prompt or the text "input_ids" (Cohere Compass, HunyuanOCR, Muse-Glimmer); Inkling/Cohere crash at batch_size>1 #4633.
DeepSeekV4MTPQuantizationModel(deepseek_v4.py:3247) overridesprepare_datasetfor durable MTP replay batches, which are neither text norconversations; it is not a VLM calibration input.
4. Failure contract
list[int],input_idsmapping)"input_ids"text, or AttributeErrortokenizer(texts))input_idsrow pluspixel_valuescalibration_concat_sizewith conversationscalibration_dataset_sort/calibration_data_min_lengthbatch_size > 1apply_chat_templatepadding=Trueonsupport_batch_quantize=Truemodels5. Tests
tests/test_vlm_prepare_dataset_contract_4633.py(96 nodes) discovers everyexported definition with its own
prepare_datasetand pins:test_pretokenized_rows_never_reach_the_processorload_processornever calledtest_columnar_mapping_is_unpacked_not_rendered_as_key_namestokenizer(texts)mapping keeps its rowstest_conversation_path_rejects_concat_sizecalibration_concat_sizetest_batch_size_two_pads_unequal_conversations(2, max_len)batch withpadding=Truetest_discovery_covers_the_models_named_in_the_issueRed evidence (origin/main @
504769ea8, new test file copied in):The pre-tokenized failures are the intended assertion
(
AssertionError: Pre-tokenized calibration must not load the multimodal processor); the bs=2 failures are the intended ValueError(
Unable to create tensor ... padding=True).Green evidence (this branch, after the fix):
ruff check gptqmodel docs/eora tests setup.py->All checks passed.Real-checkpoint repro (CPU, local HunYuanOCR processor):
Known pre-existing failures (also on
origin/main, not caused by this PR):tests/models/test_inkling_mm_model.py5 nodes (defuser/torchvision env),TestInklingMMModel::test_inkling_mm_model,TestInternS2Preview::test_intern_s2_preview(missingsentencepiece),test_diffusion_gemma_post_quantize_syncs_encoder_layer, and theobject.__new__monkeypatch teardown errors.6. Validation env
OMP_NUM_THREADS=4 MKL_NUM_THREADS=4 OPENBLAS_NUM_THREADS=4exported for every run./opt/venvs/rocm101t/bin/pythonfor tests needingdatasets;/opt/venvs/cu130t/bin/pythonfor the CUDA-side run(
test_vlm_prepare_dataset_contract_4633.py+ pretokenized: 180 passed).PYTHONPATH=$PWDfrom the clone root; no installs.7. Limitations
torchvisionfor the Cohere Compass videoprocessor and a loadable image processor for Muse-Glimmer / Inkling; neither
venv has
torchvision, and the local Muse/Inkling checkpoints do not expose animage processor to Transformers 5.22. Those two models are therefore covered
by the stub-processor contract tests (including the bs=2 crash), not by the
real checkpoints. HunYuanOCR is covered by both.
BaseQwen3VLGPTQand the MTP replay overrides are documented exceptions; seesection 3.
downloads were made.