Repository navigation
Native FSDP2: keep gathered weights allocated so block CUDA graphs replay - #15215
Conversation
…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.
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. What shall we delve into next? Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
) ## 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>
Problem
UNSLOTH_BLOCK_CUDAGRAPHS=1records 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:
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_CUDAGRAPHSis unset.Tests
tests/test_fsdp2_native_block_cudagraphs_gloo.pyruns on 2 gloo CPU ranks, in the fit layout (LoRA) and the sharded layout (full finetuning). It checks that:Results:
On GPU, the table above, Kaggle 2 x T4, fp16:
On one GPU with FSDP2 at world size 1 (bf16, LoRA):