@@ -39,12 +39,20 @@ should be taken into account:
39
39
array-api-compat. The addition of functions that are not part of the array
40
40
API standard is currently out-of-scope for this package.
41
41
42
- - * No Monkey Patching.* ` array-api-compat ` should not attempt to modify anything
43
- about the underlying library. It is a * wrapper* library only.
42
+ - * No side-effects* . array_api_compat behavior should be localized to only the
43
+ specific code that imports and uses it. It should be invisible to end-users
44
+ or users of dependent codes. This specifically applies to the next two
45
+ points.
44
46
45
- - * No modifying the array object.* Due to the above restrictions, the array
46
- (or tensor) object of the array library cannot be modified. Any behavior
47
- that is built-in to the array object, such as the behavior of [ array
47
+ - * No Monkey Patching.* ` array-api-compat ` should not attempt to modify
48
+ anything about the underlying library. It is a * wrapper* library only.
49
+
50
+ - * No modifying the array object.* The array (or tensor) object of the array
51
+ library cannot be modified. Attempting to wrap or subclass a library's array
52
+ object would break the localized wrapping goal.
53
+
54
+ Any behavior that is built-in to the array object, such as the behavior of
55
+ [ array
48
56
methods] ( https://data-apis.org/array-api/latest/API_specification/array_object.html )
49
57
is therefore left unwrapped. Users can workaround issues by using
50
58
corresponding [ elementwise
@@ -53,10 +61,10 @@ should be taken into account:
53
61
functions] ( ../helper-functions.md ) provided by array-api-compat instead of
54
62
methods like ` to_device ` .
55
63
56
- - * Avoid Restricting Non-Standard Behavior.* All array libraries have
57
- functions and behaviors that are outside of the scope of what is specified
58
- by the standard. These behaviors should be left intact whenever possible,
59
- unless the standard explicitly disallows something. This means
64
+ - * Avoid Restricting Behavior Outside the Scope of the Standard .* All array
65
+ libraries have functions and behaviors that are outside of the scope of what
66
+ is specified by the standard. These behaviors should be left intact whenever
67
+ possible, unless the standard explicitly disallows something. This means
60
68
61
69
- All namespaces are * extended* with wrapper functions. You may notice the
62
70
extensive use of ` import * ` in various files in ` array_api_compat ` . While
@@ -72,18 +80,9 @@ should be taken into account:
72
80
73
81
## Avoiding Code Duplication
74
82
75
- As noted before, the goal of this library is to reuse the NumPy and CuPy array
76
- objects, rather than wrapping or extending them. This means that the functions
77
- need to accept and return ` np.ndarray ` for NumPy and ` cp.ndarray ` for CuPy.
78
-
79
- Each namespace (` array_api_compat.numpy ` , ` array_api_compat.cupy ` , and
80
- ` array_api_compat.torch ` ) is populated with the normal library namespace (like
81
- ` from numpy import * ` ). Then specific functions are replaced with wrapped
82
- variants.
83
-
84
- Since NumPy and CuPy are nearly identical in behavior, most wrapping logic can
85
- be shared between them. Wrapped functions that have the same logic between
86
- NumPy and CuPy are in ` array_api_compat/common/ ` .
83
+ Since NumPy, CuPy, and to a degree, Dask, are nearly identical in behavior,
84
+ most wrapping logic can be shared between them. Wrapped functions that have
85
+ the same logic between multiple libraries are in ` array_api_compat/common/ ` .
87
86
These functions are defined like
88
87
89
88
``` py
@@ -93,7 +92,7 @@ def acos(x, /, xp):
93
92
return xp.arccos(x)
94
93
```
95
94
96
- The ` xp ` argument refers to the original array namespace (either ` numpy ` or
95
+ The ` xp ` argument refers to the original array namespace (e.g., ` numpy ` or
97
96
` cupy ` ). Then in the specific ` array_api_compat/numpy/ ` and
98
97
` array_api_compat/cupy/ ` namespaces, the ` @get_xp ` decorator is applied to
99
98
these functions, which automatically removes the ` xp ` argument from the
@@ -123,38 +122,38 @@ import cupy as cp
123
122
acos = get_xp(cp)(_aliases.acos)
124
123
```
125
124
126
- Since NumPy and CuPy are nearly identical in their behaviors, this allows
127
- writing the wrapping logic for both libraries only once.
128
-
129
- PyTorch uses a similar layout in ` array_api_compat/torch/ ` , but it differs
130
- enough from NumPy/CuPy that very few common wrappers for those libraries are
131
- reused.
132
-
133
- See https://numpy.org/doc/stable/reference/array_api.html for a full list of
134
- changes from the base NumPy (the differences for CuPy are nearly identical). A
135
- corresponding document does not yet exist for PyTorch, but you can examine the
136
- various comments in the
137
- [ implementation] ( https://github.com/data-apis/array-api-compat/blob/main/array_api_compat/torch/_aliases.py )
138
- to see what functions and behaviors have been wrapped.
125
+ Most NumPy and CuPy are defined in this way, since their behaviors are nearly
126
+ identical PyTorch uses a similar layout in ` array_api_compat/torch/ ` , but it
127
+ differs enough from NumPy/CuPy that very few common wrappers for those
128
+ libraries are reused. Dask is close to NumPy in behavior and so most Dask
129
+ functions also reuse the NumPy/CuPy common wrappers.
139
130
140
131
## Tests
141
132
142
133
The majority of the behavior for array-api-compat is tested by the
143
134
[ array-api-tests] ( https://github.com/data-apis/array-api-tests ) test suite for
144
- the array API standard. There are also specific tests in
135
+ the array API standard. There are also array-api-compat specific tests in
145
136
[ ` tests/ ` ] ( https://github.com/data-apis/array-api-compat/tree/main/tests ) .
146
137
These tests should be limited to things that are not tested by the test suite,
147
- e.g., tests for [ helper functions] ( ../helper-functions.md ) or for behavior that is
148
- not strictly required by the standard.
138
+ e.g., tests for [ helper functions] ( ../helper-functions.md ) or for behavior
139
+ that is not strictly required by the standard.
149
140
150
141
array-api-tests is run against all supported libraries are tested on CI
151
- (except for JAX,
152
- [ wrapping] ( jax-support ) ). This is achieved by a [ reusable GitHub Actions
142
+ ([ except for JAX] ( jax-support ) ). This is achieved by a [ reusable GitHub Actions
153
143
Workflow] ( https://github.com/data-apis/array-api-compat/blob/main/.github/workflows/array-api-tests.yml ) .
154
144
Most libraries have tests that must be xfailed or skipped for various reasons.
155
145
These are defined in specific ` <library>-xfails.txt ` files and are
156
146
automatically forwarded to array-api-tests.
157
147
148
+ You may often need to update these xfail files, either to add new xfails
149
+ (e.g., because of new test suite features, or because a test that was
150
+ previously thought to be passing actually flaky fails). Try to keep the xfails
151
+ files organized, with comments pointing to upstream issues whenever possible.
152
+
153
+ From time to time, xpass tests should be removed from the xfail files, but be
154
+ aware that many xfail tests are flaky, so an xpass should only be removed if
155
+ you know that the underlying issue has been fixed.
156
+
158
157
Array libraries that require a GPU to run (currently only CuPy) cannot be
159
158
tested on CI. There is a helper script ` test_cupy.sh ` that can be used to
160
159
manually test CuPy on a machine with a CUDA GPU.
0 commit comments