Skip to content

[torch-neuronx 2.14.3] Silent wrong results: matmul on the neuron device ignores the lazy conjugate bit (A.mH @ B returns A.T @ B) #1455

Done

Description

@scttfrdmn

Environment: trn2.3xlarge, Neuron SDK 2.33.0, torch 2.14.0, torch-neuronx 2.14.3.0.1+b1b6c66, native neuron device, LNC=2.

Summary: silent wrong results.

  • matmul / @ on the neuron device ignores the lazy conjugate bit of a complex view (Tensor.is_conj() == True).
  • A.mH @ B and A.conj().T @ B return A.T @ B, and C.conj() @ X returns C @ X, with no error or warning.
  • Materialising the conjugate first (.resolve_conj() or .contiguous()) gives the right answer.
  • .mH is the standard PyTorch idiom for the conjugate transpose, so any complex linear algebra written the usual way is silently wrong on device.

Repro:

import torch
import torch_neuronx  # noqa: F401

torch.manual_seed(0)
A = torch.randn(64, 48, dtype=torch.complex64)
B = torch.randn(64, 32, dtype=torch.complex64)
C = torch.randn(32, 48, dtype=torch.complex64)
Ad, Bd, Cd = A.to("neuron"), B.to("neuron"), C.to("neuron")


def rel(x, ref):
    return float((x.cpu() - ref).abs().max() / ref.abs().max())


ref_h = A.conj().T @ B          # A^H B, computed on the CPU
ref_c = C.conj() @ A.T          # conj(C) A^T  (lazy conj without transpose)
ref_t = A.T @ B                 # what you get if the conjugate is dropped
cases = [
    ("A.mH @ B", lambda: Ad.mH @ Bd, ref_h),
    ("A.conj().T @ B", lambda: Ad.conj().T @ Bd, ref_h),
    ("torch.matmul(A.mH, B)", lambda: torch.matmul(Ad.mH, Bd), ref_h),
    ("A.mH.contiguous() @ B", lambda: Ad.mH.contiguous() @ Bd, ref_h),
    ("A.mH.resolve_conj() @ B", lambda: Ad.mH.resolve_conj() @ Bd, ref_h),
    ("C.conj() @ A.T", lambda: Cd.conj() @ Ad.T, ref_c),
    ("C.conj().resolve_conj() @ A.T", lambda: Cd.conj().resolve_conj() @ Ad.T, ref_c),
]
print("is_conj(A.mH) on neuron:", Ad.mH.is_conj())
for name, f, ref in cases:
    try:
        y = f()
        print(f"{name:32s} rel_err={rel(y, ref):.3e}   rel_err_vs_unconjugated={rel(y, ref_t) if ref is ref_h else float('nan'):.3e}")
    except Exception as e:  # noqa: BLE001
        print(f"{name:32s} RAISES {type(e).__name__}: {str(e).splitlines()[0][:100]}")

Observed (identical in two runs):

is_conj(A.mH) on neuron: True
A.mH @ B                         rel_err=1.340e+00   rel_err_vs_unconjugated=2.236e-07
A.conj().T @ B                   rel_err=1.340e+00   rel_err_vs_unconjugated=2.236e-07
torch.matmul(A.mH, B)            rel_err=1.340e+00   rel_err_vs_unconjugated=2.236e-07
A.mH.contiguous() @ B            rel_err=2.373e-07   rel_err_vs_unconjugated=1.553e+00
A.mH.resolve_conj() @ B          rel_err=2.373e-07   rel_err_vs_unconjugated=1.553e+00
C.conj() @ A.T                   rel_err=1.440e+00
C.conj().resolve_conj() @ A.T    rel_err=2.470e-07

rel_err is against the CPU result. rel_err_vs_unconjugated is against A.T @ B, which the device result matches to 2e-7.

Expected: matmul honours is_conj() on its inputs, as the CPU and CUDA backends do (or materialises them), or it raises.

Impact we saw: in a scientific-computing library (trnsci/trnsolver#65), complex Hermitian paths written with .mH gave relative errors of 0.2–1.8 in eigh, pinv and solve_spd on device. We work around it with resolve_conj() before every device matmul.

Activity

  1. github-actions commented on Nov 2, 2026

    @github-actions

    Hi @scttfrdmn, Thank you for filing the issue! We will take a look and get back to you.

  2. added
    bug
    on Nov 2, 2026
  3. mgray07 commented on Nov 5, 2026

    @mgray07

    Thanks for the detailed reproducer and CPU comparisons, will look for a relevant owner.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Trn2bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions