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

オープン
#32,671 コメント 6 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
55/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
c, python
領域
data

調査の方向性

einsum_sumprod.c.src のカーネル検索から始め、提供された新しいスタイルの DType の再現を実行します。segfault が発生する reduction case も含めてください。テーブルをインデックスする前に、型番号がどのように検証されるかを追跡します。これらの入力が、誤った結果を黙って返したり Python をクラッシュさせたりするのではなく、エラーを報告すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

00 - 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:
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:
(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.

主要言語
Python
スター
32.8k
フォーク
12.8k
平均マージ
1日 7時間
マージ済み PR(30日)
197

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

numpy/numpy のほかの issue

numpy/numpy の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。