Skip to content

Bug with automatic rechunking on reshape #10603

Closed

Description

@dcherian

From pydata/xarray#8345

import dask.array
array = dask.array.ones(shape=(2, 1000, 100, 4800), chunks=(2, 1000, 100, 83))
with dask.config.set({'array.slicing.split_large_chunks': True}):
    array = array.reshape(2, 1000, 100, 4, 1200)
array
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Cell In[16], line 3
      1 array = dask.array.ones(shape=(2, 1000, 100, 4800), chunks=(2, 1000, 100, 83))
      2 with dask.config.set({'array.slicing.split_large_chunks': True}):
----> 3     array = array.reshape(2, 1000, 100, 4, 1200)
      4 array

File ~/miniforge3/envs/xarray-release/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 ~/miniforge3/envs/xarray-release/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 ~/miniforge3/envs/xarray-release/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 ~/miniforge3/envs/xarray-release/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 ~/miniforge3/envs/xarray-release/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 range

Activity

  1. added
    needs triage
    on Oct 26, 2023
  2. mrocklin commented on Oct 26, 2023

    @mrocklin
    Member

    I'm not personally familiar with this code. Git blame points to @TomAugspurger . Tom, can you help out here?

  3. TomAugspurger commented on Oct 28, 2023

    @TomAugspurger
    Member

    I'm not too sure either.

    I took a quick look and it appears that auto_chunks implicitly requires that the length of chunks and previous_chunks matches at

    result = {a: np.median(previous_chunks[a]) for a in autos}
    .

    In this case, we're using it as part of a reshape operation that is changing the number of dimensions, so the number of input chunks differs from the number of output chunks.

    I'd hoped we could work around it by inserting a dummy dimension at the end, but I didn't have any luck with that. We might need to update auto_chunks to handle the case where the number of dimensions is changing.

  4. dcherian commented on Oct 29, 2023

    @dcherian
    CollaboratorAuthor

    Could just skip it, that's better than failing.

  5. added
    needs attention
    on Dec 4, 2023
  6. schlunma commented on Feb 27, 2024

    @schlunma
    Contributor

    I just stumbled across this problem as well. Is there any workaround for this (except for omitting limit=... in reshape, which creates large chunks)?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    needs attentionneeds triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions