|
| 1 | +import typing |
| 2 | +from collections.abc import Sequence |
| 3 | +from functools import partial |
| 4 | +from types import EllipsisType |
| 5 | + |
| 6 | +import pytensor.scalar as ps |
| 7 | +from pytensor.graph.basic import Apply |
| 8 | +from pytensor.tensor.math import variadic_mul |
| 9 | +from pytensor.xtensor.basic import XOp |
| 10 | +from pytensor.xtensor.math import neq, sqrt |
| 11 | +from pytensor.xtensor.math import sqr as square |
| 12 | +from pytensor.xtensor.type import as_xtensor, xtensor |
| 13 | + |
| 14 | + |
| 15 | +REDUCE_DIM = str | Sequence[str] | EllipsisType | None |
| 16 | + |
| 17 | + |
| 18 | +class XReduce(XOp): |
| 19 | + __slots__ = ("binary_op", "dims") |
| 20 | + |
| 21 | + def __init__(self, binary_op, dims: Sequence[str]): |
| 22 | + super().__init__() |
| 23 | + self.binary_op = binary_op |
| 24 | + # Order of reduce dims doesn't change the behavior of the Op |
| 25 | + self.dims = tuple(sorted(dims)) |
| 26 | + |
| 27 | + def make_node(self, x): |
| 28 | + x = as_xtensor(x) |
| 29 | + x_dims = x.type.dims |
| 30 | + x_dims_set = set(x_dims) |
| 31 | + reduce_dims_set = set(self.dims) |
| 32 | + if x_dims_set == reduce_dims_set: |
| 33 | + out_dims, out_shape = [], [] |
| 34 | + else: |
| 35 | + if not reduce_dims_set.issubset(x_dims_set): |
| 36 | + raise ValueError( |
| 37 | + f"Reduced dims {self.dims} not found in array dimensions {x_dims}." |
| 38 | + ) |
| 39 | + out_dims, out_shape = zip( |
| 40 | + *[ |
| 41 | + (d, s) |
| 42 | + for d, s in zip(x_dims, x.type.shape) |
| 43 | + if d not in reduce_dims_set |
| 44 | + ] |
| 45 | + ) |
| 46 | + output = xtensor(dtype=x.type.dtype, shape=out_shape, dims=out_dims) |
| 47 | + return Apply(self, [x], [output]) |
| 48 | + |
| 49 | + |
| 50 | +def _process_user_dims(x, dim: REDUCE_DIM) -> Sequence[str]: |
| 51 | + if isinstance(dim, str): |
| 52 | + return (dim,) |
| 53 | + elif dim is None or dim is Ellipsis: |
| 54 | + x = as_xtensor(x) |
| 55 | + return typing.cast(tuple[str], x.type.dims) |
| 56 | + return dim |
| 57 | + |
| 58 | + |
| 59 | +def reduce(x, dim: REDUCE_DIM = None, *, binary_op): |
| 60 | + dims = _process_user_dims(x, dim) |
| 61 | + return XReduce(binary_op=binary_op, dims=dims)(x) |
| 62 | + |
| 63 | + |
| 64 | +sum = partial(reduce, binary_op=ps.add) |
| 65 | +prod = partial(reduce, binary_op=ps.mul) |
| 66 | +max = partial(reduce, binary_op=ps.scalar_maximum) |
| 67 | +min = partial(reduce, binary_op=ps.scalar_minimum) |
| 68 | + |
| 69 | + |
| 70 | +def bool_reduce(x, dim: REDUCE_DIM = None, *, binary_op): |
| 71 | + x = as_xtensor(x) |
| 72 | + if x.type.dtype != "bool": |
| 73 | + x = neq(x, 0) |
| 74 | + return reduce(x, dim=dim, binary_op=binary_op) |
| 75 | + |
| 76 | + |
| 77 | +all = partial(bool_reduce, binary_op=ps.and_) |
| 78 | +any = partial(bool_reduce, binary_op=ps.or_) |
| 79 | + |
| 80 | + |
| 81 | +def _infer_reduced_size(original_var, reduced_var): |
| 82 | + reduced_dims = reduced_var.dims |
| 83 | + return variadic_mul( |
| 84 | + *[size for dim, size in original_var.sizes if dim not in reduced_dims] |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +def mean(x, dim: REDUCE_DIM): |
| 89 | + x = as_xtensor(x) |
| 90 | + sum_x = sum(x, dim) |
| 91 | + n = _infer_reduced_size(x, sum_x) |
| 92 | + return sum_x / n |
| 93 | + |
| 94 | + |
| 95 | +def var(x, dim: REDUCE_DIM, *, ddof: int = 0): |
| 96 | + x = as_xtensor(x) |
| 97 | + x_mean = mean(x, dim) |
| 98 | + n = _infer_reduced_size(x, x_mean) |
| 99 | + return square(x - x_mean) / (n - ddof) |
| 100 | + |
| 101 | + |
| 102 | +def std(x, dim: REDUCE_DIM, *, ddof: int = 0): |
| 103 | + return sqrt(var(x, dim, ddof=ddof)) |
| 104 | + |
| 105 | + |
| 106 | +class XCumReduce(XOp): |
| 107 | + __props__ = ("binary_op", "dims") |
| 108 | + |
| 109 | + def __init__(self, binary_op, dims: Sequence[str]): |
| 110 | + self.binary_op = binary_op |
| 111 | + self.dims = tuple(sorted(dims)) # Order doesn't matter |
| 112 | + |
| 113 | + def make_node(self, x): |
| 114 | + x = as_xtensor(x) |
| 115 | + out = x.type() |
| 116 | + return Apply(self, [x], [out]) |
| 117 | + |
| 118 | + |
| 119 | +def cumreduce(x, dim: REDUCE_DIM, *, binary_op): |
| 120 | + dims = _process_user_dims(x, dim) |
| 121 | + return XCumReduce(dims=dims, binary_op=binary_op)(x) |
| 122 | + |
| 123 | + |
| 124 | +cumsum = partial(cumreduce, binary_op=ps.add) |
| 125 | +cumprod = partial(cumreduce, binary_op=ps.mul) |
0 commit comments