Skip to content

Convert between complex widths in portable _to_dim_order_copy - #25903

Open
AxelNoun wants to merge 1 commit into
pytorch:mainfrom
AxelNoun:fix/et-t04-1-complex-to-complex-copy
Open

AxelNoun wants to merge 1 commit into
pytorch:mainfrom
AxelNoun:fix/et-t04-1-complex-to-complex-copy

Conversation

@AxelNoun

@AxelNoun AxelNoun commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

When both the input and the output were complex, _to_dim_order_copy picked the C++ type from the input only, so complex64 to complex128 wrote wrong values and complex128 to complex64 wrote past the end of the output buffer (reproduction in #25902). This switches on both complex types and converts the real and imaginary parts separately, since c10::complex<Half> cannot be constructed from c10::complex<double>.

Fixes #25902

Test plan

ComplexFloatToComplexDouble and ComplexDoubleToComplexFloat in kernels/test/op__to_dim_order_copy_test.cpp. Before the change the first one got wrong values and the second one aborted with double free or corruption. This test file is excluded from the CMake build (kernels/test/CMakeLists.txt, "requires rtti"), so it does not run in the CMake CI; I ran it by enabling it locally:

./cmake-out/kernels/test/portable_kernels_test --gtest_filter='OpToDimOrderCopyTest.*'   # 18 tests passed
ctest --output-on-failure                                                                # 90/90 passed

From Python, complex64 to complex128 and complex128 to complex64 (4 and 4096 elements) now give the same values as eager. ComplexHalf pairs are not exported by to_edge (mismatched dtypes) and no test builds a ComplexHalf tensor, so I checked complex128 to ComplexHalf and back with a local probe only: same values as eager, including overflow to inf.

This PR was authored with the help of Claude Code (Anthropic); I reviewed and tested it.

cc @nil-is-all

When both the input and the output were complex, the kernel picked the C++ type from the input only, so complex64 to complex128 wrote wrong values and complex128 to complex64 wrote past the end of the output buffer. Switch on both complex types and convert the real and imaginary parts separately.
@pytorch-bot

pytorch-bot Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

⚠️ 18 Awaiting Approval

As of commit e6f3790 with merge base f748232 (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 Dec 1, 2026
@AxelNoun

AxelNoun commented Dec 1, 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 Dec 1, 2026
@executorch-triage executorch-triage Bot added the community: contribution PRs coming from community (excluding hardware partners) label Dec 1, 2026

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.

_to_dim_order_copy between complex64 and complex128 gives wrong values or corrupts the heap

1 participant