pytorch - ✅(Solved) Fix [MPS] torch.conj(x).imag returns un-conjugated imag on conjugate view (CPU correct) [1 pull requests, 1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
pytorch/pytorch#184379Fetched 2026-05-20 03:39:00
View on GitHub
Comments
1
Participants
2
Timeline
32
Reactions
0
Author
Participants
Assignees
Timeline (top)
mentioned ×11subscribed ×11labeled ×6assigned ×1

Fix Action

Fixed

PR fix notes

PR #184403: [MPS] Materialize neg bit in copy_kernel_mps

Description (problem / solution / changelog)

Stack from ghstack (oldest at bottom):

  • -> #184403

MPS copy kernel handles is_conj but not is_neg, so cloning or printing a tensor with the negative bit set (x.conj().imag for example) returns the un-negated underlying data.

Fix by mirroring existing conjugation handling logic Fixes https://github.com/pytorch/pytorch/issues/184379

Authored with Claude

Changed files

  • aten/src/ATen/native/mps/operations/Copy.mm (modified, +13/-5)
  • test/test_mps.py (modified, +18/-0)

Code Example

import torch

x = torch.complex(
    torch.tensor([1.0, 3.0], device="mps"),
    torch.tensor([2.0, 4.0], device="mps"),
)
c = torch.conj(x)

print("is_conj():       ", c.is_conj())              # True
print("c (repr):        ", c)                        # tensor([1.-2.j, 3.-4.j], device='mps:0')  (resolved for display)
print("c.real:          ", c.real)                   # tensor([1., 3.])   correct
print("c.imag:          ", c.imag)                   # tensor([2., 4.])WRONG, expected [-2., -4.]
print("c.resolve_conj().imag:", c.resolve_conj().imag)  # tensor([-2., -4.])   correct
print("CPU baseline:    ", torch.conj(x.cpu()).imag) # tensor([-2., -4.])   correct

---

is_conj():        True
c (repr):         tensor([1.-2.j, 3.-4.j], device='mps:0')
c.real:           tensor([1., 3.], device='mps:0')
c.imag:           tensor([2., 4.], device='mps:0')
c.resolve_conj().imag: tensor([-2., -4.], device='mps:0')
CPU baseline:     tensor([-2., -4.])

---

PyTorch version: 2.10.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 26.4.1 (arm64)
GCC version: Could not collect
Clang version: 21.0.0 (clang-2100.1.1.101)
CMake version: version 4.3.2
Libc version: N/A

Python version: 3.13.13 (main, Apr  7 2026, 18:19:01) [Clang 21.0.0 (clang-2100.0.123.102)] (64-bit runtime)
Python platform: macOS-26.4.1-arm64-arm-64bit-Mach-O
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A

CPU:
Apple M3 Max

Versions of relevant libraries:
[pip3] diffractorch==0.2.0
[pip3] mypy_extensions==1.1.0
[pip3] numpy==2.4.3
[pip3] torch==2.10.0
[pip3] torchvision==0.25.0
[conda] Could not collect
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

On the MPS backend, .imag accessed on the lazy conjugate view returned by torch.conj() ignores the conjugate bit and returns the original imaginary part instead of its negation. The same code is correct on CPU, and calling .resolve_conj() first restores correctness — so the bug is specifically in the .imag accessor's handling of is_conj() views on MPS.

.real is not affected (it's the same with or without conjugation, so it doesn't expose the bug), and the printed repr of the conjugate view goes through a materialization path that displays the correct values — which makes this easy to miss in interactive use.

Minimal repro:

import torch

x = torch.complex(
    torch.tensor([1.0, 3.0], device="mps"),
    torch.tensor([2.0, 4.0], device="mps"),
)
c = torch.conj(x)

print("is_conj():       ", c.is_conj())              # True
print("c (repr):        ", c)                        # tensor([1.-2.j, 3.-4.j], device='mps:0')  (resolved for display)
print("c.real:          ", c.real)                   # tensor([1., 3.])   correct
print("c.imag:          ", c.imag)                   # tensor([2., 4.])   ← WRONG, expected [-2., -4.]
print("c.resolve_conj().imag:", c.resolve_conj().imag)  # tensor([-2., -4.])   correct
print("CPU baseline:    ", torch.conj(x.cpu()).imag) # tensor([-2., -4.])   correct

Output (torch 2.10.0, macOS 26.4.1, Apple M3 Max):

is_conj():        True
c (repr):         tensor([1.-2.j, 3.-4.j], device='mps:0')
c.real:           tensor([1., 3.], device='mps:0')
c.imag:           tensor([2., 4.], device='mps:0')
c.resolve_conj().imag: tensor([-2., -4.], device='mps:0')
CPU baseline:     tensor([-2., -4.])

Versions

PyTorch version: 2.10.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 26.4.1 (arm64)
GCC version: Could not collect
Clang version: 21.0.0 (clang-2100.1.1.101)
CMake version: version 4.3.2
Libc version: N/A

Python version: 3.13.13 (main, Apr  7 2026, 18:19:01) [Clang 21.0.0 (clang-2100.0.123.102)] (64-bit runtime)
Python platform: macOS-26.4.1-arm64-arm-64bit-Mach-O
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A

CPU:
Apple M3 Max

Versions of relevant libraries:
[pip3] diffractorch==0.2.0
[pip3] mypy_extensions==1.1.0
[pip3] numpy==2.4.3
[pip3] torch==2.10.0
[pip3] torchvision==0.25.0
[conda] Could not collect

cc @ezyang @anjali411 @dylanbespalko @mruberry @nikitaved @amjames @kulinseth @malfet @DenisVieriu97 @jhavukainen @aditvenk

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

pytorch - ✅(Solved) Fix [MPS] torch.conj(x).imag returns un-conjugated imag on conjugate view (CPU correct) [1 pull requests, 1 comments, 2 participants]