Skip to content

Native FSDP2: keep gathered weights allocated so block CUDA graphs replay - #15215

Merged
danielhanchen merged 6 commits into
mainfrom
dh/s6-fsdp2-cgstable
Oct 26, 2026
Merged

danielhanchen merged 6 commits into
mainfrom
dh/s6-fsdp2-cgstable

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Problem

UNSLOTH_BLOCK_CUDAGRAPHS=1 records each compiled decoder block as a CUDA graph. A graph replays at the weight addresses it saw when it was recorded.

Native FSDP2 frees every unit's gathered weights at reshard and allocates them again at the next all-gather. So each step the weights land at new addresses, and torch re-records every block graph instead of replaying it, until cudagraph_unexpected_rerecord_limit (128) is reached.

Measured on Kaggle 2 x T4, fp16, a 2-layer Qwen3 with fixed-length batches:

arm graphs recorded / replayed over 8 steps step ms
DDP, graphs on 7 / 39 16.6
FSDP2, graphs off 0 / 0 38.7
FSDP2, graphs on, before 40 / 6 (6 new recordings every step) 107.6
FSDP2, graphs on, after 7 / 39 21.0

Full finetuning (one FSDP2 unit per block) showed the same pattern: 46 recordings and 0 replays before (112 ms per step), 7 and 39 after (45 ms per step). With graphs off it runs at 52 ms.

Fix

When block CUDA graphs are requested, native FSDP2 now keeps the gathered storage of every weight inside a compiled decoder block allocated across steps. Reshard still puts the sharded DTensor back on the module, and the next all-gather copies the updated shards into the same memory. The addresses stay fixed, so the graphs replay.

The cost is one gathered copy of those block weights per rank, printed once on rank 0. Weights outside the blocks (embeddings, LM head, final norm) never enter a block graph, so they are still freed at reshard; with no compiled blocks (block compile off, CPU, legacy path) nothing is kept. For LoRA in the default layout this is the adapters only. Nothing changes when UNSLOTH_BLOCK_CUDAGRAPHS is unset.

Tests

tests/test_fsdp2_native_block_cudagraphs_gloo.py runs on 2 gloo CPU ranks, in the fit layout (LoRA) and the sharded layout (full finetuning). It checks that:

  • with graphs requested, the gathered block weights stay allocated at one address across 3 optimizer steps;
  • the losses are bitwise equal to the run without graphs, so the kept weights still follow the optimizer;
  • weights outside the blocks, and every weight when no block is compiled, are still freed at reshard;
  • without graphs, the gathered weights are still freed at reshard.

Results:

  • Before: 2 failed, 6 passed. After: 8 passed, on torch 2.13 and 2.14. The two new checks (weights outside the blocks, no compiled blocks) fail on the first version of this PR, which kept every unit.
  • The neighbouring FSDP2 gloo tests (GA sync modes, layout agreement, fp16 scaler, unit discovery) still pass: 55 passed.

On GPU, the table above, Kaggle 2 x T4, fp16:

  • Losses with the fix equal the graphs-off FSDP2 run to 1e-5 (LoRA) and 7e-6 (full finetuning).
  • With gradient accumulation 2, the losses are bitwise equal to graphs off.

On one GPU with FSDP2 at world size 1 (bf16, LoRA):

  • Recordings go from 49 to 7 on an RTX PRO 6000 and from 37 to 7 on an H100.
  • Losses are bitwise equal to the run without FSDP2.

…raphs

FSDP2 frees each unit's gathered parameters at reshard and allocates them again at
the next all-gather, so with UNSLOTH_BLOCK_CUDAGRAPHS=1 every step moved the weight
addresses and torch re-recorded every block graph (6 records, 0 replays per step on
2 x T4 Qwen3 LoRA, 3x the step time of no graphs). With graphs requested, keep the
gathered storage allocated: reshard still swaps the sharded DTensor back and the next
all-gather copies the updated shards into the same memory.
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-25T08:44:10.346441Z 96c2e27 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. What shall we delve into next?

Reviewed commit: 2c462f8ee9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Bravo.

Reviewed commit: 96c2e27484

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 1185f84 into main Oct 26, 2026
32 of 37 checks passed
@danielhanchen
danielhanchen deleted the dh/s6-fsdp2-cgstable branch October 26, 2026 02:45
danielhanchen added a commit that referenced this pull request Oct 28, 2026
)

## Problem

`UNSLOTH_BLOCK_CUDAGRAPHS=1` under native FSDP2 keeps the all-gathered
weights of every parameter inside a compiled decoder block allocated
(`_keep_unsharded_for_block_cudagraphs`, #15215), so the block CUDA
graphs see one address per weight and replay instead of re-recording.

Since #15553, `_block_compile` runs MoE blocks compiled without CUDA
graphs (`_graphs_unsafe_moe`: routing outputs are overwritten across
replays and the grouped GEMM copies offsets to the host during capture).
FSDP2 still kept the gathered weights of those blocks. Those graphs
never record, so nothing reads the fixed addresses. The cost is that
every rank holds every expert of every MoE block unsharded after the
first forward. For MoE models, the experts are most of the weights, so
most of what FSDP2 shards stays fully gathered on every rank.

## Fix

`_compiled_block_modules` skips blocks that
`_block_compile._graphs_unsafe_moe` marks as MoE. It uses the same check
that routes them to the graphs-off path, so the two cannot disagree.
Their gathered weights are freed at reshard as usual. Dense blocks keep
theirs, and nothing changes without `UNSLOTH_BLOCK_CUDAGRAPHS=1` or
without block compile, because the check only runs on blocks
`_block_compile` wrapped.

## Tests

`tests/test_fsdp2_native_block_cudagraphs_moe_gloo.py` (CPU, gloo, world
size 2). It uses a tiny random Qwen3-MoE whose layer 0 is dense
(`mlp_only_layers=[0]`) and layer 1 is MoE,
`UNSLOTH_BLOCK_CUDAGRAPHS=1`, full finetune, 2 steps,
`_block_compile.py` loaded by file path:

| test | main | this branch |
|---|---|---|
| `test_moe_block_weights_are_freed_under_block_cudagraphs` | FAIL (MoE
block storages kept, size > 0) | PASS (size 0) |
| `test_dense_block_weights_stay_kept_under_block_cudagraphs` | PASS |
PASS |

The existing `tests/test_fsdp2_native_block_cudagraphs_gloo.py` and
`tests/test_block_cudagraphs_clone.py` still pass (61 passed with the
new file, main at d21d016). `tests/test_fsdp2_native_units.py` passes
too (40 passed, before the rebase onto #15352). All runs used torch 2.13
and transformers 5.17 on CPU.

## Not covered

- No GPU run. The change only affects which FSDP2 parameters skip
`free_unsharded_param`, and the CPU gloo test checks that directly.
- Flex attention blocks (Gemma3, which #15553 also routes off graphs)
are detected per call from a `BlockMask` argument, so they cannot be
known when FSDP2 shards. Their gathered weights are still kept. They are
dense, so the cost is far smaller.
- Since #15352, the kept bytes are checked against
`UNSLOTH_BLOCK_CUDAGRAPHS_FSDP2_KEEP_GB` (0.5 GB by default), and over
that budget no block keeps its weights and every block runs without
graphs. MoE weights used to count toward that budget, so a MoE model
could turn graphs off for its dense blocks as well. With MoE blocks
skipped, only the blocks that can be graphed count. This follows from
the code and was not measured on a GPU.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant