Repository navigation
fix(dynamo): save compiled modules whose single output is a bare graph node - #4811
narendasan wants to merge 1 commit into
Conversation
…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]>
| # 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) |
There was a problem hiding this comment.
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.
|
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. |
Summary
create_trt_exp_programread the output node's argument as a sequence of nodes, but a single output can also be a bareNode.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_adaptersonly rewraps it when the model has complex inputs or outputs).torch_tensorrt.savethen failed in every format that goes through this function,exported_programandexecutorchalike, withTypeError: 'Node' object is not iterable.out_specstays a single leaf, so the reloaded program still returns the tensor itself.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 onmainwith theTypeError.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 onmainas well.output_format="executorch"and the.pteruns.🤖 Generated with Claude Code