|
1 | 1 | """
|
2 |
| -A NumPy sub-namespace that conforms to the Python array API standard. |
3 |
| -
|
4 |
| -This submodule accompanies NEP 47, which proposes its inclusion in NumPy. It |
5 |
| -is still considered experimental, and will issue a warning when imported. |
6 |
| -
|
7 |
| -This is a proof-of-concept namespace that wraps the corresponding NumPy |
8 |
| -functions to give a conforming implementation of the Python array API standard |
9 |
| -(https://data-apis.github.io/array-api/latest/). The standard is currently in |
10 |
| -an RFC phase and comments on it are both welcome and encouraged. Comments |
11 |
| -should be made either at https://github.com/data-apis/array-api or at |
12 |
| -https://github.com/data-apis/consortium-feedback/discussions. |
13 |
| -
|
14 |
| -NumPy already follows the proposed spec for the most part, so this module |
15 |
| -serves mostly as a thin wrapper around it. However, NumPy also implements a |
16 |
| -lot of behavior that is not included in the spec, so this serves as a |
17 |
| -restricted subset of the API. Only those functions that are part of the spec |
18 |
| -are included in this namespace, and all functions are given with the exact |
19 |
| -signature given in the spec, including the use of position-only arguments, and |
20 |
| -omitting any extra keyword arguments implemented by NumPy but not part of the |
21 |
| -spec. The behavior of some functions is also modified from the NumPy behavior |
22 |
| -to conform to the standard. Note that the underlying array object itself is |
23 |
| -wrapped in a wrapper Array() class, but is otherwise unchanged. This submodule |
24 |
| -is implemented in pure Python with no C extensions. |
25 |
| -
|
26 |
| -The array API spec is designed as a "minimal API subset" and explicitly allows |
27 |
| -libraries to include behaviors not specified by it. But users of this module |
28 |
| -that intend to write portable code should be aware that only those behaviors |
29 |
| -that are listed in the spec are guaranteed to be implemented across libraries. |
30 |
| -Consequently, the NumPy implementation was chosen to be both conforming and |
31 |
| -minimal, so that users can use this implementation of the array API namespace |
32 |
| -and be sure that behaviors that it defines will be available in conforming |
33 |
| -namespaces from other libraries. |
34 |
| -
|
35 |
| -A few notes about the current state of this submodule: |
36 |
| -
|
37 |
| -- There is a test suite that tests modules against the array API standard at |
38 |
| - https://github.com/data-apis/array-api-tests. The test suite is still a work |
39 |
| - in progress, but the existing tests pass on this module, with a few |
40 |
| - exceptions: |
41 |
| -
|
42 |
| - - DLPack support (see https://github.com/data-apis/array-api/pull/106) is |
43 |
| - not included here, as it requires a full implementation in NumPy proper |
44 |
| - first. |
45 |
| -
|
46 |
| - The test suite is not yet complete, and even the tests that exist are not |
47 |
| - guaranteed to give a comprehensive coverage of the spec. Therefore, when |
48 |
| - reviewing and using this submodule, you should refer to the standard |
49 |
| - documents themselves. There are some tests in array_api_strict.tests, but |
50 |
| - they primarily focus on things that are not tested by the official array API |
51 |
| - test suite. |
52 |
| -
|
53 |
| -- There is a custom array object, array_api_strict.Array, which is returned by |
54 |
| - all functions in this module. All functions in the array API namespace |
55 |
| - implicitly assume that they will only receive this object as input. The only |
56 |
| - way to create instances of this object is to use one of the array creation |
57 |
| - functions. It does not have a public constructor on the object itself. The |
58 |
| - object is a small wrapper class around numpy.ndarray. The main purpose of it |
59 |
| - is to restrict the namespace of the array object to only those dtypes and |
60 |
| - only those methods that are required by the spec, as well as to limit/change |
61 |
| - certain behavior that differs in the spec. In particular: |
62 |
| -
|
63 |
| - - The array API namespace does not have scalar objects, only 0-D arrays. |
64 |
| - Operations on Array that would create a scalar in NumPy create a 0-D |
65 |
| - array. |
66 |
| -
|
67 |
| - - Indexing: Only a subset of indices supported by NumPy are required by the |
68 |
| - spec. The Array object restricts indexing to only allow those types of |
69 |
| - indices that are required by the spec. See the docstring of the |
70 |
| - array_api_strict.Array._validate_indices helper function for more |
71 |
| - information. |
72 |
| -
|
73 |
| - - Type promotion: Some type promotion rules are different in the spec. In |
74 |
| - particular, the spec does not have any value-based casting. The spec also |
75 |
| - does not require cross-kind casting, like integer -> floating-point. Only |
76 |
| - those promotions that are explicitly required by the array API |
77 |
| - specification are allowed in this module. See NEP 47 for more info. |
78 |
| -
|
79 |
| - - Functions do not automatically call asarray() on their input, and will not |
80 |
| - work if the input type is not Array. The exception is array creation |
81 |
| - functions, and Python operators on the Array object, which accept Python |
82 |
| - scalars of the same type as the array dtype. |
83 |
| -
|
84 |
| -- All functions include type annotations, corresponding to those given in the |
85 |
| - spec (see _typing.py for definitions of some custom types). These do not |
86 |
| - currently fully pass mypy due to some limitations in mypy. |
87 |
| -
|
88 |
| -- Dtype objects are just the NumPy dtype objects, e.g., float64 = |
89 |
| - np.dtype('float64'). The spec does not require any behavior on these dtype |
90 |
| - objects other than that they be accessible by name and be comparable by |
91 |
| - equality, but it was considered too much extra complexity to create custom |
92 |
| - objects to represent dtypes. |
93 |
| -
|
94 |
| -- All places where the implementations in this submodule are known to deviate |
95 |
| - from their corresponding functions in NumPy are marked with "# Note:" |
96 |
| - comments. |
97 |
| -
|
98 |
| -Still TODO in this module are: |
99 |
| -
|
100 |
| -- DLPack support for numpy.ndarray is still in progress. See |
101 |
| - https://github.com/numpy/numpy/pull/19083. |
102 |
| -
|
103 |
| -- The copy=False keyword argument to asarray() is not yet implemented. This |
104 |
| - requires support in numpy.asarray() first. |
105 |
| -
|
106 |
| -- Some functions are not yet fully tested in the array API test suite, and may |
107 |
| - require updates that are not yet known until the tests are written. |
108 |
| -
|
109 |
| -- The spec is still in an RFC phase and may still have minor updates, which |
110 |
| - will need to be reflected here. |
111 |
| -
|
112 |
| -- Complex number support in array API spec is planned but not yet finalized, |
113 |
| - as are the fft extension and certain linear algebra functions such as eig |
114 |
| - that require complex dtypes. |
| 2 | +array_api_strict is a strict, minimal implementation of the Python array |
| 3 | +API (https://data-apis.org/array-api/latest/) |
| 4 | +
|
| 5 | +The purpose of array-api-strict is to provide an implementation of the array |
| 6 | +API for consuming libraries to test against so they can be completely sure |
| 7 | +their usage of the array API is portable. |
| 8 | +
|
| 9 | +It is *not* intended to be used by end-users. End-users of the array API |
| 10 | +should just use their favorite array library (NumPy, CuPy, PyTorch, etc.) as |
| 11 | +usual. It is also not intended to be used as a dependency by consuming |
| 12 | +libraries. Consuming library code should use the |
| 13 | +array-api-compat (https://github.com/data-apis/array-api-compat) package to |
| 14 | +support the array API. Rather, it is intended to be used in the test suites of |
| 15 | +consuming libraries to test their array API usage. |
115 | 16 |
|
116 | 17 | """
|
117 | 18 |
|
|
0 commit comments