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
'cumsum' results differ from 'cumsum' on a pure numpy array #12359
Describe the issue:
I wanted to calculate a cumsum on a dask array. The results differ from a cumsum on the same data opened as a pure numpy array.
An example plot of the difference between the two approaches (cumsum over the horizontal axis):
There are 3 chunks in time. The results start to diverge at the start of the second chunk.
I expected that using pure numpy arrays or dask arrays should yield results that are numerically close(r). Or maybe the difference can be explained solely by the fact that floating point numbers are not associative, and using dask means the data is added in a different order?
Minimal Complete Verifiable Example:
frompathlibimportPathimportdask.arrayasdaimportmatplotlib.pyplotaspltimportnumpyasnp# Create a random numpy arraychunk_size=500other_dim_size=200shape= (other_dim_size, 3*chunk_size)
numpy_arr=1000*np.random.rand(*shape)
# Save it to diskpath=Path("large_array.npy")
np.save(path, numpy_arr)
# Load it again as a dask arraydata_memmap=np.load(path, mmap_mode="r")
dask_arr=da.from_array(data_memmap, chunks=(other_dim_size, chunk_size))
# Show the difference of the cumsum on the numpy versus dask arrayplt.imshow(numpy_arr.cumsum(axis=-1) -dask_arr.cumsum(axis=-1))
plt.axvline(x=chunk_size, color="red", linestyle="--", label="start of second chunk")
plt.title("Difference of `cumsum` on a numpy and a dask array.")
plt.legend()
plt.colorbar()
plt.gca().set_aspect("auto")
plt.show()
Note that the difference vanishes when calling .compute() before the cumsum, as this loads the data as a numpy array, so, as you saw yourself, this is not related to the IO round trip.
Snippet:
plt.imshow(numpy_arr.cumsum(axis=-1) -dask_arr.compute().cumsum(axis=-1)) # Identicalplt.imshow(numpy_arr.cumsum(axis=-1) -dask_arr.cumsum(axis=-1).compute()) # Different
which is identical for integer where there is no rounding, but in float it causes different rounding errors from numpy. These errors will change with chunk size.
To have identical behaviour dask should be changed to
For clarity: you'll have the same discrepancies with plain sum(). Which will never be fixed, as it would be impossible to do so without a drastic increase in memory usage, as one would need to hold chunk[j] fully in memory until chunk[j-1] is finished. In cumsum, we need to do that anyway, so it is reasonable to exactly match numpy.
Activity
jsignell commentedon Apr 23, 2026
I had two theories:
But I tried:
and:
and I still saw this same behavior.
muttener commentedon Apr 24, 2026
Thanks for looking into this, @jsignell!
Note that the difference vanishes when calling
.compute()before thecumsum, as this loads the data as a numpy array, so, as you saw yourself, this is not related to the IO round trip.Snippet:
crusaderky commentedon May 21, 2026
This is caused by the fact that floating point addition is not associative, and the order of the addition is different between numpy and dask.
In numpy:
In dask:
which is identical for integer where there is no rounding, but in float it causes different rounding errors from numpy. These errors will change with chunk size.
To have identical behaviour dask should be changed to
crusaderky commentedon May 21, 2026
For clarity: you'll have the same discrepancies with plain
sum(). Which will never be fixed, as it would be impossible to do so without a drastic increase in memory usage, as one would need to hold chunk[j] fully in memory until chunk[j-1] is finished. In cumsum, we need to do that anyway, so it is reasonable to exactly match numpy.