Skip to content

Commit d57ac14

Browse files
authored
update test_random.py and test_random_state.py (#2349)
In this PR, `test_random.py` and `test_random_state.py` are updated. In addition, the functions in `dpnp/tests/helper.py` are re-organized in alphabetic order.
1 parent 456e55c commit d57ac14

File tree

3 files changed

+247
-335
lines changed

3 files changed

+247
-335
lines changed

dpnp/tests/helper.py

Lines changed: 133 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -87,61 +87,102 @@ def assert_dtype_allclose(
8787
assert dpnp_arr.dtype == numpy_arr.dtype
8888

8989

90-
def get_integer_dtypes(all_int_types=False, no_unsigned=False):
91-
"""
92-
Build a list of integer types supported by DPNP.
90+
def generate_random_numpy_array(
91+
shape,
92+
dtype=None,
93+
order="C",
94+
hermitian=False,
95+
seed_value=None,
96+
low=-10,
97+
high=10,
98+
probability=0.5,
99+
):
93100
"""
101+
Generate a random numpy array with the specified shape and dtype.
94102
95-
dtypes = [dpnp.int32, dpnp.int64]
103+
If required, the array can be made Hermitian (for complex data types) or
104+
symmetric (for real data types).
96105
97-
if config.all_int_types or all_int_types:
98-
dtypes += [dpnp.int8, dpnp.int16]
99-
if not no_unsigned:
100-
dtypes += [dpnp.uint8, dpnp.uint16, dpnp.uint32, dpnp.uint64]
106+
Parameters
107+
----------
108+
shape : tuple
109+
Shape of the generated array.
110+
dtype : str or dtype, optional
111+
Desired data-type for the output array.
112+
If not specified, data type will be determined by numpy.
101113
102-
return dtypes
114+
Default : ``None``
115+
order : {"C", "F"}, optional
116+
Specify the memory layout of the output array.
103117
118+
Default: ``"C"``.
119+
hermitian : bool, optional
120+
If True, generates a Hermitian (symmetric if `dtype` is real) matrix.
104121
105-
def get_complex_dtypes(device=None):
106-
"""
107-
Build a list of complex types supported by DPNP based on device capabilities.
108-
"""
122+
Default : ``False``
123+
seed_value : int, optional
124+
The seed value to initialize the random number generator.
109125
110-
dev = dpctl.select_default_device() if device is None else device
126+
Default : ``None``
127+
low : {int, float}, optional
128+
Lower boundary of the generated samples from a uniform distribution.
111129
112-
# add complex types
113-
dtypes = [dpnp.complex64]
114-
if dev.has_aspect_fp64:
115-
dtypes.append(dpnp.complex128)
116-
return dtypes
130+
Default : ``-10``.
131+
high : {int, float}, optional
132+
Upper boundary of the generated samples from a uniform distribution.
117133
134+
Default : ``10``.
135+
probability : float, optional
136+
If dtype is bool, the probability of True. Ignored for other dtypes.
137+
138+
Default : ``0.5``.
139+
Returns
140+
-------
141+
out : numpy.ndarray
142+
A random numpy array of the specified shape, dtype and memory layout.
143+
The array is Hermitian or symmetric if `hermitian` is True.
144+
145+
Note:
146+
For arrays with more than 2 dimensions, the Hermitian or
147+
symmetric property is ensured for each 2D sub-array.
118148
119-
def get_float_dtypes(no_float16=True, device=None):
120-
"""
121-
Build a list of floating types supported by DPNP based on device capabilities.
122149
"""
123150

124-
dev = dpctl.select_default_device() if device is None else device
151+
if seed_value is None:
152+
seed_value = 42
153+
numpy.random.seed(seed_value)
125154

126-
# add floating types
127-
dtypes = []
128-
if not no_float16 and dev.has_aspect_fp16:
129-
dtypes.append(dpnp.float16)
155+
if numpy.issubdtype(dtype, numpy.unsignedinteger):
156+
low = 0
130157

131-
dtypes.append(dpnp.float32)
132-
if dev.has_aspect_fp64:
133-
dtypes.append(dpnp.float64)
134-
return dtypes
158+
# dtype=int is needed for 0d arrays
159+
size = numpy.prod(shape, dtype=int)
160+
if dtype == dpnp.bool:
161+
a = numpy.random.choice(
162+
[False, True], size, p=[1 - probability, probability]
163+
)
164+
else:
165+
a = numpy.random.uniform(low, high, size).astype(dtype)
135166

167+
if numpy.issubdtype(a.dtype, numpy.complexfloating):
168+
a += 1j * numpy.random.uniform(low, high, size)
136169

137-
def get_float_complex_dtypes(no_float16=True, device=None):
138-
"""
139-
Build a list of floating and complex types supported by DPNP based on device capabilities.
140-
"""
170+
a = a.reshape(shape)
171+
if hermitian and a.size > 0:
172+
if a.ndim > 2:
173+
orig_shape = a.shape
174+
# get 3D array
175+
a = a.reshape(-1, orig_shape[-2], orig_shape[-1])
176+
for i in range(a.shape[0]):
177+
a[i] = numpy.conj(a[i].T) @ a[i]
178+
a = a.reshape(orig_shape)
179+
else:
180+
a = numpy.conj(a.T) @ a
141181

142-
dtypes = get_float_dtypes(no_float16, device)
143-
dtypes.extend(get_complex_dtypes(device))
144-
return dtypes
182+
# a.reshape(shape) returns an array in C order by default
183+
if order != "C" and a.ndim > 1:
184+
a = numpy.array(a, order=order)
185+
return a
145186

146187

147188
def get_abs_array(data, dtype=None):
@@ -214,102 +255,79 @@ def get_array(xp, a):
214255
return a
215256

216257

217-
def generate_random_numpy_array(
218-
shape,
219-
dtype=None,
220-
order="C",
221-
hermitian=False,
222-
seed_value=None,
223-
low=-10,
224-
high=10,
225-
probability=0.5,
226-
):
258+
def get_complex_dtypes(device=None):
259+
"""
260+
Build a list of complex types supported by DPNP based on device capabilities.
227261
"""
228-
Generate a random numpy array with the specified shape and dtype.
229262

230-
If required, the array can be made Hermitian (for complex data types) or
231-
symmetric (for real data types).
263+
dev = dpctl.select_default_device() if device is None else device
232264

233-
Parameters
234-
----------
235-
shape : tuple
236-
Shape of the generated array.
237-
dtype : str or dtype, optional
238-
Desired data-type for the output array.
239-
If not specified, data type will be determined by numpy.
265+
# add complex types
266+
dtypes = [dpnp.complex64]
267+
if dev.has_aspect_fp64:
268+
dtypes.append(dpnp.complex128)
269+
return dtypes
240270

241-
Default : ``None``
242-
order : {"C", "F"}, optional
243-
Specify the memory layout of the output array.
244271

245-
Default: ``"C"``.
246-
hermitian : bool, optional
247-
If True, generates a Hermitian (symmetric if `dtype` is real) matrix.
272+
def get_float_dtypes(no_float16=True, device=None):
273+
"""
274+
Build a list of floating types supported by DPNP based on device capabilities.
275+
"""
248276

249-
Default : ``False``
250-
seed_value : int, optional
251-
The seed value to initialize the random number generator.
277+
dev = dpctl.select_default_device() if device is None else device
252278

253-
Default : ``None``
254-
low : {int, float}, optional
255-
Lower boundary of the generated samples from a uniform distribution.
279+
# add floating types
280+
dtypes = []
281+
if not no_float16 and dev.has_aspect_fp16:
282+
dtypes.append(dpnp.float16)
256283

257-
Default : ``-10``.
258-
high : {int, float}, optional
259-
Upper boundary of the generated samples from a uniform distribution.
284+
dtypes.append(dpnp.float32)
285+
if dev.has_aspect_fp64:
286+
dtypes.append(dpnp.float64)
287+
return dtypes
260288

261-
Default : ``10``.
262-
probability : float, optional
263-
If dtype is bool, the probability of True. Ignored for other dtypes.
264289

265-
Default : ``0.5``.
266-
Returns
267-
-------
268-
out : numpy.ndarray
269-
A random numpy array of the specified shape, dtype and memory layout.
270-
The array is Hermitian or symmetric if `hermitian` is True.
290+
def get_float_complex_dtypes(no_float16=True, device=None):
291+
"""
292+
Build a list of floating and complex types supported by DPNP based on device capabilities.
293+
"""
294+
295+
dtypes = get_float_dtypes(no_float16, device)
296+
dtypes.extend(get_complex_dtypes(device))
297+
return dtypes
271298

272-
Note:
273-
For arrays with more than 2 dimensions, the Hermitian or
274-
symmetric property is ensured for each 2D sub-array.
275299

300+
def get_integer_dtypes(all_int_types=False, no_unsigned=False):
301+
"""
302+
Build a list of integer types supported by DPNP.
276303
"""
277304

278-
if seed_value is None:
279-
seed_value = 42
280-
numpy.random.seed(seed_value)
305+
dtypes = [dpnp.int32, dpnp.int64]
281306

282-
if numpy.issubdtype(dtype, numpy.unsignedinteger):
283-
low = 0
307+
if config.all_int_types or all_int_types:
308+
dtypes += [dpnp.int8, dpnp.int16]
309+
if not no_unsigned:
310+
dtypes += [dpnp.uint8, dpnp.uint16, dpnp.uint32, dpnp.uint64]
284311

285-
# dtype=int is needed for 0d arrays
286-
size = numpy.prod(shape, dtype=int)
287-
if dtype == dpnp.bool:
288-
a = numpy.random.choice(
289-
[False, True], size, p=[1 - probability, probability]
290-
)
291-
else:
292-
a = numpy.random.uniform(low, high, size).astype(dtype)
312+
return dtypes
293313

294-
if numpy.issubdtype(a.dtype, numpy.complexfloating):
295-
a += 1j * numpy.random.uniform(low, high, size)
296314

297-
a = a.reshape(shape)
298-
if hermitian and a.size > 0:
299-
if a.ndim > 2:
300-
orig_shape = a.shape
301-
# get 3D array
302-
a = a.reshape(-1, orig_shape[-2], orig_shape[-1])
303-
for i in range(a.shape[0]):
304-
a[i] = numpy.conj(a[i].T) @ a[i]
305-
a = a.reshape(orig_shape)
306-
else:
307-
a = numpy.conj(a.T) @ a
315+
def has_support_aspect16(device=None):
316+
"""
317+
Return True if the device supports 16-bit precision floating point operations,
318+
False otherwise.
319+
"""
320+
dev = dpctl.select_default_device() if device is None else device
321+
return dev.has_aspect_fp16
308322

309-
# a.reshape(shape) returns an array in C order by default
310-
if order != "C" and a.ndim > 1:
311-
a = numpy.array(a, order=order)
312-
return a
323+
324+
def has_support_aspect64(device=None):
325+
"""
326+
Return True if the device supports 64-bit precision floating point operations,
327+
False otherwise.
328+
"""
329+
dev = dpctl.select_default_device() if device is None else device
330+
return dev.has_aspect_fp64
313331

314332

315333
def is_cpu_device(device=None):
@@ -335,23 +353,5 @@ def is_win_platform():
335353
return platform.startswith("win")
336354

337355

338-
def has_support_aspect16(device=None):
339-
"""
340-
Return True if the device supports 16-bit precision floating point operations,
341-
False otherwise.
342-
"""
343-
dev = dpctl.select_default_device() if device is None else device
344-
return dev.has_aspect_fp16
345-
346-
347-
def has_support_aspect64(device=None):
348-
"""
349-
Return True if the device supports 64-bit precision floating point operations,
350-
False otherwise.
351-
"""
352-
dev = dpctl.select_default_device() if device is None else device
353-
return dev.has_aspect_fp64
354-
355-
356356
def numpy_version():
357357
return numpy.lib.NumpyVersion(numpy.__version__)

0 commit comments

Comments
 (0)