Skip to content

Expected behavior of dask.array.full() for non-scalar fill values? #6107

Closed

Description

@Huite

What is the expected behavior for dask.array.full() for a non-scalar fill_value?

The numpy.full() signature in the documentation mentions "scalar" for fill_value.
https://docs.scipy.org/doc/numpy/reference/generated/numpy.full.html

However, it'll broadcast non-scalar values if it can:

a = np.full((3, 2), [1, 2])

Since dask.array.full() wraps this function, it works similarly:

import dask.array

a1 = dask.array.full((2, 3), [[1, 2, 3], [4, 5, 6]])
a1.compute()

The issue is that when the chunk sizes change, it won't work anymore, as it starts broadcasting per chunk:

a2 = dask.array.full((2, 3), [[1, 2, 3], [4, 5, 6]], chunks=(1, 3))
a2.compute()  # ValueError: could not broadcast input array from shape (2,3) into shape (1,3)
a3 = dask.array.full((2, 3), [1, 2, 3], chunks=(1, 3))
a3.compute()

My feeling is that choosing different chunk sizes shouldn't lead to different behavior here. I think I'd prefer a ValueError for a non-scalar fill_value.

Activity

  1. jsignell commented on Apr 17, 2020

    @jsignell
    Member

    Thanks for raising this issue. I think your proposal of raising an Error when the fill_value is not a scalar is quite reasonable but I wouldn't want to break people unnecessarily. Using full with a list works with chunks as long as the fill_value can be contained in one chunk.

    image

    I do think it'd be better if we can raise an error before calling .compute. I'm just not sure how to go about that. For the record that would happen here:

  2. Huite commented on Apr 19, 2020

    @Huite
    ContributorAuthor

    A few thoughts:
    If I take a look at the dask.array API (https://docs.dask.org/en/latest/array-api.html#dask.array), I don't think many of the functions implicitly do this kind of broadcasting? -- So this "chunkwise broadcasting" maybe only affects full?

    This is probably a naive idea, but:

    import dask.array
    
    def full(shape, fill_value, chunks=None):
        return dask.array.broadcast_to(fill_value, shape, chunks=chunks)

    broadcast_to does raise the kind of error we'd be after:

    (From your examples):

    a1 = full((4, 2, 3), [[1, 2, 3], [4, 5, 6]])
    a1.compute()
    
    a2 = full((4, 2, 3), [[1, 2, 3], [4, 5, 6]], chunks=(2, 2, 3))
    a2.compute()
    
    a3 = full((4, 2, 3), [[1, 2, 3], [4, 5, 6]], chunks=(2, 2, 1))

    The last line raises an error immediately, without .compute:

    ValueError: cannot broadcast chunks ((2,), (3,)) to chunks ((2, 2), (2,), (1, 1, 1)): new chunks must either be along a new dimension or a dimension of size 1

    (Ignoring the order and dtype arguments of full in this example)

  3. jcrist commented on Apr 20, 2020

    @jcrist
    Member

    I think we should just error if fill_value isn't a scalar. (I didn't even know numpy supported broadcasting of fill_value). The broadcasting behavior only works for dask in extremely specific cases, and only works by accident, not by design.

    @Huite, would you be interested in making a PR adding a nice error message for this case?

  4. added
    good first issue
    on Apr 20, 2020
  5. Huite commented on Apr 21, 2020

    @Huite
    ContributorAuthor

    I'd be happy to oblige, but I'm not sure what the best way of approaching this is, since full is currently justs wraps np.full:

    full = w(np.full)

    This requires a specific check on fill_value, so I don't think there's an elegant solution within the wrapping functions. This is the easiest way I could special case it:

    # full() requires special casing due to argument check on fill_value
    _full = w(np.full)  # Generate only once
    
    
    def full(shape, fill_value, dtype=None, order='C'):
        # np.isscalar has somewhat strange behavior: 
        # https://docs.scipy.org/doc/numpy/reference/generated/numpy.isscalar.html 
        if np.ndim(fill_value) != 0:
            raise ValueError(f"fill_value must be scalar. Received {fill_value} instead.")
        return _full(shape, fill_value, dtype, order)
    
    
    full.__doc__ = _full.__doc__
    full.__name__ = _full.__name__

    Not sure if that's what you had in mind, more or less?

    The most elegant fix seems tackling it upstream in numpy. But I'd argue it's not a real issue in numpy anyway (even though the numpy signature explicitly says "scalar"), and it would likely cause significant breakage.

  6. TomAugspurger commented on Apr 22, 2020

    @TomAugspurger
    Member

    @Huite that looks good. Could you submit a pull request with it?

  7. Huite commented on Apr 22, 2020

    @Huite
    ContributorAuthor

    Yes, will do!

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

    arraygood first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions