Skip to content

Fix complex div broadcasting in CPU kernels - #24409

Open
mshr-h wants to merge 1 commit into
pytorch:mainfrom
mshr-h:fix-complex-div-broadcasting
Open

mshr-h wants to merge 1 commit 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: 28 optimized CPU tests and 32 portable CPU tests passed.

Use broadcast-aware indexing for complex div in optimized and portable CPU kernels to avoid reading past broadcast operands. Require matching input and output complex dtypes, consistent with the existing complex arithmetic kernels.
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.

⚠️ 17 Awaiting Approval

As of commit 0922e6f with merge base 05092c1 (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 on lines +68 to +72
apply_binary_elementwise_fn<CTYPE, CTYPE, CTYPE>(
[](const CTYPE val_a, const CTYPE val_b) { return val_a / val_b; },
a,
b,
out);

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. 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.

2 participants