Skip to content

Deterministic chunk padding #2755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/2755.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Upon creation, an empty ``zarr.core.buffer.cpu.NDBuffer`` is filled with zeros to ensure deterministic behavior.
7 changes: 3 additions & 4 deletions src/zarr/core/buffer/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,9 @@ def create(
order: Literal["C", "F"] = "C",
fill_value: Any | None = None,
) -> Self:
ret = cls(np.empty(shape=tuple(shape), dtype=dtype, order=order))
if fill_value is not None:
ret.fill(fill_value)
return ret
return cls(
np.full(shape=tuple(shape), fill_value=fill_value or 0, dtype=dtype, order=order)
)

@classmethod
def from_numpy_array(cls, array_like: npt.ArrayLike) -> Self:
Expand Down
28 changes: 28 additions & 0 deletions tests/test_store/test_memory.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
from __future__ import annotations

from typing import TYPE_CHECKING

import pytest

import zarr
from zarr.core.buffer import Buffer, cpu, gpu
from zarr.storage import GpuMemoryStore, MemoryStore
from zarr.testing.store import StoreTests
from zarr.testing.utils import gpu_test

if TYPE_CHECKING:
from zarr.core.common import ZarrFormat


class TestMemoryStore(StoreTests[MemoryStore, cpu.Buffer]):
store_cls = MemoryStore
Expand Down Expand Up @@ -46,6 +52,28 @@ def test_store_supports_partial_writes(self, store: MemoryStore) -> None:
def test_list_prefix(self, store: MemoryStore) -> None:
assert True

@pytest.mark.parametrize("dtype", ["uint8", "float32", "int64"])
@pytest.mark.parametrize("zarr_format", [2, 3])
async def test_deterministic_size(
self, store: MemoryStore, dtype, zarr_format: ZarrFormat
) -> None:
def padding_size() -> int:
a = zarr.empty(
store=store,
shape=(3,),
chunks=(1000,),
dtype=dtype,
zarr_format=zarr_format,
overwrite=True,
)
a[...] = 1
key = "0" if zarr_format == 2 else "c/0"
return len(store._store_dict[key])

l1 = padding_size()
l2 = padding_size()
assert l1 == l2


@gpu_test
class TestGpuMemoryStore(StoreTests[GpuMemoryStore, gpu.Buffer]):
Expand Down
Loading