Skip to content

add forward portable Conv3d support for contiguous 5D inputs in aten:convolution.out - #19977

Merged
rascani merged 4 commits into
pytorch:mainfrom
arkadip-maitra:fix_11704
Jul 8, 2026
Merged

rascani merged 4 commits into
pytorch:mainfrom
arkadip-maitra:fix_11704

Conversation

@arkadip-maitra

@arkadip-maitra arkadip-maitra commented Jun 3, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #11704

  • add forward portable Conv3d support for contiguous 5D inputs in aten::convolution.out
  • add direct kernel tests and a portable_lib e2e regression, including nonzero circular padding

updated according to comments, added tests that cover float16, bfloat16, int
ci failures seems unrelated to my test

cc @larryliu0820 @manuelcandales @psiddh @AdrianLundell @digantdesai @rascani

@pytorch-bot

pytorch-bot Bot commented Jun 3, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/19977

Note: Links to docs will display an error until the docs builds have been completed.

✅ You can merge normally! (1 Unrelated Failure)

As of commit 26cde89 with merge base dcc04ef (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jun 3, 2026
@github-actions

github-actions Bot commented Jun 3, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@arkadip-maitra
arkadip-maitra marked this pull request as ready for review June 3, 2026 15:12
expected = inputs[0] + inputs[0]
self.assertEqual(str(expected), str(executorch_output))

def test_conv3d_portable_e2e(self):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit: This test maybe doesn't belong in test_pybindings. I think the native test coverage should be sufficient, so you can likely drop this one.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok dropped it, thanks for the comment

EXPECT_TENSOR_CLOSE(out, expected);
}

TEST_F(OpConvCorrectnessTest, Conv3dDefaultParams) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you add tests that cover float16, bfloat16, and int? You could do something similar to

