Gradient of batched block_diag falls back to numba object mode (Blockwise ARange)
I maintainer di solito rispondono entro 1 giorno
@saksham10arora-dotcom ci sta già lavorando.
Dal 21/11/2026.
- #2703 di @saksham10arora-dotcom — aperta
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- numpy, python
- Ambito
- performance
Direzione di ricerca
Reproduce with the snippet in mode="NUMBA" and confirm the Blockwise{ARange} object-mode warning and the timing gap. Then read BlockDiagonal.pullback and how Blockwise calls vectorize_shape, since that is where core shapes become batched; compare against the concatenate-based construction to see what a non-batched pullback would look like. Done means the gradient runs in nopython mode, timings approach the concatenate figure, and a regression test covers batched plus unbatched inputs.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
When one input to block_diag is batched, its gradient compiles a Blockwise{ARange} that numba runs in object mode. In a small example the gradient is about 20x slower than building the same matrix with concatenate.
import pytensor
import pytensor.tensor as pt
A = pt.tensor3("A") # batch of 6x6 blocks
B = pt.matrix("B") # 2x2, not batched
out = (pt.linalg.block_diag(A, B) ** 2).sum()
f = pytensor.function([A, B], pytensor.grad(out, [A, B]), mode="NUMBA")
# UserWarning: Numba will use object mode to run Blockwise{ARange{dtype='int64'}, (),(),()->(o00)}'s perform method
Timings with A of shape (1000, 6, 6), pytensor 3.3.8, M-series Mac:
| gradient of | ms per call |
|---|---|
block_diag(A, B) as it is now |
3.2 |
same, with the pullback using slices instead of arange |
1.1 (still object mode, now Blockwise{Subtensor}) |
the same matrix built with concatenate and zeros |
0.14 |
What I think happens: BlockDiagonal.pullback indexes gout with arange(index_begin, index_end), computed from i.shape. When Blockwise vectorizes the pullback, vectorize_shape broadcasts each core shape over the batch (which is correct in general), so the indices and the arange become batched even though they are identical for every batch element. Swapping arange for slices removes the ARange, but the slice bounds are still batched, so it moves the problem to Blockwise{Subtensor}.
Where I hit it: pymc-extras structural models. TimeSeasonality(duration > 1) has a time-varying transition matrix, and adding any other component (e.g. Autoregressive) combines them with block_diag(T, o_T), so the Kalman filter gradient picks this up (pymc-devs/pymc-extras#833).
I'm not sure what the preferred fix is: a pullback that avoids batched indices, a rewrite that drops batch dims from Blockwise inputs that only come from broadcast shapes, or something else. Happy to work on it with some direction.
- Lingua principale
- Python
- Stelle
- 654
- Fork
- 258
- Merge medio
- 2g 7h
- PR unite (30g)
- 39
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di pymc-devs/pytensor
-
Docs of mode still mentions C/CVM as the default with FAST_RUNForse già presa @ADITYASINGH1206 l’ha presa 8 giorni fa. Apertadocs
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
pymc-devs/pytensor#2641 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
docs
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno
-
BUG: geomspace returns wrong sign for the endpoints when start and stop are negativeForse già presa @mayuriphad l’ha presa 27 giorni fa. Aperta
Difficoltà 2/5 Meno di un'ora Idoneità per principianti 85/100
I maintainer di solito rispondono entro 1 giorno
-
Reciprocal.impl computes np.float32(1.0) / x: float32 precision for float64 scalars under NEP 50 (Python linker)Forse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
pymc-devs/pytensor#2430 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
pymc-devs/pytensor#2428 · 5 commenti ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di pymc-devs/pytensor
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
dgunning/edgartools#2019 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
topoteretes/cognee#6818 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
source_verification_agent: no outcome when the publisher confirms a work but its DOI resolves to a different recordForse già presa @pinjun99 l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
Imbad0202/academic-research-skills#1159 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
[Bug]: DoubleMLData.from_arrays checks the treatment d with force_all_x_finite instead of force_all_d_finiteForse già presa @leninworld l’ha presa oggi. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 88/100
DoubleML/doubleml-for-py#471 ·
I maintainer di solito rispondono entro 1 giorno