From d0ae0ee52a097cc7780ecddaf9dcaa2502d5d6df Mon Sep 17 00:00:00 2001 From: Mit Kotak Date: Thu, 16 Sep 2021 00:54:14 +0000 Subject: [PATCH 1/5] Update arraycontext/loopy.py, arraycontext/fake_numpy.py, arraycontext/impl/pyopencl/fake_numpy.py files --- arraycontext/fake_numpy.py | 27 +--------------- arraycontext/impl/pyopencl/fake_numpy.py | 15 ++++++++- arraycontext/loopy.py | 41 ++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/arraycontext/fake_numpy.py b/arraycontext/fake_numpy.py index cdb9534..8ee331d 100644 --- a/arraycontext/fake_numpy.py +++ b/arraycontext/fake_numpy.py @@ -144,32 +144,7 @@ class BaseFakeNumpyNamespace: _c_to_numpy_arc_functions = {c_name: numpy_name for numpy_name, c_name in _numpy_to_c_arc_functions.items()} - - def __getattr__(self, name): - def loopy_implemented_elwise_func(*args): - actx = self._array_context - prg = _get_scalar_func_loopy_program(actx, - c_name, nargs=len(args), naxes=len(args[0].shape)) - outputs = actx.call_loopy(prg, - **{"inp%d" % i: arg for i, arg in enumerate(args)}) - return outputs["out"] - - if name in self._c_to_numpy_arc_functions: - from warnings import warn - warn(f"'{name}' in ArrayContext.np is deprecated. " - "Use '{c_to_numpy_arc_functions[name]}' as in numpy. " - "The old name will stop working in 2021.", - DeprecationWarning, stacklevel=3) - - # normalize to C names anyway - c_name = self._numpy_to_c_arc_functions.get(name, name) - - # limit which functions we try to hand off to loopy - if name in self._numpy_math_functions: - return multimapped_over_array_containers(loopy_implemented_elwise_func) - else: - raise AttributeError(name) - + def _new_like(self, ary, alloc_like): from numbers import Number diff --git a/arraycontext/impl/pyopencl/fake_numpy.py b/arraycontext/impl/pyopencl/fake_numpy.py index 01054ba..a1f74ea 100644 --- a/arraycontext/impl/pyopencl/fake_numpy.py +++ b/arraycontext/impl/pyopencl/fake_numpy.py @@ -31,6 +31,8 @@ import operator from arraycontext.fake_numpy import \ BaseFakeNumpyNamespace, BaseFakeNumpyLinalgNamespace +from arraycontext.loopy import \ + LoopyBasedFakeNumpyNamespace from arraycontext.container.traversal import ( rec_multimap_array_container, rec_map_array_container, rec_map_reduce_array_container, @@ -45,7 +47,7 @@ except ImportError: # {{{ fake numpy -class PyOpenCLFakeNumpyNamespace(BaseFakeNumpyNamespace): +class PyOpenCLFakeNumpyNamespace(LoopyBasedFakeNumpyNamespace): def _get_fake_numpy_linalg_namespace(self): return _PyOpenCLFakeNumpyLinalgNamespace(self._array_context) @@ -58,6 +60,17 @@ class PyOpenCLFakeNumpyNamespace(BaseFakeNumpyNamespace): # These operations provide access to numpy-style comparisons in that # case. + def __getattr__(self, name): + print(name) + cl_funcs = ["abs", "sin", "cos", "tan", "arcsin", "arccos", "arctan", + "sinh", "cosh", "tanh", "exp", "log", "log10", "isnan", + "sqrt", "exp"] + if name in cl_funcs: + from functools import partial + return partial(rec_map_array_container, getattr(cl, name)) + + return super().__getattr__(name) + def equal(self, x, y): return rec_multimap_array_container(operator.eq, x, y) diff --git a/arraycontext/loopy.py b/arraycontext/loopy.py index f4c9775..6358c8b 100644 --- a/arraycontext/loopy.py +++ b/arraycontext/loopy.py @@ -29,6 +29,7 @@ THE SOFTWARE. import loopy as lp from loopy.version import MOST_RECENT_LANGUAGE_VERSION +from arraycontext.fake_numpy import BaseFakeNumpyNamespace # {{{ loopy @@ -68,6 +69,46 @@ def get_default_entrypoint(t_unit): raise TypeError("unable to find default entry point for loopy " "translation unit") +class LoopyBasedFakeNumpyNamespace(BaseFakeNumpyNamespace): + _numpy_to_c_arc_functions = { + "arcsin": "asin", + "arccos": "acos", + "arctan": "atan", + "arctan2": "atan2", + + "arcsinh": "asinh", + "arccosh": "acosh", + "arctanh": "atanh", + } + + _c_to_numpy_arc_functions = {c_name: numpy_name + for numpy_name, c_name in _numpy_to_c_arc_functions.items()} + + def __getattr__(self, name): + def loopy_implemented_elwise_func(*args): + actx = self._array_context + prg = _get_scalar_func_loopy_program(actx, + c_name, nargs=len(args), naxes=len(args[0].shape)) + outputs = actx.call_loopy(prg, + **{"inp%d" % i: arg for i, arg in enumerate(args)}) + return outputs["out"] + + if name in self._c_to_numpy_arc_functions: + from warnings import warn + warn(f"'{name}' in ArrayContext.np is deprecated. " + "Use '{c_to_numpy_arc_functions[name]}' as in numpy. " + "The old name will stop working in 2022.", + DeprecationWarning, stacklevel=3) + + # normalize to C names anyway + c_name = self._numpy_to_c_arc_functions.get(name, name) + + # limit which functions we try to hand off to loopy + if name in self._numpy_math_functions: + return multimapped_over_array_containers(loopy_implemented_elwise_func) + else: + raise AttributeError(name) + # }}} -- GitLab From bf4ed7d622cad043556029a790e08059aa2aa330 Mon Sep 17 00:00:00 2001 From: Mit Kotak Date: Thu, 16 Sep 2021 01:08:44 +0000 Subject: [PATCH 2/5] Resolved Flake8 Errors --- arraycontext/fake_numpy.py | 4 +-- arraycontext/impl/pyopencl/fake_numpy.py | 2 +- arraycontext/loopy.py | 34 +++++++++++++++++++++++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/arraycontext/fake_numpy.py b/arraycontext/fake_numpy.py index 8ee331d..72a6a48 100644 --- a/arraycontext/fake_numpy.py +++ b/arraycontext/fake_numpy.py @@ -25,8 +25,7 @@ THE SOFTWARE. import numpy as np from arraycontext.container import is_array_container, serialize_container -from arraycontext.container.traversal import ( - rec_map_array_container, multimapped_over_array_containers) +from arraycontext.container.traversal import rec_map_array_container from pytools import memoize_in @@ -144,7 +143,6 @@ class BaseFakeNumpyNamespace: _c_to_numpy_arc_functions = {c_name: numpy_name for numpy_name, c_name in _numpy_to_c_arc_functions.items()} - def _new_like(self, ary, alloc_like): from numbers import Number diff --git a/arraycontext/impl/pyopencl/fake_numpy.py b/arraycontext/impl/pyopencl/fake_numpy.py index a1f74ea..c82d782 100644 --- a/arraycontext/impl/pyopencl/fake_numpy.py +++ b/arraycontext/impl/pyopencl/fake_numpy.py @@ -30,7 +30,7 @@ from functools import partial, reduce import operator from arraycontext.fake_numpy import \ - BaseFakeNumpyNamespace, BaseFakeNumpyLinalgNamespace + BaseFakeNumpyLinalgNamespace from arraycontext.loopy import \ LoopyBasedFakeNumpyNamespace from arraycontext.container.traversal import ( diff --git a/arraycontext/loopy.py b/arraycontext/loopy.py index 6358c8b..a8f0690 100644 --- a/arraycontext/loopy.py +++ b/arraycontext/loopy.py @@ -30,7 +30,7 @@ THE SOFTWARE. import loopy as lp from loopy.version import MOST_RECENT_LANGUAGE_VERSION from arraycontext.fake_numpy import BaseFakeNumpyNamespace - +from arraycontext.container.traversal import multimapped_over_array_containers # {{{ loopy @@ -69,6 +69,38 @@ def get_default_entrypoint(t_unit): raise TypeError("unable to find default entry point for loopy " "translation unit") +def _get_scalar_func_loopy_program(actx, c_name, nargs, naxes): + @memoize_in(actx, _get_scalar_func_loopy_program) + def get(c_name, nargs, naxes): + from pymbolic import var + + var_names = ["i%d" % i for i in range(naxes)] + size_names = ["n%d" % i for i in range(naxes)] + subscript = tuple(var(vname) for vname in var_names) + from islpy import make_zero_and_vars + v = make_zero_and_vars(var_names, params=size_names) + domain = v[0].domain() + for vname, sname in zip(var_names, size_names): + domain = domain & v[0].le_set(v[vname]) & v[vname].lt_set(v[sname]) + + domain_bset, = domain.get_basic_sets() + + import loopy as lp + from .loopy import make_loopy_program + from arraycontext.transform_metadata import ElementwiseMapKernelTag + return make_loopy_program( + [domain_bset], + [ + lp.Assignment( + var("out")[subscript], + var(c_name)(*[ + var("inp%d" % i)[subscript] for i in range(nargs)])) + ], + name="actx_special_%s" % c_name, + tags=(ElementwiseMapKernelTag(),)) + + return get(c_name, nargs, naxes) + class LoopyBasedFakeNumpyNamespace(BaseFakeNumpyNamespace): _numpy_to_c_arc_functions = { "arcsin": "asin", -- GitLab From cb1c1d24c1223cfdaff7daaef665aff94071e060 Mon Sep 17 00:00:00 2001 From: Mit Kotak Date: Thu, 16 Sep 2021 01:31:38 +0000 Subject: [PATCH 3/5] Removed spaces --- arraycontext/fake_numpy.py | 1 + arraycontext/loopy.py | 3 +++ 2 files changed, 4 insertions(+) diff --git a/arraycontext/fake_numpy.py b/arraycontext/fake_numpy.py index 72a6a48..900b211 100644 --- a/arraycontext/fake_numpy.py +++ b/arraycontext/fake_numpy.py @@ -143,6 +143,7 @@ class BaseFakeNumpyNamespace: _c_to_numpy_arc_functions = {c_name: numpy_name for numpy_name, c_name in _numpy_to_c_arc_functions.items()} + def _new_like(self, ary, alloc_like): from numbers import Number diff --git a/arraycontext/loopy.py b/arraycontext/loopy.py index a8f0690..37ce9de 100644 --- a/arraycontext/loopy.py +++ b/arraycontext/loopy.py @@ -31,6 +31,7 @@ import loopy as lp from loopy.version import MOST_RECENT_LANGUAGE_VERSION from arraycontext.fake_numpy import BaseFakeNumpyNamespace from arraycontext.container.traversal import multimapped_over_array_containers +from pytools import memoize_in # {{{ loopy @@ -69,6 +70,7 @@ def get_default_entrypoint(t_unit): raise TypeError("unable to find default entry point for loopy " "translation unit") + def _get_scalar_func_loopy_program(actx, c_name, nargs, naxes): @memoize_in(actx, _get_scalar_func_loopy_program) def get(c_name, nargs, naxes): @@ -101,6 +103,7 @@ def _get_scalar_func_loopy_program(actx, c_name, nargs, naxes): return get(c_name, nargs, naxes) + class LoopyBasedFakeNumpyNamespace(BaseFakeNumpyNamespace): _numpy_to_c_arc_functions = { "arcsin": "asin", -- GitLab From b635cb6b2ce47ace00b1527700daa4f63dbe27ca Mon Sep 17 00:00:00 2001 From: Mit Kotak Date: Thu, 16 Sep 2021 15:45:35 +0000 Subject: [PATCH 4/5] Fixed typo self.arraycontext_-> self._arraycontext + Pytato derives from LoopyBasedFakeNumpyNamespace --- arraycontext/fake_numpy.py | 4 ++-- arraycontext/impl/pytato/fake_numpy.py | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/arraycontext/fake_numpy.py b/arraycontext/fake_numpy.py index 900b211..3d39010 100644 --- a/arraycontext/fake_numpy.py +++ b/arraycontext/fake_numpy.py @@ -69,8 +69,8 @@ def _get_scalar_func_loopy_program(actx, c_name, nargs, naxes): # {{{ BaseFakeNumpyNamespace class BaseFakeNumpyNamespace: - def __init__(self, array_context): - self._array_context = array_context + def __init__(self, _array_context): + self._array_context = _array_context self.linalg = self._get_fake_numpy_linalg_namespace() def _get_fake_numpy_linalg_namespace(self): diff --git a/arraycontext/impl/pytato/fake_numpy.py b/arraycontext/impl/pytato/fake_numpy.py index f17a4ab..05f87ce 100644 --- a/arraycontext/impl/pytato/fake_numpy.py +++ b/arraycontext/impl/pytato/fake_numpy.py @@ -23,9 +23,10 @@ THE SOFTWARE. """ from functools import partial, reduce -from arraycontext.fake_numpy import ( - BaseFakeNumpyNamespace, BaseFakeNumpyLinalgNamespace, - ) +from arraycontext.fake_numpy import \ + BaseFakeNumpyLinalgNamespace +from arraycontext.loopy import \ + LoopyBasedFakeNumpyNamespace from arraycontext.container.traversal import ( rec_multimap_array_container, rec_map_array_container, rec_map_reduce_array_container, @@ -38,7 +39,7 @@ class PytatoFakeNumpyLinalgNamespace(BaseFakeNumpyLinalgNamespace): pass -class PytatoFakeNumpyNamespace(BaseFakeNumpyNamespace): +class PytatoFakeNumpyNamespace(LoopyBasedFakeNumpyNamespace): """ A :mod:`numpy` mimic for :class:`PytatoPyOpenCLArrayContext`. -- GitLab From 9bcfc3c0691b56f6117ca617fccb16efe0f23844 Mon Sep 17 00:00:00 2001 From: Mit Kotak Date: Thu, 16 Sep 2021 15:54:31 +0000 Subject: [PATCH 5/5] Fixed one more self.arraycontext_-> self._arraycontext_ --- arraycontext/fake_numpy.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arraycontext/fake_numpy.py b/arraycontext/fake_numpy.py index 3d39010..b9784e1 100644 --- a/arraycontext/fake_numpy.py +++ b/arraycontext/fake_numpy.py @@ -69,12 +69,12 @@ def _get_scalar_func_loopy_program(actx, c_name, nargs, naxes): # {{{ BaseFakeNumpyNamespace class BaseFakeNumpyNamespace: - def __init__(self, _array_context): - self._array_context = _array_context + def __init__(self, array_context): + self._array_context = array_context self.linalg = self._get_fake_numpy_linalg_namespace() def _get_fake_numpy_linalg_namespace(self): - return BaseFakeNumpyLinalgNamespace(self.array_context) + return BaseFakeNumpyLinalgNamespace(self._array_context) _numpy_math_functions = frozenset({ # https://numpy.org/doc/stable/reference/routines.math.html -- GitLab