Repository navigation
[Model] Gemma 4: native packed vision tower on MMEncoderAttention - #1415
Draft
roberteg16 wants to merge 5 commits into
Draft
roberteg16 wants to merge 5 commits into
roberteg16 wants to merge 5 commits into
Conversation
roberteg16
force-pushed
the
rogarcia.gemma4-native-vision
branch
7 times, most recently
from
October 30, 2026 09:08
d94b1ad to
b5cd8b7
Compare
roberteg16
changed the base branch from
rogarcia.upstream-main-3c39dda
to
rogarcia.mm-encoder-graph-infra
October 30, 2026 09:29
roberteg16
force-pushed
the
rogarcia.gemma4-native-vision
branch
from
October 30, 2026 11:37
b5cd8b7 to
33db17f
Compare
roberteg16
force-pushed
the
rogarcia.mm-encoder-graph-infra
branch
2 times, most recently
from
November 1, 2026 10:02
3296c7e to
84ee5f6
Compare
roberteg16
force-pushed
the
rogarcia.gemma4-native-vision
branch
from
November 1, 2026 10:02
33db17f to
b37ab27
Compare
roberteg16
force-pushed
the
rogarcia.mm-encoder-graph-infra
branch
from
November 1, 2026 10:02
84ee5f6 to
291a2fd
Compare
roberteg16
force-pushed
the
rogarcia.gemma4-native-vision
branch
from
November 1, 2026 10:02
b37ab27 to
768f58c
Compare
roberteg16
force-pushed
the
rogarcia.mm-encoder-graph-infra
branch
from
November 2, 2026 12:58
291a2fd to
4e21958
Compare
roberteg16
force-pushed
the
rogarcia.gemma4-native-vision
branch
2 times, most recently
from
November 2, 2026 16:57
0f1b4e7 to
69150d7
Compare
roberteg16
changed the base branch from
rogarcia.mm-encoder-graph-infra
to
rogarcia.upstream-main-3c39dda
November 2, 2026 16:57
roberteg16
force-pushed
the
rogarcia.gemma4-native-vision
branch
from
November 3, 2026 17:07
69150d7 to
7f3503e
Compare
roberteg16
changed the base branch from
rogarcia.upstream-main-3c39dda
to
rogarcia.upstream-main-c1d8fad
November 3, 2026 17:08
roberteg16
force-pushed
the
rogarcia.gemma4-native-vision
branch
from
November 3, 2026 21:51
7f3503e to
a65ccd1
Compare
roberteg16
changed the base branch from
rogarcia.upstream-main-c1d8fad
to
rogarcia.upstream-main-81a2879
November 3, 2026 21:51
roberteg16
force-pushed
the
rogarcia.gemma4-native-vision
branch
from
November 6, 2026 13:46
a65ccd1 to
5e8b91d
Compare
roberteg16
changed the base branch from
rogarcia.upstream-main-81a2879
to
rogarcia.upstream-main-1b2505c
November 6, 2026 13:46
…oderAttention The CUDA graph padding for cu_seqlens that Qwen2.5-VL and Qwen3-VL share (cumulative, or FlashInfer's two-section layout) depends on the layout maybe_recompute_cu_seqlens produces, so it moves from qwen2_5_vl.py next to it as MMEncoderAttention.cu_seqlens_buffer_padding(backend), and models no longer branch on the attention backend to pick it. No functional change. Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Robert Esclapez Garcia <robert.garcia@amd.com>
- EncoderItemSpec.eager_only lets a model send items its captured graphs cannot hold (e.g. longer than the captured max_seqlen) to the eager path; the manager packs the other items as before. - TORCH_SDPA splits packed sequences (cu_seqlens) on the host, which cannot run under CUDA graph capture: Qwen2.5-VL or Qwen3-VL with TORCH_SDPA (the default ViT backend on RDNA without the flash-attn Triton backend) and cudagraph_mm_encoder failed at startup with a device-copy error. The wrapper now raises an error that names the backend and the way out. Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Robert Esclapez Garcia <robert.garcia@amd.com>
Replace the transformers vision tower (padded images, SDPA with a mask)
with a native one built from vLLM layers that packs all images or video
frames of a batch into one sequence: cu_seqlens instead of padding,
MMEncoderAttention (any --mm-encoder-attn-backend), the axial 2D RoPE
from get_rope applied with ApplyRotaryEmb, and moonvit's packed patch
merger for pooling. Parameter names match transformers, so checkpoints
load without a mapper.
Encoder CUDA graphs move to token budgets (as in Qwen3-VL): eager,
capture and replay share prepare_encoder_metadata and the tower forward.
Sequence slots and max_seqlen are sized from what a budget can hold and
the configured max_soft_tokens; other items are marked eager_only.
TORCH_SDPA cannot capture packed images, so cudagraph_mm_encoder with it
now stops at startup with an error naming another backend.
With mm_device_do_normalize, pixels reach the tower as raw uint8 and are
normalized per channel in _embed_vision, shared by the eager and CUDA
graph paths; the CUDA graph buffers keep uint8. The data-parallel encoder
dispatch wraps the packed _process_{image,video}_input_local.
DiffusionGemma and EmbeddingGemma2 reuse the same tower; Gemma 4 Unified
moves the (now host-side) positions to the device and opts out of
encoder graphs.
Co-authored-by: Hoang Nguyen Tien <hoang.nguyentien.2601@gmail.com>
Co-Authored-By: Claude <noreply@anthropic.com>
Signed-off-by: Robert Esclapez Garcia <robert.garcia@amd.com>
Signed-off-by: Hoang Nguyen Tien <hoang.nguyentien.2601@gmail.com>
Packing every image or frame of an encoder batch into one tower call raised the encoder's peak activations: on gemma-4-31B-it (H100, max_model_len 8192, gpu_memory_utilization 0.9) the profile run's peak activation memory grew from 2.92 GiB to 3.96 GiB and the KV cache shrank 11.7%. Eager calls now pack at most 4 x 1120 soft tokens. With tower LoRA the batch stays one call, since the adapter mapping covers the whole encoder batch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Hoang Nguyen Tien <hoang.nguyentien.2601@gmail.com>
roberteg16
force-pushed
the
rogarcia.gemma4-native-vision
branch
from
November 6, 2026 14:15
5e8b91d to
3bb1277
Compare
roberteg16
changed the base branch from
rogarcia.upstream-main-1b2505c
to
rogarcia.upstream-main-b2094ac
November 6, 2026 14:16
Restore the empty-input guards the local image and video paths had: with no images, or a video with no frames, _encode_vision returns no soft tokens instead of packing an empty batch, which failed in np.concatenate. The data-parallel paths already skipped empty inputs; the local ones are also reached directly from DiffusionGemma and EmbeddingGemma2. Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Robert Esclapez Garcia <robert.garcia@amd.com>
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.
Overview
Gemma 4's vision tower becomes a native vLLM tower that packs images and video frames with
cu_seqlensinstead of padding them, so it runs on any--mm-encoder-attn-backend(vllm-project#43178, review of vllm-project#43440).Not a duplicate: vllm-project#60577 keeps SDPA with a mask and is superseded; vllm-project#60478 patches the transformers tower this PR removes. Tower LoRA (vllm-project#42662), the data-parallel encoder (vllm-project#60314) and on-device input normalization (vllm-project#65577) keep working on the new tower.
Claims
MMEncoderAttention,get_rope+ApplyRotaryEmb, moonvit's packed merger); same checkpoints, no mapper. Reused by DiffusionGemma and EmbeddingGemma2.TRITON_ATTN, which picks up gfx1151's encoder attention work ([ROCm][Perf] Encoder prefill attention on gfx1151: split-D head dims, alignment hint, launch config vllm-project/vllm#56450).TORCH_SDPAundercudagraph_mm_encodernow fails at startup with a clear error (it used to fail with a device-copy error during capture).Validation
gfx1151, PR on 1b2505c vs that commit ("main"), torch 2.14 (ROCm nightly 10.3.0a20261106); unit tests,
test_commonand LoRA re-run after rebasing on b2094ac.TRITON_ATTN(ms)MMStar: first 300 questions, greedy, 64 tokens. TTFT: one 336x336 image per request, median of 15, stdev ≤ 13.4 ms (1024x800 gives the same gains). Scripts below.
(*)
test_mha_attn_platform[hip]deselected: needsflash_attn, fails the same on main.(**) gfx1151 defaults to
TORCH_SDPA, which now stops with the new error; on main Qwen2.5-VL fails there with "Cannot copy between CPU and CUDA tensors during CUDA graph capture".Also checked: encoder graph replay matches eager (images, videos, oversized items);
VLLM_GPU_SYNC_CHECK=errorpasses; 31B AWQ withmax_model_len=8192keeps main's KV cache (7904 vs 7902 blocks);--dtype float16on 26B AWQ works.On H100, hnt2601 (vllm-project#65591) ran 31B BF16 with FLASH_ATTN, FLASHINFER and TRITON_ATTN, eager and with encoder graphs, matching main within bf16 noise, and DP (
mm_encoder_tp_mode="data", TP=2) matching"weights". Online FP8 (vllm-project#64168) cannot run on gfx1151 (no kernel);test_patch_embedder_keeps_model_dtype_with_fp8_input_projchecks that pixels reach an FP8-storedinput_projin the model dtype, and fails with the pre-vllm-project#64168 cast.MMStar and TTFT scripts
Details
Four commits:
cu_seqlensgraph-buffer padding shared by Qwen2.5-VL and Qwen3-VL toMMEncoderAttention.cu_seqlens_buffer_padding(backend).EncoderItemSpec.eager_onlyand the clearTORCH_SDPAerror.gemma4_vision.py(440 lines) whilegemma4_mm.pydrops 689. Removes the resolution buckets,_encoder_chunk, the tower LoRA padding and the row-width graph axis. Raw uint8 pixels are normalized on the device in_embed_vision, shared by eager and graph replay; the DP dispatch wraps the packed_process_*_input_local. Graphs size their sequence slots (budget // 35) andmax_seqlen(configuredmax_soft_tokens) to what they can hold; larger items run eager.max_model_len=8192failed to start without it). Tower LoRA keeps one call.Trade-offs:
TORCH_SDPA(RDNA default) andcudagraph_mm_encoder(off by default) now needs another backend, e.g.TRITON_ATTN; documented indocs/design/cuda_graphs_multimodal.md.GELUTanhis used directly becauseget_act_fnswaps in exact GELU on ROCm. vLLM'sRMSNormrounds differently from transformers', which MMStar covers.separate_qkvflag onmaybe_recompute_cu_seqlens; GQA is rejected.AI assistance (Claude Code) was used; the submitter reviewed every line and ran the tests above.
🤖 Generated with Claude Code
Pull Request Checklist
I used vLLM's
/pr-checklistskill. (Mandatory for agents, optional for humans).AI assistance was used during the creation of this PR.
Design Fit: Minimizes impact on core components, reuses existing functionality, and justifies added complexity.
Testing and Validation: Validates the change and ensures any added tests are meaningful and reliable, with CI coverage or documented CI resource constraints and validation performed outside CI.
Code Quality and Style: Keeps code and comments clear and concise, and updates relevant documentation and examples.
Pull Request Contents: Includes a brief summary and relevant links, supports claims with evidence, explains root causes and implementation trade-offs, and follows the contributing guide.