Repository navigation
Share CUDA graph memory between training and validation - #5241
Conversation
[ghstack-poisoned]
[ghstack-poisoned]
- Capture eval before training with the existing shared pool. - Use real validation runs for warmup and keep scheduled step 1 eval. - Prepare PP metadata outside capture and finish PP/FSDP eval cleanup. - Reject eval capture in unsupported trainers and document tensor lifetimes. - Add CPU and GPU tests for capture order, numerics, and memory reuse. CPU and GPU tests passed. Full Muse Glimmer 30B matched eager over 10 updates on 8 GPUs. ghstack-source-id: 8331835 Pull-Request: #5241
[ghstack-poisoned]
- Capture eval before training with the existing shared pool. - Use real validation runs for warmup and keep scheduled step 1 eval. - Prepare PP metadata outside capture and finish PP/FSDP eval cleanup. - Reject eval capture in unsupported trainers and document tensor lifetimes. - Add CPU and GPU tests for capture order, numerics, and memory reuse. CPU and GPU tests passed. Full Muse Glimmer 30B matched eager over 10 updates on 8 GPUs. ghstack-source-id: 0891679 Pull-Request: #5241
- Capture eval before training with the existing shared pool. - Use real validation runs for warmup and keep scheduled step 1 eval. - Prepare PP metadata outside capture and finish PP/FSDP eval cleanup. - Reject eval capture in unsupported trainers and document tensor lifetimes. - Add CPU and GPU tests for capture order, numerics, and memory reuse. CPU and GPU tests passed. Full Muse Glimmer 30B matched eager over 10 updates on 8 GPUs. ghstack-source-id: feba0b9 Pull-Request: #5241
[ghstack-poisoned]
- Capture eval before training with the existing shared pool. - Use real validation runs for warmup and keep scheduled step 1 eval. - Prepare PP metadata outside capture and finish PP/FSDP eval cleanup. - Reject eval capture in unsupported trainers and document tensor lifetimes. - Add CPU and GPU tests for capture order, numerics, and memory reuse. CPU and GPU tests passed. Full Muse Glimmer 30B matched eager over 10 updates on 8 GPUs. ghstack-source-id: 75e1c5b Pull-Request: #5241
[ghstack-poisoned]
- Capture eval before training with the existing shared pool. - Run one validation pass per call and use real runs for warmup. - Let CUDA graph wrappers prepare PP metadata outside capture. - Finish PP/FSDP eval cleanup and reject capture in unsupported trainers. - Document tensor lifetimes and test capture order, numerics, and memory reuse. CPU and GPU tests passed. Full Muse Glimmer 30B has not been rerun for this revision. ghstack-source-id: 310eece Pull-Request: #5241
|
@tianyu-l I updated the design, the logic is still the same but make the decision self-contained in validator. Let's chat on this. |
|
@claude review this PR |
|
Claude finished @tianyu-l's task in 3m 13s —— View job Review: Share CUDA graph memory between training and validation
The current design is much simpler than the earlier version. The trainer no longer runs validation twice. The validator holds Below are the remaining concerns, most important first. I only read the code: I could not run Python, pytest or pre-commit in this sandbox, and I could not read the installed torch pipelining source. 1. PP stage
|
[ghstack-poisoned]
- Capture eval before training with the existing shared pool. - Run one validation pass per call and use real runs for warmup. - Let CUDA graph wrappers prepare PP metadata outside capture. - Finish PP/FSDP eval cleanup and reject capture in unsupported trainers. - Document tensor lifetimes and test capture order, numerics, and memory reuse. CPU and GPU tests passed. Full Muse Glimmer 30B has not been rerun for this revision. ghstack-source-id: ead5181 Pull-Request: #5241
|
Response to Claude's review
|
- Capture eval before training with the existing shared pool. - Run one validation pass per call and use real runs for warmup. - Let CUDA graph wrappers prepare PP metadata outside capture. - Finish PP/FSDP eval cleanup and reject capture in unsupported trainers. - Document tensor lifetimes and test capture order, numerics, and memory reuse. CPU and GPU tests passed. Full Muse Glimmer 30B has not been rerun for this revision. ghstack-source-id: e80e2f4 Pull-Request: #5241
pytorch#3980 made PP preprocessing return (arg_mbs, kwarg_mbs, target_mbs, loss_kwarg_mbs), but two callers still unpacked three values: - TrainingEngine's prepare_for_capture (PP + CUDA graphs, from pytorch#5241) raised "too many values to unpack" in every PP integration test. It now passes the first microbatch's loss arguments to schedule initialization, which calls the loss on that microbatch, as the real step does. - GraphTrainer PP reuses the core preprocessing, so its forward-backward loop failed the same way. Per-microbatch loss arguments are rejected there, so the PP branch drops the empty fourth element. The CPU test for prepare_for_capture still fed the old 3-tuple, which hid the mismatch; it now uses the PP layout and checks the loss arguments.
…x per layer (#5547) **TLDR:** Two CI fixes for the GraphTrainer and 8 GPU Integration jobs. 1. Fixes #5366. The Flex eager-vs-trace bitwise tests compiled flex once per layer in the eager reference, and after 8 graphs Dynamo silently ran unfused eager flex. The traced side was correct; the eager reference changed. With this PR the eager reference compiles flex once, like the core and RL trainers, and hitting the recompile limit raises instead of falling back. 2. Fixes PP after #3980. #3980 made PP preprocessing return 4 values, but two callers still unpacked 3, so every PP + CUDA graph integration test and the GraphTrainer PP numerics test fail on main with `ValueError: too many values to unpack (expected 3)`. ## 1. Flex bitwise tests compiled flex per layer (#5366) **Problem** 1. **The eager reference was annotated.** `_run_steps` called `annotate_graph_trainer_model` for both `Trainer` and `GraphTrainer`. It wraps every submodule's forward with `annotate_fn({"module_fqn": fqn})`. Only GraphTrainer's graph passes read these tags. The core and RL trainers never set them. 2. **Dynamo now guards on the annotation.** pytorch/pytorch#198959 (October 10 nightly, the first failing one) adds an `FX_ANNOTATION` guard. Each layer's `inner_attention` has a different `module_fqn`, so the eager compiled flex compiles once per layer: 8 graphs for Qwen3 debugmodel_moe, all identical. 3. **The graphs carry over between tests.** Dynamo caches them on `flex_attention`'s code object, which outlives the `torch.compile` wrapper rebuilt in `setUp`. Llama3 Flex leaves 6. Qwen3 MoE Flex then passes the limit of 8 at its third layer. 4. **Silent fallback.** Past the limit, Dynamo runs flex eagerly, which is the unfused implementation, so the eager loss changes: `hit config.recompile_limit (8)`, then `eager vs aot_fx_trace: loss mismatch`. **Solution** 1. **Annotate only GraphTrainer runs.** The eager reference stays unannotated, like the core and RL trainers. 2. **`fullgraph=True` and `recompile_limit=16`** on the test's flex compile, the same settings as #5442. Hitting the limit raises instead of falling back. 3. **`torch._dynamo.reset()` in `setUp`.** Tests no longer share flex graphs. 4. **Unchanged:** the GraphTrainer runs, their annotations, and every assertion. ## 2. PP callers still unpacked 3 values after #3980 **Problem** 1. #3980 made `TrainingEngine._preprocess_microbatch_group` return `(arg_mbs, kwarg_mbs, target_mbs, loss_kwarg_mbs)` under PP. 2. `prepare_for_capture` (PP + CUDA graphs, added by #5241 before #3980 landed) still unpacked 3 values. Every PP test in the 8 GPU features job fails there: 3d, pp_1f1b, pp_dp_tp, pp_looped_zero_bubble, lora, and others. On main, that job passes at #3980's parent `efdde5eda` and fails at #3980 `a4a62b349`. 3. GraphTrainer PP reuses the core preprocessing, and its forward-backward loop also unpacked 3 values, so `test_dense_llama3_hsdp_graph_pp_vs_eager` fails. The GraphTrainer job uses `set -eux`, so it stops there and never reaches `test_bitwise_deterministic.py`. 4. The CPU test for `prepare_for_capture` still fed the old 3-tuple, which hid the mismatch. **Solution** 1. **`prepare_for_capture`** unpacks 4 values and passes the first microbatch's loss arguments plus `global_loss_token_counts` to schedule initialization. Initialization calls the loss on that microbatch, as the real step does. 2. **GraphTrainer PP** drops the fourth element. GraphTrainer rejects per-microbatch loss arguments earlier, so it is always empty, and both paths now return `(arg_mbs, kwarg_mbs, target_mbs)`, as before #3980. 3. **The CPU test** feeds the PP layout with a per-microbatch loss argument and checks the loss arguments passed to initialization. It fails without the fix with the same `ValueError`. ## Results ```text torch 2.16.0.dev20261019+cu132, GB200 (2 GPUs), flash-attn-4 1. Llama3 Flex -> Qwen3 MoE Flex test_aot_fx_trace_vs_eager, in one process main: "hit config.recompile_limit (8)", then eager vs aot_fx_trace: loss mismatch: 6.995096683502197 vs 6.995151519775391 this PR: pass, 1 flex graph per test, eager == traced == 6.995151519775391 flex graphs after the Qwen3 MoE Flex eager run (test run alone) annotated eager (main): 8 (one per layer) unannotated eager: 1, same loss Full test_bitwise_deterministic.py main: recompile limit hit once; on GB200 it breaks TestDSv3FlexAttnBitwiseDeterministic::test_aot_fx_trace_vs_eager and ::test_ep_chunk_matches_eager_chunking_bitwise this PR: 16 passed (Varlen and Flex), 9 skipped, no recompile limit hit 2. PP pp_1f1b integration test (2 GPUs, PP + CUDA graphs) main: ValueError in prepare_for_capture this PR: 10 steps pass GraphTrainer PP (the HSDP+PP numerics config reduced to PP=2, 2 GPUs) main: ValueError in GraphTrainer _forward_backward_body this PR: trains ``` You may ask: does the core trainer recompile flex per layer too? No. Llama3 1B (16 layers) with flex on the core `Trainer` compiles flex once and never recompiles. Only an annotated eager model recompiles per layer. NOTE: GraphTrainer's validator runs the annotated model eagerly, so Flex validation on a model with more than 8 layers should hit the same per-layer recompiles. Not covered here. NOTE: `test_dense_llama3_hsdp_graph_pp_vs_eager` needs 8 GPUs, so I could not run it locally. On the PP=2 reduction, eager PP vs GraphPP misses its grad_norm tolerance (max 1.7e-4) both with this PR and at #3980's parent, so that gap comes from the reduced config, not this PR. The full 8 GPU test passed on main before #3980. **Test plan** - `pytest torchtitan/experiments/graph_trainer/tests/test_bitwise_deterministic.py`: 16 passed, 9 skipped. - The Llama3 Flex -> Qwen3 MoE Flex pair from #5366 passes in one process. - `python -m tests.integration_tests.run_tests <out> --test_suite features --test_name pp_1f1b --ngpu 2`: passes; fails on main. - `pytest tests/unit_tests/cpu/test_trainer.py tests/unit_tests/cpu/distributed/test_cuda_graph.py tests/unit_tests/cpu/components/test_validator.py tests/unit_tests/cpu/distributed/test_pipeline_parallel.py`: 135 passed. - CI: GraphTrainer 8 GPU and 8 GPU Integration (features) jobs.
Stack from ghstack (oldest at bottom):
Summary
Add optional validation CUDA graphs with
validator.enable_cuda_graphs=True. Training, validation, and optimizer graphs share the existing memory pool. Capturing eval first lets its temporary buffers share storage with training gradients._CUDAGraphGradientState.Memory usage
Peak allocated memory per GPU for muse glimmer 30B:
• Training only: 59.024 GiB.
• Original main train/eval (w/o cudagraph): 61.531 GiB.
• This PR's train/eval (w/ cudagraph): 61.530 GiB.