numpy.max/min and numpy.maximum/minimum drop NaN, and np.max can return a value that is neither the maximum nor NaN

Open
#2,730 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
numpy, python
Domain
backend, compilers

Research direction

Start by comparing the NumPy frontend replacements with ReductionType::Max and Min in reduction.h; the report says the shared reduction behavior also affects GPU atomics, SVE, and the Fortran frontend. Reproduce the shown NaN cases on CPU, then map which operations use each lowering path. Done means the affected max/min operations match NumPy’s NaN propagation without breaking the other listed backends; the implementation scope needs maintainer direction.

Written by the indexing model from the issue text.

Description

NumPy propagates NaN through np.max, np.min, np.maximum and np.minimum (np.fmax/np.fmin and np.nanmax are the NaN-ignoring variants). In DaCe all of them lower to max/min, implemented as (a > b) ? a : b, and every comparison with NaN is false, so a NaN resets the running result partway through a reduction:

import numpy as np, dace

A = np.array([1.0, 5.0, np.nan, 2.0, 3.0, -1.0, 0.0, 4.0])

@dace.program
def amax(A: dace.float64[8], out: dace.float64[1]):
    out[0] = np.max(A)

@dace.program
def amin(A: dace.float64[8], out: dace.float64[1]):
    out[0] = np.min(A)

o = np.zeros(1); amax(A.copy(), o); print(o, np.max(A))   # [2.] nan
o = np.zeros(1); amin(A.copy(), o); print(o, np.min(A))   # [2.] nan

On the same array A.max() returns 5.0 and np.maximum.reduce(A) returns 4.0, so the result depends on which path the program takes. Elementwise, np.maximum(A, B) and np.minimum(A, B) return the non-NaN operand where NumPy returns NaN, and np.clip(nan, ...), np.sign(nan) and np.heaviside(nan, h) return a number where NumPy returns NaN. Checked on main (413889d), CPU, with NumPy 2.4.6.

A fix probably needs NaN-propagating max/min for floating-point types in the NumPy frontend and in ReductionType::Max/Min (reduction.h), which also affects GPU atomics, SVE and the Fortran frontend's MAX/MIN, so I wanted to ask how you would like it done before sending a PR: NumPy-specific helpers used only by the NumPy replacements, or NaN propagation in the reduction types themselves.

Dominant language
Python
Stars
602
Forks
171
Avg merge
1d 16h
Merged PRs (30d)
43

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from spcl/dace

All issues in spcl/dace

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.