Skip to content

Commit 97ddaf8

Browse files
committed
api: make buffer public api
1 parent bb8ab0f commit 97ddaf8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+159
-151
lines changed

docs/user-guide/config.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ This is the current default configuration::
7070
'string': {'name': 'vlen-utf8'}},
7171
'write_empty_chunks': False},
7272
'async': {'concurrency': 10, 'timeout': None},
73-
'buffer': 'zarr.core.buffer.cpu.Buffer',
73+
'buffer': 'zarr.buffer.cpu.Buffer',
7474
'codec_pipeline': {'batch_size': 1,
7575
'path': 'zarr.core.codec_pipeline.BatchedCodecPipeline'},
7676
'codecs': {'blosc': 'zarr.codecs.blosc.BloscCodec',
@@ -85,5 +85,5 @@ This is the current default configuration::
8585
'zstd': 'zarr.codecs.zstd.ZstdCodec'},
8686
'default_zarr_format': 3,
8787
'json_indent': 2,
88-
'ndbuffer': 'zarr.core.buffer.cpu.NDBuffer',
88+
'ndbuffer': 'zarr.buffer.cpu.NDBuffer',
8989
'threading': {'max_workers': None}}

src/zarr/core/buffer/core.py renamed to src/zarr/abc/buffer.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,23 @@
2323
from zarr.codecs.bytes import Endian
2424
from zarr.core.common import BytesLike, ChunkCoords
2525

26-
# Everything here is imported into ``zarr.core.buffer`` namespace.
27-
__all__: list[str] = []
26+
__all__ = [
27+
"ArrayLike",
28+
"Buffer",
29+
"BufferPrototype",
30+
"NDArrayLike",
31+
"NDBuffer",
32+
]
33+
34+
35+
def _check_item_key_is_1d_contiguous(key: Any) -> None:
36+
"""Raises error if `key` isn't a 1d contiguous slice"""
37+
if not isinstance(key, slice):
38+
raise TypeError(
39+
f"Item key has incorrect type (expected slice, got {key.__class__.__name__})"
40+
)
41+
if not (key.step is None or key.step == 1):
42+
raise ValueError("slice must be contiguous")
2843

2944

