|
| 1 | +import pytensor.scalar as ps |
| 2 | +import pytensor.xtensor as px |
| 3 | +from pytensor.graph import Apply, Op |
| 4 | +from pytensor.tensor import TensorType |
| 5 | + |
| 6 | + |
| 7 | +class TensorFromXTensor(Op): |
| 8 | + # TODO: May need mapping of named dims to positional dims? |
| 9 | + |
| 10 | + def make_node(self, x) -> Apply: |
| 11 | + if not isinstance(x.type, px.XTensorType): |
| 12 | + raise TypeError(f"x must be have an XTensorType, got {type(x.type)}") |
| 13 | + output = TensorType(x.type.dtype, shape=x.type.shape)() |
| 14 | + return Apply(self, [x], [output]) |
| 15 | + |
| 16 | + def perform(self, node, inputs, output_storage) -> None: |
| 17 | + [x] = inputs |
| 18 | + output_storage[0][0] = x.copy() |
| 19 | + |
| 20 | + |
| 21 | +tensor_from_xtensor = TensorFromXTensor() |
| 22 | + |
| 23 | + |
| 24 | +class XTensorFromTensor(Op): |
| 25 | + __props__ = ("dims",) |
| 26 | + |
| 27 | + def __init__(self, dims): |
| 28 | + super().__init__() |
| 29 | + self.dims = dims |
| 30 | + |
| 31 | + def make_node(self, x) -> Apply: |
| 32 | + if not isinstance(x.type, TensorType): |
| 33 | + raise TypeError(f"x must be an TensorType type, got {type(x.type)}") |
| 34 | + output = px.XTensorType(x.type.dtype, dims=self.dims, shape=x.type.shape)() |
| 35 | + return Apply(self, [x], [output]) |
| 36 | + |
| 37 | + def perform(self, node, inputs, output_storage) -> None: |
| 38 | + [x] = inputs |
| 39 | + output_storage[0][0] = x.copy() |
| 40 | + |
| 41 | + |
| 42 | +def xtensor_from_tensor(x, dims): |
| 43 | + return XTensorFromTensor(dims=dims)(x) |
| 44 | + |
| 45 | + |
| 46 | +class XElemwise(Op): |
| 47 | + __props__ = ("scalar_op",) |
| 48 | + |
| 49 | + def __init__(self, scalar_op): |
| 50 | + super().__init__() |
| 51 | + self.scalar_op = scalar_op |
| 52 | + |
| 53 | + def make_node(self, *inputs): |
| 54 | + inputs = [px.as_xtensor(inp) for inp in inputs] |
| 55 | + |
| 56 | + # TODO: This ordering is different than what xarray does |
| 57 | + unique_dims: dict[str, int | None] = {} |
| 58 | + for inp in inputs: |
| 59 | + for dim, dim_length in zip(inp.type.dims, inp.type.shape): |
| 60 | + if dim not in unique_dims: |
| 61 | + unique_dims[dim] = dim_length |
| 62 | + elif dim_length is not None: |
| 63 | + # Check for conflicting shapes |
| 64 | + if (unique_dims[dim] is not None) and ( |
| 65 | + unique_dims[dim] != dim_length |
| 66 | + ): |
| 67 | + raise ValueError(f"Dimension {dim} has conflicting shapes") |
| 68 | + # Keep the non-None shape |
| 69 | + unique_dims[dim] = dim_length |
| 70 | + |
| 71 | + dims, shape = zip(*sorted(unique_dims.items())) |
| 72 | + |
| 73 | + # TODO: Fix dtype |
| 74 | + output_type = px.XTensorType("float64", dims=dims, shape=shape) |
| 75 | + outputs = [output_type() for _ in range(self.scalar_op.nout)] |
| 76 | + return Apply(self, inputs, outputs) |
| 77 | + |
| 78 | + def perform(self, *args, **kwargs) -> None: |
| 79 | + raise NotImplementedError( |
| 80 | + "xtensor operations must be rewritten as tensor operations" |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +add = XElemwise(ps.add) |
| 85 | +exp = XElemwise(ps.exp) |
0 commit comments