Skip to content

Commit f80f157

Browse files
committed
ENH: wrap iinfo/finfo
1 parent b6900df commit f80f157

12 files changed

+84
-22
lines changed

array_api_compat/common/_aliases.py

+19-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from __future__ import annotations
66

77
import inspect
8-
from typing import NamedTuple, Optional, Sequence, Tuple, Union
8+
from typing import Any, NamedTuple, Optional, Sequence, Tuple, Union
99

1010
from ._typing import Array, Device, DType, Namespace
1111
from ._helpers import (
@@ -609,13 +609,30 @@ def sign(x: Array, /, xp: Namespace, **kwargs) -> Array:
609609
out[xp.isnan(x)] = xp.nan
610610
return out[()]
611611

612+
613+
def finfo(type_: DType | Array, /, xp: Namespace) -> Any:
614+
# It is surprisingly difficult to recognize a dtype apart from an array.
615+
# np.int64 is not the same as np.asarray(1).dtype!
616+
try:
617+
return xp.finfo(type_)
618+
except (ValueError, TypeError):
619+
return xp.finfo(type_.dtype)
620+
621+
622+
def iinfo(type_: DType | Array, /, xp: Namespace) -> Any:
623+
try:
624+
return xp.iinfo(type_)
625+
except (ValueError, TypeError):
626+
return xp.iinfo(type_.dtype)
627+
628+
612629
__all__ = ['arange', 'empty', 'empty_like', 'eye', 'full', 'full_like',
613630
'linspace', 'ones', 'ones_like', 'zeros', 'zeros_like',
614631
'UniqueAllResult', 'UniqueCountsResult', 'UniqueInverseResult',
615632
'unique_all', 'unique_counts', 'unique_inverse', 'unique_values',
616633
'std', 'var', 'cumulative_sum', 'cumulative_prod','clip', 'permute_dims',
617634
'reshape', 'argsort', 'sort', 'nonzero', 'ceil', 'floor', 'trunc',
618635
'matmul', 'matrix_transpose', 'tensordot', 'vecdot', 'isdtype',
619-
'unstack', 'sign']
636+
'unstack', 'sign', 'finfo', 'iinfo']
620637

621638
_all_ignore = ['inspect', 'array_namespace', 'NamedTuple']

array_api_compat/cupy/_aliases.py

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
matrix_transpose = get_xp(cp)(_aliases.matrix_transpose)
6262
tensordot = get_xp(cp)(_aliases.tensordot)
6363
sign = get_xp(cp)(_aliases.sign)
64+
finfo = get_xp(cp)(_aliases.finfo)
65+
iinfo = get_xp(cp)(_aliases.iinfo)
6466

6567
_copy_default = object()
6668

array_api_compat/dask/array/_aliases.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import numpy as np
66
from numpy import (
77
# dtypes
8-
iinfo,
9-
finfo,
108
bool_ as bool,
119
float32,
1210
float64,
@@ -131,6 +129,8 @@ def arange(
131129
matmul = get_xp(np)(_aliases.matmul)
132130
tensordot = get_xp(np)(_aliases.tensordot)
133131
sign = get_xp(np)(_aliases.sign)
132+
finfo = get_xp(np)(_aliases.finfo)
133+
iinfo = get_xp(np)(_aliases.iinfo)
134134

135135

136136
# asarray also adds the copy keyword, which is not present in numpy 1.0.
@@ -343,10 +343,9 @@ def count_nonzero(
343343
'__array_namespace_info__', 'asarray', 'astype', 'acos',
344344
'acosh', 'asin', 'asinh', 'atan', 'atan2',
345345
'atanh', 'bitwise_left_shift', 'bitwise_invert',
346-
'bitwise_right_shift', 'concat', 'pow', 'iinfo', 'finfo', 'can_cast',
346+
'bitwise_right_shift', 'concat', 'pow', 'can_cast',
347347
'result_type', 'bool', 'float32', 'float64', 'int8', 'int16', 'int32', 'int64',
348-
'uint8', 'uint16', 'uint32', 'uint64',
349-
'complex64', 'complex128', 'iinfo', 'finfo',
348+
'uint8', 'uint16', 'uint32', 'uint64', 'complex64', 'complex128',
350349
'can_cast', 'count_nonzero', 'result_type']
351350

352351
_all_ignore = ["array_namespace", "get_xp", "da", "np"]

array_api_compat/numpy/_aliases.py

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
matrix_transpose = get_xp(np)(_aliases.matrix_transpose)
6262
tensordot = get_xp(np)(_aliases.tensordot)
6363
sign = get_xp(np)(_aliases.sign)
64+
finfo = get_xp(np)(_aliases.finfo)
65+
iinfo = get_xp(np)(_aliases.iinfo)
6466

6567

6668
def _supports_buffer_protocol(obj):

array_api_compat/torch/_aliases.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,9 @@ def min(x: Array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, keep
227227
unstack = get_xp(torch)(_aliases.unstack)
228228
cumulative_sum = get_xp(torch)(_aliases.cumulative_sum)
229229
cumulative_prod = get_xp(torch)(_aliases.cumulative_prod)
230+
finfo = get_xp(torch)(_aliases.finfo)
231+
iinfo = get_xp(torch)(_aliases.iinfo)
232+
230233

231234
# torch.sort also returns a tuple
232235
# https://github.com/pytorch/pytorch/issues/70921
@@ -832,6 +835,6 @@ def sign(x: Array, /) -> Array:
832835
'UniqueAllResult', 'UniqueCountsResult', 'UniqueInverseResult',
833836
'unique_all', 'unique_counts', 'unique_inverse', 'unique_values',
834837
'matmul', 'matrix_transpose', 'vecdot', 'tensordot', 'isdtype',
835-
'take', 'take_along_axis', 'sign']
838+
'take', 'take_along_axis', 'sign', 'finfo', 'iinfo']
836839

837840
_all_ignore = ['torch', 'get_xp']

cupy-xfails.txt

+8-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ array_api_tests/test_array_object.py::test_getitem
1414
# copy=False is not yet implemented
1515
array_api_tests/test_creation_functions.py::test_asarray_arrays
1616

17-
# finfo test is testing that the result is a float instead of float32 (see
18-
# also https://github.com/data-apis/array-api/issues/405)
19-
array_api_tests/test_data_type_functions.py::test_finfo[float32]
17+
# attributes are np.float32 instead of float
18+
# (see also https://github.com/data-apis/array-api/issues/405)
19+
array_api_tests/test_data_type_functions.py::test_finfo[float32-dtype]
20+
array_api_tests/test_data_type_functions.py::test_finfo[float32-array]
21+
array_api_tests/test_data_type_functions.py::test_finfo[float32-array.dtype]
22+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-dtype]
23+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array]
24+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array.dtype]
2025

2126
# Some array attributes are missing, and we do not wrap the array object
2227
array_api_tests/test_has_names.py::test_has_names[array_method-__array_namespace__]

dask-xfails.txt

+8-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ array_api_tests/test_array_object.py::test_getitem_masking
1212
# zero division error, and typeerror: tuple indices must be integers or slices not tuple
1313
array_api_tests/test_creation_functions.py::test_eye
1414

15-
# finfo(float32).eps returns float32 but should return float
16-
array_api_tests/test_data_type_functions.py::test_finfo[float32]
15+
# attributes are np.float32 instead of float
16+
# (see also https://github.com/data-apis/array-api/issues/405)
17+
array_api_tests/test_data_type_functions.py::test_finfo[float32-dtype]
18+
array_api_tests/test_data_type_functions.py::test_finfo[float32-array]
19+
array_api_tests/test_data_type_functions.py::test_finfo[float32-array.dtype]
20+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-dtype]
21+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array]
22+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array.dtype]
1723

1824
# out[-1]=dask.array<getitem ...> but should be some floating number
1925
# (I think the test is not forcing the op to be computed?)

numpy-1-21-xfails.txt

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
# asarray(copy=False) is not yet implemented
22
array_api_tests/test_creation_functions.py::test_asarray_arrays
33