3045
@runtime_checkable
@@ -105,16 +120,6 @@ def __eq__(self, other: object) -> Self: # type: ignore[explicit-override, over
105120
"""
106121

107122

108-
def check_item_key_is_1d_contiguous(key: Any) -> None:
109-
"""Raises error if `key` isn't a 1d contiguous slice"""
110-
if not isinstance(key, slice):
111-
raise TypeError(
112-
f"Item key has incorrect type (expected slice, got {key.__class__.__name__})"
113-
)
114-
if not (key.step is None or key.step == 1):
115-
raise ValueError("slice must be contiguous")
116-
117-
118123
class Buffer(ABC):
119124
"""A flat contiguous memory block
120125
@@ -266,11 +271,11 @@ def to_bytes(self) -> bytes:
266271
return bytes(self.as_numpy_array())
267272

268273
def __getitem__(self, key: slice) -> Self:
269-
check_item_key_is_1d_contiguous(key)
274+
_check_item_key_is_1d_contiguous(key)
270275
return self.__class__(self._data.__getitem__(key))
271276

272277
def __setitem__(self, key: slice, value: Any) -> None:
273-
check_item_key_is_1d_contiguous(key)
278+
_check_item_key_is_1d_contiguous(key)
274279
self._data.__setitem__(key, value)
275280

276281
def __len__(self) -> int:
@@ -498,13 +503,3 @@ class BufferPrototype(NamedTuple):
498503

499504
buffer: type[Buffer]
500505
nd_buffer: type[NDBuffer]
501-
502-
503-
# The default buffer prototype used throughout the Zarr codebase.
504-
def default_buffer_prototype() -> BufferPrototype:
505-
from zarr.registry import (
506-
get_buffer_class,
507-
get_ndbuffer_class,
508-
)
509-
510-
return BufferPrototype(buffer=get_buffer_class(), nd_buffer=get_ndbuffer_class())

src/zarr/abc/codec.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from abc import abstractmethod
44
from typing import TYPE_CHECKING, Any, Generic, TypeVar
55

6+
from zarr.abc.buffer import Buffer, NDBuffer
67
from zarr.abc.metadata import Metadata
7-
from zarr.core.buffer import Buffer, NDBuffer
88
from zarr.core.common import ChunkCoords, concurrent_map
99
from zarr.core.config import config
1010

src/zarr/abc/store.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from itertools import starmap
66
from typing import TYPE_CHECKING, Protocol, runtime_checkable
77

8-
from zarr.core.buffer.core import default_buffer_prototype
8+
from zarr.buffer import default_buffer_prototype
99
from zarr.core.common import concurrent_map
1010
from zarr.core.config import config
1111

@@ -14,7 +14,7 @@
1414
from types import TracebackType
1515
from typing import Any, Self, TypeAlias
1616

17-
from zarr.core.buffer import Buffer, BufferPrototype
17+
from zarr.abc.buffer import Buffer, BufferPrototype
1818
from zarr.core.common import BytesLike
1919

2020
__all__ = ["ByteGetter", "ByteSetter", "Store", "set_or_delete"]

src/zarr/api/asynchronous.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
import numpy.typing as npt
1010
from typing_extensions import deprecated
1111

12+
from zarr.abc.buffer import NDArrayLike
1213
from zarr.core.array import Array, AsyncArray, create_array, get_array_metadata
1314
from zarr.core.array_spec import ArrayConfig, ArrayConfigLike
14-
from zarr.core.buffer import NDArrayLike
1515
from zarr.core.common import (
1616
JSON,
1717
AccessModeLiteral,

src/zarr/api/synchronous.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import numpy.typing as npt
1818

19+
from zarr.abc.buffer import NDArrayLike
1920
from zarr.abc.codec import Codec
2021
from zarr.api.asynchronous import ArrayLike, PathLike
2122
from zarr.core.array import (
@@ -25,7 +26,6 @@
2526
ShardsLike,
2627
)
2728
from zarr.core.array_spec import ArrayConfig, ArrayConfigLike
28-
from zarr.core.buffer import NDArrayLike
2929
from zarr.core.chunk_key_encodings import ChunkKeyEncoding, ChunkKeyEncodingLike
3030
from zarr.core.common import (
3131
JSON,

src/zarr/buffer/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from zarr.abc.buffer import BufferPrototype
2+
from zarr.buffer.cpu import numpy_buffer_prototype
3+
4+
__all__ = [
5+
"default_buffer_prototype",
6+
"numpy_buffer_prototype",
7+
]
8+
9+
10+
# The default buffer prototype used throughout the Zarr codebase.
11+
def default_buffer_prototype() -> BufferPrototype:
12+
from zarr.registry import (
13+
get_buffer_class,
14+
get_ndbuffer_class,
15+
)
16+
17+
return BufferPrototype(buffer=get_buffer_class(), nd_buffer=get_ndbuffer_class())

src/zarr/core/buffer/cpu.py renamed to src/zarr/buffer/cpu.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import numpy as np
1010
import numpy.typing as npt
1111

12-
from zarr.core.buffer import core
12+
import zarr.abc.buffer
1313
from zarr.registry import (
1414
register_buffer,
1515
register_ndbuffer,
@@ -19,11 +19,11 @@
1919
from collections.abc import Callable, Iterable
2020
from typing import Self
2121

22-
from zarr.core.buffer.core import ArrayLike, NDArrayLike
22+
from zarr.abc.buffer import ArrayLike, NDArrayLike
2323
from zarr.core.common import BytesLike
2424

2525

26-
class Buffer(core.Buffer):
26+
class Buffer(zarr.abc.buffer.Buffer):
2727
"""A flat contiguous memory block
2828
2929
We use Buffer throughout Zarr to represent a contiguous block of memory.
@@ -52,7 +52,7 @@ def create_zero_length(cls) -> Self:
5252
return cls(np.array([], dtype="b"))
5353

