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

avoids setting jax tracer as lazy property attribute #1843

Merged
merged 8 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion numpyro/distributions/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from jax.scipy.linalg import solve_triangular
from jax.scipy.special import digamma

from numpyro.util import not_jax_tracer

# Parameters for Transformed Rejection with Squeeze (TRS) algorithm - page 3.
_tr_params = namedtuple(
"tr_params", ["c", "b", "a", "alpha", "u_r", "v_r", "m", "log_p", "log1_p", "log_h"]
Expand Down Expand Up @@ -692,7 +694,8 @@ def __get__(self, instance, obj_type=None):
if instance is None:
return self
value = self.wrapped(instance)
setattr(instance, self.wrapped.__name__, value)
if not_jax_tracer(value):
setattr(instance, self.wrapped.__name__, value)
return value


Expand Down
42 changes: 42 additions & 0 deletions test/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from jax.scipy.special import expit, logsumexp
from jax.scipy.stats import norm as jax_norm, truncnorm as jax_truncnorm

import numpyro
import numpyro.distributions as dist
from numpyro.distributions import (
SineBivariateVonMises,
Expand All @@ -49,8 +50,10 @@
sum_rightmost,
vec_to_tril_matrix,
)
from numpyro.infer import MCMC, NUTS, Predictive
sbidari marked this conversation as resolved.
Show resolved Hide resolved
from numpyro.nn import AutoregressiveNN

numpyro.set_host_device_count(2)
TEST_FAILURE_RATE = 2e-5 # For all goodness-of-fit tests.


Expand Down Expand Up @@ -3423,3 +3426,42 @@ def test_gaussian_random_walk_linear_recursive_equivalence():
x2 = dist2.sample(random.PRNGKey(7))
assert jnp.allclose(x1, x2.squeeze())
assert jnp.allclose(dist1.log_prob(x1), dist2.log_prob(x2))


@pytest.mark.parametrize(
"my_dist",
[
dist.TruncatedNormal(low=-1, high=1),
dist.TruncatedCauchy(low=-1, high=1),
],
)
def test_no_tracer_leakage_in_truncated_distribution(my_dist):
"""
Tests parallel sampling and use of multiple predictve methods
on models using truncated distributions.
Reference: https://github.com/pyro-ppl/numpyro/issues/1836, and
https://github.com/CDCgov/multisignal-epi-inference/issues/282
"""
n_data_samples = 10
n_prior_samples = 10
n_mcmc_samples = 10
n_mcmc_warmup = 10
n_mcmc_chains = 2
sbidari marked this conversation as resolved.
Show resolved Hide resolved
rng_key = jax.random.PRNGKey(0)

data_samples = my_dist.sample(rng_key, (n_data_samples,))
sbidari marked this conversation as resolved.
Show resolved Hide resolved

def my_model(obs=None):
numpyro.sample("obs", my_dist, obs=obs)

prior_predictive = Predictive(my_model, num_samples=n_prior_samples)
prior_predictive(rng_key)
sbidari marked this conversation as resolved.
Show resolved Hide resolved

nuts_kernel = NUTS(my_model)
mcmc = MCMC(
nuts_kernel,
num_warmup=n_mcmc_warmup,
num_samples=n_mcmc_samples,
num_chains=n_mcmc_chains,
)
mcmc.run(rng_key, obs=data_samples)
sbidari marked this conversation as resolved.
Show resolved Hide resolved
Loading