Skip to content

ops.maximum does not propagate NaN and returns the other operand instead #368

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.ops.maximum appears to handle NaN incorrectly for float32 tensors.

When one operand contains NaN, calling ops.maximum(x, y) returns the numeric value from the other operand instead of preserving NaN.

Actual result:

[[1.2180202007293701, inf, 1.2976676225662231],
 [-1.0, 1.0, 0.5523166656494141]]

The first element should remain NaN, but it is returned as 1.2180202007293701.

Describe the expected behavior

NaN should be propagated by ops.maximum.

Expected result:

[[nan, inf, 1.2976676225662231],
 [-1.0, 1.0, 0.5523166656494141]]

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.ops.function as ops

x = ms.Tensor(
    np.array(
        [
            [np.nan, np.inf, -np.inf],
            [-1.0, 1.0, 9.999999974752427e-07],
        ],
        dtype=np.float32,
    )
)

y = ms.Tensor(
    np.array(
        [
            [1.2180202007293701, 0.22934786975383759, 1.2976676225662231],
            [-1.0315568447113037, 0.013808977790176868, 0.5523166656494141],
        ],
        dtype=np.float32,
    )
)

wrong = ops.maximum(x, y).asnumpy()

correct = np.array(
    [
        [np.nan, np.inf, 1.2976676225662231],
        [-1.0, 1.0, 0.5523166656494141],
    ],
    dtype=np.float32,
)

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

Related log / screenshot

Running the script produces:

wrong: [[1.2180202007293701, inf, 1.2976676225662231], [-1.0, 1.0, 0.5523166656494141]]
correct: [[nan, inf, 1.2976676225662231], [-1.0, 1.0, 0.5523166656494141]]

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 ops.maximum.

For the first element, the operation is equivalent to:

maximum(nan, 1.2180202007293701)

The current result is:

1.2180202007293701

The expected result is:

nan

Other elements involving +Inf, -Inf, and finite values appear to be handled as expected in this example.

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