Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update trace.py tests / fix some warnings #307

Merged
merged 3 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions cunumeric/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions cunumeric/deferred.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion cunumeric/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def is_supported_dtype(dtype: Any) -> bool:


def calculate_volume(shape: tuple[int, ...]) -> int:
if shape == ():
if len(shape) == 0:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@magnatelee This was causing a warning because sometimes the shape passed in was a numpy array. This seems like an OK immediate fix until more in-depth typing work is started (and the proper fix in any case if arrays are valid inputs)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I noticed the same warning today and put in exactly the same fix in my local change. I'll chase down where the numpy array is being passed and fix it as well

return 0
return reduce(lambda x, y: x * y, shape)

Expand Down
41 changes: 23 additions & 18 deletions tests/trace.py → tests/integration/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)