-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpx_tensors.py
252 lines (213 loc) · 8.88 KB
/
npx_tensors.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
from typing import Any, Union
import numpy as np
from .._helpers import np_dtype_to_tensor_dtype
from .npx_types import DType, ElemType, ParType, TensorType
from .npx_array_api import BaseArrayApi, ArrayApiError
class JitTensor:
"""
Defines a value for a specific jit mode
"""
class EagerTensor(BaseArrayApi):
"""
Defines a value for a specific eager mode.
An eager tensor must overwrite every call to a method listed in class
:class:`BaseArrayApi
<onnx_array_api.npx.npx_array_api.BaseArrayApi>`.
"""
@classmethod
def __class_getitem__(cls, tensor_type: type):
"""
Returns tensor_type.
"""
if not issubclass(tensor_type, TensorType):
raise TypeError(f"Unexpected type {tensor_type!r}.")
return tensor_type
def __iter__(self):
"""
The :epkg:`Array API` does not define this function (2022/12).
This method raises an exception with a better error message.
"""
raise ArrayApiError(
f"Iterators are not implemented in the generic case. "
f"Every function using them cannot be converted into ONNX "
f"(tensors - {type(self)})."
)
@staticmethod
def _op_impl(*inputs, method_name=None):
# avoids circular imports.
from .npx_var import Var
for i, x in enumerate(inputs):
if not isinstance(x, Var):
raise TypeError(f"Input {i} must be a Var not {type(x)}.")
meth = getattr(Var, method_name)
return meth(*inputs)
@staticmethod
def _reduce_impl(x, axes, keepdims=0, method_name=None):
# avoids circular imports.
from .npx_var import Var
if not isinstance(x, Var):
raise TypeError(f"Input 0 must be a Var not {type(x)}.")
meth = getattr(Var, method_name)
return meth(x, axes, keepdims=keepdims)
@staticmethod
def _reduce_impl_noaxes(x, keepdims=0, method_name=None):
# avoids circular imports.
from .npx_var import Var
if not isinstance(x, Var):
raise TypeError(f"Input 0 must be a Var not {type(x)}.")
meth = getattr(Var, method_name)
return meth(x, keepdims=keepdims)
@staticmethod
def _getitem_impl_var(obj, index, method_name=None):
# avoids circular imports.
from .npx_var import Var
if not isinstance(obj, Var):
raise TypeError(f"obj must be a Var not {type(obj)}.")
meth = getattr(Var, method_name)
return meth(obj, index)
@staticmethod
def _astype_impl(
x: TensorType[ElemType.allowed, "T1"], dtype: ParType[DType], method_name=None
) -> TensorType[ElemType.allowed, "T2"]:
if dtype is None:
raise ValueError("dtype cannot be None.")
# avoids circular imports.
from .npx_var import Var
if not isinstance(x, Var):
raise TypeError(f"Input 0 must be a Var not {type(x)}.")
meth = getattr(Var, "astype") # noqa: B009
return meth(x, dtype)
@staticmethod
def _getitem_impl_tuple(obj, index=None, method_name=None):
# avoids circular imports.
from .npx_var import Var
if not isinstance(obj, Var):
raise TypeError(f"obj must be a Var not {type(obj)}.")
meth = getattr(Var, method_name)
return meth(obj, index)
@staticmethod
def _getitem_impl_slice(obj, index=None, method_name=None):
# avoids circular imports.
from .npx_var import Var
if not isinstance(obj, Var):
raise TypeError(f"obj must be a Var not {type(obj)}.")
meth = getattr(Var, method_name)
return meth(obj, index)
def _generic_method_getitem(self, method_name, *args: Any, **kwargs: Any) -> Any:
# avoids circular imports.
from .npx_jit_eager import eager_onnx
if len(args) != 1:
raise ValueError(
f"Unexpected number of argument {len(args)}, it should be one."
)
if isinstance(args[0], tuple):
eag = eager_onnx(
EagerTensor._getitem_impl_tuple, self.__class__, bypass_eager=True
)
res = eag(self, index=args[0], method_name=method_name, already_eager=True)
elif isinstance(args[0], slice):
eag = eager_onnx(
EagerTensor._getitem_impl_slice, self.__class__, bypass_eager=True
)
res = eag(self, index=args[0], method_name=method_name, already_eager=True)
else:
eag = eager_onnx(
EagerTensor._getitem_impl_var, self.__class__, bypass_eager=True
)
res = eag(self, args[0], method_name=method_name, already_eager=True)
if isinstance(res, tuple) and len(res) == 1:
return res[0]
return res
def _generic_method_operator(self, method_name, *args: Any, **kwargs: Any) -> Any:
# avoids circular imports.
from .npx_jit_eager import eager_onnx
if len(args) not in (0, 1):
raise ValueError(
f"An operator must have zero or one argument not {len(args)}."
)
if len(kwargs) not in (0, 1):
raise ValueError(f"Operators do not support parameters {len(kwargs)}.")
# let's cast numpy arrays into constants.
new_args = []
for a in args:
if isinstance(a, np.ndarray):
t = self.__class__(a.astype(self.dtype.np_dtype))
new_args.append(t)
elif isinstance(a, (int, float, bool)):
new_args.append(
self.__class__(np.array([a]).astype(self.dtype.np_dtype))
)
else:
new_args.append(a)
eag = eager_onnx(EagerTensor._op_impl, self.__class__, bypass_eager=True)
res = eag(self, *new_args, method_name=method_name, already_eager=True)
if isinstance(res, tuple) and len(res) == 1:
return res[0]
return res
def _generic_method_reduce(self, method_name, *args: Any, **kwargs: Any) -> Any:
# avoids circular imports.
from .npx_jit_eager import eager_onnx
if len(args) not in (0, 1):
raise ValueError(
f"An operator must have zero or one argument not {len(args)}."
)
if "axis" in kwargs:
axes = kwargs["axis"]
del kwargs["axis"]
else:
axes = None
if axes is None:
eag = eager_onnx(
EagerTensor._reduce_impl_noaxes, self.__class__, bypass_eager=True
)
res = eag(self, method_name=method_name, already_eager=True, **kwargs)
else:
eag = eager_onnx(
EagerTensor._reduce_impl, self.__class__, bypass_eager=True
)
res = eag(self, axes, method_name=method_name, already_eager=True, **kwargs)
if isinstance(res, tuple) and len(res) == 1:
return res[0]
return res
@staticmethod
def _np_dtype_to_tensor_dtype(dtype):
return np_dtype_to_tensor_dtype(dtype)
def _generic_method_astype(
self, method_name, dtype: Union[DType, "Var"], **kwargs: Any
) -> Any:
# avoids circular imports.
from .npx_jit_eager import eager_onnx
from .npx_var import Var
dtype = (
dtype
if isinstance(dtype, (DType, Var))
else self._np_dtype_to_tensor_dtype(dtype)
)
eag = eager_onnx(EagerTensor._astype_impl, self.__class__, bypass_eager=True)
res = eag(self, dtype, method_name=method_name, already_eager=True, **kwargs)
if isinstance(res, tuple) and len(res) == 1:
return res[0]
return res
def generic_method(self, method_name, *args: Any, **kwargs: Any) -> Any:
"""
The method converts the method into an ONNX graph build by the
corresponding method in class Var.
"""
# avoids circular imports.
from .npx_var import Var
if not hasattr(Var, method_name):
raise AttributeError(
f"Class Var does not implement method {method_name!r}. "
f"This method cannot be converted into an ONNX graph."
)
if method_name == "__getitem__":
return self._generic_method_getitem(method_name, *args, **kwargs)
if method_name == "__setitem__":
return BaseArrayApi.generic_method(self, method_name, *args, **kwargs)
if method_name in {"mean", "sum", "min", "max", "prod"}:
return self._generic_method_reduce(method_name, *args, **kwargs)
if method_name == "astype":
return self._generic_method_astype(method_name, *args, **kwargs)
if method_name.startswith("__") and method_name.endswith("__"):
return self._generic_method_operator(method_name, *args, **kwargs)
return BaseArrayApi.generic_method(self, method_name, *args, **kwargs)