forked from inducer/arraycontext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfake_numpy.py
More file actions
283 lines (230 loc) · 9.11 KB
/
Copy pathfake_numpy.py
File metadata and controls
283 lines (230 loc) · 9.11 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
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
from __future__ import annotations
__copyright__ = """
Copyright (C) 2021 University of Illinois Board of Trustees
"""
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from functools import partial, reduce
from typing import TYPE_CHECKING, cast, overload
import numpy as np
from typing_extensions import override
from arraycontext.container import NotAnArrayContainerError, serialize_container
from arraycontext.container.traversal import (
rec_map_container,
rec_map_reduce_array_container,
rec_multimap_array_container,
rec_multimap_reduce_array_container,
)
from arraycontext.fake_numpy import (
BaseFakeNumpyLinalgNamespace,
BaseFakeNumpyNamespace,
)
from arraycontext.typing import ArrayOrContainer, OrderCF, is_scalar_like
if TYPE_CHECKING:
from collections.abc import Callable
from numpy.typing import DTypeLike
from pymbolic import Scalar
from arraycontext.typing import (
Array,
ArrayOrContainerOrScalar,
ArrayOrScalar,
)
class NumpyFakeNumpyLinalgNamespace(BaseFakeNumpyLinalgNamespace):
# Everything is implemented in the base class for now.
pass
_NUMPY_UFUNCS = frozenset({"concatenate", "reshape",
"ones_like", "where",
*BaseFakeNumpyNamespace._numpy_math_functions
})
class NumpyFakeNumpyNamespace(BaseFakeNumpyNamespace):
"""
A :mod:`numpy` mimic for :class:`NumpyArrayContext`.
"""
@override
def _get_fake_numpy_linalg_namespace(self):
return NumpyFakeNumpyLinalgNamespace(self._array_context)
@override
def zeros(self, shape: int | tuple[int, ...], dtype: DTypeLike) -> Array:
return cast("Array", cast("object", np.zeros(shape, dtype)))
@override
def _full_like_array(self,
ary: Array,
fill_value: Scalar,
) -> Array:
return cast("Array", cast("object", np.full_like(ary, fill_value)))
def __getattr__(self, name: str):
if name in _NUMPY_UFUNCS:
from functools import partial
return partial(rec_multimap_array_container,
getattr(np, name))
raise AttributeError(name)
@overload
def sum(self,
a: ArrayOrContainer,
axis: int | tuple[int, ...] | None = None,
dtype: DTypeLike = None,
) -> Array: ...
@overload
def sum(self,
a: Scalar,
axis: int | tuple[int, ...] | None = None,
dtype: DTypeLike = None,
) -> Scalar: ...
@override
def sum(self,
a: ArrayOrContainerOrScalar,
axis: int | tuple[int, ...] | None = None,
dtype: DTypeLike = None,
) -> ArrayOrScalar:
return rec_map_reduce_array_container(sum, partial(np.sum,
axis=axis,
dtype=dtype),
a)
@overload
def min(self,
a: ArrayOrContainer,
axis: int | tuple[int, ...] | None = None,
) -> Array: ...
@overload
def min(self,
a: Scalar,
axis: int | tuple[int, ...] | None = None,
) -> Scalar: ...
@override
def min(self,
a: ArrayOrContainerOrScalar,
axis: int | tuple[int, ...] | None = None,
) -> ArrayOrScalar:
return rec_map_reduce_array_container(
partial(reduce, np.minimum), partial(np.amin, axis=axis), a)
@overload
def max(self,
a: ArrayOrContainer,
axis: int | tuple[int, ...] | None = None,
) -> Array: ...
@overload
def max(self,
a: Scalar,
axis: int | tuple[int, ...] | None = None,
) -> Scalar: ...
@override
def max(self,
a: ArrayOrContainerOrScalar,
axis: int | tuple[int, ...] | None = None,
) -> ArrayOrScalar:
return rec_map_reduce_array_container(
partial(reduce, np.maximum), partial(np.amax, axis=axis), a)
def stack(self, arrays, axis=0):
return rec_multimap_array_container(
lambda *args: np.stack(arrays=args, axis=axis),
*arrays)
def broadcast_to(self, array: ArrayOrContainerOrScalar, shape: tuple[int, ...]):
def inner_bcast(ary: ArrayOrScalar) -> ArrayOrScalar:
if is_scalar_like(ary):
return ary
else:
assert isinstance(ary, np.ndarray)
return cast("Array", cast("object", np.broadcast_to(ary, shape)))
return rec_map_container(inner_bcast, array)
# {{{ relational operators
@override
def equal(self, x, y):
return rec_multimap_array_container(np.equal, x, y)
@override
def not_equal(self, x, y):
return rec_multimap_array_container(np.not_equal, x, y)
@override
def greater(self, x, y):
return rec_multimap_array_container(np.greater, x, y)
@override
def greater_equal(self, x, y):
return rec_multimap_array_container(np.greater_equal, x, y)
@override
def less(self, x, y):
return rec_multimap_array_container(np.less, x, y)
@override
def less_equal(self, x, y):
return rec_multimap_array_container(np.less_equal, x, y)
@override
def logical_or(self,
x: ArrayOrContainerOrScalar,
y: ArrayOrContainerOrScalar
) -> Array:
return rec_multimap_array_container(np.logical_or, x, y)
@override
def logical_and(self,
x: ArrayOrContainerOrScalar,
y: ArrayOrContainerOrScalar
) -> Array:
return rec_multimap_array_container(np.logical_and, x, y)
@override
def logical_not(self,
x: ArrayOrContainerOrScalar
) -> ArrayOrContainerOrScalar:
return rec_map_container(
cast("Callable[[ArrayOrScalar], ArrayOrScalar]", np.logical_not), x)
# }}}
@override
def ravel(self, a: ArrayOrContainerOrScalar, order: OrderCF = "C"):
def inner_ravel(ary: ArrayOrScalar) -> ArrayOrScalar:
if is_scalar_like(ary):
return ary
else:
assert isinstance(ary, np.ndarray)
return cast("Array", cast("object", np.ravel(ary, order)))
return rec_map_container(inner_ravel, a)
def vdot(self, x, y):
return rec_multimap_reduce_array_container(sum, np.vdot, x, y)
def any(self, a):
return rec_map_reduce_array_container(partial(reduce, np.logical_or),
lambda subary: np.any(subary), a)
def all(self, a):
return rec_map_reduce_array_container(partial(reduce, np.logical_and),
lambda subary: np.all(subary), a)
@override
def array_equal(self,
a: ArrayOrContainerOrScalar,
b: ArrayOrContainerOrScalar
) -> Array:
false_ary = np.array(False)
true_ary = np.array(True)
if type(a) is not type(b):
return false_ary
try:
serialized_x = serialize_container(a)
serialized_y = serialize_container(b)
except NotAnArrayContainerError:
assert isinstance(a, np.ndarray)
assert isinstance(b, np.ndarray)
return np.array(np.array_equal(a, b))
else:
if len(serialized_x) != len(serialized_y):
return false_ary
return np.logical_and.reduce(
[(true_ary if kx_i == ky_i else false_ary)
and cast("np.ndarray", self.array_equal(x_i, y_i))
for (kx_i, x_i), (ky_i, y_i)
in zip(serialized_x, serialized_y, strict=True)],
initial=true_ary)
@override
def arange(self, *args, **kwargs):
return np.arange(*args, **kwargs)
@override
def linspace(self, *args, **kwargs):
return np.linspace(*args, **kwargs)
# vim: fdm=marker