Skip to content

[Model] Add Gemma4 Unified (encoder-free) support - #44429

Merged
vllm-bot merged 3 commits into
vllm-project:mainfrom
lucianommartins:lucianommartins/gemma4-unified
Jun 3, 2026
Merged

vllm-bot merged 3 commits into
vllm-project:mainfrom
lucianommartins:lucianommartins/gemma4-unified

Conversation

@lucianommartins

Copy link
Copy Markdown
Collaborator

Summary

Adds Gemma4UnifiedForConditionalGeneration support for the encoder-free Gemma 4 12B model family. Unlike the tower-based Gemma 4 variants (E2B, E4B, 26B-A4B, 31B), the unified variant has no vision encoder and no audio encoder: raw pixel patches and audio waveform frames are projected directly into LM space.

Architecture

  • Gemma4UnifiedForConditionalGeneration subclasses Gemma4ForConditionalGeneration using the nn.Module.__init__ + full rebuild pattern (same approach as LightOnOCRForConditionalGeneration), keeping the parent class free of encoder-free branching
  • Vision: Gemma4UnifiedVisionEmbedder replaces the vision tower with a Dense+LayerNorm pipeline and factorized 2D positional embeddings
  • Audio: raw 16kHz waveform frames projected through Gemma4MultimodalEmbedder with no conformer audio tower
  • Gemma4UnifiedProcessingInfo subclasses Gemma4ProcessingInfo to handle the num_soft_tokens vs default_output_length field mapping
  • All modalities (image, video, audio) are supported

Changes

  • New file: vllm/model_executor/models/gemma4_unified.py (466 lines) with Gemma4UnifiedVisionEmbedder, Gemma4UnifiedProcessingInfo, and Gemma4UnifiedForConditionalGeneration
  • gemma4_mm.py: SigLIP-style quantization check for vision/audio towers (BNB/torchao always quantize, others check dimensions % 64); suppress_tokens from generation_config applied in compute_logits
  • gemma4_mtp.py: attention_k_eq_v + num_global_key_value_heads handling for full-attention MTP decoder layers; suppress_tokens applied in drafter compute_logits; gemma4_unified_assistant mapped to Gemma4MTPModel
  • gpu_model_runner.py: audio modality skip for bidirectional attention ranges
  • Registry/config: Gemma4UnifiedForConditionalGeneration registered in model registry, config map, arch config convertor, and spec decode proposer
  • Docs: updated supported_models.md and mtp.md with unified variant and 12B assistant model

Duplicate-work check

No existing PR addresses Gemma 4 Unified / encoder-free support. This is the initial implementation of Gemma4UnifiedForConditionalGeneration in vLLM.

Test commands and results

# Processing tests (unified image schema, soft-token caps, prompt updates, limit_mm_per_prompt)
python -m pytest tests/models/multimodal/processing/test_gemma4_unified.py -v

# Lint
ruff check vllm/model_executor/models/gemma4_unified.py vllm/model_executor/models/gemma4_mm.py vllm/model_executor/models/gemma4_mtp.py
ruff format --check vllm/model_executor/models/gemma4_unified.py vllm/model_executor/models/gemma4_mm.py vllm/model_executor/models/gemma4_mtp.py

@mergify

mergify Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Documentation preview: https://vllm--44429.org.readthedocs.build/en/44429/

