Repository navigation
[graph_trainer] Add remove_b2b_transpose_pass; bundle graph cleanup into canonicalize_graph_pass - #3488
Merged
Conversation
…nto canonicalize_graph_pass
Adds remove_b2b_transpose_pass, which collapses back-to-back
aten.t(aten.t(x)) transpose pairs. These pairs appear in traced fwd+bwd
graphs from F.linear when simple_fsdp redistributes weight tensors. Two
consecutive aten.t form an involution (identical shape and strides), so
removing them is bitwise numerics-preserving. The pass also handles
chains (t(t(t(x))) -> t(x)) and keeps the inner transpose when it feeds
other consumers.
Refactors the individual no-op cleanup passes into a single
canonicalize_graph_pass entry in compile_time_passes:
remove_detach_pass, remove_identity_view_pass, remove_b2b_transpose_pass,
remove_identity_slice_pass, normalize_view_ops_as_reshape.
normalize_view_ops_as_reshape moves from passes.py into
remove_noop_passes.py alongside the other graph-cleanup passes. The
sub-passes stay public so each is unit-tested in isolation.
Note: --compile.disable_passes now toggles canonicalize_graph_pass as a
whole rather than the individual sub-passes (no callsite disabled them
individually).
Verified on the llama3 debug model (FSDP=4, TP=2, aot_fx_trace,
--compile.debug_graph_passes). canonicalize_graph_pass graph diff:
nodes: 2460 -> 1851 (-609)
t.default: 215 -> 86 (-129) <- remove_b2b_transpose_pass
detach.default: 40 -> 0 (-40)
view.default: 634 -> 0 (-634)
_unsafe_view: 43 -> 0 (-43)
slice.Tensor: 40 -> 0 (-40)
reshape.default: 0 -> 277 (+277)
3 training steps, loss 8.11353 -> 7.80330 -> 7.09355.
Test plan:
pytest torchtitan/experiments/graph_trainer/tests/test_passes.py \
-k "TestRemoveB2BTransposePass or TestCanonicalizeGraphPass or \
TestNormalizeViewOpsAsReshape or TestRemoveDetachPass or \
TestRemoveIdentityViewPass or TestRemoveIdentitySlicePass"
SherlockNoMad
requested review from
IvanKobzarev,
aditvenk,
sanketpurandare,
tianyu-l,
xmfan and
yiming0416
as code owners
June 2, 2026 20:28
IvanKobzarev
approved these changes
Jun 3, 2026
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
Two related graph-pass changes in
graph_trainer:Add
remove_b2b_transpose_pass— collapses back-to-backaten.t(aten.t(x))transpose pairs. These appear in traced fwd+bwd graphs fromF.linearwhensimple_fsdpredistributes weight tensors. Two consecutiveaten.tform an involution (identical shape and strides), so removing them is bitwise numerics-preserving. The pass also handles chains (t(t(t(x))) -> t(x)) and keeps the inner transpose when it still feeds other consumers.Bundle the no-op cleanup passes into a single
canonicalize_graph_passentry incompile_time_passes:remove_detach_passremove_identity_view_passremove_b2b_transpose_passremove_identity_slice_passnormalize_view_ops_as_reshapenormalize_view_ops_as_reshapemoves frompasses.pyintoremove_noop_passes.pyalongside the other graph-cleanup passes. The sub-passes stay public so each is unit-tested in isolation.Why
The cleanup passes are all numerics-preserving local rewrites that ran as four separate pass-list entries. Folding them into one
canonicalize_graph_passkeepscompile_time_passesreadable and groups them as one logical step, while the new b2b-transpose rewrite removes redundant transpose pairs thatF.linear+ FSDP introduce.Behavior note
--compile.disable_passesnow togglescanonicalize_graph_passas a whole rather than the individual sub-passes. No callsite (configs/scripts/README) disabled them individually, so nothing breaks.Verification
Ran the llama3 debug model (FSDP=4, TP=2,
aot_fx_trace,--compile.debug_graph_passes).remove_b2b_transpose_passlogs its removal count directly:canonicalize_graph_passis pass #1; its op-count diff confirms the same 129 nodes (t.default: 215 -> 86):3 training steps, loss
8.11353 -> 7.80330 -> 7.09355.Test plan
Added
TestRemoveB2BTransposePass(pair removal + numerics, lone transpose kept, inner transpose with another user kept, chain collapse, no-op on transpose-free graphs) andTestCanonicalizeGraphPass(end-to-end bundle). All pass;pre-commit run --all-filesclean.