Skip to content

Fuse pad into convolution - #4442

Open
micwill755 wants to merge 3 commits into
mainfrom
fuse-pad-into-convolution
Open

Fuse pad into convolution#4442
micwill755 wants to merge 3 commits into
mainfrom
fuse-pad-into-convolution

Conversation

@micwill755

Copy link
Copy Markdown
Collaborator

Description

Fold asymmetric constant_pad_nd → convolution patterns into TensorRT convolution pre_padding / post_padding via a new Dynamo post-lowering pass (fuse_pad_into_convolution) and tensorrt::conv_asym_pad converter.

Motivation: Wan-style causal 3D convolutions (used by Cosmos3 / Cosmos3-Edge VAE) zero their own padding and apply it themselves, often asymmetrically in time (e.g. (2 * pad_t, 0)). TensorRT’s ONNX parser already folds Pad → Conv into the convolution’s explicit pre/post padding. Torch-TRT previously only set symmetric padding_nd, so those pads stayed as materialized copy kernels (extra work on the VAE encode path).

Change:

Post-lowering pass rewrites eligible constant_pad_nd → convolution into tensorrt::conv_asym_pad
Converter emits IConvolutionLayer with explicit pre_padding / post_padding (via extended convNd)
Unit tests in tests/py/dynamo/lowering/test_fuse_pad_into_convolution.py
Validation on Cosmos3-Edge policy (Torch-TRT → cosmos3_policy_inference):

Geometry: 256×256, 5 frames, und_len 121, batch 1, 4 denoise steps
Pad fusion removed ~45 extra VAE pad kernels and improved VAE encode 7.46 → 5.50 ms (~1.96 ms)
Full policy steady-state median improved ~23.85 → 21.81 ms (~2.0 ms) after enabling this pass (with MoT builder settings already applied)
No new runtime dependencies beyond existing Torch-TensorRT / TensorRT.

Fixes # (n/a — performance / parity with ONNX Pad→Conv folding)

Type of change
New feature (non-breaking change which adds functionality)
Performance optimization for models with asymmetric Pad→Conv (e.g. Cosmos3 VAE)
Checklist:
My code follows the style guidelines of this project (You can use the linters)
I have performed a self-review of my own code
I have commented my code, particularly in hard-to-understand areas and hacks
I have made corresponding changes to the documentation
I have added tests to verify my fix or my feature
New and existing unit tests pass locally with my changes
I have added the relevant labels to my PR in so that relevant reviewers are notified

Torch-TensorRT only set symmetric padding_nd on convolutions, so models
that apply asymmetric padding themselves (e.g. causal 3D conv with
(2*pad_t, 0) in time) left the pad as a materialized copy. Add a
post-lowering pass that rewrites constant_pad_nd -> convolution into
tensorrt::conv_asym_pad, and convert that op with TRT pre_padding /
post_padding — matching what the ONNX parser already does.
Cover graph rewrite (causal 3D/2D pads, conv-padding combine, multi-user
and non-zero fill skips, idempotence, eager custom-op parity) and end-to-end
TRT numerical checks for padded conv2d/conv3d across stride, dilation,
groups, and bias configurations.
@meta-cla meta-cla Bot added the cla signed label Jul 29, 2026
@micwill755
micwill755 requested a review from narendasan July 29, 2026 02:49
@github-actions github-actions Bot added component: tests Issues re: Tests component: lowering Issues re: The lowering / preprocessing passes component: conversion Issues re: Conversion stage component: core Issues re: The core compiler component: converters Issues re: Specific op converters component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Jul 29, 2026
@micwill755
micwill755 requested a review from cehongwang July 29, 2026 02:49

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to Python style guidelines:

--- /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/lowering/test_fuse_pad_into_convolution.py	2026-07-29 02:49:49.445228+00:00
+++ /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/lowering/test_fuse_pad_into_convolution.py	2026-07-29 02:50:08.060518+00:00
@@ -206,11 +206,13 @@
        x = torch.randn(1, 4, 5, 8, 8)
        weight = torch.randn(4, 4, 3, 3, 3)
        bias = torch.randn(4)
        pre, post = [2, 1, 1], [0, 1, 1]
        expected = F.conv3d(F.pad(x, [1, 1, 1, 1, 2, 0]), weight, bias, 1, 0, 1, 1)
-        got = tensorrt_conv_asym_pad_op(x, weight, bias, [1, 1, 1], pre, post, [1, 1, 1], 1)
+        got = tensorrt_conv_asym_pad_op(
+            x, weight, bias, [1, 1, 1], pre, post, [1, 1, 1], 1
+        )
        torch.testing.assert_close(got, expected)


class TestFusePadIntoConvolutionConverter(DispatchTestCase):
    """End-to-end TRT numerical tests exercising the pad→conv fusion."""

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to Python style guidelines:

--- /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/lowering/test_fuse_pad_into_convolution.py	2026-07-29 02:49:49.054179+00:00
+++ /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/lowering/test_fuse_pad_into_convolution.py	2026-07-29 02:50:15.101134+00:00
@@ -206,11 +206,13 @@
        x = torch.randn(1, 4, 5, 8, 8)
        weight = torch.randn(4, 4, 3, 3, 3)
        bias = torch.randn(4)
        pre, post = [2, 1, 1], [0, 1, 1]
        expected = F.conv3d(F.pad(x, [1, 1, 1, 1, 2, 0]), weight, bias, 1, 0, 1, 1)
-        got = tensorrt_conv_asym_pad_op(x, weight, bias, [1, 1, 1], pre, post, [1, 1, 1], 1)
+        got = tensorrt_conv_asym_pad_op(
+            x, weight, bias, [1, 1, 1], pre, post, [1, 1, 1], 1
+        )
        torch.testing.assert_close(got, expected)


class TestFusePadIntoConvolutionConverter(DispatchTestCase):
    """End-to-end TRT numerical tests exercising the pad→conv fusion."""

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to Python style guidelines:

--- /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/lowering/test_fuse_pad_into_convolution.py	2026-07-29 02:50:10.421070+00:00
+++ /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/lowering/test_fuse_pad_into_convolution.py	2026-07-29 02:50:40.458077+00:00
@@ -206,11 +206,13 @@
        x = torch.randn(1, 4, 5, 8, 8)
        weight = torch.randn(4, 4, 3, 3, 3)
        bias = torch.randn(4)
        pre, post = [2, 1, 1], [0, 1, 1]
        expected = F.conv3d(F.pad(x, [1, 1, 1, 1, 2, 0]), weight, bias, 1, 0, 1, 1)
-        got = tensorrt_conv_asym_pad_op(x, weight, bias, [1, 1, 1], pre, post, [1, 1, 1], 1)
+        got = tensorrt_conv_asym_pad_op(
+            x, weight, bias, [1, 1, 1], pre, post, [1, 1, 1], 1
+        )
        torch.testing.assert_close(got, expected)


class TestFusePadIntoConvolutionConverter(DispatchTestCase):
    """End-to-end TRT numerical tests exercising the pad→conv fusion."""

@narendasan
narendasan requested a review from zewenli98 July 29, 2026 03:56

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some changes that do not conform to Python style guidelines:

--- /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/lowering/test_fuse_pad_into_convolution.py	2026-07-29 03:56:45.218617+00:00
+++ /home/runner/work/TensorRT/TensorRT/tests/py/dynamo/lowering/test_fuse_pad_into_convolution.py	2026-07-29 03:57:10.275509+00:00
@@ -206,11 +206,13 @@
        x = torch.randn(1, 4, 5, 8, 8)
        weight = torch.randn(4, 4, 3, 3, 3)
        bias = torch.randn(4)
        pre, post = [2, 1, 1], [0, 1, 1]
        expected = F.conv3d(F.pad(x, [1, 1, 1, 1, 2, 0]), weight, bias, 1, 0, 1, 1)
-        got = tensorrt_conv_asym_pad_op(x, weight, bias, [1, 1, 1], pre, post, [1, 1, 1], 1)
+        got = tensorrt_conv_asym_pad_op(
+            x, weight, bias, [1, 1, 1], pre, post, [1, 1, 1], 1
+        )
        torch.testing.assert_close(got, expected)


class TestFusePadIntoConvolutionConverter(DispatchTestCase):
    """End-to-end TRT numerical tests exercising the pad→conv fusion."""

@narendasan

Copy link
Copy Markdown
Collaborator

@micwill755 run precommit for the linting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: conversion Issues re: Conversion stage component: converters Issues re: Specific op converters component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: lowering Issues re: The lowering / preprocessing passes component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants