Skip to content

Commit 70007a5

Browse files
authored
RNG to desc 3 (#805)
1 parent 7266bee commit 70007a5

File tree

2 files changed

+45
-46
lines changed

2 files changed

+45
-46
lines changed

dpnp/random/dpnp_algo_random.pyx

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ cpdef utils.dpnp_descriptor dpnp_rng_multivariate_normal(numpy.ndarray mean, num
553553
return result
554554

555555

556-
cpdef dparray dpnp_rng_negative_binomial(double a, double p, size):
556+
cpdef utils.dpnp_descriptor dpnp_rng_negative_binomial(double a, double p, size):
557557
"""
558558
Returns an array populated with samples from negative binomial distribution.
559559
@@ -564,28 +564,27 @@ cpdef dparray dpnp_rng_negative_binomial(double a, double p, size):
564564
"""
565565

566566
dtype = numpy.int32
567-
cdef dparray result
567+
cdef utils.dpnp_descriptor result
568568
cdef DPNPFuncType param1_type
569569
cdef DPNPFuncData kernel_data
570570
cdef fptr_dpnp_rng_negative_binomial_c_1out_t func
571-
571+
cdef dparray_shape_type result_shape
572+
572573
if p == 0.0:
573574
filled_val = numpy.iinfo(dtype).min
574-
result = dparray(size, dtype=dtype)
575-
result.fill(filled_val)
575+
return dpnp_full(size, filled_val, dtype)
576576
elif p == 1.0:
577-
result = dparray(size, dtype=dtype)
578-
result.fill(0)
577+
return dpnp_full(size, 0, dtype)
579578
else:
580579
# convert string type names (dparray.dtype) to C enum DPNPFuncType
581580
param1_type = dpnp_dtype_to_DPNPFuncType(dtype)
582581

583582
# get the FPTR data structure
584583
kernel_data = get_dpnp_function_ptr(DPNP_FN_RNG_NEGATIVE_BINOMIAL, param1_type, param1_type)
585584

586-
result_type = dpnp_DPNPFuncType_to_dtype(< size_t > kernel_data.return_type)
587585
# ceate result array with type given by FPTR data
588-
result = dparray(size, dtype=result_type)
586+
result_shape = utils._object_to_tuple(size)
587+
result = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)
589588

590589
func = <fptr_dpnp_rng_negative_binomial_c_1out_t > kernel_data.ptr
591590
# call FPTR function
@@ -619,7 +618,7 @@ cpdef utils.dpnp_descriptor dpnp_rng_noncentral_chisquare(double df, double nonc
619618
return result
620619

621620

622-
cpdef dparray dpnp_rng_normal(double loc, double scale, size):
621+
cpdef utils.dpnp_descriptor dpnp_rng_normal(double loc, double scale, size):
623622
"""
624623
Returns an array populated with samples from normal distribution.
625624
`dpnp_rng_normal` generates a matrix filled with random floats sampled from a
@@ -628,24 +627,24 @@ cpdef dparray dpnp_rng_normal(double loc, double scale, size):
628627
"""
629628

630629
dtype = numpy.float64
631-
cdef dparray result
630+
cdef dparray_shape_type result_shape
631+
cdef utils.dpnp_descriptor result
632632
cdef DPNPFuncType param1_type
633633
cdef DPNPFuncData kernel_data
634634
cdef fptr_dpnp_rng_normal_c_1out_t func
635635

636636
if scale == 0.0:
637-
result = dparray(size, dtype=dtype)
638-
result.fill(loc)
637+
return dpnp_full(size, loc, dtype)
639638
else:
640639
# convert string type names (dparray.dtype) to C enum DPNPFuncType
641640
param1_type = dpnp_dtype_to_DPNPFuncType(dtype)
642641

643642
# get the FPTR data structure
644643
kernel_data = get_dpnp_function_ptr(DPNP_FN_RNG_NORMAL, param1_type, param1_type)
645644

646-
result_type = dpnp_DPNPFuncType_to_dtype(< size_t > kernel_data.return_type)
647645
# ceate result array with type given by FPTR data
648-
result = dparray(size, dtype=result_type)
646+
result_shape = utils._object_to_tuple(size)
647+
result = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)
649648

650649
func = <fptr_dpnp_rng_normal_c_1out_t > kernel_data.ptr
651650
# call FPTR function
@@ -680,7 +679,7 @@ cpdef utils.dpnp_descriptor dpnp_rng_pareto(double alpha, size):
680679
return result
681680

682681

683-
cpdef dparray dpnp_rng_poisson(double lam, size):
682+
cpdef utils.dpnp_descriptor dpnp_rng_poisson(double lam, size):
684683
"""
685684
Returns an array populated with samples from Poisson distribution.
686685
`dpnp_rng_poisson` generates a matrix filled with random floats sampled from a
@@ -690,24 +689,24 @@ cpdef dparray dpnp_rng_poisson(double lam, size):
690689
"""
691690

692691
dtype = numpy.int32
693-
cdef dparray result
692+
cdef dparray_shape_type result_shape
693+
cdef utils.dpnp_descriptor result
694694
cdef DPNPFuncType param1_type
695695
cdef DPNPFuncData kernel_data
696696
cdef fptr_dpnp_rng_poisson_c_1out_t func
697697

698698
if lam == 0:
699-
result = dparray(size, dtype=dtype)
700-
result.fill(0)
699+
return dpnp_full(size, 0, dtype)
701700
else:
702701
# convert string type names (dparray.dtype) to C enum DPNPFuncType
703702
param1_type = dpnp_dtype_to_DPNPFuncType(dtype)
704703

705704
# get the FPTR data structure
706705
kernel_data = get_dpnp_function_ptr(DPNP_FN_RNG_POISSON, param1_type, param1_type)
707706

708-
result_type = dpnp_DPNPFuncType_to_dtype(< size_t > kernel_data.return_type)
709707
# ceate result array with type given by FPTR data
710-
result = dparray(size, dtype=result_type)
708+
result_shape = utils._object_to_tuple(size)
709+
result = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)
711710

