Skip to content

Fix avg_pool2d CUDA backward for channels_last inputs with padding - #188345

Closed
Sunt-ing wants to merge 2 commits into
pytorch:mainfrom
Sunt-ing:30
Closed

Sunt-ing wants to merge 2 commits into
pytorch:mainfrom
Sunt-ing:30

Conversation

@Sunt-ing

@Sunt-ing Sunt-ing commented Jun 27, 2026 •

Copy link
Copy Markdown
Contributor

Issue

Fixes #188344

Summary

The channels_last (NHWC) CUDA backward kernel avg_pool2d_backward_out_cuda_frame_nhwc recovered the input pixel coordinates from the flat index without the padding offset (const int w = (index / channels) % width;), while the phstart/phend/pwstart/pwend window formulas it then uses require padded coordinates. The contiguous (NCHW) kernel avg_pool2d_backward_out_cuda_frame adds the offset (+ pad_w / + pad_h); the NHWC kernel did not, so for padding > 0 it mapped each input pixel to the wrong set of output windows and returned silently wrong input gradients. padding = 0 was unaffected because the two coordinate systems coincide. This adds the missing + pad_w / + pad_h, matching the NCHW kernel.

test_avg_pool2d_nhwc did not catch this because its helper accepted a padding argument but never forwarded it to AvgPool2d (nor into the grad-output shape), so its padding=1 / padding=2 cases actually ran with padding=0. This PR forwards padding so those cases exercise the path, fixes the one call whose padding=2, kernel_size=3 combination is invalid once padding is applied (pad must be <= kernel_size / 2), and adds a count_include_pad=True padded case. The repaired test fails on the current kernel and passes with the fix.

Checklist

  • Passes lint (spin fixlint)
  • Added/updated tests
  • Updated documentation (if applicable)
  • Included benchmark results (for PRs impacting perf)

BC-breaking?

No. This corrects silently-wrong gradients; only incorrect outputs change.

The channels_last (NHWC) CUDA backward kernel recovered the input pixel
coordinates without the padding offset, while its window formulas require
padded coordinates (the NCHW kernel adds the offset). For padding > 0 this
returned silently wrong input gradients. Add the missing pad offsets and
repair test_avg_pool2d_nhwc, which never forwarded its padding argument.

Fixes pytorch#188344

Signed-off-by: Ting Sun <[email protected]>
@pytorch-bot

pytorch-bot Bot commented Jun 27, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure

As of commit 5013831 with merge base 7649a5b (image):

NEW FAILURE - The following job has failed:

  • pull / linux-jammy-aarch64-py3.10 / build-osdc (gh)
    Executing the custom container implementation failed. Please contact your self hosted runner administrator.

    AI verdict: AI verdict: garbage

    The failure is a self-hosted runner container infrastructure error ("Executing the custom container implementation failed"), completely unrelated to the code change which only modifies Python test xfail markers.

    Full reasoning on HUD →

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

@linux-foundation-easycla

linux-foundation-easycla Bot commented Jun 27, 2026 •

Copy link
Copy Markdown

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

  • ✅ login: Sunt-ing / name: Ting Sun (0138494)

@Skylion007

Copy link
Copy Markdown
Collaborator

Looks like the CPU kernel is broken too

@Skylion007

Copy link
Copy Markdown
Collaborator

RuntimeError: pad should be at most half of effective kernel size, but got pad=2, kernel_size=3 and dilation=1

@Skylion007

Copy link
Copy Markdown
Collaborator

Yeah these test cases do not work with CPU style checks.

@soulitzer soulitzer added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jun 29, 2026
@Sunt-ing

Copy link
Copy Markdown
Contributor Author

#188494 landed the same fix and closed #188344, so closing this as superseded

@Sunt-ing Sunt-ing closed this Jun 30, 2026
@eqy

eqy commented Jun 30, 2026 •

Copy link
Copy Markdown
Collaborator

@Sunt-ing sorry I didn't see this, do you want to try landing yours since my change was reverted? You just need to remove some xfails

@eqy eqy reopened this Jun 30, 2026
@Sunt-ing

Sunt-ing commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @eqy. I removed the stale AvgPool2d CUDA test_memory_format expectedFailure and fixed the remaining invalid padding=2, kernel_size=3 test case.

I also rebuilt the PR branch on an RTX 4090 and ran:

CUDA_VISIBLE_DEVICES= python test/nn/test_pooling.py TestPoolingNNDeviceTypeCPU.test_avg_pool2d_nhwc_cpu_float32
python test/nn/test_pooling.py TestPoolingNNDeviceTypeCUDA.test_avg_pool2d_nhwc_cuda_float32
python test/test_modules.py TestModuleCUDA.test_memory_format_nn_AvgPool2d_cuda_float32

All three passed.

@Sunt-ing

Sunt-ing commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: nn"

@pytorch-bot pytorch-bot Bot added the release notes: nn release notes category label Jul 1, 2026
@eqy

eqy commented Jul 1, 2026

Copy link
Copy Markdown
Collaborator

@pytorchmergebot merge

@pytorch-bot

pytorch-bot Bot commented Jul 1, 2026

Copy link
Copy Markdown

This PR needs to be approved by an authorized maintainer before merge.

@eqy

eqy commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator

@pytorchmergebot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Jul 3, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 mandatory check(s) failed. The first few are:

Dig deeper by viewing the failures on hud

Details for Dev Infra team Raised by workflow job

Failing merge rule: Core Maintainers

@eqy

eqy commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

@pytorchmergebot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 1 checks: pull / linux-jammy-aarch64-py3.10 / build-osdc

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

