Skip to content

Commit 5a27528

Browse files
authored
fix: Dynamo compile support for None outputs (#4640)
1 parent c4ce919 commit 5a27528

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎py/torch_tensorrt/dynamo/_compiler.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,8 @@ def preserve_module_specs(
16651665
# than a tuple/list of Nodes.
16661666
outputs = arg if isinstance(arg, (list, tuple)) else [arg]
16671667
for output in outputs:
1668+
if not isinstance(output, torch.fx.Node):
1669+
continue
16681670
target = output.target
16691671
if "_run_on_acc" not in str(target):
16701672
continue

‎tests/py/dynamo/models/test_models.py‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,3 +637,44 @@ def forward(self, x):
637637

638638
# Clean up model env
639639
torch._dynamo.reset()
640+
641+
642+
@pytest.mark.unit
643+
def test_model_with_none_output(ir):
644+
class ModelWithNoneOutput(nn.Module):
645+
def __init__(self):
646+
super().__init__()
647+
self.relu = nn.ReLU()
648+
649+
def forward(self, x):
650+
return self.relu(x), None
651+
652+
model = ModelWithNoneOutput().eval().to("cuda")
653+
input = torch.randn((1, 5)).to("cuda")
654+
655+
compile_spec = {
656+
"inputs": [
657+
torchtrt.Input(
658+
input.shape, dtype=torch.float, format=torch.contiguous_format
659+
)
660+
],
661+
"device": torchtrt.Device("cuda:0"),
662+
"enabled_precisions": {torch.float},
663+
"ir": ir,
664+
"pass_through_build_failures": True,
665+
"min_block_size": 1,
666+
"cache_built_engines": False,
667+
"reuse_cached_engines": False,
668+
}
669+
670+
trt_mod = torchtrt.compile(model, **compile_spec)
671+
trt_out = trt_mod(input)
672+
assertions.assertIsInstance(trt_out, tuple)
673+
assertions.assertEqual(len(trt_out), 2)
674+
assertions.assertIsNone(trt_out[1])
675+
676+
torch_out = model(input)
677+
torch.testing.assert_close(trt_out[0], torch_out[0])
678+
679+
# Clean up model env
680+
torch._dynamo.reset()

0 commit comments

Comments
 (0)