@mergify mergify Bot added documentation Improvements or additions to documentation multi-modality Related to multi-modality (#4194) new-model Requests to new models speculative-decoding v1 labels Jun 3, 2026
@mergify

mergify Bot commented Jun 3, 2026

Copy link
Copy Markdown
Contributor

Hi @lucianommartins, the pre-commit checks have failed. Please run:

uv pip install pre-commit>=4.5.1
pre-commit install
pre-commit run --all-files

Then, commit the changes and push to your branch.

For future commits, pre-commit will run automatically on changed files before each commit.

Tip

Is mypy failing?
mypy is run differently in CI. If the failure is related to this check, please use the following command to run it locally:
# For mypy (substitute "3.10" with the failing version if needed)
pre-commit run --hook-stage manual mypy-3.10

Comment thread tests/models/registry.py Resolved
torch.tensor(config.hidden_size**0.5),
torch.tensor(
config.hidden_size**0.5,
dtype=self.embed_tokens.weight.dtype,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use a different dtype source here? If embed_tokens is quantized, then "weight" won't exist right. Same problem I found in #44340

Comment on lines +1015 to +1030
# Only quantize towers when the quant method supports their
# dimensions. BNB/torchao handle arbitrary sizes; other methods
# (Marlin, FP8, …) require dimensions divisible by 64, which
# the vision tower (intermediate_size=4304) does not satisfy.
if quant_config and quant_config.get_name() in [
"bitsandbytes",
"torchao",
]:
tower_quant = quant_config
else:
vision_cfg = config.vision_config
quantizable = (
vision_cfg.hidden_size % 64 == 0
and vision_cfg.intermediate_size % 64 == 0
)
tower_quant = quant_config if quantizable else None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add this padding support to the quant methods rather than specializing specific quant configs like this, this is a kernel limitation not a checkpoint limitation. If the checkpoint is quantized but we say the layers aren't quantized, then the model will be incorrectly loading weights. We should instead mark those layers in the ignore list in the checkpoint format i.e. compressed-tensors

Comment thread vllm/model_executor/models/gemma4_mm.py Resolved
@mgoin mgoin added the ready ONLY add when PR is ready to merge/full CI is needed label Jun 3, 2026
Comment on lines +2469 to +2470
if mm_feature.modality == "audio":
continue

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this affect the previous gemma4-with-audio behavior? Maybe worth testing E2B on audio quickly

Comment on lines +369 to +376
for pv, pp in zip(pixel_values, pixel_position_ids, strict=True):
pv = pv.unsqueeze(0)
pp = pp.unsqueeze(0)
embedded = self.vision_embedder(pv, pp)
projected = self.embed_vision(embedded.to(target_dtype))
padding_mask = (pp.squeeze(0) == -1).all(dim=-1)
valid_features = projected.squeeze(0)[~padding_mask]
per_image_features.append(valid_features)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perf nit: for both image and video we are looping one image/frame at a time through the vision embedder, one forward per item. we should consider batching in the future

)
if config.audio_config is not None
else None
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are constructed without quant_config where it is passed through in gemma4_mm, so it could be supported the same way I think. If intentional should note

- Add Gemma4UnifiedForConditionalGeneration as a subclass of
  Gemma4ForConditionalGeneration, following the
  (nn.Module.__init__ + full rebuild, no super().__init__) pattern
- Encoder-free vision: Gemma4UnifiedVisionEmbedder projects raw pixel
  patches to LM space via Dense+LayerNorm with factorized 2D positional
  embeddings, replacing the SigLIP vision tower
- Encoder-free audio: raw 16kHz waveform frames projected directly
  through Gemma4MultimodalEmbedder, no conformer audio tower
- Subclassed Gemma4UnifiedProcessingInfo with num_soft_tokens field
  mapping (unified uses num_soft_tokens, tower-based uses
  default_output_length)
- Register Gemma4UnifiedForConditionalGeneration in model registry,
  config map, arch config convertor, and spec decode proposer
- Add suppress_tokens support from generation_config in both target
  (gemma4_mm.py) and drafter (gemma4_mtp.py) compute_logits to prevent
  stray audio/image token generation
- Map gemma4_unified_assistant to Gemma4MTPModel for speculative
  decoding with shared KV cache
- Handle attention_k_eq_v with num_global_key_value_heads in MTP
  drafter decoder layers for full-attention layers
- Restore bidi sliding-window guard in gpu_model_runner.py and add
  audio modality skip for bidirectional attention ranges
- Use SigLIP-style quantization check for vision/audio towers:
  BNB/torchao always quantize, others require dimensions divisible
  by 64
- Inline audio token count computation to remove dependency on
  transformers processor internals
- Add processing tests for unified image schema, soft-token caps,
  prompt updates, and limit_mm_per_prompt
- Update supported models docs and MTP speculative decoding docs
)

