-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Inplace operators behaviour for dask arrays inconsistent with numpy #5199
Copy link
Copy link
Open
Labels
Description
Inplace operators modify numpy arrays inplace, but end up making a copy of dask arrays.
# Using dask v2.2.0, numpy 1.17.0
import numpy as np
import dask.array as da
x_np = np.zeros(5)
y_np = x_np
y_np += 1
x_da = da.zeros(5)
y_da = x_da
y_da += 1
print(x_np == y_np)
# [ True True True True True]
print(id(x_np) == id(y_np))
# True
print(x_da.compute() == y_da.compute())
# [False False False False False]
print(id(x_da) == id(y_da))
# FalseI'm not sure if you'd qualify this as a bug, since the behaviour between the two is known to not line up completely. I have also seen related issues have been brought up, like #2588 and #2000.
I think a warning for this behavior could be useful. Alternatively, I think this could just be an inplace operation. Here's are some proof of concept implementations. This should probably be done with ufuncs if implemented.
import dask.array as da
import numpy as np
# Something like this
def dask_iadd(x, y):
np.add(x, y, out=x)
return x
da.core.Array.__iadd__ = dask_iadd
a = da.ones((10, 10))
b = a
b += 1
print(id(a) == id(b))
# True
print(np.all(a.compute() == b.compute()))
# TruePrevious crude implementation
import dask.array as da
def _replace(x, y):
x._meta = y._meta
x.dask = y.dask
x.name = y.name
x._chunks = y.chunks
return x
da.core.Array.__iadd__ = lambda x, y: _replace(x, x + y)
# etc
z = da.zeros((2, 2))
z += 1
z.compute()
# array([[1., 1.],
# [1., 1.]])Reactions are currently unavailable