|
1 | 1 | # Implementation Notes
|
2 | 2 |
|
| 3 | +This page outlines some notes on the implementation of array-api-compat. These |
| 4 | +details are not important for users of the package, but they may be useful to |
| 5 | +contributors. |
| 6 | + |
| 7 | +## Special Considerations |
| 8 | + |
| 9 | +array-api-compat requires some special development considerations that are |
| 10 | +different from most other Python libraries. The goal of array-api-compat is to |
| 11 | +be a small library that packages can either vendor or add as a dependency to |
| 12 | +implement array API support. Consequently, certain design considerations |
| 13 | +should be taken into account: |
| 14 | + |
| 15 | +- *No Hard Dependencies.* Although array-api-compat "depends" on NumPy, CuPy, |
| 16 | + PyTorch, etc., it does not hard depend on them. These libraries are not |
| 17 | + imported unless either an array object is passed to `array_namespace()`, or |
| 18 | + the specific `array_api_compat.<namespace>` sub-namespace is explicitly |
| 19 | + imported. |
| 20 | + |
| 21 | +- *Vendorability.* array-api-compat should be [vendorable](vendoring). This |
| 22 | + means that, for instance, all imports in the library are relative imports. |
| 23 | + No code in the package specifically references the name `array_api_compat` |
| 24 | + (we also support renaming the package to something else). |
| 25 | + Vendorability support is tested in `tests/test_vendoring.py`. |
| 26 | + |
| 27 | +- *Pure Python.* To make array-api-compat as easy as possible to add as a |
| 28 | + dependency, the code is all pure Python. |
| 29 | + |
| 30 | +- *Minimal Wrapping Only.* The wrapping functionality is minimal. This means |
| 31 | + that if something is difficult to wrap using pure Python, or if trying to |
| 32 | + support some array API behavior would require a significant amount of code, |
| 33 | + we prefer to leave the behavior as an upstream issue for the array library, |
| 34 | + and [document it as a known difference](../supported-array-libraries.md). |
| 35 | + |
| 36 | + This also means that we do not at this point in time implement anything |
| 37 | + other than wrappers for functions in the standard, and basic [helper |
| 38 | + functions](../helper-functions.md) that would be useful for most users of |
| 39 | + array-api-compat. The addition of functions that are not part of the array |
| 40 | + API standard is currently out-of-scope for this package. |
| 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. |
| 44 | + |
| 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 |
| 48 | + methods](https://data-apis.org/array-api/latest/API_specification/array_object.html) |
| 49 | + is therefore left unwrapped. Users can workaround issues by using |
| 50 | + corresponding [elementwise |
| 51 | + functions](https://data-apis.org/array-api/latest/API_specification/elementwise_functions.html) |
| 52 | + instead of operators, and using the [helper |
| 53 | + functions](../helper-functions.md) provided by array-api-compat instead of |
| 54 | + methods like `to_device`. |
| 55 | + |
| 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 |
| 60 | + |
| 61 | + - All namespaces are *extended* with wrapper functions. You may notice the |
| 62 | + extensive use of `import *` in various files in `array_api_compat`. While |
| 63 | + this would normally be questionable, this is the [one actual legitimate |
| 64 | + use-case for `import *`](https://peps.python.org/pep-0008/#imports), to |
| 65 | + re-export names from an external namespace. |
| 66 | + |
| 67 | + - All wrapper functions pass `**kwargs` through to the wrapped function. |
| 68 | + |
| 69 | + The onus is on users of array-api-compat to ensure they are only using |
| 70 | + portable array API behavior, e.g., by testing against [array-api-strict](array-api-strict). |
| 71 | + |
| 72 | + |
| 73 | +## Avoiding Code Duplication |
| 74 | + |
3 | 75 | As noted before, the goal of this library is to reuse the NumPy and CuPy array
|
4 | 76 | objects, rather than wrapping or extending them. This means that the functions
|
5 | 77 | need to accept and return `np.ndarray` for NumPy and `cp.ndarray` for CuPy.
|
@@ -64,3 +136,25 @@ corresponding document does not yet exist for PyTorch, but you can examine the
|
64 | 136 | various comments in the
|
65 | 137 | [implementation](https://github.com/data-apis/array-api-compat/blob/main/array_api_compat/torch/_aliases.py)
|
66 | 138 | to see what functions and behaviors have been wrapped.
|
| 139 | + |
| 140 | +## Tests |
| 141 | + |
| 142 | +The majority of the behavior for array-api-compat is tested by the |
| 143 | +[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 |
| 145 | +[`tests/`](https://github.com/data-apis/array-api-compat/tree/main/tests). |
| 146 | +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. |
| 149 | + |
| 150 | +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 |
| 153 | +Workflow](https://github.com/data-apis/array-api-compat/blob/main/.github/workflows/array-api-tests.yml). |
| 154 | +Most libraries have tests that must be xfailed or skipped for various reasons. |
| 155 | +These are defined in specific `<library>-xfails.txt` files and are |
| 156 | +automatically forwarded to array-api-tests. |
| 157 | + |
| 158 | +Array libraries that require a GPU to run (currently only CuPy) cannot be |
| 159 | +tested on CI. There is a helper script `test_cupy.sh` that can be used to |
| 160 | +manually test CuPy on a machine with a CUDA GPU. |
0 commit comments