Repository navigation
fix(sharding): keep whole-shard reads in the buffer's own memory - #4669
Merged
d-v-b merged 6 commits intoDec 4, 2026
Merged
Conversation
The fused pipeline's fast path for reading a whole uncompressed shard copied the shard to host memory with `as_numpy_array()`, then assigned each chunk into the output buffer. Under `zarr.config.enable_gpu()` the output is a CuPy array, which refuses host arrays, so the read failed. The fast path now uses `as_array_like()`, as `BytesCodec` does. The stored-byte-order view dtype is now computed by one helper, `BytesCodec._stored_dtype`, shared by the per-chunk and fast paths. It handles structured data types, so the fast path no longer has to decline them. Closes zarr-developers#4654 Assisted-by: ClaudeCode:claude-opus-5-5
Assisted-by: ClaudeCode:claude-opus-5-5
…y test The whole-shard fast path now takes structured data types, so its equivalence test with the per-chunk path draws a mixed-endian struct. The changelog fragment now says the structured fast path applies to FusedCodecPipeline only. Assisted-by: ClaudeCode:claude-opus-5-5
Assisted-by: ClaudeCode:claude-opus-5-5
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4669 +/- ##
==========================================
- Coverage 95.00% 95.00% -0.01%
==========================================
Files 96 96
Lines 14115 14110 -5
==========================================
- Hits 13410 13405 -5
Misses 705 705
🚀 New features to boost your workflow:
|
Merging this PR will not alter performance
Comparing Footnotes
|
d-v-b
marked this pull request as ready for review
November 30, 2026 17:08
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🤖 AI text below 🤖
Reading an uncompressed sharded array into GPU memory with
FusedCodecPipelineno longer raisesValueError: non-scalar numpy.ndarray cannot be used for fill. UnderFusedCodecPipeline, whole uncompressed shards with structured data types now also take the fast path.Fixes #4654.
Cause and fix
ShardingCodec._decode_full_shard_bulk_if_uncompressed, the fast path for reading a whole uncompressed shard, read the shard withas_numpy_array(). Underzarr.config.enable_gpu()that copies the shard to host memory, and the next step assigns each NumPy chunk into a CuPy output array, which CuPy refuses. Only the fused pipeline's synchronous partial-decode path reaches this function, soBatchedCodecPipelineand compressed shards were unaffected. The fast path arrived with #3885, so 3.3.0 through 3.4.2 are affected.The fast path now uses
as_array_like(), asBytesCodec._decode_syncdoes, so the payloads stay in the buffer's own memory.The fast path also had its own copy of
BytesCodec's stored-byte-order logic, without the structured-dtype branch, and declined structured shards for that reason. That logic is now one helper,BytesCodec._stored_dtype, used by both paths, and the structured-dtype exclusion is gone.GPU CI did not catch this because its only sharded test uses the default pipeline and compressed shards.
Tests and measurements
tests/test_buffer.py::test_read_sharded_array_into_gpu_memory(GPU only) reads a sharded array into CuPy under both pipelines, with and without compression. The fused, uncompressed case is the issue's reproduction.test_bulk_decode_declines_structured_dtypepinned the removed exclusion. It is replaced bytest_bulk_decode_reads_stored_byte_order, which checks that plain and structured shards decode correctly in both byte orders.enable_gpu()does, and anNDBuffer.__setitem__that rejects plain host arrays as CuPy does. With the fix, both pipelines read plain, big-endian and compressed shards correctly. The new GPU test has since run on real CuPy in upstream's GPU workflow (jobpy=3.12, head 281d66f), and all four cases passed.The agent first tried decoding each chunk through
BytesCodec._decode_sync. That cost up to 50% on shards of many small chunks, so it dropped that approach.Adversarial review
Round 1 (a fresh reviewer given only the diff and the issue) found nothing serious:
>i4and relies on CuPy's cast, exactly asBytesCodec._decode_syncalready does on the per-chunk path. The new GPU test covers only native byte order. Left as is, since both paths now behave the same.index_location="start", payloads of 8-byte dtypes start at offsets that are 4 mod 8, which could be misaligned for CUDA kernels. The per-chunk path takes the same zero-copy slices. Left as is.The reviewer found the fast path and the per-chunk path byte-identical on CPU across plain, mixed-endian, nested and datetime struct dtypes, both endians, C and F order, index at start and end, morton and colexicographic write orders, and 1-D to 3-D shards.
Round 2 (a second fresh reviewer, on the pushed diff) found nothing serious:
issue_formatwould render as a link to an unrelated upstream PR. Fixed by renaming it to this PR's number (4b4cb64).test_bulk_shard_decode_equals_general_decodenever drew a structured dtype, though structured dtypes now take the fast path. Fixed in 661f488: it draws a mixed-endian integer struct (59 of 100 examples), and it fails on the pre-fix code.FusedCodecPipeline.isinstance(codec, BytesCodec), so aBytesCodecsubclass that overrides_decode_syncwould be bypassed. No such subclass exists in the tree. Left as is.The second reviewer compared fused and batched reads on 416 configurations (312 on the fast path) with zero mismatches, and confirmed that edge shards, nested sharding, sparse shards and the async path all decline the fast path.
Two rounds in a row found nothing serious, so the review loop stopped.
🤖 Generated with Claude Code