Buffer
zarr.buffer ¶
Implementations of the Zarr Buffer interface.
See Also¶
zarr.abc.buffer: Abstract base class for the Zarr Buffer interface.
zarr.buffer.cpu ¶
Buffer ¶
Bases: Buffer
A flat contiguous memory block
We use Buffer throughout Zarr to represent a contiguous block of memory.
A Buffer is backed by a underlying array-like instance that represents the memory. The memory type is unspecified; can be regular host memory, CUDA device memory, or something else. The only requirement is that the array-like instance can be copied/converted to a regular Numpy array (host memory).
Notes
This buffer is untyped, so all indexing and sizes are in bytes.
Parameters:
-
array_like
(ArrayLike
) –array-like object that must be 1-dim, contiguous, and byte dtype.
Source code in zarr/core/buffer/cpu.py
__add__ ¶
Concatenate two buffers
Source code in zarr/core/buffer/cpu.py
as_array_like ¶
as_array_like() -> ArrayLike
Returns the underlying array (host or device memory) of this buffer
This will never copy data.
Returns:
-
The underlying 1d array such as a NumPy or CuPy array.
–
Source code in zarr/core/buffer/core.py
as_buffer_like ¶
Returns the buffer as an object that implements the Python buffer protocol.
Notes
Might have to copy data, since the implementation uses .as_numpy_array()
.
Returns:
-
An object that implements the Python buffer protocol
–
Source code in zarr/core/buffer/core.py
as_numpy_array ¶
Returns the buffer as a NumPy array (host memory).
Notes
Might have to copy data, consider using .as_array_like()
instead.
Returns:
-
NumPy array of this buffer (might be a data copy)
–
Source code in zarr/core/buffer/cpu.py
from_array_like
classmethod
¶
Create a new buffer of an array-like object
Parameters:
-
array_like
(ArrayLike
) –array-like object that must be 1-dim, contiguous, and byte dtype.
Returns:
-
New buffer representing `array_like`
–
Source code in zarr/core/buffer/core.py
from_buffer
classmethod
¶
Create a new buffer of an existing Buffer
This is useful if you want to ensure that an existing buffer is of the correct subclass of Buffer. E.g., MemoryStore uses this to return a buffer instance of the subclass specified by its BufferPrototype argument.
Typically, this only copies data if the data has to be moved between memory types, such as from host to device memory.
Parameters:
-
buffer
(Buffer
) –buffer object.
Returns:
-
A new buffer representing the content of the input buffer
–
Notes
Subclasses of Buffer
must override this method to implement
more optimal conversions that avoid copies where possible
Source code in zarr/core/buffer/cpu.py
from_bytes
classmethod
¶
from_bytes(bytes_like: BytesLike) -> Self
Create a new buffer of a bytes-like object (host memory)
Parameters:
-
bytes_like
(BytesLike
) –bytes-like object
Returns:
-
New buffer representing `bytes_like`
–
Source code in zarr/core/buffer/cpu.py
to_bytes ¶
to_bytes() -> bytes
Returns the buffer as bytes
(host memory).
Warnings
Will always copy data, only use this method for small buffers such as metadata
buffers. If possible, use .as_numpy_array()
or .as_array_like()
instead.
Returns:
-
`bytes` of this buffer (data copy)
–
Source code in zarr/core/buffer/core.py
NDBuffer ¶
Bases: NDBuffer
An n-dimensional memory block
We use NDBuffer throughout Zarr to represent a n-dimensional memory block.
A NDBuffer is backed by a underlying ndarray-like instance that represents the memory. The memory type is unspecified; can be regular host memory, CUDA device memory, or something else. The only requirement is that the ndarray-like instance can be copied/converted to a regular Numpy array (host memory).
Notes
The two buffer classes Buffer and NDBuffer are very similar. In fact, Buffer is a special case of NDBuffer where dim=1, stride=1, and dtype="B". However, in order to use Python's type system to differentiate between the contiguous Buffer and the n-dim (non-contiguous) NDBuffer, we keep the definition of the two classes separate.
Parameters:
-
array
(NDArrayLike
) –ndarray-like object that is convertible to a regular Numpy array.
Source code in zarr/core/buffer/cpu.py
all_equal ¶
Compare to other
using np.array_equal.
Source code in zarr/core/buffer/core.py
as_ndarray_like ¶
as_ndarray_like() -> NDArrayLike
Returns the underlying array (host or device memory) of this buffer
This will never copy data.
Returns:
-
The underlying array such as a NumPy or CuPy array.
–
Source code in zarr/core/buffer/core.py
as_numpy_array ¶
Returns the buffer as a NumPy array (host memory).
Warnings
Might have to copy data, consider using .as_ndarray_like()
instead.
Returns:
-
NumPy array of this buffer (might be a data copy)
–
Source code in zarr/core/buffer/cpu.py
as_scalar ¶
Returns the buffer as a scalar value
create
classmethod
¶
create(
*,
shape: Iterable[int],
dtype: DTypeLike,
order: Literal["C", "F"] = "C",
fill_value: Any | None = None,
) -> Self
Create a new buffer and its underlying ndarray-like object
Parameters:
-
shape
(Iterable[int]
) –The shape of the buffer and its underlying ndarray-like object
-
dtype
(DTypeLike
) –The datatype of the buffer and its underlying ndarray-like object
-
order
(Literal['C', 'F']
, default:'C'
) –Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
-
fill_value
(Any | None
, default:None
) –If not None, fill the new buffer with a scalar value.
Returns:
-
New buffer representing a new ndarray_like object
–
Notes
A subclass can overwrite this method to create a ndarray-like object other then the default Numpy array.
Source code in zarr/core/buffer/cpu.py
empty
classmethod
¶
Create an empty buffer with the given shape, dtype, and order.
This method can be faster than NDBuffer.create
because it doesn't
have to initialize the memory used by the underlying ndarray-like
object.
Parameters:
-
shape
(tuple[int, ...]
) –The shape of the buffer and its underlying ndarray-like object
-
dtype
(DTypeLike
) –The datatype of the buffer and its underlying ndarray-like object
-
order
(Literal['C', 'F']
, default:'C'
) –Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
Returns:
-
buffer
–New buffer representing a new ndarray_like object with empty data.
See Also
NDBuffer.create Create a new buffer with some initial fill value.
from_ndarray_like
classmethod
¶
from_ndarray_like(ndarray_like: NDArrayLike) -> Self
Create a new buffer of a ndarray-like object
Parameters:
-
ndarray_like
(NDArrayLike
) –ndarray-like object
Returns:
-
New buffer representing `ndarray_like`
–
Source code in zarr/core/buffer/core.py
as_numpy_array_wrapper ¶
as_numpy_array_wrapper(
func: Callable[[NDArray[Any]], bytes],
buf: Buffer,
prototype: BufferPrototype,
) -> Buffer
Converts the input of func
to a numpy array and the output back to Buffer
.
This function is useful when calling a func
that only support host memory such
as GZip.decode
and Blosc.decode
. In this case, use this wrapper to convert
the input buf
to a Numpy array and convert the result back into a Buffer
.
Parameters:
-
func
(Callable[[NDArray[Any]], bytes]
) –The callable that will be called with the converted
buf
as input.func
must return bytes, which will be converted into aBuffer
before returned. -
buf
(Buffer
) –The buffer that will be converted to a Numpy array before given as input to
func
. -
prototype
(BufferPrototype
) –The prototype of the output buffer.
Returns:
-
The result of `func` converted to a `Buffer`
–
Source code in zarr/core/buffer/cpu.py
zarr.buffer.gpu ¶
Buffer ¶
Bases: Buffer
A flat contiguous memory block on the GPU
We use Buffer throughout Zarr to represent a contiguous block of memory.
A Buffer is backed by a underlying array-like instance that represents the memory. The memory type is unspecified; can be regular host memory, CUDA device memory, or something else. The only requirement is that the array-like instance can be copied/converted to a regular Numpy array (host memory).
Notes
This buffer is untyped, so all indexing and sizes are in bytes.
Parameters:
-
array_like
(ArrayLike
) –array-like object that must be 1-dim, contiguous, and byte dtype.
Source code in zarr/core/buffer/gpu.py
__add__ ¶
Concatenate two buffers
Source code in zarr/core/buffer/gpu.py
as_array_like ¶
as_array_like() -> ArrayLike
Returns the underlying array (host or device memory) of this buffer
This will never copy data.
Returns:
-
The underlying 1d array such as a NumPy or CuPy array.
–
Source code in zarr/core/buffer/core.py
as_buffer_like ¶
Returns the buffer as an object that implements the Python buffer protocol.
Notes
Might have to copy data, since the implementation uses .as_numpy_array()
.
Returns:
-
An object that implements the Python buffer protocol
–
Source code in zarr/core/buffer/core.py
as_numpy_array ¶
Returns the buffer as a NumPy array (host memory).
Notes
Might have to copy data, consider using .as_array_like()
instead.
Returns:
-
NumPy array of this buffer (might be a data copy)
–
create_zero_length
classmethod
¶
create_zero_length() -> Self
Create an empty buffer with length zero
Returns:
-
New empty 0-length buffer
–
from_array_like
classmethod
¶
Create a new buffer of an array-like object
Parameters:
-
array_like
(ArrayLike
) –array-like object that must be 1-dim, contiguous, and byte dtype.
Returns:
-
New buffer representing `array_like`
–
Source code in zarr/core/buffer/core.py
from_buffer
classmethod
¶
Create an GPU Buffer given an arbitrary Buffer
This will try to be zero-copy if buffer
is already on the
GPU and will trigger a copy if not.
Returns:
-
New GPU Buffer constructed from `buffer`
–
Source code in zarr/core/buffer/gpu.py
from_bytes
classmethod
¶
from_bytes(bytes_like: BytesLike) -> Self
Create a new buffer of a bytes-like object (host memory)
Parameters:
-
bytes_like
(BytesLike
) –bytes-like object
Returns:
-
New buffer representing `bytes_like`
–
to_bytes ¶
to_bytes() -> bytes
Returns the buffer as bytes
(host memory).
Warnings
Will always copy data, only use this method for small buffers such as metadata
buffers. If possible, use .as_numpy_array()
or .as_array_like()
instead.
Returns:
-
`bytes` of this buffer (data copy)
–
Source code in zarr/core/buffer/core.py
NDBuffer ¶
Bases: NDBuffer
A n-dimensional memory block on the GPU
We use NDBuffer throughout Zarr to represent a n-dimensional memory block.
A NDBuffer is backed by a underlying ndarray-like instance that represents the memory. The memory type is unspecified; can be regular host memory, CUDA device memory, or something else. The only requirement is that the ndarray-like instance can be copied/converted to a regular Numpy array (host memory).
Notes
The two buffer classes Buffer and NDBuffer are very similar. In fact, Buffer is a special case of NDBuffer where dim=1, stride=1, and dtype="B". However, in order to use Python's type system to differentiate between the contiguous Buffer and the n-dim (non-contiguous) NDBuffer, we keep the definition of the two classes separate.
Parameters:
-
array
(NDArrayLike
) –ndarray-like object that is convertible to a regular Numpy array.
Source code in zarr/core/buffer/gpu.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
all_equal ¶
Compare to other
using np.array_equal.
Source code in zarr/core/buffer/core.py
as_ndarray_like ¶
as_ndarray_like() -> NDArrayLike
Returns the underlying array (host or device memory) of this buffer
This will never copy data.
Returns:
-
The underlying array such as a NumPy or CuPy array.
–
Source code in zarr/core/buffer/core.py
as_numpy_array ¶
Returns the buffer as a NumPy array (host memory).
Warnings
Might have to copy data, consider using .as_ndarray_like()
instead.
Returns:
-
NumPy array of this buffer (might be a data copy)
–
Source code in zarr/core/buffer/gpu.py
as_scalar ¶
Returns the buffer as a scalar value
create
classmethod
¶
create(
*,
shape: Iterable[int],
dtype: DTypeLike,
order: Literal["C", "F"] = "C",
fill_value: Any | None = None,
) -> Self
Create a new buffer and its underlying ndarray-like object
Parameters:
-
shape
(Iterable[int]
) –The shape of the buffer and its underlying ndarray-like object
-
dtype
(DTypeLike
) –The datatype of the buffer and its underlying ndarray-like object
-
order
(Literal['C', 'F']
, default:'C'
) –Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
-
fill_value
(Any | None
, default:None
) –If not None, fill the new buffer with a scalar value.
Returns:
-
New buffer representing a new ndarray_like object
–
Notes
A subclass can overwrite this method to create a ndarray-like object other then the default Numpy array.
Source code in zarr/core/buffer/gpu.py
empty
classmethod
¶
Create an empty buffer with the given shape, dtype, and order.
This method can be faster than NDBuffer.create
because it doesn't
have to initialize the memory used by the underlying ndarray-like
object.
Parameters:
-
shape
(tuple[int, ...]
) –The shape of the buffer and its underlying ndarray-like object
-
dtype
(DTypeLike
) –The datatype of the buffer and its underlying ndarray-like object
-
order
(Literal['C', 'F']
, default:'C'
) –Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory.
Returns:
-
buffer
–New buffer representing a new ndarray_like object with empty data.
See Also
NDBuffer.create Create a new buffer with some initial fill value.
from_ndarray_like
classmethod
¶
from_ndarray_like(ndarray_like: NDArrayLike) -> Self
Create a new buffer of a ndarray-like object
Parameters:
-
ndarray_like
(NDArrayLike
) –ndarray-like object
Returns:
-
New buffer representing `ndarray_like`
–