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

Fix event dim of some arg constraints #1967

Merged
merged 1 commit into from
Jan 30, 2025
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
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