vllm - ✅(Solved) Fix [Bug]: Gemma4 multimodal: missing vision-aware bidirectional attention mask for use_bidirectional_attention="vision" models [1 pull requests, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vllm-project/vllm#40106Fetched 2026-04-18 05:52:35
View on GitHub
Comments
0
Participants
1
Timeline
2
Reactions
0
Author
Participants
Timeline (top)
cross-referenced ×1labeled ×1

PR fix notes

PR #40185: gemma4: Enable mm prefix-lm masking for vision bidirectional attention

Description (problem / solution / changelog)

Purpose

Fix Gemma4 multimodal attention masking when the HF text config enables vision-only bidirectional attention (use_bidirectional_attention="vision").

This PR sets hf_config.is_mm_prefix_lm = True during Gemma4Config.verify_and_update_config when use_bidirectional_attention == "vision", which activates vLLM’s existing multimodal prefix-LM masking path (via mm_prefix_range) so that vision tokens can attend bidirectionally within the multimodal prefix while preserving causal behavior elsewhere.

Fixes: https://github.com/vllm-project/vllm/issues/40106

Test Plan

Run the unit test: pytest -q --noconftest tests/test_gemma4_mm_prefix_lm.py

Test Result

Ran in Docker (clean env): docker run --rm -v ${PWD}:/repo -w /repo python:3.10-slim bash -lc "python -m pip install -q --upgrade pip && pip install -q pytest numpy && pip install -q torch --index-url https://download.pytorch.org/whl/cpu && PYTHONPATH=/repo pytest -q --noconftest tests/test_gemma4_mm_prefix_lm.py"

Output: 2 passed, 1 warning in 7.13s

Changed files

  • tests/test_gemma4_mm_prefix_lm.py (added, +45/-0)
  • vllm/model_executor/models/config.py (modified, +7/-0)
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

For Gemma4 checkpoints whose text config has use_bidirectional_attention="vision" (e.g. gemma-4-31B-it, gemma-4-26B-A4B-it), vLLM's Gemma4 implementation silently ignores the flag and runs standard causal attention on vision tokens. HF transformers' Gemma4Model.forward calls create_causal_mask_mapping, which makes tokens inside the same vision group bidirectionally visible to each other. The vLLM forward diverges from HF on multimodal inputs as a result.

This affects any multimodal use case (generation, logprobs) and causes significant numerical divergence compared to the HF reference, concentrated on image token positions.

Reproduction

Running a teacher-forcing forward pass on gemma-4-31B-it (dense, use_bidirectional_attention="vision") and gemma-4-26B-A4B-it (MoE, same flag) with a multimodal prompt (a synthetic 224×224 image + text instruction), and comparing HF logprobs to vLLM logprobs on the generation tokens:

| model | KL(P_HF || P_vLLM) on generation tokens | |---|---| | gemma-4-E4B-it (no bidir) | 0.0004 | | gemma-4-31B-it (bidir=vision) | ~0.03 | | gemma-4-26B-A4B-it (bidir=vision) | ~0.09 |

Before submitting a new issue...

  • Make sure you already searched for relevant issues, and asked the chatbot living at the bottom right corner of the documentation page, which can answer lots of frequently asked questions.

extent analysis

TL;DR

The issue can be addressed by modifying the vLLM's Gemma4 implementation to correctly handle the use_bidirectional_attention="vision" flag for multimodal inputs.

Guidance

  • Review the Gemma4Model.forward method in vLLM to ensure it correctly implements bidirectional attention for vision tokens when the use_bidirectional_attention="vision" flag is set.
  • Compare the vLLM implementation with the HF transformers' implementation to identify the divergence point.
  • Test the modified implementation using the provided reproduction steps to verify the fix.
  • Consider adding additional tests to cover different multimodal use cases and ensure the fix does not introduce new issues.

Example

No code snippet is provided as the issue does not contain sufficient information to create a specific example.

Notes

The fix may require modifications to the vLLM's Gemma4 implementation, and thorough testing is necessary to ensure the change does not introduce new issues.

Recommendation

Apply a workaround by modifying the vLLM's Gemma4 implementation to correctly handle the use_bidirectional_attention="vision" flag, as this will allow for correct functionality in multimodal use cases.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING