Skip to content

Fix maximum and minimum for Nan input #71933

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 66 additions & 3 deletions paddle/phi/kernels/funcs/elementwise_functor.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ limitations under the License. */
#include "paddle/phi/core/enforce.h"
#if defined(__xpu__)
#include <xpu/runtime.h>

#include <type_traits>
#include "xpu/kernel/math_xpu2.h" // pow()
#endif
#include "paddle/phi/common/amp_type_traits.h"
Expand Down Expand Up @@ -467,9 +467,40 @@ struct MultiplyGradXYFunctor<ComplexType<InT>, ComplexType<OutT>> {
};

// Maximum
template <typename T>
template <typename T, typename Enable = void>
struct MaximumFunctor {
inline HOSTDEVICE T operator()(const T a, const T b) const {
if constexpr ((std::is_floating_point_v<T>)&&(
!(std::is_same_v<T, int32_t> ||
std::is_same_v<T, int64_t>))) {
#if defined(__CUDACC__) || defined(__HIPCC__)
if (::isnan(a)) {
return a;
}
if (::isnan(b)) {
return b;
}
#else
if (std::isnan(a)) {
return a;
}
if (std::isnan(b)) {
return b;
}
#endif
}
return a > b ? a : b;
}
};

template <typename T>
struct MaximumFunctor<
T,
typename std::enable_if<std::is_same_v<T, phi::dtype::bfloat16> ||
std::is_same_v<T, phi::dtype::float16>>::type> {
inline HOSTDEVICE T operator()(const T a, const T b) const {
if (phi::dtype::isnan(a)) return a;
if (phi::dtype::isnan(b)) return b;
return a > b ? a : b;
}
};
Expand Down Expand Up @@ -509,12 +540,44 @@ struct MaxGradXYFunctor {
};

// Minimum
template <typename T>
template <typename T, typename Enable = void>
struct MinimumFunctor {
inline HOSTDEVICE T operator()(const T a, const T b) const {
if constexpr (std::is_floating_point_v<T> &&
(!(std::is_same_v<T, int32_t> ||
std::is_same_v<T, int64_t>))) {
#if defined(__CUDACC__) || defined(__HIPCC__)
if (::isnan(a)) {
return a;
}
if (::isnan(b)) {
return b;
}
#else
if (std::isnan(a)) {
return a;
}
if (std::isnan(b)) {
return b;
}
#endif
}
return a < b ? a : b;
}
};

template <typename T>
struct MinimumFunctor<
T,
typename std::enable_if<std::is_same_v<T, phi::dtype::bfloat16> ||
std::is_same_v<T, phi::dtype::float16>>::type> {
inline HOSTDEVICE T operator()(const T a, const T b) const {
if (phi::dtype::isnan(a)) return a;
if (phi::dtype::isnan(b)) return b;
return a < b ? a : b;
}
};

template <typename T>
struct MinGradXFunctor {
inline HOSTDEVICE T operator()(const T x, const T y, const T dout) const {
Expand Down
90 changes: 90 additions & 0 deletions test/legacy_test/test_maximum_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import unittest

import numpy as np
from utils import dygraph_guard, static_guard

import paddle
from paddle.base import core
Expand All @@ -34,10 +35,22 @@ def setUp(self):
self.input_b = np.array([2, np.inf, -np.inf]).astype('int64')
self.input_c = np.array([4, 1, 3]).astype('int64')

self.input_nan_a = np.array([0, np.nan, np.nan]).astype('float32')
self.input_nan_b = np.array([0, 1, 2]).astype('float32')

self.np_expected1 = np.maximum(self.input_x, self.input_y)
self.np_expected2 = np.maximum(self.input_x, self.input_z)
self.np_expected3 = np.maximum(self.input_a, self.input_c)
self.np_expected4 = np.maximum(self.input_b, self.input_c)
self.np_expected_nan_aa = np.maximum(
self.input_nan_a, self.input_nan_a
) # maximum(Nan, Nan)
self.np_expected_nan_ab = np.maximum(
self.input_nan_a, self.input_nan_b
) # maximum(Nan, Num)
self.np_expected_nan_ba = np.maximum(
self.input_nan_b, self.input_nan_a
) # maximum(Num, Nan)

def test_static_api(self):
paddle.enable_static()
Expand Down Expand Up @@ -164,6 +177,83 @@ def test_equal_tensors(self):
1e-2,
)

@unittest.skipIf(
core.is_compiled_with_xpu(),
"XPU need fix the bug",
)
def test_dynamic_nan(self):
with dygraph_guard():
nan_a = paddle.to_tensor(self.input_nan_a)
nan_b = paddle.to_tensor(self.input_nan_b)
res = paddle.maximum(nan_a, nan_a)
res = res.numpy()
np.testing.assert_allclose(
res, self.np_expected_nan_aa, rtol=1e-05, equal_nan=True
)

res = paddle.maximum(nan_a, nan_b)
res = res.numpy()
np.testing.assert_allclose(
res, self.np_expected_nan_ab, rtol=1e-05, equal_nan=True
)

res = paddle.maximum(nan_b, nan_a)
res = res.numpy()
np.testing.assert_allclose(
res, self.np_expected_nan_ba, rtol=1e-05, equal_nan=True
)

@unittest.skipIf(
core.is_compiled_with_xpu(),
"XPU need fix the bug",
)
def test_static_nan(self):
with static_guard():
with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):
data_a = paddle.static.data("a", shape=[3], dtype="float32")
data_b = paddle.static.data("b", shape=[3], dtype="float32")
result_max = paddle.maximum(data_a, data_b)
exe = paddle.static.Executor(self.place)
(res,) = exe.run(
feed={"a": self.input_nan_a, "b": self.input_nan_a},
fetch_list=[result_max],
)
np.testing.assert_allclose(
res, self.np_expected_nan_aa, rtol=1e-05, equal_nan=True
)

