BUG: cumsum and cumprod keep bool and small integer dtypes, so cumsum(x > 0) is wrong

Aperta Adatta ai principianti
#2,727 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@rishabhsai ci sta già lavorando.

Dal 26/11/2026.

  • #2728 di @rishabhsai — aperta

Valutazione

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

Direzione di ricerca

L’issue rimanda a CumOp.make_node in pytensor/tensor/extra_ops.py e descrive differenze tra il codice C, i kernel Numba e perform. Inizia leggendo CumOp e i test corrispondenti per le operazioni cumulative, poi confronta i risultati per input bool e interi di piccole dimensioni tra i backend. Il lavoro è completato quando gli output usano il tipo di dato di accumulazione previsto e corrispondono negli esempi segnalati.

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

Descrizione

Describe the issue:

pt.cumsum and pt.cumprod give the result the input's dtype. NumPy and pt.sum accumulate bool and small integers in int64 (uint64 for unsigned), so a running count over a mask comes out wrong. In the default (Numba) mode pt.cumsum(x > 0, axis=1) is all True, and pt.cumsum(x > 0).max() returns True instead of 3. The C backend also wraps int8 totals.

The output type comes from CumOp.make_node (return Apply(self, [x], [x.type()]) in pytensor/tensor/extra_ops.py). The C code and the Numba kernels for ndim > 1 accumulate in the input dtype, while perform and the 1-d Numba kernel call np.cumsum, which upcasts and returns int64 data for a variable typed bool or int8. So the backends disagree with each other as well as with NumPy.

Reproducible code example:
import numpy as np
import pytensor
import pytensor.tensor as pt

x = pt.matrix("x")
n = pt.cumsum(x > 0, axis=1)
v = np.array([[1.0, 2.0, -1.0, 3.0]])

print(n.dtype)
print(pytensor.function([x], n)(v), np.cumsum(v > 0, axis=1))
print(pytensor.function([x], n.max())(v), np.cumsum(v > 0, axis=1).max())

i = pt.vector("i", dtype="int8")
c_mode = pytensor.compile.mode.Mode(linker="cvm", optimizer="fast_run")
print(pytensor.function([i], pt.cumsum(i), mode=c_mode)(np.array([100, 100, 2], dtype="int8")))
Error message:
bool
[[ True  True  True  True]] [[1 2 2 3]]
True 3
[100 -56 -54]
PyTensor version information:

main at e213406 (3.3.8+65), NumPy 2.5.5, Numba 0.68.1, Python 3.12.7, macOS arm64

Context for the issue:

cumsum(mask) is a common way to number the selected entries. Casting these inputs to int64/uint64 in cumsum/cumprod, with the same mapping Sum uses, fixes it for every backend. I have that change with a test and can open a PR.

Lingua principale
Python
Stelle
654
Fork
258
Merge medio
3g 17h
PR unite (30g)
43

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 pymc-devs/pytensor

Tutte le issue di pymc-devs/pytensor

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.