Skip to content

Add Buffer.as_buffer_like method. #2925

Open

Description

@kylebarron

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.

So what I want is

    @abstractmethod
    def as_bytes_like(self) -> collections.abc.Buffer:

(#2814 tracks the unification of BytesLike and collections.abc.Buffer types)

In practice, this means that the obstore integration (#1661) won't need to force a memory copy when uploading data.

ref #1661 (comment), cc @TomAugspurger

Activity

  1. tomwhite commented on Apr 21, 2025

    @tomwhite
    Member

    BTW there's a typo in the title; it should be 'Add Buffer.as_bytes_like method'.

    Also, this will be fixed in #2972.

  2. kylebarron commented on Apr 21, 2025

    @kylebarron
    ContributorAuthor

    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"

  3. carloshorn commented on Sep 23, 2026

    @carloshorn

    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:

    class MyBuffer(zarr.core.buffer.cpu.Buffer):
        def __buffer__(self, flags: int) -> memoryview:
            return self.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 TypeError
    memoryview(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.

  4. d-v-b commented on Sep 23, 2026

    @d-v-b
    Contributor

    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.

  5. d-v-b commented on Sep 23, 2026

    @d-v-b
    Contributor

    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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions