Skip to content

[XPU] Validate replication_pad2d_backward channel dimension - #4758

Merged
torchxpubot merged 3 commits into
mainfrom
agent/fix-replication-pad2d-channel-guard
Oct 13, 2026
Merged

torchxpubot merged 3 commits into
mainfrom
agent/fix-replication-pad2d-channel-guard

Conversation

@laifenxiawucha

@laifenxiawucha laifenxiawucha commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Background

XPU replication_pad2d_backward validates the spatial dimensions of grad_output but not its channel dimension. A mismatch can pass validation and lead to an invalid kernel read instead of a user-facing error.

Upstream

  • pytorch/pytorch#189463 adds the analogous channel validation to upstream CPU/CUDA padding code. XPU has a separate 2-D backward implementation, so it needs the guard here.

Fix

  • validate the grad_output channel count before launching the XPU kernel
  • preserve valid backward behavior
  • add mismatch, valid-input, and CPU-parity coverage

Before and after

Case Before After
Channel mismatch Accepted; may read out of bounds Raises gradOutput channel unexpected
Matching channels Works Still works and matches CPU

Testing

Linux XPU op_regression in CI run 34858341206: 3 passed, 0 failed.

Fixes #4754

@github-actions github-actions Bot added disable_e2e Disable all e2e test jobs for the PR disable_distributed Disable distributed UT test jobs for the PR disable_build Disable source code build for CI test, use nightly wheel labels Jul 30, 2026
@laifenxiawucha

Copy link
Copy Markdown
Contributor Author

Verified on Intel Data Center GPU Max 1100 (torch 2.14.0a0, source build).

Reproducer — mismatched channel now raises, valid case still works:

mismatch raised: gradOutput channel unexpected. Expected: 2, Got: 0
valid backward shape (2, 3, 4, 4): PASS

Regression test test/regressions/test_replication_pad_channel_mismatch.py:

test_replication_pad2d_backward_channel_mismatch_xpu ... ok
test_replication_pad2d_backward_matches_cpu_xpu ... ok
test_replication_pad2d_backward_valid_xpu ... ok
Ran 3 tests ... OK

@laifenxiawucha
laifenxiawucha force-pushed the agent/fix-replication-pad2d-channel-guard branch from 128a055 to e732647 Compare August 25, 2026 08:37
@laifenxiawucha laifenxiawucha removed the disable_build Disable source code build for CI test, use nightly wheel label Aug 25, 2026
@laifenxiawucha
laifenxiawucha force-pushed the agent/fix-replication-pad2d-channel-guard branch from e732647 to 3769b11 Compare August 25, 2026 08:40
@laifenxiawucha
laifenxiawucha force-pushed the agent/fix-replication-pad2d-channel-guard branch from 3769b11 to bfacd47 Compare August 31, 2026 03:13
jyz and others added 2 commits September 2, 2026 03:30
replication_pad2d_backward only validated the spatial (width/height)
dims of grad_output but not the channel/plane dim. A grad_output whose
channel count does not match the input (e.g. 0 channels) passed the
checks, a non-empty grad_input was allocated from the input shape, and
the kernel then read grad_output out of bounds -> SIGSEGV.

Add a channel-dim TORCH_CHECK mirroring the existing 3d backward check
and the CPU/CUDA fix in pytorch/pytorch#189463, plus a regression test.

Fixes #4754
@laifenxiawucha
laifenxiawucha force-pushed the agent/fix-replication-pad2d-channel-guard branch from bfacd47 to 9a7c25a Compare September 2, 2026 03:30
@ZhaoqiongZ

Copy link
Copy Markdown
Contributor

@torchxpubot merge

@torchxpubot
torchxpubot merged commit 692ddf7 into main Oct 13, 2026
20 checks passed
@torchxpubot

Copy link
Copy Markdown
Contributor

PR has been successfully merged by @ZhaoqiongZ.

@torchxpubot
torchxpubot deleted the agent/fix-replication-pad2d-channel-guard branch October 13, 2026 05:56
minmingzhu pushed a commit to minmingzhu/torch-xpu-ops that referenced this pull request Oct 28, 2026
## Background

XPU `replication_pad2d_backward` validates the spatial dimensions of
`grad_output` but not its channel dimension. A mismatch can pass
validation and lead to an invalid kernel read instead of a user-facing
error.

## Upstream

-
[pytorch/pytorch#189463](pytorch/pytorch#189463)
adds the analogous channel validation to upstream CPU/CUDA padding code.
XPU has a separate 2-D backward implementation, so it needs the guard
here.

## Fix

- validate the `grad_output` channel count before launching the XPU
kernel
- preserve valid backward behavior
- add mismatch, valid-input, and CPU-parity coverage

## Before and after

| Case | Before | After |
|---|---|---|
| Channel mismatch | Accepted; may read out of bounds | Raises
`gradOutput channel unexpected` |
| Matching channels | Works | Still works and matches CPU |

## Testing

Linux XPU `op_regression` in [CI run
34858341206](https://github.com/intel/torch-xpu-ops/actions/runs/34858341206):
3 passed, 0 failed.

Fixes intel#4754

---------

Co-authored-by: jyz <jyz@intel.com>
Co-authored-by: laifenxiawucha <184592536+laifenxiawucha@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

disable_distributed Disable distributed UT test jobs for the PR disable_e2e Disable all e2e test jobs for the PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[xpu-aglinment] Validate replication_pad2d_backward channel mismatch

4 participants