Remove unnecessary expand_as in get_placeholder_mask across VLMs#44907
Open
syncdoth wants to merge 2 commits intohuggingface:mainfrom
Open
Remove unnecessary expand_as in get_placeholder_mask across VLMs#44907syncdoth wants to merge 2 commits intohuggingface:mainfrom
syncdoth wants to merge 2 commits intohuggingface:mainfrom
Conversation
The placeholder mask was being expanded from (B, S, 1) to (B, S, H) via `.expand_as(inputs_embeds)` before being passed to `masked_scatter`. Since `masked_scatter` natively supports broadcasting, this expansion materializes a large boolean tensor unnecessarily. Changes: - Remove `.expand_as(inputs_embeds)` from mask creation, keeping masks as (B, S, 1) and relying on `masked_scatter`/`torch.where` broadcasting - Replace `inputs_embeds[mask].numel() == features.numel()` validation with equivalent arithmetic `n_tokens * hidden_dim == features.numel()`, which avoids data-dependent boolean indexing and is more torch.compile-friendly
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: aria, aya_vision, blip_2, chameleon, cohere2_vision, colqwen2, deepseek_vl, deepseek_vl_hybrid, emu3, ernie4_5_vl_moe, fast_vlm, florence2, fuyu, gemma3, gemma3n, glm46v |
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Member
|
cc @zucchini-nlp it's an agent PR but looks high-quality to me, mostly because I'm a sucker for ways to avoid materializing broadcasts and tensor expansions 😅 |
Member
|
Yea, this should be mostly copy-paste. I'm coming to this later today |
This was referenced Apr 29, 2026
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.
Fixes #44906
Summary
.expand_as(inputs_embeds)from placeholder mask creation inget_placeholder_maskand equivalent inline patterns across all VLM models.masked_scatternatively broadcasts(B, S, 1)→(B, S, H), making the expansion unnecessary.inputs_embeds[special_image_mask].numel() == image_features.numel()validation with equivalent arithmeticn_tokens * inputs_embeds.shape[-1] == image_features.numel(), which avoids data-dependent boolean indexing and is moretorch.compile-friendly.How this was developed
The core fix was first implemented and verified on
llava/modeling_llava.py, then expanded to all other models following the same pattern using Claude Code. Each file was reviewed to ensure the transformation was appropriate — files with genuinely differentexpand_asusage (e.g.,pe_audiowhere the mask is later.reshape()-ed) were left unchanged.Test plan
masked_scatterwith broadcast(B,S,1)mask produces identical results to expanded(B,S,H)maskpytest tests/models/llava/test_modeling_llava.py -x -v -k "not slow"— 136 passedpytest tests/models/qwen2_vl/test_modeling_qwen2_vl.py -x -v -k "not slow"— 137 passedpytest tests/models/paligemma/test_modeling_paligemma.py -x -v -k "not slow"— 124 passedruff check— all checks passedcheck_modular_conversion.py --fix_and_overwrite— all generated files consistentgh pr list --search "expand_as placeholder mask"/"get_placeholder_mask")This PR uses AI assistance (Claude Code). I have reviewed all changes and validated the behavior end-to-end.