[GraphTrainer] Support transformer_block_bucketing in aot_fx_trace mode#2934
Merged
yiming0416 merged 1 commit intoApr 20, 2026
Merged
Conversation
e7a8559 to
3d07026
Compare
63e130f to
89b5762
Compare
a95cd87 to
83d06fc
Compare
Comment on lines
+53
to
+54
| traced_result: "TracedResult", | ||
| model: torch.nn.Module, |
Contributor
There was a problem hiding this comment.
Keep the interface clean if possible.
- trace_result has some internal GraphModule, can we use that?
- if not, alternative is to cache an instance of model inside TracedResult
Contributor
Author
There was a problem hiding this comment.
I attached an instance of model to TracedResult.
We can use the internal GraphModule from TracedResult as they has been flattened and lost the module hierarchy info
991e351 to
1daa927
Compare
3362a5e to
68737e0
Compare
tugsbayasgalan
added a commit
that referenced
this pull request
Apr 16, 2026
…ackward subgraphs" Graph-trainer was only copying seq_nr metadata within the outer FX graph. That misses the way old AOT behaves for flex attention: the matching forward source for a backward flex node can live inside a nested GraphModule, including placeholder nodes from the captured flex subgraphs. In practice that meant backward flex nodes in the graph-trainer path could miss the forward custom / nn_module_stack metadata that old AOT used to recover. The old outer-graph heuristic was also unreliable once remat-tagged nodes showed up in the same seq_nr bucket, because the first outer node with that seq_nr was no longer a reliable proxy for the true forward source. Fix _copy_fwd_metadata_to_bw_nodes() to mirror the old AOT behavior more closely: - walk all nested GraphModules - build the seq_nr -> forward-node map across those subgraphs - only copy onto nodes identified as backward via partitioner_tag or remat metadata 1) This should address failures in #2934 [ghstack-poisoned]
tugsbayasgalan
added a commit
that referenced
this pull request
Apr 16, 2026
Graph-trainer was only copying seq_nr metadata within the outer FX graph. That misses the way old AOT behaves for flex attention: the matching forward source for a backward flex node can live inside a nested GraphModule, including placeholder nodes from the captured flex subgraphs. In practice that meant backward flex nodes in the graph-trainer path could miss the forward custom / nn_module_stack metadata that old AOT used to recover. The old outer-graph heuristic was also unreliable once remat-tagged nodes showed up in the same seq_nr bucket, because the first outer node with that seq_nr was no longer a reliable proxy for the true forward source. Fix _copy_fwd_metadata_to_bw_nodes() to mirror the old AOT behavior more closely: - walk all nested GraphModules - build the seq_nr -> forward-node map across those subgraphs - only copy onto nodes identified as backward via partitioner_tag or remat metadata 1) This should address failures in #2934 [ghstack-poisoned]
pytorchmergebot
pushed a commit
to pytorch/pytorch
that referenced
this pull request
Apr 16, 2026
…nstance (#180579) Problem: When `manual_overlap_bucketing` is called multiple times on the same GraphModule (e.g. separate forward and backward passes), the old implementation stored bucketed node types in node.meta, which persists on the graph nodes across calls. The second call's _manual_reorder_graph iterates all graph nodes and misclassifies nodes bucketed by the first call, building incorrect overlap dependency edges. Fix: Moved bucketed node type tracking from `node.meta["manual_bucket_node_type"]` to `ManualOverlapPreservingBucketer.bucketed_node_types` dict, preventing metadata leaking across multiple `manual_overlap_bucketing` calls on the same graph. Test: In pytorch/torchtitan#2934 ``` MODULE=graph_trainer.llama3 CONFIG=graph_trainer_llama3_debugmodel ./run_train.sh --compile.mode aot_fx_trace ``` Pull Request resolved: #180579 Approved by: https://github.com/SherlockNoMad
tugsbayasgalan
added a commit
that referenced
this pull request
Apr 16, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack) (oldest at bottom): * __->__ #2875 Graph-trainer was only copying seq_nr metadata within the outer FX graph. That misses the way old AOT behaves for flex attention: the matching forward source for a backward flex node can live inside a nested GraphModule, including placeholder nodes from the captured flex subgraphs. In practice that meant backward flex nodes in the graph-trainer path could miss the forward custom / nn_module_stack metadata that old AOT used to recover. The old outer-graph heuristic was also unreliable once remat-tagged nodes showed up in the same seq_nr bucket, because the first outer node with that seq_nr was no longer a reliable proxy for the true forward source. Fix _copy_fwd_metadata_to_bw_nodes() to mirror the old AOT behavior more closely: - walk all nested GraphModules - build the seq_nr -> forward-node map across those subgraphs - only copy onto nodes identified as backward via partitioner_tag or remat metadata 1) This should address failures in #2934
236184d to
5ad46c8
Compare
| output_spec: Original output pytree spec used during reconstruction. | ||
| model: The original nn.Module, cached for downstream passes that need | ||
| module structure (e.g. transformer block bucketing). Only set when | ||
| traced via :func:`trace_train_step`. |
Contributor
There was a problem hiding this comment.
Only set when traced via :func:`trace_train_step`.
I am not a fan of conditional behavior.
Also TracedResult is now becoming a contract between Trainer and pre-compile, so adding model is not feasible now.
So this is pointing towards something more fundamental, we probably need a different way to come up with bucketing than get_transformer_block_buckets(traced_result.model).
Let's land for now, but add to TODO here to remove the model field from Traced Result.
SherlockNoMad
approved these changes
Apr 20, 2026
5ad46c8 to
6e0b03c
Compare
aot_fx_trace mode traces with record_module_stack=False, so nodes lack nn_module_stack metadata that manual_overlap_bucketing uses to match nodes to modules. This change uses the existing annotate_fn mechanism (already used for AC region tagging) to tag module FQNs during tracing, then provides a custom module_stack_fn to manual_overlap_bucketing. Changes: - Add annotate_module_fqns() and get_module_stack_from_annotation() to common_utils.py - Call annotate_module_fqns() in both llama3 and deepseek_v3 annotate functions - Add module_stack_fn parameter to transformer_block_bucketing_reordering_pass - Update construct_default_graph_passes to accept compile_config, model, and parallel_dims, and wire up the bucketing pass with reassign_to_pg - Pass config/model/parallel_dims from GraphTrainer to construct_default_graph_passes - Add numerics tests for both llama3 and deepseek_v3 with aot_fx_trace + transformer_block_bucketing
6e0b03c to
b83b975
Compare
bobrenjc93
added a commit
that referenced
this pull request
Apr 21, 2026
Stack from [ghstack](https://github.com/ezyang/ghstack/tree/0.14.0) (oldest at bottom): * #2916 * __->__ #3043 Precompile integration tests (run_precompile_tests.py) were never run in CI, which allowed the bucketing + CooR incompatibility from #2934 (enabled in #3036) to go undetected. Add them to both CI workflows: - A10 workflow: Llama3 aot_fx_trace precompile test (FSDP+TP) - H100 workflow: DSv3 aot_fx_trace precompile test (FSDP+TP+EP)
bobrenjc93
added a commit
that referenced
this pull request
Apr 22, 2026
Precompile integration tests (run_precompile_tests.py) were never run in CI, which allowed the bucketing + CooR incompatibility from #2934 to go undetected. Add them to both CI workflows: - A10 workflow: Llama3 aot_fx_trace precompile test (FSDP+TP) - H100 workflow: DSv3 aot_fx_trace precompile test (FSDP+TP+EP) Also fix the DSv3 precompile test config from debugmodel to debugmodel_ep — EP tests require AllToAllTokenDispatcher which is only created when moe_comm_backend="standard". ghstack-source-id: 35a8421 Pull-Request: #3043
aperson30
pushed a commit
to aperson30/pytorch
that referenced
this pull request
Jun 20, 2026
…nstance (pytorch#180579) Problem: When `manual_overlap_bucketing` is called multiple times on the same GraphModule (e.g. separate forward and backward passes), the old implementation stored bucketed node types in node.meta, which persists on the graph nodes across calls. The second call's _manual_reorder_graph iterates all graph nodes and misclassifies nodes bucketed by the first call, building incorrect overlap dependency edges. Fix: Moved bucketed node type tracking from `node.meta["manual_bucket_node_type"]` to `ManualOverlapPreservingBucketer.bucketed_node_types` dict, preventing metadata leaking across multiple `manual_overlap_bucketing` calls on the same graph. Test: In pytorch/torchtitan#2934 ``` MODULE=graph_trainer.llama3 CONFIG=graph_trainer_llama3_debugmodel ./run_train.sh --compile.mode aot_fx_trace ``` Pull Request resolved: pytorch#180579 Approved by: https://github.com/SherlockNoMad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Enable
transformer_block_bucketingpass inaot_fx_tracecompile mode. Previously this only worked inaotandjitmodes becauseaot_fx_tracetraces a joint fwd+bwd graph wherenn_module_stackmetadata alone isn't sufficient to correctly identify and separate forward vs backward nodes for bucketing.Root cause: In the joint graph produced by
aot_fx_trace, forward and backward collectives coexist. The existingmanual_overlap_bucketingpass needs to bucket each direction independently — forward collectives in forward execution order, backward collectives in reverse order — but had no way to distinguish them.Solution: Leverage the existing
annotate_fnmechanism (already used for AC region tagging) to:annotate_module_fqns)is_bwd=Trueduring metadata propagationmodule_stack_fntomanual_overlap_bucketingthat filters by fwd/bwdA new
joint_transformer_block_bucketing_reordering_passapplies bucketing in two passes — once for forward nodes, once for backward — so each direction's collectives are bucketed and reordered independently.