Signed-off-by: Luciano Martins <lucianommartins@users.noreply.github.com>
@lucianommartins
lucianommartins force-pushed the lucianommartins/gemma4-unified branch from 0f206d9 to f39dc8e Compare June 3, 2026 16:30
    - Add Gemma4UnifiedForConditionalGeneration as a subclass of
      Gemma4ForConditionalGeneration, following the
      (nn.Module.__init__ + full rebuild, no super().__init__) pattern
    - Encoder-free vision: Gemma4UnifiedVisionEmbedder projects raw pixel
      patches to LM space via Dense+LayerNorm with factorized 2D positional
      embeddings, replacing the SigLIP vision tower
    - Encoder-free audio: raw 16kHz waveform frames projected directly
      through Gemma4MultimodalEmbedder, no conformer audio tower
    - Subclassed Gemma4UnifiedProcessingInfo with num_soft_tokens field
      mapping (unified uses num_soft_tokens, tower-based uses
      default_output_length)
    - Register Gemma4UnifiedForConditionalGeneration in model registry,
      config map, arch config convertor, and spec decode proposer
    - Add suppress_tokens support from generation_config in both target
      (gemma4_mm.py) and drafter (gemma4_mtp.py) compute_logits to prevent
      stray audio/image token generation
    - Map gemma4_unified_assistant to Gemma4MTPModel for speculative
      decoding with shared KV cache
    - Handle attention_k_eq_v with num_global_key_value_heads in MTP
      drafter decoder layers for full-attention layers
    - Restore bidi sliding-window guard in gpu_model_runner.py and add
      audio modality skip for bidirectional attention ranges
    - Use SigLIP-style quantization check for vision/audio towers:
      BNB/torchao always quantize, others require dimensions divisible
      by 64
    - Inline audio token count computation to remove dependency on
      transformers processor internals
    - Add processing tests for unified image schema, soft-token caps,
      prompt updates, and limit_mm_per_prompt
    - Update supported models docs and MTP speculative decoding docs

Signed-off-by: Luciano Martins <lucianommartins@users.noreply.github.com>
@lucianommartins

Copy link
Copy Markdown
Collaborator Author

hey @mgoin @ywang96 @zifeitong - the only failed test was the Llama TP+SP (sequence parallelism) test with Ray -- unrelated to this PR.

can we go ahead and merge it?

@vllm-bot
vllm-bot merged commit a248b45 into vllm-project:main Jun 3, 2026
75 of 77 checks passed
@ThomasKluiters

ThomasKluiters commented Jun 3, 2026 •

Copy link
Copy Markdown

When audio requests are batched together I obtain an error. input_features_padded is a list, but
_process_audio_input() assumes a tensor and calls .squeeze(1) though, this has been an issue with previous gemma versions as well

@lucianommartins
lucianommartins deleted the lucianommartins/gemma4-unified branch June 3, 2026 19:25
@Sunt-ing Sunt-ing mentioned this pull request Jun 4, 2026
1 task done
0Shark added a commit to MediSync-AI/gemma-cookbook that referenced this pull request Jun 11, 2026
…rride

Smoke test on 4x L4 caught it: v0.22.1 has no native gemma-4-12B
implementation and silently falls back to the generic Transformers
multimodal backend, which crashes under tensor parallelism and would
misrepresent the 12B in single-GPU comparisons. Native support merged
in vllm-project/vllm#44429 (nightly only). The registry can now pin a
per-model image; gemma-4-12b overrides to nightly until the next
stable release.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
0Shark added a commit to MediSync-AI/gemma-cookbook that referenced this pull request Jun 12, 2026
…rride

Smoke test on 4x L4 caught it: v0.22.1 has no native gemma-4-12B
implementation and silently falls back to the generic Transformers
multimodal backend, which crashes under tensor parallelism and would
misrepresent the 12B in single-GPU comparisons. Native support merged
in vllm-project/vllm#44429 (nightly only). The registry can now pin a
per-model image; gemma-4-12b overrides to nightly until the next
stable release.
nkzhenhua pushed a commit to nkzhenhua/vllm that referenced this pull request Jun 24, 2026
Signed-off-by: Luciano Martins <lucianommartins@users.noreply.github.com>
Co-authored-by: Luciano Martins <lucianommartins@users.noreply.github.com>
@lucianommartins lucianommartins mentioned this pull request Jul 6, 2026
1 task done
philippesic pushed a commit to philippesic/vllm-semantic-cache that referenced this pull request Jul 19, 2026
Signed-off-by: Luciano Martins <lucianommartins@users.noreply.github.com>
Co-authored-by: Luciano Martins <lucianommartins@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation multi-modality Related to multi-modality (#4194) new-model Requests to new models ready ONLY add when PR is ready to merge/full CI is needed speculative-decoding v1

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants