Skip to content

Commit f539955

Browse files
Merge master into skip_cholesky_tests_dg2
2 parents ac2221d + 266faed commit f539955

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5757
* Fixed `dpnp.unique` with 1d input array and `axis=0`, `equal_nan=True` keywords passed where the produced result doesn't collapse the NaNs [#2530](https://github.com/IntelPython/dpnp/pull/2530)
5858
* Resolved issue when `dpnp.ndarray` constructor is called with `dpnp.ndarray.data` as `buffer` keyword [#2533](https://github.com/IntelPython/dpnp/pull/2533)
5959
* Fixed `dpnp.linalg.cond` to always return a real dtype [#2547](https://github.com/IntelPython/dpnp/pull/2547)
60+
* Resolved the issue in `dpnp.random` functions to allow any value of `size` where each element is castable to `Py_ssize_t` type [#2578](https://github.com/IntelPython/dpnp/pull/2578)
6061

6162
### Security
6263

dpnp/dpnp_utils/dpnp_algo_utils.pyx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,10 +390,26 @@ cpdef inline tuple _object_to_tuple(object obj):
390390
if obj is None:
391391
return ()
392392

393-
if cpython.PySequence_Check(obj):
394-
return tuple(obj)
393+
# dpnp.ndarray unconditionally succeeds in PySequence_Check as it implements __getitem__
394+
if cpython.PySequence_Check(obj) and not dpnp.is_supported_array_type(obj):
395+
if isinstance(obj, numpy.ndarray):
396+
obj = numpy.atleast_1d(obj)
397+
398+
nd = len(obj)
399+
shape = []
400+
401+
for i in range(0, nd):
402+
if cpython.PyBool_Check(obj[i]):
403+
raise TypeError("DPNP object_to_tuple(): no item in size can be bool")
404+
405+
# Assumes each item is castable to Py_ssize_t,
406+
# otherwise TypeError will be raised
407+
shape.append(<Py_ssize_t> obj[i])
408+
return tuple(shape)
395409

396410
if dpnp.isscalar(obj):
411+
if cpython.PyBool_Check(obj):
412+
raise TypeError("DPNP object_to_tuple(): 'obj' can't be bool")
397413
return (obj, )
398414

399415
raise ValueError("DPNP object_to_tuple(): 'obj' should be 'None', collections.abc.Sequence, or 'int'")

dpnp/tests/test_random_state.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
is_cpu_device,
2222
is_gpu_device,
2323
)
24+
from .third_party.cupy import testing
2425

2526
# aspects of default device:
2627
_def_device = dpctl.SyclQueue().sycl_device
@@ -1115,3 +1116,31 @@ def test_invalid_dtype(self, dtype):
11151116
def test_invalid_usm_type(self, usm_type):
11161117
# dtype must be float32 or float64
11171118
assert_raises(ValueError, RandomState().uniform, usm_type=usm_type)
1119+
1120+
def test_size_castable_to_integer(self):
1121+
M = numpy.int64(31)
1122+
N = numpy.int64(31)
1123+
K = 63 # plain Python int
1124+
1125+
sizes = [(M, K), (M, N), (K, N)]
1126+
for size in sizes:
1127+
result = RandomState().uniform(size=size)
1128+
assert result.shape == size
1129+
1130+
@testing.with_requires("numpy>=2.3.2")
1131+
@pytest.mark.parametrize("xp", [numpy, dpnp])
1132+
@pytest.mark.parametrize(
1133+
"size",
1134+
[True, [True], dpnp.bool(True), numpy.array(True), numpy.array([True])],
1135+
)
1136+
def test_bool_size(self, xp, size):
1137+
rs = xp.random.RandomState()
1138+
assert_raises(TypeError, rs.uniform, size=size)
1139+
1140+
@pytest.mark.parametrize("size", [numpy.array(1), numpy.array([2])])
1141+
def test_numpy_ndarray_size(self, size):
1142+
result = RandomState().uniform(size=size)
1143+
assert result.shape == size
1144+
1145+
def test_dpnp_ndarray_size(self):
1146+
assert_raises(ValueError, RandomState().uniform, size=dpnp.array(1))

0 commit comments

Comments
 (0)