Skip to content

[muse_glimmer] Change vision encoder forward internally - #4130

Merged
fegin merged 1 commit into
pytorch:mainfrom
jinsooihm:muse-glimmer-encoder-refactor
Aug 13, 2026
Merged

[muse_glimmer] Change vision encoder forward internally#4130
fegin merged 1 commit into
pytorch:mainfrom
jinsooihm:muse-glimmer-encoder-refactor

Conversation

@jinsooihm

Copy link
Copy Markdown
Contributor

Summary:

Although MuseGlimmerVisionEncoder takes in the same padded (N, P, D) (num images, max patches, latent dimension) formatted input as Qwen3.5 and Kimi2.7, internally, it ran in a packed (1, total_tokens, D) format.

This PR changes the internal behavior to keep (N, P, ...) shape throughout.
This improves consistency with other models and would make spmd_types enablement easier after #3895 and #4079.

Test Plan:

Verified numerical parity against the pre-refactor packed forward (max abs diff ~5e-7, well within 1e-4).

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 12, 2026
Summary:
Convert MuseGlimmerVisionEncoder.forward from the internal packed
(1, total_tokens, D) path to a padded (N, P, D) batch, matching qwen3_5 and
kimi_k2_7. Per-image block-diagonal attention is expressed via the batch dim
(get_vision_block_mask_mod); positional embeddings, complex 2D RoPE, and the
sparse-window permutation + window ids are built as padded (N, P, ...) tensors
via per-unique-(h,w) grouped helpers. The sparse-window permutation is applied
per row through _VisionTokenPermute.forward (which now dispatches on index.ndim:
1-D packed advanced-index or 2-D row-wise gather), keeping each window
contiguous so the FlexAttention mask stays block-sparse. Windows are un-permuted
and pixel-shuffle downsample loops per image, then valid tokens are flattened to
(total_output_tokens, output_dim). Public forward contract and model.py are
unchanged. Dead packed-path helpers (_embed_image, _finalize_image,
_block_diag_mask) and the unused create_attention_mask import are removed.

Test Plan:
Verified numerical parity against the pre-refactor packed forward with a golden
oracle (max abs diff ~5e-7, well within 1e-4), plus padded pos-emb / 2D-RoPE /
sparse-perm+win_id builder parity, token_permute dispatch, block-mask semantics,
TP sharding-config compatibility, model-level encoder->adapter forward, and
sparse-vs-global block-sparsity. Verification harness kept local (not landed).
@jinsooihm
jinsooihm force-pushed the muse-glimmer-encoder-refactor branch from 842c78c to 5910ed3 Compare August 12, 2026 22:26
@jinsooihm
jinsooihm marked this pull request as ready for review August 12, 2026 22:36
@fegin
fegin merged commit 2d8b2ec into pytorch:main Aug 13, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/8gpu CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants