-
-
Notifications
You must be signed in to change notification settings - Fork 398
feat: subchunk write order #3826
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
base: main
Are you sure you want to change the base?
Changes from 15 commits
5477d70
2e36679
c6498b2
58e071c
417df78
11b94c0
b0c622d
22a5dda
39634f0
f4498a6
be7ac83
a89249a
5ea1cf3
7b663ff
eae06dd
f36ea93
027c469
3f53182
8eb1792
1889212
09e2bc5
73352f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added a `subchunk_write_order` option to `ShardingCodec` to allow for `morton`, `unordered`, `lexicographic`, and `colexicographic` subchunk orderings. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,7 +5,7 @@ | |
| from enum import Enum | ||
| from functools import lru_cache | ||
| from operator import itemgetter | ||
| from typing import TYPE_CHECKING, Any, NamedTuple, cast | ||
| from typing import TYPE_CHECKING, Any, Literal, NamedTuple, cast | ||
|
|
||
| import numpy as np | ||
| import numpy.typing as npt | ||
|
|
@@ -47,7 +47,6 @@ | |
| BasicIndexer, | ||
| ChunkProjection, | ||
| SelectorTuple, | ||
| _morton_order, | ||
| _morton_order_keys, | ||
| c_order_iter, | ||
| get_indexer, | ||
|
|
@@ -59,7 +58,7 @@ | |
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Iterator | ||
| from typing import Self | ||
| from typing import Final, Self | ||
|
|
||
| from zarr.core.common import JSON | ||
| from zarr.core.dtype.wrapper import TBaseDType, TBaseScalar, ZDType | ||
|
|
@@ -78,6 +77,15 @@ class ShardingCodecIndexLocation(Enum): | |
| end = "end" | ||
|
|
||
|
|
||
| SubchunkWriteOrder = Literal["morton", "unordered", "lexicographic", "colexicographic"] | ||
| SUBCHUNK_WRITE_ORDER: Final[tuple[str, str, str, str]] = ( | ||
| "morton", | ||
| "unordered", | ||
| "lexicographic", | ||
| "colexicographic", | ||
| ) | ||
|
|
||
|
|
||
| def parse_index_location(data: object) -> ShardingCodecIndexLocation: | ||
| return parse_enum(data, ShardingCodecIndexLocation) | ||
|
|
||
|
|
@@ -305,7 +313,9 @@ class ShardingCodec( | |
| chunk_shape: tuple[int, ...] | ||
| codecs: tuple[Codec, ...] | ||
| index_codecs: tuple[Codec, ...] | ||
| rng: np.random.Generator | None | ||
| index_location: ShardingCodecIndexLocation = ShardingCodecIndexLocation.end | ||
| subchunk_write_order: SubchunkWriteOrder = "morton" | ||
|
|
||
| def __init__( | ||
| self, | ||
|
|
@@ -314,16 +324,24 @@ def __init__( | |
| codecs: Iterable[Codec | dict[str, JSON]] = (BytesCodec(),), | ||
| index_codecs: Iterable[Codec | dict[str, JSON]] = (BytesCodec(), Crc32cCodec()), | ||
| index_location: ShardingCodecIndexLocation | str = ShardingCodecIndexLocation.end, | ||
| subchunk_write_order: SubchunkWriteOrder = "morton", | ||
| rng: np.random.Generator | None = None, | ||
| ) -> None: | ||
| chunk_shape_parsed = parse_shapelike(chunk_shape) | ||
| codecs_parsed = parse_codecs(codecs) | ||
| index_codecs_parsed = parse_codecs(index_codecs) | ||
| index_location_parsed = parse_index_location(index_location) | ||
| if subchunk_write_order not in SUBCHUNK_WRITE_ORDER: | ||
| raise ValueError( | ||
| f"Unrecognized subchunk write order: {subchunk_write_order}. Only {SUBCHUNK_WRITE_ORDER} are allowed." | ||
| ) | ||
|
|
||
| object.__setattr__(self, "chunk_shape", chunk_shape_parsed) | ||
| object.__setattr__(self, "codecs", codecs_parsed) | ||
| object.__setattr__(self, "index_codecs", index_codecs_parsed) | ||
| object.__setattr__(self, "index_location", index_location_parsed) | ||
| object.__setattr__(self, "subchunk_write_order", subchunk_write_order) | ||
| object.__setattr__(self, "rng", rng) | ||
|
|
||
| # Use instance-local lru_cache to avoid memory leaks | ||
|
|
||
|
|
@@ -336,14 +354,15 @@ def __init__( | |
|
|
||
| # todo: typedict return type | ||
| def __getstate__(self) -> dict[str, Any]: | ||
| return self.to_dict() | ||
| return {"rng": self.rng, **self.to_dict()} | ||
|
|
||
| def __setstate__(self, state: dict[str, Any]) -> None: | ||
| config = state["configuration"] | ||
| object.__setattr__(self, "chunk_shape", parse_shapelike(config["chunk_shape"])) | ||
| object.__setattr__(self, "codecs", parse_codecs(config["codecs"])) | ||
| object.__setattr__(self, "index_codecs", parse_codecs(config["index_codecs"])) | ||
| object.__setattr__(self, "index_location", parse_index_location(config["index_location"])) | ||
| object.__setattr__(self, "rng", state["rng"]) | ||
|
|
||
| # Use instance-local lru_cache to avoid memory leaks | ||
| # object.__setattr__(self, "_get_chunk_spec", lru_cache()(self._get_chunk_spec)) | ||
|
|
@@ -523,6 +542,22 @@ async def _decode_partial_single( | |
| else: | ||
| return out | ||
|
|
||
| def _subchunk_order_iter(self, chunks_per_shard: tuple[int, ...]) -> Iterable[tuple[int, ...]]: | ||
| match self.subchunk_write_order: | ||
| case "morton": | ||
| subchunk_iter = morton_order_iter(chunks_per_shard) | ||
| case "lexicographic": | ||
| subchunk_iter = np.ndindex(chunks_per_shard) | ||
| case "colexicographic": | ||
| subchunk_iter = (c[::-1] for c in np.ndindex(chunks_per_shard[::-1])) | ||
| case "unordered": | ||
| subchunk_list = list(np.ndindex(chunks_per_shard)) | ||
| (self.rng if self.rng is not None else np.random.default_rng()).shuffle( | ||
| subchunk_list | ||
| ) | ||
| subchunk_iter = iter(subchunk_list) | ||
| return subchunk_iter | ||
|
|
||
| async def _encode_single( | ||
| self, | ||
| shard_array: NDBuffer, | ||
|
|
@@ -540,8 +575,7 @@ async def _encode_single( | |
| chunk_grid=RegularChunkGrid(chunk_shape=chunk_shape), | ||
| ) | ||
| ) | ||
|
|
||
| shard_builder = dict.fromkeys(morton_order_iter(chunks_per_shard)) | ||
| shard_builder = dict.fromkeys(self._subchunk_order_iter(chunks_per_shard)) | ||
|
|
||
| await self.codec_pipeline.write( | ||
| [ | ||
|
|
@@ -582,7 +616,7 @@ async def _encode_partial_single( | |
| ) | ||
|
|
||
| if self._is_complete_shard_write(indexer, chunks_per_shard): | ||
| shard_dict = dict.fromkeys(morton_order_iter(chunks_per_shard)) | ||
| shard_dict = dict.fromkeys(np.ndindex(chunks_per_shard)) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @mkitti Here and below, I don't think there is any need to construct the @d-v-b This now ensures we only shuffle in the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Python, dicts are ordered and I think the optimal iteration order may need to be encoded in the dict the last time I examined the situation. I was just trying to preserve the situation before my edits.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this wasn't about dictionary order, but instead in the vectorized case, the order to So I'm going to add something to the hyptothesis tests for this. I had the same feeling initially that the dictionary order mattered, but it turns out the final call to |
||
| else: | ||
| shard_reader = await self._load_full_shard_maybe( | ||
| byte_getter=byte_setter, | ||
|
|
@@ -592,7 +626,7 @@ async def _encode_partial_single( | |
| shard_reader = shard_reader or _ShardReader.create_empty(chunks_per_shard) | ||
| # Use vectorized lookup for better performance | ||
| shard_dict = shard_reader.to_dict_vectorized( | ||
| np.asarray(_morton_order(chunks_per_shard)) | ||
| np.array(list(np.ndindex(chunks_per_shard))) | ||
| ) | ||
|
|
||
| await self.codec_pipeline.write( | ||
|
|
@@ -631,7 +665,7 @@ async def _encode_shard_dict( | |
|
|
||
| template = buffer_prototype.buffer.create_zero_length() | ||
| chunk_start = 0 | ||
| for chunk_coords in morton_order_iter(chunks_per_shard): | ||
| for chunk_coords in self._subchunk_order_iter(chunks_per_shard): | ||
| value = map.get(chunk_coords) | ||
| if value is None: | ||
| continue | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.