Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 1 addition & 2 deletions numpyro/distributions/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import jax
from jax import Array, jit, lax, random, vmap
from jax.core import Tracer
import jax.numpy as jnp
from jax.scipy.linalg import solve_triangular
from jax.scipy.special import digamma
Expand Down Expand Up @@ -298,7 +297,7 @@ def _multinomial(key, p, n, n_max, shape=()):

def multinomial(key, p, n, shape=(), total_count_max=None):
if total_count_max is None:
if isinstance(n, Tracer):
if not not_jax_tracer(n):
raise ValueError(
"Please specify total_count_max in Multinomial distribution."
)
Expand Down
3 changes: 1 addition & 2 deletions numpyro/infer/reparam.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import numpy as np

import jax
import jax.numpy as jnp

import numpyro
Expand Down Expand Up @@ -92,7 +91,7 @@ class LocScaleReparam(Reparam):

def __init__(self, centered=None, shape_params=()):
assert centered is None or isinstance(
centered, (int, float, np.generic, np.ndarray, jnp.ndarray, jax.core.Tracer)
centered, (int, float, np.generic, np.ndarray, jnp.ndarray)
)
assert isinstance(shape_params, (tuple, list))
assert all(isinstance(name, str) for name in shape_params)
Expand Down
11 changes: 10 additions & 1 deletion test/infer/test_reparam.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from numpy.testing import assert_allclose
import pytest

from jax import jacobian, lax, random
from jax import jacobian, jit, lax, random
import jax.numpy as jnp

import numpyro
Expand Down Expand Up @@ -591,3 +591,12 @@ def run(config):
return mcmc.get_samples()["x"].mean(0)

assert_allclose(run({"x": reparam}), run({}), atol=0.15)


def test_loc_scale_traced_centered():
@jit
def f(centered):
reparam = LocScaleReparam(centered)
return reparam.centered

assert f(jnp.array(0.5)) == 0.5
11 changes: 11 additions & 0 deletions test/test_distributions_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,14 @@ def test_promote_shapes_array_like():
assert y.shape == (1,)
assert x.shape == (3,)
assert_allclose(y.todense(), jnp.array([2.0]))


def test_multinomial_traced_n_requires_total_count_max():
p = jnp.array([0.2, 0.3, 0.5])

@jax.jit
def f(n):
return multinomial(random.key(0), p, n)

with pytest.raises(ValueError, match="total_count_max"):
f(jnp.array(10))
Loading