|
| 1 | +from typing import Iterable, Optional, Union, Sequence, TypeVar |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +import pytensor |
| 6 | +from pytensor import scalar as aes |
| 7 | +from pytensor.graph.basic import Variable |
| 8 | +from pytensor.graph.type import HasDataType |
| 9 | +from pytensor.tensor.type import TensorType |
| 10 | + |
| 11 | + |
| 12 | +_XTensorTypeType = TypeVar("_XTensorTypeType", bound=TensorType) |
| 13 | + |
| 14 | + |
| 15 | +class XTensorType(TensorType, HasDataType): |
| 16 | + """A `Type` for sparse tensors. |
| 17 | +
|
| 18 | + Notes |
| 19 | + ----- |
| 20 | + Currently, sparse tensors can only be matrices (i.e. have two dimensions). |
| 21 | +
|
| 22 | + """ |
| 23 | + |
| 24 | + __props__ = ("dtype", "shape", "dims") |
| 25 | + |
| 26 | + def __init__( |
| 27 | + self, |
| 28 | + dtype: Union[str, np.dtype], |
| 29 | + *, |
| 30 | + dims: Sequence[str], |
| 31 | + shape: Optional[Iterable[Optional[Union[bool, int]]]] = None, |
| 32 | + name: Optional[str] = None, |
| 33 | + ): |
| 34 | + super().__init__(dtype, shape=shape, name=name) |
| 35 | + if not isinstance(dims, (list, tuple)): |
| 36 | + raise TypeError("dims must be a list or tuple") |
| 37 | + dims = tuple(dims) |
| 38 | + self.dims = dims |
| 39 | + |
| 40 | + def clone( |
| 41 | + self, |
| 42 | + dtype=None, |
| 43 | + dims=None, |
| 44 | + shape=None, |
| 45 | + **kwargs, |
| 46 | + ): |
| 47 | + if dtype is None: |
| 48 | + dtype = self.dtype |
| 49 | + if dims is None: |
| 50 | + dims = self.dims |
| 51 | + if shape is None: |
| 52 | + shape = self.shape |
| 53 | + return type(self)(format, dtype, shape=shape, dims=dims, **kwargs) |
| 54 | + |
| 55 | + def filter(self, value, strict=False, allow_downcast=None): |
| 56 | + # TODO: Implement this |
| 57 | + return value |
| 58 | + |
| 59 | + if isinstance(value, Variable): |
| 60 | + raise TypeError( |
| 61 | + "Expected an array-like object, but found a Variable: " |
| 62 | + "maybe you are trying to call a function on a (possibly " |
| 63 | + "shared) variable instead of a numeric array?" |
| 64 | + ) |
| 65 | + |
| 66 | + if ( |
| 67 | + isinstance(value, self.format_cls[self.format]) |
| 68 | + and value.dtype == self.dtype |
| 69 | + ): |
| 70 | + return value |
| 71 | + |
| 72 | + if strict: |
| 73 | + raise TypeError( |
| 74 | + f"{value} is not sparse, or not the right dtype (is {value.dtype}, " |
| 75 | + f"expected {self.dtype})" |
| 76 | + ) |
| 77 | + |
| 78 | + # The input format could be converted here |
| 79 | + if allow_downcast: |
| 80 | + sp = self.format_cls[self.format](value, dtype=self.dtype) |
| 81 | + else: |
| 82 | + data = self.format_cls[self.format](value) |
| 83 | + up_dtype = aes.upcast(self.dtype, data.dtype) |
| 84 | + if up_dtype != self.dtype: |
| 85 | + raise TypeError(f"Expected {self.dtype} dtype but got {data.dtype}") |
| 86 | + sp = data.astype(up_dtype) |
| 87 | + |
| 88 | + assert sp.format == self.format |
| 89 | + |
| 90 | + return sp |
| 91 | + |
| 92 | + def convert_variable(self, var): |
| 93 | + # TODO: Implement this |
| 94 | + return var |
| 95 | + res = super().convert_variable(var) |
| 96 | + |
| 97 | + if res is None: |
| 98 | + return res |
| 99 | + |
| 100 | + if not isinstance(res.type, type(self)): |
| 101 | + return None |
| 102 | + |
| 103 | + if res.dims != self.dims: |
| 104 | + # TODO: Does this make sense? |
| 105 | + return None |
| 106 | + |
| 107 | + return res |
| 108 | + |
| 109 | + def __hash__(self): |
| 110 | + return super().__hash__() ^ hash(self.dims) |
| 111 | + |
| 112 | + def __repr__(self): |
| 113 | + # TODO: Add `?` for unknown shapes like `TensorType` does |
| 114 | + return f"XTensorType({self.dtype}, {self.dims}, {self.shape})" |
| 115 | + |
| 116 | + def __eq__(self, other): |
| 117 | + res = super().__eq__(other) |
| 118 | + |
| 119 | + if isinstance(res, bool): |
| 120 | + return res and other.dims == self.dims |
| 121 | + |
| 122 | + return res |
| 123 | + |
| 124 | + def is_super(self, otype): |
| 125 | + # TODO: Implement this |
| 126 | + return True |
| 127 | + |
| 128 | + if not super().is_super(otype): |
| 129 | + return False |
| 130 | + |
| 131 | + if self.dims == otype.dims: |
| 132 | + return True |
| 133 | + |
| 134 | + return False |
| 135 | + |
| 136 | + |
| 137 | +# TODO: Implement creater helper xtensor |
| 138 | + |
| 139 | +pytensor.compile.register_view_op_c_code( |
| 140 | + XTensorType, |
| 141 | + """ |
| 142 | + Py_XDECREF(%(oname)s); |
| 143 | + %(oname)s = %(iname)s; |
| 144 | + Py_XINCREF(%(oname)s); |
| 145 | + """, |
| 146 | + 1, |
| 147 | +) |
0 commit comments