Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: wrap iinfo/finfo #294

Merged
merged 3 commits into from
Apr 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions array_api_compat/common/_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from __future__ import annotations

import inspect
from typing import NamedTuple, Optional, Sequence, Tuple, Union
from typing import Any, NamedTuple, Optional, Sequence, Tuple, Union

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


def finfo(type_: DType | Array, /, xp: Namespace) -> Any:
# It is surprisingly difficult to recognize a dtype apart from an array.
# np.int64 is not the same as np.asarray(1).dtype!
try:
return xp.finfo(type_)
except (ValueError, TypeError):
return xp.finfo(type_.dtype)


def iinfo(type_: DType | Array, /, xp: Namespace) -> Any:
try:
return xp.iinfo(type_)
except (ValueError, TypeError):
return xp.iinfo(type_.dtype)


__all__ = ['arange', 'empty', 'empty_like', 'eye', 'full', 'full_like',
'linspace', 'ones', 'ones_like', 'zeros', 'zeros_like',
'UniqueAllResult', 'UniqueCountsResult', 'UniqueInverseResult',
'unique_all', 'unique_counts', 'unique_inverse', 'unique_values',
'std', 'var', 'cumulative_sum', 'cumulative_prod','clip', 'permute_dims',
'reshape', 'argsort', 'sort', 'nonzero', 'ceil', 'floor', 'trunc',
'matmul', 'matrix_transpose', 'tensordot', 'vecdot', 'isdtype',
'unstack', 'sign']
'unstack', 'sign', 'finfo', 'iinfo']

_all_ignore = ['inspect', 'array_namespace', 'NamedTuple']
2 changes: 2 additions & 0 deletions array_api_compat/cupy/_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
matrix_transpose = get_xp(cp)(_aliases.matrix_transpose)
tensordot = get_xp(cp)(_aliases.tensordot)
sign = get_xp(cp)(_aliases.sign)
finfo = get_xp(cp)(_aliases.finfo)
iinfo = get_xp(cp)(_aliases.iinfo)

_copy_default = object()

Expand Down
9 changes: 4 additions & 5 deletions array_api_compat/dask/array/_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import numpy as np
from numpy import (
# dtypes
iinfo,
finfo,
bool_ as bool,
float32,
float64,
Expand Down Expand Up @@ -131,6 +129,8 @@ def arange(
matmul = get_xp(np)(_aliases.matmul)
tensordot = get_xp(np)(_aliases.tensordot)
sign = get_xp(np)(_aliases.sign)
finfo = get_xp(np)(_aliases.finfo)
iinfo = get_xp(np)(_aliases.iinfo)


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

_all_ignore = ["array_namespace", "get_xp", "da", "np"]
2 changes: 2 additions & 0 deletions array_api_compat/numpy/_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
matrix_transpose = get_xp(np)(_aliases.matrix_transpose)
tensordot = get_xp(np)(_aliases.tensordot)
sign = get_xp(np)(_aliases.sign)
finfo = get_xp(np)(_aliases.finfo)
iinfo = get_xp(np)(_aliases.iinfo)


def _supports_buffer_protocol(obj):
Expand Down
5 changes: 4 additions & 1 deletion array_api_compat/torch/_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ def min(x: Array, /, *, axis: Optional[Union[int, Tuple[int, ...]]] = None, keep
unstack = get_xp(torch)(_aliases.unstack)
cumulative_sum = get_xp(torch)(_aliases.cumulative_sum)
cumulative_prod = get_xp(torch)(_aliases.cumulative_prod)
finfo = get_xp(torch)(_aliases.finfo)
iinfo = get_xp(torch)(_aliases.iinfo)


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

_all_ignore = ['torch', 'get_xp']
11 changes: 8 additions & 3 deletions cupy-xfails.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ array_api_tests/test_array_object.py::test_getitem
# copy=False is not yet implemented
array_api_tests/test_creation_functions.py::test_asarray_arrays

# finfo test is testing that the result is a float instead of float32 (see
# also https://github.com/data-apis/array-api/issues/405)
array_api_tests/test_data_type_functions.py::test_finfo[float32]
# attributes are np.float32 instead of float
# (see also https://github.com/data-apis/array-api/issues/405)
array_api_tests/test_data_type_functions.py::test_finfo[float32-dtype]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternatively to this mess, considering the ongoing controversy in data-apis/array-api#405, we could relax array-api-tests to check that the output is either a float or a 0-dimensional real floating array.

array_api_tests/test_data_type_functions.py::test_finfo[float32-array]
array_api_tests/test_data_type_functions.py::test_finfo[float32-array.dtype]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-dtype]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array.dtype]

# Some array attributes are missing, and we do not wrap the array object
array_api_tests/test_has_names.py::test_has_names[array_method-__array_namespace__]
Expand Down
10 changes: 8 additions & 2 deletions dask-xfails.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ array_api_tests/test_array_object.py::test_getitem_masking
# zero division error, and typeerror: tuple indices must be integers or slices not tuple
array_api_tests/test_creation_functions.py::test_eye

# finfo(float32).eps returns float32 but should return float
array_api_tests/test_data_type_functions.py::test_finfo[float32]
# attributes are np.float32 instead of float
# (see also https://github.com/data-apis/array-api/issues/405)
array_api_tests/test_data_type_functions.py::test_finfo[float32-dtype]
array_api_tests/test_data_type_functions.py::test_finfo[float32-array]
array_api_tests/test_data_type_functions.py::test_finfo[float32-array.dtype]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-dtype]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array.dtype]

# out[-1]=dask.array<getitem ...> but should be some floating number
# (I think the test is not forcing the op to be computed?)
Expand Down
12 changes: 9 additions & 3 deletions numpy-1-21-xfails.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
# asarray(copy=False) is not yet implemented
array_api_tests/test_creation_functions.py::test_asarray_arrays

# finfo(float32).eps returns float32 but should return float
array_api_tests/test_data_type_functions.py::test_finfo[float32]
# attributes are np.float32 instead of float
# (see also https://github.com/data-apis/array-api/issues/405)
array_api_tests/test_data_type_functions.py::test_finfo[float32-dtype]
array_api_tests/test_data_type_functions.py::test_finfo[float32-array]
array_api_tests/test_data_type_functions.py::test_finfo[float32-array.dtype]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-dtype]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array.dtype]

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

# finfo has no smallest_normal
array_api_tests/test_data_type_functions.py::test_finfo[float64]
array_api_tests/test_data_type_functions.py::test_finfo

# dlpack stuff
array_api_tests/test_has_names.py::test_has_names[creation-from_dlpack]
Expand Down
10 changes: 8 additions & 2 deletions numpy-1-26-xfails.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# finfo(float32).eps returns float32 but should return float
array_api_tests/test_data_type_functions.py::test_finfo[float32]
# attributes are np.float32 instead of float
# (see also https://github.com/data-apis/array-api/issues/405)
array_api_tests/test_data_type_functions.py::test_finfo[float32-dtype]
array_api_tests/test_data_type_functions.py::test_finfo[float32-array]
array_api_tests/test_data_type_functions.py::test_finfo[float32-array.dtype]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-dtype]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array.dtype]

# Array methods and attributes not already on np.ndarray cannot be wrapped
array_api_tests/test_has_names.py::test_has_names[array_method-__array_namespace__]
Expand Down
10 changes: 8 additions & 2 deletions numpy-dev-xfails.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# finfo(float32).eps returns float32 but should return float
array_api_tests/test_data_type_functions.py::test_finfo[float32]
# attributes are np.float32 instead of float
# (see also https://github.com/data-apis/array-api/issues/405)
array_api_tests/test_data_type_functions.py::test_finfo[float32-dtype]
array_api_tests/test_data_type_functions.py::test_finfo[float32-array]
array_api_tests/test_data_type_functions.py::test_finfo[float32-array.dtype]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-dtype]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array.dtype]

# The test suite cannot properly get the signature for vecdot
# https://github.com/numpy/numpy/pull/26237
Expand Down
10 changes: 8 additions & 2 deletions numpy-xfails.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# finfo(float32).eps returns float32 but should return float
array_api_tests/test_data_type_functions.py::test_finfo[float32]
# attributes are np.float32 instead of float
# (see also https://github.com/data-apis/array-api/issues/405)
array_api_tests/test_data_type_functions.py::test_finfo[float32-dtype]
array_api_tests/test_data_type_functions.py::test_finfo[float32-array]
array_api_tests/test_data_type_functions.py::test_finfo[float32-array.dtype]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-dtype]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array]
array_api_tests/test_data_type_functions.py::test_finfo[complex64-array.dtype]

# The test suite cannot properly get the signature for vecdot
# https://github.com/numpy/numpy/pull/26237
Expand Down
4 changes: 4 additions & 0 deletions torch-xfails.txt
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ array_api_tests/test_operators_and_elementwise_functions.py::test_round
array_api_tests/test_set_functions.py::test_unique_counts
array_api_tests/test_set_functions.py::test_unique_values

# finfo/iinfo.dtype is a string instead of a dtype
array_api_tests/test_data_type_functions.py::test_finfo_dtype
array_api_tests/test_data_type_functions.py::test_iinfo_dtype

# 2023.12 support
array_api_tests/test_has_names.py::test_has_names[manipulation-repeat]
array_api_tests/test_manipulation_functions.py::test_repeat
Expand Down
Loading