Repository navigation
[Recreate of 2753] [graph_trainer] Replace trace_module/run_traced_module with minimal_fx_tracer API - #2882
Merged
Conversation
…dule with minimal_fx_tracer API Authored-by: Claude Redesign the graph trainer's tracing API. Key changes: make_fx_tracer.py: - Rename trace_module -> minimal_fx_tracer. Takes a callable and returns a tracer that is invoked with args where args[0] must be the nn.Module. Its params/buffers are lifted as graph inputs. fn must be a plain callable (not nn.Module) so trace and execute have the same calling convention with the live module passed explicitly. - Keep explicit traced execution via run_traced (with run_traced_module kept as an alias), instead of making TracedResult itself callable. - Store and restore output pytree spec so run_traced returns the same pytree structure as the original function (e.g. single tensor, list, tuple, dict), not a flat list. - Store param/buffer FQNs at trace time and validate them at execution time to catch module structure mismatches early. - Install TracingContext before make_fx so invoke_subgraph deduplication works. - Validate that all pytree leaves in args are tensors or primitives (int/float/bool/str). Callables like loss_fn must be captured in fn's closure, not passed as args. - Handle tensor subclass (e.g. DTensor) unwrapping/rewrapping via dict-based layouts. Only subclass positions get entries; plain tensors have no entry. - Document run_traced as the reference implementation of traced-graph execution. trainer.py: - Replace FwdBwdStepModule (nn.Module wrapper that only existed because the old trace_module required nn.Module as fn) with make_fwd_bwd_step, a plain function factory. The model is now passed as the first arg, loss_fn is captured in the closure. - Update execution to use run_traced with the live module instead of manually building params_and_buffers snapshots. test_trace_module.py: - Replace TrainStepModule with make_train_step plain function factory. - Update all callsites: trace_module -> minimal_fx_tracer and run_traced_module -> run_traced. - Register BlockMask as pytree node at module level so flex_attention tests pass the leaf validation. - Add test_mismatched_module_raises: FQN validation catches wrong module. - Add test_non_tensor_leaf_raises: callable leaf in args raises ValueError. - Fix DTensor forward test to assert the single traced output as a DTensor, rather than indexing it like a sequence.
tugsbayasgalan
requested review from
SherlockNoMad,
aditvenk,
tianyu-l,
xmfan and
yiming0416
as code owners
April 8, 2026 03:21
SherlockNoMad
approved these changes
Apr 8, 2026
TXacs
pushed a commit
to McmillanTAC/torchtitan
that referenced
this pull request
Apr 13, 2026
…dule with minimal_fx_tracer API (pytorch#2882) Redesign the graph trainer's tracing API. Key changes: make_fx_tracer.py: - Rename trace_module -> minimal_fx_tracer. Takes a callable and returns a tracer that is invoked with args where args[0] must be the nn.Module. Its params/buffers are lifted as graph inputs. fn must be a plain callable (not nn.Module) so trace and execute have the same calling convention with the live module passed explicitly. - Keep explicit traced execution via run_traced (with run_traced_module kept as an alias), instead of making TracedResult itself callable. - Store and restore output pytree spec so run_traced returns the same pytree structure as the original function (e.g. single tensor, list, tuple, dict), not a flat list. - Store param/buffer FQNs at trace time and validate them at execution time to catch module structure mismatches early. - Install TracingContext before make_fx so invoke_subgraph deduplication works. - Validate that all pytree leaves in args are tensors or primitives (int/float/bool/str). Callables like loss_fn must be captured in fn's closure, not passed as args. - Handle tensor subclass (e.g. DTensor) unwrapping/rewrapping via dict-based layouts. Only subclass positions get entries; plain tensors have no entry. - Document run_traced as the reference implementation of traced-graph execution. trainer.py: - Replace FwdBwdStepModule (nn.Module wrapper that only existed because the old trace_module required nn.Module as fn) with make_fwd_bwd_step, a plain function factory. The model is now passed as the first arg, loss_fn is captured in the closure. - Update execution to use run_traced with the live module instead of manually building params_and_buffers snapshots. test_trace_module.py: - Replace TrainStepModule with make_train_step plain function factory. - Update all callsites: trace_module -> minimal_fx_tracer and run_traced_module -> run_traced. - Register BlockMask as pytree node at module level so flex_attention tests pass the leaf validation. - Add test_mismatched_module_raises: FQN validation catches wrong module. - Add test_non_tensor_leaf_raises: callable leaf in args raises ValueError. - Fix DTensor forward test to assert the single traced output as a DTensor, rather than indexing it like a sequence. Authored-by: Claude
jemitche1
pushed a commit
to jemitche1/torchtitan
that referenced
this pull request
Apr 14, 2026
…dule with minimal_fx_tracer API (pytorch#2882) Redesign the graph trainer's tracing API. Key changes: make_fx_tracer.py: - Rename trace_module -> minimal_fx_tracer. Takes a callable and returns a tracer that is invoked with args where args[0] must be the nn.Module. Its params/buffers are lifted as graph inputs. fn must be a plain callable (not nn.Module) so trace and execute have the same calling convention with the live module passed explicitly. - Keep explicit traced execution via run_traced (with run_traced_module kept as an alias), instead of making TracedResult itself callable. - Store and restore output pytree spec so run_traced returns the same pytree structure as the original function (e.g. single tensor, list, tuple, dict), not a flat list. - Store param/buffer FQNs at trace time and validate them at execution time to catch module structure mismatches early. - Install TracingContext before make_fx so invoke_subgraph deduplication works. - Validate that all pytree leaves in args are tensors or primitives (int/float/bool/str). Callables like loss_fn must be captured in fn's closure, not passed as args. - Handle tensor subclass (e.g. DTensor) unwrapping/rewrapping via dict-based layouts. Only subclass positions get entries; plain tensors have no entry. - Document run_traced as the reference implementation of traced-graph execution. trainer.py: - Replace FwdBwdStepModule (nn.Module wrapper that only existed because the old trace_module required nn.Module as fn) with make_fwd_bwd_step, a plain function factory. The model is now passed as the first arg, loss_fn is captured in the closure. - Update execution to use run_traced with the live module instead of manually building params_and_buffers snapshots. test_trace_module.py: - Replace TrainStepModule with make_train_step plain function factory. - Update all callsites: trace_module -> minimal_fx_tracer and run_traced_module -> run_traced. - Register BlockMask as pytree node at module level so flex_attention tests pass the leaf validation. - Add test_mismatched_module_raises: FQN validation catches wrong module. - Add test_non_tensor_leaf_raises: callable leaf in args raises ValueError. - Fix DTensor forward test to assert the single traced output as a DTensor, rather than indexing it like a sequence. Authored-by: Claude
ACharacterInASimulation
pushed a commit
to ACharacterInASimulation/torchtitan
that referenced
this pull request
Apr 21, 2026
…dule with minimal_fx_tracer API (pytorch#2882) Redesign the graph trainer's tracing API. Key changes: make_fx_tracer.py: - Rename trace_module -> minimal_fx_tracer. Takes a callable and returns a tracer that is invoked with args where args[0] must be the nn.Module. Its params/buffers are lifted as graph inputs. fn must be a plain callable (not nn.Module) so trace and execute have the same calling convention with the live module passed explicitly. - Keep explicit traced execution via run_traced (with run_traced_module kept as an alias), instead of making TracedResult itself callable. - Store and restore output pytree spec so run_traced returns the same pytree structure as the original function (e.g. single tensor, list, tuple, dict), not a flat list. - Store param/buffer FQNs at trace time and validate them at execution time to catch module structure mismatches early. - Install TracingContext before make_fx so invoke_subgraph deduplication works. - Validate that all pytree leaves in args are tensors or primitives (int/float/bool/str). Callables like loss_fn must be captured in fn's closure, not passed as args. - Handle tensor subclass (e.g. DTensor) unwrapping/rewrapping via dict-based layouts. Only subclass positions get entries; plain tensors have no entry. - Document run_traced as the reference implementation of traced-graph execution. trainer.py: - Replace FwdBwdStepModule (nn.Module wrapper that only existed because the old trace_module required nn.Module as fn) with make_fwd_bwd_step, a plain function factory. The model is now passed as the first arg, loss_fn is captured in the closure. - Update execution to use run_traced with the live module instead of manually building params_and_buffers snapshots. test_trace_module.py: - Replace TrainStepModule with make_train_step plain function factory. - Update all callsites: trace_module -> minimal_fx_tracer and run_traced_module -> run_traced. - Register BlockMask as pytree node at module level so flex_attention tests pass the leaf validation. - Add test_mismatched_module_raises: FQN validation catches wrong module. - Add test_non_tensor_leaf_raises: callable leaf in args raises ValueError. - Fix DTensor forward test to assert the single traced output as a DTensor, rather than indexing it like a sequence. Authored-by: Claude
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.
Redesign the graph trainer's tracing API.
Key changes:
make_fx_tracer.py:
trainer.py:
test_trace_module.py:
Authored-by: Claude