Skip to content

Support PyTorch slicing of complex tensors - #2939

Open
Karthik016996148 wants to merge 1 commit into
apple:mainfrom
Karthik016996148:fix/pytorch-complex-slice
Open

Karthik016996148 wants to merge 1 commit into
apple:mainfrom
Karthik016996148:fix/pytorch-complex-slice

Conversation

@Karthik016996148

Copy link
Copy Markdown

Fixes #2112.

PyTorch models can slice a complex intermediate, such as the output of an STFT, and return real-valued results. The converter currently passes that intermediate to MIL's slice_by_index, which rejects complex64 inputs.

This change reuses the existing slice rules for the output shape, then lowers the complex slice to two real-valued slices with the same bounds, masks, and strides. A full slice can pass the complex value directly to its next consumer.

The regression test slices a tensor with a flexible dimension and checks both its real and imaginary components against PyTorch. All three backend and precision combinations failed on the original code and passed with this change.

Testing

  • Ran TestSlice, TestComplex, and test_lower_complex_dialect_ops.py on Apple silicon with PyTorch 2.8.0. 263 passed, with three existing expected failures for [ExecuTorch] Cannot Use Dynamic Index to Select #2189.
  • Converted and ran a 32-sample STFT example on CPU using both ML Program and Neural Network. The maximum absolute difference from PyTorch was 4.77e-7 for both.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error when converting a PyTorch traced model to “neuralnetwork” or “mlprogram" format

1 participant