Skip to content

Fix quantized Conv1d same padding with even kernels in XNNPACK#20734

Open
SakshamKapoor2911 wants to merge 2 commits into
pytorch:mainfrom
SakshamKapoor2911:sk/fix-xnnpack-conv1d-same-padding
Open

Fix quantized Conv1d same padding with even kernels in XNNPACK#20734
SakshamKapoor2911 wants to merge 2 commits into
pytorch:mainfrom
SakshamKapoor2911:sk/fix-xnnpack-conv1d-same-padding

Conversation

@SakshamKapoor2911

Copy link
Copy Markdown

Fixes #20558.
Related to #20553.

Summary

Quantized nn.Conv1d(..., padding="same") with an even kernel exports as:

dequant -> constant_pad_nd -> convolution

XNNPACK's Conv1d lowering maps Conv1d through Conv2d by inserting an extra width dimension. For even-kernel same-padding, the explicit temporal pad has to be represented as asymmetric Conv2d input padding after that 1d-to-2d mapping. Otherwise the Conv1d case either remains partially undelegated or reaches XNNPACK with mismatched output metadata and fails/produces incorrect output.

This PR:

  • pulls temporal-only zero constant_pad_nd into the quantized Conv1d partition,
  • folds that temporal pad into XNNPACK asymmetric Conv2d input padding during Conv1d unsqueeze lowering,
  • serializes folded asymmetric padding when present,
  • repairs Conv1d folded-pad metadata after later XNNPACK passes retrace from the symmetric ATen convolution args,
  • adds a regression for quantized Conv1d even-kernel padding="same".

Relationship to #20553

#20553 folds constant_pad_nd into convolution input padding for the Conv2d path. I tested its current head (a04093bba2766afe609ef325737effdd01c3c4ad) against the Conv1d repro from #20558. The odd-kernel case works, but the even-kernel Conv1d case still fails at runtime with xnn_status_invalid_parameter.

This PR handles the Conv1d-specific path where the temporal dimension is later mapped to Conv2d height and where metadata must be corrected after XNNPACK passes retrace from symmetric ATen conv args.

I noticed #20558 is assigned to @JakeStevens; happy to adapt or defer if there is already an internal Conv1d fix in flight.

Test plan

Local note: this checkout does not expose src/executorch/exir/_serialize/program.fbs as a package resource, so to_executorch() pytest runs used a temporary sitecustomize.py shim that redirects missing FlatBuffer resource reads to repo schema/. The manual repro uses the same schema-resource workaround. CI/dev install should package these resources normally.

Manual quantized Conv1d repro on patched main:

kernel_size=3: max abs diff = 0.000000
kernel_size=4: max abs diff = 0.000000

Additional manual quantized Conv1d same-padding matrix:

seed=0 length=16 k=2 dilation=1 bias=True:  max_abs_diff=0.000000 shape=(1,4,16)
seed=1 length=16 k=4 dilation=1 bias=True:  max_abs_diff=0.000000 shape=(1,4,16)
seed=2 length=15 k=4 dilation=1 bias=False: max_abs_diff=0.000000 shape=(1,4,15)
seed=3 length=17 k=6 dilation=1 bias=True:  max_abs_diff=0.000000 shape=(1,4,17)
seed=4 length=16 k=4 dilation=3 bias=True:  max_abs_diff=0.000000 shape=(1,4,16)

Commands run:

PYTHONPATH=/tmp/executorch20558_site:src python -m pytest backends/xnnpack/test/ops/test_conv1d.py::TestConv1d::test_qs8_conv1d_even_kernel_same_padding -q
# 1 passed

PYTHONPATH=/tmp/executorch20558_site:src python -m pytest backends/xnnpack/test/ops/test_conv1d.py -q
# 7 passed

PYTHONPATH=/tmp/executorch20558_site:src python -m pytest backends/xnnpack/test/ops/test_conv2d.py -q
# 28 passed

PYTHONPATH=/tmp/executorch20558_site:src python -m pytest backends/xnnpack/test/ops/test_static_constant_pad.py -q
# 8 passed

python -m py_compile \
  backends/xnnpack/_passes/conv1d_unsqueeze_pass.py \
  backends/xnnpack/operators/op_conv2d.py \
  backends/xnnpack/partition/config/gemm_configs.py \
  backends/xnnpack/test/ops/test_conv1d.py

python -m black --check \
  backends/xnnpack/_passes/__init__.py \
  backends/xnnpack/_passes/conv1d_unsqueeze_pass.py \
  backends/xnnpack/operators/op_conv2d.py \
  backends/xnnpack/partition/config/gemm_configs.py \
  backends/xnnpack/test/ops/test_conv1d.py

python -m flake8 \
  backends/xnnpack/_passes/__init__.py \
  backends/xnnpack/_passes/conv1d_unsqueeze_pass.py \
  backends/xnnpack/operators/op_conv2d.py \
  backends/xnnpack/partition/config/gemm_configs.py \
  backends/xnnpack/test/ops/test_conv1d.py

python -m ufmt check \
  backends/xnnpack/_passes/__init__.py \
  backends/xnnpack/_passes/conv1d_unsqueeze_pass.py \
  backends/xnnpack/operators/op_conv2d.py \
  backends/xnnpack/partition/config/gemm_configs.py \
  backends/xnnpack/test/ops/test_conv1d.py

git diff --check

Copilot AI review requested due to automatic review settings July 5, 2026 12:35
@pytorch-bot

pytorch-bot Bot commented Jul 5, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 11 Awaiting Approval

As of commit 529a792 with merge base 4af91c3 (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

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 Jul 5, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jul 5, 2026

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: SakshamKapoor2911 / name: SakshamKapoor2911 (e7636f5)

Copilot AI left a comment

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.

Pull request overview

This PR fixes an XNNPACK delegation/runtime correctness issue for statically-quantized nn.Conv1d(padding="same") with even kernel sizes by folding the explicit temporal constant_pad_nd into asymmetric Conv2d padding during Conv1d→Conv2d lowering, and ensuring the serialized graph’s metadata matches the folded asymmetric padding.

Changes:

  • Extend XNNPACK partitioning and Conv1d unsqueeze lowering to absorb temporal-only zero constant_pad_nd into the quantized Conv1d partition and fold it into asymmetric xnnpack_input_padding.
  • Teach Conv2d serialization to use folded asymmetric padding metadata when present, and add a late pass to repair Conv1d folded-pad tensor metadata after retracing passes.
  • Add a regression test covering quantized Conv1d even-kernel padding="same".

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
backends/xnnpack/test/ops/test_conv1d.py Adds a regression test for quantized Conv1d even-kernel padding="same" and factors out calibration sample generation.
backends/xnnpack/partition/config/gemm_configs.py Extends convolution activation-dependency discovery to include temporal-only zero constant_pad_nd for quantized Conv1d so it can be partitioned/delegated.
backends/xnnpack/operators/op_conv2d.py Uses folded asymmetric xnnpack_input_padding metadata (top/right/bottom/left) when present instead of symmetric ATen padding args.
backends/xnnpack/_passes/conv1d_unsqueeze_pass.py Folds eligible temporal-only constant_pad_nd into asymmetric input padding during Conv1d→Conv2d lowering and adds a pass to restore correct tensor metadata for folded-pad Conv1d.
backends/xnnpack/_passes/init.py Registers the new folded-pad metadata repair pass in the default XNNPACK pass pipeline.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread backends/xnnpack/_passes/conv1d_unsqueeze_pass.py
@pytorch-bot

pytorch-bot Bot commented Jul 5, 2026

Copy link
Copy Markdown

❌ 🤖 pytorchbot command failed:

Got EOF while in a quoted string```
Try `@pytorchbot --help` for more info.

@SakshamKapoor2911

Copy link
Copy Markdown
Author

@pytorchbot label "release notes: xnnpack"

@pytorch-bot pytorch-bot Bot added the release notes: xnnpack Changes to the XNNPack backend delegate label Jul 5, 2026
@digantdesai digantdesai requested a review from JakeStevens July 7, 2026 16:19
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. release notes: xnnpack Changes to the XNNPack backend delegate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quantized Conv1d with even kernel + padding="same" produces incorrect XNNPACK output

3 participants