Skip to content

Commit

Permalink
[MXNET-1422] Fix wrong results of min([inf, inf]) and max([-inf,-inf]) (
Browse files Browse the repository at this point in the history
apache#16226)

* fix min max infinity value

* add test maximum

* use HUGE_VAL macro for nvcc
  • Loading branch information
wkcn authored and larroy committed Sep 28, 2019
1 parent 01211c1 commit af0e458
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 2 deletions.
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 @@ -9286,6 +9286,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()

0 comments on commit af0e458

Please sign in to comment.