Skip to content

feat(gemma3): declare Gemma 3 VL modality and add image calibration support - #3307

Merged
Qubitium merged 1 commit into
mainfrom
fix/gemma3-vl-modality-1465
Oct 16, 2026
Merged

Qubitium merged 1 commit into
mainfrom
fix/gemma3-vl-modality-1465

Conversation

@ZX-ModelCloud

@ZX-ModelCloud ZX-ModelCloud commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1465

Summary

Issue #1465 reported that GPT-QModel only accepted gemma3_text (used by the 1B
checkpoint) and did not handle the multimodal gemma3 checkpoints that all the
larger Gemma 3 releases use.

The registry gap was closed earlier: gemma3 has been mapped to
Gemma3ForConditionalGenerationGPTQ since #1627, and the vision/weight-aliasing
fixes 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:

  • the model reported 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):

  • Declare modality = [MODALITY.TEXT, MODALITY.IMAGE_TO_TEXT] and
    require_load_processor = True, matching the other VL definitions
    (lfm2_vl.py, smolvlm.py, interns1.py).
  • Add the processor-backed calibration pipeline: load_processor,
    prepare_inputs_for_conversations, prepare_dataset, preprocess_dataset.
    Text rows, {"text": ...} rows and raw strings are still normalized into chat
    conversations by batched_conversations, so existing text-only calibration
    input keeps working.
  • Add pre_quantize_generate_hook_start / pre_quantize_generate_hook_end so
    embed_tokens, rotary_emb, norm, vision_tower and
    multi_modal_projector are materialized onto the quantization device for
    calibration input capture and released afterwards (back to disk when
    offload_to_disk is set, otherwise back to CPU). This is the same pattern the
    other 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

# New CPU unit tests for the multimodal contract
python -m pytest tests/test_gemma3_vl_support.py -q
# 9 passed in 5.49s

# Multimodal Gemma 3: fast mode (2-layer quant prefix) with image calibration
cd tests/models && CUDA_VISIBLE_DEVICES=1 python -m pytest test_gemma3_4b_it.py -q
# 1 passed in 126.26s
# "capturing layer inputs from 20 calibration batches"
# arc_challenge:acc 0.5324 (109.87%), acc_norm 0.5563 (110.69%)
# image output: "**The image depicts a stunning, snow-covered mountain range.**"

# Multimodal Gemma 3: slow mode (all 34 layers) with image calibration
cd tests/models && CUDA_VISIBLE_DEVICES=1 GPTQMODEL_MODEL_TEST_MODE=slow \
    python -m pytest test_gemma3_4b_it.py -q
# 1 passed in 144.94s
# arc_challenge:acc 0.4846 (100.01%), acc_norm 0.5034 (100.16%)

# Text-only Gemma 3 (1B) regression
cd tests/models && CUDA_VISIBLE_DEVICES=1 python -m pytest test_gemma3.py -q
# 1 passed in 76.77s

# Related CPU suites
python -m pytest tests/test_gemma3_vl_support.py tests/test_model_test_helpers.py \
    tests/test_lazy_turtle_conversion_mapping.py -q
# 55 passed in 5.04s

ruff check gptqmodel/models/definitions/gemma3.py tests/models/ovis/image_to_test_dataset.py \
    tests/models/test_gemma3_4b_it.py tests/test_gemma3_vl_support.py
# All checks passed!

Additional end-to-end checks on the same box:

  • LAION image calibration with offload_to_disk=True, full 34-layer
    quantization, save, reload, then image QA: the quantized answer matches the
    bf16 baseline (AutoModelForImageTextToText) for the same image.
  • Plain-text calibration ({"text": ...} rows and bare strings) still
    quantizes 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) to 0.4846 on this checkpoint. That is the
calibration distribution change, not a quantization regression -- the bf16
baseline prompt and output match the quantized output, acc is bit-identical
across repeated slow-mode runs, and acc_norm varies by only ~0.1%
(0.5026-0.5034) between runs. floor_pct: 0.04 leaves room for that spread
while 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:

  • Reviewed this PR.
  • Checked that the code matches existing project structure, APIs, and conventions.
  • Avoided unnecessary monkeypatching and used the project's normal extension points where possible.
  • Minimized and compacted the impacted code surface.
  • Considered and eliminated potential regressions.

Kernel Accuracy Requirements

  • Not applicable: this change touches model definition, calibration data
    plumbing and test scaffolding only. No quantization math, weight packer or
    inference kernel is modified.

…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.
@ZX-ModelCloud
ZX-ModelCloud marked this pull request as ready for review October 15, 2026 14:11
@Qubitium
Qubitium merged commit a5d5d73 into main Oct 16, 2026
6 checks passed
@Qubitium
Qubitium deleted the fix/gemma3-vl-modality-1465 branch October 16, 2026 06:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Compat] Gemma 3 (VL) not supported: Only Gemma 3 text-only supported for now (1B)

2 participants