Skip to content

[graph_trainer] Offload after the last forward write into the storage - #5629

Open
andrewliu2001 wants to merge 1 commit into
pytorch:mainfrom
andrewliu2001:fix-cpu-offload-after-forward-writes
Open

andrewliu2001 wants to merge 1 commit into
pytorch:mainfrom
andrewliu2001:fix-cpu-offload-after-forward-writes

Conversation

@andrewliu2001

Copy link
Copy Markdown
Contributor

Fixes #5628

apply_cpu_offload_pass inserted ao.offload right after the offloaded node, but ao.offload copies the tensor when it runs. If the forward pass writes into that storage afterwards, e.g. h.view(-1)[:k].add_(1), the CPU copy is stale and backward reloads the pre-write value.

This moves the offload after the last forward write into the node's storage, using mutation_utils.mutation_deps to find writes (it follows views back to the base). With no forward writes, the offload stays right after the node, so nothing changes for the usual case. The forward wait was already placed after the last forward user of the storage, including writes, so it still comes after the offload.

Test plan:

  • repro from the issue (3-layer model, sac_and_offload, view write into an mm output): max grad error ~5e3 before, 0 after
  • combined with [graph_trainer] Save tensors written by forward in-place ops instead of recomputing them #5622 (written tensor that would otherwise be recomputed, then offloaded): correct only with both fixes
  • 0 of 490 offload candidates move on the llama3 and qwen3 MoE debug models
  • two new tests in test_cpu_offload.py, both fail on main: one checks the offload lands after the write, the other compares gradients to eager under sac_and_offload
  • test_cpu_offload, test_passes, test_remat_metadata, test_sac_peak_memory, test_numerics, test_paged_stash: 267 passed, 6 skipped (existing skips)

ao.offload copies the tensor when it runs, but apply_cpu_offload_pass
inserted it right after the offloaded node. A later forward write into the
same storage, e.g. through a view, was missing from the CPU copy, so backward
reloaded the pre-write value.

Insert the offload after the last forward write found by mutation_deps
instead. Without forward writes it stays right after the node, so nothing
changes in the usual case (no offloads move on the llama3 / qwen3 MoE debug
models).
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Oct 26, 2026
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Oct 26, 2026
@pytorch-bot

pytorch-bot Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Workflows were awaiting approval. CI has now been triggered for the ciflow labels on this PR.

