Skip to content

Commit

Permalink
Fix random test failures in CPU-only runs (#1025)
Browse files Browse the repository at this point in the history
* Anticipate some test failures when cuRand is not available

* Ensure input to unstable sort algorithms contains no duplicates

* Add mergesort to stable sort methods
  • Loading branch information
manopapad authored Aug 14, 2023
1 parent 9017514 commit 06cd534
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
3 changes: 1 addition & 2 deletions cunumeric/random/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ def randint(
--------
Multiple GPUs, Multiple CPUs
"""

if not isinstance(low, int):
raise NotImplementedError("'low' must be an integer")
if high is not None and not isinstance(high, int):
Expand All @@ -126,7 +125,7 @@ def randint(
dtype = np.dtype(np.int64)
# TODO: randint must support unsigned integer dtypes as well
if dtype.kind != "i":
raise TypeError(
raise NotImplementedError(
"cunumeric.random.randint must be given an integer dtype"
)
if isinstance(size, int):
Expand Down
31 changes: 17 additions & 14 deletions tests/integration/test_argsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,9 @@
(DIM, DIM, DIM),
]

SORT_TYPES = ["quicksort", "mergesort", "stable"]
UNSTABLE_SORT_TYPE = ["heapsort"]
STABLE_SORT_TYPES = ["stable", "mergesort"]
UNSTABLE_SORT_TYPES = ["heapsort", "quicksort"]
SORT_TYPES = STABLE_SORT_TYPES + UNSTABLE_SORT_TYPES


class TestArgSort(object):
Expand Down Expand Up @@ -137,7 +138,7 @@ def test_basic_axis(self, size):
assert np.array_equal(res_num, res_np)

@pytest.mark.parametrize("size", SIZES)
@pytest.mark.parametrize("sort_type", SORT_TYPES)
@pytest.mark.parametrize("sort_type", STABLE_SORT_TYPES)
def test_basic_axis_sort_type(self, size, sort_type):
arr_np = np.random.randint(-100, 100, size)
arr_num = num.array(arr_np)
Expand All @@ -146,13 +147,14 @@ def test_basic_axis_sort_type(self, size, sort_type):
res_num = num.argsort(arr_num, axis=axis, kind=sort_type)
assert np.array_equal(res_num, res_np)

@pytest.mark.xfail
@pytest.mark.parametrize("size", SIZES)
@pytest.mark.parametrize("sort_type", UNSTABLE_SORT_TYPE)
@pytest.mark.parametrize("sort_type", UNSTABLE_SORT_TYPES)
def test_basic_axis_sort_type_unstable(self, size, sort_type):
# intermittent failed due to
# https://github.com/nv-legate/cunumeric/issues/782
arr_np = np.random.randint(-100, 100, size)
# have to guarantee unique values in input
# see https://github.com/nv-legate/cunumeric/issues/782
arr_np = np.arange(np.prod(size))
np.random.shuffle(arr_np)
arr_np = arr_np.reshape(size)
arr_num = num.array(arr_np)
for axis in range(-arr_np.ndim + 1, arr_np.ndim):
res_np = np.argsort(arr_np, axis=axis, kind=sort_type)
Expand All @@ -171,7 +173,7 @@ def test_arr_basic_axis(self, size):
assert np.array_equal(arr_np_copy, arr_num_copy)

@pytest.mark.parametrize("size", SIZES)
@pytest.mark.parametrize("sort_type", SORT_TYPES)
@pytest.mark.parametrize("sort_type", STABLE_SORT_TYPES)
def test_arr_basic_axis_sort(self, size, sort_type):
arr_np = np.random.randint(-100, 100, size)
arr_num = num.array(arr_np)
Expand All @@ -182,13 +184,14 @@ def test_arr_basic_axis_sort(self, size, sort_type):
arr_num_copy.argsort(axis=axis, kind=sort_type)
assert np.array_equal(arr_np_copy, arr_num_copy)

@pytest.mark.xfail
@pytest.mark.parametrize("size", SIZES)
@pytest.mark.parametrize("sort_type", UNSTABLE_SORT_TYPE)
@pytest.mark.parametrize("sort_type", UNSTABLE_SORT_TYPES)
def test_arr_basic_axis_sort_unstable(self, size, sort_type):
# intermittent failed due to
# https://github.com/nv-legate/cunumeric/issues/782
arr_np = np.random.randint(-100, 100, size)
# have to guarantee unique values in input
# see https://github.com/nv-legate/cunumeric/issues/782
arr_np = np.arange(np.prod(size))
np.random.shuffle(arr_np)
arr_np = arr_np.reshape(size)
arr_num = num.array(arr_np)
for axis in range(-arr_num.ndim + 1, arr_num.ndim):
arr_np_copy = arr_np
Expand Down
11 changes: 9 additions & 2 deletions tests/integration/test_random_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,15 @@ def test_randint_float_range(low, high):


@pytest.mark.xfail(
not EAGER_TEST, reason="cuNumeric raises NotImplementedError"
not num.runtime.has_curand or not EAGER_TEST,
reason="cuNumeric raises NotImplementedError",
)
@pytest.mark.parametrize("size", ALL_RNG_SIZES, ids=str)
@pytest.mark.parametrize("low, high", [(1000, 65535), (0, 1024)], ids=str)
@pytest.mark.parametrize("dtype", UINT_DTYPES, ids=str)
def test_randint_uint(low, high, dtype, size):
# NotImplementedError: cunumeric.random.randint must be given an integer
# dtype
# NotImplementedError: type for random.integers has to be int64 or int32
# or int16
arr_np, arr_num = gen_random_from_both(
Expand Down Expand Up @@ -287,7 +290,8 @@ def test_randint_distribution(low, high, size, dtype):


@pytest.mark.xfail(
not EAGER_TEST, reason="cuNumeric raises NotImplementedError"
not num.runtime.has_curand or not EAGER_TEST,
reason="cuNumeric raises NotImplementedError",
)
@pytest.mark.parametrize("size", (1024, 1025))
def test_randint_bool(size):
Expand All @@ -297,6 +301,9 @@ def test_randint_bool(size):
arr_num, np.mean(arr_np), np.std(arr_np), mean_tol=0.05
)
# NumPy pass
# cuNumeric not num.runtime.has_curand:
# NotImplementedError: cunumeric.random.randint must be given an integer
# dtype
# cuNumeric LEGATE_TEST=1 or size > 1024:
# NotImplementedError: type for random.integers has to be int64 or int32
# or int16
Expand Down

0 comments on commit 06cd534

Please sign in to comment.