Repository navigation
Description
What happened?
Xarray stack does not chunk along the last coordinate, producing huge chunks, as described in #5754. Dask, seeing code like this:
da2 = da.stack(new=("z", "t")).groupby("new").map(sum).unstack("new")
produces warning and suggestion to use context manager:
with dask.config.set(**{"array.slicing.split_large_chunks": True}):
da2 = da.stack(new=("z", "t")).groupby("new").map(sum).unstack("new")
This fails with message IndexError: tuple index out of range.
What did you expect to happen?
I expect this to work. #5754 is closed.
Minimal Complete Verifiable Example
import dask.array
import numpy as np
import xarray as xr
var = xr.Variable(
("t", "z", "u", "x", "y"),
dask.array.random.random((1200, 4, 2, 1000, 100), chunks=(1, 1, -1, -1, -1)),
)
da = xr.DataArray(var)
def sum(ds):
return ds.sum(dim="u")
with dask.config.set(**{"array.slicing.split_large_chunks": True}):
da2 = da.stack(new=("z", "t")).groupby("new").map(sum).unstack("new")
da2MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
- Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
Cell In[21], line 5
2 return ds.sum(dim="u")
4 with dask.config.set(**{"array.slicing.split_large_chunks": True}):
----> 5 da2 = da.stack(new=("z", "t")).groupby("new").map(sum).unstack("new")
6 da2
File ~/mambaforge/envs/icec/lib/python3.11/site-packages/xarray/core/dataarray.py:2855, in DataArray.unstack(self, dim, fill_value, sparse)
2795 def unstack(
2796 self,
2797 dim: Dims = None,
2798 fill_value: Any = dtypes.NA,
2799 sparse: bool = False,
2800 ) -> Self:
2801 """
2802 Unstack existing dimensions corresponding to MultiIndexes into
2803 multiple new dimensions.
(...)
2853 DataArray.stack
2854 """
-> 2855 ds = self._to_temp_dataset().unstack(dim, fill_value, sparse)
2856 return self._from_temp_dataset(ds)
File ~/mambaforge/envs/icec/lib/python3.11/site-packages/xarray/core/dataset.py:5500, in Dataset.unstack(self, dim, fill_value, sparse)
5498 for d in dims:
5499 if needs_full_reindex:
-> 5500 result = result._unstack_full_reindex(
5501 d, stacked_indexes[d], fill_value, sparse
5502 )
5503 else:
5504 result = result._unstack_once(d, stacked_indexes[d], fill_value, sparse)
File ~/mambaforge/envs/icec/lib/python3.11/site-packages/xarray/core/dataset.py:5395, in Dataset._unstack_full_reindex(self, dim, index_and_vars, fill_value, sparse)
5393 if name not in index_vars:
5394 if dim in var.dims:
-> 5395 variables[name] = var.unstack({dim: new_dim_sizes})
5396 else:
5397 variables[name] = var
File ~/mambaforge/envs/icec/lib/python3.11/site-packages/xarray/core/variable.py:1930, in Variable.unstack(self, dimensions, **dimensions_kwargs)
1928 result = self
1929 for old_dim, dims in dimensions.items():
-> 1930 result = result._unstack_once_full(dims, old_dim)
1931 return result
File ~/mambaforge/envs/icec/lib/python3.11/site-packages/xarray/core/variable.py:1820, in Variable._unstack_once_full(self, dims, old_dim)
1817 reordered = self.transpose(*dim_order)
1819 new_shape = reordered.shape[: len(other_dims)] + new_dim_sizes
-> 1820 new_data = reordered.data.reshape(new_shape)
1821 new_dims = reordered.dims[: len(other_dims)] + new_dim_names
1823 return type(self)(
1824 new_dims, new_data, self._attrs, self._encoding, fastpath=True
1825 )
File ~/mambaforge/envs/icec/lib/python3.11/site-packages/dask/array/core.py:2219, in Array.reshape(self, merge_chunks, limit, *shape)
2217 if len(shape) == 1 and not isinstance(shape[0], Number):
2218 shape = shape[0]
-> 2219 return reshape(self, shape, merge_chunks=merge_chunks, limit=limit)
File ~/mambaforge/envs/icec/lib/python3.11/site-packages/dask/array/reshape.py:285, in reshape(x, shape, merge_chunks, limit)
283 else:
284 chunk_plan.append("auto")
--> 285 outchunks = normalize_chunks(
286 chunk_plan,
287 shape=shape,
288 limit=limit,
289 dtype=x.dtype,
290 previous_chunks=inchunks,
291 )
293 x2 = x.rechunk(inchunks)
295 # Construct graph
File ~/mambaforge/envs/icec/lib/python3.11/site-packages/dask/array/core.py:3095, in normalize_chunks(chunks, shape, limit, dtype, previous_chunks)
3092 chunks = tuple("auto" if isinstance(c, str) and c != "auto" else c for c in chunks)
3094 if any(c == "auto" for c in chunks):
-> 3095 chunks = auto_chunks(chunks, shape, limit, dtype, previous_chunks)
3097 if shape is not None:
3098 chunks = tuple(c if c not in {None, -1} else s for c, s in zip(chunks, shape))
File ~/mambaforge/envs/icec/lib/python3.11/site-packages/dask/array/core.py:3218, in auto_chunks(chunks, shape, limit, dtype, previous_chunks)
3212 largest_block = math.prod(
3213 cs if isinstance(cs, Number) else max(cs) for cs in chunks if cs != "auto"
3214 )
3216 if previous_chunks:
3217 # Base ideal ratio on the median chunk size of the previous chunks
-> 3218 result = {a: np.median(previous_chunks[a]) for a in autos}
3220 ideal_shape = []
3221 for i, s in enumerate(shape):
File ~/mambaforge/envs/icec/lib/python3.11/site-packages/dask/array/core.py:3218, in <dictcomp>(.0)
3212 largest_block = math.prod(
3213 cs if isinstance(cs, Number) else max(cs) for cs in chunks if cs != "auto"
3214 )
3216 if previous_chunks:
3217 # Base ideal ratio on the median chunk size of the previous chunks
-> 3218 result = {a: np.median(previous_chunks[a]) for a in autos}
3220 ideal_shape = []
3221 for i, s in enumerate(shape):
IndexError: tuple index out of rangeAnything else we need to know?
The most recent traceback entry point to an issue in dask code.
Environment
Details
INSTALLED VERSIONS ------------------ commit: None python: 3.11.6 | packaged by conda-forge | (main, Oct 3 2023, 10:40:35) [GCC 12.3.0] python-bits: 64 OS: Linux OS-release: 6.5.5-1-MANJARO machine: x86_64 processor: byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.14.2 libnetcdf: 4.9.2xarray: 2023.9.0
pandas: 2.1.1
numpy: 1.24.4
scipy: 1.11.3
netCDF4: 1.6.4
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: 2.16.1
cftime: 1.6.2
nc_time_axis: None
PseudoNetCDF: None
iris: None
bottleneck: 1.3.7
dask: 2023.9.3
distributed: 2023.9.3
matplotlib: 3.8.0
cartopy: 0.22.0
seaborn: None
numbagg: None
fsspec: 2023.9.2
cupy: None
pint: None
sparse: 0.14.0
flox: 0.7.2
numpy_groupies: 0.10.2
setuptools: 68.2.2
pip: 23.2.1
conda: None
pytest: None
mypy: None
IPython: 8.16.1
sphinx: None
Activity
max-sixty commentedon Oct 19, 2023
It does seem like we produce large chunks here. For those who know more than me: what should we be doing here — should we be taking control here? Or defer to the user / to dask?
Could you help me understand the relevance of the stack trace? Is that because of the large chunks? It seems like possibly it's a dask bug?
I'll change the title of the issue, if that's OK
[-]Issue #5754 revisited[/-][+]`.stack` produces large chunks[/+]yt87 commentedon Oct 19, 2023
When you run
dask will respond with a warning:
I followed the above advice. I suspect this is a dask issue, but I decided to post it here because it seems to be closely related to #5754.
Actually, the message originates with xarray, not dask.
max-sixty commentedon Oct 19, 2023
Yes I get the same thing. My question is whether xarray should be intervening here...
OK, that does seem like a dask bug — xarray is just doing a reshape. So I think we can separate that issue and push it upstream, unless I'm missing something (though will take some work to create a minimal dask example)
dcherian commentedon Oct 26, 2023
Thanks @yt87 I could reproduce with only dask (dask/dask#10603). So closing here.