Skip to content

fix(codecs): build buffers from the given prototype instead of zarr.config - #474

Draft
d-v-b wants to merge 7 commits into
mainfrom
claude/silly-wozniak-87525b
Draft

d-v-b wants to merge 7 commits into
mainfrom
claude/silly-wozniak-87525b

Conversation

@d-v-b

@d-v-b d-v-b commented Nov 30, 2026 •

Copy link
Copy Markdown
Owner

🤖 AI text below 🤖

Reading or writing a sharded array with a buffer prototype other than the one zarr.config names no longer fails, and a Zarr format 2 array decodes into the requested prototype's buffer kind. The codec pipelines now hand each codec its input in the buffer kind its chunk spec names and convert a Buffer/NDBuffer of another kind that a codec returns, so codecs that build their buffers from zarr.config keep working.

Related PRs: this unblocks #470, whose CPU-prototype default for ZarrArrayIO broke every sharded read and write in GPU mode before this change. A third-party codec test suite is being built on top of this branch in a separate session (branch claude/clever-blackwell-27699d); it will be opened as a draft stacked on this PR. This PR is based on main and independent of #470.

Summary of the change
  • ShardingCodec builds shard blobs and empty shard readers from the shard's prototype (not default_buffer_prototype()), drops the Non-default buffer prototypes are not supported within shards error, and keeps the shard index in host memory (numpy_buffer_prototype()): the index is a few bytes per chunk and is consumed as a NumPy array. The encoded index is converted to the shard's kind when the shard is assembled.
  • V2Codec decodes into chunk_spec.prototype.nd_buffer, not get_ndbuffer_class().
  • ChunkTransform, AsyncChunkTransform and BatchedCodecPipeline.decode_batch/encode_batch pass each codec's input through _as_prototype_buffer/_as_prototype_nd_buffer (new, in zarr.core.buffer.core) for the codec's own spec, and convert the chain's output to the caller's spec. scatter_chunk converts partial-decode output the same way. The helpers convert only Buffer/NDBuffer instances of another kind (an isinstance check; nothing is copied when the kind already matches) and pass any other value through unchanged, so codecs that return raw NumPy arrays or bytes behave as before.
  • Buffer conversion goes through prototype.buffer.from_bytes(buffer.as_buffer_like()): no slow-path warning for host-to-GPU, and no copy between host kinds.
  • Array/AsyncArray defaults still follow zarr.config.
  • docs/user-guide/extending.md states the prototype contract for custom codecs.
Tests

tests/test_codecs/test_buffer_prototype.py uses a fake device buffer kind that, like CuPy, refuses to mix with host memory (host buffers refuse its arrays; its arrays refuse to be combined with host arrays; its combine/from_buffer warn on host input like gpu.Buffer). Tests:

  • test_codecs_use_the_given_prototype: round trip (partial write into absent shards, full write, partial write, full and partial read) over both pipelines; layouts v3, v3 sharded, v3 sharded with a bytes-only index at the start, nested sharding, v2; chains of zarr codecs, zarr's numcodecs wrappers, an async-only codec, and prototype-ignoring codecs (each next to a checking codec); directions config=device/call=host, config=host/call=device, config=device/call=device. Checks the result's exact array type, the exact buffer type of every stored chunk, and that each test codec ran both ways. numcodecs cases run with a host prototype only (numcodecs is host-only on real CuPy).
  • test_codecs_read_no_buffer_config: zarr.config names a buffer kind that cannot be built; reads and writes with codecs that follow the prototype still work.
  • test_codec_chains_convert_input_of_another_kind, test_codec_chains_give_each_codec_its_own_specs_kind: the three chain runners directly.
  • test_scatter_chunk_converts_a_chunk_of_another_kind, test_v2_codec_decodes_into_the_given_prototype: the two places where an array read alone would hide a wrong kind.
  • test_codecs_that_return_raw_arrays_and_bytes_keep_working: codecs returning NumPy arrays / bytes (a partial write through them fails on main too, so it is not covered).

The change removes an error path and adds none, so there are no error-case tests. Mutation check: each of 48 single-site reversions of the change (every conversion call, own-spec vs caller-spec, the pass-through, from_bytes vs from_buffer, each config read in sharding and v2) fails at least one test. 63 of the first 105 matrix cases fail on main. test_sharding_use_of_gpu_prototype (GPU CI only) no longer expects a slow-path warning; this could not be run locally (no GPU).

Full suite: 13655 passed, 1318 skipped, 9 xfailed. mypy and ruff clean.

Adversarial review record

Fresh reviewer agents were given the diff and files, not the author's conclusions.

Round 1 (two reviewers, on 4ddd33a): no serious finding.

  • Conversion only at the shard boundary missed inner decode stages, prototype-ignoring index codecs in GPU mode (a narrow regression), and unsharded arrays; fixed holistically in a045f43 by converting in the pipelines.
  • Tests used a CPU subclass as the "other" kind, so 7 of 11 mutations survived; fixed in a045f43 with the fake device kind.
  • Rejected / out of scope: the bulk uncompressed shard decode assigns host chunks into a device output (pre-existing; fixed separately in upstream fix(sharding): keep whole-shard reads in the buffer's own memory zarr-developers/zarr-python#4669); np.array(out_array) in coordinate/vindex reads drops the prototype (array layer, not codecs); cast_value uses np.asarray on as_ndarray_like() (GPU-only, pre-existing).

Round 2 (two reviewers, on a045f43): two serious findings, both confirmed and fixed in e4a63ff.

  • Serious: codecs whose decode returns a raw NumPy array or bytes (e.g. MeteoSwiss icon-ch-vzarr's EccodesCodec) failed every read in the conversion helpers. Reproduced on both pipelines; fixed by passing non-buffer values through.
  • Serious: test_sharding_use_of_gpu_prototype would fail on GPU CI ("DID NOT WARN"): the warning came from crc32c running on the index under the GPU spec. Traced and reproduced with a warning-faithful CPU stand-in; fixed by dropping the pytest.warns.
  • Minor, fixed: conversion now targets each codec's own spec, not the caller's; conversion uses from_bytes(as_buffer_like()) (no GPU slow-path warning); fill_value_or_default re-export kept; numcodecs/v2 device cases removed (the fake let host-only codecs pass); docs note added.
  • Not changed: partial-decode reads through BytesCodec skip the chain's input conversion (only reachable with a store that breaks its prototype contract); the autouse fixture leaves the fake kinds registered (harmless).
  • Measured: no slowdown on the default CPU path (1000x1000 f8, 10x10 chunks, both pipelines, sharded and not; 20k-chunk read/write).

Round 3 (one reviewer, fix diff e4a63ff only): no serious finding. Fixed in 5735fba:

  • the T -> T helper signatures hid type errors; now Buffer -> Buffer / None -> None overloads;
  • stale ChunkTransform/AsyncChunkTransform docstrings and the docs' "at the cost of a copy" wording;
  • raw-value test codecs made sync-capable; V2Codec unit test runs on every direction with no numcodecs.
    Not changed: (a) ChunkTransform/AsyncChunkTransform hand bytes-to-bytes codecs ab_spec while BatchedCodecPipeline threads each bytes codec's resolve_metadata, so the runners disagree if a bytes codec changes the prototype; this divergence predates this PR (it applies to dtype and shape too) and no codec changes the prototype there. (b) Converting between two device buffer classes copies through the host; a from_buffer branch for device sources was tried and dropped because it only saves copies in that rare case and cannot be tested without a GPU. (c) A non-contiguous Buffer fails to convert (BufferError); the Buffer contract requires contiguous data.

Round 4 (one reviewer, fix diff 5735fba only): no serious finding. Fixed in the next commit: making the raw-value codecs sync-capable had dropped async-runner coverage, so the test now runs sync-capable and async-only variants. Nits not changed: the fake device kind checks the array type in from_buffer/combine (equivalent in this suite, closer to gpu.Buffer).

Rounds 3 and 4 found nothing serious, so review stopped there.

🤖 Generated with Claude Code

d-v-b and others added 4 commits November 30, 2026 12:03
The sharding codec refused any prototype other than the one `zarr.config`
names, and built shard blobs, the shard index, and empty shard readers from
`zarr.config`. The v2 codec built decoded chunks from `zarr.config` too. A
read or write that passed a prototype different from the configured one
therefore failed (sharded arrays) or decoded through the configured kind
(v2 arrays).

Codecs now use the prototype in the chunk spec. The shard index stays in
host memory whatever the data prototype, because it is consumed as a NumPy
array; it is converted to the shard's buffer kind when the shard is
assembled. Buffers an inner codec returns in another kind are converted at
the shard boundary, so codecs that ignore the prototype keep working.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… the pipelines

A codec that ignores `chunk_spec.prototype` (for instance one that builds its
buffers from `zarr.config`, as codecs from other packages may) could hand a
buffer of another kind (host or device memory) to the next codec, the store,
or the output array. Converting only at the shard boundary missed inner decode
stages, the shard index codecs, and unsharded arrays.

`ChunkTransform`, `AsyncChunkTransform` and `BatchedCodecPipeline` now pass
every codec's input and output through `_as_prototype_buffer` /
`_as_prototype_nd_buffer`, which convert only on a type mismatch, and
`scatter_chunk` converts partial-decode output the same way. The sharding
codec's own conversions, now redundant, are removed; it keeps the host-memory
index spec and converts the encoded index to the shard's kind with
`from_bytes`, which on a GPU avoids the slow-path copy `combine` would take.

The tests now use a fake device buffer kind that, like CuPy, refuses to mix
with host memory, add chains whose last codec ignores the prototype, a
bytes-only shard index, a `zarr.config` naming a buffer kind that cannot be
built, and direct tests of the chain runners and `scatter_chunk`. Each of 18
single-site reversions of the change fails at least one test.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Round-2 review fixes:
- `_as_prototype_buffer`/`_as_prototype_nd_buffer` convert only `Buffer` and
  `NDBuffer` instances; anything else a codec returns (a NumPy array, `bytes`)
  passes through unchanged, as before. Codecs from other packages that return
  raw arrays (e.g. icon-ch-vzarr's `EccodesCodec`) failed every read.
- Conversion goes through `from_bytes(as_buffer_like())`, so a host-to-GPU
  conversion takes no slow-path warning and host kinds convert without a copy;
  the shard index uses the same helper.
- Each codec gets its input as the kind its own spec names, and the chain's
  output is converted to the caller's spec, instead of converting every stage
  to the caller's prototype.
- `test_sharding_use_of_gpu_prototype` no longer expects a slow-path warning:
  the shard index no longer round-trips through the GPU kind.
- `fill_value_or_default` stays importable from `zarr.core.codec_pipeline`.
- The extending guide states the prototype contract for codecs.

The tests add codecs that return raw arrays and bytes, a codec that names
another prototype for the rest of the chain, bytes-side prototype checks next
to each prototype-ignoring codec, and restrict numcodecs (host-only) cases to
host prototypes. All 48 single-site reversions of the change fail a test.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d-v-b and others added 2 commits November 30, 2026 13:19
Round-3 review fixes:
- `_as_prototype_buffer`/`_as_prototype_nd_buffer` have `Buffer -> Buffer` /
  `None -> None` overloads instead of `T -> T`, so passing the wrong kind of
  value is a type error and the declared return type is the real one.
- The `ChunkTransform` and `AsyncChunkTransform` docstrings and the extending
  guide describe per-stage conversion and when it copies.
- The raw-value test codecs are sync-capable, so the sync chain runner is run
  with raw values too; the `V2Codec` unit test runs on every direction, with
  no numcodecs filters (numcodecs runs on host memory only).

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The raw-value test codecs became sync-capable in the previous commit, which
moved the fused pipeline from the async chain runner to the sync one instead
of adding it. Parametrize over sync-capable and async-only variants so both
runners see raw values. Also rewrap the extending-guide paragraph.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
d-v-b added a commit that referenced this pull request Nov 30, 2026
…474 now does

DeviceBuffer.from_buffer and combine check the source array, not the
buffer class, matching test_buffer_prototype.py on #474.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…prototype tests

On the big-endian s390x runner, `Delta(dtype="<i4")` declared a byte order
different from the array's native int32, so the v3 numcodecs-chain cases read
back byte-swapped values. The tests are about buffer kinds, not byte order;
use the native `int32`.

Assisted-by: ClaudeCode:claude-opus-5-5
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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