Skip to content

Conversation

@tillahoffmann
Copy link
Collaborator

The plate primitive may leak tracers through its _indices attribute if created outside a pure function. The AutoGuide implementation caches plates in a plates attribute which results in a tracer leak when the guide is called (see example below).

This PR removes the caching such that calls to the guide are pure.

>>> import jax
>>> import numpyro


>>> def model():
...     with numpyro.plate("n", 2):
...         numpyro.sample("x", numpyro.distributions.Normal())

>>> guide = numpyro.infer.autoguide.AutoDelta(model)

>>> numpyro.handlers.seed(guide, 9)()
>>> guide.plates["n"]._indices
Array([0, 1], dtype=int32)

>>> jax.jit(lambda: numpyro.handlers.seed(guide, 9)())()
>>> guide.plates["n"]._indices
Traced<ShapedArray(int32[2])>with<DynamicJaxprTrace>

Copy link
Member

@fehiepsi fehiepsi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Till!

@fehiepsi fehiepsi merged commit 5793d09 into pyro-ppl:master Feb 16, 2025
10 checks passed
@tillahoffmann tillahoffmann deleted the leaky-plates branch February 16, 2025 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants