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
The core Buffer class represents a block of memory in either CPU or GPU memory.
There's no current method on Buffer to expose the memory via the buffer protocol without always forcing a copy. The closest thing is the as_array_like method, which returns an ArrayLike, but there's no way to get a BytesLike from it.
I called it "buffer" like to refer to the buffer protocol, hence the name in collections.abc.Buffer. FWIW I think that's a better name than "bytes like"
I see that the as_buffer_like method now exists. However, I was wondering why the Zarr Buffer itself does not implement the __buffer__ method to become a collections.abc.Buffer.
Here a simple demonstration:
classMyBuffer(zarr.core.buffer.cpu.Buffer):
def__buffer__(self, flags: int) ->memoryview:
returnself.as_buffer_like()
bfr_old=zarr.core.buffer.cpu.Buffer.from_bytes(b"abc")
bfr_new=MyBuffer.from_bytes(b"abc")
#memoryview(bfr_old) # raises TypeErrormemoryview(bfr_new) # works fine
I know that this is Python >= 3.12, so the as_buffer_like is likely still relevant. In general, I would feel comfortable if the Zarr buffer were also a Python buffer.
I believe this is impossible if we also want to support generic CUDA memory with the exact same interface, as CUDA memory generally doesn't support the buffer protocol (I think because the buffer protocol assumes immediate access, and CUDA device memory is essentially async? I'm not sure exactly. see cupy/cupy#1532). As some Zarr buffers -- those backed by numpy arrays -- do have a backend that implements the buffer protocol, we could look at a public interface to expose this.
I'm assuming here that the contract of the buffer protocol is that consumers get a view of the underlying memory. copying from CUDA memory to system memory would not maintain that contract. someone who knows better should correct me if this is wrong!
Activity
tomwhite commentedon Apr 21, 2025
BTW there's a typo in the title; it should be 'Add
Buffer.as_bytes_likemethod'.Also, this will be fixed in #2972.
kylebarron commentedon Apr 21, 2025
I called it "buffer" like to refer to the buffer protocol, hence the name in
collections.abc.Buffer. FWIW I think that's a better name than "bytes like"carloshorn commentedon Sep 23, 2026
I see that the
as_buffer_likemethod now exists. However, I was wondering why the ZarrBufferitself does not implement the__buffer__method to become acollections.abc.Buffer.Here a simple demonstration:
I know that this is Python >= 3.12, so the
as_buffer_likeis likely still relevant. In general, I would feel comfortable if the Zarr buffer were also a Python buffer.d-v-b commentedon Sep 23, 2026
I believe this is impossible if we also want to support generic CUDA memory with the exact same interface, as CUDA memory generally doesn't support the buffer protocol (I think because the buffer protocol assumes immediate access, and CUDA device memory is essentially async? I'm not sure exactly. see cupy/cupy#1532). As some Zarr buffers -- those backed by numpy arrays -- do have a backend that implements the buffer protocol, we could look at a public interface to expose this.
d-v-b commentedon Sep 23, 2026
I'm assuming here that the contract of the buffer protocol is that consumers get a view of the underlying memory. copying from CUDA memory to system memory would not maintain that contract. someone who knows better should correct me if this is wrong!