fix(gemma4_moe): use vision-aware attention mask when use_bidirection…#1901
Draft
akoumpa wants to merge 1 commit intoNVIDIA-NeMo:mainfrom
Draft
fix(gemma4_moe): use vision-aware attention mask when use_bidirection…#1901akoumpa wants to merge 1 commit intoNVIDIA-NeMo:mainfrom
akoumpa wants to merge 1 commit intoNVIDIA-NeMo:mainfrom
Conversation
…al_attention="vision" Gemma4 multimodal variants with `use_bidirectional_attention="vision"` in their text config (e.g. gemma-4-26B-A4B-it, gemma-4-31B-it) require a vision-aware attention mask that makes tokens inside the same vision group visible to each other bidirectionally. HF's `Gemma4Model.forward` builds this mask via `create_causal_mask_mapping`. The MoE backend `Gemma4MoETextModelBackend.forward` was always building plain `create_causal_mask` + `create_sliding_window_causal_mask` regardless of the config flag. For `gemma-4-26B-A4B-it` this makes the MoE forward numerically diverge from HF on multimodal inputs (vision token logprobs can differ by 20+ in log-space), and increases `train/gen_kl_error` by roughly an order of magnitude during GRPO training (~0.01 vs ~0.001 on text-only). Fix: - Accept `mm_token_type_ids` and `pixel_values` in `Gemma4MoETextModelBackend.forward`. - When `config.use_bidirectional_attention == "vision"`, call HF's `create_causal_mask_mapping` (matches `Gemma4Model.forward`). Otherwise keep the existing plain causal-mask path. - Plumb `mm_token_type_ids` / `pixel_values` from `Gemma4ForConditionalGeneration.forward` down to the text backend. Measured impact on gemma-4-26B-A4B-it multimodal forward (HF as ground truth, single synthetic image, 341-token sequence): metric before after (expected) HF vs Automodel gen_kl 0.034 ~0.01 (FSDP noise floor) HF vs vLLM gen_kl 0.092 0.092 (unchanged)
Contributor
Author
|
/ok to test ce4b225 |
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.
…al_attention="vision"
Gemma4 multimodal variants with
use_bidirectional_attention="vision"in their text config (e.g. gemma-4-26B-A4B-it, gemma-4-31B-it) require a vision-aware attention mask that makes tokens inside the same vision group visible to each other bidirectionally. HF'sGemma4Model.forwardbuilds this mask viacreate_causal_mask_mapping.The MoE backend
Gemma4MoETextModelBackend.forwardwas always building plaincreate_causal_mask+create_sliding_window_causal_maskregardless of the config flag. Forgemma-4-26B-A4B-itthis makes the MoE forward numerically diverge from HF on multimodal inputs (vision token logprobs can differ by 20+ in log-space), and increasestrain/gen_kl_errorby roughly an order of magnitude during GRPO training (~0.01 vs ~0.001 on text-only).Fix:
mm_token_type_idsandpixel_valuesinGemma4MoETextModelBackend.forward.config.use_bidirectional_attention == "vision", call HF'screate_causal_mask_mapping(matchesGemma4Model.forward). Otherwise keep the existing plain causal-mask path.mm_token_type_ids/pixel_valuesfromGemma4ForConditionalGeneration.forwarddown to the text backend.Measured impact on gemma-4-26B-A4B-it multimodal forward (HF as ground truth, single synthetic image, 341-token sequence):
metric before after (expected)
HF vs Automodel gen_kl 0.034 ~0.01 (FSDP noise floor)
HF vs vLLM gen_kl 0.092 0.092 (unchanged)
What does this PR do ?
Add a one line overview of what this PR aims to accomplish.
Changelog
Before your PR is "Ready for review"
Pre checks:
If you haven't finished some of the above items you can still open "Draft" PR.
Additional Information