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
{{ message }}
Repository navigation
Commit ae0da21
Browse filesBrowse the repository at this point in the historyBrowse files
|`oom_messages`| Lower-case text that marks an out-of-memory error message. |`()`|
125
161
|`oom_exception_paths`| Attribute paths from the module to its out-of-memory exception classes. |`()`|
126
162
127
-
Every library defines three methods asked for by `ArrayLibrary` itself:
163
+
Every library defines three methods asked for by `ArrayLibrary` itself, and
164
+
the array operations listed under "Array operations" below:
128
165
129
166
-`device_of(data)`: the device holding the array. It never returns `None`.
130
167
-`_to_host(data)`: the array as a NumPy array.
@@ -139,12 +176,11 @@ class must define.
139
176
| Parent | The library... | Abstract methods |
140
177
|---|---|---|
141
178
|`HostDevices`| keeps arrays in main memory. |`_wrap_host_array(data)`, `_move_to_host(data)`|
142
-
|`AcceleratorDevices`| can keep arrays on numbered accelerators. |`_accelerator_count()`, `_accelerator_scope(device)`, `_move_to_accelerator(data, device)`, `_upload(data, device)`|
143
-
|`CudaLibrary`| is an `AcceleratorDevices` library with its own CUDA streams and a memory cache to release. | those of `AcceleratorDevices`, plus `stream_scope(stream)`, `current_device()`, `_free_cached_memory()`|
179
+
|`AcceleratorDevices`| can keep arrays on numbered accelerators. |`_accelerator_count()`, `_accelerator_scope(device)`, `_move_to_accelerator(data, device)`, `_upload(data, device)`, `_device_if_accepted(value)`|
180
+
|`CudaLibrary`| is an `AcceleratorDevices` library with its own CUDA streams and a memory cache to release. | those of `AcceleratorDevices`, plus `create_stream()`, `stream_scope(stream)`, `current_device()`, `_free_cached_memory()`|
144
181
|`DLPackImport`| can wrap another library's buffer through DLPack. |`_import_dlpack(payload)`|
145
182
|`DLPackProtocolExport`| has arrays with the standard DLPack export methods on every device. | none |
146
183
|`NvidiaWheelLibraries`| loads CUDA from the `nvidia-*` wheels; child processes get their library directories on the search path. | none |
147
-
|`MutableArrays`| has the NumPy array API and arrays that can be changed in place; supplies `_scale_dtype` and in-place plane normalisation. |`_rescales_into(target_dtype)`|
148
184
149
185
Methods with a default worth knowing:
150
186
@@ -154,18 +190,63 @@ Methods with a default worth knowing:
|`InPlaceAssignment`| has arrays that take `array[index] = values`. |`assign`| none |
213
+
|`ArrayMath`| can make arrays without a reference array, on its default device. A library that cannot (pyclesperanto) does not inherit it and has none of these four methods. | nothing |`linspace`, `ones`, `outer`, `floor`|
214
+
|`NumpyArrayApi`| has an array module and arrays that follow the NumPy API (`array_module.max(data, axis=...)`, `data.astype(...)`, ...). It is an `ArrayMath` library. | every operation above except `assign`, and the four of `ArrayMath`|`array_module`|
215
+
|`MutableArrays`| is a `NumpyArrayApi` and `InPlaceAssignment` library; supplies `_scale_dtype` and in-place plane normalisation. | all of the above |`array_module`, `_rescales_into(target_dtype)`|
216
+
217
+
Override an inherited operation only where the library's numeric rule
218
+
differs (JAX's `mean` casts to float32 first).
165
219
166
220
The six built-in classes at the end of `src/arraybridge/libraries.py` are the
167
221
reference for each combination of roles.
168
222
223
+
## Typing
224
+
225
+
The checker reads a library from `stubs/<library>/`, never from an installed
226
+
copy, so `mypy` gives the same result on every machine. For a new library:
227
+
228
+
- declare in `stubs/<library>/` exactly the functions, classes and attributes
229
+
the class calls, with the library's own names and types;
230
+
- state the module in the class, so its calls are checked against that
231
+
declaration:
232
+
233
+
```python
234
+
ifTYPE_CHECKING:
235
+
import mylib
236
+
237
+
classMylibLibrary(HostDevices["mylib.Array"]):
238
+
name ="mylib"
239
+
240
+
ifTYPE_CHECKING:
241
+
module = mylib
242
+
```
243
+
244
+
- call `self.module.<function>(...)` directly: a local `module = self.module`
245
+
is a plain `ModuleType` to the checker and loses the declaration;
246
+
- a `NumpyArrayApi` library returns its module of NumPy-named functions from
247
+
`array_module`; the checker compares that module with the `NumpyFunctions`
248
+
Protocol (`MutableNumpyFunctions` for `MutableArrays`).
249
+
169
250
## Tests to add
170
251
171
252
- detection by array and by module name, and behaviour when the package is
@@ -181,7 +262,7 @@ Run:
181
262
```bash
182
263
ruff check src tests
183
264
black --check src tests
184
-
mypy src --ignore-missing-imports
265
+
mypy src stubs
185
266
pytest
186
267
python -m sphinx -E -W --keep-going -b html docs/source /tmp/arraybridge-docs
0 commit comments