tianyu-l added a commit that referenced this pull request Nov 26, 2026
… deterministic mode (#6398)

## 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
Since torch `2.16.0.dev20261125`, "1 GPU Integration (Fake PG)" fails on
every PR and "8 GPU Integration (Real PG - full suite - models)" fails
on main. The A10G golden losses drift in the low digits. Torch
`dev20261124` passed (last green: 11:04 UTC run 45601059954), and every
run after the nightly switch fails the same tests with the same values.

The cause is pytorch/pytorch#204322, "[inductor] Deterministic mode:
turn off Triton FMA contraction, emit explicit FMAs". In
`config.deterministic` mode, Inductor now compiles its generated Triton
kernels with `enable_fp_fusion=False`, so the result no longer depends
on which block size or `num_warps` autotuning picks. The golden runs set
`debug.deterministic=True`, so their rounding changes. This is an
intentional upstream numerics change, not a torchtitan bug, so the fix
is to regenerate the goldens.

**Example** (`llama3_fsdp+tp+cp`, Fake PG, A10G):

```
step  golden (dev20261124)          dev20261125
4     3.6361570  gn 0.76279575      3.6361570  gn 0.76279598
5     3.6227007                     3.6228001
10    3.4226971                     3.4225903
```

## How the cause was isolated
The two nightlies are 70 PyTorch commits apart
(283f59dd27...122eafd311), and the new torch also pins cuDNN 9.28.1 (was
9.26.0). I bisected locally on GB300 with `deepseek_v3_fsdp+ep` (Fake
PG), which reproduces the drift there. Each row below gives
bitwise-identical results on repeat runs.

| torch binary | cuDNN | Python change | step 1 loss |
| --- | --- | --- | --- |
| dev20261124 | 9.26.0 | -- | 7.990665912628174 |
| dev20261124 | 9.28.1 | -- | 7.990665912628174 |
| dev20261125 | 9.28.1 | -- | 7.990651607513428 |
| dev20261125 | 9.28.1 | dev20261124's `_inductor`/`_dynamo`/`fx`/... |
7.990665912628174 |
| dev20261125 | 9.28.1 | only #204322 reverted | **7.990665912628174** |
| dev20261125 | 9.28.1 | only #204320 reverted | 7.990651607513428 |
| dev20261125 | 9.28.1 | #184946, #206365, #203784, #203888 reverted |
7.990651607513428 |

So the cuDNN bump has no effect, and reverting #204322 alone restores
the old numerics bit for bit.

## What changed
Mechanical: the numeric rows of 11 golden files are replaced. Headers
and step counts are unchanged and were checked against the source
blocks.

| Golden file | Tests | max rel. loss diff | final loss old -> new |
| --- | --- | --- | --- |
| `fake_pg/a10g/llama3.txt` | `llama3_fsdp+tp+cp`, `+region_ac` |
3.1e-05 | 3.42270 -> 3.42259 |
| `fake_pg/a10g/deepseek_v3.txt` | `deepseek_v3_fsdp+ep` | 4.0e-05 |
6.32932 -> 6.32955 |
| `fake_pg/a10g/qwen3.txt` | `qwen3_moe_fsdp+tp+cp+ep_param_groups` |
5.5e-03 | 2.96522 -> 2.98026 |
| `fake_pg/a10g/muse_glimmer.txt` | `muse_glimmer_text_fsdp` | 3.4e-04 |
6.71710 -> 6.71748 |
| `real_pg/a10g/llama3.txt` | `llama3_fsdp+tp+cp`, `+region_ac` |
7.1e-05 | 4.05240 -> 4.05239 |
| `real_pg/a10g/llama3_pp.txt` | `llama3_fsdp+tp+pp` | 6.3e-05 | 4.19607
-> 4.19609 |
| `real_pg/a10g/deepseek_v3.txt` | `deepseek_v3_fsdp+ep` | 1.1e-04 |
3.81001 -> 3.81031 |
| `real_pg/a10g/deepseek_v3_cp_pp.txt` | `deepseek_v3_fsdp+cp+pp+ep` |
2.5e-04 | 3.83078 -> 3.83064 |
| `real_pg/a10g/gpt_oss_pp.txt` | `gpt_oss_pp+fsdp+cp+ep+sacop` |
1.7e-03 | 3.90803 -> 3.90858 |
| `real_pg/a10g/qwen3.txt` | `qwen3_moe_fsdp+tp+cp+ep_param_groups` |
6.8e-05 | 5.34935 -> 5.34934 |
| `real_pg/a10g/muse_glimmer.txt` | `muse_glimmer_text_fsdp` | 8.0e-03 |
3.71158 -> 3.70928 |

Over 10 steps, last-bit differences grow more in some runs than others
(`real_pg` `muse_glimmer` 8.0e-3, `gpt_oss_pp` 1.7e-3, `fake_pg` `qwen3`
5.5e-3). Final losses move in both directions. Fake PG grad norms are
already meaningless (up to ~1e17) and change by more.

`{fake_pg,real_pg}/a10g/sft.txt` are unchanged and still pass.
`gpt_oss_fsdp+tp+ep` (`gpt_oss.txt`), and `gpt_oss_pp+fsdp+ep+sacop` in
Real PG, crash under `dev20261125` for a separate reason.
pytorch/pytorch#206398 made varlen's lse differentiable, and only FA4
can backpropagate through it. That is fixed in a separate PR, so this
PR's Fake PG and Real PG jobs still fail on those two tests.

## Numerics
The new values are the "Baseline metrics in import file format" blocks
that `loss_compare` printed in the A10G CI logs under `dev20261125`.
A10G numerics can't be reproduced off that hardware, so they come
straight from CI. The values agree across runs:
- Fake PG: #6395 (main HEAD deb3e43) == #6245 == #5678 == #6389. #5629
differs only in tests its own changes touch.
- Real PG: main 7ccb1bb == d2a8af2 == 7a60bbb == 8919259 (the
scheduled run).

## Testing
- This PR's CI is the test. `ciflow/fake-pg` checks the 4 fake-PG files.
`ciflow/real-pg` is added so the full Real PG suite checks all 7 real-PG
files. The `real_pg_required` subset only covers `llama3_pp`,
`deepseek_v3_cp_pp` and `gpt_oss_pp`.
- The local bisection above used `python -m
tests.integration_tests.run_tests --execution_mode fake_pg --ngpu 1
--test_name deepseek_v3_fsdp+ep` on GB300, with each torch/cuDNN
combination as a `PYTHONPATH` overlay.
- `pre-commit run` on the changed files passes.

## Known issues / follow-ups
- `real_pg/mi350x/*` is not updated. The ROCm workflow
(`integration_test_rocm.yaml`) still passes on main because the ROCm
nightly index serves torch `dev20261119`, which predates #204322 (run
45670678052). Expect the same drift on MI350X once the ROCm nightly
advances. Those goldens need a refresh from that run's logs then.
- `real_pg/kimi_k3_b200.txt` (B200 workflow) and `rl_grpo_cuda.txt` (RL
loss guard) are unaffected for now. Those jobs end up on pinned older
torch (`dev20261102` and `dev20261107` in today's runs). Expect the same
drift when their torch pins move past `dev20261125`.
- The goldens track whatever nightly CI installs, so a later nightly can
shift them again.

This branch has not been deployed

No deployments
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.

[graph_trainer] sac_and_offload reloads stale data when a forward in-place write goes through a view

1 participant