Flows rebuilt with the same structure retrace every jitted function

Aperta
#365 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
1-2 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python

Direzione di ricerca

Start by reading make_flow in nutpie.normalizing_flow and flowjax.utils.get_ravelled_pytree_constructor, which the issue identifies as creating new closures on each rebuild. Reproduce the example with the JAX backend and compare trace counts across identical flow structures. Done when repeated builds reuse JIT cache entries and the example reports fewer traces.

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

Descrizione

normalizing-flows

Flows that make_flow builds with the same seed and configuration never share a jit cache entry, so each rebuild during adaptation traces and compiles the loss and the hot-path functions again. The flows store new function objects in static fields. make_flow creates the scale-map lambda on every call, and flowjax.utils.get_ravelled_pytree_constructor returns a new closure for every coupling layer. In a 50-dimensional run with the JAX backend, _init_from_transformed_position compiled 8 times, and the run compiled 620 XLA programs in total.

import equinox as eqx
import jax.numpy as jnp
import numpy as np
from nutpie.normalizing_flow import make_flow

traces = 0


@eqx.filter_jit
def log_det(flow, x):
    global traces
    traces += 1
    return flow.transform_and_log_det(x)[1]


rng = np.random.default_rng(0)
for n_layers in [0, 4]:
    traces = 0
    for _ in range(5):
        draws = rng.normal(size=(200, 10))
        flow = make_flow(0, draws, -draws, n_layers=n_layers)  # same seed and config every time
        log_det(flow, jnp.zeros(10, dtype=jnp.float32))
    print(f"n_layers={n_layers}: {traces} traces for 5 flows with identical structure")
# n_layers=0: 5 traces for 5 flows with identical structure
# n_layers=4: 5 traces for 5 flows with identical structure
Lingua principale
Python
Stelle
214
Fork
30
Merge medio
3h 50m
PR unite (30g)
1

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

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/nutpie

Tutte le issue di pymc-devs/nutpie

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.