Skip to content

update test_random.py and test_random_state.py #2349

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

Merged
merged 8 commits into from
Feb 27, 2025
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
266 changes: 133 additions & 133 deletions dpnp/tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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__)
Loading
Loading