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
17 changes: 17 additions & 0 deletions pytensor/link/jax/dispatch/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@ def sample_fn(rng, size, dtype, *parameters):
return sample_fn


@jax_sample_fn.register(aer.HalfNormalRV)
def jax_sample_fn_halfnormal(op):
"""JAX implementation of `HalfNormalRV`."""

def sample_fn(rng, size, dtype, *parameters):
rng_key = rng["jax_state"]
rng_key, sampling_key = jax.random.split(rng_key, 2)
loc, scale = parameters
sample = (
loc + jax.numpy.abs(jax.random.normal(sampling_key, size, dtype)) * scale
)
rng["jax_state"] = rng_key
return (rng, sample)

return sample_fn


@jax_sample_fn.register(aer.ChoiceRV)
def jax_funcify_choice(op):
"""JAX implementation of `ChoiceRV`."""
Expand Down
16 changes: 16 additions & 0 deletions tests/link/jax/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,22 @@ def test_random_updates(rng_ctor):
"uniform",
lambda *args: args,
),
(
aer.halfnormal,
[
set_test_value(
at.dvector(),
np.array([-1.0, 2.0], dtype=np.float64),
),
set_test_value(
at.dscalar(),
np.array(1000.0, dtype=np.float64),
),
],
(2,),
"halfnorm",
lambda *args: args,
),
],
)
def test_random_RandomVariable(rv_op, dist_params, base_size, cdf_name, params_conv):
Expand Down