-
Notifications
You must be signed in to change notification settings - Fork 53
Add Array API inspection utilities #689
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
Merged
Merged
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
1e5ee83
Add namespace inspection utilities
kgryte 9494a13
Fix build error
kgryte f9436b7
Fix missing item in toctree
kgryte fb69405
Rename class
kgryte bf6eca5
Update toctree
kgryte 58e5152
Fix item name
kgryte ff0a5b0
Update copy
kgryte a7fffab
Rename file and fix rendering issues
kgryte 4e968f7
Add annotations
kgryte 553d389
Update description
kgryte e927b35
Update description
kgryte aa012a8
Add note regarding aliasing
kgryte 43a8acc
Add note regarding use of canonical names
kgryte c1308ff
Merge branch 'main' of https://github.com/data-apis/array-api into in…
kgryte bcec0d3
Refactor to use `Protocol`
kgryte 0ed4686
Add `self`
kgryte 6a4e24a
Merge branch 'main' of https://github.com/data-apis/array-api into in…
kgryte 8207bbe
Replaces underscores with spaces
kgryte 93c85ad
Replace underscores with spaces to ensure consistent conventions
kgryte e89882e
Document returned object
kgryte d481eb4
Add introspection API preamble to clarify API relationship
kgryte 42ad3e3
Update copy
kgryte 4d3b575
Update indentation
kgryte d32ec1f
Remove empty line
kgryte 332b7d2
Update comment
kgryte 908600a
Update copy
kgryte ad1c844
Add hyphen to match prose in specification
kgryte feb92fd
Update copy
kgryte 98a3b90
Add note
kgryte f27b0d8
Update copy
kgryte ad79bb0
Fix typo
kgryte 3210b5a
docs: update capabilities guidance and replace `should` with `must`
kgryte 828a061
Merge branch 'inspection-namespace' of https://github.com/kgryte/arra…
kgryte 5d13d41
docs: add clause
kgryte 083806e
style: fix backticks
kgryte 8015457
Apply suggestions from code review
kgryte File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
Inspection | ||
========== | ||
|
||
Array API specification for namespace inspection utilities. | ||
|
||
A conforming implementation of the array API standard must provide and support the following functions and associated inspection APIs. | ||
|
||
|
||
Objects in API | ||
-------------- | ||
|
||
.. currentmodule:: array_api.info | ||
|
||
.. | ||
NOTE: please keep the functions in alphabetical order | ||
|
||
.. autosummary:: | ||
:toctree: generated | ||
:template: method.rst | ||
|
||
__array_namespace_info__ | ||
|
||
|
||
Inspection APIs | ||
--------------- | ||
|
||
In the namespace (or class) returned by ``__array_namespace_info__``, a conforming implementation of the array API standard must provide and support the following functions (or methods) for programmatically querying data type and device support, capabilities, and other specification-defined implementation-specific behavior, as documented in the functions described below. | ||
|
||
.. | ||
NOTE: please keep the functions in alphabetical order | ||
|
||
.. autosummary:: | ||
:toctree: generated | ||
:template: method.rst | ||
|
||
capabilities | ||
default_device | ||
default_dtypes | ||
devices | ||
dtypes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
__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 | ||
----- | ||
|
||
The returned object may be either a namespace or a class, so 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() | ||
# ... | ||
""" | ||
|
||
|
||
def capabilities() -> Capabilities: | ||
""" | ||
Returns a dictionary of array library capabilities. | ||
|
||
The 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``. | ||
|
||
Returns | ||
------- | ||
out: Capabilities | ||
a dictionary of array library capabilities. | ||
""" | ||
|
||
|
||
def default_device() -> device: | ||
""" | ||
Returns the default device. | ||
|
||
Returns | ||
------- | ||
out: device | ||
an object corresponding to the default device. | ||
""" | ||
|
||
|
||
def default_dtypes( | ||
*, | ||
device: Optional[device] = None, | ||
) -> DefaultDataTypes: | ||
""" | ||
Returns a dictionary containing default data types. | ||
|
||
The 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. | ||
|
||
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``. | ||
|
||
.. note:: | ||
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. | ||
|
||
Returns | ||
------- | ||
out: DefaultDataTypes | ||
a dictionary containing the default data type for respective data type kinds. | ||
""" | ||
|
||
|
||
def dtypes( | ||
*, | ||
device: Optional[device] = None, | ||
kind: Optional[Union[str, Tuple[str, ...]]] = None, | ||
) -> DataTypes: | ||
""" | ||
Returns a dictionary of supported *Array API* data types. | ||
|
||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.. note:: | ||
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. | ||
|
||
.. note:: | ||
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. | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Parameters | ||
---------- | ||
kind: Optional[Union[str, Tuple[str, ...]]] | ||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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``. | ||
|
||
.. note:: | ||
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. | ||
|
||
Returns | ||
------- | ||
out: DataTypes | ||
a dictionary containing supported data types. | ||
|
||
.. note:: | ||
Dictionary keys must only consist of canonical names as defined in :ref:`data-types`. | ||
""" | ||
|
||
|
||
def devices() -> List[device]: | ||
""" | ||
Returns a list of supported devices which are available at runtime. | ||
|
||
kgryte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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. | ||
""" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.