Skip to content

Commit

Permalink
fix event dim of some arg constraints (#1967)
Browse files Browse the repository at this point in the history
  • Loading branch information
fehiepsi authored Jan 30, 2025
1 parent a9c9fc6 commit 7514990
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ license: FORCE
python scripts/update_headers.py

format: license FORCE
ruff check --fix .
ruff format .
ruff check --fix .

install: FORCE
pip install -e '.[dev,doc,test,examples]'
Expand Down
9 changes: 6 additions & 3 deletions numpyro/distributions/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -2426,7 +2426,10 @@ def entropy(self):


class Uniform(Distribution):
arg_constraints = {"low": constraints.dependent, "high": constraints.dependent}
arg_constraints = {
"low": constraints.dependent(is_discrete=False, event_dim=0),
"high": constraints.dependent(is_discrete=False, event_dim=0),
}
reparametrized_params = ["low", "high"]
pytree_data_fields = ("low", "high", "_support")

Expand Down Expand Up @@ -2727,7 +2730,7 @@ class Wishart(TransformedDistribution):
"""

arg_constraints = {
"concentration": constraints.dependent(is_discrete=False),
"concentration": constraints.dependent(is_discrete=False, event_dim=0),
"scale_matrix": constraints.positive_definite,
"rate_matrix": constraints.positive_definite,
"scale_tril": constraints.lower_cholesky,
Expand Down Expand Up @@ -2820,7 +2823,7 @@ class WishartCholesky(Distribution):
"""

arg_constraints = {
"concentration": constraints.dependent(is_discrete=False),
"concentration": constraints.dependent(is_discrete=False, event_dim=0),
"scale_matrix": constraints.positive_definite,
"rate_matrix": constraints.positive_definite,
"scale_tril": constraints.lower_cholesky,
Expand Down
5 changes: 4 additions & 1 deletion numpyro/distributions/discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,10 @@ def Categorical(probs=None, logits=None, *, validate_args=None):


class DiscreteUniform(Distribution):
arg_constraints = {"low": constraints.dependent, "high": constraints.dependent}
arg_constraints = {
"low": constraints.dependent(is_discrete=True, event_dim=0),
"high": constraints.dependent(is_discrete=True, event_dim=0),
}
has_enumerate_support = True
pytree_data_fields = ("low", "high", "_support")

Expand Down
4 changes: 2 additions & 2 deletions numpyro/distributions/truncated.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,8 @@ def var(self):

class TwoSidedTruncatedDistribution(Distribution):
arg_constraints = {
"low": constraints.dependent,
"high": constraints.dependent,
"low": constraints.dependent(is_discrete=False, event_dim=0),
"high": constraints.dependent(is_discrete=False, event_dim=0),
}
reparametrized_params = ["low", "high"]
supported_types = (Cauchy, Laplace, Logistic, Normal, SoftLaplace, StudentT)
Expand Down
11 changes: 11 additions & 0 deletions test/test_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,17 @@ def test_has_rsample(jax_dist, sp_dist, params):
transf_dist.rsample(random.PRNGKey(0))


@pytest.mark.parametrize(
"jax_dist_cls, sp_dist, params", CONTINUOUS + DISCRETE + DIRECTIONAL
)
def test_args_attributes(jax_dist_cls, sp_dist, params):
jax_dist = jax_dist_cls(*params)
for constraint in jax_dist.arg_constraints.values():
if jax_dist_cls != dist.Delta:
constraint.event_dim
constraint.is_discrete


@pytest.mark.parametrize("batch_shape", [(), (4,), (3, 2)])
def test_unit(batch_shape):
log_factor = random.normal(random.PRNGKey(0), batch_shape)
Expand Down

0 comments on commit 7514990

Please sign in to comment.