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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357 | class OverrideTensor(Tensor):
@property
def mT(self):
return self.mod.matrix_transpose(self)
@property
def T(self):
if self.ndim != 2:
raise ValueError("T is only defined for 2D arrays")
return self.mod.matrix_transpose(self)
@property
def mod(self):
return self.override_module()
def override_module(self):
return sys.modules["finch.interface"]
def __array_function__(self, func, types, args, kwargs):
"""Override NumPy functions using the __array_function__ protocol."""
# https://numpy.org/neps/nep-0018-array-function-protocol.html
override_func = getattr(self.mod, func.__name__, None)
if override_func is None:
return NotImplemented
return override_func(*args, **kwargs)
def __array_ufunc__(self, ufunc: np.ufunc, method, *inputs, **kwargs):
"""Override NumPy ufuncs using the __array_ufunc__ protocol."""
# https://numpy.org/devdocs/user/basics.ufuncs.html#ufuncs-basics
# https://numpy.org/devdocs/reference/ufuncs.html#ufuncs-methods
if kwargs.get("out") is not None:
raise NotImplementedError("out parameter is not supported")
if kwargs.get("where") is not None:
raise NotImplementedError("where parameter is not supported")
if kwargs.get("casting") is not None:
raise NotImplementedError("casting parameter is not supported")
if kwargs.get("order") is not None:
raise NotImplementedError("order parameter is not supported")
if kwargs.get("axes") is not None:
kwargs["axis"] = kwargs.pop("axes")
if ufunc in element_wise_ufunc_map:
if method == "__call__":
return self.mod.elementwise(
element_wise_ufunc_map[ufunc], *inputs, **kwargs
)
if method == "reduce":
return self.mod.reduce(ufunc, *inputs, **kwargs)
if ufunc in ufunc_map:
func_name = ufunc_map[ufunc]
if method == "__call__":
return getattr(self.mod, func_name)(*inputs, **kwargs)
return NotImplemented
def __array_namespace__(self, *, api_version=None):
# https://data-apis.org/array-api/latest/API_specification/generated/array_api.array.__array_namespace__.html#array_api.array.__array_namespace__
if api_version is None:
api_version = "2024.12"
if api_version not in {"2024.12"}:
raise ValueError(f'"{api_version}" Array API version not supported.')
return sys.modules["finch"]
def __add__(self, other):
return self.mod.add(self, other)
def __radd__(self, other):
return self.mod.add(other, self)
def __sub__(self, other):
return self.mod.subtract(self, other)
def __rsub__(self, other):
return self.mod.subtract(other, self)
def __mul__(self, other):
return self.mod.multiply(self, other)
def __rmul__(self, other):
return self.mod.multiply(other, self)
def __abs__(self):
return self.mod.abs(self)
def __pos__(self):
return self.mod.positive(self)
def __neg__(self):
return self.mod.negative(self)
def __invert__(self):
return self.mod.bitwise_invert(self)
def __and__(self, other):
return self.mod.bitwise_and(self, other)
def __rand__(self, other):
return self.mod.bitwise_and(other, self)
def __lshift__(self, other):
return self.mod.bitwise_left_shift(self, other)
def __rlshift__(self, other):
return self.mod.bitwise_left_shift(other, self)
def __or__(self, other):
return self.mod.bitwise_or(self, other)
def __ror__(self, other):
return self.mod.bitwise_or(other, self)
def __rshift__(self, other):
return self.mod.bitwise_right_shift(self, other)
def __rrshift__(self, other):
return self.mod.bitwise_right_shift(other, self)
def __xor__(self, other):
return self.mod.bitwise_xor(self, other)
def __rxor__(self, other):
return self.mod.bitwise_xor(other, self)
def __truediv__(self, other):
return self.mod.truediv(self, other)
def __rtruediv__(self, other):
return self.mod.truediv(other, self)
def __floordiv__(self, other):
return self.mod.floor_divide(self, other)
def __rfloordiv__(self, other):
return self.mod.floor_divide(other, self)
def __mod__(self, other):
return self.mod.mod(self, other)
def __rmod__(self, other):
return self.mod.mod(other, self)
def __pow__(self, other):
return self.mod.power(self, other)
def __rpow__(self, other):
return self.mod.power(other, self)
def __matmul__(self, other):
return self.mod.matmul(self, other)
def __rmatmul__(self, other):
return self.mod.matmul(other, self)
def __sin__(self):
return self.mod.sin(self)
def __sinh__(self):
return self.mod.sinh(self)
def __cos__(self):
return self.mod.cos(self)
def __cosh__(self):
return self.mod.cosh(self)
def __tan__(self):
return self.mod.tan(self)
def __tanh__(self):
return self.mod.tanh(self)
def __asin__(self):
return self.mod.asin(self)
def __asinh__(self):
return self.mod.asinh(self)
def __acos__(self):
return self.mod.acos(self)
def __acosh__(self):
return self.mod.acosh(self)
def __atan__(self):
return self.mod.atan(self)
def __atanh__(self):
return self.mod.atanh(self)
def __atan2__(self, other):
return self.mod.atan2(self, other)
def __complex__(self):
"""
Converts a zero-dimensional array to a Python `complex` object.
"""
if self.ndim != 0:
raise ValueError("Cannot convert non-scalar tensor to complex.")
# dispatch to the scalar value's `__complex__` method
return complex(self.item())
def __float__(self):
"""
Converts a zero-dimensional array to a Python `float` object.
"""
if self.ndim != 0:
raise ValueError("Cannot convert non-scalar tensor to float.")
# dispatch to the scalar value's `__float__` method
return float(self.item())
def __int__(self):
"""
Converts a zero-dimensional array to a Python `int` object.
"""
if self.ndim != 0:
raise ValueError("Cannot convert non-scalar tensor to int.")
# dispatch to the scalar value's `__int__` method
return int(self.item())
def __bool__(self):
"""
Converts a zero-dimensional array to a Python `bool` object.
"""
if self.ndim != 0:
raise ValueError("Cannot convert non-scalar tensor to bool.")
# dispatch to the scalar value's `__bool__` method
return bool(self.item())
def __index__(self) -> int:
if self.ndim != 0:
raise ValueError("Cannot convert non-scalar tensor to index.")
return operator.index(self.item())
def __log__(self):
return self.mod.log(self)
def __log1p__(self):
return self.mod.log1p(self)
def __log2__(self):
return self.mod.log2(self)
def __log10__(self):
return self.mod.log10(self)
def __logaddexp__(self, other):
return self.mod.logaddexp(self, other)
def __logical_and__(self, other):
return self.mod.logical_and(self, other)
def __logical_or__(self, other):
return self.mod.logical_or(self, other)
def __logical_xor__(self, other):
return self.mod.logical_xor(self, other)
def __logical_not__(self):
return self.mod.logical_not(self)
def __lt__(self, other):
return self.mod.less(self, other)
def __le__(self, other):
return self.mod.less_equal(self, other)
def __gt__(self, other):
return self.mod.greater(self, other)
def __ge__(self, other):
return self.mod.greater_equal(self, other)
def __eq__(self, other):
return self.mod.equal(self, other)
def __ne__(self, other):
return self.mod.not_equal(self, other)
|