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
Flattening Dask Arrays without spreading out chunks #4855
Currently functions like ravel and flatten follow the same traversal that one would expect when using NumPy. This makes sense for users coming from NumPy, who would expect this. Also there are some Dask Array functions that rely on this behavior to match their NumPy counterparts. However this means that ravel and flatten must spread out the chunks (rechunk the array), which can have a non-negligible performance cost associated.
That said, not all cases rely on matching the same order that NumPy would provide when flattening out an array. For cases like this, a nice alternative would be to flatten chunks themselves and merely stitch together the flattened chunks into a new 1-D array. This strategy would require no rechunking and would be embarrassingly parallel. Thus it would avoid the performance penalties that ravel and flatten have today.
Assuming this strategy is reasonable, there are a few ways we could go about implementing it.
Allow some additional options to the order parameter to handle this need.
Add a new parameter to toggle NumPy or chunk-based traversal strategies.
Include a config option to enable this behavior for Dask Arrays more generally.
Just to provide more context for item 3 (config option), this could be useful when the user does not have direct access to ravel/flatten, but still wants this behavior to be used under the hood. For example, argwhere flattens the array and nonzero also does so indirectly.
I would much rather thread a new argument through argwhere/nonzero in dask than add a configuration option. Global configuration options lead to spooky "action at a distance" that can be very hard to debug, e.g., if unexpected operations also happen to indirectly use flattening.
FWIW here is the code I hacked together to do this myself. Not to say it should be implemented exactly like this, but hopefully it is useful when this actually gets implemented somewhere.
Sorry for joining the discussion late, but while I agree with @shoyer's point on global configurations, I'm also growing more concerned about non-NumPy API arguments. If someone wants to use Dask today calling np.ravel(dask_array, non_numpy_arg=some_value), non_numpy_arg would have to be removed for a non-Dask array in the future.
Actually, I can't think of any good solutions that won't introduce issues on one way or another. Option 1 would probably introduce the same problem as I've mentioned before. Option 4 doesn't do that, but on the other hand, it would introduce another function, that wouldn't work with __array_function__. I think option 4 would at least be more explicit to users, since they won't need to check which library introduces some non-NumPy argument, but not necessarily a better solution than the others.
How often do such cases happen? If not very often, I would prefer, by order of highest preference, solutions 4 or 3.
The major point I want to make is that if this is implemented, an explanation of it should also go in the 'best practices for dask arrays' docs - I bet there are users out there who'd be grateful to know about the performance implications.
I'd also vote no for option 3 (I agree with @shoyer on this, he explains the potential pitfalls well upthread).
I'd want to prioritise an API that is compatible with numpy as far as possible. Ideally it would be relatively easy for users to take code they wrote for dask arrays and re-use it for numpy arrays without it breaking. In cases where that's not possible, maybe it's best to try and make things obviously non-compatible with numpy? Would that help narrow down the remaining options?
My thinking around point 2 is very fuzzy, so I'd welcome anyone else expanding on these points or arguing against them.
2. I'd want to prioritise an API that is compatible with numpy as far as possible. Ideally it would be relatively easy for users to take code they wrote for dask arrays and re-use it for numpy arrays without it breaking. In cases where that's not possible, maybe it's best to try and make things obviously non-compatible with numpy? Would that help narrow down the remaining options?
I strongly agree with this. Given the negative impact of option 3, that only leaves us with option 4, which wouldn't meddle with NumPy API at all.
Am I correct in the assessment that there hasn't been any work done on this yet, but the decision is to pursue option 4 following the pattern described in #4855 (comment) ? If you aren't planning on working on this @jakirkham then maybe it'd be a good issue for a new-comer.
Activity
jakirkham commentedon May 29, 2019
cc @shoyer @pentschev @GenevieveBuckley (to get a few different perspectives on this)
shoyer commentedon May 29, 2019
Yes, this would be useful. I think either a new option in the
orderparameter or a new parameter to these functions/method would work.jakirkham commentedon May 29, 2019
Just to provide more context for item 3 (config option), this could be useful when the user does not have direct access to
ravel/flatten, but still wants this behavior to be used under the hood. For example,argwhereflattens the array andnonzeroalso does so indirectly.shoyer commentedon May 29, 2019
I would much rather thread a new argument through argwhere/nonzero in dask than add a configuration option. Global configuration options lead to spooky "action at a distance" that can be very hard to debug, e.g., if unexpected operations also happen to indirectly use flattening.
jakirkham commentedon May 29, 2019
Good point. Just wanted to make sure it made sense why that was in the list. 🙂
jakirkham commentedon May 29, 2019
FWIW here is the code I hacked together to do this myself. Not to say it should be implemented exactly like this, but hopefully it is useful when this actually gets implemented somewhere.
pentschev commentedon May 29, 2019
Sorry for joining the discussion late, but while I agree with @shoyer's point on global configurations, I'm also growing more concerned about non-NumPy API arguments. If someone wants to use Dask today calling
np.ravel(dask_array, non_numpy_arg=some_value),non_numpy_argwould have to be removed for a non-Dask array in the future.jakirkham commentedon May 29, 2019
Does this mean you would prefer option 1 or 4?
pentschev commentedon May 29, 2019
Actually, I can't think of any good solutions that won't introduce issues on one way or another. Option 1 would probably introduce the same problem as I've mentioned before. Option 4 doesn't do that, but on the other hand, it would introduce another function, that wouldn't work with
__array_function__. I think option 4 would at least be more explicit to users, since they won't need to check which library introduces some non-NumPy argument, but not necessarily a better solution than the others.How often do such cases happen? If not very often, I would prefer, by order of highest preference, solutions 4 or 3.
shoyer commentedon May 29, 2019
I have no strong preference between options (1), (2) and (4).
GenevieveBuckley commentedon May 30, 2019
The major point I want to make is that if this is implemented, an explanation of it should also go in the 'best practices for dask arrays' docs - I bet there are users out there who'd be grateful to know about the performance implications.
GenevieveBuckley commentedon May 30, 2019
For the question you actually asked:
My thinking around point 2 is very fuzzy, so I'd welcome anyone else expanding on these points or arguing against them.
pentschev commentedon May 30, 2019
I strongly agree with this. Given the negative impact of option 3, that only leaves us with option 4, which wouldn't meddle with NumPy API at all.
That said, my vote remains for option 4.
jakirkham commentedon May 31, 2019
Sounds like the consensus is 4, correct? Please either +1 or -1 this comment if that is either correct or incorrect.
jsignell commentedon Apr 30, 2020
Am I correct in the assessment that there hasn't been any work done on this yet, but the decision is to pursue option 4 following the pattern described in #4855 (comment) ? If you aren't planning on working on this @jakirkham then maybe it'd be a good issue for a new-comer.
GenevieveBuckley commentedon Mar 21, 2021
@dask/triage can we add a "good first issue" label here?
We have some Summer of Code potential applicants who are looking for more labelled issues right now.
krokosik commentedon Mar 1, 2023
@jakirkham Is it okay if I pick up this issue?
cmarmo commentedon Mar 3, 2023
Sorry @wkrasnicki ! I didn't see your comment here! I was working on it and I didn't check the issue before submitting the PR.
krokosik commentedon Mar 3, 2023
@cmarmo No problem and well done 🙂