Skip to content

[graph_trainer] Add full recompute memory policy - #3429

Merged
SherlockNoMad merged 2 commits into
mainfrom
graph-trainer-full-memory-policy
Jun 2, 2026
Merged

SherlockNoMad merged 2 commits into
mainfrom
graph-trainer-full-memory-policy

Conversation

@SherlockNoMad

@SherlockNoMad SherlockNoMad commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Add a "full" memory policy for graph_trainer that marks all ops as PREFER_RECOMPUTE, saving only layer boundary outputs, RNG ops (dropout), and higher-order ops (flex_attention, inductor_compiled_code whose GraphModule subgraph arguments can't be duplicated by node_copy). This mirrors eager mode's _apply_full_ac (checkpoint_wrapper with no context_fn).
  • Useful for memory-constrained scenarios: larger batch sizes, longer sequences, or bigger models where trading ~10% throughput for ~35-39% memory savings is worthwhile.
  • Usage: --compile.memory_policy full

Llama3 8B benchmarks (8×H100, 20 steps, c4_test)

FSDP4 + TP2

Metric default full Delta
Peak memory 30.92 GiB (33%) 20.70 GiB (22%) -33.0%
TPS (step20) 6,607 5,933 -10.2%
MFU (step20) 38.69% 34.74% -10.2%

FSDP8 + TP1

Metric default full Delta
Peak memory 52.44 GiB (55%) 32.07 GiB (34%) -38.8%
TPS (step20) 7,890 7,006 -11.2%
MFU (step20) 46.20% 41.02% -11.2%

DeepSeek-v3 16B benchmarks (8×H100, FSDP8+EP8, 20 steps, c4_test)

Metric default full Delta
Peak memory 84.78 GiB (89%) 52.02 GiB (55%) -38.6%
TPS (step20) 8,325 7,444 -10.6%
MFU (step20) 15.24% 13.63% -10.6%

Test plan

  • TestFullMemoryPolicy (4 unit tests): all ops marked recompute, save ops also recomputed, layer boundaries forced to MUST_SAVE, same-layer nodes all recomputed
  • TestApplySACPass (8 existing tests): no regressions
  • test_llama3_debugmodel_full_ac_saves_memory_vs_default: full uses less peak memory than default
  • test_llama3_debugmodel_full_ac_numerics_match_eager: bitwise-identical loss and grads vs eager full AC
  • test_llama3_debugmodel_peak_memory_matches_eager_selective_ac: existing test still passes
  • Llama3 8B FSDP4+TP2, FSDP8+TP1 benchmarks
  • DeepSeek-v3 16B FSDP8+EP8 benchmark

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 26, 2026
@SherlockNoMad
SherlockNoMad requested a review from syed-ahmed May 26, 2026 16:45
@SherlockNoMad SherlockNoMad added the ciflow/h100.8 Trigger H100.8 CI label May 26, 2026
@SherlockNoMad
SherlockNoMad marked this pull request as ready for review May 26, 2026 16:46
@SherlockNoMad
SherlockNoMad force-pushed the graph-trainer-full-memory-policy branch from ac01726 to a0667a3 Compare May 26, 2026 16:58
and torch.Tag.nondeterministic_seeded in node.target.tags
):
return CheckpointPolicy.MUST_SAVE
return CheckpointPolicy.PREFER_RECOMPUTE

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Should this be MUST_RECOMPUTE? Can there be a situation where a PREFER_RECOMPUTE gets promoted to MUST_SAVE?

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.

in current graphtrainer impl, prefer_x == must_x, these is actually no different between these two.

but I agree, must_* is better than prefer_. I should use must_ in all cases.

@SherlockNoMad
SherlockNoMad force-pushed the graph-trainer-full-memory-policy branch from a0667a3 to 2237830 Compare May 26, 2026 17:31
@syed-ahmed

Copy link
Copy Markdown
Collaborator

Ok I tried this on DeepSeek V3 671B and 64x4 GB200 (on pytorch commit: pytorch/pytorch@090b49d, note latest pytorch is hanging right now). Still seeing OOM for PP=1,EP=64,Local BS=8,FSDP=256 compared to eager which doesn't OOM with full AC.

8: [rank0]:[titan] 2026-05-27 19:46:14,631 - root - INFO - Applying 11 graph passes:
 8: [rank0]:  1. remove_detach_pass
 8: [rank0]:  2. remove_identity_view_pass
 8: [rank0]:  3. remove_identity_slice_pass
 8: [rank0]:  4. normalize_view_ops_as_reshape
 8: [rank0]:  5. tag_with_memory_policy_pass
 8: [rank0]:  6. apply_cpu_offload_pass
 8: [rank0]:  7. selective_activation_remat_pass
 8: [rank0]:  8. joint_transformer_block_bucketing_reordering_pass
 8: [rank0]:  9. annotate_flex_attention_for_regional_inductor_pass
 8: [rank0]:  10. regional_inductor_pass
 8: [rank0]:  11. custom_codegen_pass
 8: [rank0]:[titan] 2026-05-27 19:46:16,666 - root - INFO - Removed 730 aten.detach.default node(s) from the graph
 8: [rank0]:[titan] 2026-05-27 19:46:17,699 - root - INFO - Removed 3210 identity view/reshape node(s) from the graph
 8: [rank0]:[titan] 2026-05-27 19:46:17,714 - root - INFO - Removed 735 identity slice node(s)
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO - Applied selective activation checkpointing (SAC) graph pass.
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Forced 61 nodes to MUST_SAVE at layer boundaries
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer non-layer: 0 MUST_SAVE, 7 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 0: 1 MUST_SAVE, 105 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 1: 1 MUST_SAVE, 105 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 2: 1 MUST_SAVE, 105 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 3: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 4: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 5: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 6: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 7: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 8: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 9: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 10: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 11: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 12: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 13: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 14: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 15: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
41: E0527 19:48:59.250000 1295643 /opt/pytorch/pytorch/torch/distributed/elastic/multiprocessing/errors/error_handler.py:145] no error file defined for parent, to copy child error file (/tmp/torchelastic_patdhyc8/101_v9i5dive/attempt_0/0/error.json)
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 16: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 17: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 18: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,560 - root - INFO -   Layer 19: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 20: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 21: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 22: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 23: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 24: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 25: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 1: E0527 19:48:59.251000 2098023 /opt/pytorch/pytorch/torch/distributed/elastic/multiprocessing/api.py:988] failed (exitcode: 1) local_rank: 1 (pid: 2098108) of binary: /usr/bin/python3
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 26: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
41: [rank0]:[titan] 2026-05-27 19:44:51,975 - root - INFO - torchtitan version: 0.2.2+git75c5e5942 (0.0.0 means __version__ is not defined correctly).
41: [rank0]:[titan] 2026-05-27 19:44:52,104 - root - INFO - Float8 training active with recipe rowwise
41: [rank0]:[titan] 2026-05-27 19:44:52,106 - root - INFO - Swapped to Float8Linear layers
41: [rank0]:[titan] 2026-05-27 19:44:52,106 - root - WARNING - Compile is required for high performance float8 MoE training; enable it with --compile.enable
41: [rank0]:[titan] 2026-05-27 19:44:52,109 - root - INFO - Converted GroupedExperts to use dynamic float8 rowwise quantization with scaled grouped GEMMs
41: [rank0]:[titan] 2026-05-27 19:44:52,202 - torchtitan.observability.structured_logger.jsonl_handler - INFO - Structured logging -> JSONL: /workspace/torchtitan/outputs/structured_logs/training.global_rank_164.20260527-194452-KU3RPP.jsonl
41: [rank0]:[titan] 2026-05-27 19:44:52,635 - root - WARNING - ENV[TORCH_NCCL_ASYNC_ERROR_HANDLING] = 1 will be overridden to 3 based on job config
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 27: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 28: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 29: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 30: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 31: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 32: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 33: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 34: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 35: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 36: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
41: [rank0]:[titan] 2026-05-27 19:44:52,636 - root - INFO - Building device mesh with parallelism: pp=1, dp_replicate=1, dp_shard=256, cp=1, tp=1, ep=64
41: [rank0]:[titan] 2026-05-27 19:44:52,679 - root - INFO - Successfully created meshes with active dimensions: ['batch', 'loss', 'ep', 'efsdp', 'fsdp']
41: [rank0]:[titan] 2026-05-27 19:44:52,679 - root - INFO - [GC] Initial GC collection took 0.00 seconds
41: [rank0]:[titan] 2026-05-27 19:44:52,681 - root - INFO - Loading tokenizer from tokenizer.json
41: [rank0]:[titan] 2026-05-27 19:44:52,856 - root - INFO - Preparing c4_test dataset from /jet_art_root/unverified/text/core-models/c4/en_train_3gb
41: [rank0]:[titan] 2026-05-27 19:45:34,970 - root - INFO - Building graph_trainer/deepseek_v3 671B
41: [rank0]:[titan] 2026-05-27 19:45:35,174 - root - INFO - TensorBoard logging enabled. Logs will be saved at ./outputs/tb/20260527-1945/rank_164
41: [rank0]:[titan] 2026-05-27 19:45:35,174 - root - INFO - CUDA capacity: NVIDIA GB200 with 184.31GiB memory
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 37: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 38: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 39: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 40: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 41: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 42: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 43: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 44: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 45: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 46: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
41: [rank0]:[titan] 2026-05-27 19:45:35,269 - root - INFO - Total parameter count: dense 14,456,871,936, sparse 656,569,532,416, active 37,552,282,624
41: [rank0]:[titan] 2026-05-27 19:45:35,269 - root - INFO - �[34mModel graph_trainer/deepseek_v3 671B �[31msize: 671,026,404,352 total parameters�[39m
41: [rank0]:[titan] 2026-05-27 19:45:35,269 - root - INFO - Compiling the loss function with torch.compile
41: [rank0]:[titan] 2026-05-27 19:45:35,880 - root - INFO - Applied Data Parallel (simple_fsdp) (dp mode=fully_shard) to the model
41: [rank0]:[titan] 2026-05-27 19:45:35,884 - root - INFO - Initializing HybridEP buffer: hidden_dim=7168, max_tokens_per_rank=32768, num_local_experts=4, ep_size=64
41: [rank0]:[titan] 2026-05-27 19:45:40,131 - root - INFO - Pre-initialized HybridEP buffer (hidden_dim=7168, num_tokens=32768, num_local_experts=4)
41: [rank0]:[titan] 2026-05-27 19:45:40,131 - root - INFO - aot_fx_trace compile mode: graph capture will happen at training time
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 47: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 48: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 49: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 50: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 51: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 52: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 53: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 54: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 55: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 56: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
41: [rank0]:[titan] 2026-05-27 19:45:41,117 - root - INFO - Peak FLOPS used for computing MFU: 2.500e+15
41: [rank0]:[titan] 2026-05-27 19:45:41,117 - root - INFO - CUDA memory usage for model: 9.85GiB(5.35%)
41: [rank0]:[titan] 2026-05-27 19:45:41,120 - root - WARNING - Warmup steps (2000) exceed total steps (50). Adjusting warmup steps to 50.
41: [rank0]:[titan] 2026-05-27 19:45:41,120 - root - WARNING - Warmup (50) + decay (40) steps exceed total steps (50). Adjusting decay steps to 0.
41: [rank0]:[titan] 2026-05-27 19:45:41,121 - root - WARNING - model.safetensors.index.json not found at hf_assets_path: /jet_art_root/unverified/model/core-models/deepseek-v3.1-base_tokenizer/hf-d3d4eaf/model.safetensors.index.json.                     Defaulting to saving a single safetensors file if checkpoint is saved in HF format
41: [rank0]:[titan] 2026-05-27 19:45:47,809 - root - INFO - Trainer is initialized with local batch size 8, global batch size 2048, gradient accumulation steps 1, sequence length 4096, total steps 50 (warmup 2000)
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 57: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 58: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 59: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:46:20,561 - root - INFO -   Layer 60: 1 MUST_SAVE, 150 PREFER_RECOMPUTE
 8: [rank0]:[titan] 2026-05-27 19:47:24,323 - root - INFO - [CUSTOM_CODEGEN] Saving code to /tmp/torchtitan_fx_codegen_0
 8: [rank0]:[titan] 2026-05-27 19:47:29,472 - root - INFO - [CUSTOM_CODEGEN] Dumped new file: /tmp/torchtitan_fx_codegen_0/fx_7289c97e8acc8f1f_rank32.py
 8: [rank0]:[titan] 2026-05-27 19:47:31,839 - root - INFO - [CUSTOM_CODEGEN] Loaded module from /tmp/torchtitan_fx_codegen_0/fx_7289c97e8acc8f1f_rank32.py, hash: 7289c97e
 8: [rank0]:[titan] 2026-05-27 19:47:31,841 - root - INFO - All 11 graph passes took 77.209s
 8: [rank0]:[titan] 2026-05-27 19:48:42,245 - root - WARNING - 1 CUDA memory allocation retries.
41: [rank0]:[titan] 2026-05-27 19:45:47,809 - root - INFO - Training starts at step 1
...
torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 15.78 GiB. GPU 2 has a total capacity of 184.31 GiB of which 13.05 GiB is free. Including non-PyTorch memory, this process has 171.24 GiB memory in use. Of the allocated memory 136.25 GiB is allocated by PyTorch, and 1.11 GiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management  (https://docs.pytorch.org/docs/stable/notes/cuda.html#optimizing-memory-usage-with-pytorch-cuda-alloc-conf)

@syed-ahmed

Copy link
Copy Markdown
Collaborator

After disabling the boundary saves, I still see OOM, but much lower memory request at the point of OOM:

torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 448.00 MiB. GPU 0 has a total capacity of 184.31 GiB of which 11.44 MiB is free. Including non-PyTorch memory, this process has 184.28 GiB memory in use. Of the allocated memory 150.26 GiB is allocated by PyTorch, and 161.64 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management  (https://docs.pytorch.org/docs/stable/notes/cuda.html#optimizing-memory-usage-with-pytorch-cuda-alloc-conf)

Probably the rest of the MUST_SAVEs.

Add a "full" memory policy that marks all ops as PREFER_RECOMPUTE,
saving only layer boundary outputs (and RNG ops like dropout whose
random state cannot be replayed, and higher-order ops like
flex_attention/inductor_compiled_code whose GraphModule subgraph
arguments cannot be duplicated by node_copy).

This mirrors eager mode's full AC (checkpoint_wrapper with no context_fn).

## Llama3 8B benchmarks (8×H100, 20 steps, c4_test)

### FSDP4 + TP2
| Metric       | default         | full            | Delta     |
|--------------|-----------------|-----------------|-----------|
| Peak memory  | 30.92 GiB (33%) | 20.70 GiB (22%) | **-33.0%** |
| TPS (step20) | 6,607           | 5,933           | -10.2%    |
| MFU (step20) | 38.69%          | 34.74%          | -10.2%    |

### FSDP8 + TP1
| Metric       | default         | full            | Delta     |
|--------------|-----------------|-----------------|-----------|
| Peak memory  | 52.44 GiB (55%) | 32.07 GiB (34%) | **-38.8%** |
| TPS (step20) | 7,890           | 7,006           | -11.2%    |
| MFU (step20) | 46.20%          | 41.02%          | -11.2%    |

## DeepSeek-v3 16B benchmarks (8×H100, FSDP8+EP8, 20 steps, c4_test)

| Metric       | default         | full            | Delta     |
|--------------|-----------------|-----------------|-----------|
| Peak memory  | 84.78 GiB (89%) | 52.02 GiB (55%) | **-38.6%** |
| TPS (step20) | 8,325           | 7,444           | -10.6%    |
| MFU (step20) | 15.24%          | 13.63%          | -10.6%    |

Usage: --compile.memory_policy full
@SherlockNoMad
SherlockNoMad force-pushed the graph-trainer-full-memory-policy branch 2 times, most recently from 3d9b9f0 to 35a635d Compare June 2, 2026 07:29
The `full` policy previously force-saved flex_attention (and all HOPs) on
the assumption that node_copy couldn't duplicate HOPs with GraphModule
subgraph args. This made `full` keep one attention activation per layer
that eager full AC recomputes for free — the main memory gap behind the
DeepSeek-V3 671B OOM.

Root cause: node_copy *can* duplicate flex_attention. The real blocker is
regional_inductor, which partitions each flex node together with its
score_mod/mask_mod get_attr subgraphs. When remat duplicated the flex
node, original and duplicate shared those get_attr nodes; a shared get_attr
can only live in one partition, so the other flex received the subgraph as
a raw GraphModule arg and failed to compile.

Fix:
- memory_policy: drop the HOP MUST_SAVE workaround so flex is recomputed.
  RNG ops are still saved (the remat pass can't replay RNG state, unlike
  eager's preserve_rng_state).
- selective_activation_remat: give each recompute duplicate private copies
  of its subgraph (GraphModule) get_attr inputs, keeping each flex region
  self-contained for regional_inductor. Plain-tensor get_attrs stay shared.

Also fix the SAC per-layer log: compute the summary after the boundary
pass so boundary-forced saves are reflected per layer, and label the
recompute column RECOMPUTE (it covers both PREFER_ and MUST_RECOMPUTE)
instead of the misleading PREFER_RECOMPUTE.

Validation (DeepSeek-V3 flex debugmodel, 8xH100 FSDP8): full vs default
produce identical loss/grad_norm every step; peak memory 2.88 -> 1.46 GiB
(-49%). Bitwise-identical to eager across llama3/deepseek-v3/qwen3 flex
debug models. New CPU unit tests cover the get_attr duplication and the
full-policy RNG-save / flex-recompute mapping.
@SherlockNoMad
SherlockNoMad force-pushed the graph-trainer-full-memory-policy branch from 35a635d to 7faef1e Compare June 2, 2026 16:13
@SherlockNoMad

Copy link
Copy Markdown
Contributor Author

@syed-ahmed the latest commit now recomputes flex_attn, whose output tensor is about 1GB per layer, and was saved in the earlier impl.

I think it now matches eager FullAC impl.

I am landing this, will iterator on it if you see more OOM issues.

@SherlockNoMad
SherlockNoMad merged commit 27aa490 into main Jun 2, 2026
16 of 18 checks passed
@SherlockNoMad
SherlockNoMad deleted the graph-trainer-full-memory-policy branch June 2, 2026 16:44
saforem2 added a commit to saforem2/torchtitan that referenced this pull request Jun 2, 2026
…no ezpz replay

Pulled 2 commits from upstream/main (04a3098..27aa490, merge
commit 45a2b25):

- 27aa490 [graph_trainer] Add full recompute memory policy (pytorch#3429)
- 051562e [graph_trainer] Re-enable DSv3 eager bitwise deterministic
  tests (pytorch#3482)

Both entirely inside torchtitan/experiments/graph_trainer/. Zero
ezpz code or doc changes triggered. No conflicts. Tree clean.

Documents both commits in upstream-sync.md and adds a one-paragraph
journal note pointing at the sync entry.
SherlockNoMad added a commit that referenced this pull request Jun 2, 2026
…oE) (#3484)

## Summary

`test_eager_self_deterministic` baselines for **Llama3 FlexAttention**,
**Qwen3 MoE**, and **Qwen3 MoE FlexAttention** drifted after upstream
PyTorch nightly numerics changes — the same class of drift #3432/#3482
already handled for DSv3. The H100.8 CI was red on these three.

This is the **pure-eager path** (base `Trainer`, no compile/trace):
- Loss strings are **unchanged** → low-order numeric drift, not a real
break.
- `test_aot_fx_trace_vs_eager` still passes → the trace path stays
bitwise-identical to eager; only the absolute `model_hash`/`grad_hash`
baselines were stale.

## How the new hashes were obtained
- **Qwen3 MoE** / **Qwen3 MoE FlexAttn**: regenerated on a local H100
and verified **bitwise-equal to the CI nightly (cu130)** `model_hash`,
so their `grad_hash` is authoritative.
- **Llama3 FlexAttn**: local (CUDA 12.8) diverges from CI (cu130) for
this flex path, so both hashes were taken from the CI run.

## Validation
GraphTrainer 8-GPU H100 CI green on this branch: bitwise suite **21
passed, 3 skipped** (the 3 skips are the pre-existing
FlexAttn-precompile tests). Loss strings untouched.

> Note (separate issue): one earlier run on this branch hit a **flaky**
`qwen3_moe` FSDP+TP+EP all-to-all crash (`RuntimeError: Split sizes
doesn't match total dim 0 size`) — intermittent, unrelated to this
test-only change (the prior main run on #3429 passed that flavor and
failed only on these same stale hashes). Worth a separate look.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100.8 Trigger H100.8 CI 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