Bug: full einsum contractions upcast the dtype and count booleans

Aperta Adatta ai principianti
#1,022 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@RaphaelFakhri ci sta già lavorando.

Dal 3/11/2026.

  • #1023 di @RaphaelFakhri — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
numpy, python
Ambito
data

Direzione di ricerca

Inizia leggendo _einsum_single e i due rami di contrazione completa menzionati nell’issue; confronta le chiamate a .sum() con il percorso di contrazione parziale e il comportamento di dtype di NumPy. Riproduci gli esempi con interi, booleani e dtype esplicito dell’issue, poi verifica che le contrazioni complete mantengano il dtype e i valori previsti.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

sparse version checks
  • I checked that this issue has not been reported before.
  • I have confirmed this bug exists on the latest version of sparse.
  • I have confirmed this bug exists on the main branch of sparse.
Describe the bug

When sparse.einsum contracts every index (an empty output such as "ij->" or "ij,ij->"), the result does not keep the input dtype. Integer inputs come back as int64 or uint64, boolean inputs come back as int64 holding a count instead of True, and the dtype argument has no effect for integer types. A partial contraction such as "ij->i" keeps the dtype, and so does numpy.einsum.

Steps or code to reproduce the bug
import numpy as np
import sparse

x = np.array([[0, 1, 0], [2, 0, 1], [1, 1, 0]], dtype=np.int8)
s = sparse.COO.from_numpy(x)
print(sparse.einsum("ij->", s).dtype, sparse.einsum("ij->i", s).dtype)

b = sparse.COO.from_numpy(x.astype(bool))
print(sparse.einsum("ij->", b).todense())

f = sparse.COO.from_numpy(x.astype(float))
print(sparse.einsum("ij,ij->", f, f, dtype=np.int32).dtype)
Expected results

The same dtypes and values as numpy.einsum:

int8 int8
True
int32
Actual results
int64 int8
5
int64
Please describe your system.
  1. OS and version: Linux (Oracle Linux 9)
  2. sparse version: main at 95f0da5
  3. NumPy version: 2.5.4
Cause

The two full-contraction branches in _einsum_single call .sum() without a dtype, so NumPy's default promotion for small integer and boolean types applies.

Lingua principale
Python
Stelle
670
Fork
167
Merge medio
3g 4h
PR unite (30g)
7

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pydata/sparse

Tutte le issue di pydata/sparse

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.