-
-
Notifications
You must be signed in to change notification settings - Fork 393
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
Open
ilan-gold
wants to merge
10
commits into
zarr-developers:main
Choose a base branch
from
ilan-gold:ig/shard_order
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+153
−10
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5477d70
feat: subchunk write order
ilan-gold 2e36679
chore: export `SubchunkWriteOrder`
ilan-gold c6498b2
chore: docs
ilan-gold 58e071c
chore: relnote
ilan-gold 417df78
Merge branch 'main' into ig/shard_order
ilan-gold 11b94c0
rename
ilan-gold b0c622d
refactor: no enums
ilan-gold 22a5dda
Merge branch 'main' into ig/shard_order
ilan-gold 39634f0
Merge branch 'main' into ig/shard_order
ilan-gold f4498a6
Merge branch 'main' into ig/shard_order
ilan-gold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,12 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import random | ||
| from collections.abc import Iterable, Mapping, MutableMapping, Sequence | ||
| from dataclasses import dataclass, replace | ||
| 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 | ||
|
|
@@ -59,7 +60,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 +79,15 @@ class ShardingCodecIndexLocation(Enum): | |
| end = "end" | ||
|
|
||
|
|
||
| SubchunkWriteOrder = Literal["morton", "unordered", "lexicographic", "colexicographic"] | ||
| SUBCHUNK_WRITE_ORDER: Final[tuple[str, str, str, str]] = ( | ||
ilan-gold marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "morton", | ||
| "unordered", | ||
| "lexicographic", | ||
| "colexicographic", | ||
| ) | ||
|
|
||
|
|
||
| def parse_index_location(data: object) -> ShardingCodecIndexLocation: | ||
| return parse_enum(data, ShardingCodecIndexLocation) | ||
|
|
||
|
|
@@ -306,6 +316,7 @@ class ShardingCodec( | |
| codecs: tuple[Codec, ...] | ||
| index_codecs: tuple[Codec, ...] | ||
| index_location: ShardingCodecIndexLocation = ShardingCodecIndexLocation.end | ||
| subchunk_write_order: SubchunkWriteOrder = "morton" | ||
|
|
||
| def __init__( | ||
| self, | ||
|
|
@@ -314,16 +325,22 @@ 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", | ||
| ) -> 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) | ||
|
|
||
| # Use instance-local lru_cache to avoid memory leaks | ||
|
|
||
|
|
@@ -523,6 +540,31 @@ 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)) | ||
| random.shuffle(subchunk_list) | ||
|
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. do we want a seed here, e.g. to get deterministic output? |
||
| subchunk_iter = iter(subchunk_list) | ||
| return subchunk_iter | ||
|
|
||
| def _subchunk_order_vectorized(self, chunks_per_shard: tuple[int, ...]) -> npt.NDArray[np.intp]: | ||
| match self.subchunk_write_order: | ||
| case "morton": | ||
| subchunk_order_vectorized = _morton_order(chunks_per_shard) | ||
| case _: | ||
| subchunk_order_vectorized = np.fromiter( | ||
| self._subchunk_order_iter(chunks_per_shard), | ||
| dtype=np.dtype((int, len(chunks_per_shard))), | ||
| ) | ||
| return subchunk_order_vectorized | ||
|
|
||
| async def _encode_single( | ||
| self, | ||
| shard_array: NDBuffer, | ||
|
|
@@ -540,8 +582,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 +623,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(self._subchunk_order_iter(chunks_per_shard)) | ||
| else: | ||
| shard_reader = await self._load_full_shard_maybe( | ||
| byte_getter=byte_setter, | ||
|
|
@@ -592,7 +633,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)) | ||
| self._subchunk_order_vectorized(chunks_per_shard) | ||
| ) | ||
|
|
||
| await self.codec_pipeline.write( | ||
|
|
@@ -631,7 +672,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 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.