|
| 1 | +from collections.abc import Sequence |
| 2 | +from functools import wraps |
| 3 | + |
| 4 | +import pytensor.tensor.random.basic as ptr |
| 5 | +from pytensor.graph.basic import Variable |
| 6 | +from pytensor.tensor.random.op import RandomVariable |
| 7 | +from pytensor.xtensor.vectorization import XRV |
| 8 | + |
| 9 | + |
| 10 | +def _as_xrv( |
| 11 | + core_op: RandomVariable, |
| 12 | + core_dims_needed: int | None = None, |
| 13 | + core_inps_dims_map: Sequence[Sequence[int]] | None = None, |
| 14 | + core_out_dims_map: Sequence[int] | None = None, |
| 15 | +): |
| 16 | + if core_inps_dims_map is None: |
| 17 | + core_inps_dims_map = [tuple(range(ndim)) for ndim in core_op.ndims_params] |
| 18 | + if core_out_dims_map is None: |
| 19 | + core_out_dims_map = tuple(range(core_op.ndim_supp)) |
| 20 | + if core_dims_needed is None: |
| 21 | + core_dims_needed = max( |
| 22 | + (*(len(i) for i in core_inps_dims_map), len(core_out_dims_map)), default=0 |
| 23 | + ) |
| 24 | + |
| 25 | + @wraps(core_op) |
| 26 | + def xrv_constructor( |
| 27 | + *params, |
| 28 | + core_dims: Sequence[str] | str, |
| 29 | + extra_dims: dict[str, Variable] | None = None, |
| 30 | + rng: Variable | None = None, |
| 31 | + ): |
| 32 | + if isinstance(core_dims, str): |
| 33 | + core_dims = (core_dims,) |
| 34 | + if len(core_dims) != core_dims_needed: |
| 35 | + raise ValueError( |
| 36 | + f"{core_op.name} needs {core_dims_needed} core_dims, but got {len(core_dims)}" |
| 37 | + ) |
| 38 | + |
| 39 | + full_input_core_dims = tuple( |
| 40 | + tuple(core_dims[i] for i in inp_dims_map) |
| 41 | + for inp_dims_map in core_inps_dims_map |
| 42 | + ) |
| 43 | + full_output_core_dims = (tuple(core_dims[i] for i in core_out_dims_map),) |
| 44 | + full_core_dims = (full_input_core_dims, full_output_core_dims) |
| 45 | + |
| 46 | + if extra_dims is None: |
| 47 | + extra_dims = {} |
| 48 | + |
| 49 | + return XRV(core_op, core_dims=full_core_dims, extra_dims=extra_dims.keys())( |
| 50 | + rng, *extra_dims.values(), *params |
| 51 | + ) |
| 52 | + |
| 53 | + return xrv_constructor |
| 54 | + |
| 55 | + |
| 56 | +bernoulli = _as_xrv(ptr.bernoulli) |
| 57 | +beta = _as_xrv(ptr.beta) |
| 58 | +betabinom = _as_xrv(ptr.betabinom) |
| 59 | +binomial = _as_xrv(ptr.binomial) |
| 60 | +categorical = _as_xrv(ptr.categorical) |
| 61 | +cauchy = _as_xrv(ptr.cauchy) |
| 62 | +dirichlet = _as_xrv(ptr.dirichlet) |
| 63 | +exponential = _as_xrv(ptr.exponential) |
| 64 | +gamma = _as_xrv(ptr._gamma) |
| 65 | +gengamma = _as_xrv(ptr.gengamma) |
| 66 | +geometric = _as_xrv(ptr.geometric) |
| 67 | +gumbel = _as_xrv(ptr.gumbel) |
| 68 | +halfcauchy = _as_xrv(ptr.halfcauchy) |
| 69 | +halfnormal = _as_xrv(ptr.halfnormal) |
| 70 | +hypergeometric = _as_xrv(ptr.hypergeometric) |
| 71 | +integers = _as_xrv(ptr.integers) |
| 72 | +invgamma = _as_xrv(ptr.invgamma) |
| 73 | +laplace = _as_xrv(ptr.laplace) |
| 74 | +logistic = _as_xrv(ptr.logistic) |
| 75 | +lognormal = _as_xrv(ptr.lognormal) |
| 76 | +multinomial = _as_xrv( |
| 77 | + ptr.multinomial, |
| 78 | +) |
| 79 | +multivariate_normal = _as_xrv( |
| 80 | + ptr.multivariate_normal, |
| 81 | +) |
| 82 | +nbinom = negative_binomial = _as_xrv(ptr.negative_binomial) |
| 83 | +normal = _as_xrv(ptr.normal) |
| 84 | +pareto = _as_xrv(ptr.pareto) |
| 85 | +poisson = _as_xrv(ptr.poisson) |
| 86 | +t = _as_xrv(ptr.t) |
| 87 | +triangular = _as_xrv(ptr.triangular) |
| 88 | +truncexpon = _as_xrv(ptr.truncexpon) |
| 89 | +uniform = _as_xrv(ptr.uniform) |
| 90 | +vonmises = _as_xrv(ptr.vonmises) |
| 91 | +wald = _as_xrv(ptr.wald) |
| 92 | +weibull = _as_xrv(ptr.weibull) |
| 93 | + |
| 94 | +# Missing special cases |
| 95 | +# standard_normal |
| 96 | +# chisquare |
| 97 | +# rayleigh |
| 98 | +# multivariate_normal (method) |
| 99 | +# choice |
| 100 | +# permutation |
0 commit comments