forked from data-apis/array-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfo.py
More file actions
193 lines (140 loc) · 7.68 KB
/
info.py
File metadata and controls
193 lines (140 loc) · 7.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
__all__ = [
"__array_namespace_info__",
"capabilities",
"default_device",
"default_dtypes",
"devices",
"dtypes",
]
from ._types import (
Optional,
Union,
Tuple,
List,
device,
dtype,
DefaultDataTypes,
DataTypes,
Capabilities,
Info,
)
def __array_namespace_info__() -> Info:
"""
Returns a namespace with Array API namespace inspection utilities.
Returns
-------
out: Info
an object containing Array API namespace inspection utilities.
Notes
-----
- See :ref:`inspection` for a list of inspection APIs.
- The returned object **may** be either a namespace or a class, as long as an Array API user can access inspection utilities as follows:
::
info = xp.__array_namespace_info__()
info.capabilities()
info.devices()
info.dtypes()
info.default_dtypes()
# ...
.. versionadded: 2023.12
"""
def capabilities() -> Capabilities:
"""
Returns a dictionary of array library capabilities.
Returns
-------
out: Capabilities
a dictionary of array library capabilities. The returned dictionary **must** contain the following keys:
- `"boolean indexing"`: boolean indicating whether an array library supports boolean indexing. If a conforming implementation fully supports boolean indexing in compliance with this specification (see :ref:`indexing`), the corresponding dictionary value **must** be ``True``; otherwise, the value **must** be ``False``.
- `"data-dependent shapes"`: boolean indicating whether an array library supports data-dependent output shapes. If a conforming implementation fully supports all APIs included in this specification (excluding boolean indexing) which have data-dependent output shapes, as explicitly demarcated throughout the specification, the corresponding dictionary value **must** be ``True``; otherwise, the value **must** be ``False``.
- `"max dimensions"`: maximum number of supported dimensions. If a conforming implementation supports arrays having an arbitrary number of dimensions (potentially infinite), the corresponding dictionary value **must** be ``None``; otherwise, the value **must** be a finite integer.
Notes
-----
.. versionadded: 2023.12
.. versionchanged:: 2024.12
Added support for querying the maximum number of supported dimensions.
"""
def default_device() -> device:
"""
Returns the default device.
Returns
-------
out: device
an object corresponding to the default device.
Notes
-----
.. versionadded: 2023.12
"""
def default_dtypes(
*,
device: Optional[device] = None,
) -> DefaultDataTypes:
"""
Returns a dictionary containing default data types.
Parameters
----------
device: Optional[device]
device for which to return default data types. If ``device`` is ``None``, the returned data types **must** be the default data types for the current device; otherwise, the returned data types **must** be default data types specific to the specified device. Default: ``None``.
Returns
-------
out: DefaultDataTypes
a dictionary containing the default data type for respective data type kinds. The returned dictionary **must** have the following keys:
- `"real floating"`: default real floating-point data type.
- `"complex floating"`: default complex floating-point data type.
- `"integral"`: default integral data type.
- `"indexing"`: default array index data type.
Dictionary values **must** be the corresponding data type object.
Notes
-----
- Some array libraries have the concept of a device context manager, allowing library consumers to manage the current device context. When ``device`` is ``None``, libraries supporting a device context **must** return the default data types for the current device. For libraries without a context manager or supporting only a single device, those libraries **must** return the default data types for the default device.
.. versionadded: 2023.12
"""
def dtypes(
*,
device: Optional[device] = None,
kind: Optional[Union[str, Tuple[str, ...]]] = None,
) -> DataTypes:
"""
Returns a dictionary of supported *Array API* data types.
Parameters
----------
kind: Optional[Union[str, Tuple[str, ...]]]
data type kind.
- If ``kind`` is ``None``, the function **must** return a dictionary containing all supported Array API data types.
- If ``kind`` is a string, the function **must** return a dictionary containing the data types belonging to the specified data type kind. The following data type kinds **must** be supported:
- ``'bool'``: boolean data types (e.g., ``bool``).
- ``'signed integer'``: signed integer data types (e.g., ``int8``, ``int16``, ``int32``, ``int64``).
- ``'unsigned integer'``: unsigned integer data types (e.g., ``uint8``, ``uint16``, ``uint32``, ``uint64``).
- ``'integral'``: integer data types. Shorthand for ``('signed integer', 'unsigned integer')``.
- ``'real floating'``: real-valued floating-point data types (e.g., ``float32``, ``float64``).
- ``'complex floating'``: complex floating-point data types (e.g., ``complex64``, ``complex128``).
- ``'numeric'``: numeric data types. Shorthand for ``('integral', 'real floating', 'complex floating')``.
- If ``kind`` is a tuple, the tuple specifies a union of data type kinds, and the function **must** return a dictionary containing the data types belonging to at least one of the specified data type kinds.
Default: ``None``.
device: Optional[device]
device for which to return supported data types. If ``device`` is ``None``, the returned data types **must** be the supported data types for the current device; otherwise, the returned data types **must** be supported data types specific to the specified device. Default: ``None``.
Returns
-------
out: DataTypes
a dictionary containing supported data types. The returned dictionary **must** only contain keys corresponding to the canonical names as defined in :ref:`data-types`.
Notes
-----
- While specification-conforming array libraries **may** support additional data types which are not present in this specification, data types which are not present in this specification **must not** be included in the returned dictionary.
- Specification-conforming array libraries **must** only return supported data types having expected properties as described in :ref:`data-types`. For example, if a library decides to alias ``float32`` as ``float64``, that library **must not** include ``float64`` in the dictionary of supported data types.
- Some array libraries have the concept of a device context manager, allowing library consumers to manage the current device context. When ``device`` is ``None``, libraries supporting a device context **must** return the supported data types for the current device. For libraries without a context manager or supporting only a single device, those libraries **must** return the supported data types for the default device.
.. versionadded: 2023.12
"""
def devices() -> List[device]:
"""
Returns a list of supported devices which are available at runtime.
Returns
-------
out: List[device]
a list of supported devices.
Notes
-----
- Each device object (see :ref:`device-support`) in the list of returned devices **must** be an object which can be provided as a valid keyword-argument to array creation functions.
Notes
-----
.. versionadded: 2023.12
"""