void test_dtype() {
to exercise all of the supported types.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks for the comment, made this change

@arkadip-maitra

Copy link
Copy Markdown
Contributor Author

@GregoryComer thanks for the reviews, updated!

@arkadip-maitra

Copy link
Copy Markdown
Contributor Author

@GregoryComer can you take a look at this again pls, the failures seems unrelated

@rascani

rascani commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Updating the branch to fix the size tests.

Comment thread backends/cortex_m/test/ops/test_conv.py Resolved
@arkadip-maitra

Copy link
Copy Markdown
Contributor Author

@rascani hi can you also merge pls. Thanks!

@rascani
rascani merged commit 636daf1 into pytorch:main Jul 8, 2026
173 of 174 checks passed
@rascani

rascani commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

@rascani hi can you also merge pls. Thanks!

Done. Thank you for the contribution!

@nil-is-all nil-is-all added module: kernels Issues related to kernel libraries and utilities, and code under kernels/ module: microcontrollers For embedded MCUs like Cortex-M, or RTOS like Zephyr, does not track NPU backend like Arm Ethos. labels Jul 10, 2026
shoumikhin added a commit that referenced this pull request Nov 2, 2026
## What is wrong today

A model with `torch.nn.Conv3d` lowers without an error but fails at run
time on CPU, for example with XNNPACK:

```
Check failed (in.dim() == 3 || in.dim() == 4): Expect input tensor to be 3-D or 4-D, but got, 5.
```

XNNPACK does not take 3-D or bfloat16 convolutions, so they run on the
portable convolution kernel, which only supported 1-D and 2-D. This
blocks vision encoders with a `Conv3d` patch embedding, such as Qwen2-VL
and Qwen2.5-VL. It is the failure in #11704.

## What this change does

The portable convolution kernel now runs 3-D convolutions, regular and
transposed. 1-D and 2-D convolutions run as 3-D ones with extra dims of
size 1 and add up their terms in the same order as before, so their
results stay the same (except that, with fused multiply-add, a result of
-0 can now be +0, as in PyTorch). The input and output may now have
different dim orders, because `torch.export` gives a `Conv3d` with a
channels-last input a contiguous output. The backend test suite no
longer skips 3-D convolutions.

It also fixes older bugs in the argument checks, which affect 1-D and
2-D too:

- A bias of the wrong length was read past its end (missing
parentheses), and a Bool bias crashed the kernel.
- `groups = 0` divided by zero, and a huge `groups` could overflow in
the bias check.
- A huge padding or dilation, or an output size too large for the 32-bit
tensor sizes, let a call that should fail run. Stride, padding and
dilation must now fit in 32 bits.
- Like PyTorch, the checks now reject a kernel dim of 0, a regular
convolution's kernel larger than the padded input, and an empty spatial
dim unless the batch or channel dim is empty too.

## Testing

- New tests cover grouped regular and transposed 3-D convolutions with
contiguous, channels-last and mixed layouts, and the checks above. All
but one fail without this change; that one checks that an empty batch or
channel dim still works.
- The portable kernel tests pass with AddressSanitizer and
UndefinedBehaviorSanitizer on Linux Arm64.
- The Cortex-M `conv3d` test, now with several channels, a 2x3x3 kernel,
stride and padding, no longer expects a failure and passes in CI on the
Arm simulator.
- A `Conv3d` patch embedding lowered with XNNPACK, and random
convolutions on the portable kernel, match eager PyTorch in float32 and
bfloat16.
- Speed against main on Arm64 and x86-64: regular 1-D and 2-D
convolutions are 1.5 to 13 times faster with GCC and with Clang at
`-Os`, and 18 percent faster to 11 percent slower with Clang at `-O2`
and `-O3`. Transposed ones are 5 percent faster to 17 percent slower.
- Code size for a Cortex-M55 (Arm GNU toolchain 15.2):
`op_convolution.o` grows from 32,484 to 33,263 bytes at `-Os` and from
48,477 to 61,457 at `-O3`. `kernel_ops_util.o`, which holds the argument
checks, grows from 15,746 to 17,743 bytes at `-Os` and from 25,932 to
28,979 at `-O3`.

## Notes

- Depends on #24380. Without it, a static int8 `Conv3d` lowered with
XNNPACK returns wrong values with no error, or fails to load, and the
backend test suite's static int8 XNNPACK `Conv3d` cases fail. Please
merge #24380 first or together with this change.
- An earlier version of 3-D support was reverted, see #19977.
JacobSzwejbka pushed a commit that referenced this pull request Nov 3, 2026
Merge after #24445. It does not depend on it to build, but #24445 brings
the XNNPACK fix for statically quantized 3-D convolutions, and without
it the backend test suite would report those cases as failing once this
pick removes the 3-D skip.

## What is wrong today

A model with `torch.nn.Conv3d` lowers without an error but fails at run
time on CPU, for example with XNNPACK:

```
Check failed (in.dim() == 3 || in.dim() == 4): Expect input tensor to be 3-D or 4-D, but got, 5.
```

XNNPACK does not take 3-D or bfloat16 convolutions, so they run on the
portable convolution kernel, which only supported 1-D and 2-D. This
blocks vision encoders with a `Conv3d` patch embedding, such as Qwen2-VL
and Qwen2.5-VL. It is the failure in #11704.

## What this change does

The portable convolution kernel now runs 3-D convolutions, regular and
transposed. 1-D and 2-D convolutions run as 3-D ones with extra dims of
size 1 and add up their terms in the same order as before, so their
results stay the same (except that, with fused multiply-add, a result of
-0 can now be +0, as in PyTorch). The input and output may now have
different dim orders, because `torch.export` gives a `Conv3d` with a
channels-last input a contiguous output. The backend test suite no
longer skips 3-D convolutions.

It also fixes older bugs in the argument checks, which affect 1-D and
2-D too:

- A bias of the wrong length was read past its end (missing
parentheses), and a Bool bias crashed the kernel.
- `groups = 0` divided by zero, and a huge `groups` could overflow in
the bias check.
- A huge padding or dilation, or an output size too large for the 32-bit
tensor sizes, let a call that should fail run. Stride, padding and
dilation must now fit in 32 bits.
- Like PyTorch, the checks now reject a kernel dim of 0, a regular
convolution's kernel larger than the padded input, and an empty spatial
dim unless the batch or channel dim is empty too.

## Testing

- New tests cover grouped regular and transposed 3-D convolutions with
contiguous, channels-last and mixed layouts, and the checks above. All
but one fail without this change; that one checks that an empty batch or
channel dim still works.
- The portable kernel tests pass with AddressSanitizer and
UndefinedBehaviorSanitizer on Linux Arm64.
- The Cortex-M `conv3d` test, now with several channels, a 2x3x3 kernel,
stride and padding, no longer expects a failure and passes in CI on the
Arm simulator.
- A `Conv3d` patch embedding lowered with XNNPACK, and random
convolutions on the portable kernel, match eager PyTorch in float32 and
bfloat16.
- Speed against main on Arm64 and x86-64: regular 1-D and 2-D
convolutions are 1.5 to 13 times faster with GCC and with Clang at
`-Os`, and 18 percent faster to 11 percent slower with Clang at `-O2`
and `-O3`. Transposed ones are 5 percent faster to 17 percent slower.
- Code size for a Cortex-M55 (Arm GNU toolchain 15.2):
`op_convolution.o` grows from 32,484 to 33,263 bytes at `-Os` and from
48,477 to 61,457 at `-O3`. `kernel_ops_util.o`, which holds the argument
checks, grows from 15,746 to 17,743 bytes at `-Os` and from 25,932 to
28,979 at `-O3`.

## Notes

- Depends on #24380. Without it, a static int8 `Conv3d` lowered with
XNNPACK returns wrong values with no error, or fails to load, and the
backend test suite's static int8 XNNPACK `Conv3d` cases fail. Please
merge #24380 first or together with this change.
- An earlier version of 3-D support was reverted, see #19977.

Co-authored-by: Anthony Shoumikhin <anthony@shoumikh.in>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. module: kernels Issues related to kernel libraries and utilities, and code under kernels/ module: microcontrollers For embedded MCUs like Cortex-M, or RTOS like Zephyr, does not track NPU backend like Arm Ethos.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Conv3d with circular padding on portable fails at runtime

4 participants