Repository navigation
feat(gemma3): declare Gemma 3 VL modality and add image calibration support - #3307
Merged
Merged
Conversation
…1465) Multimodal Gemma 3 checkpoints (model_type=gemma3) quantized the text stack but never declared an image modality or a processor, so they could only be calibrated with text data and the test harness never exercised image inputs. Declare modality=[TEXT, IMAGE_TO_TEXT] and require_load_processor on Gemma3ForConditionalGenerationGPTQ, add the processor-based calibration pipeline (load_processor / prepare_inputs_for_conversations / prepare_dataset), and add the pre-quantize hooks that materialize and release embed_tokens, rotary_emb, norm, vision_tower and multi_modal_projector around calibration input capture. Route Gemma3 VL test calibration to the shared image-caption dataset and re-record the ARC baselines for that calibration path; add an image-inference assertion plus CPU unit tests for the new contract. Verified on 4x4090 with the gp_314t environment: tests/models/test_gemma3_4b_it.py passes in fast and slow (all 34 layers) modes with 20 image calibration batches, tests/models/test_gemma3.py (text-only 1B) still passes, and tests/test_gemma3_vl_support.py covers the definition contract, dataset routing and both quantize hooks.
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 #1465
Summary
Issue #1465 reported that GPT-QModel only accepted
gemma3_text(used by the 1Bcheckpoint) and did not handle the multimodal
gemma3checkpoints that all thelarger Gemma 3 releases use.
The registry gap was closed earlier:
gemma3has been mapped toGemma3ForConditionalGenerationGPTQsince #1627, and the vision/weight-aliasingfixes from #2822 keep that path saving and reloading correctly. What was still
missing was the multimodal contract itself. The VL definition never declared an
image modality or a processor, so:
modality = [TEXT]even though it is an image-to-text checkpoint;This PR adds that contract and the calibration pipeline behind it, so multimodal
Gemma 3 is a first-class image-to-text model instead of a text stack that happens
to sit inside a VL shell.
What Changed
gptqmodel/models/definitions/gemma3.py(Gemma3ForConditionalGenerationGPTQ):modality = [MODALITY.TEXT, MODALITY.IMAGE_TO_TEXT]andrequire_load_processor = True, matching the other VL definitions(
lfm2_vl.py,smolvlm.py,interns1.py).load_processor,prepare_inputs_for_conversations,prepare_dataset,preprocess_dataset.Text rows,
{"text": ...}rows and raw strings are still normalized into chatconversations by
batched_conversations, so existing text-only calibrationinput keeps working.
pre_quantize_generate_hook_start/pre_quantize_generate_hook_endsoembed_tokens,rotary_emb,norm,vision_towerandmulti_modal_projectorare materialized onto the quantization device forcalibration input capture and released afterwards (back to disk when
offload_to_diskis set, otherwise back to CPU). This is the same pattern theother multimodal definitions use, and it avoids holding the vision tower on
the accelerator outside the capture window.
Gemma3QModel(gemma3_text) is intentionally unchanged and stays text-only.No public API changes. Behavior changes are limited to multimodal Gemma 3
checkpoints, which now load a processor and calibrate through chat conversations
instead of raw text.
Tests
Environment: Python 3.14t, Transformers 5.19.0, Torch 2.14.0+cu130,
4x RTX 4090
Additional end-to-end checks on the same box:
offload_to_disk=True, full 34-layerquantization, save, reload, then image QA: the quantized answer matches the
bf16 baseline (
AutoModelForImageTextToText) for the same image.{"text": ...}rows and bare strings) stillquantizes and still answers the image prompt correctly, so the new processor
path does not regress text-calibrated runs.
Note on the re-recorded baselines: image calibration moves the text ARC score
from
0.5060(text-calibrated) to0.4846on this checkpoint. That is thecalibration distribution change, not a quantization regression -- the bf16
baseline prompt and output match the quantized output,
accis bit-identicalacross repeated slow-mode runs, and
acc_normvaries by only ~0.1%(
0.5026-0.5034) between runs.floor_pct: 0.04leaves room for that spreadwhile still catching a real regression.
Review Requirements
AI-assisted code is welcome. The PR must still be reviewed before it is opened as ready for review.
I or an AI agent (such as Codex or Claude) have:
Kernel Accuracy Requirements
plumbing and test scaffolding only. No quantization math, weight packer or
inference kernel is modified.