Skip to content

fix(dynamo): save compiled modules whose single output is a bare graph node - #4811

Closed
narendasan wants to merge 1 commit into
mainfrom
narendasan/exporter-bare-output
Closed

narendasan wants to merge 1 commit into
mainfrom
narendasan/exporter-bare-output

Conversation

@narendasan

Copy link
Copy Markdown
Collaborator

Summary

  • create_trt_exp_program read the output node's argument as a sequence of nodes, but a single output can also be a bare Node. complex_decomposition_adapter's retrace leaves the graph that way for a model whose complex tensors are all intermediates, such as a rotary embedding (_insert_complex_io_adapters only rewraps it when the model has complex inputs or outputs). torch_tensorrt.save then failed in every format that goes through this function, exported_program and executorch alike, with TypeError: 'Node' object is not iterable.
  • A bare output is now wrapped in a 1-tuple for the program, and the user-facing out_spec stays a single leaf, so the reloaded program still returns the tensor itself.
  • Two LeRobot policies built on Wan 2.2 (LingBot-VA, FastWAM) could not be saved because of this.

Tests

  • tests/py/dynamo/models/test_export_serde.py::test_complex_intermediate_single_output (new): compiles a rotary embedding, saves it, reloads it and checks that a tensor comes back. Fails on main with the TypeError.
  • tests/py/dynamo/models/test_export_serde.py, test_exporter_inlining.py, tests/py/dynamo/lowering/test_exporter_lift_dedup.py, test_complex_decomposition_adapter.py: 37 passed, 1 failed: test_arange_export, which fails on main as well.
  • Checked by hand that the same model saves with output_format="executorch" and the .pte runs.

🤖 Generated with Claude Code

…h node

create_trt_exp_program read the output node's argument as a sequence of nodes,
but a single output can also be a bare Node. complex_decomposition_adapter's
retrace leaves the graph that way for a model whose complex tensors are all
intermediates, such as a rotary embedding (_insert_complex_io_adapters only
rewraps it when the model has complex inputs or outputs). torch_tensorrt.save
then failed in every format that goes through this function, exported_program
and executorch alike, with "'Node' object is not iterable". Wrap a bare output
in a 1-tuple, and keep the user-facing out_spec a single leaf so the reloaded
program still returns the tensor itself.

Two LeRobot policies built on Wan 2.2 (LingBot-VA, FastWAM) could not be saved
because of this.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@meta-cla meta-cla Bot added the cla signed label Oct 7, 2026
@github-actions github-actions Bot added component: tests Issues re: Tests component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Oct 7, 2026
@github-actions
github-actions Bot requested a review from zewenli98 October 7, 2026 23:34
@narendasan
narendasan requested review from apbose and removed request for zewenli98 October 8, 2026 01:49
# complex_decomposition_adapter leaves the graph that way for a model whose
# complex tensors are all intermediates. The program needs the flat tuple form;
# the user-facing out_spec built below still returns the output unwrapped.
bare_output = isinstance(output_nodes, torch.fx.Node)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes the single tensor case, but the same retrace can leave other output shapes here, and those still fail. If the rotary model in the new test returns a dict or a nested tuple, save(retrace=False) still fails further down in this function with 'str' object has no attribute 'meta' or 'tuple' object has no attribute 'meta'.

#4891 fixes the cause instead: it restores the exported codegen and flattens the output inside the complex adapter. With #4891 applied, this branch is never reached for these models, and test_complex_intermediate_single_output passes even without this change. So once both land, this test no longer covers this code.

Please also avoid landing or cherry-picking this one without #4891. With this PR alone, a complex model with keyword inputs can now be saved, but the inputs may be bound in the wrong order. The retrace renames the placeholders to arg0_1, arg1_1, ..., so the kwarg reorder check further down cannot match them by name. If the kwarg_inputs given to save are in a different order than at export time, the reloaded program binds them to the wrong inputs. With same-shaped inputs it returns wrong values, with no error. With #4891 this goes away.

My suggestion: land #4891 and move this test there (or keep it in both). If you still want a guard in the exporter, flattening _output_node.args[0] with pytree.tree_flatten and using its spec as out_spec also handled dict, nested and list outputs in my runs. Pair it with a small test that builds a bare-output graph directly.

@narendasan

Copy link
Copy Markdown
Collaborator Author

Superseded by #4891: the complex adapter now retraces with a plain codegen and restores the exported one, so the output stays a flat tuple and this exporter branch is never reached. test_complex_intermediate_single_output passes on main without this change. Thanks @shoumikhin for the review.

@narendasan narendasan closed this Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants