Skip to content

Chunk the dask view of a sharded array by shard, not by inner chunk - #58

Open
rhoadesScholar wants to merge 6 commits into
funkelab:mainfrom
rhoadesScholar:fix/sharded-dask-chunks
Open

rhoadesScholar wants to merge 6 commits into
funkelab:mainfrom
rhoadesScholar:fix/sharded-dask-chunks

Conversation

@rhoadesScholar

@rhoadesScholar rhoadesScholar commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

open_ds(chunks="strict") and prepare_ds build the dask view on the zarr array's .chunks, which for a zarr v3 sharded array is the inner chunk. On a large array stored as small inner chunks that view is unusable:

  • a (577, 64777, 78161) uint8 array stored as (1, 10, 336, 336) shards of (1, 10, 56, 56) inner chunks becomes a dask array of 93.7 million blocks;
  • a lazy channel selection over it (lazy_op(np.s_[0])) materialises about 80 GB of graph per process;
  • a (64, 512, 512) block read then takes 55 s.

Chunked by shard the same read takes 1.3 s and 0.7 GB and returns the same bytes (checked against a direct zarr read on a block with 11 M nonzero voxels).

This PR makes the default dask chunking (chunks="strict") follow the stored write unit: the shard shape when sharded, else the chunk shape (stored_chunks, which _data_mismatches reads too), in open_ds and in both Array constructions of prepare_ds. open_ds(chunks="inner") is new and chunks the view by stored chunk, the inner chunk of a sharded array (what the default did before). An explicit chunks= still passes through to dask.

chunk_shape, shard_shape and inner_chunk_shape now describe the stored layout of a zarr source whatever the dask view is chunked by (the view's chunking is on data.chunksize): previously inner_chunk_shape was the dask chunk, which made it the override under chunks=, and chunk_shape of an unsharded array was dask's shape-clipped chunk, so a stored unit longer than the array along an axis read back clipped and prepare_ds(..., chunk_shape=a.chunk_shape, inner_chunk_shape=a.inner_chunk_shape) refused or copied a different layout. The stored layout is read from any source carrying chunks and shards, so the iohub image array open_ome_ds hands Array keeps reporting its shard as chunk_shape (it is not a zarr.Array subclass); for data that records no layout, a dask or numpy array, both stay the view's own chunk.

One trade-off, stated in the open_ds and lazy_op docstrings: a callable lazy op (lambda d: d / 255) is computed per dask block, so on an array of large shards a read through it decodes and transforms every shard it touches whole, where the inner-chunk view decoded only the chunks under the read; plain slices and integer indices fuse into the read and are unaffected. chunks="inner" is the way back where that cost matters. Not changed here: open_ome_ds still builds its view with dask's auto chunking.

Tests: test_sharded_dask_view_is_chunked_by_shard (open_ds, prepare_ds create and reopen on every sharded store type, chunks="inner", a (1, 32, 32) override), test_unsharded_dask_view_is_chunked_by_chunk, test_a_shard_longer_than_the_array_still_names_the_stored_inner_chunk (sharded and unsharded, with the append and copy recipes) and test_inner_chunk_shape_of_data_that_is_not_zarr_is_the_views_chunk. Full suite (248 passed), ruff and ty pass.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VLT61ERzhKe9Ja3q1SS7o5

rhoadesScholar and others added 5 commits November 27, 2026 17:41
open_ds(chunks="strict") and prepare_ds built the dask view on the zarr
array's .chunks, which for a zarr v3 sharded array is the inner chunk.
A (577, 64777, 78161) uint8 array stored as (1, 10, 336, 336) shards of
(1, 10, 56, 56) inner chunks became a dask array of 93.7 million blocks:
a lazy channel selection over it materialised ~80 GB of graph per
process and a (64, 512, 512) block read took 55 s. Chunked by shard the
same read takes 1.3 s and 0.7 GB and returns the same bytes.

The dask view now follows the stored write unit: the shard shape when
sharded, else the chunk shape. inner_chunk_shape keeps naming the
stored inner chunk, read from the source array rather than from the
dask chunking, so it no longer changes with a chunks= override.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VLT61ERzhKe9Ja3q1SS7o5
A view built from a dask array has no stored chunk, and a view opened
with a chunks= override is not chunked as stored: both report the view's
own chunk shape, as before. The decision is taken once, at construction,
so a lazy op that truncates the view does not change it.

The test's override is (1, 32, 32), which neither the shard nor the
inner chunk equals, so the assertion discriminates; a dask-sourced
Array is covered. _stored_chunks reads zarr's .shards directly and is
defined before its first use.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VLT61ERzhKe9Ja3q1SS7o5
…e array.py already needs

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VLT61ERzhKe9Ja3q1SS7o5
…he shape; one stored_chunks rule; document the block cost of callable lazy ops

A stored shard (or chunk) longer than the array along an axis is clipped by dask, so the
view-as-stored test compared unequal tuples and inner_chunk_shape fell back to the clipped
shard; the copy-layout recipe then refused. _data_mismatches reads the write unit through
the same stored_chunks helper the views use. Tests: the unsharded view, a shard longer than
the array, and the create path on every sharded store type.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VLT61ERzhKe9Ja3q1SS7o5
…t of a zarr source; open_ds(chunks="inner")

The dask chunking is a read strategy (data.chunksize); the layout properties no longer
follow it, so an override or a stored unit longer than the array leaves the copy-layout
recipe intact, sharded or not. chunks="inner" restores one block per stored chunk for
reads through callable lazy ops, whose per-block cost the docstrings now state.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VLT61ERzhKe9Ja3q1SS7o5
@rhoadesScholar
rhoadesScholar marked this pull request as ready for review November 28, 2026 02:44
… (an OME image array) is read as stored

chunk_shape fell to the dask chunk for the iohub array open_ome_ds hands Array, while
shard_shape still read its shards. stored_chunks duck-types chunks/shards and answers None
for a dask or numpy array; the view-chunks keyword compare is guarded for array overrides.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VLT61ERzhKe9Ja3q1SS7o5

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant