-
Notifications
You must be signed in to change notification settings - Fork 410
fix(dynamo): correct legacy exporter (retrace=False) submodule inlining for hybrid graphs #4446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Conarnar
wants to merge
1
commit into
pytorch:main
Choose a base branch
from
Conarnar:fix/executorch-retrace-false-hybrid
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+175
−60
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| """Unit tests for the legacy dynamo exporter's submodule inlining | ||
| (torch_tensorrt.dynamo._exporter). These run on plain fx graphs and need neither a | ||
| GPU nor a TensorRT build.""" | ||
|
|
||
| import operator | ||
|
|
||
| import pytest | ||
| import torch | ||
| from torch_tensorrt.dynamo._exporter import inline_torch_modules | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_inline_torch_modules_wires_inputs_by_position(): | ||
| """inline_torch_modules must wire a _run_on_gpu submodule's inputs from the | ||
| call_module args by POSITION, not by matching placeholder names to graph nodes. | ||
|
|
||
| Regression: the old name-matching path bound a submodule input to a same-named | ||
| but unrelated graph node, rewiring a consumer to the wrong producer (and, for a | ||
| submodule mixing graph-input and computed-intermediate inputs, leaking the | ||
| latter as spurious graph placeholders). Here the submodule's first input | ||
| placeholder is named "y", colliding with the parent's second input "y" even | ||
| though the first *argument* is the parent's "x"; positional wiring must ignore | ||
| the collision. Subtraction makes the input order observable. | ||
| """ | ||
| # Submodule: out = first - second. First placeholder deliberately named "y". | ||
| sub_graph = torch.fx.Graph() | ||
| first = sub_graph.placeholder("y") | ||
| second = sub_graph.placeholder("z") | ||
| sub_graph.output(sub_graph.call_function(torch.sub, (first, second))) | ||
| submodule = torch.fx.GraphModule(torch.nn.Module(), sub_graph) | ||
|
|
||
| # Parent: inputs (x, y); call _run_on_gpu_0(x, y) -> expected x - y. | ||
| parent_graph = torch.fx.Graph() | ||
| x = parent_graph.placeholder("x") | ||
| y = parent_graph.placeholder("y") | ||
| root = torch.nn.Module() | ||
| root.add_module("_run_on_gpu_0", submodule) | ||
| call = parent_graph.call_module("_run_on_gpu_0", (x, y)) | ||
| parent_graph.output(call) | ||
| parent = torch.fx.GraphModule(root, parent_graph) | ||
|
|
||
| n_placeholders_before = sum(1 for n in parent.graph.nodes if n.op == "placeholder") | ||
|
|
||
| inline_torch_modules(parent) | ||
| parent.recompile() | ||
|
|
||
| # No spurious placeholders leaked by the inlining. | ||
| assert ( | ||
| sum(1 for n in parent.graph.nodes if n.op == "placeholder") | ||
| == n_placeholders_before | ||
| ) | ||
| # No call_module node survives (the submodule was inlined). | ||
| assert not any(n.op == "call_module" for n in parent.graph.nodes) | ||
| # Positional wiring: first input <- x, second input <- y, so out == x - y. | ||
| out = parent(torch.tensor(5.0), torch.tensor(3.0)) | ||
| assert torch.allclose(out, torch.tensor(2.0)) | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_inline_torch_modules_preserves_all_submodule_outputs(): | ||
| """A multi-output _run_on_gpu submodule must keep every output wired to its | ||
| consumer after inlining. Regression: a mis-wired input orphaned one submodule | ||
| output, which dead-code elimination then pruned, leaving a downstream consumer | ||
| (or, in the hybrid case, a TensorRT engine) short an output at runtime. | ||
| """ | ||
| # Submodule returns (a + b, a - b); both outputs are consumed downstream. | ||
| sub_graph = torch.fx.Graph() | ||
| a = sub_graph.placeholder("a") | ||
| b = sub_graph.placeholder("b") | ||
| add = sub_graph.call_function(torch.add, (a, b)) | ||
| sub = sub_graph.call_function(torch.sub, (a, b)) | ||
| sub_graph.output((add, sub)) | ||
| submodule = torch.fx.GraphModule(torch.nn.Module(), sub_graph) | ||
|
|
||
| parent_graph = torch.fx.Graph() | ||
| x = parent_graph.placeholder("x") | ||
| y = parent_graph.placeholder("y") | ||
| root = torch.nn.Module() | ||
| root.add_module("_run_on_gpu_0", submodule) | ||
| call = parent_graph.call_module("_run_on_gpu_0", (x, y)) | ||
| o0 = parent_graph.call_function(operator.getitem, (call, 0)) | ||
| o1 = parent_graph.call_function(operator.getitem, (call, 1)) | ||
| # Consume both outputs: (a+b) * (a-b). | ||
| parent_graph.output(parent_graph.call_function(torch.mul, (o0, o1))) | ||
| parent = torch.fx.GraphModule(root, parent_graph) | ||
|
|
||
| inline_torch_modules(parent) | ||
| parent.recompile() | ||
|
|
||
| # (x+y)*(x-y) == x^2 - y^2 ; with x=5, y=3 -> 25 - 9 = 16. | ||
| out = parent(torch.tensor(5.0), torch.tensor(3.0)) | ||
| assert torch.allclose(out, torch.tensor(16.0)) | ||
|
|
||
|
|
||
| @pytest.mark.unit | ||
| def test_inline_torch_modules_computed_intermediate_inputs(): | ||
| """A _run_on_gpu submodule whose inputs are computed intermediates (not top-level | ||
| graph placeholders, and not name-matching any graph node) must inline correctly. | ||
| This is the case the old zero-duplicate path handled; positional wiring preserves | ||
| it (and it is the shape that leaked spurious placeholders in the mixed case). | ||
| """ | ||
| # Submodule: out = m + n. Names don't collide with anything in the parent. | ||
| sub_graph = torch.fx.Graph() | ||
| m = sub_graph.placeholder("m") | ||
| n = sub_graph.placeholder("n") | ||
| sub_graph.output(sub_graph.call_function(torch.add, (m, n))) | ||
| submodule = torch.fx.GraphModule(torch.nn.Module(), sub_graph) | ||
|
|
||
| # Parent: x -> c0 = x*2, c1 = x+1; call _run_on_gpu_0(c0, c1) -> c0 + c1. | ||
| parent_graph = torch.fx.Graph() | ||
| x = parent_graph.placeholder("x") | ||
| c0 = parent_graph.call_function(torch.mul, (x, 2)) | ||
| c1 = parent_graph.call_function(torch.add, (x, 1)) | ||
| root = torch.nn.Module() | ||
| root.add_module("_run_on_gpu_0", submodule) | ||
| call = parent_graph.call_module("_run_on_gpu_0", (c0, c1)) | ||
| parent_graph.output(call) | ||
| parent = torch.fx.GraphModule(root, parent_graph) | ||
|
|
||
| inline_torch_modules(parent) | ||
| parent.recompile() | ||
|
|
||
| # No spurious placeholders leaked; the computed intermediates stay in-graph. | ||
| assert sum(1 for node in parent.graph.nodes if node.op == "placeholder") == 1 | ||
| # out = (x*2) + (x+1); x=5 -> 10 + 6 = 16. | ||
| out = parent(torch.tensor(5.0)) | ||
| assert torch.allclose(out, torch.tensor(16.0)) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should fix: when no example inputs are given, this builds an
in_specthat says the model takes zero inputs, while the graph still has its placeholders.torch_tensorrt.save(..., retrace=False)passesarg_inputs=(), notNone(_compile.py:831), and the API tells users inputs are not needed on this path (_compile.py:1012). Soexample_argsis(),in_specends up with 0 leaves, and nothing compares it against the placeholders.I reproduced this on torch 2.11. The
ExportedProgrambuilds,torch.export.saveandtorch.export.loadboth succeed, and it only fails much later:ep.module()(x)raisesTrying to flatten user inputs with exported input tree spec ...output_format="executorch"fails insideto_edgewithleaves has length 2 but the spec refers to a pytree that holds 0 itemsBefore this PR the same call failed immediately with
'CodeGen' object has no attribute 'pytree_info', so this turns a loud failure into a silently broken artifact.It is reachable in the normal flow:
lowering/_buffer_lifting.py:166doesgm.graph.set_codegen(torch.fx.graph.CodeGen())for any model with mutated buffers, and nothing puts the pytree codegen back.One more thing: the comment above says this fallback matches
retrace=True. In the no-input case it does not.retrace=Truegoes throughtorch.export.export(module, args=())and raisesTypeError: missing a required argument.Suggestion: when no inputs are supplied, build the spec positionally from the graph placeholders instead of from an empty tuple, and assert that
in_spec.num_leavesmatches the placeholder count. The supplied-input path is already correct, including kwargs, so it does not need to change.