Skip to content

max() and min() handle complex NaN differently from NumPy #2750

@leofang

Description

@leofang

Reproducer:

import numpy as np
import cupy as cp


cp.cuda.cub_enabled = False  # fall back to cupy's reduction routines
a = [0.5 + 3.7j, np.complex(0.7, np.nan), np.complex(np.nan, -3.9),  np.complex(np.nan, np.nan)]

a_np = np.asarray(a)
print(a_np.max(), a_np.min())

a_cp = cp.asarray(a_np)
print(a_cp.max(), a_cp.min())

Outputs from NumPy and CuPy do not match:

(0.7+nanj) (0.7+nanj)
(nan-3.9j) (nan-3.9j)

NumPy seems to propagate the first encountered NaN, but I can't verified this rigorously. BTW, CUB handles this correctly (set cp.cuda.cub_enabled = True).

I can't figure out what's wrong by inspecting the _amax and _amin implementations in cupy/core/_routines_statistics.pyx. Seems to be an issue of how pairwise operations are actually done in the reduction kernel.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions