Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[MXNET-1422] Fix wrong results of min([inf, inf]) and max([-inf,-inf]) #16226

Merged
merged 3 commits into from
Sep 20, 2019
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
43 changes: 41 additions & 2 deletions 3rdparty/mshadow/mshadow/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,25 @@ MSHADOW_XINLINE int64_t MinValue<int64_t>(void) {
return LLONG_MIN;
}

/*!
* \brief negative infinity of certain types
* \tparam DType data type
*/
template<typename DType>
MSHADOW_XINLINE DType NegInfValue(void) {
return MinValue<DType>();
}
/*! \brief negative infinity value of float */
template<>
MSHADOW_XINLINE float NegInfValue<float>(void) {
return -HUGE_VALF;
}
/*! \brief negative infinity value of double */
template<>
MSHADOW_XINLINE double NegInfValue<double>(void) {
return -HUGE_VAL;
}

/*!
* \brief maximum value of certain types
* \tparam DType data type
Expand Down Expand Up @@ -686,6 +705,26 @@ template<>
MSHADOW_XINLINE int64_t MaxValue<int64_t>(void) {
return LLONG_MAX;
}

/*!
* \brief positive infinity of certain types
* \tparam DType data type
*/
template<typename DType>
MSHADOW_XINLINE DType PosInfValue(void) {
return MaxValue<DType>();
}
/*! \brief positive infinity value of float */
template<>
MSHADOW_XINLINE float PosInfValue<float>(void) {
return HUGE_VALF;
}
/*! \brief positive infinity value of double */
template<>
MSHADOW_XINLINE double PosInfValue<double>(void) {
return HUGE_VAL;
}

} // namespace limits

/*! \brief sum reducer */
Expand Down Expand Up @@ -793,7 +832,7 @@ struct maximum {
*/
template<typename DType>
MSHADOW_XINLINE static void SetInitValue(DType &initv) { // NOLINT(*)
initv = limits::MinValue<DType>();
initv = limits::NegInfValue<DType>();
}
/*!
*\brief set the initial value during reduction
Expand Down Expand Up @@ -849,7 +888,7 @@ struct minimum {
*/
template<typename DType>
MSHADOW_XINLINE static void SetInitValue(DType &initv) { // NOLINT(*)
initv = limits::MaxValue<DType>();
initv = limits::PosInfValue<DType>();
}
/*!
*\brief set the initial value during reduction
Expand Down
17 changes: 17 additions & 0 deletions tests/python/unittest/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9252,6 +9252,23 @@ def test_sample_normal_default_shape():
assert s.shape == (1, 1)


def test_min_max_inf():
dtypes = [np.float32, np.double]
elem_list = [-1, 1, 0, np.inf, -np.inf]

for dtype in dtypes:
for a in elem_list:
for b in elem_list:
data_np = np.array([a, b], dtype=dtype)
data_mx = mx.nd.array(data_np, dtype=dtype)

min_data_np, max_data_np = data_np.min(), data_np.max()
min_data_mx, max_data_mx = data_mx.min(), data_mx.max()

assert_array_equal(min_data_np, min_data_mx.asnumpy())
assert_array_equal(max_data_np, max_data_mx.asnumpy())


if __name__ == '__main__':
import nose
nose.runmodule()