Cpu
zarr.buffer.cpu ¶
__all__
module-attribute
¶
__all__ = [
"Buffer",
"NDBuffer",
"as_numpy_array_wrapper",
"buffer_prototype",
"numpy_buffer_prototype",
]
buffer_prototype
module-attribute
¶
buffer_prototype = BufferPrototype(
buffer=Buffer, nd_buffer=NDBuffer
)
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 an 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__ ¶
__eq__ ¶
__getitem__ ¶
__setitem__ ¶
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
combine ¶
Concatenate many buffers
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.
An NDBuffer is backed by an 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
__getitem__ ¶
__init__ ¶
__init__(array: NDArrayLike) -> None
__setitem__ ¶
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
astype ¶
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 an 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 an ndarray-like object
Parameters:
-
ndarray_like(NDArrayLike) –ndarray-like object
Returns:
-
New buffer representing `ndarray_like`–
Source code in zarr/core/buffer/core.py
from_numpy_array
classmethod
¶
Create a new buffer of Numpy array-like object
Parameters:
-
array_like(ArrayLike) –Object that can be coerced into a Numpy array
Returns:
-
New buffer representing `array_like`–
reshape ¶
squeeze ¶
transpose ¶
transpose(
axes: SupportsIndex | Sequence[SupportsIndex] | None,
) -> Self
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
bufas input.funcmust return bytes, which will be converted into aBufferbefore 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
numpy_buffer_prototype ¶
numpy_buffer_prototype() -> BufferPrototype