Skip to content

WIP: Fix complex div broadcasting in CPU kernels - #24409

Draft
mshr-h wants to merge 4 commits into
pytorch:mainfrom
mshr-h:fix-complex-div-broadcasting
Draft

mshr-h wants to merge 4 commits into
pytorch:mainfrom
mshr-h:fix-complex-div-broadcasting

Conversation

@mshr-h

@mshr-h mshr-h commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Refs #24408

The optimized and portable complex div kernels index both inputs using the output index. When an operand is broadcast, this can read past its buffer and produce incorrect results.

Use apply_binary_elementwise_fn to handle broadcasting, and require matching input/output dtypes before accessing typed data.

Add regression tests for complex64/128 covering scalar operands on either side, multidimensional broadcasting, empty tensors, and mismatched dtypes.

Test plan

Run the div tests in both CPU configurations:

cmake-out/kernels/test/optimized_kernels_test --gtest_filter='OpDiv*'
cmake-out/kernels/test/portable_kernels_test --gtest_filter='OpDiv*'

Result: 34 optimized CPU tests and 38 portable CPU tests passed.

Copilot AI balanced review requested due to automatic review settings November 2, 2026 01:53
@pytorch-bot

pytorch-bot Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 27 Awaiting Approval

As of commit 135c5b6 with merge base b3b565b (image):

AWAITING APPROVAL - The following workflows need approval before CI can run:

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 Nov 2, 2026
@mshr-h

mshr-h commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot label "release notes: ops & kernels"

@pytorch-bot pytorch-bot Bot added the release notes: ops & kernels Changes to the opset and any new / changed kernel implementations label Nov 2, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The portable and optimized division helpers write broadcast results sequentially rather than using output strides, permuting results for channels-last tensors and requiring a stride-aware fix with regression coverage.

1 open issue
What changed in this PR

This PR addresses the complex division broadcasting bug from #24408 in ExecuTorch’s portable and optimized CPU kernels.

Changes:

  • Replace direct input indexing with the shared binary elementwise broadcasting helper.
  • Require matching complex input and output dtypes before typed data access.
  • Add complex64/128 regression tests for scalar and multidimensional broadcasting, empty tensors, and dtype mismatches.
File Description
kernels/​test/​op_div_test.cpp Adds complex broadcasting and dtype-rejection tests.
kernels/​portable/​cpu/​op_div.cpp Uses the broadcasting helper and validates complex dtypes.
kernels/​optimized/​cpu/​op_div.cpp Applies the same changes to optimized complex division.

Review effort: Balanced

Comment thread kernels/portable/cpu/op_div.cpp Resolved
Copilot AI balanced review requested due to automatic review settings November 2, 2026 13:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the complex-division broadcasting and dtype-validation changes.

1 open issue

Review effort: Balanced

@executorch-triage executorch-triage Bot added the community: contribution PRs coming from community (excluding hardware partners) label Nov 2, 2026
@mshr-h
mshr-h force-pushed the fix-complex-div-broadcasting branch from 614b52b to 3d24ca9 Compare November 2, 2026 22:01
Copilot AI balanced review requested due to automatic review settings November 2, 2026 22:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain for the complex-division broadcasting changes.

1 open issue

Review effort: Balanced

@mshr-h
mshr-h force-pushed the fix-complex-div-broadcasting branch from 3d24ca9 to 01f6068 Compare November 3, 2026 06:26
Copilot AI balanced review requested due to automatic review settings November 3, 2026 06:26

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are recorded for the complex-division broadcasting changes.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@mshr-h
mshr-h force-pushed the fix-complex-div-broadcasting branch from 01f6068 to acadf9b Compare November 3, 2026 11:04
Copilot AI balanced review requested due to automatic review settings November 3, 2026 11:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block the complex-division broadcasting changes.

No open issues 🎉

Review effort: Balanced

@mshr-h
mshr-h force-pushed the fix-complex-div-broadcasting branch from acadf9b to 9241ec6 Compare November 3, 2026 13:53
Copilot AI balanced review requested due to automatic review settings November 3, 2026 13:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The stride-ignoring fast path remains enabled for mixed-rank channels-last tensors, causing both division kernels to return incorrectly ordered results.

1 open issue

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 3, 2026 14:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The shared helper unconditionally disables the no-broadcast fast path, adding unnecessary axis traversal for equal-shaped contiguous operands and losing sequential memory access for equal-shaped channels-last tensors.

1 open issue
1 resolved since last review

Review effort: Balanced

@mshr-h
mshr-h marked this pull request as draft November 3, 2026 14:27
@mshr-h
mshr-h force-pushed the fix-complex-div-broadcasting branch from 050b241 to fb256ec Compare November 3, 2026 15:56
@mshr-h mshr-h changed the title Fix complex div broadcasting in CPU kernels WIP: Fix complex div broadcasting in CPU kernels Nov 3, 2026
@JakeStevens

Copy link
Copy Markdown
Contributor

@mshr-h is this ready for review? it is marked draft and WIP

@mshr-h

mshr-h commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

@JakeStevens thanks for checking. it's not ready for review. i'll ping you when it's ready.

@mshr-h
mshr-h force-pushed the fix-complex-div-broadcasting branch from fb256ec to 21a37dd Compare November 15, 2026 04:48
Use broadcast-aware indexing for complex div in optimized and portable CPU
kernels, and require matching complex input and output dtypes. Respect
output strides while preserving linear iteration for matching dense layouts.
Cover scalar and channels-last broadcasting, mixed ranks, empty tensors,
bounded output resizing, and invalid inputs.

Refs pytorch#24408
@mshr-h
mshr-h force-pushed the fix-complex-div-broadcasting branch from 21a37dd to 3e058da Compare November 15, 2026 04:54
@mshr-h
mshr-h requested a balanced review from Copilot November 15, 2026 04:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The traversal change needs channels-last masked_scatter regressions to verify sequential source-to-mask placement in both traversal paths and insufficient-source failure.

2 open issues

Review effort: Balanced

Consume source values in logical output order for channels-last tensors
while preserving the default-layout fast path.

Add regression tests for matching shapes, mixed ranks, mask broadcasting,
and insufficient sources. Include masked_scatter tests in the CMake suite.
@mshr-h
mshr-h force-pushed the fix-complex-div-broadcasting branch from 3e058da to 06aa91e Compare November 15, 2026 08:11
@mshr-h
mshr-h requested a max review from Copilot November 15, 2026 08:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new masked_scatter path can produce incorrect results for channels-last sources because it consumes source elements in physical rather than logical order.

2 open issues
2 resolved since last review

Review effort: Max

Comment thread kernels/test/op_div_test.cpp Resolved
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new source-stride remapping lacks channels-last source coverage, leaving incorrect physical-order consumption undetected by the existing tests.

1 open issue
2 resolved since last review

Review effort: Max

Comment thread kernels/test/op_masked_scatter_test.cpp Resolved
…rage for masked scatter stride remapping'

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Human assessment is needed because an unpublished finding identifies a complex-scalar/channels-last compatibility gap in both division implementations.

No open issues 🎉

1 resolved since last review

Review effort: Max

This branch has not been deployed

No deployments
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. community: contribution PRs coming from community (excluding hardware partners) release notes: ops & kernels Changes to the opset and any new / changed kernel implementations

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants