Skip to content

Commit c009c02

Browse files
authored
refactor!: allow subsets of dtypes and allow device-dependent dtype support
PR-URL: data-apis#1005 Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Ralf Gommers <ralf.gommers@gmail.com> Reviewed-by: Olivier Grisel Ref: data-apis#998
1 parent 8884c40 commit c009c02

4 files changed

Lines changed: 17 additions & 1 deletion

File tree

spec/draft/API_specification/creation_functions.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ Objects in API
3434
triu
3535
zeros
3636
zeros_like
37+
38+
39+
.. note::
40+
Creation functions which accept ``device`` and ``dtype`` arguments should raise an
41+
exception if the explicitly provided ``dtype`` is not supported by the ``device``.

spec/draft/API_specification/data_types.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Data Types
55

66
Array API specification for supported data types.
77

8-
A conforming implementation of the array API standard must provide and support
8+
A conforming implementation of the array API standard should provide and support
99
the following data types ("dtypes") in its array object, and as data type
1010
objects in its main namespace under the specified names:
1111

@@ -39,6 +39,8 @@ objects in its main namespace under the specified names:
3939
| complex128 | Double-precision (128-bit) complex floating-point number whose real and imaginary components must be IEEE 754 double-precision (64-bit) binary floating-point numbers (see IEEE 754-2019). |
4040
+--------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
4141

42+
If a library only supports a subset of data types, it must at a minimum support ``bool`` and at least one integer dtype and one real floating-point dtype.
43+
4244
Data type objects must have the following methods (no attributes are required):
4345

4446
..

spec/draft/design_topics/device_support.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ rather than hard requirements:
7777
- Respect explicit device assignment (i.e. if the input to the ``device=`` keyword is not ``None``, guarantee that the array is created on the given device, and raise an exception otherwise).
7878
- Preserve device assignment as much as possible (e.g. output arrays from a function are expected to be on the same device as input arrays to the function).
7979
- Raise an exception if an operation involves arrays on different devices (i.e. avoid implicit data transfer between devices).
80+
- Raise an exception if the user explicitly requests an unsupported combination of a ``device=`` and ``dtype=`` arguments (e.g. when a user attempts to create an array of a data type which is not supported by the device, we recommend raising an error instead of silently returning an array of a dtype supported by the device).
8081
- When a function accepts a mix of arrays and Python scalars, the scalars should inherit the device of the arrays, much like what happens with :ref:`type-promotion`.
8182
- Use a default for ``device=None`` which is consistent between functions within the same library.
8283
- If a library has multiple ways of controlling device placement, the most explicit method should have the highest priority:

src/array_api_stubs/_draft/array_object.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,10 @@ def __getitem__(
669669
- See :ref:`indexing` for details on supported indexing semantics.
670670
- When ``__getitem__`` is defined on an object, Python will automatically define iteration (i.e., the behavior from ``iter(x)``) as ``x[0]``, ``x[1]``, ..., ``x[N-1]``. This can also be implemented directly by defining ``__iter__``. Therefore, for a one-dimensional array ``x``, iteration should produce a sequence of zero-dimensional arrays ``x[0]``, ``x[1]``, ..., ``x[N-1]``, where ``N`` is the number of elements in the array. Iteration behavior for arrays having zero dimensions or more than one dimension is unspecified and thus implementation-defined.
671671
672+
.. note::
673+
When ``key`` contains integer arrays on a device different from the device of ``self``, the
674+
behavior is unspecified and thus implementation-defined.
675+
672676
.. versionchanged:: 2024.12
673677
Clarified that iteration is defined for one-dimensional arrays.
674678
"""
@@ -1154,6 +1158,10 @@ def __setitem__(
11541158
.. note::
11551159
Indexing semantics when ``key`` is an integer array or a tuple of integers and integer arrays is currently unspecified and thus implementation-defined. This will be revisited in a future revision of this standard.
11561160
1161+
.. note::
1162+
When ``value`` is an array on a device different from the device of ``self``, the
1163+
behavior is unspecified and thus implementation-defined.
1164+
11571165
- Setting array values must not affect the data type of ``self``.
11581166
- ``value`` must be promoted to the data type of ``self`` according to :ref:`type-promotion`. If this is not supported according to :ref:`type-promotion`, behavior is unspecified and thus implementation-defined.
11591167

0 commit comments

Comments
 (0)