You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There, each function, constant, and object is described with details
105
105
on parameters, return values, and special cases.
106
106
107
-
### Testing against Array API
107
+
### Testing against array API
108
108
109
109
There are two main ways to test your API for compliance: either using
110
110
`array-api-tests` suite or testing your API manually against the
111
111
`array-api-strict` reference implementation.
112
112
113
-
#### Array API Test suite (Recommended)
113
+
#### array-api-tests suite (Recommended)
114
114
115
115
{ref}`array-api-tests` is a test suite which verifies that your API
116
116
adheres to the standard. For each function or method, it confirms
@@ -144,13 +144,15 @@ cover only the minimal workflow:
144
144
option is to skip these for the time being.
145
145
146
146
We strongly advise you to embed this setup in your CI as well. This will allow
147
-
you to continuously monitor Array API coverage, and make sure new changes don't break existing
148
-
APIs. As a reference, see [NumPy's Array API Tests CI setup](https://github.com/numpy/numpy/blob/581d10f43b539a189a2d37856e5130464de9e5f6/.github/workflows/linux.yml#L296).
147
+
you to continuously monitor array API standard coverage, and make sure new
148
+
changes don't break existing APIs. As a reference, see
149
+
[NumPy's array-api-tests CI setup](https://github.com/numpy/numpy/blob/581d10f43b539a189a2d37856e5130464de9e5f6/.github/workflows/linux.yml#L296)
150
+
and [a Pixi workspace setup](https://github.com/mdhaber/mparray/blob/0ef47e008fef92c605f73907436d4c6617419161/pixi.toml#L119-L179).
149
151
150
152
151
-
#### Array API Strict
153
+
#### array-api-strict
152
154
153
-
A simpler, and more manual, way of testing Array API coverage is to
155
+
A simpler, and more manual, way of testing array API standard coverage is to
154
156
run your API calls along with the {ref}`array-api-strict` Python implementation.
155
157
156
158
This way, you can ensure that the outputs coming from your API match the minimal
@@ -163,10 +165,9 @@ cases.
163
165
164
166
## Array Consumers
165
167
166
-
For array consumers, the main premise is to keep in mind that your **array
167
-
manipulation operations should not lock in for a particular array producing
168
-
library**. For instance, if you use NumPy for arrays, then your code could
169
-
contain:
168
+
For array consumers, the main premise is that your **array manipulation operations
169
+
should not be specific to one particular array producing library**. For instance,
170
+
if your code is specific to NumPy, it might contain:
170
171
171
172
```python
172
173
import numpy as np
@@ -178,12 +179,12 @@ return np.dot(c, b)
178
179
```
179
180
180
181
The first step should be as simple as assigning the `np` namespace to a dedicated
181
-
namespace variable. The convention used in the ecosystem is to name it `xp`. Then,
182
-
it is vital to ensure that each method and function call is something that the Array API
183
-
supports. For example, `dot` is present in the NumPy's API, but the standard
184
-
doesn't support it. For the sake of simplicity, let's assume both `c` and `b`
185
-
are `ndim=2`; therefore, we select `tensordot` instead, as both NumPy and the
186
-
standard define it:
182
+
namespace variable. The convention used in the ecosystem is to name it `xp`.
183
+
Then, it is vital to ensure that each method and function call is something that
184
+
the array API standard supports. For example, `dot` is present in the NumPy's
185
+
API, but the standard doesn't support it. For the sake of simplicity, let's
186
+
assume both `c` and `b`are `ndim=2`; therefore, we select `tensordot` instead,
187
+
as both NumPy and the standard define it:
187
188
188
189
```python
189
190
import numpy as np
@@ -196,18 +197,19 @@ c = xp.mean(a, axis=0)
196
197
return xp.tensordot(c, b, axes=1)
197
198
```
198
199
199
-
At this point, replacing one backend with another one should only require providing a different
200
-
namespace, such as `xp = torch` (e.g., via an environment variable). This can be useful
201
-
if you're writing a script or in your custom software. The other alternatives are:
200
+
At this point, replacing one backend with another one should only require
201
+
providing a different namespace, such as `xp = torch` (e.g., via an environment
202
+
variable). This can be useful if you're writing a script or in your custom
203
+
software. The other alternatives are:
202
204
203
-
- If you are building a library where the backend is determined by input arrays,
204
-
and your function accepts array arguments, then a recommended way is to ask
205
-
your input arrays for a namespace to use: `xp = arr.__array_namespace__()`.
206
-
If the given library doesn't have it, then [`array_api_compat.array_namespace()`](https://data-apis.org/array-api-compat/helper-functions.html#array_api_compat.array_namespace)
207
-
should be used instead:
205
+
- If you are building a library where the backend is determined by input
206
+
arrays, and your function accepts array arguments, then a recommended way to
207
+
fetch the namespace is to use[`array_api_compat.array_namespace()`](https://data-apis.org/array-api-compat/helper-functions.html#array_api_compat.array_namespace).
208
+
In case you don't want to introduce a new package dependency, you can rely
209
+
on a plain `xp = arr.__array_namespace__()`:
208
210
```python
209
211
deffunc(array1, scalar1, scalar2):
210
-
xp =array1.__array_namespace__() # or array_namespace(array1)
212
+
xp =array_namespace(array1) # or array1.__array_namespace__()
211
213
return xp.arange(scalar1, scalar2) @ array1
212
214
```
213
215
- For a function that accepts scalars and returns arrays, use namespace `xp` as
@@ -227,7 +229,7 @@ offers a set of useful utility functions, such as:
0 commit comments