|
| 1 | +from collections.abc import Sequence |
| 2 | + |
| 3 | +from pytensor.graph import Apply, Op |
| 4 | +from pytensor.tensor.type import TensorType |
| 5 | +from pytensor.xtensor.type import XTensorType, as_xtensor, xtensor |
| 6 | + |
| 7 | + |
| 8 | +class XOp(Op): |
| 9 | + """A base class for XOps that shouldn't be materialized""" |
| 10 | + |
| 11 | + def perform(self, node, inputs, outputs): |
| 12 | + raise NotImplementedError( |
| 13 | + f"xtensor operation {self} must be lowered to equivalent tensor operations" |
| 14 | + ) |
| 15 | + |
| 16 | + |
| 17 | +class XViewOp(Op): |
| 18 | + # Make this a View Op with C-implementation |
| 19 | + view_map = {0: [0]} |
| 20 | + |
| 21 | + def perform(self, node, inputs, output_storage): |
| 22 | + output_storage[0][0] = inputs[0] |
| 23 | + |
| 24 | + |
| 25 | +class TensorFromXTensor(XViewOp): |
| 26 | + __props__ = () |
| 27 | + |
| 28 | + def make_node(self, x) -> Apply: |
| 29 | + if not isinstance(x.type, XTensorType): |
| 30 | + raise TypeError(f"x must be have an XTensorType, got {type(x.type)}") |
| 31 | + output = TensorType(x.type.dtype, shape=x.type.shape)() |
| 32 | + return Apply(self, [x], [output]) |
| 33 | + |
| 34 | + |
| 35 | +tensor_from_xtensor = TensorFromXTensor() |
| 36 | + |
| 37 | + |
| 38 | +class XTensorFromTensor(XViewOp): |
| 39 | + __props__ = ("dims",) |
| 40 | + |
| 41 | + def __init__(self, dims: Sequence[str]): |
| 42 | + super().__init__() |
| 43 | + self.dims = tuple(dims) |
| 44 | + |
| 45 | + def make_node(self, x) -> Apply: |
| 46 | + if not isinstance(x.type, TensorType): |
| 47 | + raise TypeError(f"x must be an TensorType type, got {type(x.type)}") |
| 48 | + output = xtensor(dtype=x.type.dtype, dims=self.dims, shape=x.type.shape) |
| 49 | + return Apply(self, [x], [output]) |
| 50 | + |
| 51 | + |
| 52 | +def xtensor_from_tensor(x, dims): |
| 53 | + return XTensorFromTensor(dims=dims)(x) |
| 54 | + |
| 55 | + |
| 56 | +class Rename(XViewOp): |
| 57 | + __props__ = ("new_dims",) |
| 58 | + |
| 59 | + def __init__(self, new_dims: tuple[str, ...]): |
| 60 | + super().__init__() |
| 61 | + self.new_dims = new_dims |
| 62 | + |
| 63 | + def make_node(self, x): |
| 64 | + x = as_xtensor(x) |
| 65 | + output = x.type.clone(dims=self.new_dims)() |
| 66 | + return Apply(self, [x], [output]) |
| 67 | + |
| 68 | + |
| 69 | +def rename(x, name_dict: dict[str, str] | None = None, **names: str): |
| 70 | + if name_dict is not None: |
| 71 | + if names: |
| 72 | + raise ValueError("Cannot use both positional and keyword names in rename") |
| 73 | + names = name_dict |
| 74 | + |
| 75 | + x = as_xtensor(x) |
| 76 | + old_names = x.type.dims |
| 77 | + new_names = list(old_names) |
| 78 | + for old_name, new_name in names.items(): |
| 79 | + try: |
| 80 | + new_names[old_names.index(old_name)] = new_name |
| 81 | + except IndexError: |
| 82 | + raise ValueError( |
| 83 | + f"Cannot rename {old_name} to {new_name}: {old_name} not in {old_names}" |
| 84 | + ) |
| 85 | + |
| 86 | + return Rename(tuple(new_names))(x) |
0 commit comments