Building `pt.diag` is 50x slower than building an Elemwise

Aperta
#2,631 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
performance

Direzione di ricerca

Start by tracing pt.diag(x) through SymbolicOp.__call__ and the AllocDiag construction described in the issue; compare that path with reusing the existing op. Run the included timing snippet to reproduce the slowdown. Done means repeated calls avoid the reported construction cost while preserving correct behavior; the issue leaves open whether to memoize AllocDiag or make OpFromGraph construction cheaper.

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

Descrizione

OpFromGraph performance

pt.diag(x) takes about 1.2 ms to build on 3.3.8. pt.exp(x) takes 0.024 ms. Most of the time goes to SymbolicOp.__call__ making a brand new AllocDiag on every call. Each new one reruns build_inner_graph and the whole OpFromGraph constructor, even when the props and input types match one that already exists.

Reusing an existing AllocDiag takes 0.008 ms, so memoizing the specialized op seems like an obvious thing to try. Maybe that's a hack and the real fix is making OpFromGraph construction cheaper in general? Not sure.

import timeit

import pytensor.tensor as pt

x = pt.vector("x")
diag_op = pt.diag(x).owner.op

# seconds per 1000 calls == ms per call
print(timeit.timeit(lambda: pt.diag(x), number=1000))   # 1.20
print(timeit.timeit(lambda: diag_op(x), number=1000))   # 0.008
print(timeit.timeit(lambda: pt.exp(x), number=1000))    # 0.024
print(pt.diag(x).owner.op is pt.diag(x).owner.op)       # False
Lingua principale
Python
Stelle
654
Fork
258
Merge medio
3g 41m
PR unite (30g)
42

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.