From 5e35e5ead6d2f5fd1b3140519005970c1e4df6a5 Mon Sep 17 00:00:00 2001 From: Kaipeng Deng Date: Sat, 23 Nov 2019 15:09:29 +0800 Subject: [PATCH] [cherry-pick] fix elementwise mod (#21315) * fix elementwise_mod FP kernel. test=develop * fix unittest. test=develop --- paddle/fluid/operators/elementwise/elementwise_mod_op.h | 4 +++- .../paddle/fluid/tests/unittests/test_elementwise_mod_op.py | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/operators/elementwise/elementwise_mod_op.h b/paddle/fluid/operators/elementwise/elementwise_mod_op.h index e568a5dc72c..4306a471b76 100644 --- a/paddle/fluid/operators/elementwise/elementwise_mod_op.h +++ b/paddle/fluid/operators/elementwise/elementwise_mod_op.h @@ -29,7 +29,9 @@ struct ModFunctor { template struct ModFunctorFP { - inline HOSTDEVICE T operator()(T a, T b) const { return std::fmod(a, b); } + inline HOSTDEVICE T operator()(T a, T b) const { + return fmod(b + fmod(a, b), b); + } }; template diff --git a/python/paddle/fluid/tests/unittests/test_elementwise_mod_op.py b/python/paddle/fluid/tests/unittests/test_elementwise_mod_op.py index fcda179a093..c4740e063ff 100644 --- a/python/paddle/fluid/tests/unittests/test_elementwise_mod_op.py +++ b/python/paddle/fluid/tests/unittests/test_elementwise_mod_op.py @@ -71,7 +71,7 @@ class TestElementwiseModOpFloat(TestElementwiseModOp): def init_input_output(self): self.x = np.random.uniform(-1000, 1000, [10, 10]).astype(self.dtype) self.y = np.random.uniform(-100, 100, [10, 10]).astype(self.dtype) - self.out = np.fmod(self.x, self.y) + self.out = np.fmod(self.y + np.fmod(self.x, self.y), self.y) def test_check_output(self): self.check_output(atol=2e-5) -- GitLab