712711
func = <fptr_dpnp_rng_poisson_c_1out_t > kernel_data.ptr
713712
# call FPTR function
@@ -794,7 +793,7 @@ cpdef utils.dpnp_descriptor dpnp_rng_random(dims):
794793
return result
795794

796795

797-
cpdef dparray dpnp_rng_rayleigh(double scale, size):
796+
cpdef utils.dpnp_descriptor dpnp_rng_rayleigh(double scale, size):
798797
"""
799798
Returns an array populated with samples from Rayleigh distribution.
800799
`dpnp_rayleigh` generates a matrix filled with random floats sampled from a
@@ -803,24 +802,24 @@ cpdef dparray dpnp_rng_rayleigh(double scale, size):
803802
"""
804803

805804
dtype = numpy.float64
806-
cdef dparray result
805+
cdef dparray_shape_type result_shape
806+
cdef utils.dpnp_descriptor result
807807
cdef DPNPFuncType param1_type
808808
cdef DPNPFuncData kernel_data
809809
cdef fptr_dpnp_rng_rayleigh_c_1out_t func
810810

811811
if scale == 0.0:
812-
result = dparray(size, dtype=dtype)
813-
result.fill(0.0)
812+
return dpnp_full(size, 0.0, dtype)
814813
else:
815814
# convert string type names (dparray.dtype) to C enum DPNPFuncType
816815
param1_type = dpnp_dtype_to_DPNPFuncType(dtype)
817816

818817
# get the FPTR data structure
819818
kernel_data = get_dpnp_function_ptr(DPNP_FN_RNG_RAYLEIGH, param1_type, param1_type)
820819

821-
result_type = dpnp_DPNPFuncType_to_dtype(< size_t > kernel_data.return_type)
822820
# ceate result array with type given by FPTR data
823-
result = dparray(size, dtype=result_type)
821+
result_shape = utils._object_to_tuple(size)
822+
result = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)
824823

825824
func = <fptr_dpnp_rng_rayleigh_c_1out_t > kernel_data.ptr
826825
# call FPTR function
@@ -920,7 +919,7 @@ cpdef utils.dpnp_descriptor dpnp_rng_standard_exponential(size):
920919
return result
921920

922921

923-
cpdef dparray dpnp_rng_standard_gamma(double shape, size):
922+
cpdef utils.dpnp_descriptor dpnp_rng_standard_gamma(double shape, size):
924923
"""
925924
Returns an array populated with samples from standard gamma distribution.
926925
`dpnp_standard_gamma` generates a matrix filled with random floats sampled from a
@@ -929,24 +928,24 @@ cpdef dparray dpnp_rng_standard_gamma(double shape, size):
929928
"""
930929

931930
dtype = numpy.float64
932-
cdef dparray result
931+
cdef dparray_shape_type result_shape
932+
cdef utils.dpnp_descriptor result
933933
cdef DPNPFuncType param1_type
934934
cdef DPNPFuncData kernel_data
935935
cdef fptr_dpnp_rng_standard_gamma_c_1out_t func
936936

937937
if shape == 0.0:
938-
result = dparray(size, dtype=dtype)
939-
result.fill(0.0)
938+
return dpnp_full(size, 0.0, dtype)
940939
else:
941940
# convert string type names (dparray.dtype) to C enum DPNPFuncType
942941
param1_type = dpnp_dtype_to_DPNPFuncType(dtype)
943942

944943
# get the FPTR data structure
945944
kernel_data = get_dpnp_function_ptr(DPNP_FN_RNG_STANDARD_GAMMA, param1_type, param1_type)
946945

947-
result_type = dpnp_DPNPFuncType_to_dtype(< size_t > kernel_data.return_type)
948946
# ceate result array with type given by FPTR data
949-
result = dparray(size, dtype=result_type)
947+
result_shape = utils._object_to_tuple(size)
948+
result = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)
950949

951950
func = <fptr_dpnp_rng_standard_gamma_c_1out_t > kernel_data.ptr
952951
# call FPTR function
@@ -1030,7 +1029,7 @@ cpdef utils.dpnp_descriptor dpnp_rng_triangular(double left, double mode, double
10301029
return result
10311030

10321031

1033-
cpdef dparray dpnp_rng_uniform(long low, long high, size, dtype):
1032+
cpdef utils.dpnp_descriptor dpnp_rng_uniform(long low, long high, size, dtype):
10341033
"""
10351034
Returns an array populated with samples from standard uniform distribution.
10361035
Generates a matrix filled with random numbers sampled from a
@@ -1039,24 +1038,24 @@ cpdef dparray dpnp_rng_uniform(long low, long high, size, dtype):
10391038
10401039
"""
10411040

1042-
cdef dparray result
1041+
cdef dparray_shape_type result_shape
1042+
cdef utils.dpnp_descriptor result
10431043
cdef DPNPFuncType param1_type
10441044
cdef DPNPFuncData kernel_data
10451045
cdef fptr_dpnp_rng_uniform_c_1out_t func
10461046

10471047
if low == high:
1048-
result = dparray(size, dtype=dtype)
1049-
result.fill(low)
1048+
return dpnp_full(size, low, dtype)
10501049
else:
10511050
# convert string type names (dparray.dtype) to C enum DPNPFuncType
10521051
param1_type = dpnp_dtype_to_DPNPFuncType(dtype)
10531052

10541053
# get the FPTR data structure
10551054
kernel_data = get_dpnp_function_ptr(DPNP_FN_RNG_UNIFORM, param1_type, param1_type)
10561055

1057-
result_type = dpnp_DPNPFuncType_to_dtype(< size_t > kernel_data.return_type)
10581056
# ceate result array with type given by FPTR data
1059-
result = dparray(size, dtype=result_type)
1057+
result_shape = utils._object_to_tuple(size)
1058+
result = utils.create_output_descriptor(result_shape, kernel_data.return_type, None)
10601059

10611060
func = <fptr_dpnp_rng_uniform_c_1out_t > kernel_data.ptr
10621061
# call FPTR function

dpnp/random/dpnp_iface_random.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ def negative_binomial(n, p, size=None):
768768
elif n <= 0:
769769
pass
770770
else:
771-
return dpnp_rng_negative_binomial(n, p, size)
771+
return dpnp_rng_negative_binomial(n, p, size).get_pyobj()
772772

773773
return call_origin(numpy.random.negative_binomial, n, p, size)
774774

@@ -804,7 +804,7 @@ def normal(loc=0.0, scale=1.0, size=None):
804804
elif scale < 0:
805805
pass
806806
else:
807-
return dpnp_rng_normal(loc, scale, size)
807+
return dpnp_rng_normal(loc, scale, size).get_pyobj()
808808

809809
return call_origin(numpy.random.normal, loc, scale, size)
810810

@@ -952,7 +952,7 @@ def poisson(lam=1.0, size=None):
952952
elif lam < 0:
953953
pass
954954
else:
955-
return dpnp_rng_poisson(lam, size)
955+
return dpnp_rng_poisson(lam, size).get_pyobj()
956956

957957
return call_origin(numpy.random.poisson, lam, size)
958958

@@ -1074,7 +1074,7 @@ def randint(low, high=None, size=None, dtype=int):
10741074
else:
10751075
low = int(low)
10761076
high = int(high)
1077-
return dpnp_rng_uniform(low, high, size, _dtype)
1077+
return dpnp_rng_uniform(low, high, size, _dtype).get_pyobj()
10781078

10791079
return call_origin(numpy.random.randint, low, high, size, dtype)
10801080

@@ -1258,7 +1258,7 @@ def rayleigh(scale=1.0, size=None):
12581258
elif scale < 0:
12591259
pass
12601260
else:
1261-
return dpnp_rng_rayleigh(scale, size)
1261+
return dpnp_rng_rayleigh(scale, size).get_pyobj()
12621262

12631263
return call_origin(numpy.random.rayleigh, scale, size)
12641264

@@ -1423,7 +1423,7 @@ def standard_gamma(shape, size=None):
14231423
elif shape < 0:
14241424
pass
14251425
else:
1426-
return dpnp_rng_standard_gamma(shape, size)
1426+
return dpnp_rng_standard_gamma(shape, size).get_pyobj()
14271427

14281428
return call_origin(numpy.random.standard_gamma, shape, size)
14291429

@@ -1565,7 +1565,7 @@ def uniform(low=0.0, high=1.0, size=None):
15651565
else:
15661566
if low > high:
15671567
low, high = high, low
1568-
return dpnp_rng_uniform(low, high, size, dtype=numpy.float64)
1568+
return dpnp_rng_uniform(low, high, size, dtype=numpy.float64).get_pyobj()
15691569

15701570
return call_origin(numpy.random.uniform, low, high, size)
15711571

0 commit comments

Comments
 (0)