Skip to content

Commit 836fe4c

Browse files
FIX: update random.uniform (#375)
* FIX: update random.uniform * update
1 parent 40f3c1a commit 836fe4c

File tree

5 files changed

+44
-50
lines changed

5 files changed

+44
-50
lines changed

dpnp/random/_random.pyx

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -841,27 +841,37 @@ cpdef dparray dpnp_standard_normal(size):
841841
return result
842842

843843

844-
cpdef dparray dpnp_uniform(long low, long high, size, dtype=numpy.int32):
844+
cpdef dparray dpnp_uniform(long low, long high, size, dtype):
845845
"""
846846
Returns an array populated with samples from standard uniform distribution.
847847
Generates a matrix filled with random numbers sampled from a
848848
uniform distribution of the certain left (low) and right (high)
849849
bounds.
850850
851851
"""
852-
# convert string type names (dparray.dtype) to C enum DPNPFuncType
853-
cdef DPNPFuncType param1_type = dpnp_dtype_to_DPNPFuncType(dtype)
854852

855-
# get the FPTR data structure
856-
cdef DPNPFuncData kernel_data = get_dpnp_function_ptr(DPNP_FN_RNG_UNIFORM, param1_type, param1_type)
853+
cdef dparray result
854+
cdef DPNPFuncType param1_type
855+
cdef DPNPFuncData kernel_data
856+
cdef fptr_custom_rng_uniform_c_1out_t func
857857

858-
result_type = dpnp_DPNPFuncType_to_dtype( < size_t > kernel_data.return_type)
859-
# ceate result array with type given by FPTR data
860-
cdef dparray result = dparray(size, dtype=result_type)
858+
if low == high:
859+
result = dparray(size, dtype=dtype)
860+
result.fill(low)
861+
else:
862+
# convert string type names (dparray.dtype) to C enum DPNPFuncType
863+
param1_type = dpnp_dtype_to_DPNPFuncType(dtype)
861864

862-
cdef fptr_custom_rng_uniform_c_1out_t func = <fptr_custom_rng_uniform_c_1out_t > kernel_data.ptr
863-
# call FPTR function
864-
func(result.get_data(), low, high, result.size)
865+
# get the FPTR data structure
866+
kernel_data = get_dpnp_function_ptr(DPNP_FN_RNG_UNIFORM, param1_type, param1_type)
867+
868+
result_type = dpnp_DPNPFuncType_to_dtype( < size_t > kernel_data.return_type)
869+
# ceate result array with type given by FPTR data
870+
result = dparray(size, dtype=result_type)
871+
872+
func = <fptr_custom_rng_uniform_c_1out_t > kernel_data.ptr
873+
# call FPTR function
874+
func(result.get_data(), low, high, result.size)
865875

866876
return result
867877

dpnp/random/dpnp_iface_random.py

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,28 +1476,20 @@ def uniform(low=0.0, high=1.0, size=None):
14761476
uniform(low=0.0, high=1.0, size=None)
14771477
14781478
Draw samples from a uniform distribution.
1479-
Samples are uniformly distributed over the half-open interval
1480-
``[low, high)`` (includes low, but excludes high). In other words,
1481-
any value within the given interval is equally likely to be drawn
1482-
by `uniform`.
14831479
1484-
Parameters
1485-
----------
1486-
low : float, optional
1487-
Lower boundary of the output interval. All values generated will be
1488-
greater than or equal to low. The default value is 0.
1489-
high : float
1490-
Upper boundary of the output interval. All values generated will be
1491-
less than high. The default value is 1.0.
1492-
size : int or tuple of ints, optional
1493-
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
1494-
``m * n * k`` samples are drawn. If size is ``None`` (default),
1495-
a single value is returned if ``low`` and ``high`` are both scalars.
1480+
For full documentation refer to :obj:`numpy.random.uniform`.
14961481
1497-
Returns
1498-
-------
1499-
out : array or scalar
1500-
Drawn samples from the parameterized uniform distribution.
1482+
Limitations
1483+
-----------
1484+
Parameters ``low`` and ``high`` are supported as scalar.
1485+
Otherwise, :obj:`numpy.random.uniform(low, high, size)` samples are drawn.
1486+
Output array data type is :obj:`dpnp.float64`.
1487+
1488+
Examples
1489+
--------
1490+
Draw samples from the distribution:
1491+
>>> low, high = 0, 0.1 # low and high
1492+
>>> s = dpnp.random.uniform(low, high, 10000)
15011493
15021494
See Also
15031495
--------
@@ -1506,16 +1498,14 @@ def uniform(low=0.0, high=1.0, size=None):
15061498
"""
15071499

15081500
if not use_origin_backend(low):
1509-
if low == high:
1510-
# TODO:
1511-
# currently dparray.full is not implemented
1512-
# return dpnp.dparray.dparray.full(size, low, dtype=numpy.float64)
1513-
message = "`low` equal to `high`, should return an array, filled with `low` value."
1514-
message += " Currently not supported. See: numpy.full TODO"
1515-
checker_throw_runtime_error("uniform", message)
1516-
elif low > high:
1517-
low, high = high, low
1518-
return dpnp_uniform(low, high, size, dtype=numpy.float64)
1501+
if not dpnp.isscalar(low):
1502+
pass
1503+
elif not dpnp.isscalar(high):
1504+
pass
1505+
else:
1506+
if low > high:
1507+
low, high = high, low
1508+
return dpnp_uniform(low, high, size, dtype=numpy.float64)
15191509

15201510
return call_origin(numpy.random.uniform, low, high, size)
15211511

tests/skipped_tests.tbl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,8 +1282,6 @@ tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsUnif
12821282
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsUniform_param_3_{high_shape=(), low_shape=(3, 2), shape=(3, 2)}::test_uniform
12831283
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsUniform_param_4_{high_shape=(3, 2), low_shape=(), shape=(4, 3, 2)}::test_uniform
12841284
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsUniform_param_5_{high_shape=(3, 2), low_shape=(), shape=(3, 2)}::test_uniform
1285-
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsUniform_param_6_{high_shape=(3, 2), low_shape=(3, 2), shape=(4, 3, 2)}::test_uniform
1286-
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsUniform_param_7_{high_shape=(3, 2), low_shape=(3, 2), shape=(3, 2)}::test_uniform
12871285
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsWald_param_0_{mean_shape=(), scale_shape=(), shape=(4, 3, 2)}::test_wald
12881286
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsWald_param_1_{mean_shape=(), scale_shape=(), shape=(3, 2)}::test_wald
12891287
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsWald_param_2_{mean_shape=(), scale_shape=(3, 2), shape=(4, 3, 2)}::test_wald

tests/skipped_tests_gpu.tbl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,8 +1640,6 @@ tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsUnif
16401640
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsUniform_param_3_{high_shape=(), low_shape=(3, 2), shape=(3, 2)}::test_uniform
16411641
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsUniform_param_4_{high_shape=(3, 2), low_shape=(), shape=(4, 3, 2)}::test_uniform
16421642
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsUniform_param_5_{high_shape=(3, 2), low_shape=(), shape=(3, 2)}::test_uniform
1643-
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsUniform_param_6_{high_shape=(3, 2), low_shape=(3, 2), shape=(4, 3, 2)}::test_uniform
1644-
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsUniform_param_7_{high_shape=(3, 2), low_shape=(3, 2), shape=(3, 2)}::test_uniform
16451643
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsWald_param_0_{mean_shape=(), scale_shape=(), shape=(4, 3, 2)}::test_wald
16461644
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsWald_param_1_{mean_shape=(), scale_shape=(), shape=(3, 2)}::test_wald
16471645
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsWald_param_2_{mean_shape=(), scale_shape=(3, 2), shape=(4, 3, 2)}::test_wald

tests/third_party/cupy/random_tests/test_distributions.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -665,14 +665,12 @@ def test_triangular_for_invalid_params(self, param_dtype):
665665
@testing.gpu
666666
class TestDistributionsUniform(RandomDistributionsTestCase):
667667

668-
@helper.for_float_dtypes('dtype', no_float16=True)
669668
@helper.for_dtypes_combination(
670-
_float_dtypes, names=['low_dtype', 'high_dtype'])
671-
def test_uniform(self, low_dtype, high_dtype, dtype):
669+
_regular_float_dtypes, names=['low_dtype', 'high_dtype'])
670+
def test_uniform(self, low_dtype, high_dtype):
672671
low = numpy.ones(self.low_shape, dtype=low_dtype)
673672
high = numpy.ones(self.high_shape, dtype=high_dtype) * 2.
674-
self.check_distribution('uniform',
675-
{'low': low, 'high': high}, dtype)
673+
self.check_distribution('uniform', {'low': low, 'high': high})
676674

677675

678676
@testing.parameterize(*testing.product({

0 commit comments

Comments
 (0)