Skip to content

[graph_trainer] Improve SAC tagging and CPU offload pass metadata - #3321

Merged
SherlockNoMad merged 3 commits into
mainfrom
bahuang/sac-offload-improvements
May 12, 2026
Merged

SherlockNoMad merged 3 commits into
mainfrom
bahuang/sac-offload-improvements

Conversation

@SherlockNoMad

@SherlockNoMad SherlockNoMad commented May 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Rename apply_sac_pass to tag_sac_policy to clarify it only tags nodes (not the remat transform)
  • Skip torch.ops.device_mesh._get_submesh in SAC tagging (metadata-only op, no tensor output)
  • Propagate source node metadata (stacktrace, module_fqn, seq_nr) to CPU offload chain nodes for better tlparse/graph dump readability
  • Remove unused cpu_offload_reload_node metadata (remat pass discovers offload chain via graph structure)
  • Fix defer_offload_waits to defer from production site instead of latest storage-chain consumer — avoids pushing waits too far (e.g. layers.1's wait landing at layers.3)
  • Fix deferred waits landing before offload ops by including ao.offload in region anchors

Test plan

  • pytest torchtitan/experiments/graph_trainer/tests/test_passes.py -x
  • NGPU=8 run with --compile.debug_graph_passes and verify tlparse graph dumps show correct metadata and ordering
  • Verify bitwise deterministic test still passes

Following are desirable, but didn't' land.

Before the fix, we have both waits from layer0 and 1 at the end of layer 2
image

after the fix
image

Before the fix
https://www.internalfb.com/intern/diffing/?before_paste_number=2321471471&after_paste_number=2321471510&regex_remove_pattern=&enable_regex_remove=0&strip_empty_lines=0&line_wrap=0&selected_tab=plain_diff

After the fix (removing these few line)
https://www.internalfb.com/intern/diffing/?before_paste_number=2320391950&after_paste_number=2320391970&regex_remove_pattern=&enable_regex_remove=0&strip_empty_lines=0&line_wrap=0&selected_tab=plain_diff

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label May 12, 2026
Summary:
- Rename apply_sac_pass to tag_sac_policy to distinguish tagging from remat
- Skip _get_submesh metadata-only op in SAC tagging
- Propagate source node metadata (stacktrace, module_fqn) to offload chain
- Remove unused cpu_offload_reload_node metadata
- Fix defer_offload_waits to defer from production site, not latest consumer
- Fix deferred waits landing before offload ops by including ao.offload in anchors

Test Plan:
- pytest torchtitan/experiments/graph_trainer/tests/test_passes.py -x
- NGPU=8 run_graph_trainer_llama3_8b.sh with --compile.debug_graph_passes
- Verified tlparse graph dumps show correct metadata and ordering

Reviewers:

Subscribers:

Tasks:

Tags:
@SherlockNoMad
SherlockNoMad force-pushed the bahuang/sac-offload-improvements branch from de9b292 to 0579b7a Compare May 12, 2026 04:45
@SherlockNoMad
SherlockNoMad marked this pull request as ready for review May 12, 2026 04:46
@SherlockNoMad SherlockNoMad added the ciflow/h100.8 Trigger H100.8 CI label May 12, 2026
The torch.ops.device_mesh._get_submesh op was removed upstream,
causing AttributeError in CI on PyTorch nightly.
Comment on lines -521 to -522
if n.target in _AO_OPS:
continue

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.

cc @mlazos

Removing these 2 lines, so that we have deferred waits after the ao.offload (for the output of layer)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

yes this seems good, at first I was skeptical but this is why you propagate the metadata to the offload nodes right?

Comment on lines -541 to -548
# If storage chain extends to a later region (cross-layer views),
# use the latest consumer region.
chain_nodes, _ = _get_storage_chain(node)
real_consumers = {n for n in chain_nodes if n.target not in _AO_OPS}
for c in real_consumers:
idx = node_to_region_idx.get(c)
if idx is not None:
consumer_idx = max(consumer_idx, idx)

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.

this was deferring layer0's wait to layer 2.

Removing this fixed it


# If storage chain extends to a later region (cross-layer views),
# use the latest consumer region.
chain_nodes, _ = _get_storage_chain(node)

@mlazos mlazos May 12, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure if we can remove this part - is the reason you removed because we don't handle views? This deferred it N regions after the last consumer

Copy link
Copy Markdown
Contributor Author

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.

discussed offline, we will do it by using the dep argument from ao.wait node instead.

This change is reverted in the PR.


# Store mapping so the remat pass can redirect recomputed nodes
# to the reloaded tensor instead of the freed forward tensor.
node.meta["cpu_offload_reload_node"] = wait_node

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this piece was also important iirc, did we handle this some other way?

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.

yes, remat can walk the graph to find the upstream paired offload.
don't need to rely on node.meta

Summary: When a storage chain extends into a later region (e.g. cross-layer views), the prefetch scheduling used the first consumer region. This could cause premature prefetches. Fix by scanning the full storage chain and using the latest consumer region index.

Test Plan: Run graph_trainer with CPU offload on Llama3 8B debug model and verify identical loss.
@SherlockNoMad
SherlockNoMad merged commit 34801c0 into main May 12, 2026
15 of 16 checks passed
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