Skip to content

Commit 3f8a73c

Browse files
tp5uiucclaude
andauthored
fix: guard the 3D convolutions the pad-folding pass hides from the conv guard (#4644)
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
1 parent e37cc6d commit 3f8a73c

4 files changed

Lines changed: 115 additions & 16 deletions

File tree

‎py/torch_tensorrt/dynamo/conversion/aten_ops_converters.py‎

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3272,6 +3272,30 @@ def aten_ops_le(
32723272
_CONV_ARG_TRANSPOSED = 6
32733273

32743274

3275+
def turing_rejects_forward_convolution(
3276+
node: Node,
3277+
spatial_rank: Optional[int],
3278+
settings: Optional[CompilationSettings] = None,
3279+
) -> bool:
3280+
"""Whether a forward convolution over ``spatial_rank`` spatial dims must fall back.
3281+
3282+
No valid kernel config for 3D ConvFwd on SM 7.5 for TensorRT-RTX, a known gap.
3283+
Transposed 3D is a distinct layer and is unaffected, so callers must only ask about
3284+
non-transposed convolutions.
3285+
"""
3286+
unsupported_spatial_rank = 3
3287+
if spatial_rank != unsupported_spatial_rank:
3288+
return False
3289+
if not trt_rtx_targets_turing(settings):
3290+
return False
3291+
_LOGGER.debug(
3292+
"3D convolution '%s' is not supported on TensorRT-RTX for Turing "
3293+
"(SM 7.5). Falling back to PyTorch.",
3294+
node.name,
3295+
)
3296+
return True
3297+
3298+
32753299
def convolution_capability_validator(
32763300
node: Node, settings: Optional[CompilationSettings] = None
32773301
) -> bool:
@@ -3298,20 +3322,14 @@ def convolution_capability_validator(
32983322
)
32993323
return False
33003324

3301-
# No valid kernel config for 3D ConvFwd on SM 7.5: the engine builds, but
3302-
# createExecutionContext() then returns nullptr. Transposed 3D is a distinct layer
3303-
# and is unaffected. aten.convolution input is (N, C, *spatial), so ndim 5 is 3D.
3304-
is_forward_conv = not args_bounds_check(node.args, _CONV_ARG_TRANSPOSED)
3305-
if trt_rtx_targets_turing(settings) and is_forward_conv:
3325+
# aten.convolution input is (N, C, *spatial), so ndim - 2 is the spatial rank.
3326+
# Like every validator in this module this relies on meta["val"] and fails open
3327+
# when it is absent.
3328+
if not args_bounds_check(node.args, _CONV_ARG_TRANSPOSED):
33063329
input_node = node.args[_CONV_ARG_INPUT]
33073330
val = input_node.meta.get("val") if hasattr(input_node, "meta") else None
3308-
if (ndim := getattr(val, "ndim", None)) == 5:
3309-
_LOGGER.debug(
3310-
"3D convolution '%s' (ndim %s) is not supported on TensorRT-RTX for "
3311-
"Turing (SM 7.5). Falling back to PyTorch.",
3312-
node.name,
3313-
ndim,
3314-
)
3331+
spatial_rank = val.ndim - 2 if val is not None else None
3332+
if turing_rejects_forward_convolution(node, spatial_rank, settings):
33153333
return False
33163334

33173335
return True

‎py/torch_tensorrt/dynamo/conversion/custom_ops_converters.py‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
# mypy: disallow-untyped-decorators=False
22

33
import logging
4-
from typing import Dict, Sequence, Tuple, Union
4+
from typing import Dict, Optional, Sequence, Tuple, Union
55

66
import tensorrt as trt
7-
from torch.fx.node import Argument, Target
7+
from torch.fx.node import Argument, Node, Target
88
from torch_tensorrt._features import ENABLED_FEATURES
9+
from torch_tensorrt.dynamo._settings import CompilationSettings
910
from torch_tensorrt.dynamo._SourceIR import SourceIR
1011
from torch_tensorrt.dynamo.conversion import impl
1112
from torch_tensorrt.dynamo.conversion._ConversionContext import ConversionContext
1213
from torch_tensorrt.dynamo.conversion._ConverterRegistry import (
1314
dynamo_tensorrt_converter,
1415
)
16+
from torch_tensorrt.dynamo.conversion.aten_ops_converters import (
17+
turing_rejects_forward_convolution,
18+
)
19+
from torch_tensorrt.dynamo.conversion.converter_utils import args_bounds_check
1520
from torch_tensorrt.dynamo.lowering.passes.fuse_distributed_ops import (
1621
tensorrt_fused_nccl_all_gather_op,
1722
tensorrt_fused_nccl_all_reduce_op,
@@ -27,7 +32,34 @@
2732
_LOGGER: logging.Logger = logging.getLogger(__name__)
2833

2934

30-
@dynamo_tensorrt_converter(tensorrt_conv_asym_pad_op, supports_dynamic_shapes=True)
35+
def conv_asym_pad_capability_validator(
36+
node: Node, settings: Optional[CompilationSettings] = None
37+
) -> bool:
38+
"""Apply the aten.convolution capability guards to the fused pad+conv op.
39+
40+
``fuse_pad_into_convolution`` rewrites constant_pad_nd -> aten.convolution into
41+
this op during lowering, which erases the aten.convolution node that
42+
``convolution_capability_validator`` would otherwise have rejected. Without this,
43+
the pass smuggles a 3D convolution past that guard onto Turing (SM 7.5), where it
44+
surfaces at runtime as a null execution context. The pass runs in the normal
45+
compile path, so this is not confined to tests.
46+
47+
Spatial rank comes from the argument list rather than node meta, so the guard holds
48+
whether or not the caller traced with dynamo.
49+
"""
50+
# conv_asym_pad(source, weight, bias, stride, pre_padding, post_padding, dilation,
51+
# groups) -- no transposed argument; the pass skips those outright.
52+
_ARG_STRIDE = 3
53+
stride = args_bounds_check(node.args, _ARG_STRIDE)
54+
spatial_rank = len(stride) if stride is not None else None
55+
return not turing_rejects_forward_convolution(node, spatial_rank, settings)
56+
57+
58+
@dynamo_tensorrt_converter(
59+
tensorrt_conv_asym_pad_op,
60+
capability_validator=conv_asym_pad_capability_validator,
61+
supports_dynamic_shapes=True,
62+
)
3163
def conv_asym_pad(
3264
ctx: ConversionContext,
3365
target: Target,

‎tests/py/dynamo/lowering/test_fuse_pad_into_convolution.py‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import torch.nn.functional as F
66
from parameterized import parameterized
77

8-
from ..conversion.harness import DispatchTestCase
8+
from ..conversion.harness import DispatchTestCase, skip_if_trt_rtx_turing
99

1010

1111
def _node_targets(gm: torch.fx.GraphModule) -> list:
@@ -230,6 +230,11 @@ class TestFusePadIntoConvolutionConverter(DispatchTestCase):
230230
]
231231
)
232232
def test_padded_conv3d(self, _, pad, stride, dilation, groups, bias):
233+
# The fusion turns this into tensorrt::conv_asym_pad, whose validator rejects
234+
# 3D on Turing. DispatchTestCase has no PyTorch-fallback path, so without this
235+
# the rejection surfaces as UnsupportedOperatorException.
236+
skip_if_trt_rtx_turing(self, "3D convolution")
237+
233238
class PaddedConv3d(nn.Module):
234239
def __init__(self) -> None:
235240
super().__init__()

‎tests/py/dynamo/models/test_turing_capability_guards.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,33 @@ def forward(self, x):
7070
return self.conv(x)
7171

7272

73+
class PaddedConv3d(nn.Module):
74+
"""A conv3d the 3D-conv guard never sees as a convolution.
75+
76+
fuse_pad_into_convolution folds the pad into the conv and rewrites the pair as
77+
tensorrt::conv_asym_pad, erasing the aten.convolution node. It fires for any
78+
zero-fill, non-negative pad -- asymmetry is not required.
79+
"""
80+
81+
def __init__(self):
82+
super().__init__()
83+
self.conv = nn.Conv3d(4, 8, 3, padding=0)
84+
85+
def forward(self, x):
86+
return self.conv(F.pad(x, (1, 1, 1, 1, 2, 0)))
87+
88+
89+
class PaddedConv2d(nn.Module):
90+
"""Same fusion, 2D -- which Turing does support."""
91+
92+
def __init__(self):
93+
super().__init__()
94+
self.conv = nn.Conv2d(4, 8, 3, padding=0)
95+
96+
def forward(self, x):
97+
return self.conv(F.pad(x, (0, 1, 0, 1)))
98+
99+
73100
@unittest.skipIf(
74101
not ENABLED_FEATURES.tensorrt_rtx,
75102
"Turing capability guards only apply to TensorRT-RTX",
@@ -154,6 +181,23 @@ def test_declared_turing_target_keeps_conv2d_on_trt(self):
154181
compiled = self._compile(mod, inputs, target_compute_capabilities=[TURING])
155182
self.assertGreater(_trt_submodule_count(compiled), 0)
156183

184+
def test_declared_turing_target_falls_back_padded_conv3d(self):
185+
# The fused op needs its own guard: by partitioning time the aten.convolution
186+
# node the conv guard keys on no longer exists.
187+
mod = PaddedConv3d()
188+
inputs = (torch.randn(1, 4, 8, 8, 8).cuda(),)
189+
compiled = self._compile(mod, inputs, target_compute_capabilities=[TURING])
190+
self.assertEqual(_trt_submodule_count(compiled), 0)
191+
self._assert_matches_eager(mod, compiled, inputs)
192+
193+
def test_declared_turing_target_keeps_padded_conv2d_on_trt(self):
194+
# Only rank 3 is rejected; the fused op must keep serving 2D.
195+
mod = PaddedConv2d()
196+
inputs = (torch.randn(1, 4, 16, 16).cuda(),)
197+
compiled = self._compile(mod, inputs, target_compute_capabilities=[TURING])
198+
self.assertGreater(_trt_submodule_count(compiled), 0)
199+
self._assert_matches_eager(mod, compiled, inputs)
200+
157201
def test_declared_turing_target_keeps_transposed_conv3d_on_trt(self):
158202
# Transposed 3D convolution is a distinct layer and does work on Turing.
159203
mod = ConvTranspose3d()

0 commit comments

Comments
 (0)