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
Automatically rechunk blockwise when chunk size increases substantially #7124
Currently blockwise blindly performs an outer product of chunks when doing outer products. This can result in very large chunks, which break computation.
importdask.arrayasdax=da.ones((1000000, 1))
y=da.ones((1, 1000000))
z=x.dot(y)
z
In these situations we should probably take a look at the output chunk sizes, and consider rechunking preemptively.
(we probably wanted to do this)
z=x.rechunk((2000, 1)).dot(y.rechunk((1, 2000)))
Where the value of 2000 was determined by looking at dask.config.get("array.chunksize")
It would be nice to do this in a way that didn't require going through all n x m chunk outputs (or greater if there we're doing outer products with more axes), but instead in a way that only required going through n + m values.
Sounds like a good idea but tricky to get right. On thought that crossed my mind is that da.blockwise can already do rechunking if the input arrays don't match (it calls unify_chunks); if one adds more rechunking logic into blockwise it would be good to harmonize it with that, so that one doesn't end up with silly situations where an array gets rechunked once to align chunk sizes and then is immediately rechunked differently to control memory usage.
Unexpected changes in chunk sizes can create issues with chunk alignment with other arrays. See #6646 for an example of this. I wonder if there's a way to make sure consistent chunking is maintained across multiple blockwise calls.
Perhaps the presence of "array.chunksize" can turn this behaviour on and off?
On thought that crossed my mind is that da.blockwise can already do rechunking if the input arrays don't match (it calls unify_chunks); if one adds more rechunking logic into blockwise it would be good to harmonize it with that
Good point. Maybe this logic actually belongs in unify_chunks, somehow rolling in the auto_chunks capability into that logic.
Getting chunking right is, as you both suggest, both very important and very complex. So far we've been able to represent a surprising amount of logic (blockwise, rechunk, unify_chunks, "auto" sizing, ...) in a way that is composable. I am hopeful that there is a way to combine this logic in a sensible way, but that probably requires deep thought from someone.
I appreciate the concern about surprising results. I agree that it should be able to turn automatic rechunking off. I do think that the default should be "on" though. A decent fraction of performance issues in dask array can be solved with better chunking.
Unexpected changes in chunk sizes can create issues with chunk alignment with other arrays.
It can also cause issues with even a single array. I have code that depends on having only a single chunk along a certain dimension (because of the access pattern of the per-block function). I ensure that by rechunking if necessary before calling da.blockwise, but if blockwise rechunks under me it will break things.
Here's an idea for semantics that will be backwards-compatible but still allow automatic rechunking for common usage: add an optional argument to blockwise to allow auto-rechunking, with three possible values e.g. True, False, None, with None being the default. True means do rechunking automatically. None means don't do anything, but warn the user if it creates big chunks. False means don't do anything and don't warn. Where blockwise is used internally by dask and it's known to be safe (e.g. in da.outer), pass True.
I think it is important to remember that blockwise doesn't prescribe what the function it is applying can do. From personal experience, I can say that having chunking changed surprisingly can break things.
I think that the root cause is that not all functions can "commute" with a rechunking operation. That is, if a chunk is a specific size for a reason (due to a particular access pattern, or because the function requires the points in that chunk to be processed simultaneously), rechunking is not an option.
I think @bmerry's suggestion is sound - warn by default, with the option to enable or disable rechunking. From a practical perspective, I can imagine that having rechunking turned on by default will be really confusing for new users.
I can imagine that having rechunking turned on by default will be really confusing for new users.
To be fair, new users are unlikely to be calling blockwise directly, but dask will be calling it under the hood for them, and in those cases it's generally safe to do rechunking.
In general, my feeling is that if a dask function directly exposes the chunking to the user (such as blockwise, map_blocks) it should (by default) respect the input chunking. If the semantics of a function are independent of the chunking (e.g. element-wise operators, outer, sum, slicing) then the chunking isn't part of the contract and it can legally rechunk if appropriate (although as #6646 revealed, that could still cause performance issues, so shouldn't be done lightly).
It's just occurred to me that da.blockwise already has a "don't-mess-with-chunking" flag, namely align_arrays=False. It's not documented, but it's used by map_blocks. When align_arrays is True (the default), blockwise can already affect the chunking, although I think it won't currently affect chunking on dimensions where all the inputs agree. So perhaps rather than adding a new flag as I suggested, we should just document the existing flag and note that users who care about the incoming chunking should manually align their inputs and pass align_arrays=False to ensure that it isn't altered. In that case I'd feel slightly more comfortable with automatically splitting large chunks.
It's just occurred to me that da.blockwise already has a "don't-mess-with-chunking" flag, namely align_arrays=False. It's not documented, but it's used by map_blocks. When align_arrays is True (the default), blockwise can already affect the chunking, although I think it won't currently affect chunking on dimensions where all the inputs agree.
I don't believe this is quite accurate. I've always understood align_arrays to be an option that allows blockwise to match arrays with incompatible chunk sizes (but the same number of chunks along the dimension).
assertx.chunks== ((5, 5, 5, 5),)
asserty.chunks== ((2, 1, 3, 4),)
assertx.numblocks==y.numblocks# Fails because chunk sizes differz=da.blockwise(fn, "d", x, "d", y, "d", align_arrays=True)
# Succeeds, because numblocks are the same along each dim# explicitly choose y's chunk sizes for the outputz=da.blockwise(fn, "d", x, "d", y, "d", align_arrays=False,
adjust_chunks={"d", y.chunks[0]})
Terminology:
We didn't use these particular phrases in my maths classes, but here's what I think they're being used to mean in these discussions
inner dimensions <- are summed over by tensordot
outer dimensions <- not summed over by tensordot
For tensordot, it looks like the process should go something like this:
Work out which are the outer dimension axes for arrays a and b
Use the auto_chunks function to work out what the best chunk size is for the tensordot array output, given the chunk size limit from dask.config.get('array.chunk-size')
Work backwards from this to figure out how the input tensordot arrays should be rechunked for the outer dimension axes.
Inner dimension axes - do we (a) make no change to the chunking here, (b) rechunk all inner dimensions to a chunksize of 1, or (c) rechunk inner dimensions to some smaller chunksize but ?? what size they should be.
Rechunk the tensordot input arrays
Set kwarg align_arrays=False when tensordot hands over to blockwise (might need to double check if there is any extra logic in the unify_chunks function we now need to take care of in tensordot?)
I've written some code for this, while I was trying to work out the process. I'll need a bit more clarity around the questions in point 4 and 6 above. (Hacky code here)
It is clear that the rechunking logic is going to have to happen in tensordot before it gets to blockwise, blockwise just doesn't know enough information to do what we want.
Most of my comments below are relevant for #2225 rather than this issue (I think that that issue is a bigger deal than this one). It's worth noting that these are two different concerns and not necessarily fully compatible.
First, I highly recommend multiplying two matrices two-dimensional matrices together and experimenting with chunking structure before trying to generalize any of this. I also recommend using the dask.distributed scheduler and having the dashboard up. The chunking structure will make a very large impact on if the computation finishes or not. I think that you probably need to feel this pain viscerally a bit before these issues make much sense.
I would also be totally ok with a first pass that only worked in the 2d x 2d case. This is the common case, probably much easier to get right, and much less likely to stall out with a half-finished PR.
I think that if you build up a small set of interesting examples that you'll be able to try out different approaches, like what you propose above, and see how they work. I think that this will quickly give you more intuition than anyone here has. The approach that you lay out above sounds sensible to me, but I would bet large amounts of money that it's wrong. I don't think that anyone today knows enough to be able to tell if any approach is solid or not. I'm certainly not able to do so.
Activity
mrocklin commentedon Jan 29, 2021
cc @dask/array this issue may interest you
bmerry commentedon Jan 30, 2021
Sounds like a good idea but tricky to get right. On thought that crossed my mind is that
da.blockwisecan already do rechunking if the input arrays don't match (it callsunify_chunks); if one adds more rechunking logic into blockwise it would be good to harmonize it with that, so that one doesn't end up with silly situations where an array gets rechunked once to align chunk sizes and then is immediately rechunked differently to control memory usage.sjperkins commentedon Feb 3, 2021
Unexpected changes in chunk sizes can create issues with chunk alignment with other arrays. See #6646 for an example of this. I wonder if there's a way to make sure consistent chunking is maintained across multiple blockwise calls.
Perhaps the presence of "array.chunksize" can turn this behaviour on and off?
/cc @JSKenyon
mrocklin commentedon Feb 3, 2021
Good point. Maybe this logic actually belongs in
unify_chunks, somehow rolling in theauto_chunkscapability into that logic.Getting chunking right is, as you both suggest, both very important and very complex. So far we've been able to represent a surprising amount of logic (blockwise, rechunk, unify_chunks, "auto" sizing, ...) in a way that is composable. I am hopeful that there is a way to combine this logic in a sensible way, but that probably requires deep thought from someone.
I appreciate the concern about surprising results. I agree that it should be able to turn automatic rechunking off. I do think that the default should be "on" though. A decent fraction of performance issues in dask array can be solved with better chunking.
bmerry commentedon Feb 4, 2021
It can also cause issues with even a single array. I have code that depends on having only a single chunk along a certain dimension (because of the access pattern of the per-block function). I ensure that by rechunking if necessary before calling
da.blockwise, but if blockwise rechunks under me it will break things.Here's an idea for semantics that will be backwards-compatible but still allow automatic rechunking for common usage: add an optional argument to
blockwiseto allow auto-rechunking, with three possible values e.g. True, False, None, with None being the default. True means do rechunking automatically. None means don't do anything, but warn the user if it creates big chunks. False means don't do anything and don't warn. Whereblockwiseis used internally by dask and it's known to be safe (e.g. in da.outer), pass True.JSKenyon commentedon Feb 4, 2021
I think it is important to remember that blockwise doesn't prescribe what the function it is applying can do. From personal experience, I can say that having chunking changed surprisingly can break things.
I think that the root cause is that not all functions can "commute" with a rechunking operation. That is, if a chunk is a specific size for a reason (due to a particular access pattern, or because the function requires the points in that chunk to be processed simultaneously), rechunking is not an option.
I think @bmerry's suggestion is sound - warn by default, with the option to enable or disable rechunking. From a practical perspective, I can imagine that having rechunking turned on by default will be really confusing for new users.
bmerry commentedon Feb 4, 2021
To be fair, new users are unlikely to be calling blockwise directly, but dask will be calling it under the hood for them, and in those cases it's generally safe to do rechunking.
In general, my feeling is that if a dask function directly exposes the chunking to the user (such as
blockwise,map_blocks) it should (by default) respect the input chunking. If the semantics of a function are independent of the chunking (e.g. element-wise operators,outer,sum, slicing) then the chunking isn't part of the contract and it can legally rechunk if appropriate (although as #6646 revealed, that could still cause performance issues, so shouldn't be done lightly).bmerry commentedon Feb 4, 2021
It's just occurred to me that
da.blockwisealready has a "don't-mess-with-chunking" flag, namelyalign_arrays=False. It's not documented, but it's used bymap_blocks. Whenalign_arraysis True (the default),blockwisecan already affect the chunking, although I think it won't currently affect chunking on dimensions where all the inputs agree. So perhaps rather than adding a new flag as I suggested, we should just document the existing flag and note that users who care about the incoming chunking should manually align their inputs and passalign_arrays=Falseto ensure that it isn't altered. In that case I'd feel slightly more comfortable with automatically splitting large chunks.sjperkins commentedon Feb 4, 2021
I don't believe this is quite accurate. I've always understood
align_arraysto be an option that allows blockwise to match arrays with incompatible chunk sizes (but the same number of chunks along the dimension).align_chunk_sizesmight be a better name.GenevieveBuckley commentedon Jul 20, 2021
I've tried to piece the discussion together from this thread, and these two related conversations:
Terminology:
We didn't use these particular phrases in my maths classes, but here's what I think they're being used to mean in these discussions
For tensordot, it looks like the process should go something like this:
dask.config.get('array.chunk-size')align_arrays=Falsewhen tensordot hands over to blockwise (might need to double check if there is any extra logic in theunify_chunksfunction we now need to take care of in tensordot?)I've written some code for this, while I was trying to work out the process. I'll need a bit more clarity around the questions in point 4 and 6 above. (Hacky code here)
It is clear that the rechunking logic is going to have to happen in tensordot before it gets to blockwise, blockwise just doesn't know enough information to do what we want.
mrocklin commentedon Jul 20, 2021
Most of my comments below are relevant for #2225 rather than this issue (I think that that issue is a bigger deal than this one). It's worth noting that these are two different concerns and not necessarily fully compatible.
First, I highly recommend multiplying two matrices two-dimensional matrices together and experimenting with chunking structure before trying to generalize any of this. I also recommend using the dask.distributed scheduler and having the dashboard up. The chunking structure will make a very large impact on if the computation finishes or not. I think that you probably need to feel this pain viscerally a bit before these issues make much sense.
I would also be totally ok with a first pass that only worked in the 2d x 2d case. This is the common case, probably much easier to get right, and much less likely to stall out with a half-finished PR.
mrocklin commentedon Jul 20, 2021
I think that if you build up a small set of interesting examples that you'll be able to try out different approaches, like what you propose above, and see how they work. I think that this will quickly give you more intuition than anyone here has. The approach that you lay out above sounds sensible to me, but I would bet large amounts of money that it's wrong. I don't think that anyone today knows enough to be able to tell if any approach is solid or not. I'm certainly not able to do so.