vishalgoyal316 pushed a commit to vishalgoyal316/pytorch that referenced this pull request Jul 16, 2026
…ytorch#188345)

## Issue

Fixes pytorch#188344

## Summary

The channels_last (NHWC) CUDA backward kernel `avg_pool2d_backward_out_cuda_frame_nhwc` recovered the input pixel coordinates from the flat index without the padding offset (`const int w = (index / channels) % width;`), while the `phstart/phend/pwstart/pwend` window formulas it then uses require padded coordinates. The contiguous (NCHW) kernel `avg_pool2d_backward_out_cuda_frame` adds the offset (`+ pad_w` / `+ pad_h`); the NHWC kernel did not, so for `padding > 0` it mapped each input pixel to the wrong set of output windows and returned silently wrong input gradients. `padding = 0` was unaffected because the two coordinate systems coincide. This adds the missing `+ pad_w` / `+ pad_h`, matching the NCHW kernel.

`test_avg_pool2d_nhwc` did not catch this because its `helper` accepted a `padding` argument but never forwarded it to `AvgPool2d` (nor into the grad-output shape), so its `padding=1` / `padding=2` cases actually ran with `padding=0`. This PR forwards `padding` so those cases exercise the path, fixes the one call whose `padding=2, kernel_size=3` combination is invalid once padding is applied (`pad` must be `<= kernel_size / 2`), and adds a `count_include_pad=True` padded case. The repaired test fails on the current kernel and passes with the fix.

## Checklist

- [x] Passes lint (`spin fixlint`)
- [x] Added/updated tests
- [ ] Updated documentation (if applicable)
- [ ] Included benchmark results (for PRs impacting perf)

## BC-breaking?

No. This corrects silently-wrong gradients; only incorrect outputs change.

Pull Request resolved: pytorch#188345
Approved by: https://github.com/eqy
DamJanusz pushed a commit to DamJanusz/pytorch that referenced this pull request Jul 21, 2026
…ytorch#188345)

## Issue

Fixes pytorch#188344

## Summary

The channels_last (NHWC) CUDA backward kernel `avg_pool2d_backward_out_cuda_frame_nhwc` recovered the input pixel coordinates from the flat index without the padding offset (`const int w = (index / channels) % width;`), while the `phstart/phend/pwstart/pwend` window formulas it then uses require padded coordinates. The contiguous (NCHW) kernel `avg_pool2d_backward_out_cuda_frame` adds the offset (`+ pad_w` / `+ pad_h`); the NHWC kernel did not, so for `padding > 0` it mapped each input pixel to the wrong set of output windows and returned silently wrong input gradients. `padding = 0` was unaffected because the two coordinate systems coincide. This adds the missing `+ pad_w` / `+ pad_h`, matching the NCHW kernel.

`test_avg_pool2d_nhwc` did not catch this because its `helper` accepted a `padding` argument but never forwarded it to `AvgPool2d` (nor into the grad-output shape), so its `padding=1` / `padding=2` cases actually ran with `padding=0`. This PR forwards `padding` so those cases exercise the path, fixes the one call whose `padding=2, kernel_size=3` combination is invalid once padding is applied (`pad` must be `<= kernel_size / 2`), and adds a `count_include_pad=True` padded case. The repaired test fails on the current kernel and passes with the fix.

## Checklist

- [x] Passes lint (`spin fixlint`)
- [x] Added/updated tests
- [ ] Updated documentation (if applicable)
- [ ] Included benchmark results (for PRs impacting perf)

## BC-breaking?

No. This corrects silently-wrong gradients; only incorrect outputs change.

Pull Request resolved: pytorch#188345
Approved by: https://github.com/eqy
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…ytorch#188345)

## Issue

Fixes pytorch#188344

## Summary

The channels_last (NHWC) CUDA backward kernel `avg_pool2d_backward_out_cuda_frame_nhwc` recovered the input pixel coordinates from the flat index without the padding offset (`const int w = (index / channels) % width;`), while the `phstart/phend/pwstart/pwend` window formulas it then uses require padded coordinates. The contiguous (NCHW) kernel `avg_pool2d_backward_out_cuda_frame` adds the offset (`+ pad_w` / `+ pad_h`); the NHWC kernel did not, so for `padding > 0` it mapped each input pixel to the wrong set of output windows and returned silently wrong input gradients. `padding = 0` was unaffected because the two coordinate systems coincide. This adds the missing `+ pad_w` / `+ pad_h`, matching the NCHW kernel.

`test_avg_pool2d_nhwc` did not catch this because its `helper` accepted a `padding` argument but never forwarded it to `AvgPool2d` (nor into the grad-output shape), so its `padding=1` / `padding=2` cases actually ran with `padding=0`. This PR forwards `padding` so those cases exercise the path, fixes the one call whose `padding=2, kernel_size=3` combination is invalid once padding is applied (`pad` must be `<= kernel_size / 2`), and adds a `count_include_pad=True` padded case. The repaired test fails on the current kernel and passes with the fix.

## Checklist

- [x] Passes lint (`spin fixlint`)
- [x] Added/updated tests
- [ ] Updated documentation (if applicable)
- [ ] Included benchmark results (for PRs impacting perf)

## BC-breaking?

No. This corrects silently-wrong gradients; only incorrect outputs change.

Pull Request resolved: pytorch#188345
Approved by: https://github.com/eqy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request Merged open source release notes: nn release notes category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

avg_pool2d backward returns wrong input gradients on CUDA for channels_last inputs when padding > 0

6 participants