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 98d8c28
Browse filesBrowse the repository at this point in the historyBrowse files
* Fix JLArray buffer-backed temporaries against JLArrays 0.3.2
JLArrays 0.3.2 changed `JLArray`'s `offset` field from a count of elements
to a count of bytes, and `pointer` from `pointer(x.data) + x.offset*elsize(x)`
to `pointer(x.data) + x.offset`. `unsafe_buffer_wrap` built the temporary with
the constructor directly, so under 0.3.2 every buffer-backed temporary landed
at byte `start ÷ sizeof(T)` instead of `start`: misaligned, and overlapping
both each other and earlier temporaries, which silently corrupted results.
Go through `GPUArrays.derive` instead, the documented backend hook for
producing an array of a different type and size backed by the same data. Its
offset is expressed in elements on both versions, so this is insensitive to
how the backend stores it. `ROCArray` already used a byte offset directly and
was unaffected.
Compat gets a `0.3.1` lower bound, matching the versions this was verified on.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* Share the buffer-backing implementation across GPU backends
`unsafe_buffer_wrap` was written three times, once per backend, and each
reached for a different private detail: `JLArray`'s constructor with an
element offset, `ROCArray`'s with a byte offset, and `unsafe_wrap` on a raw
`CuPtr`. The first of those is what broke against JLArrays 0.3.2.
Route all three through `GPUArrays.derive` in a new `TensorOperationsGPUArraysExt`
instead. It is the documented backend hook for producing an array of a different
type and size backed by the same data, its offset is in elements on every
backend and version, and sharing the storage's refcounted handle keeps the
buffer alive for as long as a temporary derived from it -- which the `CuArray`
path, wrapping a bare pointer, did not do.
`buffer_arraytype` likewise collapses into one method, via a new
`buffer_similartype` that asks the buffer's own storage what it produces for the
requested element type and rank. This resolves at compile time and reproduces
each backend's answer exactly, memory space and buffer type included. It stays
out of the core default because a storage is not always the kind of array it
hands out: `similar(::Memory{UInt8}, T, Dims{1})` is a `Memory`, not a `Vector`.
Since `derive` offsets by whole elements, the element-addressability restriction
that only `JLArray` applied now covers all three, as `buffer_iselementaddressable`.
Element types it rejects fall back on a regular allocation, so this costs buffer
backing for exotic types, never correctness.
Verified against JLArrays 0.3.1 and 0.3.2, so the compat bound stays at "0.3".
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* Make the buffer's element-offset restriction unnecessary
`GPUArrays.derive` counts its offset in elements, which previously restricted
buffer-backed temporaries to element types whose size divides the alignment the
offset is padded to: `buffer_iselementaddressable` rejected the others, and the
GPUArrays extension had to override `buffer_arraytype` to apply that check.
Pad the offset to a multiple of `sizeof(T)` instead. This is the only padding
that can express a byte offset as an element offset on every backend -- some
carry the offset in bytes, others in elements, and no way of calling `derive`
can represent a non-element-aligned offset in the latter -- and it costs no
additional padding for element types whose size divides the alignment, which is
all the standard ones. Oversized and oddly sized element types are now served
from the buffer as well, rather than falling back on a regular allocation.
With the check gone, what is left of the extension's `buffer_arraytype` is the
`similar`-based rule, which is the right default for any storage: promote it to
the default implementation, and keep an `Array` answer for `Memory` storage,
the one storage whose `similar` stays a `Memory` at rank 1 while the temporaries
it hands out are `Array`s at every rank.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* Trim the comments and docstrings down
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
* Infer `buffer_arraytype` from `unsafe_buffer_wrap` itself
Asking `similar` what the storage would produce needed a `Memory` exception,
since a `Memory` stays a `Memory` at rank 1 while the temporaries it hands out
are `Array`s. Infer the wrap instead: the answer is then the type that is
actually handed out, and no method applying is itself the `nothing` answer, so a
storage only has to implement `unsafe_buffer_wrap` to be served from the buffer.
Dispatch that host method on host storages, so that a device buffer asked for an
`Array` has no method rather than a pointer conversion that would throw.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
0 commit comments