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 6f69ea4
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: ADDING_NEW_FRAMEWORKS.md
+1Lines changed: 1 addition & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -176,6 +176,7 @@ class must define.
176
176
| Parent | The library... | Abstract methods |
177
177
|---|---|---|
178
178
|`HostDevices`| keeps arrays in main memory. |`_wrap_host_array(data)`, `_move_to_host(data)`|
179
+
|`NumpyBufferSharing`| is a `HostDevices` library whose main-memory array and a NumPy array can be one buffer (measure it before declaring it). | those of `HostDevices`, plus `_first_element_address(data)`|
179
180
|`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
181
|`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()`|
181
182
|`DLPackImport`| can wrap another library's buffer through DLPack. |`_import_dlpack(payload)`|
Copy file name to clipboardExpand all lines: CHANGELOG.md
+103-3Lines changed: 103 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -77,12 +77,112 @@ two different libraries.
77
77
`MemoryType`'s metaclass overrides are annotated with `EnumMeta`'s signatures;
78
78
its `__call__` override is gone (`Enum` already asks `_missing_`).
79
79
80
+
### Conversion
81
+
82
+
The mechanism names a consumer can now see, in the order the routes are tried:
83
+
84
+
| Mechanism | Route class | Meaning |
85
+
|---|---|---|
86
+
|`"stays in place"`|`StaysInPlace` (new) | Same library, same device. Nothing is converted; the input object is returned. No record. |
87
+
|`"device move"`|`DeviceMove`| Same library, another device. |
88
+
|`"dlpack"`|`DLPackExchange`| The target wraps the source's buffer through DLPack. |
89
+
|`"shared host buffer"`|`SharedHostBuffer` (new) | Both arrays are in main memory and the result begins at the same address as the source: no bytes were copied. |
90
+
|`"host copy"`|`HostCopy`| The values were copied through a NumPy array in main memory. |
91
+
92
+
**The plan and the run agree that nothing is converted.**
`StaysInPlace`. In 0.5.0 it returned `DeviceMove`, while running that
95
+
conversion returned the input and recorded nothing, so a caller had to restate
96
+
the rule. `DeviceMove` now applies only when the device differs. The rule is
97
+
stated once, in `StaysInPlace.applies`; `convert_to`, `to_numpy`, `from_numpy`
98
+
and `move_to_device` all ask it. Every route has `writes_a_record`; it is
99
+
`False` for `StaysInPlace` only. A record means data was moved, copied or
100
+
wrapped.
101
+
102
+
**A host exchange that shares the buffer is no longer called a copy.** On the
103
+
host, torch and NumPy share one buffer in both directions, and JAX hands NumPy
104
+
its own buffer; 0.5.0 recorded these as `"host copy"`. They are now recorded
105
+
as `"shared host buffer"`. A library declares that it can share by inheriting
106
+
`NumpyBufferSharing` (NumPy, torch and JAX do; TensorFlow copies in both
107
+
directions and does not). Whether one array was shared is decided after the
108
+
exchange by comparing where the two arrays begin (`host_address_of`).
109
+
110
+
What was measured (Python 3.12, NumPy 2.5.4, torch 2.14.1 CPU, JAX 0.11.2,
111
+
TensorFlow 2.21.0 CPU; pointers, `np.shares_memory` and writing to one array
112
+
and reading the other):
113
+
114
+
| Exchange on the host | Result |
115
+
|---|---|
116
+
| NumPy to torch | shared, for every dtype tried, Fortran order, stepped and offset views, read-only arrays. A view with a negative step is copied (arraybridge makes it contiguous first). |
117
+
| torch to NumPy | shared |
118
+
| JAX to NumPy | shared (the NumPy array is read-only) |
119
+
| NumPy to JAX | copied, unless the buffer is C-contiguous, starts on a 64-byte boundary and keeps its dtype; then shared |
120
+
| NumPy to TensorFlow, TensorFlow to NumPy | copied |
121
+
| an array with no elements | nothing to share: `"host copy"` of 0 bytes |
122
+
123
+
**What `planned` returns when sharing depends on the array.** A plan sees
124
+
only declarations and devices, never an array. It returns the route that is
125
+
tried first: `SharedHostBuffer` for NumPy, torch and JAX with NumPy on the
126
+
host. The record names what was done. Where the exchange copied (NumPy to JAX
127
+
for an ordinary NumPy allocation, a reversed view to torch) the record's
128
+
mechanism is `"host copy"` and `declined_mechanisms` is
129
+
`("shared host buffer",)`, the way a declined DLPack exchange is already
130
+
reported. The exchange is done once either way.
131
+
132
+
What a consumer that keyed on the old answers sees:
133
+
134
+
-`planned(...) is DeviceMove` is no longer true for the same library and
135
+
device; test `is StaysInPlace` (or `route.writes_a_record`).
136
+
- Rows that were `"host copy"` for NumPy and torch on the host, and for JAX
137
+
to NumPy on the host, are now `"shared host buffer"`. `"host copy"` rows
138
+
that follow a declined `SharedHostBuffer` carry it in `declined_mechanisms`,
139
+
which `ConversionCounters` does not key on.
140
+
-`planned` for NumPy with torch or JAX on the host (either direction) is
141
+
`SharedHostBuffer`. Between torch and JAX on the host it is still `"dlpack"`.
142
+
Anything to or from an accelerator, pyclesperanto or TensorFlow is planned
143
+
as before.
144
+
- The `nbytes` of a `from_numpy` record is the size of the array that was
145
+
made (it was the size of the NumPy input; they differ when JAX narrows
146
+
float64 to float32).
147
+
148
+
`ConversionRoute.taken(data, result, source, target)` is new: the route that
149
+
names what `convert` did, the route itself unless it says otherwise.
150
+
`ArrayLibrary.shares_numpy_buffer_on(device)` and
151
+
`ArrayLibrary.host_address_of(data)` are new, as is
152
+
`NumpyLibrary.registered()`. `HostCopy` and `SharedHostBuffer` share the new
153
+
parent `HostExchange`, which has no mechanism of its own.
154
+
`arraybridge.libraries` no longer imports `ConversionObserver`,
155
+
`ConversionRecord` and `HostCopy` (they were never exported from it; they are
156
+
in `arraybridge.conversion`).
157
+
158
+
**`ConversionObserver.current` belongs to the context that reads it.** It was
159
+
one attribute for the whole process, and `installed()` put back whatever it
160
+
had seen on entry: two threads that entered and left in the same order left
161
+
the second thread's observer installed for the rest of the process. The hook
162
+
is now a `contextvars.ContextVar`.
163
+
164
+
-`ConversionObserver.current` reads the observer of the running thread or
165
+
asyncio task. Assigning to it sets the observer of that context only.
166
+
-`observer.install()` makes it the observer of this context and returns the
167
+
one it replaced. `with observer.installed():` does so for a block.
168
+
- A new thread starts with the default observer, which ignores conversions.
169
+
**An observer installed in the main thread no longer sees conversions made
170
+
in worker threads.** To observe a thread you start, run it in a copy of your
171
+
context: `Thread(target=contextvars.copy_context().run, args=(work,))`, or
0 commit comments