Skip to content

[muse_spark] Use varlen attention in the CP-16 LoRA SFT recipe - #5706

Merged
felipemello1 merged 1 commit into
mainfrom
felipemello1/muse-attn/1-cp16-varlen
Nov 5, 2026
Merged

felipemello1 merged 1 commit into
mainfrom
felipemello1/muse-attn/1-cp16-varlen

Conversation

@felipemello1

@felipemello1 felipemello1 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

Agent details

Written with Claude Code: the analysis, measurements and test plan below.

TLDR: The CP-16 LoRA SFT recipe moves from Ulysses flex on FA4 (256-token blocks) to Ulysses varlen, pinned to FA4. The Muse Ulysses CP transform now maps the varlen global and sliding-window layers. Attention kernel time per 128k step drops by 16% on one document and by 25-30% when the 128k tokens hold 4-8 documents (the recipe packs up to 8). Step time was not measured. The shared 816B builder still defaults to flex, so the Dist-MoE scale recipes keep it.

CP-16 LoRA SFT, one rank (8 q / 1 kv heads, the full sequence after the Ulysses all-to-all)
main:     global + window layers -> flex, FA4 backend, 256-token block mask
this PR:  global layers          -> varlen, FA4 (cuDNN is slower here, Results)
          window layers          -> varlen, FA4 with its native 2048 window
# before
config = _muse_spark_816b_a42b(seq_len=seq_len, parallelism=...)
...
for _, inner_attention, _, _ in config.model.traverse(FlexInnerAttention.Config):
    inner_attention.kernel_options = {"BACKEND": "FLASH"}
    inner_attention.block_size = 256
# after
config = _muse_spark_816b_a42b(seq_len=seq_len, parallelism=..., attn_backend="varlen")
...
for _, inner_attention, _, _ in config.model.traverse(VarlenInnerAttention.Config):
    inner_attention.sdpa_backends = ["FLASH_ATTENTION"]

Independent: based on main. It touches only the Muse Spark recipe and its test.

Problem

  1. Layers. The 816B model has 16 global and 46 sliding-window (2048) layers: head dim 64, 128 q / 16 kv heads.
  2. Flex only. The shared 816B builder, _muse_spark_816b_a42b, hard-codes attn_backend="flex". The CP-16 LoRA SFT recipe then puts every flex layer on its FA4 backend with 256-token blocks.
  3. Window layers. Flex passes the window to FA4 as a 256-token block mask plus a mask function on the partial blocks. Varlen hands the window to FA4 directly. At the recipe's per-rank shape, flex's window layers take 1.7x varlen's time (Results).

