Repository navigation
[graph_trainer] Trace forward loss backward step with make_fx - #2561
Conversation
3a54326 to
ed0f024
Compare
| self._train_step_module: TrainStepModule | None = None | ||
| self._traced_step: TracedResult | None = None | ||
|
|
||
| def forward_backward_step( |
There was a problem hiding this comment.
Does this trainer work for other models?
There was a problem hiding this comment.
It works on existing llama3 and dsv3 models in graph_trainer.
Added integration tests and numerics tests
| fsdp_reshard_after_forward, | ||
| joint_passes=[], | ||
| ) | ||
| elif mode == "make_fx": |
There was a problem hiding this comment.
can you think of better name?
There was a problem hiding this comment.
I think we might still want to mention aot in this mode.
What about aot_step or aot_fx_trace
| self.model = model | ||
| self.loss_fn = loss_fn | ||
|
|
||
| def forward(self, inputs, labels, global_valid_tokens, extra_inputs, extra_kwargs): |
There was a problem hiding this comment.
this forward func's signature is not general.
There was a problem hiding this comment.
It matches the caller-side contract used by Trainer.forward_backward_step (in torchtitan/trainer.py).
All Decoder-based models (Llama3, Llama4, Qwen3, DeepSeekV3, GptOss) and the VLM model accept this pattern.
|
|
||
| def forward(self, inputs, labels, global_valid_tokens, extra_inputs, extra_kwargs): | ||
| pred = self.model(inputs, **extra_inputs, **extra_kwargs) | ||
| loss = self.loss_fn(pred, labels) / global_valid_tokens |
There was a problem hiding this comment.
Does this loss pattern apply to all the models?
There was a problem hiding this comment.
Yes. It follows the same pattern in torchtitan/trainer.py, which is used for all models in torchtitan
There was a problem hiding this comment.
Well, all LLM models follow this pattern. The only exception is FLUX model which has its own customized trainer.
| labels: torch.Tensor, | ||
| global_valid_tokens: torch.Tensor, | ||
| ) -> torch.Tensor: | ||
| if self.config.compile.mode != "make_fx": |
There was a problem hiding this comment.
it should raise here.
non-make-fx code shouldn't reach here
There was a problem hiding this comment.
In graph_trainer experiment, we'll always use GraphTrainer which inherits the original Trainer.
The forward_backward_step is overridden so that when we are in non-make-fx mode, we just fallback to super().forward_backward_step
| train_context=self.train_context, | ||
| ) | ||
|
|
||
| params_and_buffers = { |
There was a problem hiding this comment.
is params_and_buffers available from tracedresult, I feel it should? @ydwu4
There was a problem hiding this comment.
Right now the separation of parameters/buffers and execution (the gm and metadata) feels OK. But if we want to follow something like exported program, packaging them together in traced result (e.g. one single object that gets serded) seems reasonable. So whatever that's easy to understand.
|
@claude review this. Think about generality, this path should be applicable for all models in the repo. |
|
Claude finished @SherlockNoMad's task in 4m 12s —— View job Review of PR #2561: [graph_trainer] Trace forward loss backward step with make_fx
I reviewed all 6 changed files and cross-referenced against the base Critical: Generality Issues1. The forward signature
Suggestion: Rather than trying to make one 2. I agree with @SherlockNoMad's existing comment — adding with self.train_context():
self._traced_step = trace_module(
self._train_step_module,
(inputs, labels, global_valid_tokens, extra_inputs, extra_kwargs),
)However, note that Naming Issues3. As @SherlockNoMad noted, "train_step" conventionally means fwd + loss + bwd + optimizer.step(). Since this module only covers fwd + loss + bwd (no optimizer step), 4. Variable name
Logic Issues5. Should raise instead of falling through (trainer.py:62) Currently,
Given the existing review comment asking for a raise, I think the right approach is: keep the 6. @SherlockNoMad asked whether 7. Missing The base Minor Issues8.
9. Gradient accumulation semantics (trainer.py:127-131) if param.grad is None:
param.grad = grad
else:
param.grad += gradThis 10. Loss function unwrapping (trainer.py:103-106) orig = getattr(loss_fn, "_torchdynamo_orig_callable", None)
if orig is not None:
loss_fn = origThis is fragile — it relies on a private Dynamo attribute. If this attribute is renamed or removed in a future PyTorch version, it will silently fail (the compiled loss_fn would be passed to make_fx, which would then fail). Add a comment explaining why this is necessary and consider a more robust approach. 11. Test coverage is Llama3-only Both SummaryThe core idea of tracing fwd+loss+bwd as a single graph via
|
699f3ea to
bc8f822
Compare
bc8f822 to
d06e885
Compare
d06e885 to
52ce35d
Compare
52ce35d to
f437d42
Compare
| loss = outputs[0] | ||
| grads = outputs[1:] | ||
|
|
||
| for param, grad in zip(params, grads): |
There was a problem hiding this comment.
It's not always the case that we can directly += the grad i think? There is an optimizer step.
Since we're not tracing into optimizer at the moment, the other options is to return the loss and grads and do the gradient accumulation outside. Don't have to do in this pr though
There was a problem hiding this comment.
I think this is already doing outside the non-strict region?
This PR adds a new `aot_fx_trace` compilation mode to the graph_trainer experiment that uses `make_fx_tracer` to trace the entire forward pass, loss computation, and backward pass as a single unified graph. It introduces `FwdBwdStepModule` that composes `model(inputs)` → `loss_fn(pred, labels)` → `autograd.grad(loss, params)` into a single traceable forward. Overrides `forward_backward_step` in `GraphTrainer` to use this path when `compile.mode == "aot_fx_trace"`, with lazy tracing on the first step.
This PR adds a new `aot_fx_trace` compilation mode to the graph_trainer experiment that uses `make_fx_tracer` to trace the entire forward pass, loss computation, and backward pass as a single unified graph. It introduces `FwdBwdStepModule` that composes `model(inputs)` → `loss_fn(pred, labels)` → `autograd.grad(loss, params)` into a single traceable forward. Overrides `forward_backward_step` in `GraphTrainer` to use this path when `compile.mode == "aot_fx_trace"`, with lazy tracing on the first step.
…h#2561) This PR adds a new `aot_fx_trace` compilation mode to the graph_trainer experiment that uses `make_fx_tracer` to trace the entire forward pass, loss computation, and backward pass as a single unified graph. It introduces `FwdBwdStepModule` that composes `model(inputs)` → `loss_fn(pred, labels)` → `autograd.grad(loss, params)` into a single traceable forward. Overrides `forward_backward_step` in `GraphTrainer` to use this path when `compile.mode == "aot_fx_trace"`, with lazy tracing on the first step.
…h#2561) This PR adds a new `aot_fx_trace` compilation mode to the graph_trainer experiment that uses `make_fx_tracer` to trace the entire forward pass, loss computation, and backward pass as a single unified graph. It introduces `FwdBwdStepModule` that composes `model(inputs)` → `loss_fn(pred, labels)` → `autograd.grad(loss, params)` into a single traceable forward. Overrides `forward_backward_step` in `GraphTrainer` to use this path when `compile.mode == "aot_fx_trace"`, with lazy tracing on the first step.
This PR adds a new
aot_fx_tracecompilation mode to the graph_trainer experiment that usesmake_fx_tracerto trace the entire forward pass, loss computation, and backward pass as a single unified graph.It introduces
FwdBwdStepModulethat composesmodel(inputs)→loss_fn(pred, labels)→autograd.grad(loss, params)into a single traceable forward. Overridesforward_backward_stepinGraphTrainerto use this path whencompile.mode == "aot_fx_trace", with lazy tracing on the first step.