Repository navigation
Muse Glimmer: add varlen (FA3) attention + vLLM generator support - #4126
Merged
Merged
Conversation
Muse Glimmer currently only supports FlexAttention: get_attention_masks raises TypeError for any other backend. This adds varlen (FA3) as an opt-in second backend, so the model can run the same attention path as the rest of TorchTitan and, in particular, serve as both trainer and vLLM generator from one ModelSpec. Mirrors gpt_oss, which supports both backends the same way. Builds on pytorch#4106 (Add Muse Glimmer (30B) model and integration tests). The two backends express the per-layer sliding window differently: - flex encodes it in the mask -- a window-keyed dict of BlockMasks that Attention.forward indexes by its own window. - varlen carries it as an FA3 kernel argument, so every layer shares a single VarlenMetadata describing document boundaries. Changes: - __init__.py: for the varlen backend, bake each layer's window into VarlenAttention.window_size as (window - 1, 0) at build time. Full-attention layers keep the (-1, 0) default. The flex build path is untouched. - model.py, get_attention_masks: varlen branch returning a single create_varlen_metadata_for_document(positions). - model.py, Attention.forward: only index the mask when it is a dict. Varlen passes a VarlenMetadata straight through to the kernel. Required for varlen to work at all (assert -> if). flex remains the default and is unchanged; varlen is opt-in via model_registry(..., attn_backend="varlen"). Test Plan: Per-layer window bake on the 30B spec: 39 sliding -> (2047, 0), 13 full -> (-1, 0), zero mismatches. Flex still reports the untouched default. flex-vs-varlen parity on the debugmodel (identical weights, bf16 forward, exercising both sliding and full layers; masks: flex=dict, varlen=VarlenMetadata): loss 1.060066 / 1.059845 rel 2.1e-04 grad_norm 45.6010 / 45.8513 rel 5.5e-03 logits cosine 0.999538 rel-L2 3.0e-02 Not bitwise identical by construction: FA3 varlen is bf16-only and a different kernel from flex's Triton, so the bar is training-equivalence rather than identity. Per-element differences accumulate with depth (layer-0 attention output agrees to ~1e-4), and the same profile appears at seq_len < window, where the sliding mask provably clips nothing -- i.e. this is kernel-substitution noise, not window semantics.
Follow-up to the varlen commit. Together they let one ModelSpec drive both the TorchTitan RL trainer and the vLLM generator, which is what guarantees the rollout policy is the policy being trained. Mirrors qwen3/gpt_oss. - model.py: sliding_window_size property on Attention.Config, aliasing window_size. The vLLM wrapper reads sliding_window_size to configure per-layer paged-attention windows; Muse Glimmer named the field window_size. A property alias keeps every existing window_size use working (gpt_oss uses the same field name). - parallelize.py: skip_dp flag that returns before FSDP wrapping. The generator wants TP but not FSDP -- FSDP's forward hooks are incompatible with the torch.inference_mode() vLLM runs under. Same shape as gpt_oss. No change is needed in the RL generator itself: it already selects the vLLM attention backend from the inner attention type (flex -> FLEX_ATTENTION, else CUSTOM/varlen), with no model-name special-casing. Test Plan: Bitwise parity, trainer == vLLM generator, via the existing harness in torchtitan/experiments/rl/tests/test_bitwise_parity.py. Subclassed BitwiseParityTestBase with a Muse Glimmer debugmodel config (varlen both roles, TP=2, batch-invariant, weights synced trainer -> vLLM), mirroring TestBitwiseParityGptOssVarlen: torchrun --nproc_per_node=2 -m pytest -s <test> -v -> 3 passed test_batch_invariance prefill(bsz=2) vs prefill(bsz=5) test_trainer_vs_vllm_prefill trainer vs vLLM, 5/5 sequences test_vllm_decode_vs_prefill decode vs 2nd-pass prefill, 5/5 sequences every comparison max_delta = 0.00e+00, bitwise_equal=True By transitivity of the last two, trainer == vLLM decode bitwise -- the generator serves numerically the same model the trainer updates, which is the correctness invariant RL depends on. Also verified: parallelize_muse_glimmer exposes skip_dp: bool = False and returns before FSDP when set; sliding_window_size tracks window_size on both layer kinds (2048 on a sliding layer, None on a full-attention layer); the flex path is unaffected. Note: generator TP is capped at 2 by the model's 2 KV heads. The parity subclass above is a few lines over the existing harness and is not included in this diff; happy to add it as a test if reviewers would like it checked in.
HosseinKaviani-H
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
August 12, 2026 18:46
tianyu-l
reviewed
Aug 12, 2026
tianyu-l
left a comment
Contributor
There was a problem hiding this comment.
does this follow how gpt-oss handles swa?
Contributor
Author
@tianyu-l Yes. Three varlen mechanics match gpt-oss: the (window - 1, 0) bake guarded by isinstance(.., VarlenAttention.Config), the create_varlen_metadata_for_document(positions) branch in get_attention_masks, and the if isinstance(attention_masks, dict) guard before indexing (gpt-oss already does this at model.py:181). The flex path differs slightly: Muse_glimmer indexes the mask dict in Attention.forward and keys it by window value (supporting multiple windows) vs gpt-oss's block-level attn_mask_key with one window. This part doesn't touch varlen tho. |
fegin
approved these changes
Aug 12, 2026
tianyu-l
approved these changes
Aug 12, 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.
Summary
Muse Glimmer currently supports only FlexAttention —
get_attention_masksraisesTypeErrorfor anything else. This adds varlen (FA3) as an opt-in second backend and makes the model servable by the TorchTitan RL vLLM generator, so a single ModelSpec drives both trainer and generator. Mirrors howgpt_ossalready supports both.Builds on #4106.
Two commits, reviewable independently:
1.
Add varlen (FA3) attention support— the two backends express the per-layer sliding window differently: flex puts it in the mask (a window-keyed dict ofBlockMasks), varlen puts it in an FA3kernel arg with one shared
VarlenMetadata.__init__.py— bake each layer's window intoVarlenAttention.window_sizeas(window-1, 0); full-attention layers keep(-1, 0)model.py— varlen branch inget_attention_masks;Attention.forwardonly indexes the mask when it's a dict (assert→if)2.
Let the vLLM generator serve the ModelSpecmodel.py—sliding_window_sizeproperty aliasingwindow_size(the vLLM wrapper reads that name)parallelize.py—skip_dpto bypass FSDP for inference (its forward hooks conflict withinference_mode)No change needed in the RL generator: it already picks the backend from the inner attention type, with no model-name special-casing.
flex remains the default and is unchanged; varlen is opt-in via
model_registry(..., attn_backend="varlen").Test plan
Bitwise parity, trainer == vLLM generator. Subclassed the existing
test_bitwise_parity.pyharness with a Muse Glimmer debugmodel config (varlen both roles, TP=2, batch-invariant, weights syncedtrainer → vLLM), mirroring
TestBitwiseParityGptOssVarlen:torchrun --nproc_per_node=2 -m pytest -s -v
test_batch_invariance prefill(bsz=2) vs prefill(bsz=5)
test_trainer_vs_vllm_prefill trainer vs vLLM, 5/5 sequences
test_vllm_decode_vs_prefill decode vs 2nd-pass prefill, 5/5 sequences
every comparison: max_delta = 0.00e+00, bitwise_equal=True
By transitivity of the last two, trainer == vLLM decode bitwise.
flex vs varlen on the debugmodel (identical weights, bf16 forward, exercising both sliding and full layers; masks:
flex=dict,varlen=VarlenMetadata):Not bitwise identical by construction: FA3 varlen is bf16-only and a different kernel from flex's Triton, so the bar is training-equivalence rather than identity. Layer-0 attention output agrees to ~1e-4, and the same profile appears at
seq_len < windowwhere the sliding mask provably clips nothing.Per-layer window bake on the 30B spec: 39 sliding →
(2047, 0), 13 full →(-1, 0), zero mismatches. Flex still reports the untouched default.Notes