You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to reshape a 4D dask array in such a way that I expect should preserve the original chunk structure. I am finding that reshape is instead rechunking my array in a non-optimal way.
Rather than seeing a neat stack of 8 chunks as I expected, the chunks have been fused.
This causes big problems for me when I am trying to do some processing of very large arrays. I can't afford to have the chunks fused, or else I will run out of memory.
I think your intuition that the intermediate rechunk-merge and split / reshape can be avoided is correct. The trick is probably to figure out when the output chunks align with the input chunks.
If no one beats me to it, I can take a look next week.
My guess is that the implicit C-ordering of data might make the intermediate reshaping actually necessary in some cases, even if we don't care in this case.
I have a similar problem, but perhaps even simpler. I just want to reshape an array with chunk sizes of 1 along one dimension. I would like the reshaped array to have chunksize (1,1).
In [31]: a = da.ones((10), chunks=1)
In [32]: a
Out[32]: dask.array<ones, shape=(10,), dtype=float64, chunksize=(1,), chunktype=numpy.ndarray>
In [33]: a.reshape((2, 5))
Out[33]: dask.array<reshape, shape=(2, 5), dtype=float64, chunksize=(1, 5), chunktype=numpy.ndarray>
This is important to me because I want xarrayunstack to preserve chunking in the trivial case where the chunk size is 1.
@nbren12 to confirm, your ideal chunking in the output of a.reshape((2, 5)) is ((1, 1), (1, 1, 1, 1, 1)). Essentially, a.reshape((2, 5)).rechunk((1, 1)), without the intermediate merged ``(1, 1), (5,))` chunks? One concern is that this will lead to larger task graphs since you'll have more chunks and tasks. Since not-merging isn't obviously better, we'd need a parameter or config setting to support this.
@TomAugspurger That is correct. I have been running out of memory due to having too big chunks, and was trying to very carefully ensure that "chunks in= chunks_out".
Just to provide some context, my workflow involves open a list of urls as dask delayed objects which are wrapped with xarray. The url string is parsed into dimension information, which I can represent as a stacked "xarray" dimension. I want to ensure that concatenated_url_datasets.unstack('urls') does not alter the chunking strategy.
I just ran into this, as well, using chunksize=1 dimensions and seeing my graphs get complex. In the case of chunksize=1 dimensions, there is the possibility of a shortcut implementation. It seems pretty thorny to optimally get this optimal for all cases.
I agree that that looks pretty good. It seems the method you use here is to find the smallest chunk along a dimension and rechunk that dimension to that chunksize.
It seems the method you use here is to find the smallest chunk along a dimension and rechunk that dimension to that chunksize.
I think that's right, if you want to minimize communication between chunks. It does result in more chunks / tasks, but that might be a good tradeoff in certain cases.
the method you use here is to find the smallest chunk along a dimension and rechunk that dimension to that chunksize.
Could this algorithm be implemented and controlled using a kwarg like arr.reshape(16, strategy="preserve-chunks")? It would be friendlier and a lot easier than asking the user to specify inchunks and outchunks. The existing behaviour could be strategy="minimize-tasks"
If 1 can be solved then I think the following code isn't too bad. It allows easy opt-in to one of two strategies (default vs "preserve-chunks") and is flexible enough to allow motivated users to have full control (this might even lead to discovery of new strategies?)
I'm running into the same issue. Being able to preserve the original chunk size as proposed by @dcherian would solve my problem.
After reading the discussion above I was wondering about the following: Is there a difference in performance when rechunking small blocks by combining them, and rechunking large blocks by splitting them? In the first case the strategy of preserving the chunks might be more flexible then you think because you would still be able to rechunk after the reshape without much overhead.
I was looking into this again last week, to implement the strategy keyword. I'm hoping to have something soon.
I don't think that "just maintain the original chunksize" will work. There are cases like #5544 (comment) where we need to rechunk the input to smaller chunks.
After reading the discussion above I was wondering about the following: Is there a difference in performance when rechunking small blocks by combining them, and rechunking large blocks by splitting them?
I'm not sure offhand, but all else equal the more tasks you have, the slower it'll be. In this case we're trading (some) additional tasks for less communication in the hope that it's faster. But we'll want to avoid doing unnecessary rechunking.
At a minimum though, I think we'll have a requirement that the fastest-changing dimension (the last with order="C") have the largest input chunks, and the slowest-changing dimensions (the first) have the smallest.
I've identified one special case: When reshaping from a larger to smaller number of dimensions (e.g. 3d -> 2d), we're able to avoid rechunking entirely *when the "early" axes all have size-1 chunks. For example
Because of the "all low axes have chunksize 1" property, we avoid needing to rechunk the input and we're merely moving blocks around. So I think that for this special case, not rechunking is the strictly superior strategy.
For cases like @chrisroat's in #5544 (comment), we need to rechunk the inputs (since the "early" axes aren't all chunksize 1) and so the strategy avoiding rechunk-merge isn't necessarily better. I think it'll depend on the overhead of scheduling additional tasks, the cost of moving data around, maximum memory usage, ...
I've identified one special case: When reshaping from a larger to smaller number of dimensions (e.g. 3d -> 2d), we're able to avoid rechunking entirely *when the "early" axes all have size-1 chunks.
That's great. This seems to align with my use case.
Unless I'm mistaken, then #5544 (comment) indicates the kind of API / implementation we'd need to solve @chrisroat's problem in #5544 (comment). To do a "zero-communication" / "no-merge" reshape, we need all the early axes to have a chunksize of 1. So that's necessary and sufficient for this optimization to kick in. That also covers @rabernat's original use-case.
So if I'm right that having a chunksize of 1 is necessary, then we just need a keyword in reshape saying "I want to rechunk to a chunksize of 1 for all my dimensions being collapsed". That would make the following two equivalent (with #6748, which disables merging for this special case).
Where strategy is something like strategy="minimize_tasks" (the default) vs. "minimize_memory" (rechunk to chunksize=1) or "minimize_communication".
Or we could have a boolean flag like merge_chunks=True / False. I think I like merge_chunks, but we might not be able to completely disable merging chunks (I haven't looked at the reshape from lower to higher dim case yet).
Activity
TomAugspurger commentedon Oct 30, 2019
I think your intuition that the intermediate rechunk-merge and split / reshape can be avoided is correct. The trick is probably to figure out when the output chunks align with the input chunks.
If no one beats me to it, I can take a look next week.
mrocklin commentedon Oct 31, 2019
I'm curious, if the order of the dimensions is flipped then does this problem persist? I.e. (5, 20, 20) -> (5, 400)?
mrocklin commentedon Oct 31, 2019
My guess is that the implicit C-ordering of data might make the intermediate reshaping actually necessary in some cases, even if we don't care in this case.
rabernat commentedon Oct 31, 2019
It's the same. Same number of tasks and graph structure.
nbren12 commentedon Mar 3, 2020
I have a similar problem, but perhaps even simpler. I just want to reshape an array with chunk sizes of 1 along one dimension. I would like the reshaped array to have chunksize (1,1).
This is important to me because I want
xarrayunstackto preserve chunking in the trivial case where the chunk size is 1.mrocklin commentedon Mar 3, 2020
cc @TomAugspurger in case he's interested in this from the Anaconda/Pangeo angle
TomAugspurger commentedon Mar 3, 2020
@nbren12 to confirm, your ideal chunking in the output of
a.reshape((2, 5))is((1, 1), (1, 1, 1, 1, 1)). Essentially,a.reshape((2, 5)).rechunk((1, 1)), without the intermediate merged ``(1, 1), (5,))` chunks? One concern is that this will lead to larger task graphs since you'll have more chunks and tasks. Since not-merging isn't obviously better, we'd need a parameter or config setting to support this.nbren12 commentedon Mar 3, 2020
@TomAugspurger That is correct. I have been running out of memory due to having too big chunks, and was trying to very carefully ensure that "chunks in= chunks_out".
nbren12 commentedon Mar 3, 2020
Just to provide some context, my workflow involves open a list of urls as dask delayed objects which are wrapped with xarray. The url string is parsed into dimension information, which I can represent as a stacked "xarray" dimension. I want to ensure that
concatenated_url_datasets.unstack('urls')does not alter the chunking strategy.chrisroat commentedon Sep 1, 2020
I just ran into this, as well, using chunksize=1 dimensions and seeing my graphs get complex. In the case of chunksize=1 dimensions, there is the possibility of a shortcut implementation. It seems pretty thorny to optimally get this optimal for all cases.
A less trivial case could be demonstrated with:
In this case, the individual chunks are not contiguous and cannot be simply reshaped and stuck end-to-end.
That said, the above case gets rechunked in
arr.reshape(16)as:which I find odd.
Here's an example of a fastpath implementation for chunksize=1:
https://github.com/deisseroth-lab/dask/commit/39d56b91a016edbcbf01b281da2ba433915ee23a
TomAugspurger commentedon Sep 2, 2020
@nbren12 I think your use-case would be solved by something like #6272. I'll see if I can turn that into a PR today.
@chrisroat what's your desired output chunking for
arr.reshape(16)? Maybe something likeThat would be a "zero-communication" reshape / rechunk IIUC.
TomAugspurger commentedon Sep 2, 2020
@chrisroat I think this is what you would need for your problem
Figuring out the right inchunks / outchunks isn't the easiest, but I'm not sure how much we can simplify the API while giving total control.
chrisroat commentedon Sep 3, 2020
I agree that that looks pretty good. It seems the method you use here is to find the smallest chunk along a dimension and rechunk that dimension to that chunksize.
TomAugspurger commentedon Sep 3, 2020
I think that's right, if you want to minimize communication between chunks. It does result in more chunks / tasks, but that might be a good tradeoff in certain cases.
dcherian commentedon Sep 3, 2020
Could this algorithm be implemented and controlled using a kwarg like
arr.reshape(16, strategy="preserve-chunks")? It would be friendlier and a lot easier than asking the user to specifyinchunksandoutchunks. The existing behaviour could bestrategy="minimize-tasks"TomAugspurger commentedon Sep 3, 2020
I was considering that. My concerns were
So I think that if we think people won't really need complete flexibility in the chunk structure then the
strategykeyword would be best.dcherian commentedon Sep 3, 2020
If 1 can be solved then I think the following code isn't too bad. It allows easy opt-in to one of two strategies (default vs "preserve-chunks") and is flexible enough to allow motivated users to have full control (this might even lead to discovery of new strategies?)
peterroelants commentedon Oct 19, 2020
I'm running into the same issue. Being able to preserve the original chunk size as proposed by @dcherian would solve my problem.
After reading the discussion above I was wondering about the following: Is there a difference in performance when rechunking small blocks by combining them, and rechunking large blocks by splitting them? In the first case the strategy of preserving the chunks might be more flexible then you think because you would still be able to rechunk after the reshape without much overhead.
TomAugspurger commentedon Oct 19, 2020
I was looking into this again last week, to implement the
strategykeyword. I'm hoping to have something soon.I don't think that "just maintain the original chunksize" will work. There are cases like #5544 (comment) where we need to rechunk the input to smaller chunks.
I'm not sure offhand, but all else equal the more tasks you have, the slower it'll be. In this case we're trading (some) additional tasks for less communication in the hope that it's faster. But we'll want to avoid doing unnecessary rechunking.
At a minimum though, I think we'll have a requirement that the fastest-changing dimension (the last with
order="C") have the largest input chunks, and the slowest-changing dimensions (the first) have the smallest.TomAugspurger commentedon Oct 19, 2020
I've identified one special case: When reshaping from a larger to smaller number of dimensions (e.g.
3d -> 2d), we're able to avoid rechunking entirely *when the "early" axes all have size-1 chunks. For exampleBecause of the "all low axes have chunksize 1" property, we avoid needing to rechunk the input and we're merely moving blocks around. So I think that for this special case, not rechunking is the strictly superior strategy.
For cases like @chrisroat's in #5544 (comment), we need to rechunk the inputs (since the "early" axes aren't all chunksize 1) and so the strategy avoiding rechunk-merge isn't necessarily better. I think it'll depend on the overhead of scheduling additional tasks, the cost of moving data around, maximum memory usage, ...
Avoid rechunking in reshape with chunksize=1
Avoid rechunking in reshape with chunksize=1
peterroelants commentedon Oct 19, 2020
That's great. This seems to align with my use case.
TomAugspurger commentedon Oct 19, 2020
Good to hear.
Unless I'm mistaken, then #5544 (comment) indicates the kind of API / implementation we'd need to solve @chrisroat's problem in #5544 (comment). To do a "zero-communication" / "no-merge" reshape, we need all the early axes to have a chunksize of 1. So that's necessary and sufficient for this optimization to kick in. That also covers @rabernat's original use-case.
So if I'm right that having a chunksize of 1 is necessary, then we just need a keyword in
reshapesaying "I want to rechunk to a chunksize of 1 for all my dimensions being collapsed". That would make the following two equivalent (with #6748, which disables merging for this special case).Where
strategyis something likestrategy="minimize_tasks"(the default) vs."minimize_memory"(rechunk to chunksize=1) or"minimize_communication".Or we could have a boolean flag like
merge_chunks=True / False. I think I likemerge_chunks, but we might not be able to completely disable merging chunks (I haven't looked at the reshape from lower to higher dim case yet).Add option to control rechunking in reshape (#6753)
Add option to control rechunking in reshape (dask#6753)
rabernat commentedon Aug 12, 2024
Love it when a 5-year-old issue gets closed! 💪