-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
161 lines (145 loc) · 4.43 KB
/
__init__.py
File metadata and controls
161 lines (145 loc) · 4.43 KB
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
from typing import Any, Callable, List, Dict
import warnings
import numpy as np
from onnx import TensorProto
from .._helpers import np_dtype_to_tensor_dtype
from ..npx.npx_types import DType
from ..npx import npx_functions
supported_functions = [
"abs",
"absolute",
"all",
"any",
"arange",
"asarray",
"astype",
"empty",
"equal",
"eye",
"full",
"full_like",
"isdtype",
"isfinite",
"isinf",
"isnan",
"linspace",
"ones",
"ones_like",
"reshape",
"sum",
"take",
"zeros",
"zeros_like",
]
def _finfo(dtype):
"""
Similar to :class:`numpy.finfo`.
"""
dt = dtype.np_dtype if isinstance(dtype, DType) else dtype
res = np.finfo(dt)
d = {}
for k, v in res.__dict__.items():
if k.startswith("__"):
continue
if isinstance(v, (np.float32, np.float64, np.float16)):
d[k] = float(v)
elif isinstance(v, (np.complex128, np.complex64)):
d[k] = complex(v)
else:
d[k] = v
d["dtype"] = DType(np_dtype_to_tensor_dtype(dt))
nres = type("finfo", (res.__class__,), d)
setattr(nres, "smallest_normal", float(res.smallest_normal)) # noqa: B010
setattr(nres, "tiny", float(res.tiny)) # noqa: B010
return nres
def _iinfo(dtype):
"""
Similar to :class:`numpy.finfo`.
"""
dt = dtype.np_dtype if isinstance(dtype, DType) else dtype
res = np.iinfo(dt)
d = {}
for k, v in res.__dict__.items():
if k.startswith("__"):
continue
if isinstance(
v,
(
np.int16,
np.int32,
np.int64,
np.uint16,
np.uint32,
np.uint64,
np.int8,
np.uint8,
),
):
d[k] = int(v)
else:
d[k] = v
d["dtype"] = DType(np_dtype_to_tensor_dtype(dt))
nres = type("iinfo", (res.__class__,), d)
setattr(nres, "min", int(res.min)) # noqa: B010
setattr(nres, "max", int(res.max)) # noqa: B010
return nres
def array_api_wrap_function(f: Callable, TEagerTensor: type) -> Callable:
"""
Converts an eager function takeing EagerTensor into a function
available through an Array API.
:param callable: function
:param TEagerTensor: EagerTensor class
:return: new function
"""
def wrap(*args: List[Any], **kwargs: Dict[str, Any]) -> Any:
new_args = []
for a in args:
if isinstance(a, np.ndarray):
b = TEagerTensor(a)
else:
b = a
new_args.append(b)
res = f(TEagerTensor, *new_args, **kwargs)
return res
wrap.__doc__ = f.__doc__
return wrap
def _finalize_array_api(module, function_names, TEagerTensor):
"""
Adds common attributes to Array API defined in this modules
such as types.
"""
from . import _onnx_common
module.float16 = DType(TensorProto.FLOAT16)
module.float32 = DType(TensorProto.FLOAT)
module.float64 = DType(TensorProto.DOUBLE)
module.complex64 = DType(TensorProto.COMPLEX64)
module.complex128 = DType(TensorProto.COMPLEX128)
module.int8 = DType(TensorProto.INT8)
module.int16 = DType(TensorProto.INT16)
module.int32 = DType(TensorProto.INT32)
module.int64 = DType(TensorProto.INT64)
module.uint8 = DType(TensorProto.UINT8)
module.uint16 = DType(TensorProto.UINT16)
module.uint32 = DType(TensorProto.UINT32)
module.uint64 = DType(TensorProto.UINT64)
module.bfloat16 = DType(TensorProto.BFLOAT16)
setattr(module, "bool", DType(TensorProto.BOOL)) # noqa: B010
setattr(module, "str", DType(TensorProto.STRING)) # noqa: B010
setattr(module, "finfo", _finfo) # noqa: B010
setattr(module, "iinfo", _iinfo) # noqa: B010
if function_names is None:
function_names = supported_functions
for name in function_names:
f = getattr(_onnx_common, name, None)
if f is None:
f2 = getattr(npx_functions, name, None)
if f2 is None:
warnings.warn(
f"Function {name!r} is not available in {module!r}.",
stacklevel=0,
)
continue
f = lambda TEagerTensor, *args, _f=f2, **kwargs: _f( # noqa: E731
*args, **kwargs
)
setattr(module, name, array_api_wrap_function(f, TEagerTensor))