-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path__init__.py
More file actions
248 lines (189 loc) · 7.55 KB
/
Copy path__init__.py
File metadata and controls
248 lines (189 loc) · 7.55 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
"""
.. currentmodule:: arraycontext
A :mod:`numpy`-based array context.
.. autoclass:: NumpyArrayContext
"""
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 typing import TYPE_CHECKING, Any, cast, overload
import numpy as np
from typing_extensions import override
import loopy as lp
from arraycontext.container.traversal import (
rec_map_array_container as rec_map_array_container,
rec_map_container,
with_array_context,
)
from arraycontext.context import (
ArrayContext,
CSRMatrix,
SparseMatrix,
UntransformedCodeWarning,
)
from arraycontext.typing import (
Array,
ArrayOrContainer,
ArrayOrContainerOrScalar,
ArrayOrContainerOrScalarT,
ArrayOrScalar,
ContainerOrScalarT,
NumpyOrContainerOrScalar,
is_scalar_like,
)
if TYPE_CHECKING:
from pymbolic import Scalar
from pytools.tag import Tag, ToTagSetConvertible
from arraycontext.typing import ArrayContainerT
_EMPTY_TAG_SET: frozenset[Tag] = frozenset()
class NumpyNonObjectArrayMetaclass(type):
@override
def __instancecheck__(cls, instance: object) -> bool:
return isinstance(instance, np.ndarray) and instance.dtype != object
class NumpyNonObjectArray(metaclass=NumpyNonObjectArrayMetaclass):
pass
class NumpyArrayContext(ArrayContext):
"""
A :class:`ArrayContext` that uses :class:`numpy.ndarray` to represent arrays.
.. automethod:: __init__
"""
_loopy_transform_cache: dict[lp.TranslationUnit, lp.ExecutorBase]
def __init__(self) -> None:
super().__init__()
self._loopy_transform_cache = {}
array_types = (NumpyNonObjectArray,)
def _get_fake_numpy_namespace(self):
from .fake_numpy import NumpyFakeNumpyNamespace
return NumpyFakeNumpyNamespace(self)
# {{{ ArrayContext interface
@override
def clone(self):
return type(self)()
@overload
def from_numpy(self, array: np.ndarray) -> Array:
...
@overload
def from_numpy(self, array: Scalar) -> Array:
...
@overload
def from_numpy(self, array: ArrayContainerT) -> ArrayContainerT:
...
@override
def from_numpy(self,
array: NumpyOrContainerOrScalar
) -> ArrayOrContainerOrScalar:
if isinstance(array, np.ndarray) or is_scalar_like(array):
return cast("Array", cast("object", np.array(array)))
return array
@overload
def to_numpy(self, array: Array) -> np.ndarray:
...
@overload
def to_numpy(self, array: ContainerOrScalarT) -> ContainerOrScalarT:
...
@override
def to_numpy(self,
array: ArrayOrContainerOrScalar
) -> NumpyOrContainerOrScalar:
return cast("NumpyOrContainerOrScalar", array)
@override
def call_loopy(
self,
t_unit: lp.TranslationUnit, **kwargs: Any
) -> dict[str, Array]:
t_unit = t_unit.copy(target=lp.ExecutableCTarget())
try:
executor = self._loopy_transform_cache[t_unit]
except KeyError:
executor = self.transform_loopy_program(t_unit).executor()
self._loopy_transform_cache[t_unit] = executor
_, result = executor(**kwargs)
return result
@override
def freeze(self, array: ArrayOrContainerOrScalarT) -> ArrayOrContainerOrScalarT:
def _freeze(ary):
return ary
return with_array_context(rec_map_container(_freeze, array), actx=None)
@override
def thaw(self, array: ArrayOrContainerOrScalarT) -> ArrayOrContainerOrScalarT:
def _thaw(ary):
return ary
return with_array_context(rec_map_container(_thaw, array), actx=self)
# }}}
def transform_loopy_program(self, t_unit):
from warnings import warn
warn("Using the base "
f"{type(self).__name__}.transform_loopy_program "
"to transform a translation unit. "
"This is a no-op and will result in unoptimized C code for"
"the requested optimization, all in a single statement."
"This will work, but is unlikely to be performant."
f"Instead, subclass {type(self).__name__} and implement "
"the specific transform logic required to transform the program "
"for your package or application. Check higher-level packages "
"(e.g. meshmode), which may already have subclasses you may want "
"to build on.",
UntransformedCodeWarning, stacklevel=2)
return t_unit
def tag(self,
tags: ToTagSetConvertible,
array: ArrayOrContainerOrScalarT) -> ArrayOrContainerOrScalarT:
# Numpy doesn't support tagging
return array
def tag_axis(self,
iaxis: int, tags: ToTagSetConvertible,
array: ArrayOrContainerOrScalarT) -> ArrayOrContainerOrScalarT:
# Numpy doesn't support tagging
return array
def einsum(self, spec, *args, arg_names=None, tagged=()):
return np.einsum(spec, *args, optimize="optimal")
@override
def sparse_matmul(
self, x1: SparseMatrix, x2: ArrayOrContainer) -> ArrayOrContainer:
if isinstance(x1, CSRMatrix):
assert isinstance(x1.elem_values, np.ndarray)
assert isinstance(x1.elem_col_indices, np.ndarray)
assert isinstance(x1.row_starts, np.ndarray)
try:
from scipy.sparse import csr_matrix
except ImportError as exc:
raise ImportError(
"sparse_matmul requires SciPy, which is not installed. "
"Install the 'arraycontext[sparse]' extra to enable sparse "
"matrix support.") from exc
np_matrix = csr_matrix(
(x1.elem_values, x1.elem_col_indices, x1.row_starts),
shape=x1.shape)
def _matmul(ary: ArrayOrScalar) -> ArrayOrScalar:
assert isinstance(ary, np.ndarray)
return cast("ArrayOrScalar", cast("object", np_matrix @ ary))
return cast("ArrayOrContainer", rec_map_container(_matmul, x2))
else:
raise TypeError(f"unrecognized sparse matrix type '{type(x1).__name__}'")
@property
def permits_inplace_modification(self):
return True
@property
def supports_nonscalar_broadcasting(self):
return True
@property
def permits_advanced_indexing(self):
return True