Skip to content

mnp.nancumsum produces NaN after mixed +Inf and -Inf instead of finite accumulated values #370

Description

@ALinrunrun

Environment

Hardware Environment(Ascend/GPU/CPU):

/device cpu

Software Environment:

  • MindSpore version (source or binary): MindSpore 2.9.0
  • Python version (e.g., Python 3.7.5): Python 3.11.15
  • OS platform and distribution (e.g., Linux Ubuntu 16.04): Ubuntu 24.04.1, Linux 6.17.0-29-generic, x86_64
  • GCC/Compiler version (if compiled from source): gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0

Describe the current behavior

mindspore.numpy.nancumsum appears to handle NaN/Inf values incorrectly for float32 tensors.

For the input:

[[nan, inf, -inf],
 [-4.2111430168151855, -12.858903884887695, 4.690601348876953]]

calling:

mnp.nancumsum(x)

returns:

[0.0, inf, nan, nan, nan, nan]

After the +Inf and -Inf values are accumulated together, the result becomes NaN, and the following cumulative values also remain NaN.

Describe the expected behavior

mnp.nancumsum should ignore NaN values and produce the expected accumulated float32 result.

Expected result:

[0.0, 3.4028234663852886e+38, 0.0, -4.2111430168151855, -17.07004737854004, -12.379446029663086]

Steps to reproduce the issue

  1. Install MindSpore 2.9.0 and NumPy 1.26.4.
  2. Save the following script as reproduce.py.
  3. Run python reproduce.py.
import numpy as np
import mindspore as ms
import mindspore.numpy as mnp

x = ms.Tensor(
    np.array(
        [
            [np.nan, np.inf, -np.inf],
            [-4.2111430168151855, -12.858903884887695, 4.690601348876953],
        ],
        dtype=np.float32,
    )
)

wrong = mnp.nancumsum(x).asnumpy()

correct = np.array(
    [
        0.0,
        3.4028234663852886e38,
        0.0,
        -4.2111430168151855,
        -17.07004737854004,
        -12.379446029663086,
    ],
    dtype=np.float32,
)

print("wrong:", wrong.tolist())
print("correct:", correct.tolist())

Related log / screenshot

Running the script produces:

wrong: [0.0, inf, nan, nan, nan, nan]
correct: [0.0, 3.4028234663852886e+38, 0.0, -4.2111430168151855, -17.07004737854004, -12.379446029663086]

Environment output:

Python version: 3.11.15
OS platform: Linux-6.17.0-29-generic-x86_64-with-glibc2.39
Machine: x86_64
MindSpore version: 2.9.0
NumPy version: 1.26.4
GCC version: gcc (Ubuntu 13.3.0-6ubuntu2~24.04.1) 13.3.0

Special notes for this issue

This looks like a NaN/Inf handling issue in mnp.nancumsum.

In this example, NaN is correctly treated as 0.0 at the first position, but the accumulated result becomes NaN after the +Inf and -Inf values are processed. The expected behavior is to keep producing finite accumulated float32 values instead of propagating NaN through the rest of the result.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions