Repository navigation
Implement task-based array shuffle - #11262
Conversation
|
I assume this is pretty much equivalent to |
Unit Test ResultsSee test report for an extended history of previous test failures. This is useful for diagnosing flaky tests. 15 files ± 0 15 suites ±0 3h 29m 59s ⏱️ + 2m 54s Results for commit 281ff9b. ± Comparison against base commit f95007b. ♻️ This comment has been updated with latest results. |
roughly, we can be smarter with respect to how many tasks we create, i.e. if you have a 6 x 6 array with 3 x 3 chunks and an indexer on axis 1 of
take would create a task per element, because they aren't in order. We can sort and then re-order to only slice once per chunk. Shuffle also ensures that a group of elements ends up in the same chunk. That said, worst case task graph size is roughly the same yes. |
|
A couple questions / thoughts (but I haven't thought about this as much, so feel free to disregard) Multi-stageLet's imagine a pathological case where we have two chunks and and indexer that switches back and forth between them Optimally we should be able to do this in around 6 tasks I think, assuming we're willing to do the take in a couple of stages, in worst case we'll do it in 200,000 tasks. What happens with this implementation? Also, it may not make sense to invest in the multi-stage implementation here for various reasons, I'm just making sure that we have that option on our radar. Lots of RechunkingIf we did the multi-stage thing, then suddenly we're strongly incentivized to rechunk ahead of time to make the take dimension very large. This could be useful for two reasons:
Just some thoughts. It could be that we've already been thinking about these or that they're bad for some reason. No need to anchor on them, just bringing them up in case they're useful. |
|
(I think the answer to my question is in @phofl's last comment, but I felt that my example here helped make the difference a bit more stark) |
We do this in 6 tasks at the moment. We sort the indexer and then resort after selecting the data. |
Oh grand! So I guess we're experiencing here a task blowup of shape/chunkshape for the take dimension, in this case that's 1500/50 == 30x. So 27k * 30 == ~800k, which tracks. This is a lot better than the number of chunk transitions in the indexer, which is what we had before. This seems great! |
Yes that's correct, that should be the upper bound. Just to align expectations: This is still pretty bad, less bad than before, but far away from actually being good |
|
Looks like the approach in #3901? |
|
Yep very similar approach. Task graph will be smaller but both leverage the same technique. This is not the state that I would like to end up in though. A more general question: Is this the kind of API could be used in Xarray for a group by-transform or group by-reduce if you don't want to do a tree reduction? Basically what we discussed here: pydata/xarray#9220 |
|
This should be ready for a closer look. @dcherian is this an API that would satisfy the XArray requirements for a groupby - transform approach? We can add the P2P backend in the background after we align on how we want to process things |
|
With #11267, take performance is a lot better: This is the largest array that still works with the current take implementation, everything larger just kills the cluster
|
| indexer: list[list[int]] | ||
| The indexer that determins which elements along the dimension will end up in the | ||
| same chunk. Multiple groups can be in the same chunk to avoid fragmentation, but | ||
| each group will end up in exactly one chunk. |
There was a problem hiding this comment.
This behaviour is problematic for
import dask.array
import xarray as xr
time = xr.date_range("2001-01-01", "2010-01-01", freq="D")
ds = xr.Dataset(
{
"data": (
("x", "y", "time"),
dask.array.random.random((10000, 1000, len(time)), chunks=(-1, -1, 1)),
)
},
coords={"time": ("time", time)},
)
ds.resample(time="Y")._group_indicesThere was a problem hiding this comment.
Can you say a bit more?
Is the problem that the chunks will become too large or that we fuse multiple groups together?
If the latter, then we could add control for this behavior to turn fusion of groups off.
If chunks that are too large:
Would your preferred alternative be to have one group in multiple chunks along the time dimension in your example or split the other dimensions into multiple chunks instead of -1 and -1?
There was a problem hiding this comment.
one group in multiple chunks along the time dimension in your
I feel like its preferrable to do this and then have the user do the rechunk-to-one-block-while-adjusting-others.
There was a problem hiding this comment.
Is the user in this case xarray or users from xarray?
When we put one group into multiple chunks, all chunks that have parts of the group should be in consecutive chunks, so that
x = x.rechunk(("x": "foo", "y": "bar", "time": 365})
would put all values of said group into a single chunk in your example?
There was a problem hiding this comment.
^ yes. Once the groups are in order, we can use slicing to extract each group and pass to the user.
Basically it'd be nice to preserve the cases where the map approach works nicely today.
array.resample(time="Y").reduce(np.mean)
Today this dispatches to dask.array.mean with each group split across multiple chunks, so we get the tree reduction.
There was a problem hiding this comment.
Yep ok, got it. This is what you would use take for after #11267 (what we discussed in the other issue). take uses the same algorithm under the hood but is not placing everything into a single group.
| chunks = (3, 2, 3) | ||
| with dask.config.set({"array.shuffle.chunksize-tolerance": 1}): | ||
| result = darr.shuffle(indexer, axis=1) | ||
| expected = arr[:, list(flatten(indexer))] |
There was a problem hiding this comment.
nit: I'd personally find this test more readable if we just replaced list(flatten(indexer)) with the corresponding literal values.
| assert result.chunks[1] == chunks | ||
|
|
||
|
|
||
| def test_shuffle_config_tolerance(arr, darr): |
There was a problem hiding this comment.
Should we test different tolerances?
| description: | | ||
| Tolerance for the shuffle algorithm when creating output chunks. | ||
| Default is 1.25. This means that the shuffle algorithm can exceed | ||
| the average input chunk size along this dimension by 25%. |
There was a problem hiding this comment.
Do we also impose a lower limit? When thinking about tolerances, I think about limits in both directions. If it's only an upper limit, it might make sense to make this explicit in the naming.
| ---------- | ||
| x: dask array | ||
| Array to be shuffled. | ||
| indexer: list[list[int]] |
There was a problem hiding this comment.
nit: It might be safer to convert the indexer to a tuple[tuple[int]] internally to avoid unintentional modification.
There was a problem hiding this comment.
I am messing with list methods later, do you want to protect against modifying the input?
There was a problem hiding this comment.
do you want to protect against modifying the input?
Yes, I was wondering if that would be helpful here.
There was a problem hiding this comment.
I would rather copy in this case, you ok with?
| this_slice = slices.copy() | ||
|
|
||
| # Cache the takers to allow de-duplication when serializing | ||
| # Ugly! |
There was a problem hiding this comment.
Hm I wanted to express that this is necessary but very ugly, so this is kind of intended
Co-authored-by: Hendrik Makait <[email protected]>
Co-authored-by: Hendrik Makait <[email protected]>
hendrikmakait
left a comment
There was a problem hiding this comment.
LGTM assuming CI is green. Thanks, @phofl
| ---------- | ||
| x: dask array | ||
| Array to be shuffled. | ||
| indexer: list[list[int]] |
| this_slice = slices.copy() | ||
|
|
||
| # Cache the takers to allow de-duplication when serializing | ||
| # Ugly! |
I think this gets part of the way there, yes. pydata/xarray#9220 (comment) |

pre-commit run --all-filesThis is a first rough implementation of an array-based shuffle that uses the task implementation. Not polished and probably has a few edge cases that aren't covered yet.
TLDR: We are preserving chunk sizes (roughly) compared to take but we still have a very large graph, which strains the scheduler pretty heavily. Subsequent results are easier to process though in case of a random access pattern because the chunks stay reasonable. An improvement over random access take generally speaking, but still not very good
Let's mess around with a toy example:
Our indexer has a random access pattern (and we access 5 elements at a time, but we could do random stuff here).
Shuffling keeps the number of chunks consistent. Doing the equivalent take blows up the number of chunks
The number of tasks are in the same ballpark though, the growth is still exponential in a case of an all to all pattern.
Generating close to a million tasks for a 27k chunks array is not great.