Skip to content

viable/strict/1789065007

@malfet malfet tagged this 10 Sep 14:39
Similar to the pooling input validation unification (https://github.com/pytorch/pytorch/pull/196230), the six pad ops hand-wrote the same shape checks in a dozen places across CPU, CUDA and MPS, and the copies had drifted. Collect them in Padding.h next to the existing check_valid_input, parameterized by rank and by whether the caller is reflection padding, and route every backend through them. All three now report identical errors, matching the already-unified meta implementations in _meta_registrations.py, so eager and torch.compile agree too.

Consolidating turns up two bugs. Reflection's "output too small" guard used || where replication used &&, so a non-positive output dimension slipped through whenever another dimension was fine and F.pad reached the allocator instead of raising; MPS had its own broken spelling of the same guard, which for 1d never fired at all. Separately, the backward channel-dim check added for replication in #142834 was never applied to reflection, which still crashed on a mismatched gradOutput, and MPS validated the channel dim for neither family, so it aborted on a Metal assertion -- the @skipMPS on that regression test is gone. The shared backward helper checks it for both families, and the meta registrations gain the same check so the two do not diverge again.

Review Padding.h first for the new helpers, then the CPU call sites, then CUDA and MPS, which additionally drop dimension bookkeeping that existed only to feed the deleted checks.

This PR was authored with the assistance of Claude Code.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Pull Request resolved: https://github.com/pytorch/pytorch/pull/196457
Approved by: https://github.com/Skylion007
Assets 2
Loading