Skip to content

Commit 5ce386a

Browse files
authored
Add a description of how to adopt the API (#33)
* Add a description of how to adopt the API Ref discussion from 20 Aug. The thing I left out of this text is a global dispatch. The last point made on that was: ... there is some inherent difficulties. Local dispatch is the more straightforward approach, but difficulties arise when you want a particular function to use a particular backend implementation. and I wasn't quite sure if that concern is valid - maybe I'm missing some details. If the "you" in there is the library author, then they should just ignore the whole API standard namespace and just do `import lib_I_want; lib_I_want.<somefunc>`. If it is the end user, then they normally would pass in an array of the right type that would control this. So the one case I see where the concern applies is if there is some function that does _not_ take any array input, but creates arrays internally _and_ the default array type for that creation is somehow undesirable. That seems a bit unlikely. * Change the namespace convention to `xp` (borrowed from CuPy) * Clarify that using an API version to retrieve the namespace is optional * Minor textual clarification to address review comment.
1 parent dc65531 commit 5ce386a

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

spec/API_specification/index.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _api-specification:
2+
13
API specification
24
=================
35

@@ -17,6 +19,13 @@ API specification
1719
statistical_functions
1820
linear_algebra_functions
1921
searching_functions
22+
linear_algebra_functions
2023
set_functions
2124
utility_functions
2225
constants
26+
27+
28+
.. _api-versioning:
29+
30+
TODO: add `__array_api_version__` (or similar) version here, and the way to
31+
retrieve it - see https://github.com/data-apis/workgroup/issues/24

spec/purpose_and_scope.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,53 @@ For guidance on how to read and understand the type annotations included in this
232232

233233
## How to adopt this API
234234

235+
Most (all) existing array libraries will find something in this API standard
236+
that is incompatible with a current implementation, and that they cannot
237+
change due to backwards compatibility concerns. Therefore we expect that each
238+
of those libraries will want to offer a standard-compliant API in a _new
239+
namespace_. The question then becomes: how does a user access this namespace?
240+
241+
The simplest method is: document the import to use to directly access the
242+
namespace (e.g. `import package_name.array_api`). This has two issues though:
243+
244+
1. Array-consuming libraries that want to support multiple array libraries
245+
then have to explicitly import each library.
246+
2. It is difficult to _version_ the array API standard implementation (see
247+
:ref:`api-versioning`).
248+
249+
To address both issues, a uniform way must be provided by a conforming
250+
implementation to access the API namespace, namely a method on the array object:
251+
252+
```
253+
xp = x.__array_namespace__()
254+
```
255+
256+
The method must take one keyword, `api_version=None`, to make it possible to
257+
request a specific API version:
258+
259+
```
260+
xp = x.__array_namespace__(api_version='2020.10')
261+
```
262+
263+
.. note::
264+
265+
This is inspired by [NEP 37](https://numpy.org/neps/nep-0037-array-module.html#how-to-use-get-array-module),
266+
however it avoids adding a dependency on NumPy or having to provide a
267+
separate package just to do `get_array_module(x)`
268+
269+
NEP 37 is still in flux (it was just accepted by JAX and TensorFlow on an
270+
experimental basis), and it's possible that that should be accepted instead.
271+
272+
TBD: a decision must be made on this topic before a first version of the
273+
standard can become final. We prefer to delay this decision, to see how
274+
NEP 37 adoption will work out.
275+
276+
The `xp` namespace must contain the array object and all functionality
277+
specified in :ref:`api-specification`. It may contain other functionality,
278+
however it is recommended not to add other functions or objects, because that
279+
may make it harder for users to write code that will work with multiple array
280+
libraries.
281+
235282

236283
* * *
237284

0 commit comments

Comments
 (0)