4-
# finfo(float32).eps returns float32 but should return float
5-
array_api_tests/test_data_type_functions.py::test_finfo[float32]
4+
# attributes are np.float32 instead of float
5+
# (see also https://github.com/data-apis/array-api/issues/405)
6+
array_api_tests/test_data_type_functions.py::test_finfo[float32-dtype]
7+
array_api_tests/test_data_type_functions.py::test_finfo[float32-array]
8+
array_api_tests/test_data_type_functions.py::test_finfo[float32-array.dtype]
9+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-dtype]
10+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array]
11+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array.dtype]
612

713
# Array methods and attributes not already on np.ndarray cannot be wrapped
814
array_api_tests/test_has_names.py::test_has_names[array_method-__array_namespace__]
@@ -41,7 +47,7 @@ array_api_tests/meta/test_hypothesis_helpers.py::test_symmetric_matrices
4147
############################
4248

4349
# finfo has no smallest_normal
44-
array_api_tests/test_data_type_functions.py::test_finfo[float64]
50+
array_api_tests/test_data_type_functions.py::test_finfo
4551

4652
# dlpack stuff
4753
array_api_tests/test_has_names.py::test_has_names[creation-from_dlpack]

numpy-1-26-xfails.txt

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
# finfo(float32).eps returns float32 but should return float
2-
array_api_tests/test_data_type_functions.py::test_finfo[float32]
1+
# attributes are np.float32 instead of float
2+
# (see also https://github.com/data-apis/array-api/issues/405)
3+
array_api_tests/test_data_type_functions.py::test_finfo[float32-dtype]
4+
array_api_tests/test_data_type_functions.py::test_finfo[float32-array]
5+
array_api_tests/test_data_type_functions.py::test_finfo[float32-array.dtype]
6+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-dtype]
7+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array]
8+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array.dtype]
39

410
# Array methods and attributes not already on np.ndarray cannot be wrapped
511
array_api_tests/test_has_names.py::test_has_names[array_method-__array_namespace__]

numpy-dev-xfails.txt

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
# finfo(float32).eps returns float32 but should return float
2-
array_api_tests/test_data_type_functions.py::test_finfo[float32]
1+
# attributes are np.float32 instead of float
2+
# (see also https://github.com/data-apis/array-api/issues/405)
3+
array_api_tests/test_data_type_functions.py::test_finfo[float32-dtype]
4+
array_api_tests/test_data_type_functions.py::test_finfo[float32-array]
5+
array_api_tests/test_data_type_functions.py::test_finfo[float32-array.dtype]
6+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-dtype]
7+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array]
8+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array.dtype]
39

410
# The test suite cannot properly get the signature for vecdot
511
# https://github.com/numpy/numpy/pull/26237

numpy-xfails.txt

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
# finfo(float32).eps returns float32 but should return float
2-
array_api_tests/test_data_type_functions.py::test_finfo[float32]
1+
# attributes are np.float32 instead of float
2+
# (see also https://github.com/data-apis/array-api/issues/405)
3+
array_api_tests/test_data_type_functions.py::test_finfo[float32-dtype]
4+
array_api_tests/test_data_type_functions.py::test_finfo[float32-array]
5+
array_api_tests/test_data_type_functions.py::test_finfo[float32-array.dtype]
6+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-dtype]
7+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array]
8+
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array.dtype]
39

410
# The test suite cannot properly get the signature for vecdot
511
# https://github.com/numpy/numpy/pull/26237

torch-xfails.txt

+4
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ array_api_tests/test_operators_and_elementwise_functions.py::test_round
115115
array_api_tests/test_set_functions.py::test_unique_counts
116116
array_api_tests/test_set_functions.py::test_unique_values
117117

118+
# finfo/iinfo.dtype is a string instead of a dtype
119+
array_api_tests/test_data_type_functions.py::test_finfo_dtype
120+
array_api_tests/test_data_type_functions.py::test_iinfo_dtype
121+
118122
# 2023.12 support
119123
array_api_tests/test_has_names.py::test_has_names[manipulation-repeat]
120124
array_api_tests/test_manipulation_functions.py::test_repeat

0 commit comments

Comments
 (0)