Solution

  1. _muse_spark_816b_a42b takes attn_backend (default "flex", so other callers don't change). The CP-16 LoRA recipe passes "varlen".
  2. The Muse Ulysses transform maps VarlenInnerAttention and SlidingWindowVarlenInnerAttention to their Ulysses versions. The transform matches exact types (config/transform/context_parallel.py:60-64), so both entries are needed.
  3. The CP-16 LoRA recipe pins its varlen layers to FA4. torch's default order puts the global layers on cuDNN, which is 1% slower than flex on one 128k document and 13% slower than FA4.
  4. The recipe's flex FA4 loop goes away. With varlen decoder layers it would only reach the vision encoder, which this text-only recipe skips (dataset_modalities is empty).
  5. Unchanged: the builder's default, the Dist-MoE scale recipes, the debug recipes and every RL recipe.

Results

Numerics: not bitwise: different kernels, same masks (per-document causal, 2048 window).
          rel L2 vs fp32, one layer, 8 x 16k                 out      dq       dk       dv
          global, flex FA4 (main)                            2.1e-3   2.5e-3   2.4e-3   2.3e-3
          global, varlen FA4 (this PR)                       2.1e-3   2.5e-3   2.4e-3   2.3e-3
          window, flex FA4 and varlen FA4                    2.2e-3   2.4e-3   2.4e-3   2.3e-3
          Muse Spark debug model, 2 GPUs, 3 training steps (activation checkpointing off), flex (Triton) vs varlen with cuDNN global layers (default order):
          layout              tokens   loss rel diff   grad-norm rel diff   step-1 grads rel L2
          TP 2 + EP 2 + SP    16k      <= 4.2e-5       <= 6.4e-4            2.0e-2
                              32k      <= 3.5e-5       <= 3.2e-4            1.4e-2
          Ulysses CP 2 + EP 2 16k      <= 9.7e-5       <= 5.0e-4            2.1e-2
                              32k      <= 2.9e-5       <= 7.2e-4            1.5e-2
          yardstick, TP 2, 16k: Triton flex vs flex on FA4 (two flex kernels)                       1.9e-2
          The recipe's path (this patch on main 29f784e07 plus the generator stack, d8603fc9d): Ulysses CP 2 + EP 1, varlen FA4, max_num_documents 8, 16k, deterministic, 6 steps,
          CUDA graphs on vs off: loss and grad norm bitwise at every step on both ranks. Steps 1-2 are eager warmup,
          step 3 captures, steps 4-6 replay the graph on new microbatches.

Kernel time per 128k microbatch (= one step: num_tokens_per_train_step = 131072), 16 global + 46 window layers.
Step time not measured: this excludes the Ulysses all-to-alls and the MoE. Attention is saved, not recomputed (muse_spark.py:94).
documents    main (flex FA4)    this PR (varlen FA4)    delta              varlen with cuDNN global (not chosen)
1 x 128k     1116 ms            936 ms                  -179 ms  -16.1%    1046 ms  -6.3%
4 x 32k       441 ms            331 ms                  -110 ms  -25.0%     365 ms  -17.3%
8 x 16k       319 ms            222 ms                   -97 ms  -30.4%     237 ms  -25.7%

Per layer, fwd+bwd, eager, GB300, ms, min of 2 rounds; 8 q / 1 kv heads, head dim 64, window 2048;
varlen metadata fixed as under CUDA graphs: 8 document slots, max_context_length 131072, no padding segment
(the recipe's packer adds one padding segment: 9 slots)
documents    global: flex FA4   varlen cuDNN   varlen FA4      window: flex FA4   varlen FA4
1 x 128k             57.72      58.34          51.49                    4.18       2.44
4 x 32k              15.47      15.64          13.50                    4.20       2.49
8 x 16k               8.48       8.04           7.11                    3.99       2.36

Decode and prefill: not touched (training recipe only).

You may ask: why not keep flex on FA4 for every Muse recipe? It's one backend, but the window layers, 46 of 62, take 1.7x longer on it.
You may ask: why not varlen in the Dist-MoE scale recipes too? Three things block it today:

  • Their C4 loader sets no max_num_documents. Under CUDA graphs, validation raises for any varlen layer without it (config/validation.py:100-107).
  • Their CP is K/V all-gather. Varlen K/V all-gather is untested on Muse.
  • _use_flex_flash_attention raises on any non-flex layer (torchtitan_recipes/models/muse_spark.py:223).

NOTE: kernel times only. Not run: the CP-16 LoRA recipe itself (16 GPUs), its step time and its loss curve.

Follow up

  • CP-16 LoRA SFT, a few steps on 16 GPUs: loss and step time, main vs this PR.
  • Varlen K/V all-gather CP for the Dist-MoE scale recipes: a document cap on their loader and a step-time comparison against flex.

Test plan

  • New CPU test test_cp16_lora_recipe_uses_ulysses_varlen builds the CP-16 recipe, checks that every decoder layer is Ulysses varlen, and builds each attention module (main's sdpa_backends takes backend names). It fails on main.
  • Built every Muse recipe and ran validate_model_training_config (it runs validate_context_parallel): all pass, except the MXFP8 debug recipe, which needs a torchao nightly to build (an import error, same on main). Only the CP-16 LoRA recipe changes: 16 global + 46 window layers move from Ulysses flex to Ulysses varlen.
  • 2-GPU debug-model training, flex vs varlen, TP 2 + EP 2 + SP and Ulysses CP 2 + EP 2, 16k and 32k tokens, 3 steps. Losses are within 1e-4, and step-1 grads differ by the same rel L2 as swapping one flex kernel for another (numbers above). Activation checkpointing was off: with it on, the TP 2 layout fails in backward on main too.
  • 2-GPU run of the recipe's path, re-run with this patch on main 29f784e plus the generator stack (d8603fc): Ulysses CP 2, varlen pinned to FA4, max_num_documents 8, deterministic, 6 steps, CUDA graphs on vs off. Loss and grad norm are bitwise equal at every step, including the 3 replays on new microbatches; the graphs run logs "Recorded CUDA graph forward_backward" on each rank.
  • CPU at this head (main 29f784e): tests/unit_tests/cpu/recipes 49 passed. pre-commit passes, pyrefly included.

@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 29, 2026
@claude

claude Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 2m 57s —— View job


Approve -- please address the items below before merging (one comment fix, an optional Literal signature tidy-up, and a question about running the recipe on 16 GPUs before merge). See the review.

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

Verdict: Approve -- please address the items below before merging. The change touches only the recipe, it is the smallest way to get varlen into the CP-16 LoRA recipe, and the kernel evidence supports it. The only open point is the 16-GPU run, which belongs to the author or a maintainer.

What this PR does. _muse_spark_816b_a42b now takes an attn_backend argument. The CP-16 LoRA SFT recipe passes "varlen" and pins every varlen layer to SDPBackend.FLASH_ATTENTION, which is FA4 on sm100. The Muse Ulysses CP map gains entries for VarlenInnerAttention and SlidingWindowVarlenInnerAttention. Without them, ContextParallelTransform.transform raises "No CP inner attention configured", because it looks up the exact owner type. The recipe no longer sets the flex FA4 kernel_options and block_size. After this change, the only flex attention left in the model is the vision encoder's (flavors.py:560), and this text-only recipe never runs it.

Example.

recipe: muse_spark_816b_a42b_ultradata_lora_sft_cp16_128k (CP 16, EP 16, Ulysses)
before: layers[*].attention.inner_attention = UlyssesFlexInnerAttention / UlyssesSlidingWindowFlexInnerAttention
        kernel_options={"BACKEND": "FLASH"}, block_size=256
after:  layers[*].attention.inner_attention = UlyssesVarlenInnerAttention / UlyssesSlidingWindowVarlenInnerAttention
        sdpa_backends=[FLASH_ATTENTION]; window layers get window_size=(2047, 0) passed straight to FA4
other recipes calling _muse_spark_816b_a42b: unchanged (still flex)

Must fix

  • torchtitan_recipes/models/muse_spark.py:836: the comment blames torch for the cuDNN-first order, but the order comes from torchtitan's own VarlenInnerAttention.Config default (inline comment).

Should consider

  • torchtitan_recipes/models/muse_spark.py:325: type attn_backend as Literal["flex", "varlen"] and drop the default, since there are only two callers (inline comment).

Questions

  • Will the 16-GPU CP-16 LoRA run (loss and step time, main vs. this PR) happen before merge? Today the recipe's numerics change rests on per-layer kernel comparisons plus 2-GPU debug-model runs. CLAUDE.md asks for loss convergence on computation changes, and that run is the one direct check of the recipe that actually ships.
Details
  • Ulysses varlen: after the all-to-all, each rank holds the full sequence for its head shard, so the document-causal and window metadata are the same as without CP. Under CUDA graphs, varlen needs max_num_documents, and the UltraData loader sets it to 8 (muse_spark.py:697).
  • The traverse(VarlenInnerAttention.Config) loop matches subclasses. That includes both Ulysses varlen configs and the sliding-window variant, so all 62 decoder layers get pinned.
  • The remat save region attention.inner_attention is matched by FQN, so attention is still saved, not recomputed, after the backend change.
  • The new CPU test is the only one that builds this recipe, and the build is where a missing CP-map entry would raise. That part of the test earns its cost.

Not verified: the GPU kernel times and the numerics. I couldn't run the new CPU test in this environment either. CPU CI was skipped because the base branch isn't main.

Comment thread torchtitan_recipes/models/muse_spark.py Outdated
for _, inner_attention, _, _ in config.model.traverse(FlexInnerAttention.Config):
inner_attention.kernel_options = {"BACKEND": "FLASH"}
inner_attention.block_size = 256
# torch's default puts the 128k global layers on cuDNN, ~13% slower than FA4 here.

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.

The cuDNN-first order isn't torch's default. It's torchtitan's own VarlenInnerAttention.Config.sdpa_backends default, [CUDNN_ATTENTION, FLASH_ATTENTION] (torchtitan/models/common/attention/attention.py:336). The loop also covers the window layers (traverse matches subclasses), not just the global ones. Suggested wording:

Suggested change
# torch's default puts the 128k global layers on cuDNN, ~13% slower than FA4 here.
# VarlenInnerAttention defaults to cuDNN first; on the 128k global layers it is ~13% slower than FA4.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reply from claude: applied in 7734e68, with your wording. The default order comes from VarlenInnerAttention.Config.sdpa_backends, and the loop pins the window layers too; those already ran FA4 under the default order.

seq_len: int | None = None,
*,
parallelism: ParallelismConfig | None = None,
attn_backend: str = "flex",

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.

Two small points. This helper has only two callers (:486 and :764), so the "flex" default saves one argument at one call site and hides the choice there. Also, build_model_config accepts only "flex" and "varlen" (torchtitan/models/muse_spark/flavors.py:768). Consider attn_backend: Literal["flex", "varlen"] with no default, and pass attn_backend="flex" at :486.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Reply from claude: keeping str with the "flex" default.

  • build_model_config takes attn_backend: str and validates it (flavors.py:768), so str matches the function it forwards to.
  • The default keeps the Dist-MoE caller (:486) byte-for-byte unchanged. The PR changes only the CP-16 recipe, and an earlier review round asked for exactly that.

@felipemello1
felipemello1 force-pushed the felipemello1/muse-attn/1-cp16-varlen branch from 3d56d65 to 7734e68 Compare October 29, 2026 21:22
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: on the 16-GPU question: not run yet. It's listed under NOTE and Follow up (loss and step time, main vs this PR), and the PR stays a draft until that run is posted. The evidence so far:

  • per-layer kernel error vs fp32: varlen FA4 matches flex FA4 to 3 digits;
  • 2-GPU debug model on the recipe's path (Ulysses CP 2, varlen FA4, max_num_documents 8, deterministic, 6 steps): CUDA graphs on vs off are bitwise at every step;
  • debug-model loss within 1e-4 of flex for 3 steps under TP 2 and Ulysses CP 2.

@felipemello1
felipemello1 force-pushed the felipemello1/fa4-kv-pages/1-128-token-pages branch from b152f1d to 38c378c Compare October 29, 2026 21:25
@felipemello1
felipemello1 force-pushed the felipemello1/muse-attn/1-cp16-varlen branch from 7734e68 to e130bce Compare October 29, 2026 21:26

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

lgtm

@felipemello1
felipemello1 force-pushed the felipemello1/fa4-kv-pages/1-128-token-pages branch from 38c378c to 3f4cc0d Compare October 30, 2026 03:04
@felipemello1
felipemello1 force-pushed the felipemello1/muse-attn/1-cp16-varlen branch from e130bce to 590bf90 Compare October 30, 2026 03:04
@felipemello1
felipemello1 force-pushed the felipemello1/fa4-kv-pages/1-128-token-pages branch from 3f4cc0d to 6c2cb34 Compare November 5, 2026 19:27
@felipemello1
felipemello1 force-pushed the felipemello1/muse-attn/1-cp16-varlen branch from 590bf90 to d8603fc Compare November 5, 2026 19:27
@felipemello1
felipemello1 marked this pull request as ready for review November 5, 2026 19:28
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 5, 2026
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review

@claude

claude Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 1m 6s —— View job


Approve: the earlier comment fix is in at HEAD, I found nothing new, and the 16-GPU run is still open. See the review.

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

Verdict: Approve -- the earlier must-fix is resolved at HEAD, I found nothing new, and the only open item is the 16-GPU run, which the author has committed to posting before the PR leaves draft.

What this PR does. _muse_spark_816b_a42b now forwards an attn_backend argument to build_model_config. The CP-16 LoRA SFT recipe passes "varlen" and pins every varlen layer to FLASH_ATTENTION, which is FA4 on sm100. The Muse Ulysses CP map gains the two varlen entries. Without them, the exact-type lookup in ContextParallelTransform would raise. Other callers of the builder still get flex.

Example.

recipe: muse_spark_816b_a42b_ultradata_lora_sft_cp16_128k (CP 16, EP 16, Ulysses, max_num_documents=8)
before: 16 global + 46 window layers -> Ulysses flex, kernel_options={"BACKEND": "FLASH"}, block_size=256
after:  global -> UlyssesVarlenInnerAttention, window -> UlyssesSlidingWindowVarlenInnerAttention
        sdpa_backends=["FLASH_ATTENTION"] (default would be ["CUDNN_ATTENTION", "FLASH_ATTENTION"])
_muse_spark_816b_a42b(seq_len=...) at muse_spark.py:484: unchanged (flex)

Status of the previous review

  • Comment at torchtitan_recipes/models/muse_spark.py:833: fixed. It now names VarlenInnerAttention's own default (attention.py:375-376).
  • attn_backend: Literal[...] with no default: the author declined. The builder accepts a str and validates it, and the default keeps the Dist-MoE caller unchanged. That reasoning holds, so I'm not raising it again.
  • 16-GPU loss and step-time run: still open. The author says the PR stays a draft until it is posted. fegin has approved.
Details
  • sdpa_backends holds SDPBackend member names (Literal["CUDNN_ATTENTION", "FLASH_ATTENTION"], attention.py:372). They are resolved with getattr(SDPBackend, name) at build time (attention.py:413). The new test's .build() call exercises that resolution and the CP-map lookup. Both would otherwise fail only at launch on 16 GPUs, so the test earns its cost.
  • traverse(VarlenInnerAttention.Config) matches subclasses, so both Ulysses varlen configs are pinned.
  • "attention.inner_attention" in _MUSE_SPARK_DIST_MOE_SAVE_REGIONS is matched by FQN, so attention is still saved, not recomputed.
  • The KV all-gather CP map (muse_spark.py:111) gets no varlen entries. Its callers stay on flex, which matches the description's scope.

Not verified: the GPU kernel times, the numerics, and the recipe on 16 GPUs.

@felipemello1
felipemello1 force-pushed the felipemello1/fa4-kv-pages/1-128-token-pages branch from 6c2cb34 to 9510ed8 Compare November 5, 2026 19:35
@felipemello1
felipemello1 force-pushed the felipemello1/muse-attn/1-cp16-varlen branch from d8603fc to 1a5a740 Compare November 5, 2026 19:35
The CP-16 LoRA SFT recipe now runs varlen attention on FA4 instead of flex on
its FA4 backend, and the Muse Ulysses CP transform maps the varlen global and
sliding-window layers. The base recipe and the Dist-MoE scale recipes keep
flex.
@felipemello1
felipemello1 force-pushed the felipemello1/fa4-kv-pages/1-128-token-pages branch from 9510ed8 to 64f48fa Compare November 5, 2026 19:57
@felipemello1
felipemello1 removed this pull request from stack #5705 November 5, 2026 19:57
@felipemello1
felipemello1 force-pushed the felipemello1/muse-attn/1-cp16-varlen branch from 1a5a740 to 86be8b1 Compare November 5, 2026 19:57
@felipemello1
felipemello1 changed the base branch from felipemello1/fa4-kv-pages/1-128-token-pages to main November 5, 2026 19:57
@felipemello1
felipemello1 merged commit 8cbb7af into main Nov 5, 2026
fegin added a commit that referenced this pull request Nov 6, 2026
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why
     this approach, and what you checked yourself. -->

## Why
The Dist-MoE scale recipes run only on 256 or 512 GPUs, and they use FlexAttention only. #5706 added `attn_backend` to `_muse_spark_816b_a42b` and listed three blockers for varlen in these recipes: their loader sets no `max_num_documents`, varlen K/V all-gather is untested on Muse, and `_use_flex_flash_attention` raises on non-flex layers. This change removes those blockers and adds a 64-GPU recipe. It covers the #5706 follow-up "Varlen K/V all-gather CP for the Dist-MoE scale recipes".

**Example.**

```text
before: muse_spark_816b_a42b_dist_moe_bf16_spmd_256 / _512 only, FlexAttention
after:  muse_spark_816b_a42b_dist_moe_bf16_spmd_64 (and _64_fake):
        Muse Spark 816B, BF16 Dist-MoE, DP8 / CP8 (K/V all-gather) / EP64, TP1,
        32K sequence length, global batch 64, varlen attention with max_num_documents=64,
        Adam moments in pinned host memory
```

## What changed
**Recipe helpers**
- `_muse_spark_816b_dist_moe_bf16_spmd` takes `attn_backend` and `max_num_documents`. It passes `attn_backend` to `_muse_spark_816b_a42b`.
- It sets the data loader's `max_num_documents` before `_with_dist_moe` enables CUDA graphs, because `Trainer.Config` validation checks varlen against CUDA graphs.
- The FakePG data loader keeps `max_num_documents`. It now replaces only the dataset and shuffle fields of the data loader config.
- The K/V all-gather CP transform also maps `VarlenInnerAttention` and `SlidingWindowVarlenInnerAttention`.
- Flex-Flash block-mask tuning applies only with FlexAttention.
- The 64-GPU recipe pins its varlen layers to FA4 (`sdpa_backends = ["FLASH_ATTENTION"]`), like the UltraData CP-16 recipe. The default order tries cuDNN first.

**New recipes**
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64` and `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake`. Remat keeps the default save regions.

**README** (`torchtitan/models/muse_spark/README.md`)
- Context parallelism: varlen attention supports both Ulysses and K/V all-gather.
- CP load balancing: head-tail works with K/V all-gather varlen.
- K/V all-gather CP needs tokens per microbatch divisible by the CP degree, or by 2 x the CP degree with head-tail. The query block size rule applies only with FlexAttention.
- Varlen attention with CUDA graphs requires `dataloader.max_num_documents`. Without it, set `training.disable_accelerator_graphs`.
- A table lists the Dist-MoE scale recipes.

**Tests**
- The recipe tokenizer test covers the 64-GPU fake recipe.
- `test_dist_moe_64_recipe_uses_kv_all_gather_varlen_on_fa4` builds the 64-GPU fake recipe. It catches a decoder layer that is not K/V all-gather varlen, or not pinned to FA4.

## Numerics / performance
- The 256- and 512-GPU and UltraData recipe configs are unchanged (0 field diffs).
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake` (NGPU=64, NUMA-bound) runs and fits on one GPU of the fake-PG layout for 10 steps. The FA4 pin gives no measurable tps change. Only this `_fake` variant was run, not the real recipe on 64 GPUs.
- Varlen K/V all-gather also runs with head-tail load balancing on the same layout.

## Testing
- Muse Spark recipe and model CPU unit tests.
- Both 64-GPU recipes build and pass `Trainer.Config` validation.
- `test_debug_recipe_tokenizer[muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake]` catches a broken 64-GPU fake recipe build.

## Known issues / follow-ups
- `test_debug_recipe_tokenizer[muse_spark_debugmodel_mm]` also fails on main.

stack-info: PR: #5882, branch: fegin/stack/11
fegin added a commit that referenced this pull request Nov 8, 2026
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why
     this approach, and what you checked yourself. -->

## Why
The Dist-MoE scale recipes run only on 256 or 512 GPUs, and they use FlexAttention only. #5706 added `attn_backend` to `_muse_spark_816b_a42b` and listed three blockers for varlen in these recipes: their loader sets no `max_num_documents`, varlen K/V all-gather is untested on Muse, and `_use_flex_flash_attention` raises on non-flex layers. This change removes those blockers and adds a 64-GPU recipe. It covers the #5706 follow-up "Varlen K/V all-gather CP for the Dist-MoE scale recipes".

**Example.**

```text
before: muse_spark_816b_a42b_dist_moe_bf16_spmd_256 / _512 only, FlexAttention
after:  muse_spark_816b_a42b_dist_moe_bf16_spmd_64 (and _64_fake):
        Muse Spark 816B, BF16 Dist-MoE, DP8 / CP8 (K/V all-gather) / EP64, TP1,
        32K sequence length, global batch 64, varlen attention with max_num_documents=64,
        Adam moments in pinned host memory
```

## What changed
**Recipe helpers**
- `_muse_spark_816b_dist_moe_bf16_spmd` takes `attn_backend` and `max_num_documents`. It passes `attn_backend` to `_muse_spark_816b_a42b`.
- It sets the data loader's `max_num_documents` before `_with_dist_moe` enables CUDA graphs, because `Trainer.Config` validation checks varlen against CUDA graphs.
- The FakePG data loader keeps `max_num_documents`. It now replaces only the dataset and shuffle fields of the data loader config.
- The K/V all-gather CP transform also maps `VarlenInnerAttention` and `SlidingWindowVarlenInnerAttention`.
- Flex-Flash block-mask tuning applies only with FlexAttention.
- The 64-GPU recipe pins its varlen layers to FA4 (`sdpa_backends = ["FLASH_ATTENTION"]`), like the UltraData CP-16 recipe. The default order tries cuDNN first.

**New recipes**
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64` and `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake`. Remat keeps the default save regions.

**README** (`torchtitan/models/muse_spark/README.md`)
- Context parallelism: varlen attention supports both Ulysses and K/V all-gather.
- CP load balancing: head-tail works with K/V all-gather varlen.
- K/V all-gather CP needs tokens per microbatch divisible by the CP degree, or by 2 x the CP degree with head-tail. The query block size rule applies only with FlexAttention.
- Varlen attention with CUDA graphs requires `dataloader.max_num_documents`. Without it, set `training.disable_accelerator_graphs`.
- A table lists the Dist-MoE scale recipes.

**Tests**
- The recipe tokenizer test covers the 64-GPU fake recipe.
- `test_dist_moe_64_recipe_uses_kv_all_gather_varlen_on_fa4` builds the 64-GPU fake recipe. It catches a decoder layer that is not K/V all-gather varlen, or not pinned to FA4.

## Numerics / performance
- The 256- and 512-GPU and UltraData recipe configs are unchanged (0 field diffs).
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake` (NGPU=64, NUMA-bound) runs and fits on one GPU of the fake-PG layout for 10 steps. The FA4 pin gives no measurable tps change. Only this `_fake` variant was run, not the real recipe on 64 GPUs.
- Varlen K/V all-gather also runs with head-tail load balancing on the same layout.

## Testing
- Muse Spark recipe and model CPU unit tests.
- Both 64-GPU recipes build and pass `Trainer.Config` validation.
- `test_debug_recipe_tokenizer[muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake]` catches a broken 64-GPU fake recipe build.

## Known issues / follow-ups
- `test_debug_recipe_tokenizer[muse_spark_debugmodel_mm]` also fails on main.

stack-info: PR: #5882, branch: fegin/stack/11
fegin added a commit that referenced this pull request Nov 8, 2026
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the problem, why
     this approach, and what you checked yourself. -->

## Why
The Dist-MoE scale recipes run only on 256 or 512 GPUs, and they use FlexAttention only. #5706 added `attn_backend` to `_muse_spark_816b_a42b` and listed three blockers for varlen in these recipes: their loader sets no `max_num_documents`, varlen K/V all-gather is untested on Muse, and `_use_flex_flash_attention` raises on non-flex layers. This change removes those blockers and adds a 64-GPU recipe. It covers the #5706 follow-up "Varlen K/V all-gather CP for the Dist-MoE scale recipes".

**Example.**

```text
before: muse_spark_816b_a42b_dist_moe_bf16_spmd_256 / _512 only, FlexAttention
after:  muse_spark_816b_a42b_dist_moe_bf16_spmd_64 (and _64_fake):
        Muse Spark 816B, BF16 Dist-MoE, DP8 / CP8 (K/V all-gather) / EP64, TP1,
        32K sequence length, global batch 64, varlen attention with max_num_documents=64,
        Adam moments in pinned host memory
```

## What changed
**Recipe helpers**
- `_muse_spark_816b_dist_moe_bf16_spmd` takes `attn_backend` and `max_num_documents`. It passes `attn_backend` to `_muse_spark_816b_a42b`.
- It sets the data loader's `max_num_documents` before `_with_dist_moe` enables CUDA graphs, because `Trainer.Config` validation checks varlen against CUDA graphs.
- The FakePG data loader keeps `max_num_documents`. It now replaces only the dataset and shuffle fields of the data loader config.
- The K/V all-gather CP transform also maps `VarlenInnerAttention` and `SlidingWindowVarlenInnerAttention`.
- Flex-Flash block-mask tuning applies only with FlexAttention.
- The 64-GPU recipe pins its varlen layers to FA4 (`sdpa_backends = ["FLASH_ATTENTION"]`), like the UltraData CP-16 recipe. The default order tries cuDNN first.

**New recipes**
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64` and `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake`. Remat keeps the default save regions.

**README** (`torchtitan/models/muse_spark/README.md`)
- Context parallelism: varlen attention supports both Ulysses and K/V all-gather.
- CP load balancing: head-tail works with K/V all-gather varlen.
- K/V all-gather CP needs tokens per microbatch divisible by the CP degree, or by 2 x the CP degree with head-tail. The query block size rule applies only with FlexAttention.
- Varlen attention with CUDA graphs requires `dataloader.max_num_documents`. Without it, set `training.disable_accelerator_graphs`.
- A table lists the Dist-MoE scale recipes.

**Tests**
- The recipe tokenizer test covers the 64-GPU fake recipe.
- `test_dist_moe_64_recipe_uses_kv_all_gather_varlen_on_fa4` builds the 64-GPU fake recipe. It catches a decoder layer that is not K/V all-gather varlen, or not pinned to FA4.

## Numerics / performance
- The 256- and 512-GPU and UltraData recipe configs are unchanged (0 field diffs).
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake` (NGPU=64, NUMA-bound) runs and fits on one GPU of the fake-PG layout for 10 steps. The FA4 pin gives no measurable tps change. Only this `_fake` variant was run, not the real recipe on 64 GPUs.
- Varlen K/V all-gather also runs with head-tail load balancing on the same layout.

## Testing
- Muse Spark recipe and model CPU unit tests.
- Both 64-GPU recipes build and pass `Trainer.Config` validation.
- `test_debug_recipe_tokenizer[muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake]` catches a broken 64-GPU fake recipe build.

## Known issues / follow-ups
- `test_debug_recipe_tokenizer[muse_spark_debugmodel_mm]` also fails on main.

stack-info: PR: #5882, branch: fegin/stack/11
fegin added a commit that referenced this pull request Nov 9, 2026
## Summary (human-written)
With the CPU-offloading optimizer, we can run Muse Spark with 64 GPUs. This
PR adds a recipe to demonstrate.

## Why
The Dist-MoE scale recipes run only on 256 or 512 GPUs, and they use FlexAttention only. #5706 added `attn_backend` to `_muse_spark_816b_a42b` and listed three blockers for varlen in these recipes: their loader sets no `max_num_documents`, varlen K/V all-gather is untested on Muse, and `_use_flex_flash_attention` raises on non-flex layers. This change removes those blockers and adds a 64-GPU recipe. It covers the #5706 follow-up "Varlen K/V all-gather CP for the Dist-MoE scale recipes".

**Example.**

```text
before: muse_spark_816b_a42b_dist_moe_bf16_spmd_256 / _512 only, FlexAttention
after:  muse_spark_816b_a42b_dist_moe_bf16_spmd_64 (and _64_fake):
        Muse Spark 816B, BF16 Dist-MoE, DP8 / CP8 (K/V all-gather) / EP64, TP1,
        32K sequence length, global batch 64, varlen attention with max_num_documents=64,
        Adam moments in pinned host memory
```

## What changed
**Recipe helpers**
- `_muse_spark_816b_dist_moe_bf16_spmd` takes `attn_backend` and `max_num_documents`. It passes `attn_backend` to `_muse_spark_816b_a42b`.
- It sets the data loader's `max_num_documents` before `_with_dist_moe` enables CUDA graphs, because `Trainer.Config` validation checks varlen against CUDA graphs.
- The FakePG data loader keeps `max_num_documents`. It now replaces only the dataset and shuffle fields of the data loader config.
- The K/V all-gather CP transform also maps `VarlenInnerAttention` and `SlidingWindowVarlenInnerAttention`.
- Flex-Flash block-mask tuning applies only with FlexAttention.
- The 64-GPU recipe pins its varlen layers to FA4 (`sdpa_backends = ["FLASH_ATTENTION"]`), like the UltraData CP-16 recipe. The default order tries cuDNN first.

**New recipes**
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64` and `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake`. Remat keeps the default save regions.

**README** (`torchtitan/models/muse_spark/README.md`)
- Context parallelism: varlen attention supports both Ulysses and K/V all-gather.
- CP load balancing: head-tail works with K/V all-gather varlen.
- K/V all-gather CP needs tokens per microbatch divisible by the CP degree, or by 2 x the CP degree with head-tail. The query block size rule applies only with FlexAttention.
- Varlen attention with CUDA graphs requires `dataloader.max_num_documents`. Without it, set `training.disable_accelerator_graphs`.
- A table lists the Dist-MoE scale recipes.

**Tests**
- The recipe tokenizer test covers the 64-GPU fake recipe.
- `test_dist_moe_64_recipe_uses_kv_all_gather_varlen_on_fa4` builds the 64-GPU fake recipe. It catches a decoder layer that is not K/V all-gather varlen, or not pinned to FA4.

## Numerics / performance
- The 256- and 512-GPU and UltraData recipe configs are unchanged (0 field diffs).
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake` (NGPU=64, NUMA-bound) runs and fits on one GPU of the fake-PG layout for 10 steps. The FA4 pin gives no measurable tps change. Only this `_fake` variant was run, not the real recipe on 64 GPUs.
- Varlen K/V all-gather also runs with head-tail load balancing on the same layout.

## Testing
- Muse Spark recipe and model CPU unit tests.
- Both 64-GPU recipes build and pass `Trainer.Config` validation.
- `test_debug_recipe_tokenizer[muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake]` catches a broken 64-GPU fake recipe build.

## Known issues / follow-ups
- `test_debug_recipe_tokenizer[muse_spark_debugmodel_mm]` also fails on main.

stack-info: PR: #5882, branch: fegin/stack/11
fegin added a commit that referenced this pull request Nov 9, 2026
## Summary (human-written)
With the CPU-offloading optimizer, we can run Muse Spark with 64 GPUs. This
PR adds a recipe to demonstrate.

## Why
The Dist-MoE scale recipes run only on 256 or 512 GPUs, and they use FlexAttention only. #5706 added `attn_backend` to `_muse_spark_816b_a42b` and listed three blockers for varlen in these recipes: their loader sets no `max_num_documents`, varlen K/V all-gather is untested on Muse, and `_use_flex_flash_attention` raises on non-flex layers. This change removes those blockers and adds a 64-GPU recipe. It covers the #5706 follow-up "Varlen K/V all-gather CP for the Dist-MoE scale recipes".

**Example.**

```text
before: muse_spark_816b_a42b_dist_moe_bf16_spmd_256 / _512 only, FlexAttention
after:  muse_spark_816b_a42b_dist_moe_bf16_spmd_64 (and _64_fake):
        Muse Spark 816B, BF16 Dist-MoE, DP8 / CP8 (K/V all-gather) / EP64, TP1,
        32K sequence length, global batch 64, varlen attention with max_num_documents=64,
        Adam moments in pinned host memory
```

## What changed
**Recipe helpers**
- `_muse_spark_816b_dist_moe_bf16_spmd` takes `attn_backend` and `max_num_documents`. It passes `attn_backend` to `_muse_spark_816b_a42b`.
- It sets the data loader's `max_num_documents` before `_with_dist_moe` enables CUDA graphs, because `Trainer.Config` validation checks varlen against CUDA graphs.
- The FakePG data loader keeps `max_num_documents`. It now replaces only the dataset and shuffle fields of the data loader config.
- The K/V all-gather CP transform also maps `VarlenInnerAttention` and `SlidingWindowVarlenInnerAttention`.
- Flex-Flash block-mask tuning applies only with FlexAttention.
- The 64-GPU recipe pins its varlen layers to FA4 (`sdpa_backends = ["FLASH_ATTENTION"]`), like the UltraData CP-16 recipe. The default order tries cuDNN first.

**New recipes**
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64` and `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake`. Remat keeps the default save regions.

**README** (`torchtitan/models/muse_spark/README.md`)
- Context parallelism: varlen attention supports both Ulysses and K/V all-gather.
- CP load balancing: head-tail works with K/V all-gather varlen.
- K/V all-gather CP needs tokens per microbatch divisible by the CP degree, or by 2 x the CP degree with head-tail. The query block size rule applies only with FlexAttention.
- Varlen attention with CUDA graphs requires `dataloader.max_num_documents`. Without it, set `training.disable_accelerator_graphs`.
- A table lists the Dist-MoE scale recipes.

**Tests**
- The recipe tokenizer test covers the 64-GPU fake recipe.
- `test_dist_moe_64_recipe_uses_kv_all_gather_varlen_on_fa4` builds the 64-GPU fake recipe. It catches a decoder layer that is not K/V all-gather varlen, or not pinned to FA4.

## Numerics / performance
- The 256- and 512-GPU and UltraData recipe configs are unchanged (0 field diffs).
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake` (NGPU=64, NUMA-bound) runs and fits on one GPU of the fake-PG layout for 10 steps. The FA4 pin gives no measurable tps change. Only this `_fake` variant was run, not the real recipe on 64 GPUs.
- Varlen K/V all-gather also runs with head-tail load balancing on the same layout.

## Testing
- Muse Spark recipe and model CPU unit tests.
- Both 64-GPU recipes build and pass `Trainer.Config` validation.
- `test_debug_recipe_tokenizer[muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake]` catches a broken 64-GPU fake recipe build.

## Known issues / follow-ups
- `test_debug_recipe_tokenizer[muse_spark_debugmodel_mm]` also fails on main.

stack-info: PR: #5882, branch: fegin/stack/11
fegin added a commit that referenced this pull request Nov 9, 2026
## Summary (human-written)
With the CPU-offloading optimizer, we can run Muse Spark with 64 GPUs. This
PR adds a recipe to demonstrate.

## Why
The Dist-MoE scale recipes run only on 256 or 512 GPUs, and they use FlexAttention only. #5706 added `attn_backend` to `_muse_spark_816b_a42b` and listed three blockers for varlen in these recipes: their loader sets no `max_num_documents`, varlen K/V all-gather is untested on Muse, and `_use_flex_flash_attention` raises on non-flex layers. This change removes those blockers and adds a 64-GPU recipe. It covers the #5706 follow-up "Varlen K/V all-gather CP for the Dist-MoE scale recipes".

**Example.**

```text
before: muse_spark_816b_a42b_dist_moe_bf16_spmd_256 / _512 only, FlexAttention
after:  muse_spark_816b_a42b_dist_moe_bf16_spmd_64 (and _64_fake):
        Muse Spark 816B, BF16 Dist-MoE, DP8 / CP8 (K/V all-gather) / EP64, TP1,
        32K sequence length, global batch 64, varlen attention with max_num_documents=64,
        Adam moments in pinned host memory
```

## What changed
**Recipe helpers**
- `_muse_spark_816b_dist_moe_bf16_spmd` takes `attn_backend` and `max_num_documents`. It passes `attn_backend` to `_muse_spark_816b_a42b`.
- It sets the data loader's `max_num_documents` before `_with_dist_moe` enables CUDA graphs, because `Trainer.Config` validation checks varlen against CUDA graphs.
- The FakePG data loader keeps `max_num_documents`. It now replaces only the dataset and shuffle fields of the data loader config.
- The K/V all-gather CP transform also maps `VarlenInnerAttention` and `SlidingWindowVarlenInnerAttention`.
- Flex-Flash block-mask tuning applies only with FlexAttention.
- The 64-GPU recipe pins its varlen layers to FA4 (`sdpa_backends = ["FLASH_ATTENTION"]`), like the UltraData CP-16 recipe. The default order tries cuDNN first.

**New recipes**
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64` and `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake`. Remat keeps the default save regions.

**README** (`torchtitan/models/muse_spark/README.md`)
- Context parallelism: varlen attention supports both Ulysses and K/V all-gather.
- CP load balancing: head-tail works with K/V all-gather varlen.
- K/V all-gather CP needs tokens per microbatch divisible by the CP degree, or by 2 x the CP degree with head-tail. The query block size rule applies only with FlexAttention.
- Varlen attention with CUDA graphs requires `dataloader.max_num_documents`. Without it, set `training.disable_accelerator_graphs`.
- A table lists the Dist-MoE scale recipes.

**Tests**
- The recipe tokenizer test covers the 64-GPU fake recipe.
- `test_dist_moe_64_recipe_uses_kv_all_gather_varlen_on_fa4` builds the 64-GPU fake recipe. It catches a decoder layer that is not K/V all-gather varlen, or not pinned to FA4.

## Numerics / performance
- The 256- and 512-GPU and UltraData recipe configs are unchanged (0 field diffs).
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake` (NGPU=64, NUMA-bound) runs and fits on one GPU of the fake-PG layout for 10 steps. The FA4 pin gives no measurable tps change. Only this `_fake` variant was run, not the real recipe on 64 GPUs.
- Varlen K/V all-gather also runs with head-tail load balancing on the same layout.

## Testing
- Muse Spark recipe and model CPU unit tests.
- Both 64-GPU recipes build and pass `Trainer.Config` validation.
- `test_debug_recipe_tokenizer[muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake]` catches a broken 64-GPU fake recipe build.

## Known issues / follow-ups
- `test_debug_recipe_tokenizer[muse_spark_debugmodel_mm]` also fails on main.

stack-info: PR: #5882, branch: fegin/stack/11
fegin added a commit that referenced this pull request Nov 9, 2026
## Summary (human-written)
With the CPU-offloading optimizer, we can run Muse Spark with 64 GPUs. This
PR adds a recipe to demonstrate.

## Why
The Dist-MoE scale recipes run only on 256 or 512 GPUs, and they use FlexAttention only. #5706 added `attn_backend` to `_muse_spark_816b_a42b` and listed three blockers for varlen in these recipes: their loader sets no `max_num_documents`, varlen K/V all-gather is untested on Muse, and `_use_flex_flash_attention` raises on non-flex layers. This change removes those blockers and adds a 64-GPU recipe. It covers the #5706 follow-up "Varlen K/V all-gather CP for the Dist-MoE scale recipes".

**Example.**

```text
before: muse_spark_816b_a42b_dist_moe_bf16_spmd_256 / _512 only, FlexAttention
after:  muse_spark_816b_a42b_dist_moe_bf16_spmd_64 (and _64_fake):
        Muse Spark 816B, BF16 Dist-MoE, DP8 / CP8 (K/V all-gather) / EP64, TP1,
        32K sequence length, global batch 64, varlen attention with max_num_documents=64,
        Adam moments in pinned host memory
```

## What changed
**Recipe helpers**
- `_muse_spark_816b_dist_moe_bf16_spmd` takes `attn_backend` and `max_num_documents`. It passes `attn_backend` to `_muse_spark_816b_a42b`.
- It sets the data loader's `max_num_documents` before `_with_dist_moe` enables CUDA graphs, because `Trainer.Config` validation checks varlen against CUDA graphs.
- The FakePG data loader keeps `max_num_documents`. It now replaces only the dataset and shuffle fields of the data loader config.
- The K/V all-gather CP transform also maps `VarlenInnerAttention` and `SlidingWindowVarlenInnerAttention`.
- Flex-Flash block-mask tuning applies only with FlexAttention.
- The 64-GPU recipe pins its varlen layers to FA4 (`sdpa_backends = ["FLASH_ATTENTION"]`), like the UltraData CP-16 recipe. The default order tries cuDNN first.

**New recipes**
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64` and `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake`. Remat keeps the default save regions.

**README** (`torchtitan/models/muse_spark/README.md`)
- Context parallelism: varlen attention supports both Ulysses and K/V all-gather.
- CP load balancing: head-tail works with K/V all-gather varlen.
- K/V all-gather CP needs tokens per microbatch divisible by the CP degree, or by 2 x the CP degree with head-tail. The query block size rule applies only with FlexAttention.
- Varlen attention with CUDA graphs requires `dataloader.max_num_documents`. Without it, set `training.disable_accelerator_graphs`.
- A table lists the Dist-MoE scale recipes.

**Tests**
- The recipe tokenizer test covers the 64-GPU fake recipe.
- `test_dist_moe_64_recipe_uses_kv_all_gather_varlen_on_fa4` builds the 64-GPU fake recipe. It catches a decoder layer that is not K/V all-gather varlen, or not pinned to FA4.

## Numerics / performance
- The 256- and 512-GPU and UltraData recipe configs are unchanged (0 field diffs).
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake` (NGPU=64, NUMA-bound) runs and fits on one GPU of the fake-PG layout for 10 steps. The FA4 pin gives no measurable tps change. Only this `_fake` variant was run, not the real recipe on 64 GPUs.
- Varlen K/V all-gather also runs with head-tail load balancing on the same layout.

## Testing
- Muse Spark recipe and model CPU unit tests.
- Both 64-GPU recipes build and pass `Trainer.Config` validation.
- `test_debug_recipe_tokenizer[muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake]` catches a broken 64-GPU fake recipe build.

## Known issues / follow-ups
- `test_debug_recipe_tokenizer[muse_spark_debugmodel_mm]` also fails on main.

stack-info: PR: #5882, branch: fegin/stack/11
fegin added a commit that referenced this pull request Nov 9, 2026
## Summary (human-written)
With the CPU-offloading optimizer, we can run Muse Spark with 64 GPUs. This
PR adds a recipe to demonstrate.

## Why
The Dist-MoE scale recipes run only on 256 or 512 GPUs, and they use FlexAttention only. #5706 added `attn_backend` to `_muse_spark_816b_a42b` and listed three blockers for varlen in these recipes: their loader sets no `max_num_documents`, varlen K/V all-gather is untested on Muse, and `_use_flex_flash_attention` raises on non-flex layers. This change removes those blockers and adds a 64-GPU recipe. It covers the #5706 follow-up "Varlen K/V all-gather CP for the Dist-MoE scale recipes".

**Example.**

```text
before: muse_spark_816b_a42b_dist_moe_bf16_spmd_256 / _512 only, FlexAttention
after:  muse_spark_816b_a42b_dist_moe_bf16_spmd_64 (and _64_fake):
        Muse Spark 816B, BF16 Dist-MoE, DP8 / CP8 (K/V all-gather) / EP64, TP1,
        32K sequence length, global batch 64, varlen attention with max_num_documents=64,
        Adam moments in pinned host memory
```

## What changed
**Recipe helpers**
- `_muse_spark_816b_dist_moe_bf16_spmd` takes `attn_backend` and `max_num_documents`. It passes `attn_backend` to `_muse_spark_816b_a42b`.
- It sets the data loader's `max_num_documents` before `_with_dist_moe` enables CUDA graphs, because `Trainer.Config` validation checks varlen against CUDA graphs.
- The FakePG data loader keeps `max_num_documents`. It now replaces only the dataset and shuffle fields of the data loader config.
- The K/V all-gather CP transform also maps `VarlenInnerAttention` and `SlidingWindowVarlenInnerAttention`.
- Flex-Flash block-mask tuning applies only with FlexAttention.
- The 64-GPU recipe pins its varlen layers to FA4 (`sdpa_backends = ["FLASH_ATTENTION"]`), like the UltraData CP-16 recipe. The default order tries cuDNN first.

**New recipes**
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64` and `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake`. Remat keeps the default save regions.

**README** (`torchtitan/models/muse_spark/README.md`)
- Context parallelism: varlen attention supports both Ulysses and K/V all-gather.
- CP load balancing: head-tail works with K/V all-gather varlen.
- K/V all-gather CP needs tokens per microbatch divisible by the CP degree, or by 2 x the CP degree with head-tail. The query block size rule applies only with FlexAttention.
- Varlen attention with CUDA graphs requires `dataloader.max_num_documents`. Without it, set `training.disable_accelerator_graphs`.
- A table lists the Dist-MoE scale recipes.

**Tests**
- The recipe tokenizer test covers the 64-GPU fake recipe.
- `test_dist_moe_64_recipe_uses_kv_all_gather_varlen_on_fa4` builds the 64-GPU fake recipe. It catches a decoder layer that is not K/V all-gather varlen, or not pinned to FA4.

## Numerics / performance
- The 256- and 512-GPU and UltraData recipe configs are unchanged (0 field diffs).
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake` (NGPU=64, NUMA-bound) runs and fits on one GPU of the fake-PG layout for 10 steps. The FA4 pin gives no measurable tps change. Only this `_fake` variant was run, not the real recipe on 64 GPUs.
- Varlen K/V all-gather also runs with head-tail load balancing on the same layout.

## Testing
- Muse Spark recipe and model CPU unit tests.
- Both 64-GPU recipes build and pass `Trainer.Config` validation.
- `test_debug_recipe_tokenizer[muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake]` catches a broken 64-GPU fake recipe build.

## Known issues / follow-ups
- `test_debug_recipe_tokenizer[muse_spark_debugmodel_mm]` also fails on main.

stack-info: PR: #5882, branch: fegin/stack/11
fegin added a commit that referenced this pull request Nov 10, 2026
## Summary (human-written)
With the CPU-offloading optimizer, we can run Muse Spark with 64 GPUs. This
PR adds a recipe to demonstrate.

## Why
The Dist-MoE scale recipes run only on 256 or 512 GPUs, and they use FlexAttention only. #5706 added `attn_backend` to `_muse_spark_816b_a42b` and listed three blockers for varlen in these recipes: their loader sets no `max_num_documents`, varlen K/V all-gather is untested on Muse, and `_use_flex_flash_attention` raises on non-flex layers. This change removes those blockers and adds a 64-GPU recipe. It covers the #5706 follow-up "Varlen K/V all-gather CP for the Dist-MoE scale recipes".

**Example.**

```text
before: muse_spark_816b_a42b_dist_moe_bf16_spmd_256 / _512 only, FlexAttention
after:  muse_spark_816b_a42b_dist_moe_bf16_spmd_64 (and _64_fake):
        Muse Spark 816B, BF16 Dist-MoE, DP8 / CP8 (K/V all-gather) / EP64, TP1,
        32K sequence length, global batch 64, varlen attention with max_num_documents=64,
        Adam moments in pinned host memory
```

## What changed
**Recipe helpers**
- `_muse_spark_816b_dist_moe_bf16_spmd` takes `attn_backend` and `max_num_documents`. It passes `attn_backend` to `_muse_spark_816b_a42b`.
- It sets the data loader's `max_num_documents` before `_with_dist_moe` enables CUDA graphs, because `Trainer.Config` validation checks varlen against CUDA graphs.
- The FakePG data loader keeps `max_num_documents`. It now replaces only the dataset and shuffle fields of the data loader config.
- The K/V all-gather CP transform also maps `VarlenInnerAttention` and `SlidingWindowVarlenInnerAttention`.
- Flex-Flash block-mask tuning applies only with FlexAttention.
- The 64-GPU recipe pins its varlen layers to FA4 (`sdpa_backends = ["FLASH_ATTENTION"]`), like the UltraData CP-16 recipe. The default order tries cuDNN first.

**New recipes**
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64` and `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake`. Remat keeps the default save regions.

**README** (`torchtitan/models/muse_spark/README.md`)
- Context parallelism: varlen attention supports both Ulysses and K/V all-gather.
- CP load balancing: head-tail works with K/V all-gather varlen.
- K/V all-gather CP needs tokens per microbatch divisible by the CP degree, or by 2 x the CP degree with head-tail. The query block size rule applies only with FlexAttention.
- Varlen attention with CUDA graphs requires `dataloader.max_num_documents`. Without it, set `training.disable_accelerator_graphs`.
- A table lists the Dist-MoE scale recipes.

**Tests**
- The recipe tokenizer test covers the 64-GPU fake recipe.
- `test_dist_moe_64_recipe_uses_kv_all_gather_varlen_on_fa4` builds the 64-GPU fake recipe. It catches a decoder layer that is not K/V all-gather varlen, or not pinned to FA4.

## Numerics / performance
- The 256- and 512-GPU and UltraData recipe configs are unchanged (0 field diffs).
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake` (NGPU=64, NUMA-bound) runs and fits on one GPU of the fake-PG layout for 10 steps. The FA4 pin gives no measurable tps change. Only this `_fake` variant was run, not the real recipe on 64 GPUs.
- Varlen K/V all-gather also runs with head-tail load balancing on the same layout.

## Testing
- Muse Spark recipe and model CPU unit tests.
- Both 64-GPU recipes build and pass `Trainer.Config` validation.
- `test_debug_recipe_tokenizer[muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake]` catches a broken 64-GPU fake recipe build.

## Known issues / follow-ups
- `test_debug_recipe_tokenizer[muse_spark_debugmodel_mm]` also fails on main.

stack-info: PR: #5882, branch: fegin/stack/11
fegin added a commit that referenced this pull request Dec 1, 2026
Stacked PRs:
 * #6252
 * #5746
 * #6135
 * #6134
 * #6133
 * #6132
 * __->__#5882
 * #5881 (landed)


--- --- ---

### [muse_spark] Add a 64-GPU Dist-MoE recipe with varlen attention


## Summary (human-written)
With the CPU-offloading optimizer, we can run Muse Spark with 64 GPUs.
This
PR adds a recipe to demonstrate it.

## Why
The Dist-MoE scale recipes run only on 256 or 512 GPUs, and they use
FlexAttention only. #5706 added `attn_backend` to `muse_spark_816b_a42b`
and listed three blockers for varlen in these recipes: their loader sets
no `max_num_documents`, varlen K/V all-gather is untested on Muse, and
`use_flex_flash_attention` raises on non-flex layers. This change
removes those blockers and adds a 64-GPU recipe. It covers the #5706
follow-up "Varlen K/V all-gather CP for the Dist-MoE scale recipes".

**Example.**

```text
before: muse_spark_816b_a42b_dist_moe_bf16_spmd_256 / _512 only, FlexAttention
after:  muse_spark_816b_a42b_dist_moe_bf16_spmd_64 (and _64_fake):
        Muse Spark 816B, BF16 Dist-MoE, DP8 / CP8 (K/V all-gather) / EP64, TP1,
        32K sequence length, global batch 64, varlen attention with max_num_documents=128,
        Adam moments in pinned host memory
```

## What changed
**Recipe helpers**
- `_muse_spark_816b_dist_moe_bf16_spmd` takes `attn_backend:
Literal["flex", "varlen"]` and `max_num_documents`. It passes
`attn_backend` to `muse_spark_816b_a42b`.
- It sets the data loader's `max_num_documents` before `_with_dist_moe`
enables CUDA graphs, because `Trainer.Config` validation checks varlen
against CUDA graphs.
- The FakePG data loader keeps `max_num_documents`. It replaces only the
dataset and shuffle fields of the data loader config.
- The K/V all-gather CP transform also maps `VarlenInnerAttention` and
`SlidingWindowVarlenInnerAttention`.
- Flex-Flash block-mask tuning applies only with FlexAttention.

**New recipes**
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64` and
`muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake`. Remat also saves all
attention regions (`attention.*`).
- The 64-GPU recipe sets `fsdp_reshard_after_forward = "default"`
instead of the helper's `"always"`. With PP off, only the last [norm,
lm_head] FSDP unit changes: it stays unsharded after forward, so the
loss does not all-gather the 3 GiB lm_head weight again.
- `max_num_documents=128` is one slot per 256 tokens. The packer never
drops documents: a row that reaches the cap ends early and is padded,
and the rest of the document starts the next row. On C4 with the Muse
Spark tokenizer (600 rows on each of 8 DP ranks), documents average 460
tokens. 64 slots pad 10.7% of the tokens, and 80% of the rows reach the
cap. 128 slots pad none.
- The data loader and AdamW configs are updated with
`dataclasses.replace`, so their `__post_init__` checks run
(`max_num_documents > 0`; `moment_cpu_offload_fraction > 0` needs
`fused=True` and rejects accelerators other than CUDA, including ROCm).

**README** (`torchtitan/models/muse_spark/README.md`)
- Context parallelism: varlen attention supports both Ulysses and K/V
all-gather.
- CP load balancing: head-tail works with K/V all-gather varlen.
- K/V all-gather CP needs tokens per microbatch divisible by the CP
degree, or by 2 x the CP degree with head-tail. The query block size
rule applies only with FlexAttention.
- Varlen attention with CUDA graphs requires
`dataloader.max_num_documents`. Without it, set
`training.disable_accelerator_graphs`.
- A table lists the Dist-MoE scale recipes.

## Numerics
The 256- and 512-GPU and UltraData recipe configs are unchanged (0 field
diffs).

## Testing
- `muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake` (NGPU=64,
NUMA-bound) runs and fits on one GPU of the fake-PG layout for 10 steps.
- Varlen K/V all-gather also runs with head-tail load balancing on the
same layout.
- Muse Spark recipe and model CPU unit tests.
- Both 64-GPU recipes build and pass `Trainer.Config` validation.
-
`test_fake_scale_recipe_tokenizer[muse_spark_816b_a42b_dist_moe_bf16_spmd_64_fake]`
builds the 64-GPU fake recipe. It catches a broken build, including a
varlen layer type that the K/V all-gather CP map misses (the build
raises "No CP inner attention configured").

## Known issues / follow-ups
- The real recipe ran on 64 GPUs for 20 steps at an earlier rebase of
this stack, with the same save regions, real routing, and
`max_num_documents=64`. Peak memory was 267.4 GiB (trainer) and 272.7
GiB (NVML) of 276.6 GiB, so only about 4 GiB is free. tps was about 2%
higher than with the default save regions (1,716 vs 1,683).
- On 64 real GPUs with the same setup, `fsdp_reshard_after_forward =
"default"` lowered peak memory by 1.9 GiB (265.5 vs 267.4 GiB trainer,
270.8 vs 272.7 GiB NVML), and tps did not change (1,725 vs 1,716). The
256- and 512-GPU recipes keep `"always"`.
- `max_num_documents=128` was run on 64 real GPUs only with the whole
recipe stack, which also shards the vocab over CP and frees memory.
There, peak allocated memory was the same as with 64 slots. It is not
run at this commit alone, where only about 4 GiB is free.
- tps counts padding tokens, and padding rows skip the routed experts.
So 128 slots lower the reported tps but raise the real tokens trained
per second.
- `test_debug_recipe_tokenizer[muse_spark_debugmodel_mm]` also fails on
main.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests 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