|
| 1 | +__all__ = [ |
| 2 | + "__array_namespace_info__", |
| 3 | + "capabilities", |
| 4 | + "default_device", |
| 5 | + "default_dtypes", |
| 6 | + "devices", |
| 7 | + "dtypes", |
| 8 | +] |
| 9 | + |
| 10 | +from ._types import ( |
| 11 | + Optional, |
| 12 | + Union, |
| 13 | + Tuple, |
| 14 | + List, |
| 15 | + device, |
| 16 | + dtype, |
| 17 | + DefaultDataTypes, |
| 18 | + DataTypes, |
| 19 | + Capabilities, |
| 20 | + Info, |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +def __array_namespace_info__() -> Info: |
| 25 | + """ |
| 26 | + Returns a namespace with Array API namespace inspection utilities. |
| 27 | +
|
| 28 | + Returns |
| 29 | + ------- |
| 30 | + out: Info |
| 31 | + An object containing Array API namespace inspection utilities. |
| 32 | +
|
| 33 | + Notes |
| 34 | + ----- |
| 35 | +
|
| 36 | + The returned object may be either a namespace or a class, so long as an Array API user can access inspection utilities as follows: |
| 37 | +
|
| 38 | + :: |
| 39 | +
|
| 40 | + info = xp.__array_namespace_info__() |
| 41 | + info.capabilities() |
| 42 | + info.devices() |
| 43 | + info.dtypes() |
| 44 | + info.default_dtypes() |
| 45 | + # ... |
| 46 | + """ |
| 47 | + |
| 48 | + |
| 49 | +def capabilities() -> Capabilities: |
| 50 | + """ |
| 51 | + Returns a dictionary of array library capabilities. |
| 52 | +
|
| 53 | + The dictionary must contain the following keys: |
| 54 | +
|
| 55 | + - `"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``. |
| 56 | + - `"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``. |
| 57 | +
|
| 58 | + Returns |
| 59 | + ------- |
| 60 | + out: Capabilities |
| 61 | + a dictionary of array library capabilities. |
| 62 | + """ |
| 63 | + |
| 64 | + |
| 65 | +def default_device() -> device: |
| 66 | + """ |
| 67 | + Returns the default device. |
| 68 | +
|
| 69 | + Returns |
| 70 | + ------- |
| 71 | + out: device |
| 72 | + an object corresponding to the default device. |
| 73 | + """ |
| 74 | + |
| 75 | + |
| 76 | +def default_dtypes( |
| 77 | + *, |
| 78 | + device: Optional[device] = None, |
| 79 | +) -> DefaultDataTypes: |
| 80 | + """ |
| 81 | + Returns a dictionary containing default data types. |
| 82 | +
|
| 83 | + The dictionary must have the following keys: |
| 84 | +
|
| 85 | + - `"real floating"`: default real floating-point data type. |
| 86 | + - `"complex floating"`: default complex floating-point data type. |
| 87 | + - `"integral"`: default integral data type. |
| 88 | + - `"indexing"`: default array index data type. |
| 89 | +
|
| 90 | + Dictionary values must be the corresponding data type object. |
| 91 | +
|
| 92 | + Parameters |
| 93 | + ---------- |
| 94 | + device: Optional[device] |
| 95 | + 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``. |
| 96 | +
|
| 97 | + .. note:: |
| 98 | + 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 should return the default data types for the current device. For libraries without a context manager or supporting only a single device, those libraries should return the default data types for the default device. |
| 99 | +
|
| 100 | + Returns |
| 101 | + ------- |
| 102 | + out: DefaultDataTypes |
| 103 | + a dictionary containing the default data type for respective data type kinds. |
| 104 | + """ |
| 105 | + |
| 106 | + |
| 107 | +def dtypes( |
| 108 | + *, |
| 109 | + device: Optional[device] = None, |
| 110 | + kind: Optional[Union[str, Tuple[str, ...]]] = None, |
| 111 | +) -> DataTypes: |
| 112 | + """ |
| 113 | + Returns a dictionary of supported *Array API* data types. |
| 114 | +
|
| 115 | + .. note:: |
| 116 | + 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 should not be included in the returned dictionary. |
| 117 | +
|
| 118 | + .. note:: |
| 119 | + 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. |
| 120 | +
|
| 121 | + Parameters |
| 122 | + ---------- |
| 123 | + kind: Optional[Union[str, Tuple[str, ...]]] |
| 124 | + data type kind. |
| 125 | +
|
| 126 | + - If ``kind`` is ``None``, the function must return a dictionary containing all supported Array API data types. |
| 127 | +
|
| 128 | + - 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: |
| 129 | +
|
| 130 | + - ``'bool'``: boolean data types (e.g., ``bool``). |
| 131 | + - ``'signed integer'``: signed integer data types (e.g., ``int8``, ``int16``, ``int32``, ``int64``). |
| 132 | + - ``'unsigned integer'``: unsigned integer data types (e.g., ``uint8``, ``uint16``, ``uint32``, ``uint64``). |
| 133 | + - ``'integral'``: integer data types. Shorthand for ``('signed integer', 'unsigned integer')``. |
| 134 | + - ``'real floating'``: real-valued floating-point data types (e.g., ``float32``, ``float64``). |
| 135 | + - ``'complex floating'``: complex floating-point data types (e.g., ``complex64``, ``complex128``). |
| 136 | + - ``'numeric'``: numeric data types. Shorthand for ``('integral', 'real floating', 'complex floating')``. |
| 137 | +
|
| 138 | + - 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. |
| 139 | +
|
| 140 | + Default: ``None``. |
| 141 | + device: Optional[device] |
| 142 | + 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``. |
| 143 | +
|
| 144 | + .. note:: |
| 145 | + 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 should return the supported data types for the current device. For libraries without a context manager or supporting only a single device, those libraries should return the supported data types for the default device. |
| 146 | +
|
| 147 | + Returns |
| 148 | + ------- |
| 149 | + out: DataTypes |
| 150 | + a dictionary containing supported data types. |
| 151 | +
|
| 152 | + .. note:: |
| 153 | + Dictionary keys must only consist of canonical names as defined in :ref:`data-types`. |
| 154 | + """ |
| 155 | + |
| 156 | + |
| 157 | +def devices() -> List[device]: |
| 158 | + """ |
| 159 | + Returns a list of supported devices which are available at runtime. |
| 160 | +
|
| 161 | + Returns |
| 162 | + ------- |
| 163 | + out: List[device] |
| 164 | + a list of supported devices. |
| 165 | +
|
| 166 | + Notes |
| 167 | + ----- |
| 168 | +
|
| 169 | + 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. |
| 170 | + """ |
0 commit comments