Skip to content

Commit afd540e

Browse files
committed
refactor(dynamo): make _requires_output_allocator a pure predicate (#4369)
Signed-off-by: cehongwang <wangcehong@gmail.com> (cherry picked from commit 7eec04f)
1 parent 83f5693 commit afd540e

3 files changed

Lines changed: 28 additions & 12 deletions

File tree

py/torch_tensorrt/dynamo/partitioning/_adjacency_partitioner.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ def is_node_supported(
5353
)
5454
return False
5555

56-
if TorchTensorRTOperatorSupport._requires_output_allocator(node):
56+
settings = CONVERTERS.compilation_settings
57+
if (
58+
settings is not None
59+
and settings.fallback_data_dependent_ops
60+
and TorchTensorRTOperatorSupport._requires_output_allocator(node)
61+
):
5762
# data-dependent output shape needs a TRT output allocator, which some
5863
# runtimes cannot consume; honor the fallback and run the node in PyTorch
5964
if not node.is_impure():

py/torch_tensorrt/dynamo/partitioning/_global_partitioner.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,9 @@ def _dtype(n: torch.fx.Node) -> Optional[torch.dtype]:
168168

169169
@staticmethod
170170
def _requires_output_allocator(node: torch.fx.Node) -> bool:
171-
# A converter that needs a TRT output allocator has a data-dependent output
172-
# shape; route the node to PyTorch when fallback_data_dependent_ops is set.
173-
settings = CONVERTERS.compilation_settings
174-
if settings is None or not settings.fallback_data_dependent_ops:
175-
return False
171+
# True if the converter selected for this node needs a TRT output allocator,
172+
# i.e. the node has a data-dependent output shape (e.g. nonzero or boolean
173+
# index). The fallback_data_dependent_ops setting is honored by the caller.
176174
converter_packet = CONVERTERS.get(node)
177175
return converter_packet is not None and converter_packet[2].get(
178176
"requires_output_allocator", False
@@ -192,7 +190,12 @@ def is_node_supported(
192190
)
193191
return False
194192

195-
if self._requires_output_allocator(node):
193+
settings = CONVERTERS.compilation_settings
194+
if (
195+
settings is not None
196+
and settings.fallback_data_dependent_ops
197+
and self._requires_output_allocator(node)
198+
):
196199
# data-dependent output shape needs a TRT output allocator, which some
197200
# runtimes cannot consume; honor the fallback and run the node in PyTorch
198201
if not node.is_impure():

tests/py/dynamo/models/test_fallback_data_dependent_ops.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,29 @@ def forward(self, x):
4343
)
4444

4545

46+
def test_requires_output_allocator_is_setting_independent():
47+
# _requires_output_allocator is a pure predicate: it reports whether the
48+
# converter selected for the node needs a TRT output allocator (decided per node
49+
# via the selected converter, not by op target), independent of any setting.
50+
node = _nonzero_node()
51+
assert TorchTensorRTOperatorSupport._requires_output_allocator(node) is True
52+
53+
4654
def test_output_allocator_node_falls_back_only_when_enabled():
47-
# nonzero's selected converter requires an output allocator, so the partitioner
48-
# marks the node unsupported only when the flag is on. This is decided per node
49-
# (via the selected converter), not by op target.
55+
# The node is routed to PyTorch (unsupported) only when fallback_data_dependent_ops
56+
# is on; with it off the node stays on TensorRT.
5057
node = _nonzero_node()
58+
support = TorchTensorRTOperatorSupport()
5159
original = CONVERTERS.compilation_settings
5260
try:
5361
CONVERTERS.set_compilation_settings(
5462
CompilationSettings(fallback_data_dependent_ops=False)
5563
)
56-
assert TorchTensorRTOperatorSupport._requires_output_allocator(node) is False
64+
assert support.is_node_supported({}, node) is True
5765
CONVERTERS.set_compilation_settings(
5866
CompilationSettings(fallback_data_dependent_ops=True)
5967
)
60-
assert TorchTensorRTOperatorSupport._requires_output_allocator(node) is True
68+
assert support.is_node_supported({}, node) is False
6169
finally:
6270
CONVERTERS.compilation_settings = original
6371

0 commit comments

Comments
 (0)