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