with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):
data_a = paddle.static.data("a", shape=[3], dtype="float32")
data_b = paddle.static.data("b", shape=[3], dtype="float32")
result_max = paddle.maximum(data_a, data_b)
exe = paddle.static.Executor(self.place)
(res,) = exe.run(
feed={"a": self.input_nan_a, "b": self.input_nan_b},
fetch_list=[result_max],
)
np.testing.assert_allclose(
res, self.np_expected_nan_ab, rtol=1e-05, equal_nan=True
)

with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):
data_a = paddle.static.data("a", shape=[3], dtype="float32")
data_b = paddle.static.data("b", shape=[3], dtype="float32")
result_max = paddle.maximum(data_a, data_b)
exe = paddle.static.Executor(self.place)
(res,) = exe.run(
feed={"a": self.input_nan_b, "b": self.input_nan_a},
fetch_list=[result_max],
)
np.testing.assert_allclose(
res, self.np_expected_nan_ba, rtol=1e-05, equal_nan=True
)

def test_0size_input(self):
numpy_tensor = np.ones([0, 1, 2]).astype("float32")
paddle_x = paddle.to_tensor(numpy_tensor)
Expand Down
91 changes: 91 additions & 0 deletions test/legacy_test/test_minimum_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import unittest

import numpy as np
from utils import dygraph_guard, static_guard

import paddle
from paddle.base import core
Expand All @@ -34,10 +35,22 @@ def setUp(self):
self.input_b = np.array([2, np.inf, -np.inf]).astype('int64')
self.input_c = np.array([4, 1, 3]).astype('int64')

self.input_nan_a = np.array([0, np.nan, np.nan]).astype('float32')
self.input_nan_b = np.array([0, 1, 2]).astype('float32')

self.np_expected1 = np.minimum(self.input_x, self.input_y)
self.np_expected2 = np.minimum(self.input_x, self.input_z)
self.np_expected3 = np.minimum(self.input_a, self.input_c)
self.np_expected4 = np.minimum(self.input_b, self.input_c)
self.np_expected_nan_aa = np.minimum(
self.input_nan_a, self.input_nan_a
) # minimum(Nan, Nan)
self.np_expected_nan_ab = np.minimum(
self.input_nan_a, self.input_nan_b
) # minimum(Nan, Num)
self.np_expected_nan_ba = np.minimum(
self.input_nan_b, self.input_nan_a
) # minimum(Num, Nan)

def test_static_api(self):
paddle.enable_static()
Expand Down Expand Up @@ -164,6 +177,84 @@ def test_equal_tensors(self):
1e-2,
)

@unittest.skipIf(
core.is_compiled_with_xpu(),
"XPU need fix the bug",
)
def test_dynamic_nan(self):
with dygraph_guard():
nan_a = paddle.to_tensor(self.input_nan_a)
nan_b = paddle.to_tensor(self.input_nan_b)

res = paddle.minimum(nan_a, nan_a)
res = res.numpy()
np.testing.assert_allclose(
res, self.np_expected_nan_aa, rtol=1e-05, equal_nan=True
)

res = paddle.minimum(nan_a, nan_b)
res = res.numpy()
np.testing.assert_allclose(
res, self.np_expected_nan_ab, rtol=1e-05, equal_nan=True
)

res = paddle.minimum(nan_b, nan_a)
res = res.numpy()
np.testing.assert_allclose(
res, self.np_expected_nan_ba, rtol=1e-05, equal_nan=True
)

@unittest.skipIf(
core.is_compiled_with_xpu(),
"XPU need fix the bug",
)
def test_static_nan(self):
with static_guard():
with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):
data_a = paddle.static.data("a", shape=[3], dtype="float32")
data_b = paddle.static.data("b", shape=[3], dtype="float32")
result_max = paddle.minimum(data_a, data_b)
exe = paddle.static.Executor(self.place)
(res,) = exe.run(
feed={"a": self.input_nan_a, "b": self.input_nan_a},
fetch_list=[result_max],
)
np.testing.assert_allclose(
res, self.np_expected_nan_aa, rtol=1e-05, equal_nan=True
)

with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):
data_a = paddle.static.data("a", shape=[3], dtype="float32")
data_b = paddle.static.data("b", shape=[3], dtype="float32")
result_max = paddle.minimum(data_a, data_b)
exe = paddle.static.Executor(self.place)
(res,) = exe.run(
feed={"a": self.input_nan_a, "b": self.input_nan_b},
fetch_list=[result_max],
)
np.testing.assert_allclose(
res, self.np_expected_nan_ab, rtol=1e-05, equal_nan=True
)

with paddle.static.program_guard(
paddle.static.Program(), paddle.static.Program()
):
data_a = paddle.static.data("a", shape=[3], dtype="float32")
data_b = paddle.static.data("b", shape=[3], dtype="float32")
result_max = paddle.minimum(data_a, data_b)
exe = paddle.static.Executor(self.place)
(res,) = exe.run(
feed={"a": self.input_nan_b, "b": self.input_nan_a},
fetch_list=[result_max],
)
np.testing.assert_allclose(
res, self.np_expected_nan_ba, rtol=1e-05, equal_nan=True
)

def test_0size_input(self):
numpy_tensor = np.ones([0, 1, 2]).astype("float32")
paddle_x = paddle.to_tensor(numpy_tensor)
Expand Down
Loading