Repository navigation
[graph_trainer] Improve SAC tagging and CPU offload pass metadata - #3321
Conversation
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:
de9b292 to
0579b7a
Compare
The torch.ops.device_mesh._get_submesh op was removed upstream, causing AttributeError in CI on PyTorch nightly.
| if n.target in _AO_OPS: | ||
| continue |
There was a problem hiding this comment.
cc @mlazos
Removing these 2 lines, so that we have deferred waits after the ao.offload (for the output of layer)
There was a problem hiding this comment.
yes this seems good, at first I was skeptical but this is why you propagate the metadata to the offload nodes right?
| # 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) |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
let me show you
After the fix (removing these few line)
https://www.internalfb.com/intern/diffing/?before_paste_number=2320391950&after_paste_number=2320391970®ex_remove_pattern=&enable_regex_remove=0&strip_empty_lines=0&line_wrap=0&selected_tab=plain_diff
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
this piece was also important iirc, did we handle this some other way?
There was a problem hiding this comment.
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.
Summary
apply_sac_passtotag_sac_policyto clarify it only tags nodes (not the remat transform)torch.ops.device_mesh._get_submeshin SAC tagging (metadata-only op, no tensor output)cpu_offload_reload_nodemetadata (remat pass discovers offload chain via graph structure)defer_offload_waitsto 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)ao.offloadin region anchorsTest plan
pytest torchtitan/experiments/graph_trainer/tests/test_passes.py -xNGPU=8run with--compile.debug_graph_passesand verify tlparse graph dumps show correct metadata and orderingFollowing are desirable, but didn't' land.
Before the fix, we have both waits from layer0 and 1 at the end of layer 2

after the fix

Before the fix
https://www.internalfb.com/intern/diffing/?before_paste_number=2321471471&after_paste_number=2321471510®ex_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®ex_remove_pattern=&enable_regex_remove=0&strip_empty_lines=0&line_wrap=0&selected_tab=plain_diff