Skip to content

Expose intermediate rechunking logic da.reshape #6272

Closed

Description

@TomAugspurger

da.reshape does some clever tricks to avoid making too large intermediate chunks. Sometimes, however, the user can do better, especially given downstream operations.

In this example, we "read", reshape, and slice. The nature of the final slice happens to select data from just a single original chunk. But by default there's an intermediate rechunk-merge of two original chunks.

# a has 4 chunks
x = np.arange(2 * 2 * 5).reshape((2, 2, 5))
a = da.from_array(x, chunks=(1, 1, 5))
a_reshaped = a.reshape((4, 5))
a_reshaped[0].visualize(filename="default", optimize_graph=True)

default

If the user could specify the intermediate and output chunks, then they can get the desired chunking

a_reshaped = a.reshape((2*2, 5),
                       inchunks=((1, 1), (1, 1), (5,)),
                       outchunks=((1, 1, 1, 1), (5,)))
a_reshaped[0].visualize(filename="manual", optimize_graph=True)

manual

The change itself is pretty small

diff --git a/dask/array/core.py b/dask/array/core.py
index ef8970091..756b72bd5 100644
--- a/dask/array/core.py
+++ b/dask/array/core.py
@@ -1742,12 +1742,12 @@ class Array(DaskMethodsMixin):
         return choose(self, choices)
 
     @derived_from(np.ndarray)
-    def reshape(self, *shape):
+    def reshape(self, *shape, inchunks=None, outchunks=None):
         from .reshape import reshape
 
         if len(shape) == 1 and not isinstance(shape[0], Number):
             shape = shape[0]
-        return reshape(self, shape)
+        return reshape(self, shape, inchunks=inchunks, outchunks=outchunks)
 
     def topk(self, k, axis=-1, split_every=None):
         """The top k elements of an array.
diff --git a/dask/array/reshape.py b/dask/array/reshape.py
index 85b5ca33c..e00a0a191 100644
--- a/dask/array/reshape.py
+++ b/dask/array/reshape.py
@@ -133,7 +133,7 @@ def contract_tuple(chunks, factor):
     return tuple(out)
 
 
-def reshape(x, shape):
+def reshape(x, shape, inchunks=None, outchunks=None):
     """ Reshape array to new shape
 
     This is a parallelized version of the ``np.reshape`` function with the
@@ -145,6 +145,23 @@ def reshape(x, shape):
 
     .. _`row-major order`: https://en.wikipedia.org/wiki/Row-_and_column-major_order
 
+    Parameters
+    ----------
+    shape : tuple
+        The new shape should be compatible with the original shape and should satisfy
+        Dask's additional restrictions. If
+        an integer, then the result will be a 1-D array of that length.
+        One shape dimension can be -1. In this case, the value is
+        inferred from the length of the array and remaining dimensions.
+    inchunks : tuple, optional
+        The chunks to use for an intermediate re-chunked array. By default, the array
+        is rechunked to keep roughly the same chunk sizes.
+    outchunks : tuple, optional
+        The chunks to use for the output array.
+
+    Notes
+    -----
+
     When communication is necessary this algorithm depends on the logic within
     rechunk.  It endeavors to keep chunk sizes roughly the same when possible.
 
@@ -192,7 +209,12 @@ def reshape(x, shape):
         return Array(graph, name, chunks, meta=meta)
 
     # Logic for how to rechunk
-    inchunks, outchunks = reshape_rechunk(x.shape, shape, x.chunks)
+    if inchunks is None and outchunks is None:
+        inchunks2, outchunks2 = reshape_rechunk(x.shape, shape, x.chunks)
+        if inchunks is None:
+            inchunks = inchunks2
+        if outchunks is None:
+            outchunks = outchunks2
     x2 = x.rechunk(inchunks)
 
     # Construct graph

The primary argument against is perhaps that this perhaps something Dask should be doing automatically. I'm not sure how feasible detecting this optimization is though.

This is a workaround that might be solved properly by #5544.

Activity

  1. dewert99 commented on Jun 26, 2020

    @dewert99

    I would also find this useful.

  2. TomAugspurger commented on Jun 29, 2020

    @TomAugspurger
    MemberAuthor

    @dewert99 would you be interested in making a pull request, starting with that patch and including some tests?

  3. phofl commented on Aug 14, 2024

    @phofl
    Collaborator

    I think we can close this, recent improvements in rechunking avoid the large chunks issue and the other option was merged as well

    exposing the chunks directly is very problematic since this can easily conflict with the C ordering

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions