|
1 |
| -from ._decorators import deco_binary_ufunc_from_impl |
2 |
| -from ._detail import _ufunc_impl |
| 1 | +from typing import Optional |
| 2 | + |
| 3 | +from . import _helpers |
| 4 | +from ._detail import _binary_ufuncs |
| 5 | +from ._normalizations import ArrayLike, DTypeLike, NDArray, SubokLike, normalizer |
| 6 | + |
| 7 | +__all__ = [ |
| 8 | + name for name in dir(_binary_ufuncs) if not name.startswith("_") and name != "torch" |
| 9 | +] |
| 10 | + |
| 11 | + |
| 12 | +def deco_binary_ufunc(torch_func): |
| 13 | + """Common infra for binary ufuncs. |
| 14 | +
|
| 15 | + Normalize arguments, sort out type casting, broadcasting and delegate to |
| 16 | + the pytorch functions for the actual work. |
| 17 | + """ |
| 18 | + |
| 19 | + def wrapped( |
| 20 | + x1: ArrayLike, |
| 21 | + x2: ArrayLike, |
| 22 | + /, |
| 23 | + out: Optional[NDArray] = None, |
| 24 | + *, |
| 25 | + where=True, |
| 26 | + casting="same_kind", |
| 27 | + order="K", |
| 28 | + dtype: DTypeLike = None, |
| 29 | + subok: SubokLike = False, |
| 30 | + signature=None, |
| 31 | + extobj=None, |
| 32 | + ): |
| 33 | + tensors = _helpers.ufunc_preprocess( |
| 34 | + (x1, x2), out, where, casting, order, dtype, subok, signature, extobj |
| 35 | + ) |
| 36 | + result = torch_func(*tensors) |
| 37 | + return _helpers.result_or_out(result, out) |
| 38 | + |
| 39 | + return wrapped |
| 40 | + |
3 | 41 |
|
4 | 42 | #
|
5 |
| -# Functions in this file implement binary ufuncs: wrap two first arguments in |
6 |
| -# asarray and delegate to functions from _ufunc_impl. |
7 |
| -# |
8 |
| -# Functions in _detail/_ufunc_impl.py receive tensors, implement common tasks |
9 |
| -# with ufunc args, and delegate heavy lifting to pytorch equivalents. |
| 43 | +# For each torch ufunc implementation, decorate and attach the decorated name |
| 44 | +# to this module. Its contents is then exported to the public namespace in __init__.py |
10 | 45 | #
|
| 46 | +for name in __all__: |
| 47 | + ufunc = getattr(_binary_ufuncs, name) |
| 48 | + decorated = normalizer(deco_binary_ufunc(ufunc)) |
11 | 49 |
|
12 |
| -# the list is autogenerated, cf autogen/gen_ufunc_2.py |
13 |
| -add = deco_binary_ufunc_from_impl(_ufunc_impl.add) |
14 |
| -arctan2 = deco_binary_ufunc_from_impl(_ufunc_impl.arctan2) |
15 |
| -bitwise_and = deco_binary_ufunc_from_impl(_ufunc_impl.bitwise_and) |
16 |
| -bitwise_or = deco_binary_ufunc_from_impl(_ufunc_impl.bitwise_or) |
17 |
| -bitwise_xor = deco_binary_ufunc_from_impl(_ufunc_impl.bitwise_xor) |
18 |
| -copysign = deco_binary_ufunc_from_impl(_ufunc_impl.copysign) |
19 |
| -divide = deco_binary_ufunc_from_impl(_ufunc_impl.divide) |
20 |
| -equal = deco_binary_ufunc_from_impl(_ufunc_impl.equal) |
21 |
| -float_power = deco_binary_ufunc_from_impl(_ufunc_impl.float_power) |
22 |
| -floor_divide = deco_binary_ufunc_from_impl(_ufunc_impl.floor_divide) |
23 |
| -fmax = deco_binary_ufunc_from_impl(_ufunc_impl.fmax) |
24 |
| -fmin = deco_binary_ufunc_from_impl(_ufunc_impl.fmin) |
25 |
| -fmod = deco_binary_ufunc_from_impl(_ufunc_impl.fmod) |
26 |
| -gcd = deco_binary_ufunc_from_impl(_ufunc_impl.gcd) |
27 |
| -greater = deco_binary_ufunc_from_impl(_ufunc_impl.greater) |
28 |
| -greater_equal = deco_binary_ufunc_from_impl(_ufunc_impl.greater_equal) |
29 |
| -heaviside = deco_binary_ufunc_from_impl(_ufunc_impl.heaviside) |
30 |
| -hypot = deco_binary_ufunc_from_impl(_ufunc_impl.hypot) |
31 |
| -lcm = deco_binary_ufunc_from_impl(_ufunc_impl.lcm) |
32 |
| -ldexp = deco_binary_ufunc_from_impl(_ufunc_impl.ldexp) |
33 |
| -left_shift = deco_binary_ufunc_from_impl(_ufunc_impl.left_shift) |
34 |
| -less = deco_binary_ufunc_from_impl(_ufunc_impl.less) |
35 |
| -less_equal = deco_binary_ufunc_from_impl(_ufunc_impl.less_equal) |
36 |
| -logaddexp = deco_binary_ufunc_from_impl(_ufunc_impl.logaddexp) |
37 |
| -logaddexp2 = deco_binary_ufunc_from_impl(_ufunc_impl.logaddexp2) |
38 |
| -logical_and = deco_binary_ufunc_from_impl(_ufunc_impl.logical_and) |
39 |
| -logical_or = deco_binary_ufunc_from_impl(_ufunc_impl.logical_or) |
40 |
| -logical_xor = deco_binary_ufunc_from_impl(_ufunc_impl.logical_xor) |
41 |
| -matmul = deco_binary_ufunc_from_impl(_ufunc_impl.matmul) |
42 |
| -maximum = deco_binary_ufunc_from_impl(_ufunc_impl.maximum) |
43 |
| -minimum = deco_binary_ufunc_from_impl(_ufunc_impl.minimum) |
44 |
| -remainder = deco_binary_ufunc_from_impl(_ufunc_impl.remainder) |
45 |
| -multiply = deco_binary_ufunc_from_impl(_ufunc_impl.multiply) |
46 |
| -nextafter = deco_binary_ufunc_from_impl(_ufunc_impl.nextafter) |
47 |
| -not_equal = deco_binary_ufunc_from_impl(_ufunc_impl.not_equal) |
48 |
| -power = deco_binary_ufunc_from_impl(_ufunc_impl.power) |
49 |
| -remainder = deco_binary_ufunc_from_impl(_ufunc_impl.remainder) |
50 |
| -right_shift = deco_binary_ufunc_from_impl(_ufunc_impl.right_shift) |
51 |
| -subtract = deco_binary_ufunc_from_impl(_ufunc_impl.subtract) |
52 |
| -divide = deco_binary_ufunc_from_impl(_ufunc_impl.divide) |
| 50 | + decorated.__qualname__ = name # XXX: is this really correct? |
| 51 | + decorated.__name__ = name |
| 52 | + vars()[name] = decorated |
0 commit comments