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
{{ message }}
Repository navigation
Converting Dask Array of unknown shape to known shape (at the cost of computing) #3293
Sometimes at the end of a series of Dask Array computations one has an array of unknown shape. However once the array is computed, it should be possible to know how big each chunk is and thus how big the array is as a whole. It would be nice at this stage to determine the array's shape and possibly even convert the Dask Array into one with known shape. This ends up being pretty important when allocating space for storage.
On Fri, Mar 16, 2018 at 4:52 PM, jakirkham ***@***.***> wrote:
Sometimes at the end of a series of Dask Array computations one has an
array of unknown shape. However once the array is computed, it should be
possible to know how big each chunk is and thus how big the array is as a
whole. It would be nice at this stage to determine the array's shape and
possibly even convert the Dask Array into one with known shape. This ends
up being pretty important when allocating space for storage.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#3293>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AASszPEphUvqLY7-fVowntePuqGa3SUdks5tfCYagaJpZM4Sua-j>
.
I would suggest implementing a setter for Array.chunks, which could then automatically recompute the shape.
TensorFlow supports something similar for making unknown dimension sizes known via the Tensor.set_shape() method. They don't support assignment to Tensor.shape because that might suggest NumPy's shape alternating semantics (but that wouldn't be an issue for us if we only support setting chunks).
So there is a method setting chunks, which currently raises. Looking at the history, this tracks back to commit ( 66929ed ) in PR ( #416 ). Not sure if anyone was actually abusing chunks by setting. Perhaps this is a good argument to use rechunk here as well despite it being a bit strange. 🤔 Otherwise I do like the explicit setting of chunks because this feels like different behavior than rechunk and is a bit more inline with what is actually happening.
Have occasionally thought it might be nice to tackle this in a non-blocking way. That said, it's not totally obvious to me how this would be done. If anyone has ideas, would be interested to hear.
Would add that in some cases it appears one needs to drop empty chunks from the array, which requires that the new array with those chunks dropped pick a new name and index the chunks in order. Not sure if there is a good utility function for this sort of thing, but it seems like a good idea.
Think the code above is as far as I got. Have gotten some use out of that though. Something like that in Dask should be adequate for my needs.
However my understanding from the discussion above is we have not reached a consensus about the API (naming). Personally don't have strong feelings beyond using a reasonably short function name that gets the idea across (though many things probably work ok here :). Is there something agreeable on that front?
I wanted to make sure I was reading this correctly. Assuming I know the shape of my data (say I have a CSV that has a strangely formatted header with the number of rows in it) and I read it with dask, there is no way to "tell" dask what the shape is without computing/loading all the data, right?
If you know how many rows are in each chunk then you can manually define the chunks attribute. This is more information than the number of total rows though.
So I know the number of rows and the number of columns. I am using dask.dataframe.read_csv. It sounds like you are saying I should be able to do df.chunks = (rows, cols) right? Or even taking the dask array with to_dask_array on a Series and doing df.chunks = or rechunk? I had tried these with no luck since either I can't assign chunks directly this way or the shape is NaN so it complains.
One challenge is that with the distributed scheduler persist finishes immediately and runs asynchronously in the background, so we don't actually have any new information at this point.
That's a good point. Maybe we persist could take an argument to block (e.g. .persist(wait=True), possibly with a timeout) in which case the computation of the chunksize metadata could be automatically enabled.
But I hesitate to do this a bit because the persist API is used for other collections and for the dask.persist function as well. The inconsistency might be a little surprising to users (though probably not that bad). I think that the two-liner is ok, and in many cases probably about the same speed.
Activity
mrocklin commentedon Mar 16, 2018
jakirkham commentedon Mar 16, 2018
That sounds reasonable to me. Do you have any thoughts/suggestions on an API for this?
mrocklin commentedon Mar 16, 2018
shoyer commentedon Mar 17, 2018
I would suggest implementing a setter for
Array.chunks, which could then automatically recompute the shape.TensorFlow supports something similar for making unknown dimension sizes known via the
Tensor.set_shape()method. They don't support assignment toTensor.shapebecause that might suggest NumPy's shape alternating semantics (but that wouldn't be an issue for us if we only support settingchunks).jakirkham commentedon Mar 17, 2018
So there is a method setting chunks, which currently raises. Looking at the history, this tracks back to commit ( 66929ed ) in PR ( #416 ). Not sure if anyone was actually abusing
chunksby setting. Perhaps this is a good argument to userechunkhere as well despite it being a bit strange. 🤔 Otherwise I do like the explicit setting of chunks because this feels like different behavior thanrechunkand is a bit more inline with what is actually happening.shoyer commentedon Mar 17, 2018
We could add some validation to ensure that chunks being assigned match the original structure, only replacing unknown with known sizes.
mrocklin commentedon Mar 17, 2018
My expectation of setting chunks is that it would trigger a reshape operation, not that it would figure out the current value of unknown chunk sizes.
mrocklin commentedon Mar 17, 2018
I'll throw out the option of adding a
.compute_chunk_sizes()method or something else suitably boring.jakirkham commentedon Mar 28, 2018
FWIW this currently works. So maybe that can inform how we might want to interact with it as well.
jakirkham commentedon Apr 2, 2018
Have occasionally thought it might be nice to tackle this in a non-blocking way. That said, it's not totally obvious to me how this would be done. If anyone has ideas, would be interested to hear.
jakirkham commentedon Apr 6, 2018
Would add that in some cases it appears one needs to drop empty chunks from the array, which requires that the new array with those chunks dropped pick a new name and index the chunks in order. Not sure if there is a good utility function for this sort of thing, but it seems like a good idea.
jakirkham commentedon Apr 19, 2018
Playing with this idea a little bit more have come to the following.
This let's one do the following.
There's probably room for improvement as this is a rough sketch. That said, this could be used to solve the task.
mrocklin commentedon Oct 24, 2018
Any update on this @jakirkham ?
jakirkham commentedon Oct 29, 2018
Think the code above is as far as I got. Have gotten some use out of that though. Something like that in Dask should be adequate for my needs.
However my understanding from the discussion above is we have not reached a consensus about the API (naming). Personally don't have strong feelings beyond using a reasonably short function name that gets the idea across (though many things probably work ok here :). Is there something agreeable on that front?
djhoese commentedon Feb 7, 2019
I wanted to make sure I was reading this correctly. Assuming I know the shape of my data (say I have a CSV that has a strangely formatted header with the number of rows in it) and I read it with dask, there is no way to "tell" dask what the shape is without computing/loading all the data, right?
mrocklin commentedon Feb 7, 2019
If you know how many rows are in each chunk then you can manually define the chunks attribute. This is more information than the number of total rows though.
djhoese commentedon Feb 7, 2019
So I know the number of rows and the number of columns. I am using
dask.dataframe.read_csv. It sounds like you are saying I should be able to dodf.chunks = (rows, cols)right? Or even taking the dask array withto_dask_arrayon a Series and doingdf.chunks =orrechunk? I had tried these with no luck since either I can't assign chunks directly this way or the shape is NaN so it complains.mrocklin commentedon Feb 7, 2019
There is no way to assign length to a dataframe
You can define
chunkson a dask array, but you'll need to define it for every single chunk of the array. This is the full tuple-of-tuples form. (See http://docs.dask.org/en/latest/array-chunks.html)djhoese commentedon Feb 7, 2019
I got it to work. I had to pass
lengthsto to_dask_array which I didn't realize existed. Thanks.mrocklin commentedon Mar 6, 2019
@ogrisel asked about this here: https://stackoverflow.com/questions/54928233/how-to-update-the-shape-chunks-and-chunksize-metadata-of-a-dask-array-with-nan/55027590#55027590
jakirkham commentedon Mar 7, 2019
Should we put up a few options for the method name and vote? What’s the right way to proceed? It looks like the technical part is already solved.
mrocklin commentedon Mar 7, 2019
Seems sensible to me
ogrisel commentedon Mar 20, 2019
By the way, why wouldn't a call to
.persist()automatically compute the chunksize metadata?mrocklin commentedon Mar 20, 2019
That's an interesting idea.
One challenge is that with the distributed scheduler
persistfinishes immediately and runs asynchronously in the background, so we don't actually have any new information at this point.ogrisel commentedon Mar 21, 2019
That's a good point. Maybe we persist could take an argument to block (e.g.
.persist(wait=True), possibly with a timeout) in which case the computation of the chunksize metadata could be automatically enabled.mrocklin commentedon Mar 21, 2019
With the proposal above this would be a two-liner
We could add a flag into the persist method
But I hesitate to do this a bit because the
persistAPI is used for other collections and for thedask.persistfunction as well. The inconsistency might be a little surprising to users (though probably not that bad). I think that the two-liner is ok, and in many cases probably about the same speed.TomAugspurger commentedon Sep 10, 2019
This was fixed by #5312