5454
@classmethod
55-
def from_buffer(cls, buffer: core.Buffer) -> Self:
55+
def from_buffer(cls, buffer: zarr.abc.buffer.Buffer) -> Self:
5656
"""Create a new buffer of an existing Buffer
5757
5858
This is useful if you want to ensure that an existing buffer is
@@ -107,7 +107,7 @@ def as_numpy_array(self) -> npt.NDArray[Any]:
107107
"""
108108
return np.asanyarray(self._data)
109109

110-
def __add__(self, other: core.Buffer) -> Self:
110+
def __add__(self, other: zarr.abc.buffer.Buffer) -> Self:
111111
"""Concatenate two buffers"""
112112

113113
other_array = other.as_array_like()
@@ -117,7 +117,7 @@ def __add__(self, other: core.Buffer) -> Self:
117117
)
118118

119119

120-
class NDBuffer(core.NDBuffer):
120+
class NDBuffer(zarr.abc.buffer.NDBuffer):
121121
"""An n-dimensional memory block
122122
123123
We use NDBuffer throughout Zarr to represent a n-dimensional memory block.
@@ -186,8 +186,10 @@ def __setitem__(self, key: Any, value: Any) -> None:
186186

187187

188188
def as_numpy_array_wrapper(
189-
func: Callable[[npt.NDArray[Any]], bytes], buf: core.Buffer, prototype: core.BufferPrototype
190-
) -> core.Buffer:
189+
func: Callable[[npt.NDArray[Any]], bytes],
190+
buf: zarr.abc.buffer.Buffer,
191+
prototype: zarr.abc.buffer.BufferPrototype,
192+
) -> zarr.abc.buffer.Buffer:
191193
"""Converts the input of `func` to a numpy array and the output back to `Buffer`.
192194
193195
This function is useful when calling a `func` that only support host memory such
@@ -214,13 +216,13 @@ def as_numpy_array_wrapper(
214216

215217

216218
# CPU buffer prototype using numpy arrays
217-
buffer_prototype = core.BufferPrototype(buffer=Buffer, nd_buffer=NDBuffer)
219+
buffer_prototype = zarr.abc.buffer.BufferPrototype(buffer=Buffer, nd_buffer=NDBuffer)
218220
# default_buffer_prototype = buffer_prototype
219221

220222

221223
# The numpy prototype used for E.g. when reading the shard index
222-
def numpy_buffer_prototype() -> core.BufferPrototype:
223-
return core.BufferPrototype(buffer=Buffer, nd_buffer=NDBuffer)
224+
def numpy_buffer_prototype() -> zarr.abc.buffer.BufferPrototype:
225+
return zarr.abc.buffer.BufferPrototype(buffer=Buffer, nd_buffer=NDBuffer)
224226

225227

226228
register_buffer(Buffer)

src/zarr/core/buffer/gpu.py renamed to src/zarr/buffer/gpu.py

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
import numpy as np
1212
import numpy.typing as npt
1313

14-
from zarr.core.buffer import core
15-
from zarr.core.buffer.core import ArrayLike, BufferPrototype, NDArrayLike
14+
import zarr.abc.buffer
1615

1716
if TYPE_CHECKING:
1817
from collections.abc import Iterable
@@ -26,7 +25,7 @@
2625
cp = None
2726

2827

29-
class Buffer(core.Buffer):
28+
class Buffer(zarr.abc.buffer.Buffer):
3029
"""A flat contiguous memory block on the GPU
3130
3231
We use Buffer throughout Zarr to represent a contiguous block of memory.
@@ -47,7 +46,7 @@ class Buffer(core.Buffer):
4746
array-like object that must be 1-dim, contiguous, and byte dtype.
4847
"""
4948

50-
def __init__(self, array_like: ArrayLike) -> None:
49+
def __init__(self, array_like: zarr.abc.buffer.ArrayLike) -> None:
5150
if cp is None:
5251
raise ImportError(
5352
"Cannot use zarr.buffer.gpu.Buffer without cupy. Please install cupy."
@@ -83,7 +82,7 @@ def create_zero_length(cls) -> Self:
8382
return cls(cp.array([], dtype="b"))
8483

8584
@classmethod
86-
def from_buffer(cls, buffer: core.Buffer) -> Self:
85+
def from_buffer(cls, buffer: zarr.abc.buffer.Buffer) -> Self:
8786
"""Create an GPU Buffer given an arbitrary Buffer
8887
This will try to be zero-copy if `buffer` is already on the
8988
GPU and will trigger a copy if not.
@@ -101,7 +100,7 @@ def from_bytes(cls, bytes_like: BytesLike) -> Self:
101100
def as_numpy_array(self) -> npt.NDArray[Any]:
102101
return cast(npt.NDArray[Any], cp.asnumpy(self._data))
103102

104-
def __add__(self, other: core.Buffer) -> Self:
103+
def __add__(self, other: zarr.abc.buffer.Buffer) -> Self:
105104
other_array = other.as_array_like()
106105
assert other_array.dtype == np.dtype("b")
107106
gpu_other = Buffer(other_array)
@@ -111,7 +110,7 @@ def __add__(self, other: core.Buffer) -> Self:
111110
)
112111

113112

114-
class NDBuffer(core.NDBuffer):
113+
class NDBuffer(zarr.abc.buffer.NDBuffer):
115114
"""A n-dimensional memory block on the GPU
116115
117116
We use NDBuffer throughout Zarr to represent a n-dimensional memory block.
@@ -136,7 +135,7 @@ class NDBuffer(core.NDBuffer):
136135
ndarray-like object that is convertible to a regular Numpy array.
137136
"""
138137

139-
def __init__(self, array: NDArrayLike) -> None:
138+
def __init__(self, array: zarr.abc.buffer.NDArrayLike) -> None:
140139
if cp is None:
141140
raise ImportError(
142141
"Cannot use zarr.buffer.gpu.NDBuffer without cupy. Please install cupy."
@@ -208,10 +207,10 @@ def __getitem__(self, key: Any) -> Self:
208207
def __setitem__(self, key: Any, value: Any) -> None:
209208
if isinstance(value, NDBuffer):
210209
value = value._data
211-
elif isinstance(value, core.NDBuffer):
210+
elif isinstance(value, zarr.abc.buffer.NDBuffer):
212211
gpu_value = NDBuffer(value.as_ndarray_like())
213212
value = gpu_value._data
214213
self._data.__setitem__(key, value)
215214

216215

217-
buffer_prototype = BufferPrototype(buffer=Buffer, nd_buffer=NDBuffer)
216+
buffer_prototype = zarr.abc.buffer.BufferPrototype(buffer=Buffer, nd_buffer=NDBuffer)

src/zarr/codecs/blosc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
from numcodecs.blosc import Blosc
1111

1212
from zarr.abc.codec import BytesBytesCodec
13-
from zarr.core.buffer.cpu import as_numpy_array_wrapper
13+
from zarr.buffer.cpu import as_numpy_array_wrapper
1414
from zarr.core.common import JSON, parse_enum, parse_named_configuration
1515
from zarr.registry import register_codec
1616

1717
if TYPE_CHECKING:
1818
from typing import Self
1919

20+
from zarr.abc.buffer import Buffer
2021
from zarr.core.array_spec import ArraySpec
21-
from zarr.core.buffer import Buffer
2222

2323

2424
class BloscShuffle(Enum):

0 commit comments

Comments
 (0)