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
ValueError: cannot broadcast shape in Array.__setitem__ with boolean mask #11753
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[3], line 6
4 a = da.random.uniform(size=10)
5 mask = a >0.5
----> 6 a[mask] = a
File ~/gh/dask/dask/dask/array/core.py:1973, in Array.__setitem__(self, key, value)
1967 # If value has ndim > 0, they must be broadcastable to self.shape[idx].
1968 # This raises when the bool mask causes the size to become unknown,
1969 # e.g. this is valid in numpy but raises here:
1970 # x = da.array([1,2,3])
1971 # x[da.array([True, True, False])] = [4, 5]
1972 if value.ndim:
-> 1973 value = broadcast_to(value, self[key].shape)
1975 y = where(key, value, self)
1976 # FIXME does any backend allow mixed ops vs. numpy?
1977 # If yes, is it wise to let them change the meta?
File ~/gh/dask/dask/dask/array/core.py:5241, in broadcast_to(x, shape, chunks, meta)
5237 ndim_new = len(shape) - x.ndim
5238 if ndim_new < 0 or any(
5239 new != old for new, old in zip(shape[ndim_new:], x.shape) if old != 1
5240 ):
-> 5241 raise ValueError(f"cannot broadcast shape {x.shape} to shape {shape}")
5243 if chunks is None:
5244 chunks = tuple((s,) for s in shape[:ndim_new]) + tuple(
5245 bd if old > 1 else (new,)
5246 for bd, old, new in zip(x.chunks, x.shape, shape[ndim_new:])
5247 )
ValueError: cannot broadcast shape (10,) to shape (nan,)
Anything else we need to know?:
That snippet worked prior to #11728. dask/dask-ml#1012 is a downstream failure in dask-ml from this.
To me this looks like dask-ml is abusing accidental, incorrect, undocumented, and untested behaviour of dask <2025.2.0.
Your reproducer fails on numpy, and rightfully so, as you are trying to call __setitem__ on an array with size ~5 with a value of size 10.
On dask 2025.1.0, a[mask] = baccidentally behaves like a[mask] = b[mask] does on numpy, due to lack of validation. a[mask] = b[mask] has always failed on Dask (and I recognize that's a problem).
I would suggest making a[mask] = b[mask] work in Dask instead of restoring the previous behaviour. In other words, if both lhs and rhs have the same nan chunks, assume they have been sliced by the same mask and delay shape checks until compute time. This would be generically useful through a variety of use cases.
In the meantime, dask-ml should use da.where instead (which is what da.Array.__setitem__ calls internally) unless there is a use case about applying the same functions to numpy arrays, which would take a performance hit?
@crusaderky sorry, I trimmed my minimal example too far. It was supposed to be a[mask] = a[mask]. I see now that works when the size of the last dimension is 1.
import dask.array as da
a = da.random.uniform(size=1)
mask = a > 0.5
a[mask] = a[mask]
I'll look later whether that's just an artifact of the test, or whether it's a behavior dask-ml (and so probably others) were relying on.
File ~/gh/dask/dask-ml/.venv/lib/python3.13/site-packages/dask/array/core.py:1938, in Array.__setitem__(self, key, value)
1935 from dask.array.routines import where
1937 if key.shape != self.shape:
-> 1938 raise IndexError(
1939 f"boolean index shape {key.shape} must match indexed array's "
1940 f"{self.shape}."
1941 )
1943 # If value has ndim > 0, they must be broadcastable to self.shape[idx].
1944 # This raises when the bool mask causes the size to become unknown,
1945 # e.g. this is valid in numpy but raises here:
1946 # x = da.array([1,2,3])
1947 # x[da.array([True, True, False])] = [4, 5]
1948 if value.ndim:
IndexError: boolean index shape (nan,) must match indexed array's (nan,).
That's erroring because (nan,) != (nan,). IMO, that should use something like (key.shape != self.shape) | key.isnan() == self.isnan(), assume that if a dimension is length nan on both sides assume it's the same shape. WDYT?
Activity
phofl commentedon Feb 17, 2025
@crusaderky as well
crusaderky commentedon Feb 18, 2025
To me this looks like dask-ml is abusing accidental, incorrect, undocumented, and untested behaviour of dask <2025.2.0.
Your reproducer fails on numpy, and rightfully so, as you are trying to call
__setitem__on an array with size ~5 with a value of size 10.On dask 2025.1.0,
a[mask] = baccidentally behaves likea[mask] = b[mask]does on numpy, due to lack of validation.a[mask] = b[mask]has always failed on Dask (and I recognize that's a problem).I would suggest making
a[mask] = b[mask]work in Dask instead of restoring the previous behaviour. In other words, if both lhs and rhs have the same nan chunks, assume they have been sliced by the same mask and delay shape checks until compute time. This would be generically useful through a variety of use cases.In the meantime, dask-ml should use
da.whereinstead (which is whatda.Array.__setitem__calls internally) unless there is a use case about applying the same functions to numpy arrays, which would take a performance hit?TomAugspurger commentedon Feb 18, 2025
@crusaderky sorry, I trimmed my minimal example too far. It was supposed to be
a[mask] = a[mask]. I see now that works when the size of the last dimension is1.I'll look later whether that's just an artifact of the test, or whether it's a behavior dask-ml (and so probably others) were relying on.
crusaderky commentedon Feb 24, 2025
That exclusively worked, for some weird reason, if you had a mask sized 1 AND valued True.
On 2025.1.0:
crusaderky commentedon Mar 18, 2025
@TomAugspurger any update on this?
TomAugspurger commentedon Mar 20, 2025
Haven't had a chance to look into it yet.
TomAugspurger commentedon May 10, 2025
@crusaderky able to look at this again today. I agree that my original example was faultly.
We have one more failure in dask-ml, which I think boils down to this:
which raises with
File ~/gh/dask/dask-ml/.venv/lib/python3.13/site-packages/dask/array/core.py:1938, in Array.__setitem__(self, key, value) 1935 from dask.array.routines import where 1937 if key.shape != self.shape: -> 1938 raise IndexError( 1939 f"boolean index shape {key.shape} must match indexed array's " 1940 f"{self.shape}." 1941 ) 1943 # If value has ndim > 0, they must be broadcastable to self.shape[idx]. 1944 # This raises when the bool mask causes the size to become unknown, 1945 # e.g. this is valid in numpy but raises here: 1946 # x = da.array([1,2,3]) 1947 # x[da.array([True, True, False])] = [4, 5] 1948 if value.ndim: IndexError: boolean index shape (nan,) must match indexed array's (nan,).That's erroring because
(nan,) != (nan,). IMO, that should use something like(key.shape != self.shape) | key.isnan() == self.isnan(), assume that if a dimension is lengthnanon both sides assume it's the same shape. WDYT?Fix Array.setitem with nan shapes on both sides
Fix Array.setitem with nan shapes on both sides