Repository navigation
Conversation
| ({"x": "preserve", "y": -1}, (160, 500)), | ||
| ], | ||
| ) | ||
| def test_open_dataset_chunking_zarr_with_preserve( |
There was a problem hiding this comment.
These tests are kind of slow.
|
@dcherian is this something you would be able to review? I'd love to get more people trying it. |
* Move ``preserve_chunks`` to base ChunkManager class * Get target size from dask config options for DaskManager * Add test for open_zarr
…11230) * Add inherit='all' option to DataTree.to_dataset() * Add whats-new entry for inherit='all' (pydata#11230) * Fix prune() signature accidentally modified by ruff-format * Fix mypy errors: remove unused type-ignore, add typing.cast in test * Rename inherit='all' to inherit='all_coords' per review feedback
6512217 to
ca22bd6
Compare
ca22bd6 to
4bea2b3
Compare
|
Ok @dcherian this is now a PR that changes the behavior of This would be what the commit looks like to add the option jsignell@84cca20 |
|
I can make this not change the existing behavior at all if we want to do a slow rollout where people would do: import xarray as xr
with xr.set_options(use_dask_auto=False):
ds = xr.open_zarr("gs://gcp-public-data-arco-era5/ar/full_37-1h-0p25deg-chunk-1.zarr-v3", chunks="auto") |
|
I was just reminded of this PR and remembered that I still care about it! I am going to update this branch and include the option setting so that people can get back to the previous behavior. |
|
Sorry, I can review when you're done. I don't think an option is warranted, by saying "auto:, the user has already opted in to magic. |
😴 No significant changes. The benchmarks took a nap.Comparing |
headtr1ck
left a comment
There was a problem hiding this comment.
Thanks for picking this up again! Never splitting encoded chunks is a great default for "auto". But since "auto" now always goes through preserve_chunks, it also changes cases without encoded chunks:
import dask
import numpy as np
import xarray as xr
ds = xr.Dataset({"v": (("t", "y", "x"), np.random.rand(100, 100, 100))}) # 8 MB
ds.to_netcdf("contiguous.nc")
with dask.config.set({"array.chunk-size": "1MiB"}):
# 1. no encoded chunks: never split
print(xr.open_dataset("contiguous.nc", chunks="auto").v.data.chunksize)
# 2. existing dask chunks are not split by .chunk("auto")
print(ds.chunk(-1).chunk("auto").v.data.chunksize)
# 3. explicit chunks are ignored along dims with uneven chunks
uneven = ds.chunk({"y": (10, 20, 30, 40)})
print(uneven.chunk({"t": "auto", "y": 30, "x": "auto"}).v.chunks[1])| main | this PR | |
|---|---|---|
| 1. | (50, 50, 50) |
(100, 100, 100) |
| 2. | (50, 50, 50) |
(100, 100, 100) |
| 3. | (30, 30, 30, 10) |
(10, 20, 30, 40) |
- Without encoded chunks (contiguous netCDF, scipy, ...),
_get_chunkfalls back to the full shape as preferred chunks, whichpreserve_chunksthen never splits. Sochunks="auto"on a large contiguous variable now creates a single chunk of the whole variable. I thinkpreserve_chunksshould only be used when the backend reportspreferred_chunks, and otherwise fall back to dask's auto chunking. - In
_maybe_chunk, the existing dask chunks are passed asprevious_chunks, so.chunk("auto")can no longer split chunks that are too large, a common way to fix bad chunks. This is about dask chunks rather than encoded chunks, so I'd keep dask's behavior here. - In
preserve_chunks, a dimension with non-uniformprevious_chunkskeeps them even if an explicit chunk size was given: that branch only setsmax_chunks, and the explicit chunk is only applied whenprevious_chunkis an int.
Smaller things:
- The uniformity check
previous_chunk[1:-1] == previous_chunk[:-2]treats every 2-tuple as uniform, e.g.(128, 512). - The docstrings say multiples are taken "over the largest dimension", but the code now grows the last dimension first.
- The docs build fails because the whats-new entry needs a blank line before the "Breaking Changes" heading. Since this changes existing behavior, the entry probably belongs under "Breaking Changes" anyway.
- The comma changes in the
chunkserror messages and theopen_zarrdocstring are unrelated (and go in opposite directions). - The PR description still describes
chunks="preserve".
|
Thanks for taking a look @headtr1ck. You can see how the thinking evolved on this PR and created some gaps. I'll update it. |
|
@headtr1ck I had thought about that issue when calling |
chunks={}orchunks="auto". Since Remove special mapping ofautoto {} inopen_zarr#11010 got in xarra could eventually change the default onopen_zarrto map it to"preserve"rather than to{}if dask is available.whats-new.rstapi.rstProposal
A new
chunksoption that is only allowed to use encoded chunks or multiples of them. No chunk splitting allowed.Demo
Current behavior when
chunks="auto"This PR introduces a new option:
chunks="preserve"Context
I originally set out to update the
auto_chunksfunction in dask, but it felt like my goals were actually quite different. The goal of the daskauto_chunksfunction is to guarantee that the chunksize will be under a configurable limit while preserving the aspect ratio ofprevious_chunks(previous_chunks==encoding). This PR instead guarantees that encoded chunks are never split but it will multiply them by some factor to try to get the chunksize close to a targetsize. It doesn't try to preserve the aspect ratio of the chunks. Instead it goes after the dim where there is the greatest number of chunks and it tries to take those in bigger bites.Also:
"preserve".