diff --git a/dpnp/tests/helper.py b/dpnp/tests/helper.py index 5b43ebbeb6da..eb42d76b4d87 100644 --- a/dpnp/tests/helper.py +++ b/dpnp/tests/helper.py @@ -87,61 +87,102 @@ def assert_dtype_allclose( assert dpnp_arr.dtype == numpy_arr.dtype -def get_integer_dtypes(all_int_types=False, no_unsigned=False): - """ - Build a list of integer types supported by DPNP. +def generate_random_numpy_array( + shape, + dtype=None, + order="C", + hermitian=False, + seed_value=None, + low=-10, + high=10, + probability=0.5, +): """ + Generate a random numpy array with the specified shape and dtype. - dtypes = [dpnp.int32, dpnp.int64] + If required, the array can be made Hermitian (for complex data types) or + symmetric (for real data types). - if config.all_int_types or all_int_types: - dtypes += [dpnp.int8, dpnp.int16] - if not no_unsigned: - dtypes += [dpnp.uint8, dpnp.uint16, dpnp.uint32, dpnp.uint64] + Parameters + ---------- + shape : tuple + Shape of the generated array. + dtype : str or dtype, optional + Desired data-type for the output array. + If not specified, data type will be determined by numpy. - return dtypes + Default : ``None`` + order : {"C", "F"}, optional + Specify the memory layout of the output array. + Default: ``"C"``. + hermitian : bool, optional + If True, generates a Hermitian (symmetric if `dtype` is real) matrix. -def get_complex_dtypes(device=None): - """ - Build a list of complex types supported by DPNP based on device capabilities. - """ + Default : ``False`` + seed_value : int, optional + The seed value to initialize the random number generator. - dev = dpctl.select_default_device() if device is None else device + Default : ``None`` + low : {int, float}, optional + Lower boundary of the generated samples from a uniform distribution. - # add complex types - dtypes = [dpnp.complex64] - if dev.has_aspect_fp64: - dtypes.append(dpnp.complex128) - return dtypes + Default : ``-10``. + high : {int, float}, optional + Upper boundary of the generated samples from a uniform distribution. + Default : ``10``. + probability : float, optional + If dtype is bool, the probability of True. Ignored for other dtypes. + + Default : ``0.5``. + Returns + ------- + out : numpy.ndarray + A random numpy array of the specified shape, dtype and memory layout. + The array is Hermitian or symmetric if `hermitian` is True. + + Note: + For arrays with more than 2 dimensions, the Hermitian or + symmetric property is ensured for each 2D sub-array. -def get_float_dtypes(no_float16=True, device=None): - """ - Build a list of floating types supported by DPNP based on device capabilities. """ - dev = dpctl.select_default_device() if device is None else device + if seed_value is None: + seed_value = 42 + numpy.random.seed(seed_value) - # add floating types - dtypes = [] - if not no_float16 and dev.has_aspect_fp16: - dtypes.append(dpnp.float16) + if numpy.issubdtype(dtype, numpy.unsignedinteger): + low = 0 - dtypes.append(dpnp.float32) - if dev.has_aspect_fp64: - dtypes.append(dpnp.float64) - return dtypes + # dtype=int is needed for 0d arrays + size = numpy.prod(shape, dtype=int) + if dtype == dpnp.bool: + a = numpy.random.choice( + [False, True], size, p=[1 - probability, probability] + ) + else: + a = numpy.random.uniform(low, high, size).astype(dtype) + if numpy.issubdtype(a.dtype, numpy.complexfloating): + a += 1j * numpy.random.uniform(low, high, size) -def get_float_complex_dtypes(no_float16=True, device=None): - """ - Build a list of floating and complex types supported by DPNP based on device capabilities. - """ + a = a.reshape(shape) + if hermitian and a.size > 0: + if a.ndim > 2: + orig_shape = a.shape + # get 3D array + a = a.reshape(-1, orig_shape[-2], orig_shape[-1]) + for i in range(a.shape[0]): + a[i] = numpy.conj(a[i].T) @ a[i] + a = a.reshape(orig_shape) + else: + a = numpy.conj(a.T) @ a - dtypes = get_float_dtypes(no_float16, device) - dtypes.extend(get_complex_dtypes(device)) - return dtypes + # a.reshape(shape) returns an array in C order by default + if order != "C" and a.ndim > 1: + a = numpy.array(a, order=order) + return a def get_abs_array(data, dtype=None): @@ -214,102 +255,79 @@ def get_array(xp, a): return a -def generate_random_numpy_array( - shape, - dtype=None, - order="C", - hermitian=False, - seed_value=None, - low=-10, - high=10, - probability=0.5, -): +def get_complex_dtypes(device=None): + """ + Build a list of complex types supported by DPNP based on device capabilities. """ - Generate a random numpy array with the specified shape and dtype. - If required, the array can be made Hermitian (for complex data types) or - symmetric (for real data types). + dev = dpctl.select_default_device() if device is None else device - Parameters - ---------- - shape : tuple - Shape of the generated array. - dtype : str or dtype, optional - Desired data-type for the output array. - If not specified, data type will be determined by numpy. + # add complex types + dtypes = [dpnp.complex64] + if dev.has_aspect_fp64: + dtypes.append(dpnp.complex128) + return dtypes - Default : ``None`` - order : {"C", "F"}, optional - Specify the memory layout of the output array. - Default: ``"C"``. - hermitian : bool, optional - If True, generates a Hermitian (symmetric if `dtype` is real) matrix. +def get_float_dtypes(no_float16=True, device=None): + """ + Build a list of floating types supported by DPNP based on device capabilities. + """ - Default : ``False`` - seed_value : int, optional - The seed value to initialize the random number generator. + dev = dpctl.select_default_device() if device is None else device - Default : ``None`` - low : {int, float}, optional - Lower boundary of the generated samples from a uniform distribution. + # add floating types + dtypes = [] + if not no_float16 and dev.has_aspect_fp16: + dtypes.append(dpnp.float16) - Default : ``-10``. - high : {int, float}, optional - Upper boundary of the generated samples from a uniform distribution. + dtypes.append(dpnp.float32) + if dev.has_aspect_fp64: + dtypes.append(dpnp.float64) + return dtypes - Default : ``10``. - probability : float, optional - If dtype is bool, the probability of True. Ignored for other dtypes. - Default : ``0.5``. - Returns - ------- - out : numpy.ndarray - A random numpy array of the specified shape, dtype and memory layout. - The array is Hermitian or symmetric if `hermitian` is True. +def get_float_complex_dtypes(no_float16=True, device=None): + """ + Build a list of floating and complex types supported by DPNP based on device capabilities. + """ + + dtypes = get_float_dtypes(no_float16, device) + dtypes.extend(get_complex_dtypes(device)) + return dtypes - Note: - For arrays with more than 2 dimensions, the Hermitian or - symmetric property is ensured for each 2D sub-array. +def get_integer_dtypes(all_int_types=False, no_unsigned=False): + """ + Build a list of integer types supported by DPNP. """ - if seed_value is None: - seed_value = 42 - numpy.random.seed(seed_value) + dtypes = [dpnp.int32, dpnp.int64] - if numpy.issubdtype(dtype, numpy.unsignedinteger): - low = 0 + if config.all_int_types or all_int_types: + dtypes += [dpnp.int8, dpnp.int16] + if not no_unsigned: + dtypes += [dpnp.uint8, dpnp.uint16, dpnp.uint32, dpnp.uint64] - # dtype=int is needed for 0d arrays - size = numpy.prod(shape, dtype=int) - if dtype == dpnp.bool: - a = numpy.random.choice( - [False, True], size, p=[1 - probability, probability] - ) - else: - a = numpy.random.uniform(low, high, size).astype(dtype) + return dtypes - if numpy.issubdtype(a.dtype, numpy.complexfloating): - a += 1j * numpy.random.uniform(low, high, size) - a = a.reshape(shape) - if hermitian and a.size > 0: - if a.ndim > 2: - orig_shape = a.shape - # get 3D array - a = a.reshape(-1, orig_shape[-2], orig_shape[-1]) - for i in range(a.shape[0]): - a[i] = numpy.conj(a[i].T) @ a[i] - a = a.reshape(orig_shape) - else: - a = numpy.conj(a.T) @ a +def has_support_aspect16(device=None): + """ + Return True if the device supports 16-bit precision floating point operations, + False otherwise. + """ + dev = dpctl.select_default_device() if device is None else device + return dev.has_aspect_fp16 - # a.reshape(shape) returns an array in C order by default - if order != "C" and a.ndim > 1: - a = numpy.array(a, order=order) - return a + +def has_support_aspect64(device=None): + """ + Return True if the device supports 64-bit precision floating point operations, + False otherwise. + """ + dev = dpctl.select_default_device() if device is None else device + return dev.has_aspect_fp64 def is_cpu_device(device=None): @@ -335,23 +353,5 @@ def is_win_platform(): return platform.startswith("win") -def has_support_aspect16(device=None): - """ - Return True if the device supports 16-bit precision floating point operations, - False otherwise. - """ - dev = dpctl.select_default_device() if device is None else device - return dev.has_aspect_fp16 - - -def has_support_aspect64(device=None): - """ - Return True if the device supports 64-bit precision floating point operations, - False otherwise. - """ - dev = dpctl.select_default_device() if device is None else device - return dev.has_aspect_fp64 - - def numpy_version(): return numpy.lib.NumpyVersion(numpy.__version__) diff --git a/dpnp/tests/test_random.py b/dpnp/tests/test_random.py index 36323ae51b26..7524d124acd2 100644 --- a/dpnp/tests/test_random.py +++ b/dpnp/tests/test_random.py @@ -1,9 +1,8 @@ -import math import unittest import numpy import pytest -from numpy.testing import assert_allclose, assert_array_equal +from numpy.testing import assert_allclose, assert_array_equal, assert_equal import dpnp.random @@ -24,11 +23,11 @@ def check_moments( ): seed = 28041995 dpnp.random.seed(seed) - res = dpnp.asnumpy(getattr(dpnp.random, dist_name)(size=size, **params)) - var = numpy.var(res) - mean = numpy.mean(res) - assert math.isclose(var, expected_var, abs_tol=0.1) - assert math.isclose(mean, expected_mean, abs_tol=0.1) + res = getattr(dpnp.random, dist_name)(size=size, **params) + var = dpnp.var(res) + mean = dpnp.mean(res) + assert_allclose(var, expected_var, atol=0.1) + assert_allclose(mean, expected_mean, atol=0.1) def check_invalid_args(self, dist_name, params): size = 10 @@ -39,10 +38,10 @@ def check_seed(self, dist_name, params): seed = 28041990 size = 10 dpnp.random.seed(seed) - a1 = dpnp.asnumpy(getattr(dpnp.random, dist_name)(size=size, **params)) + a1 = getattr(dpnp.random, dist_name)(size=size, **params) dpnp.random.seed(seed) - a2 = dpnp.asnumpy(getattr(dpnp.random, dist_name)(size=size, **params)) - assert_allclose(a1, a2, rtol=1e-07, atol=0) + a2 = getattr(dpnp.random, dist_name)(size=size, **params) + assert_allclose(a1, a2) @pytest.mark.parametrize( @@ -127,7 +126,7 @@ def test_seed(func): a1 = func(args) dpnp.random.seed(seed) a2 = func(args) - assert_allclose(a1, a2, rtol=1e-07, atol=0) + assert dpnp.allclose(a1, a2) def test_randn_normal_distribution(): @@ -144,11 +143,11 @@ def test_randn_normal_distribution(): expected_var = 1.0 dpnp.random.seed(seed) - res = dpnp.asnumpy(dpnp.random.randn(pts)) - var = numpy.var(res) - mean = numpy.mean(res) - assert math.isclose(var, expected_var, abs_tol=0.03) - assert math.isclose(mean, expected_mean, abs_tol=0.03) + res = dpnp.random.randn(pts) + var = dpnp.var(res) + mean = dpnp.mean(res) + assert_allclose(var, expected_var, atol=1e-02) + assert_allclose(mean, expected_mean, atol=1e-02) @pytest.mark.skipif(not has_support_aspect64(), reason="Failed on Iris Xe") @@ -562,7 +561,7 @@ def test_check_sum(self): pvals = [1 / 6.0] * 6 dpnp.random.seed(seed) res = dpnp.random.multinomial(n, pvals, size) - assert_allclose(n, dpnp.asnumpy(res).sum(), rtol=1e-07, atol=0) + assert_equal(n, res.sum()) def test_invalid_args(self): n = -10 # parameter `n`, non-negative expected @@ -595,11 +594,9 @@ def test_moments(self): mean = [2.56, 3.23] cov = [[1, 0], [0, 1]] size = 10**5 - res = dpnp.asnumpy( - dpnp.random.multivariate_normal(mean=mean, cov=cov, size=size) - ) - res_mean = [numpy.mean(res.T[0]), numpy.mean(res.T[1])] - assert_allclose(res_mean, mean, rtol=1e-02, atol=0) + res = dpnp.random.multivariate_normal(mean=mean, cov=cov, size=size) + res_mean = [dpnp.mean(res.T[0]), dpnp.mean(res.T[1])] + assert dpnp.allclose(res_mean, mean) def test_invalid_args(self): mean = [2.56, 3.23] # OK @@ -709,13 +706,11 @@ def test_moments(self, df): size = 10**6 seed = 28041995 dpnp.random.seed(seed) - res = dpnp.asnumpy( - dpnp.random.noncentral_chisquare(df, nonc, size=size) - ) - var = numpy.var(res) - mean = numpy.mean(res) - assert math.isclose(var, expected_var, abs_tol=0.6) - assert math.isclose(mean, expected_mean, abs_tol=0.6) + res = dpnp.random.noncentral_chisquare(df, nonc, size=size) + var = dpnp.var(res) + mean = dpnp.mean(res) + assert_allclose(var, expected_var, atol=0.6) + assert_allclose(mean, expected_mean, atol=0.6) def test_invalid_args(self): size = 10 @@ -739,7 +734,7 @@ def test_seed(self, df): a1 = dpnp.asarray(dpnp.random.noncentral_chisquare(df, nonc, size=size)) dpnp.random.seed(seed) a2 = dpnp.asarray(dpnp.random.noncentral_chisquare(df, nonc, size=size)) - assert_allclose(a1, a2, rtol=1e-07, atol=0) + assert dpnp.allclose(a1, a2) @pytest.mark.skipif(not has_support_aspect64(), reason="Failed on Iris Xe") @@ -982,11 +977,11 @@ def test_moments(self, kappa): expected_mean = numpy.mean(numpy_res) expected_var = numpy.var(numpy_res) - res = dpnp.asnumpy(dpnp.random.vonmises(mu, kappa, size=size)) - var = numpy.var(res) - mean = numpy.mean(res) - assert math.isclose(var, expected_var, abs_tol=0.6) - assert math.isclose(mean, expected_mean, abs_tol=0.6) + res = dpnp.random.vonmises(mu, kappa, size=size) + var = dpnp.var(res) + mean = dpnp.mean(res) + assert_allclose(var, expected_var, atol=0.6) + assert_allclose(mean, expected_mean, atol=0.6) def test_invalid_args(self): size = 10 @@ -1006,7 +1001,7 @@ def test_seed(self, kappa): a1 = dpnp.asarray(dpnp.random.vonmises(mu, kappa, size=size)) dpnp.random.seed(seed) a2 = dpnp.asarray(dpnp.random.vonmises(mu, kappa, size=size)) - assert_allclose(a1, a2, rtol=1e-07, atol=0) + assert dpnp.allclose(a1, a2) @pytest.mark.skipif(not has_support_aspect64(), reason="Failed on Iris Xe") @@ -1072,9 +1067,7 @@ def test_seed(self): class TestPermutationsTestShuffle: @pytest.mark.parametrize( - "dtype", - [dpnp.float32, dpnp.float64, dpnp.int32, dpnp.int64], - ids=["float32", "float64", "int32", "int64"], + "dtype", [dpnp.float32, dpnp.float64, dpnp.int32, dpnp.int64] ) def test_shuffle(self, dtype): seed = 28041990 @@ -1091,9 +1084,7 @@ def test_shuffle(self, dtype): assert_array_equal(actual_x, desired_x) @pytest.mark.parametrize( - "dtype", - [dpnp.float32, dpnp.float64, dpnp.int32, dpnp.int64], - ids=["float32", "float64", "int32", "int64"], + "dtype", [dpnp.float32, dpnp.float64, dpnp.int32, dpnp.int64] ) def test_no_miss_numbers(self, dtype): seed = 28041990 diff --git a/dpnp/tests/test_random_state.py b/dpnp/tests/test_random_state.py index 2ed67259e0e7..9f3aee8526c5 100644 --- a/dpnp/tests/test_random_state.py +++ b/dpnp/tests/test_random_state.py @@ -15,12 +15,14 @@ from dpnp.dpnp_array import dpnp_array from dpnp.random import RandomState -from .helper import get_array, is_cpu_device +from .helper import assert_dtype_allclose, get_array, is_cpu_device # aspects of default device: _def_device = dpctl.SyclQueue().sycl_device _def_dev_has_fp64 = _def_device.has_aspect_fp64 +list_of_usm_types = ["host", "device", "shared"] + def assert_cfd(data, exp_sycl_queue, exp_usm_type=None): assert exp_sycl_queue == data.sycl_queue @@ -35,16 +37,8 @@ def get_default_floating(): class TestNormal: - @pytest.mark.parametrize( - "dtype", - [dpnp.float32, dpnp.float64, None], - ids=["float32", "float64", "None"], - ) - @pytest.mark.parametrize( - "usm_type", - ["host", "device", "shared"], - ids=["host", "device", "shared"], - ) + @pytest.mark.parametrize("dtype", [dpnp.float32, dpnp.float64, None]) + @pytest.mark.parametrize("usm_type", list_of_usm_types) def test_distr(self, dtype, usm_type): seed = 1234567 sycl_queue = dpctl.SyclQueue() @@ -89,31 +83,19 @@ def test_distr(self, dtype, usm_type): ], dtype=dtype, ) - # TODO: discuss with opneMKL: there is a difference between CPU and GPU + # TODO: discuss with oneMKL: there is a difference between CPU and GPU # generated samples since 9 digit while precision=15 for float64 - # precision = dpnp.finfo(dtype=dtype).precision - precision = ( - 8 if dtype == dpnp.float64 else dpnp.finfo(dtype=dtype).precision - ) - assert_array_almost_equal( - dpnp_data.asnumpy(), expected, decimal=precision - ) + # precision = dpnp.finfo(dtype).precision + precision = 8 if dtype == dpnp.float64 else dpnp.finfo(dtype).precision + assert_array_almost_equal(dpnp_data, expected, decimal=precision) # check if compute follows data isn't broken assert_cfd(dpnp_data, sycl_queue, usm_type) - @pytest.mark.parametrize( - "dtype", - [dpnp.float32, dpnp.float64, None], - ids=["float32", "float64", "None"], - ) - @pytest.mark.parametrize( - "usm_type", - ["host", "device", "shared"], - ids=["host", "device", "shared"], - ) + @pytest.mark.parametrize("dtype", [dpnp.float32, dpnp.float64, None]) + @pytest.mark.parametrize("usm_type", list_of_usm_types) def test_scale(self, dtype, usm_type): - mean = 7 + mean = 7.0 rs = RandomState(39567) func = lambda scale: rs.normal( loc=mean, scale=scale, dtype=dtype, usm_type=usm_type @@ -147,26 +129,22 @@ def test_scale(self, dtype, usm_type): ], ) def test_inf_loc(self, loc): - assert_equal( - RandomState(6531).normal(loc=loc, scale=1, size=1000), - get_default_floating()(loc), - ) + a = RandomState(6531).normal(loc=loc, scale=1, size=1000) + assert_equal(a, get_default_floating()(loc)) def test_inf_scale(self): - a = RandomState().normal(0, numpy.inf, size=1000).asnumpy() - assert_equal(numpy.isnan(a).any(), False) - assert_equal(numpy.isinf(a).all(), True) + a = RandomState().normal(0, numpy.inf, size=1000) + assert not dpnp.isnan(a).any() + assert dpnp.isinf(a).all() assert_equal(a.max(), numpy.inf) assert_equal(a.min(), -numpy.inf) - @pytest.mark.parametrize( - "loc", [numpy.inf, -numpy.inf], ids=["numpy.inf", "-numpy.inf"] - ) + @pytest.mark.parametrize("loc", [numpy.inf, -numpy.inf]) def test_inf_loc_scale(self, loc): - a = RandomState().normal(loc=loc, scale=numpy.inf, size=1000).asnumpy() - assert_equal(numpy.isnan(a).all(), False) - assert_equal(numpy.nanmin(a), loc) - assert_equal(numpy.nanmax(a), loc) + a = RandomState().normal(loc=loc, scale=numpy.inf, size=1000) + assert not dpnp.isnan(a).all() + assert_equal(dpnp.nanmin(a), loc) + assert_equal(dpnp.nanmax(a), loc) def test_extreme_bounds(self): dtype = get_default_floating() @@ -222,14 +200,12 @@ def test_fallback(self, loc, scale): ) # dpnp accepts only scalar as low and/or high, in other cases it will be a fallback to numpy - actual = data.asnumpy() expected = numpy.random.RandomState(seed).normal( loc=get_array(numpy, loc), scale=get_array(numpy, scale), size=size ) - dtype = get_default_floating() - precision = dpnp.finfo(dtype=dtype).precision - assert_array_almost_equal(actual, expected, decimal=precision) + precision = dpnp.finfo(get_default_floating()).precision + assert_array_almost_equal(data, expected, decimal=precision) # check if compute follows data isn't broken assert_cfd(data, sycl_queue) @@ -276,11 +252,7 @@ def test_invalid_usm_type(self, usm_type): class TestRand: - @pytest.mark.parametrize( - "usm_type", - ["host", "device", "shared"], - ids=["host", "device", "shared"], - ) + @pytest.mark.parametrize("usm_type", list_of_usm_types) def test_distr(self, usm_type): seed = 28042 sycl_queue = dpctl.SyclQueue() @@ -308,22 +280,20 @@ def test_distr(self, usm_type): dtype=dtype, ) - precision = dpnp.finfo(dtype=dtype).precision - assert_array_almost_equal(data.asnumpy(), expected, decimal=precision) + precision = dpnp.finfo(dtype).precision + assert_array_almost_equal(data, expected, decimal=precision) assert_cfd(data, sycl_queue, usm_type) # call with the same seed has to draw the same values data = RandomState(seed, sycl_queue=sycl_queue).rand( 3, 2, usm_type=usm_type ) - assert_array_almost_equal(data.asnumpy(), expected, decimal=precision) + assert_array_almost_equal(data, expected, decimal=precision) assert_cfd(data, sycl_queue, usm_type) # call with omitted dimensions has to draw the first element from expected data = RandomState(seed, sycl_queue=sycl_queue).rand(usm_type=usm_type) - assert_array_almost_equal( - data.asnumpy(), expected[0, 0], decimal=precision - ) + assert_array_almost_equal(data, expected[0, 0], decimal=precision) assert_cfd(data, sycl_queue, usm_type) # rand() is an alias on random_sample(), map arguments @@ -367,11 +337,7 @@ class TestRandInt: [int, dpnp.int32, dpnp.int_], ids=["int", "dpnp.int32", "dpnp.int_"], ) - @pytest.mark.parametrize( - "usm_type", - ["host", "device", "shared"], - ids=["host", "device", "shared"], - ) + @pytest.mark.parametrize("usm_type", list_of_usm_types) def test_distr(self, dtype, usm_type): seed = 9864 low = 1 @@ -390,21 +356,21 @@ def test_distr(self, dtype, usm_type): expected = numpy.array([[4, 1], [5, 3], [5, 7]], dtype=numpy.int32) else: expected = numpy.array([[1, 2], [1, 5], [3, 7]], dtype=numpy.int32) - assert_array_equal(data.asnumpy(), expected) + assert_array_equal(data, expected) assert_cfd(data, sycl_queue, usm_type) # call with the same seed has to draw the same values data = RandomState(seed, sycl_queue=sycl_queue).randint( low=low, high=high, size=(3, 2), dtype=dtype, usm_type=usm_type ) - assert_array_equal(data.asnumpy(), expected) + assert_array_equal(data, expected) assert_cfd(data, sycl_queue, usm_type) # call with omitted dimensions has to draw the first element from expected data = RandomState(seed, sycl_queue=sycl_queue).randint( low=low, high=high, dtype=dtype, usm_type=usm_type ) - assert_array_equal(data.asnumpy(), expected[0, 0]) + assert_array_equal(data, expected[0, 0]) assert_cfd(data, sycl_queue, usm_type) # rand() is an alias on random_sample(), map arguments @@ -428,7 +394,7 @@ def test_float_bounds(self): expected = numpy.array([4, 4, 3, 3, 1, 0, 3], dtype=numpy.int32) else: expected = numpy.array([0, 1, 4, 0, 3, 3, 3], dtype=numpy.int32) - assert_array_equal(actual.asnumpy(), expected) + assert_array_equal(actual, expected) def test_negative_bounds(self): actual = RandomState(5143).randint(low=-15.74, high=-3, size=(2, 7)) @@ -448,12 +414,12 @@ def test_negative_bounds(self): ], dtype=numpy.int32, ) - assert_array_equal(actual.asnumpy(), expected) + assert_array_equal(actual, expected) def test_negative_interval(self): rs = RandomState(3567) - assert_equal(-5 <= rs.randint(-5, -1) < -1, True) + assert -5 <= rs.randint(-5, -1) < -1 x = rs.randint(-7, -1, 5) assert_equal(-7 <= x, True) @@ -520,8 +486,8 @@ def test_full_range(self): def test_in_bounds_fuzz(self): for high in [4, 8, 16]: vals = RandomState().randint(2, high, size=2**16) - assert_equal(vals.max() < high, True) - assert_equal(vals.min() >= 2, True) + assert vals.max() < high + assert vals.min() >= 2 @pytest.mark.parametrize( "zero_size", @@ -553,9 +519,7 @@ def test_bounds_fallback(self, low, high): size = (3, 2, 5) # dpnp accepts only scalar as low and/or high, in other cases it will be a fallback to numpy - actual = ( - RandomState(seed).randint(low=low, high=high, size=size).asnumpy() - ) + actual = RandomState(seed).randint(low=low, high=high, size=size) expected = numpy.random.RandomState(seed).randint( low=get_array(numpy, low), high=get_array(numpy, high), size=size ) @@ -585,15 +549,13 @@ def test_dtype_fallback(self, dtype): ) # dtype must be int or dpnp.int32, in other cases it will be a fallback to numpy - actual = ( - RandomState(seed) - .randint(low=low, high=high, size=size, dtype=dtype) - .asnumpy() + actual = RandomState(seed).randint( + low=low, high=high, size=size, dtype=dtype ) expected = numpy.random.RandomState(seed).randint( low=low, high=high, size=size, dtype=dtype ) - assert_equal(actual, expected) + assert_array_equal(actual, expected) assert_raises(TypeError, RandomState().randint, dtype=dtype) @pytest.mark.parametrize( @@ -605,11 +567,7 @@ def test_invalid_usm_type(self, usm_type): class TestRandN: - @pytest.mark.parametrize( - "usm_type", - ["host", "device", "shared"], - ids=["host", "device", "shared"], - ) + @pytest.mark.parametrize("usm_type", list_of_usm_types) def test_distr(self, usm_type): seed = 3649 sycl_queue = dpctl.SyclQueue() @@ -637,20 +595,20 @@ def test_distr(self, usm_type): dtype=dtype, ) - # TODO: discuss with opneMKL: there is a difference between CPU and GPU + # TODO: discuss with oneMKL: there is a difference between CPU and GPU # generated samples since 9 digit while precision=15 for float64 - # precision = dpnp.finfo(dtype=numpy.float64).precision - precision = dpnp.finfo(dtype=numpy.float32).precision - assert_array_almost_equal(data.asnumpy(), expected, decimal=precision) + # precision = dpnp.finfo(numpy.float64).precision + precision = dpnp.finfo(numpy.float32).precision + assert_array_almost_equal(data, expected, decimal=precision) # call with the same seed has to draw the same values data = RandomState(seed, sycl_queue=sycl_queue).randn( 3, 2, usm_type=usm_type ) - assert_array_almost_equal(data.asnumpy(), expected, decimal=precision) + assert_array_almost_equal(data, expected, decimal=precision) # call with omitted dimensions has to draw the first element from expected - actual = dpnp.asnumpy(RandomState(seed).randn(usm_type=usm_type)) + actual = RandomState(seed).randn(usm_type=usm_type) assert_array_almost_equal(actual, expected[0, 0], decimal=precision) # randn() is an alias on standard_normal(), map arguments @@ -690,21 +648,19 @@ def test_wrong_dims(self): class TestSeed: @pytest.mark.parametrize( - "func", - ["normal", "standard_normal", "random_sample", "uniform"], - ids=["normal", "standard_normal", "random_sample", "uniform"], + "func", ["normal", "standard_normal", "random_sample", "uniform"] ) def test_scalar(self, func): seed = 28041997 size = (3, 2, 4) rs = RandomState(seed) - a1 = getattr(rs, func)(size=size).asnumpy() + a1 = getattr(rs, func)(size=size) rs = RandomState(seed) - a2 = getattr(rs, func)(size=size).asnumpy() + a2 = getattr(rs, func)(size=size) - precision = dpnp.finfo(dtype=numpy.float64).precision + precision = dpnp.finfo(numpy.float64).precision assert_array_almost_equal(a1, a2, decimal=precision) @pytest.mark.usefixtures("allow_fall_back_on_numpy") @@ -736,9 +692,9 @@ def test_array_range(self, seed): pytest.skip("seed as a scalar is only supported on GPU") size = 15 - a1 = RandomState(seed).uniform(size=size).asnumpy() - a2 = RandomState(seed).uniform(size=size).asnumpy() - assert_allclose(a1, a2, rtol=1e-07, atol=0) + a1 = RandomState(seed).uniform(size=size) + a2 = RandomState(seed).uniform(size=size) + assert dpnp.allclose(a1, a2) @pytest.mark.parametrize( "seed", @@ -840,11 +796,7 @@ def test_invalid_shape(self, seed): class TestStandardNormal: - @pytest.mark.parametrize( - "usm_type", - ["host", "device", "shared"], - ids=["host", "device", "shared"], - ) + @pytest.mark.parametrize("usm_type", list_of_usm_types) def test_distr(self, usm_type): seed = 1234567 sycl_queue = dpctl.SyclQueue() @@ -874,22 +826,20 @@ def test_distr(self, usm_type): dtype=dtype, ) - # TODO: discuss with opneMKL: there is a difference between CPU and GPU + # TODO: discuss with oneMKL: there is a difference between CPU and GPU # generated samples since 9 digit while precision=15 for float64 - # precision = dpnp.finfo(dtype=numpy.float64).precision - precision = dpnp.finfo(dtype=numpy.float32).precision - assert_array_almost_equal(data.asnumpy(), expected, decimal=precision) + # precision = dpnp.finfo(numpy.float64).precision + precision = dpnp.finfo(numpy.float32).precision + assert_array_almost_equal(data, expected, decimal=precision) # call with the same seed has to draw the same values data = RandomState(seed, sycl_queue=sycl_queue).standard_normal( size=(4, 2), usm_type=usm_type ) - assert_array_almost_equal(data.asnumpy(), expected, decimal=precision) + assert_array_almost_equal(data, expected, decimal=precision) # call with omitted dimensions has to draw the first element from expected - actual = dpnp.asnumpy( - RandomState(seed).standard_normal(usm_type=usm_type) - ) + actual = RandomState(seed).standard_normal(usm_type=usm_type) assert_array_almost_equal(actual, expected[0, 0], decimal=precision) # random_sample() is an alias on uniform(), map arguments @@ -920,11 +870,7 @@ def test_wrong_dims(self): class TestRandSample: - @pytest.mark.parametrize( - "usm_type", - ["host", "device", "shared"], - ids=["host", "device", "shared"], - ) + @pytest.mark.parametrize("usm_type", list_of_usm_types) def test_distr(self, usm_type): seed = 12657 sycl_queue = dpctl.SyclQueue() @@ -954,17 +900,14 @@ def test_distr(self, usm_type): dtype=dtype, ) - precision = dpnp.finfo(dtype=dtype).precision - assert_array_almost_equal(data.asnumpy(), expected, decimal=precision) + precision = dpnp.finfo(dtype).precision + assert_array_almost_equal(data, expected, decimal=precision) # call with omitted dimensions has to draw the first element from expected data = RandomState(seed, sycl_queue=sycl_queue).random_sample( usm_type=usm_type ) - assert_array_almost_equal( - data.asnumpy(), expected[0, 0], decimal=precision - ) - + assert_array_almost_equal(data, expected[0, 0], decimal=precision) # random_sample() is an alias on uniform(), map arguments with mock.patch("dpnp.random.RandomState.uniform") as m: RandomState(seed).random_sample((4, 2), usm_type=usm_type) @@ -999,15 +942,9 @@ class TestUniform: ids=["(low, high)=[1.23, 10.54]", "(low, high)=[10.54, 1.23]"], ) @pytest.mark.parametrize( - "dtype", - [dpnp.float32, dpnp.float64, dpnp.int32, None], - ids=["float32", "float64", "int32", "None"], - ) - @pytest.mark.parametrize( - "usm_type", - ["host", "device", "shared"], - ids=["host", "device", "shared"], + "dtype", [dpnp.float32, dpnp.float64, dpnp.int32, None] ) + @pytest.mark.parametrize("usm_type", list_of_usm_types) def test_distr(self, bounds, dtype, usm_type): seed = 28041997 low = bounds[0] @@ -1024,8 +961,7 @@ def test_distr(self, bounds, dtype, usm_type): return # get drawn samples by dpnp - dpnp_data = func() - actual = dpnp_data.asnumpy() + actual = func() # default dtype depends on fp64 support by the device dtype = get_default_floating() if dtype is None else dtype @@ -1038,9 +974,8 @@ def test_distr(self, bounds, dtype, usm_type): [2.030351535445079, 4.533497077834326], ] ) - assert_array_almost_equal( - actual, expected, decimal=dpnp.finfo(dtype=dtype).precision - ) + precision = dpnp.finfo(dtype).precision + assert_array_almost_equal(actual, expected, decimal=precision) else: expected = numpy.array([[3, 8], [2, 4], [1, 4]]) assert_array_equal(actual, expected) @@ -1053,26 +988,19 @@ def test_distr(self, bounds, dtype, usm_type): [3.316473517549554, 8.428297791221597], ] ) - assert_array_almost_equal( - actual, expected, decimal=dpnp.finfo(dtype=dtype).precision - ) + precision = dpnp.finfo(dtype).precision + assert_array_almost_equal(actual, expected, decimal=precision) else: expected = numpy.array([[1, 4], [5, 1], [3, 7]]) assert_array_equal(actual, expected) # check if compute follows data isn't broken - assert_cfd(dpnp_data, sycl_queue, usm_type) + assert_cfd(actual, sycl_queue, usm_type) @pytest.mark.parametrize( - "dtype", - [dpnp.float32, dpnp.float64, dpnp.int32, None], - ids=["float32", "float64", "int32", "None"], - ) - @pytest.mark.parametrize( - "usm_type", - ["host", "device", "shared"], - ids=["host", "device", "shared"], + "dtype", [dpnp.float32, dpnp.float64, dpnp.int32, None] ) + @pytest.mark.parametrize("usm_type", list_of_usm_types) def test_low_high_equal(self, dtype, usm_type): seed = 28045 low = high = 3.75 @@ -1088,18 +1016,13 @@ def test_low_high_equal(self, dtype, usm_type): return # get drawn samples by dpnp - actual = func().asnumpy() + actual = func() # default dtype depends on fp64 support by the device dtype = get_default_floating() if dtype is None else dtype expected = numpy.full(shape=shape, fill_value=low, dtype=dtype) - if dtype == dpnp.int32: - assert_array_equal(actual, expected) - else: - assert_array_almost_equal( - actual, expected, decimal=dpnp.finfo(dtype=dtype).precision - ) + assert_dtype_allclose(actual, expected) @pytest.mark.usefixtures("allow_fall_back_on_numpy") def test_range_bounds(self): @@ -1137,14 +1060,12 @@ def test_fallback(self, low, high): ) # dpnp accepts only scalar as low and/or high, in other cases it will be a fallback to numpy - actual = data.asnumpy() expected = numpy.random.RandomState(seed).uniform( low=get_array(numpy, low), high=get_array(numpy, high), size=size ) - dtype = get_default_floating() - precision = dpnp.finfo(dtype=dtype).precision - assert_array_almost_equal(actual, expected, decimal=precision) + precision = dpnp.finfo(get_default_floating()).precision + assert_array_almost_equal(data, expected, decimal=precision) # check if compute follows data isn't broken assert_cfd(data, sycl_queue)