Skip to content

'cumsum' results differ from 'cumsum' on a pure numpy array #12359

Description

@muttener

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):

Image

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:

from pathlib import Path

import dask.array as da
import matplotlib.pyplot as plt
import numpy as np

# Create a random numpy array
chunk_size = 500
other_dim_size = 200
shape = (other_dim_size, 3 * chunk_size)
numpy_arr = 1000 * np.random.rand(*shape)

# Save it to disk
path = Path("large_array.npy")
np.save(path, numpy_arr)

# Load it again as a dask array
data_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 array
plt.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()

Environment:

  • Dask version: 2026.3.0
  • Python version: 3.11
  • Operating System: Debian 13
  • Install method (conda, pip, source): pip

Activity

  1. added
    needs triage
    on Apr 23, 2026
  2. jsignell commented on Apr 23, 2026

    @jsignell
    Member

    I had two theories:

    1. Saving the data to disk was changing it in some way
    2. Different levels of precision were causing this drift

    But I tried:

    plt.imshow(data_memmap.cumsum(axis=-1) - dask_arr.cumsum(axis=-1))

    and:

    plt.imshow(data_memmap.round(9).cumsum(axis=-1) - dask_arr.round(9).cumsum(axis=-1))

    and I still saw this same behavior.

  3. muttener commented on Apr 24, 2026

    @muttener
    Author

    Thanks for looking into this, @jsignell!

    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)) # Identical
    plt.imshow(numpy_arr.cumsum(axis=-1) - dask_arr.cumsum(axis=-1).compute()) # Different
  4. added
    bug
    on Apr 24, 2026
  5. crusaderky commented on May 21, 2026

    @crusaderky
    Collaborator

    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:

      cumsum[idx=0] = value[idx=0]
      cumsum[idx=i] = cumsum[idx=i - 1] + value[idx=i]
    

    In dask:

      cumsum_map[chunk=j, chunk_idx=0] = value[chunk=j, chunk_idx=0]
      cumsum_map[chunk=j, chunk_idx=i] = cumsum_map[chunk=j, chunk_idx=i-1] + value[chunk=j, chunk_idx=i]
      cumsum[chunk=j, chunk_idx=i] = cumsum_map[chunk=j, chunk_idx=i] + sum(k=0...j-1 cumsum_map[chunk=k, chunk_idx=-1])
    

    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

      cumsum[chunk=0, chunk_idx=0] = value[chunk=0, chunk_idx=0]
      cumsum[chunk=j, chunk_idx=0] = cumsum[chunk=j-1, chunk_idx=-1]
      cumsum[chunk=j, chunk_idx=i] = cumsum[chunk=j, chunk_idx=i-1] + value[chunk=j, chunk_idx=i]
    
  6. crusaderky commented on May 21, 2026

    @crusaderky
    Collaborator

    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.

  7. removed
    needs triage
    on May 28, 2026
  8. added
    needs attention
    on Jun 29, 2026
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

    arraybugneeds attention

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions