Skip to content

Muse Glimmer: add varlen (FA3) attention + vLLM generator support - #4126

Merged
tianyu-l merged 2 commits into
pytorch:mainfrom
HosseinKaviani-H:varlen_RL
Aug 12, 2026
Merged

tianyu-l merged 2 commits into
pytorch:mainfrom
HosseinKaviani-H:varlen_RL

Conversation

@HosseinKaviani-H

Copy link
Copy Markdown
Contributor

Summary

Muse Glimmer currently supports only FlexAttention — get_attention_masks raises TypeError for 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 how gpt_oss already 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 of BlockMasks), varlen puts it in an FA3
kernel arg with one shared VarlenMetadata.

  • __init__.py — bake each layer's window into VarlenAttention.window_size as (window-1, 0); full-attention layers keep (-1, 0)
  • model.py — varlen branch in get_attention_masks; Attention.forward only indexes the mask when it's a dict (assert → if)

2. Let the vLLM generator serve the ModelSpec

  • model.py — sliding_window_size property aliasing window_size (the vLLM wrapper reads that name)
  • parallelize.py — skip_dp to bypass FSDP for inference (its forward hooks conflict with inference_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.py harness 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 -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):

metric flex varlen rel
loss 1.060066 1.059845 2.1e-04
grad_norm 45.6010 45.8513 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. Layer-0 attention output agrees to ~1e-4, and the same profile appears at seq_len < window where 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

  • Generator TP is capped at 2 by the model's 2 KV heads.
  • The parity subclass is on top of the existing harness and isn't included in this PR. Can inlcude if needed.

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.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 12, 2026

@tianyu-l tianyu-l left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does this follow how gpt-oss handles swa?

@HosseinKaviani-H

Copy link
Copy Markdown
Contributor Author

Member

@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.

@tianyu-l
tianyu-l merged commit c39b844 into pytorch:main Aug 12, 2026
25 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