From 0fd8316db21118f2c52fbb07358d987b4b2b8911 Mon Sep 17 00:00:00 2001 From: Bryan Van de Ven Date: Thu, 28 Apr 2022 14:05:33 -0700 Subject: [PATCH 1/3] np.bool -> bool --- cunumeric/array.py | 6 ++---- cunumeric/deferred.py | 6 +++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/cunumeric/array.py b/cunumeric/array.py index 0b5259fcd..f6fc31ff2 100644 --- a/cunumeric/array.py +++ b/cunumeric/array.py @@ -770,11 +770,9 @@ def _convert_key(self, key, first=True): # Otherwise convert it to a cuNumeric array, check types # and get the thunk key = convert_to_cunumeric_ndarray(key) - if key.dtype != np.bool and not np.issubdtype( - key.dtype, np.integer - ): + if key.dtype != bool and not np.issubdtype(key.dtype, np.integer): raise TypeError("index arrays should be int or bool type") - if key.dtype != np.bool and key.dtype != np.int64: + if key.dtype != bool and key.dtype != np.int64: runtime.warn( "converting index array to int64 type", category=RuntimeWarning, diff --git a/cunumeric/deferred.py b/cunumeric/deferred.py index d4a6e8baf..7dd219039 100644 --- a/cunumeric/deferred.py +++ b/cunumeric/deferred.py @@ -422,7 +422,7 @@ def _create_indexing_array(self, key, is_set=False): start_index = -1 if ( isinstance(key, NumPyThunk) - and key.dtype == np.bool + and key.dtype == bool and key.shape == rhs.shape ): if not isinstance(key, DeferredArray): @@ -476,7 +476,7 @@ def _create_indexing_array(self, key, is_set=False): transpose_needed = transpose_needed or ((dim - last_index) > 1) if ( isinstance(k, NumPyThunk) - and k.dtype == np.bool + and k.dtype == bool and k.ndim >= 2 ): for i in range(dim, dim + k.ndim): @@ -513,7 +513,7 @@ def _create_indexing_array(self, key, is_set=False): elif isinstance(k, NumPyThunk): if not isinstance(key, DeferredArray): k = self.runtime.to_deferred_array(k) - if k.dtype == np.bool: + if k.dtype == bool: for i in range(k.ndim): if k.shape[i] != store.shape[dim + i + shift]: raise ValueError( From 5ac32c52f9faf1e10429d786d4560e6d473241c8 Mon Sep 17 00:00:00 2001 From: Bryan Van de Ven Date: Thu, 28 Apr 2022 15:03:11 -0700 Subject: [PATCH 2/3] update late-added trace test --- tests/{trace.py => integration/test_trace.py} | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) rename tests/{trace.py => integration/test_trace.py} (71%) diff --git a/tests/trace.py b/tests/integration/test_trace.py similarity index 71% rename from tests/trace.py rename to tests/integration/test_trace.py index ca517c4de..c7fa8ee2a 100644 --- a/tests/trace.py +++ b/tests/integration/test_trace.py @@ -17,17 +17,14 @@ from itertools import permutations import numpy as np +import pytest from test_tools.generators import mk_seq_array import cunumeric as num from legate.core import LEGATE_MAX_DIM -def test(): - # -------------------------------------------------------------- - # TRACE - # -------------------------------------------------------------- - +def test_2d(): a = np.arange(8).reshape((2, 4)) a_num = num.array(a) res = np.trace(a) @@ -38,6 +35,8 @@ def test(): res_num = num.trace(a_num, dtype=float) assert np.array_equal(res, res_num) + +def test_3d(): a = np.arange(8).reshape((2, 2, 2)) a_num = num.array(a) res = np.trace(a) @@ -62,26 +61,32 @@ def test(): num.trace(a_num, dtype=int, out=out_num) assert np.array_equal(out, out_num) + +def test_4d(): a = np.arange(24).reshape((2, 2, 2, 3)) a_num = num.array(a) res = np.trace(a) res_num = num.trace(a_num) assert np.array_equal(res, res_num) - for ndim in range(2, LEGATE_MAX_DIM + 1): - a_shape = tuple(random.randint(1, 9) for i in range(ndim)) - np_array = mk_seq_array(np, a_shape) - num_array = mk_seq_array(num, a_shape) - # test trace - for axes in permutations(range(ndim), 2): - diag_size = min(a_shape[axes[0]], a_shape[axes[1]]) - 1 - for offset in range(-diag_size + 1, diag_size): - assert np.array_equal( - np_array.trace(offset, axes[0], axes[1]), - num_array.trace(offset, axes[0], axes[1]), - ) +@pytest.mark.parametrize("ndim", range(2, LEGATE_MAX_DIM + 1)) +def test_ndim(ndim): + a_shape = tuple(random.randint(1, 9) for i in range(ndim)) + np_array = mk_seq_array(np, a_shape) + num_array = mk_seq_array(num, a_shape) + + # test trace + for axes in permutations(range(ndim), 2): + diag_size = min(a_shape[axes[0]], a_shape[axes[1]]) - 1 + for offset in range(-diag_size + 1, diag_size): + assert np.array_equal( + np_array.trace(offset, axes[0], axes[1]), + num_array.trace(offset, axes[0], axes[1]), + ) if __name__ == "__main__": - test() + import sys + + pytest.main(sys.argv) From c646335e94d372a8dd0aecdf5bb2d2ad82eb955a Mon Sep 17 00:00:00 2001 From: Bryan Van de Ven Date: Thu, 28 Apr 2022 15:03:38 -0700 Subject: [PATCH 3/3] handle non-tuple shapes --- cunumeric/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cunumeric/utils.py b/cunumeric/utils.py index cfa04d6dd..ace4bb7b4 100644 --- a/cunumeric/utils.py +++ b/cunumeric/utils.py @@ -97,7 +97,7 @@ def is_supported_dtype(dtype: Any) -> bool: def calculate_volume(shape: tuple[int, ...]) -> int: - if shape == (): + if len(shape) == 0: return 0 return reduce(lambda x, y: x * y, shape)