Skip to content

confusing result of map_blocks. #4292

Closed

Description

@rainwoodman

Use Case:

x = da.arange(100, chunks=32)
y = x.map_blocks(lambda blk: numpy.repeat(blk, blk))

print(y.compute().shape, y.shape)

Output

(4950,) (100,)

Expected output is (4950,) (4950,), or an Exception("shape of output changed, but the chunks argument is not provided)

Manually providing the chunks gives correct output (as documented)

x = da.arange(100, chunks=32)
N = x.map_blocks(lambda blk: blk.sum().reshape(1), chunks=(1,))
y = x.map_blocks(lambda blk: numpy.repeat(blk, blk), chunks=(tuple(N.compute()),))
print(y.compute().shape, y.shape)

Output

(4950,) (4950,)

However, this is inefficient and verbose. The x array is mapped one extra time, for a piece of information that is already available during the second map_blocks, as the shape of the output.

If the output array can be constructed after the function call (e.g. use a concentrate operation), then the use case can produce expected output instead of raising an exception.

Activity

  1. mrocklin commented on Dec 12, 2018

    @mrocklin
    Member

    Expected output is (4950,) (4950,), or an Exception("shape of output changed, but the chunks argument is not provided)

    Yeah, I think that there is a tradeoff between being permissive and strict. My guess is that users appreciate being allowed to be a bit sloppy here, but I agree that this can cause issues.

    However, this is inefficient and verbose. The x array is mapped one extra time, for a piece of information that is already available during the second map_blocks, as the shape of the output.

    Agreed

    If the output array can be constructed after the function call (e.g. use a concentrate operation), then the use case can produce expected output instead of raising an exception.

    I'm not sure I understand this last statement.

  2. deleted a comment from mrocklin on Dec 13, 2018
  3. shoyer commented on Dec 13, 2018

    @shoyer
    Member

    You can also use NaN to indicate unknown chunks, i.e., y = x.map_blocks(lambda blk: numpy.repeat(blk, blk), chunks=(numpy.nan,)). Many operations work with unknown chunks.

    Dask arrays have fixed chunks at construction time. That's pretty baked into the data model -- I don't know what else we could do here.

  4. mrocklin commented on Dec 20, 2018

    @mrocklin
    Member

    Any further thoughts on this @rainwoodman ?

    Otherwise I'm inclined to close as "wont fix"

  5. rainwoodman commented on Dec 22, 2018

    @rainwoodman
    ContributorAuthor

    @shoyer the nan chunk does not feel very enjoyable. (likely due to using nan as a placeholder). I think it would work in cases where I do not actually care the result of map, e.g. if I am using map to submit futures over every chunk of the array.

    I worry that the mandate of supporting code that depends on this sloppiness makes it difficult for the API to evolve in the future. For now I think I will depend on the sloppiness here too, so yes don't fix this yet.

  6. jcrist commented on Apr 30, 2019

    @jcrist
    Member

    Since this would require computing shape information before constructing the graph (going against dask-array's data model), I'm closing this as won't fix. We recommend either specifying your known shapes beforehand, or specifying them as nan (unknown). You may also be interested in #3293.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions