From 2899715921612ef4dd147004292b5b5d0f83320b Mon Sep 17 00:00:00 2001 From: Sam Skalicky Date: Mon, 20 Aug 2018 16:21:22 -0700 Subject: [PATCH] [MXNET-792] Fix for issue #9816 with dropout operator and RNG (#12091) * added mshadow op for threshold_eq (theshold currently does <, this will do <=) modified dropout operator to use threshold_eq instead of theshold this will ensure equivalent behavior for the random numbers generated on CPU [0, 1) and GPU (0, 1] removed fixed seed for test_dropout * removed comment about flaky test --- src/operator/mshadow_op.h | 1 + src/operator/nn/dropout-inl.h | 3 ++- tests/python/unittest/test_operator.py | 4 ++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/operator/mshadow_op.h b/src/operator/mshadow_op.h index 339719375fdd..06a223dda398 100644 --- a/src/operator/mshadow_op.h +++ b/src/operator/mshadow_op.h @@ -275,6 +275,7 @@ MXNET_UNARY_MATH_OP(square_grad, 2.0f * math::id(a)); /*! \brief used for generate Bernoulli mask */ MXNET_BINARY_MATH_OP_NC(threshold, a < b ? DType(1) : DType(0)); +MXNET_BINARY_MATH_OP_NC(threshold_eq, a <= b ? DType(1) : DType(0)); /*! \brief used for generate element of abs */ MXNET_UNARY_MATH_OP(abs, math::fabs(a)); // NOLINT(*) diff --git a/src/operator/nn/dropout-inl.h b/src/operator/nn/dropout-inl.h index 8e4aac613540..b7c40fbdf52a 100644 --- a/src/operator/nn/dropout-inl.h +++ b/src/operator/nn/dropout-inl.h @@ -206,7 +206,7 @@ class DropoutOp { const real_t pkeep) { RNG_KERNEL_LOOP(xpu, DType, id, gen, N, step, { const real_t rand_num = static_cast(genImpl.uniform()); - mask_out[i] = mshadow_op::threshold::Map(rand_num, pkeep) * (1.0f / pkeep); + mask_out[i] = mshadow_op::threshold_eq::Map(rand_num, pkeep) * (1.0f / pkeep); dropout_out[i] = input_data[i] * mask_out[i]; }); } @@ -258,6 +258,7 @@ class DropoutOp { this->pkeep_); return; } + // initialize the mask LaunchRNG(s, pgen, mask.Size(), mask.dptr(), diff --git a/tests/python/unittest/test_operator.py b/tests/python/unittest/test_operator.py index 125666ba832c..0ff9a106a728 100644 --- a/tests/python/unittest/test_operator.py +++ b/tests/python/unittest/test_operator.py @@ -5722,8 +5722,7 @@ def test_stack(): check_numeric_gradient(out, inputs) -# test fails with seed 990952066: 0 output seen with dropout ratio=0. See issue #9816 -@with_seed(1234) +@with_seed() def test_dropout(): def zero_count(array, ratio): zeros = 0 @@ -5775,6 +5774,7 @@ def check_dropout_ratio(ratio, shape): exe.arg_arrays[0][:] = 1 exe.forward(is_train=True) + if not math.isnan(max_value): assert exe.outputs[0].asnumpy().max() > 0 else: