From e55454896026f22424b99b6eeed3878ae1f1e960 Mon Sep 17 00:00:00 2001 From: Tyler Reddy Date: Thu, 29 Dec 2022 12:10:00 -0700 Subject: [PATCH 1/2] ENH: greater() to API standard * adjust the `greater()` ufunc to pass its array API standard test, and turn this test on in the CI * this largely mirrors the changes in gh-150, except that we already had a draft implementation of `greater()` in place here --- .github/workflows/array_api.yml | 4 +- .github/workflows/main_ci.yml | 2 +- pykokkos/__init__.py | 1 + pykokkos/interface/views.py | 38 +++ pykokkos/lib/ufunc_workunits.py | 508 ++++++++++++++++++++++++++++++++ pykokkos/lib/ufuncs.py | 104 +++++-- 6 files changed, 629 insertions(+), 28 deletions(-) diff --git a/.github/workflows/array_api.yml b/.github/workflows/array_api.yml index 2d656c21..1b0dcb9e 100644 --- a/.github/workflows/array_api.yml +++ b/.github/workflows/array_api.yml @@ -30,7 +30,7 @@ jobs: cd /tmp git clone https://github.com/kokkos/pykokkos-base.git cd pykokkos-base - python setup.py install -- -DENABLE_LAYOUTS=ON -DENABLE_MEMORY_TRAITS=OFF + python setup.py install -- -DENABLE_LAYOUTS=ON -DENABLE_MEMORY_TRAITS=OFF -DENABLE_VIEW_RANKS=5 - name: Install pykokkos run: | python -m pip install . @@ -49,4 +49,4 @@ jobs: # for hypothesis-driven test case generation pytest $GITHUB_WORKSPACE/pre_compile_tools/pre_compile_ufuncs.py -s # only run a subset of the conformance tests to get started - pytest array_api_tests/meta/test_broadcasting.py array_api_tests/meta/test_equality_mapping.py array_api_tests/meta/test_signatures.py array_api_tests/meta/test_special_cases.py array_api_tests/test_constants.py array_api_tests/meta/test_utils.py array_api_tests/test_creation_functions.py::test_ones array_api_tests/test_creation_functions.py::test_ones_like array_api_tests/test_data_type_functions.py::test_result_type array_api_tests/test_operators_and_elementwise_functions.py::test_log10 array_api_tests/test_operators_and_elementwise_functions.py::test_sqrt array_api_tests/test_operators_and_elementwise_functions.py::test_isfinite array_api_tests/test_operators_and_elementwise_functions.py::test_log2 array_api_tests/test_operators_and_elementwise_functions.py::test_log1p array_api_tests/test_operators_and_elementwise_functions.py::test_isinf array_api_tests/test_operators_and_elementwise_functions.py::test_log array_api_tests/test_array_object.py::test_scalar_casting array_api_tests/test_operators_and_elementwise_functions.py::test_sign array_api_tests/test_operators_and_elementwise_functions.py::test_square array_api_tests/test_operators_and_elementwise_functions.py::test_cos array_api_tests/test_operators_and_elementwise_functions.py::test_round array_api_tests/test_operators_and_elementwise_functions.py::test_trunc array_api_tests/test_operators_and_elementwise_functions.py::test_ceil array_api_tests/test_operators_and_elementwise_functions.py::test_floor + pytest array_api_tests/meta/test_broadcasting.py array_api_tests/meta/test_equality_mapping.py array_api_tests/meta/test_signatures.py array_api_tests/meta/test_special_cases.py array_api_tests/test_constants.py array_api_tests/meta/test_utils.py array_api_tests/test_creation_functions.py::test_ones array_api_tests/test_creation_functions.py::test_ones_like array_api_tests/test_data_type_functions.py::test_result_type array_api_tests/test_operators_and_elementwise_functions.py::test_log10 array_api_tests/test_operators_and_elementwise_functions.py::test_sqrt array_api_tests/test_operators_and_elementwise_functions.py::test_isfinite array_api_tests/test_operators_and_elementwise_functions.py::test_log2 array_api_tests/test_operators_and_elementwise_functions.py::test_log1p array_api_tests/test_operators_and_elementwise_functions.py::test_isinf array_api_tests/test_operators_and_elementwise_functions.py::test_log array_api_tests/test_array_object.py::test_scalar_casting array_api_tests/test_operators_and_elementwise_functions.py::test_sign array_api_tests/test_operators_and_elementwise_functions.py::test_square array_api_tests/test_operators_and_elementwise_functions.py::test_cos array_api_tests/test_operators_and_elementwise_functions.py::test_round array_api_tests/test_operators_and_elementwise_functions.py::test_trunc array_api_tests/test_operators_and_elementwise_functions.py::test_ceil array_api_tests/test_operators_and_elementwise_functions.py::test_floor array_api_tests/test_operators_and_elementwise_functions.py::test_greater diff --git a/.github/workflows/main_ci.yml b/.github/workflows/main_ci.yml index 000af412..35326b0c 100644 --- a/.github/workflows/main_ci.yml +++ b/.github/workflows/main_ci.yml @@ -30,7 +30,7 @@ jobs: cd /tmp git clone https://github.com/kokkos/pykokkos-base.git cd pykokkos-base - python setup.py install -- -DENABLE_LAYOUTS=ON -DENABLE_MEMORY_TRAITS=OFF + python setup.py install -- -DENABLE_LAYOUTS=ON -DENABLE_MEMORY_TRAITS=OFF -DENABLE_VIEW_RANKS=5 - name: Install pykokkos run: | python -m pip install . diff --git a/pykokkos/__init__.py b/pykokkos/__init__.py index 3b83a079..db384dea 100644 --- a/pykokkos/__init__.py +++ b/pykokkos/__init__.py @@ -70,6 +70,7 @@ from pykokkos.lib.manipulate import reshape, ravel, expand_dims from pykokkos.lib.util import all, any, sum, find_max, searchsorted, col, linspace, logspace from pykokkos.lib.constants import e, pi, inf, nan +from pykokkos.interface.views import astype __array_api_version__ = "2021.12" diff --git a/pykokkos/interface/views.py b/pykokkos/interface/views.py index 7b169874..7e049d04 100644 --- a/pykokkos/interface/views.py +++ b/pykokkos/interface/views.py @@ -392,6 +392,38 @@ def __array__(self, dtype=None): return self.data + def __gt__(self, other): + # avoid circular import with scoped import + from pykokkos.lib.ufuncs import greater + if isinstance(other, float): + new_other = pk.View((), dtype=pk.double) + new_other[:] = other + elif isinstance(other, int): + if 0 <= other <= 255: + other_dtype = pk.uint8 + elif 0 <= other <= 65535: + other_dtype = pk.uint16 + elif 0 <= other <= 4294967295: + other_dtype = pk.uint32 + elif 0 <= other <= 18446744073709551615: + other_dtype = pk.uint64 + elif -128 <= other <= 127: + other_dtype = pk.int8 + elif -32768 <= other <= 32767: + other_dtype = pk.int16 + elif -2147483648 <= other <= 2147483647: + other_dtype = pk.int32 + elif -9223372036854775808 <= other <= 9223372036854775807: + other_dtype = pk.int64 + new_other = pk.View((), dtype=other_dtype) + new_other[:] = other + elif isinstance(other, pk.View): + new_other = other + else: + raise ValueError("unexpected types!") + return greater(self, new_other) + + @staticmethod def _get_dtype_name(type_name: str) -> str: """ @@ -785,3 +817,9 @@ class ScratchView7D(ScratchView, Generic[T]): class ScratchView8D(ScratchView, Generic[T]): pass + + +def astype(view, dtype): + new_view = pk.View([*view.shape], dtype=dtype) + new_view[:] = view + return new_view diff --git a/pykokkos/lib/ufunc_workunits.py b/pykokkos/lib/ufunc_workunits.py index 06f12530..2e5e76be 100644 --- a/pykokkos/lib/ufunc_workunits.py +++ b/pykokkos/lib/ufunc_workunits.py @@ -1,6 +1,513 @@ import pykokkos as pk +@pk.workunit +def greater_impl_5d_int8(tid: int, + view1: pk.View5D[pk.int8], + view2: pk.View5D[pk.int8], + out: pk.View5D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + for l in range(view1.extent(4)): + out[tid][i][j][k][l] = view1[tid][i][j][k][l] > view2[tid][i][j][k][l] + + +@pk.workunit +def greater_impl_5d_float(tid: int, + view1: pk.View5D[pk.float], + view2: pk.View5D[pk.float], + out: pk.View5D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + for l in range(view1.extent(4)): + out[tid][i][j][k][l] = view1[tid][i][j][k][l] > view2[tid][i][j][k][l] + + +@pk.workunit +def greater_impl_5d_double(tid: int, + view1: pk.View5D[pk.double], + view2: pk.View5D[pk.double], + out: pk.View5D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + for l in range(view1.extent(4)): + out[tid][i][j][k][l] = view1[tid][i][j][k][l] > view2[tid][i][j][k][l] + + +@pk.workunit +def greater_impl_5d_int16(tid: int, + view1: pk.View5D[pk.int16], + view2: pk.View5D[pk.int16], + out: pk.View5D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + for l in range(view1.extent(4)): + out[tid][i][j][k][l] = view1[tid][i][j][k][l] > view2[tid][i][j][k][l] + + +@pk.workunit +def greater_impl_5d_int32(tid: int, + view1: pk.View5D[pk.int32], + view2: pk.View5D[pk.int32], + out: pk.View5D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + for l in range(view1.extent(4)): + out[tid][i][j][k][l] = view1[tid][i][j][k][l] > view2[tid][i][j][k][l] + + +@pk.workunit +def greater_impl_5d_int64(tid: int, + view1: pk.View5D[pk.int64], + view2: pk.View5D[pk.int64], + out: pk.View5D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + for l in range(view1.extent(4)): + out[tid][i][j][k][l] = view1[tid][i][j][k][l] > view2[tid][i][j][k][l] + + +@pk.workunit +def greater_impl_5d_uint8(tid: int, + view1: pk.View5D[pk.uint8], + view2: pk.View5D[pk.uint8], + out: pk.View5D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + for l in range(view1.extent(4)): + out[tid][i][j][k][l] = view1[tid][i][j][k][l] > view2[tid][i][j][k][l] + + +@pk.workunit +def greater_impl_5d_uint16(tid: int, + view1: pk.View5D[pk.uint16], + view2: pk.View5D[pk.uint16], + out: pk.View5D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + for l in range(view1.extent(4)): + out[tid][i][j][k][l] = view1[tid][i][j][k][l] > view2[tid][i][j][k][l] + + +@pk.workunit +def greater_impl_5d_uint32(tid: int, + view1: pk.View5D[pk.uint32], + view2: pk.View5D[pk.uint32], + out: pk.View5D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + for l in range(view1.extent(4)): + out[tid][i][j][k][l] = view1[tid][i][j][k][l] > view2[tid][i][j][k][l] + + +@pk.workunit +def greater_impl_5d_uint64(tid: int, + view1: pk.View5D[pk.uint64], + view2: pk.View5D[pk.uint64], + out: pk.View5D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + for l in range(view1.extent(4)): + out[tid][i][j][k][l] = view1[tid][i][j][k][l] > view2[tid][i][j][k][l] + + +@pk.workunit +def greater_impl_4d_uint8(tid: int, + view1: pk.View4D[pk.uint8], + view2: pk.View4D[pk.uint8], + out: pk.View4D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + out[tid][i][j][k] = view1[tid][i][j][k] > view2[tid][i][j][k] + + +@pk.workunit +def greater_impl_4d_float(tid: int, + view1: pk.View4D[pk.float], + view2: pk.View4D[pk.float], + out: pk.View4D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + out[tid][i][j][k] = view1[tid][i][j][k] > view2[tid][i][j][k] + + +@pk.workunit +def greater_impl_4d_double(tid: int, + view1: pk.View4D[pk.double], + view2: pk.View4D[pk.double], + out: pk.View4D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + out[tid][i][j][k] = view1[tid][i][j][k] > view2[tid][i][j][k] + + +@pk.workunit +def greater_impl_4d_uint16(tid: int, + view1: pk.View4D[pk.uint16], + view2: pk.View4D[pk.uint16], + out: pk.View4D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + out[tid][i][j][k] = view1[tid][i][j][k] > view2[tid][i][j][k] + + +@pk.workunit +def greater_impl_4d_uint32(tid: int, + view1: pk.View4D[pk.uint32], + view2: pk.View4D[pk.uint32], + out: pk.View4D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + out[tid][i][j][k] = view1[tid][i][j][k] > view2[tid][i][j][k] + + +@pk.workunit +def greater_impl_4d_uint64(tid: int, + view1: pk.View4D[pk.uint64], + view2: pk.View4D[pk.uint64], + out: pk.View4D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + out[tid][i][j][k] = view1[tid][i][j][k] > view2[tid][i][j][k] + + +@pk.workunit +def greater_impl_3d_uint8(tid: int, + view1: pk.View3D[pk.uint8], + view2: pk.View3D[pk.uint8], + out: pk.View3D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + out[tid][i][j] = view1[tid][i][j] > view2[tid][i][j] + + +@pk.workunit +def greater_impl_3d_uint16(tid: int, + view1: pk.View3D[pk.uint16], + view2: pk.View3D[pk.uint16], + out: pk.View3D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + out[tid][i][j] = view1[tid][i][j] > view2[tid][i][j] + + +@pk.workunit +def greater_impl_3d_uint32(tid: int, + view1: pk.View3D[pk.uint32], + view2: pk.View3D[pk.uint32], + out: pk.View3D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + out[tid][i][j] = view1[tid][i][j] > view2[tid][i][j] + + +@pk.workunit +def greater_impl_3d_uint64(tid: int, + view1: pk.View3D[pk.uint64], + view2: pk.View3D[pk.uint64], + out: pk.View3D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + out[tid][i][j] = view1[tid][i][j] > view2[tid][i][j] + + +@pk.workunit +def greater_impl_3d_float(tid: int, + view1: pk.View3D[pk.float], + view2: pk.View3D[pk.float], + out: pk.View3D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + out[tid][i][j] = view1[tid][i][j] > view2[tid][i][j] + + +@pk.workunit +def greater_impl_3d_double(tid: int, + view1: pk.View3D[pk.double], + view2: pk.View3D[pk.double], + out: pk.View3D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + out[tid][i][j] = view1[tid][i][j] > view2[tid][i][j] + + +@pk.workunit +def greater_impl_2d_uint8(tid: int, + view1: pk.View2D[pk.uint8], + view2: pk.View2D[pk.uint8], + out: pk.View2D[pk.uint8]): + for i in range(view1.extent(1)): + out[tid][i] = view1[tid][i] > view2[tid][i] + + +@pk.workunit +def greater_impl_2d_uint16(tid: int, + view1: pk.View2D[pk.uint16], + view2: pk.View2D[pk.uint16], + out: pk.View2D[pk.uint8]): + for i in range(view1.extent(1)): + out[tid][i] = view1[tid][i] > view2[tid][i] + + +@pk.workunit +def greater_impl_2d_uint32(tid: int, + view1: pk.View2D[pk.uint32], + view2: pk.View2D[pk.uint32], + out: pk.View2D[pk.uint8]): + for i in range(view1.extent(1)): + out[tid][i] = view1[tid][i] > view2[tid][i] + + +@pk.workunit +def greater_impl_2d_uint64(tid: int, + view1: pk.View2D[pk.uint64], + view2: pk.View2D[pk.uint64], + out: pk.View2D[pk.uint8]): + for i in range(view1.extent(1)): + out[tid][i] = view1[tid][i] > view2[tid][i] + + +@pk.workunit +def greater_impl_2d_float(tid: int, + view1: pk.View2D[pk.float], + view2: pk.View2D[pk.float], + out: pk.View2D[pk.uint8]): + for i in range(view1.extent(1)): + out[tid][i] = view1[tid][i] > view2[tid][i] + + +@pk.workunit +def greater_impl_2d_double(tid: int, + view1: pk.View2D[pk.double], + view2: pk.View2D[pk.double], + out: pk.View2D[pk.uint8]): + for i in range(view1.extent(1)): + out[tid][i] = view1[tid][i] > view2[tid][i] + + +@pk.workunit +def greater_impl_1d_uint8(tid: int, + view1: pk.View1D[pk.uint8], + view2: pk.View1D[pk.uint8], + out: pk.View1D[pk.uint8]): + out[tid] = view1[tid] > view2[tid] + + +@pk.workunit +def greater_impl_1d_float(tid: int, + view1: pk.View1D[pk.float], + view2: pk.View1D[pk.float], + out: pk.View1D[pk.uint8]): + out[tid] = view1[tid] > view2[tid] + + +@pk.workunit +def greater_impl_1d_double(tid: int, + view1: pk.View1D[pk.double], + view2: pk.View1D[pk.double], + out: pk.View1D[pk.uint8]): + out[tid] = view1[tid] > view2[tid] + + +@pk.workunit +def greater_impl_1d_int8(tid: int, + view1: pk.View1D[pk.int8], + view2: pk.View1D[pk.int8], + out: pk.View1D[pk.uint8]): + out[tid] = view1[tid] > view2[tid] + + +@pk.workunit +def greater_impl_1d_int16(tid: int, + view1: pk.View1D[pk.int16], + view2: pk.View1D[pk.int16], + out: pk.View1D[pk.uint8]): + out[tid] = view1[tid] > view2[tid] + + +@pk.workunit +def greater_impl_1d_int32(tid: int, + view1: pk.View1D[pk.int32], + view2: pk.View1D[pk.int32], + out: pk.View1D[pk.uint8]): + out[tid] = view1[tid] > view2[tid] + + +@pk.workunit +def greater_impl_1d_int64(tid: int, + view1: pk.View1D[pk.int64], + view2: pk.View1D[pk.int64], + out: pk.View1D[pk.uint8]): + out[tid] = view1[tid] > view2[tid] + + +@pk.workunit +def greater_impl_1d_uint16(tid: int, + view1: pk.View1D[pk.uint16], + view2: pk.View1D[pk.uint16], + out: pk.View1D[pk.uint8]): + out[tid] = view1[tid] > view2[tid] + + +@pk.workunit +def greater_impl_1d_uint32(tid: int, + view1: pk.View1D[pk.uint32], + view2: pk.View1D[pk.uint32], + out: pk.View1D[pk.uint8]): + out[tid] = view1[tid] > view2[tid] + + +@pk.workunit +def greater_impl_1d_uint64(tid: int, + view1: pk.View1D[pk.uint64], + view2: pk.View1D[pk.uint64], + out: pk.View1D[pk.uint8]): + out[tid] = view1[tid] > view2[tid] + + +@pk.workunit +def greater_impl_1d_int64(tid: int, + view1: pk.View1D[pk.int64], + view2: pk.View1D[pk.int64], + out: pk.View1D[pk.uint8]): + out[tid] = view1[tid] > view2[tid] + +@pk.workunit +def greater_impl_2d_int8(tid: int, + view1: pk.View2D[pk.int8], + view2: pk.View2D[pk.int8], + out: pk.View2D[pk.uint8]): + for i in range(view1.extent(1)): + out[tid][i] = view1[tid][i] > view2[tid][i] + + +@pk.workunit +def greater_impl_2d_int16(tid: int, + view1: pk.View2D[pk.int16], + view2: pk.View2D[pk.int16], + out: pk.View2D[pk.uint8]): + for i in range(view1.extent(1)): + out[tid][i] = view1[tid][i] > view2[tid][i] + + +@pk.workunit +def greater_impl_2d_int32(tid: int, + view1: pk.View2D[pk.int32], + view2: pk.View2D[pk.int32], + out: pk.View2D[pk.uint8]): + for i in range(view1.extent(1)): + out[tid][i] = view1[tid][i] > view2[tid][i] + + +@pk.workunit +def greater_impl_2d_int64(tid: int, + view1: pk.View2D[pk.int64], + view2: pk.View2D[pk.int64], + out: pk.View2D[pk.uint8]): + for i in range(view1.extent(1)): + out[tid][i] = view1[tid][i] > view2[tid][i] + + +@pk.workunit +def greater_impl_3d_int8(tid: int, + view1: pk.View3D[pk.int8], + view2: pk.View3D[pk.int8], + out: pk.View3D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + out[tid][i][j] = view1[tid][i][j] > view2[tid][i][j] + + +@pk.workunit +def greater_impl_3d_int16(tid: int, + view1: pk.View3D[pk.int16], + view2: pk.View3D[pk.int16], + out: pk.View3D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + out[tid][i][j] = view1[tid][i][j] > view2[tid][i][j] + + +@pk.workunit +def greater_impl_3d_int32(tid: int, + view1: pk.View3D[pk.int32], + view2: pk.View3D[pk.int32], + out: pk.View3D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + out[tid][i][j] = view1[tid][i][j] > view2[tid][i][j] + + +@pk.workunit +def greater_impl_3d_int64(tid: int, + view1: pk.View3D[pk.int64], + view2: pk.View3D[pk.int64], + out: pk.View3D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + out[tid][i][j] = view1[tid][i][j] > view2[tid][i][j] + + +@pk.workunit +def greater_impl_4d_int8(tid: int, + view1: pk.View4D[pk.int8], + view2: pk.View4D[pk.int8], + out: pk.View4D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + out[tid][i][j][k] = view1[tid][i][j][k] > view2[tid][i][j][k] + + +@pk.workunit +def greater_impl_4d_int16(tid: int, + view1: pk.View4D[pk.int16], + view2: pk.View4D[pk.int16], + out: pk.View4D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + out[tid][i][j][k] = view1[tid][i][j][k] > view2[tid][i][j][k] + + +@pk.workunit +def greater_impl_4d_int32(tid: int, + view1: pk.View4D[pk.int32], + view2: pk.View4D[pk.int32], + out: pk.View4D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + out[tid][i][j][k] = view1[tid][i][j][k] > view2[tid][i][j][k] + + +@pk.workunit +def greater_impl_4d_int64(tid: int, + view1: pk.View4D[pk.int64], + view2: pk.View4D[pk.int64], + out: pk.View4D[pk.uint8]): + for i in range(view1.extent(1)): + for j in range(view1.extent(2)): + for k in range(view1.extent(3)): + out[tid][i][j][k] = view1[tid][i][j][k] > view2[tid][i][j][k] + + @pk.workunit def floor_impl_1d_double(tid: int, view: pk.View1D[pk.double], out: pk.View1D[pk.double]): out[tid] = floor(view[tid]) @@ -137,6 +644,7 @@ def round_impl_3d_float(tid: int, view: pk.View3D[pk.float], out: pk.View3D[pk.f for j in range(view.extent(2)): out[tid][i][j] = round(view[tid][i][j]) + @pk.workunit def isfinite_impl_1d_double(tid: int, view: pk.View1D[pk.double], out: pk.View1D[pk.uint8]): out[tid] = isfinite(view[tid]) diff --git a/pykokkos/lib/ufuncs.py b/pykokkos/lib/ufuncs.py index 13be86e2..9844def4 100644 --- a/pykokkos/lib/ufuncs.py +++ b/pykokkos/lib/ufuncs.py @@ -42,6 +42,59 @@ def _ufunc_kernel_dispatcher(tid, return ret +def _broadcast_views(view1, view2): + # support broadcasting by using the same + # shape matching rules as NumPy + # TODO: determine if this can be done with + # more memory efficiency? + if view1.shape != view2.shape: + new_shape = np.broadcast_shapes(view1.shape, view2.shape) + view1_new = pk.View([*new_shape], dtype=view1.dtype) + view1_new[:] = view1 + view1 = view1_new + view2_new = pk.View([*new_shape], dtype=view2.dtype) + view2_new[:] = view2 + view2 = view2_new + return view1, view2 + + +def _typematch_views(view1, view2): + # very crude casting implementation + # for binary ufuncs + dtype1 = view1.dtype + dtype2 = view2.dtype + dtype_extractor = re.compile(r".*(?:data_types|DataType)\.(\w+)") + res1 = dtype_extractor.match(str(dtype1)) + res2 = dtype_extractor.match(str(dtype2)) + effective_dtype = dtype1 + if res1 is not None and res2 is not None: + res1_dtype_str = res1.group(1) + res2_dtype_str = res2.group(1) + if res1_dtype_str == "double": + res1_dtype_str = "float64" + elif res1_dtype_str == "float": + res1_dtype_str = "float32" + if res2_dtype_str == "double": + res2_dtype_str = "float64" + elif res2_dtype_str == "float": + res2_dtype_str = "float32" + if (("int" in res1_dtype_str and "int" in res2_dtype_str) or + ("float" in res1_dtype_str and "float" in res2_dtype_str)): + dtype_1_width = int(res1_dtype_str.split("t")[1]) + dtype_2_width = int(res2_dtype_str.split("t")[1]) + if dtype_1_width >= dtype_2_width: + effective_dtype = dtype1 + view2_new = pk.View([*view2.shape], dtype=effective_dtype) + view2_new[:] = view2 + view2 = view2_new + else: + effective_dtype = dtype2 + view1_new = pk.View([*view1.shape], dtype=effective_dtype) + view1_new[:] = view1 + view1 = view1_new + return view1, view2, effective_dtype + + def reciprocal(view): """ Return the reciprocal of the argument, element-wise. @@ -1482,43 +1535,44 @@ def greater_impl_1d_float(tid: int, viewA: pk.View1D[pk.float], viewB: pk.View1D out[tid] = viewA[tid] > viewB[tid] -def greater(viewA, viewB): +def greater(view1, view2): """ - Return the truth value of viewA > viewB element-wise. + Return the truth value of view1 > view2 element-wise. Parameters ---------- - viewA : pykokkos view + view1 : pykokkos view Input view. - viewB : pykokkos view + view2 : pykokkos view Input view. Returns ------- - out : pykokkos view (uint8) + out : pykokkos view (bool) Output view. - """ - if len(viewA.shape) > 1 or len(viewB.shape) > 1: - raise NotImplementedError("greater() ufunc only supports 1D views") - out = pk.View([viewA.shape[0]], pk.uint8) - if str(viewA.dtype) == "DataType.double" and str(viewB.dtype) == "DataType.double": - pk.parallel_for( - viewA.shape[0], - greater_impl_1d_double, - viewA=viewA, - viewB=viewB, - out=out) - - elif str(viewA.dtype) == "DataType.float" and str(viewB.dtype) == "DataType.float": - pk.parallel_for( - viewA.shape[0], - greater_impl_1d_float, - viewA=viewA, - viewB=viewB, - out=out) + view1, view2 = _broadcast_views(view1, view2) + dtype1 = view1.dtype + dtype2 = view2.dtype + view1, view2, effective_dtype = _typematch_views(view1, view2) + ndims = len(view1.shape) + if ndims > 5: + raise NotImplementedError("greater() ufunc only supports up to 5D views") + if view1.size == 0: + return pk.View([*view1.shape], dtype=pk.bool) + out = pk.View([*view1.shape], dtype=pk.bool) + if view1.shape == (): + tid = 1 else: - raise RuntimeError("Incompatible Types") + tid = view1.shape[0] + _ufunc_kernel_dispatcher(tid=tid, + dtype=effective_dtype, + ndims=ndims, + op="greater", + sub_dispatcher=pk.parallel_for, + out=out, + view1=view1, + view2=view2) return out From 47398dcd273d7ce62f1dc47bb5d4fae4c98d6462 Mon Sep 17 00:00:00 2001 From: Tyler Reddy Date: Mon, 9 Jan 2023 14:22:35 -0700 Subject: [PATCH 2/2] TST: PR 151 revisions * `test_multi_array_1d_exposed_ufuncs_vs_numpy()` no longer requires specific names for the view kwargs because this was causing issues with how I renamed the view arguments in the `greater()` ufunc --- tests/test_ufuncs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_ufuncs.py b/tests/test_ufuncs.py index 2004c3a2..2fcd80f6 100644 --- a/tests/test_ufuncs.py +++ b/tests/test_ufuncs.py @@ -373,7 +373,7 @@ def test_multi_array_1d_exposed_ufuncs_vs_numpy(pk_ufunc, viewB: pk.View1d = pk.View([10], pk_dtype) viewB[:] = np.full(10, 5, dtype=numpy_dtype) - actual = pk_ufunc(viewA=viewA, viewB=viewB) + actual = pk_ufunc(viewA, viewB) assert_allclose(actual, expected)