Repository navigation
feat: implement the reshape extension codec #4542
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
clbarnes
wants to merge
8
commits into
zarr-developers:main
Choose a base branch
from
clbarnes:reshape
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.
+437
−0
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9862424
Implement and test the reshape codec
clbarnes ab39719
Reshape changelog
clbarnes cde8c5d
Fix numpydoc
clbarnes 40e89f7
Fix reshape test coverage
clbarnes 2f679bf
Merge branch 'main' into reshape
clbarnes 6f3d181
Merge branch 'main' into reshape
clbarnes 52ef154
Merge branch 'main' into reshape
clbarnes 0e1a994
Merge branch 'main' into reshape
d-v-b 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 @@ | ||
| Implement the [reshape extension codec](https://github.com/zarr-developers/zarr-extensions/tree/main/codecs/reshape). |
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 |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from zarr.abc.codec import ArrayArrayCodec | ||
| from zarr.core.array_spec import ArraySpec | ||
| from zarr.core.common import JSON, parse_named_configuration | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Iterable, Sequence | ||
| from typing import Self | ||
|
|
||
| from zarr.core.buffer import NDBuffer | ||
| from zarr.core.dtype.wrapper import TBaseDType, TBaseScalar, ZDType | ||
| from zarr.core.metadata.v3 import ChunkGridMetadata | ||
|
|
||
|
|
||
| # Type for shape specification: int, list of ints, or -1 | ||
| type ShapeElement = int | tuple[int, ...] | ||
| type ShapeSpec = tuple[ShapeElement, ...] | ||
|
|
||
|
|
||
| def prod(values: Iterable[int]) -> int: | ||
| p = 1 | ||
| for v in values: | ||
| p *= v | ||
| return p | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ReshapeCodec(ArrayArrayCodec): | ||
| """Reshape codec. | ||
|
|
||
| An array-to-array codec that performs a reshape operation, similar to numpy's | ||
| reshape function. The reshape operation preserves the lexicographical (C-order) | ||
| traversal of elements. | ||
|
|
||
| The given `shape`'s elements must be one of: | ||
| - A positive integer specifying an explicit size. | ||
| - A list of integers specifying input dimensions whose product determines | ||
| the output dimension size. | ||
| - The special value -1 (at most once), which is inferred to satisfy | ||
| prod(output_shape) == prod(input_shape). | ||
|
|
||
| Notes | ||
| ----- | ||
| This codec does NOT alter the lexicographical order of elements. The contents | ||
| of the output array B is related to the input array A by: | ||
| ``ravel(B) == ravel(A)``. | ||
|
|
||
| When possible, implementations should construct a virtual view rather than | ||
| copy the array. | ||
|
|
||
| References | ||
| ---------- | ||
|
|
||
| - The `reshape` codec spec: https://github.com/zarr-developers/zarr-extensions/tree/main/codecs/reshape | ||
| """ | ||
|
|
||
| is_fixed_size = True | ||
|
|
||
| shape: ShapeSpec | ||
|
|
||
| def __post_init__(self) -> None: | ||
| self._validate_shape_spec() | ||
|
|
||
| @classmethod | ||
| def from_dict(cls, data: dict[str, JSON]) -> Self: | ||
| _, configuration_parsed = parse_named_configuration(data, "reshape") | ||
| return cls(**configuration_parsed) # type: ignore[arg-type] | ||
|
|
||
| def to_dict(self) -> dict[str, JSON]: | ||
| return {"name": "reshape", "configuration": {"shape": self.shape}} | ||
|
|
||
| def validate( | ||
| self, | ||
| shape: tuple[int, ...], | ||
| dtype: ZDType[TBaseDType, TBaseScalar], | ||
| chunk_grid: ChunkGridMetadata, | ||
| ) -> None: | ||
| """Validate the codec configuration against the array metadata.""" | ||
| self._compute_output_shape(shape) | ||
|
|
||
| def evolve_from_array_spec(self, array_spec: ArraySpec) -> Self: | ||
| """Evolve the codec from an array specification.""" | ||
| self._compute_output_shape(array_spec.shape) | ||
| return self | ||
|
|
||
| def resolve_metadata(self, chunk_spec: ArraySpec) -> ArraySpec: | ||
| """Resolve the output metadata after applying this codec.""" | ||
| output_shape = self._compute_output_shape(chunk_spec.shape) | ||
| return ArraySpec( | ||
| shape=output_shape, | ||
| dtype=chunk_spec.dtype, | ||
| fill_value=chunk_spec.fill_value, | ||
| config=chunk_spec.config, | ||
| prototype=chunk_spec.prototype, | ||
| ) | ||
|
|
||
| def _decode_sync( | ||
| self, | ||
| chunk_array: NDBuffer, | ||
| chunk_spec: ArraySpec, | ||
| ) -> NDBuffer: | ||
| """Decode: reshape back to the original shape.""" | ||
| # chunk_spec contains the *decoded* (original) shape | ||
| # The input chunk_array has the encoded (reshaped) shape | ||
| # We need to reshape it back to the original shape | ||
| inferred_shape = self._compute_output_shape(chunk_spec.shape) | ||
| if inferred_shape != chunk_array.shape: | ||
| raise RuntimeError( | ||
| "Shape of chunk to decode %s does not match shape of requested array %s encoded with reshape codec shape=%s", | ||
| chunk_array.shape, | ||
| chunk_spec.shape, | ||
| self.shape, | ||
| ) | ||
| return chunk_array.reshape(chunk_spec.shape) | ||
|
|
||
| async def _decode_single( | ||
| self, | ||
| chunk_array: NDBuffer, | ||
| chunk_spec: ArraySpec, | ||
| ) -> NDBuffer: | ||
| return self._decode_sync(chunk_array, chunk_spec) | ||
|
|
||
| def _encode_sync( | ||
| self, | ||
| chunk_array: NDBuffer, | ||
| chunk_spec: ArraySpec, | ||
| ) -> NDBuffer | None: | ||
| """Encode: reshape to the output shape.""" | ||
| output_shape = self._compute_output_shape(chunk_spec.shape) | ||
| return chunk_array.reshape(output_shape) | ||
|
|
||
| async def _encode_single( | ||
| self, | ||
| chunk_array: NDBuffer, | ||
| chunk_spec: ArraySpec, | ||
| ) -> NDBuffer | None: | ||
| return self._encode_sync(chunk_array, chunk_spec) | ||
|
|
||
| def compute_encoded_size(self, input_byte_length: int, chunk_spec: ArraySpec) -> int: | ||
| """Compute the encoded size (same as input since reshape doesn't change data).""" | ||
| return input_byte_length | ||
|
|
||
| def _compute_output_shape( | ||
| self, | ||
| input_shape: Sequence[int], | ||
| ) -> tuple[int, ...]: | ||
| """ | ||
| Compute the output shape from the shape specification and input shape. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| input_shape : tuple[int, ...] | ||
| The shape of the input array. | ||
|
|
||
| Returns | ||
| ------- | ||
| tuple[int, ...] | ||
| The computed output shape. | ||
|
|
||
| Raises | ||
| ------ | ||
| ValueError | ||
| If the shape specification is invalid or the invariant | ||
| prod(output_shape) == prod(input_shape) cannot be satisfied. | ||
| """ | ||
| output_shape: list[int] = [] | ||
| infer_idx: int | None = None | ||
| input_total = int(prod(input_shape)) | ||
|
|
||
| # we can skip a lot of validity checks here because we have already used _validate_shape_spec | ||
| for i, elem in enumerate(self.shape): | ||
| if isinstance(elem, int): | ||
| if elem == -1: | ||
| infer_idx = i | ||
| output_shape.append(-1) # placeholder | ||
| else: | ||
| output_shape.append(elem) | ||
| continue | ||
|
|
||
| os = 1 | ||
|
|
||
| for d in elem: | ||
| if d >= len(input_shape): | ||
| raise ValueError(f"Dimension index {d} out of range for reshape spec") | ||
| os *= input_shape[d] | ||
|
|
||
| # Product of input dimensions specified | ||
| output_shape.append(os) | ||
|
|
||
| # Handle inference of -1 dimension | ||
| if infer_idx is not None: | ||
| known_product = prod(s for s in output_shape if s != -1) | ||
| if known_product == 0 or input_total % known_product != 0: | ||
| raise ValueError( | ||
| f"Cannot infer dimension size: input total {input_total} is not divisible " | ||
| f"by known product {known_product}." | ||
| ) | ||
| output_shape[infer_idx] = input_total // known_product | ||
|
|
||
| output_total = prod(output_shape) | ||
| if output_total != input_total: | ||
| raise ValueError( | ||
| f"The invariant prod(output_shape) == prod(input_shape) is not satisfied. " | ||
| f"Got prod({output_shape}) = {output_total} != prod({input_shape}) = {input_total}." | ||
| ) | ||
|
|
||
| return tuple(output_shape) | ||
|
|
||
| def _validate_shape_spec(self) -> None: | ||
| """ | ||
| Validate the shape specification according to the constraints. | ||
|
|
||
| Validates: | ||
| 1. Input dimensions are in monotonically increasing order. | ||
| 2. For each input_dims list with k > 0 dimensions, the constraints about | ||
| raveled indices are satisfiable. | ||
|
|
||
| Raises | ||
| ------ | ||
| ValueError | ||
| If more than explicit length of -1 is found, | ||
| or any explicit length < -1 is found, | ||
| or any indices into the array shape are below 0, | ||
| or the indices into the array shape are not monotonically increasing. | ||
| """ | ||
| last_dim_idx = -1 | ||
| found_minus_one = False | ||
|
|
||
| for elem in self.shape: | ||
| if isinstance(elem, int): | ||
| if elem == -1: | ||
| if found_minus_one: | ||
| raise ValueError("-1 should appear at most once in the reshape spec") | ||
| else: | ||
| found_minus_one = True | ||
| elif elem < 1: | ||
| raise ValueError("Explicit axis size must be >=1 in the reshape spec") | ||
| continue | ||
|
|
||
| for d in elem: | ||
| if d < 0: | ||
| raise ValueError( | ||
| "Index of input dimension must not be negative in the reshape spec" | ||
| ) | ||
| if d <= last_dim_idx: | ||
| raise ValueError( | ||
| "Index of input dimension must be monotonically increasing in reshape spec" | ||
| ) | ||
|
|
||
| last_dim_idx = d | ||
|
|
||
|
|
||
| __all__ = ["ReshapeCodec"] | ||
Oops, something went wrong.
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.
we have to validate against the chunk shape, not the array shape
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.
there's a bigger fix we need in this are that also affects sharding and transpose, see #4352. i'll try to get that merged
Uh oh!
There was an error while loading. Please reload this page.
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.
Ah, I suppose that's not accessible here then - it's called against the array, not the chunk. No validation to be done here, then, as validating the shape spec is done on instantiation.