Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove cached plates from AutoGuide. #1976

Merged
merged 1 commit into from
Feb 16, 2025

Conversation

tillahoffmann
Copy link
Contributor

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