Skip to content

[Recreate of 2753] [graph_trainer] Replace trace_module/run_traced_module with minimal_fx_tracer API - #2882

Merged
tugsbayasgalan merged 1 commit into
mainfrom
recreate-pr-2753
Apr 8, 2026
Merged

tugsbayasgalan merged 1 commit into
mainfrom
recreate-pr-2753

Conversation

@tugsbayasgalan

@tugsbayasgalan tugsbayasgalan commented Apr 8, 2026 •

Copy link
Copy Markdown
Contributor

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

…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.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Apr 8, 2026
@tugsbayasgalan tugsbayasgalan changed the title [Recreate of 2753] [graph_trainer] Replace trace_module/run_traced_mo… [Recreate of 2753] [graph_trainer] Replace trace_module/run_traced_module with minimal_fx_tracer API Apr 8, 2026
@tugsbayasgalan
tugsbayasgalan merged commit 390da99 into main Apr 8, 2026
25 of 33 checks passed
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

2 participants