#32671·numpy

BUG: np.einsum returns wrong results or a segfault, no error when a new style dtypes.

Author: tylereeCreated Sep 17, 2026Updated Sep 17, 2026
Labels00 - Bug

Describe the issue:

new style DTypes have type number -1, and the kernel lookup in einsum_sumprod.c.src only rejects type numbers at or above the legacy count, so the tables are indexed at -1

Reproduce the code example:

python
import numpy as np
from numpy_quaddtype import QuadPrecDType

rng = np.random.default_rng(0)
a64, b64 = rng.standard_normal((2, 6, 6))
a = np.asarray(a64, dtype=QuadPrecDType())
b = np.asarray(b64, dtype=QuadPrecDType())

print("type_num:", np.dtype(QuadPrecDType()).num)
for subs in ["ij,jk->ik", "ij,ij->ij", "ij->ji"]:
    got = np.asarray(np.einsum(subs, a, b) if "," in subs else np.einsum(subs, a), dtype=np.float64)
    ref = np.einsum(subs, a64, b64) if "," in subs else np.einsum(subs, a64)
    print(f"{subs:<10} max abs error vs float64: {np.max(np.abs(got - ref)):.3g}")
print("matmul     max abs error vs float64:", f"{np.max(np.abs(np.asarray(a @ b, dtype=np.float64) - a64 @ b64)):.3g}")
print(np.einsum("ij->", a))  # segfaults on Windows (on Linux: np.einsum("i,i->", a[0], a[0]))

Error message:

bash
(numpy-dev) PS E:\dev\numpy>   E:\dev\venvs\baseline\Scripts\python.exe -u E:\dev\numpy\einsum_issue_repro.py
type_num: -1
ij,jk->ik  max abs error vs float64: 4.4
ij,ij->ij  max abs error vs float64: 4.19
ij->ji     max abs error vs float64: 0
matmul     max abs error vs float64: 8.88e-16
(numpy-dev) PS E:\dev\numpy>

Python and NumPy Versions:

(numpy-dev) PS E:\dev\numpy>   E:\dev\venvs\baseline\Scripts\python.exe -P -c "import sys, numpy; print(numpy.__version__); print(sys.version)"
2.5.3
3.12.10 (tags/v3.12.10:0cc8128, Apr  8 2025, 12:21:36) [MSC v.1943 64 bit (AMD64)]
(numpy-dev) PS E:\dev\numpy> 

repo on main at e80572f 

Runtime Environment:

(numpy-dev) PS E:\dev\numpy>   E:\dev\venvs\baseline\Scripts\python.exe -m pip install threadpoolctl
Collecting threadpoolctl
  Using cached threadpoolctl-3.7.0-py3-none-any.whl.metadata (24 kB)
Using cached threadpoolctl-3.7.0-py3-none-any.whl (26 kB)
Installing collected packages: threadpoolctl
Successfully installed threadpoolctl-3.7.0
(numpy-dev) PS E:\dev\numpy> 

How does this issue affect you or how did you find it:

trying numpy_quaddtype with einsum to see if it worked, and the numbers did not match a @ b, then one reduction crashed python

No impact on me. Wrong result comes back silently with no errors so anyone using a new style dtype gets bad numbers without knowing. Fixing this will be a bigger lift and looking into it but first PR will just to give errors to end user.