-
Notifications
You must be signed in to change notification settings - Fork 413
Skip rebuilding FX forward after post-lowering and before adjacency split. #4599
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
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
4 changes: 2 additions & 2 deletions
4
py/torch_tensorrt/dynamo/lowering/passes/batch_cheap_fx_cleanups.py
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
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
104 changes: 104 additions & 0 deletions
104
tests/py/dynamo/lowering/test_skip_conversion_recompile.py
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,104 @@ | ||
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| import torch | ||
| from torch_tensorrt.dynamo._settings import CompilationSettings | ||
| from torch_tensorrt.dynamo.lowering import post_lowering | ||
| from torch_tensorrt.dynamo.lowering.passes.pass_utils import ( | ||
| clean_up_graph_after_modifications, | ||
| ) | ||
|
|
||
|
|
||
| def _count_recompile(fn: object) -> int: | ||
| calls = {"n": 0} | ||
| orig = torch.fx.GraphModule.recompile | ||
|
|
||
| def counting(self: torch.fx.GraphModule) -> None: | ||
| calls["n"] += 1 | ||
| return orig(self) | ||
|
|
||
| with patch.object(torch.fx.GraphModule, "recompile", counting): | ||
| fn() | ||
| return calls["n"] | ||
|
|
||
|
|
||
| class _AddOne(torch.nn.Module): | ||
| def forward(self, value: torch.Tensor) -> torch.Tensor: | ||
| return value + 1 | ||
|
|
||
|
|
||
| def _exported_add_one() -> torch.fx.GraphModule: | ||
| return torch.export.export(_AddOne(), (torch.ones(2, 2),)).module() | ||
|
|
||
|
|
||
| class TestSkipConversionRecompile(unittest.TestCase): | ||
| def test_post_lowering_skips_recompile(self) -> None: | ||
| gm = _exported_add_one() | ||
| n = _count_recompile(lambda: post_lowering(gm, CompilationSettings())) | ||
| self.assertEqual(n, 0) | ||
|
|
||
| def test_post_lowering_still_eliminates_dead_code(self) -> None: | ||
| gm = _exported_add_one() | ||
| output = next(n for n in reversed(list(gm.graph.nodes)) if n.op == "output") | ||
| inp = next(n for n in gm.graph.nodes if n.op == "placeholder") | ||
| with gm.graph.inserting_before(output): | ||
| gm.graph.call_function(torch.ops.aten.mul.Tensor, args=(inp, inp)) | ||
| mul_before = sum( | ||
| 1 for n in gm.graph.nodes if n.target is torch.ops.aten.mul.Tensor | ||
| ) | ||
| self.assertEqual(mul_before, 1) | ||
|
|
||
| post_lowering(gm, CompilationSettings()) | ||
|
|
||
| mul_after = sum( | ||
| 1 for n in gm.graph.nodes if n.target is torch.ops.aten.mul.Tensor | ||
| ) | ||
| self.assertEqual(mul_after, 0) | ||
|
|
||
| def test_interpreter_runs_without_recompile(self) -> None: | ||
| value = torch.ones(2, 2) | ||
| gm = torch.export.export(_AddOne(), (value,)).module() | ||
| gm = post_lowering(gm, CompilationSettings()) | ||
| out = torch.fx.Interpreter(gm).run(value) | ||
| torch.testing.assert_close(out, value + 1) | ||
|
|
||
| def test_clean_up_outside_defer_still_recompiles(self) -> None: | ||
| gm = _exported_add_one() | ||
| n = _count_recompile(lambda: clean_up_graph_after_modifications(gm)) | ||
| self.assertGreater(n, 0) | ||
|
|
||
| def test_post_lowering_recompile_true_rebuilds_forward(self) -> None: | ||
| value = torch.ones(2, 2) | ||
| gm = torch.export.export(_AddOne(), (value,)).module() | ||
| n = _count_recompile( | ||
| lambda: post_lowering(gm, CompilationSettings(), recompile=True) | ||
| ) | ||
| self.assertGreater(n, 0) | ||
| torch.testing.assert_close(gm(value), value + 1) | ||
|
|
||
| def test_fast_partition_does_not_recompile_input_module(self) -> None: | ||
| from torch_tensorrt.dynamo.partitioning import fast_partition | ||
|
|
||
| gm = _exported_add_one() | ||
| calls = {"n": 0} | ||
| orig = gm.recompile | ||
|
|
||
| def counting(*args: object, **kwargs: object) -> None: | ||
| calls["n"] += 1 | ||
| return orig(*args, **kwargs) | ||
|
|
||
| gm.recompile = counting # type: ignore[method-assign] | ||
| partitioned, _ = fast_partition( | ||
| gm, | ||
| min_block_size=1, | ||
| require_full_compilation=True, | ||
| assume_full_support=True, | ||
| skip_fusion=True, | ||
| ) | ||
| self.assertEqual(calls["n"], 0) | ||
| value = torch.ones(2, 2) | ||
| torch.testing.assert_close(partitioned(value), value + 1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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
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.
Check out
torch.fx._lazy_graph_module._LazyGraphModule.AI:
Defaulting recompile=False assumes the lowered module is never run as Python. But compile_module returns it unconverted when a graph has fewer supported ops than min_block_size, and the torch.compile backend then runs its stale forward.
backends.py:367 calls post_lowering(gm, settings) with the default, then compile_module. _compiler.py:1381 returns gm when num_supported_ops < min_block_size, which happens often with small Dynamo graph-break fragments. That gm.forward was generated before lowering. It still runs pre-lowering code, and it can refer to attributes that constant_fold deleted.
This PR already had to pass recompile=True in harness.py and test_static_cache.py for this reason. The same applies to compile(..., dryrun=True) at :1415. There's no test for the early-return path. I found this by inspection and haven't run it.
The fix is to convert to
torch.fx._lazy_graph_module._LazyGraphModuleinstead of adding the flag. Its recompile() only marks the code dirty, and code generation runs on first access to forward. I measured 56.75 ms for GraphModule.recompile() against 0.01 ms for the lazy version on a 2000-node graph. That's the same saving, with no stale-module risk.