Skip to content

Commit 0ad72df

Browse files
committed
Specify the functions further.
1 parent af51eb5 commit 0ad72df

File tree

3 files changed

+74
-12
lines changed

3 files changed

+74
-12
lines changed

spec/draft/extensions/sparse_interchange.rst

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,27 @@ If implemented, this extension must be retrievable via::
1313
>>> if hasattr(x, 'sparse'):
1414
>>> # Use the extension
1515

16+
.. currentmodule:: array_api
17+
1618
Objects in API
1719
--------------
1820

1921
A conforming implementation of this extension must provide and support the following
20-
functions/methods.
21-
22-
.. currentmodule:: array_api
22+
functions/methods. In addition, the ``asarray`` method must also be able to convert
23+
objects with supported formats which implement the protocol.
2324

2425
..
2526
NOTE: please keep the functions and their inverse together
2627
28+
.. currentmodule:: array_api.sparse
29+
2730
.. autosummary::
2831
:toctree: generated
2932
:template: method.rst
3033

31-
sparse.from_binsparse
34+
from_binsparse
3235

36+
.. currentmodule:: array_api
3337

3438
.. autosummary::
3539
:toctree: generated

src/array_api_stubs/_draft/array_object.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1261,19 +1261,28 @@ def __binsparse_descriptor__(self) -> dict:
12611261
A ``dict`` equivalent to a parsed JSON binsparse descriptor of an array. See :ref:`sparse_interchange` for details.
12621262
"""
12631263

1264-
def __binsparse__(self) -> dict[str, array]:
1264+
def __binsparse__(
1265+
self, /, *, descriptor: Optional[dict] = None
1266+
) -> dict[str, array]:
12651267
"""
12661268
Returns a key-value store of the constituent arrays of a sparse array, as specified by the `binsparse specification <https://graphblas.org/binsparse-specification/>`_.
12671269
12681270
Parameters
12691271
----------
12701272
self: array
12711273
array instance.
1274+
descriptor: Optional[dict]
1275+
If ``descriptor`` is not ``None``, the data returned must be in the format specified by it. If the format is unsupported, a ``TypeError`` must be raised.
12721276
12731277
Returns
12741278
-------
12751279
out: dict[str, array]
12761280
A ``dict`` equivalent to a parsed JSON binsparse descriptor of an array. See :ref:`sparse_interchange` for details.
1281+
1282+
Raises
1283+
------
1284+
TypeError
1285+
If ``descriptor`` is not ``None``, and the array library does not support converting to it.
12771286
"""
12781287

12791288

src/array_api_stubs/_draft/sparse.py

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
11
from __future__ import annotations
22

3-
from ._types import array
3+
from typing import Optional
4+
from ._types import array, device
45

56
__all__ = ["from_binsparse"]
67

78

8-
def from_binsparse(arrays: dict[str, array], descriptor: dict, /) -> array:
9+
def from_binsparse(
10+
x: object,
11+
/,
12+
*,
13+
descriptor: Optional[dict] = None,
14+
device: Optional[device] = None,
15+
copy: Optional[bool] = None,
16+
) -> array:
917
"""
10-
Returns a new array containing the data from another (array) object with a ``__binsparse__`` method.
18+
Returns a new array containing the data from another (array) object with a ``__binsparse__`` method,
19+
assuming the format specified in `descriptor` is supported in this library.
1120
1221
Parameters
1322
----------
14-
arrays: dict[str, array]
15-
input constituent arrays.
16-
descriptor: dict
17-
The parsed binsparse descriptor of the array.
23+
x: object
24+
input (array) object.
25+
descriptor: Optional[dict]
26+
If ``descriptor`` is ``None``, the array must be returned in the format in which it is stored or materializable to.
27+
Otherwise, it must be converted to the format specified by ``descriptor``.
28+
29+
If ``copy`` is ``False``, no conversion should be performed, and only stored data should be returned.
30+
31+
If the format specified by ``descriptor`` is unsupported by the library, a ``TypeError`` must be raised.
32+
device: Optional[device]
33+
device on which to place the created array. If ``device`` is ``None`` and ``x`` supports binsparse, the output array
34+
must be on the same device as ``x``. Default: ``None``.
35+
36+
The v2023.12 standard only mandates that a compliant library should offer a way for ``from_binsparse`` to return an array
37+
whose underlying memory is accessible to the Python interpreter, when the corresponding ``device`` is provided. If the
38+
array library does not support such cases at all, the function must raise ``BufferError``. If a copy must be made to
39+
enable this support but ``copy`` is set to ``False``, the function must raise ``ValueError``.
40+
41+
Other device kinds will be considered for standardization in a future version of this API standard.
42+
copy: Optional[bool]
43+
boolean indicating whether or not to copy the input. If ``True``, the function must always copy. If ``False``, the function must never copy, and raise ``BufferError`` in case a copy is deemed necessary (e.g. if a cross-device data movement is requested, and it is not possible without a copy). If ``None``, the function must reuse the existing memory buffer if possible and copy otherwise. Default: ``None``.
44+
1845
1946
Returns
2047
-------
@@ -25,4 +52,26 @@ def from_binsparse(arrays: dict[str, array], descriptor: dict, /) -> array:
2552
:class: note
2653
2754
The returned array may be either a copy or a view. See :ref:`data-interchange` for details.
55+
56+
Raises
57+
------
58+
BufferError
59+
The ``__binsparse__``, ``__binsparse_descriptor__``, ``__dlpack__`` or ``__dlpack_device__``
60+
methods on the input array or constituent arrays may raise ``BufferError`` when the data
61+
cannot be exported as a binsparse-compatible array. (e.g., incompatible dtype, strides, or
62+
device). It may also raise other errors when export fails for other reasons (e.g., not
63+
enough memory available to materialize the data). ``from_dlpack`` must propagate such
64+
exceptions.
65+
AttributeError
66+
If the ``__binsparse__`` and ``__binsparse_descriptor__`` methods are not present
67+
on the input array. This may happen for libraries that are never able
68+
to export their data with binsparse.
69+
ValueError
70+
If data exchange is possible via an explicit copy but ``copy`` is set to ``False``.
71+
TypeError
72+
If ``descriptor`` is ``None``, the data received from the source library is not guaranteed to
73+
be in a format that the target array library supports. In this case, a ``TypeError`` must be raised.
74+
Additionally, if ``descriptor`` is not ``None``, it must be passed along to ``__binsparse__``, which
75+
may raise a ``TypeError`` if the conversion is unsupported by the source library, which
76+
``from_binsparse`` must propagate.
2877
"""

0 commit comments

Comments
 (0)