Skip to content

[v2] Preserve channels_last memory format in pad (#9560) - #9638

Open
FinalSunFlower wants to merge 1 commit into
pytorch:mainfrom
FinalSunFlower:fix/pad-preserve-channels-last-9560
Open

[v2] Preserve channels_last memory format in pad (#9560)#9638
FinalSunFlower wants to merge 1 commit into
pytorch:mainfrom
FinalSunFlower:fix/pad-preserve-channels-last-9560

Conversation

@FinalSunFlower

Copy link
Copy Markdown

Fixes #9560

Summary

  • Preserve an unambiguous channels-last layout through v2 image padding, including scalar and vector fills.
  • Restore channels-last before reshaping back to the original image dimensions.
  • Add CPU regression coverage for constant, edge, reflect, symmetric, and vector-fill padding through both F.pad and F.pad_image.

Tests

  • TestPad: 202 passed, 128 skipped, 18 xfailed.
  • New channels-last regression: 10 passed.
  • git diff --check: passed.

Full-suite execution is affected by local environment limitations (missing JPEG extension support, pytest-mock, and TorchScript source inspection under the command wrapper).

@pytorch-bot

pytorch-bot Bot commented Aug 29, 2026

Copy link
Copy Markdown

🔗 Helpful Links

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

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

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

@meta-cla

meta-cla Bot commented Aug 29, 2026

Copy link
Copy Markdown

Hi @FinalSunFlower!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla meta-cla Bot added the cla signed label Aug 29, 2026
@meta-cla

meta-cla Bot commented Aug 29, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@fallenmi fallenmi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Reviewed exact head 3e4131cf53a34968123775f519fc0fd209102d80 against exact base ac8d215f7d45d6601451b62e9f81622dac8aa0b4.

The preservation point looks correct: the code detects an unambiguous channels-last layout after collapsing arbitrary leading dimensions to the kernel's 4D view, then restores that layout after the selected padding implementation. The vector-fill path retains it while applying the per-channel border values.

I exercised 228 CPU cases across 3D, 4D, and 5D image shapes; float32 and uint8; contiguous, strict channels-last, and row-padded channels-last-like inputs; scalar and vector fills; constant, edge, reflect, and symmetric modes; and positive and negative padding. Exact base, head, and the current GitHub merge produced byte-identical values in every case. All 66 unambiguous channels-last cases retain channels-last output on head and merge, while base loses it in 32. Scalar- and vector-fill autograd outputs and gradients also match base byte-for-byte.

The complete TestPad slice passes on all three revisions: base 192 passed / 128 skipped / 18 xfailed; head and merge 202 passed / 128 skipped / 18 xfailed. git diff --check is clean, and the current merge tree is byte-identical to the reviewed head.

GitHub currently exposes only the green CLA/import/internal-only checks rather than the normal public test matrix; the exact local coverage above is therefore the basis of this approval. I found no blocking issue.

AI disclosure: I used OpenAI Codex to inspect the exact revisions and policy, construct and run the layout/value/autograd matrix, execute the focused tests, and draft this review. I verified the results and conclusion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

torchvision.transforms.v2.functional.pad should have maintained the channels_last format of the input

2 participants