Skip to content

Fix NHWC im2row output layout in HiFi backend to match Python reference#19681

Open
wl1026sun wants to merge 1 commit into
pytorch:mainfrom
wl1026sun:export-D96846886
Open

Fix NHWC im2row output layout in HiFi backend to match Python reference#19681
wl1026sun wants to merge 1 commit into
pytorch:mainfrom
wl1026sun:export-D96846886

Conversation

@wl1026sun
Copy link
Copy Markdown
Contributor

Summary:
The Cadence im2row operator's NHWC code paths in the HiFi backend produced
an output layout inconsistent with the operator's Python reference
implementation and with its own NCHW counterpart in the same file.

Contract (channel-major, [c][kp] per output position):
ref_implementations.py defines cadence::im2row via
torch.nn.functional.unfold, producing (N, num_patches, C*kH*kW) with
channel as the OUTER index. The HiFi NCHW runtime already matched this.

Pre-fix bug (kp-major, [kp][c]):

  • HiFi NHWC template path in op_im2row_out.cpp (used for float inputs).
  • HiFi NHWC SIMD path in xa_nn_im2row.c (used for int8/uint8 inputs via
    xa_nn_im2row_quantized).

Fix:

  1. op_im2row_out.cpp — rewrite NHWC branch to write [c][kp].
  2. xa_nn_im2row.c — rewrite NHWC branch in the SIMD path to write [c][kp].
  3. replace_ops.py — add a channel_last=True guard in the AOT pass that
    elides cadence::im2row to view_copy. The elision was valid only under
    the pre-fix [kp][c] semantics (where NHWC im2row could be expressed as
    a no-op view of NHWC input); under the corrected layout, NHWC im2row
    actually rearranges data, so the elision would drop a required
    computation.

Differential Revision: D96846886

Summary:
The Cadence `im2row` operator's NHWC code paths in the HiFi backend produced
an output layout inconsistent with the operator's Python reference
implementation and with its own NCHW counterpart in the same file.

**Contract (channel-major, `[c][kp]` per output position):**
`ref_implementations.py` defines `cadence::im2row` via
`torch.nn.functional.unfold`, producing `(N, num_patches, C*kH*kW)` with
channel as the OUTER index. The HiFi NCHW runtime already matched this.

**Pre-fix bug (kp-major, `[kp][c]`):**
- HiFi NHWC template path in `op_im2row_out.cpp` (used for float inputs).
- HiFi NHWC SIMD path in `xa_nn_im2row.c` (used for int8/uint8 inputs via
  `xa_nn_im2row_quantized`).

**Fix:**
1. `op_im2row_out.cpp` — rewrite NHWC branch to write `[c][kp]`.
2. `xa_nn_im2row.c` — rewrite NHWC branch in the SIMD path to write `[c][kp]`.
3. `replace_ops.py` — add a `channel_last=True` guard in the AOT pass that
   elides `cadence::im2row` to `view_copy`. The elision was valid only under
   the pre-fix `[kp][c]` semantics (where NHWC im2row could be expressed as
   a no-op view of NHWC input); under the corrected layout, NHWC im2row
   actually rearranges data, so the elision would drop a required
   computation.

Differential Revision: D96846886
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot Bot commented May 19, 2026

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ You can merge normally! (1 Unrelated Failure)

As of commit 78595aa with merge base 3d86cc7 (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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 May 19, 2026
@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync Bot commented May 19, 2026

@wl1026sun has exported this pull request. If you are a Meta employee, you can view the originating Diff in D96846886.

@github-actions
Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

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. fb-exported meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant