Skip to content

Commit 0fe9a00

Browse files
authored
Leverage dpctl.tensor.iinfo() and dpctl.tensor.finfo() implementation. (#1582)
* Leverage dpctl.tensor.iinfo() and dpctl.tensor.finfo() implementation. * Address comment for dpnp.iinfo and dpnp.finfo functions * Update dpnp_iface_types.py
1 parent 76e3f87 commit 0fe9a00

File tree

8 files changed

+132
-48
lines changed

8 files changed

+132
-48
lines changed

dpnp/dpnp_iface_types.py

+84
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@
3434
This module provides public type interface file for the library
3535
"""
3636

37+
import dpctl.tensor as dpt
3738
import numpy
3839

40+
from dpnp.dpnp_array import dpnp_array
41+
3942
__all__ = [
4043
"bool",
4144
"bool_",
@@ -50,12 +53,14 @@
5053
"dtype",
5154
"e",
5255
"euler_gamma",
56+
"finfo",
5357
"float",
5458
"float_",
5559
"float16",
5660
"float32",
5761
"float64",
5862
"floating",
63+
"iinfo",
5964
"inexact",
6065
"Inf",
6166
"inf",
@@ -140,6 +145,85 @@
140145
PZERO = numpy.PZERO
141146

142147

148+
def finfo(dtype):
149+
"""
150+
Returns machine limits for floating-point data types.
151+
152+
For full documentation refer to :obj:`numpy.finfo`.
153+
154+
Parameters
155+
----------
156+
dtype : dtype, dpnp_array
157+
Floating-point dtype or an array with floating point data type.
158+
If complex, the information is about its component data type.
159+
160+
Returns
161+
-------
162+
out : finfo_object
163+
An object have the following attributes
164+
* bits: int
165+
number of bits occupied by dtype.
166+
* dtype: dtype
167+
real-valued floating-point data type.
168+
* eps: float
169+
difference between 1.0 and the next smallest representable
170+
real-valued floating-point number larger than 1.0 according
171+
to the IEEE-754 standard.
172+
* epsneg: float
173+
difference between 1.0 and the next smallest representable real-valued
174+
floating-point number smaller than 1.0 according to the IEEE-754
175+
standard.
176+
* max: float
177+
largest representable real-valued number.
178+
* min: float
179+
smallest representable real-valued number.
180+
* precision: float
181+
the approximate number of decimal digits to which this kind of
182+
floating point type is precise.
183+
* resolution: float
184+
the approximate decimal resolution of this type.
185+
* tiny: float
186+
an alias for `smallest_normal`
187+
* smallest_normal: float
188+
smallest positive real-valued floating-point number with
189+
full precision.
190+
191+
"""
192+
if isinstance(dtype, dpnp_array):
193+
dtype = dtype.dtype
194+
return dpt.finfo(dtype)
195+
196+
197+
def iinfo(dtype):
198+
"""
199+
Returns machine limits for integer data types.
200+
201+
For full documentation refer to :obj:`numpy.iinfo`.
202+
203+
Parameters
204+
----------
205+
dtype : dtype, dpnp_array
206+
Integer dtype or an array with integer dtype.
207+
208+
Returns
209+
-------
210+
out : iinfo_object
211+
An object with the following attributes
212+
* bits: int
213+
number of bits occupied by the data type
214+
* dtype: dtype
215+
integer data type.
216+
* max: int
217+
largest representable number.
218+
* min: int
219+
smallest representable number.
220+
221+
"""
222+
if isinstance(dtype, dpnp_array):
223+
dtype = dtype.dtype
224+
return dpt.iinfo(dtype)
225+
226+
143227
def isscalar(obj):
144228
"""
145229
Returns True if the type of `obj` is a scalar type.

dpnp/random/dpnp_algo_random.pyx

+3-3
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ cdef class MT19937(_Engine):
462462
if value < 0:
463463
return False
464464

465-
max_val = numpy.iinfo(numpy.uint32).max
465+
max_val = dpnp.iinfo(numpy.uint32).max
466466
if isinstance(value, dpnp_array):
467467
max_val = dpnp.array(max_val, dtype=numpy.uint32)
468468
return value <= max_val
@@ -499,7 +499,7 @@ cdef class MCG59(_Engine):
499499
if value < 0:
500500
return False
501501

502-
max_val = numpy.iinfo(numpy.uint64).max
502+
max_val = dpnp.iinfo(numpy.uint64).max
503503
if isinstance(value, dpnp_array):
504504
max_val = dpnp.array(max_val, dtype=numpy.uint64)
505505
return value <= max_val
@@ -1052,7 +1052,7 @@ cpdef utils.dpnp_descriptor dpnp_rng_negative_binomial(double a, double p, size)
10521052

10531053
result_shape = utils._object_to_tuple(size)
10541054
if p == 0.0:
1055-
filled_val = numpy.iinfo(dtype).min
1055+
filled_val = dpnp.iinfo(dtype).min
10561056
return utils.dpnp_descriptor(dpnp.full(result_shape, filled_val, dtype=dtype))
10571057
elif p == 1.0:
10581058
return utils.dpnp_descriptor(dpnp.full(result_shape, 0, dtype=dtype))

dpnp/random/dpnp_iface_random.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ def multinomial(n, pvals, size=None):
675675
d = len(pvals)
676676
if n < 0:
677677
pass
678-
elif n > numpy.iinfo(dpnp.int32).max:
678+
elif n > dpnp.iinfo(dpnp.int32).max:
679679
pass
680680
elif pvals_sum > 1.0:
681681
pass

dpnp/random/dpnp_random_state.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def __init__(self, seed=None, device=None, sycl_queue=None):
8181
is_cpu = self._sycl_device.is_cpu
8282
if seed is None:
8383
low = 0
84-
high = numpy.iinfo(numpy.int32).max + 1
84+
high = dpnp.iinfo(numpy.int32).max + 1
8585

8686
if is_cpu:
8787
# ask NumPy to generate an array of three random integers as default seed value
@@ -237,8 +237,8 @@ def normal(
237237
dtype = self._validate_float_dtype(
238238
dtype, (dpnp.float32, dpnp.float64)
239239
)
240-
min_floating = numpy.finfo(dtype).min
241-
max_floating = numpy.finfo(dtype).max
240+
min_floating = dpnp.finfo(dtype).min
241+
max_floating = dpnp.finfo(dtype).max
242242

243243
if (
244244
loc >= max_floating or loc <= min_floating
@@ -371,8 +371,8 @@ def randint(self, low, high=None, size=None, dtype=int, usm_type="device"):
371371
high = low
372372
low = 0
373373

374-
min_int = numpy.iinfo("int32").min
375-
max_int = numpy.iinfo("int32").max
374+
min_int = dpnp.iinfo("int32").min
375+
max_int = dpnp.iinfo("int32").max
376376

377377
if (
378378
not self._is_finite_scalar(low)
@@ -587,8 +587,8 @@ def uniform(
587587
elif not dpnp.isscalar(high):
588588
pass
589589
else:
590-
min_double = numpy.finfo("double").min
591-
max_double = numpy.finfo("double").max
590+
min_double = dpnp.finfo("double").min
591+
max_double = dpnp.finfo("double").max
592592

593593
if (
594594
not self._is_finite_scalar(low)

tests/helper.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def assert_dtype_allclose(dpnp_arr, numpy_arr, check_type=True):
1818
is_inexact = lambda x: dpnp.issubdtype(x.dtype, dpnp.inexact)
1919
if is_inexact(dpnp_arr) or is_inexact(numpy_arr):
2020
tol = 8 * max(
21-
numpy.finfo(dpnp_arr.dtype).resolution,
21+
dpnp.finfo(dpnp_arr).resolution,
2222
numpy.finfo(numpy_arr.dtype).resolution,
2323
)
2424
assert_allclose(dpnp_arr.asnumpy(), numpy_arr, atol=tol, rtol=tol)

tests/test_random.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -674,10 +674,10 @@ def test_extreme_value(self):
674674
)
675675
n = 5
676676
p = 0.0
677-
res = dpnp.asnumpy(dpnp.random.negative_binomial(n=n, p=p, size=10))
678-
check_val = numpy.iinfo(res.dtype).min
679-
assert len(numpy.unique(res)) == 1
680-
assert numpy.unique(res)[0] == check_val
677+
res = dpnp.random.negative_binomial(n=n, p=p, size=10)
678+
check_val = dpnp.iinfo(res).min
679+
assert len(dpnp.unique(res)) == 1
680+
assert dpnp.unique(res)[0] == check_val
681681

682682
def test_invalid_args(self):
683683
n = 10 # parameter `n`, OK

0 commit comments

Comments
 (0)