Skip to content

Automatically rechunk blockwise when chunk size increases substantially #7124

Open

Description

@mrocklin

Currently blockwise blindly performs an outer product of chunks when doing outer products. This can result in very large chunks, which break computation.

import dask.array as da
x = da.ones((1000000, 1))
y = da.ones((1, 1000000))
z = x.dot(y)
z
dask.array<tensordot, shape=(1000000, 1000000), dtype=float64, chunksize=(1000000, 1000000), chunktype=numpy.ndarray>

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.

cc @tomwhite

Activity

  1. mrocklin commented on Jan 29, 2021

    @mrocklin
    MemberAuthor

    cc @dask/array this issue may interest you

  2. bmerry commented on Jan 30, 2021

    @bmerry
    Contributor

    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.

  3. sjperkins commented on Feb 3, 2021

    @sjperkins
    Member

    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

  4. mrocklin commented on Feb 3, 2021

    @mrocklin
    MemberAuthor

    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.

  5. bmerry commented on Feb 4, 2021

    @bmerry
    Contributor

    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.

  6. JSKenyon commented on Feb 4, 2021

    @JSKenyon
    Contributor

    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.

  7. bmerry commented on Feb 4, 2021

    @bmerry
    Contributor

    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).

  8. bmerry commented on Feb 4, 2021

    @bmerry
    Contributor

    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.

  9. sjperkins commented on Feb 4, 2021

    @sjperkins
    Member

    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).

    assert x.chunks == ((5, 5, 5, 5),)
    assert y.chunks == ((2, 1, 3, 4),)
    assert x.numblocks == y.numblocks
    
    # Fails because chunk sizes differ
    z = 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 output
    z = da.blockwise(fn, "d", x, "d", y, "d", align_arrays=False,
                     adjust_chunks={"d", y.chunks[0]})

    align_chunk_sizes might be a better name.

  10. GenevieveBuckley commented on Jul 20, 2021

    @GenevieveBuckley
    Contributor

    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

    • 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:

    1. Work out which are the outer dimension axes for arrays a and b
    2. 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')
    3. Work backwards from this to figure out how the input tensordot arrays should be rechunked for the outer dimension axes.
    4. 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.
    5. Rechunk the tensordot input arrays
    6. 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.

  11. mrocklin commented on Jul 20, 2021

    @mrocklin
    MemberAuthor

    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.

  12. mrocklin commented on Jul 20, 2021

    @mrocklin
    MemberAuthor

    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.

  13. added
    needs attention
    on Nov 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrayneeds attention

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions