From 7fcb32ddf3d1e6bee30902639ce1b0d8858dc320 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Tue, 27 Oct 2020 08:01:31 -0500 Subject: [PATCH] fill_constant op supports NINF (#28270) --- paddle/fluid/operators/fill_constant_op.h | 2 ++ .../paddle/fluid/tests/unittests/test_fill_constant_op.py | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/paddle/fluid/operators/fill_constant_op.h b/paddle/fluid/operators/fill_constant_op.h index 239083f88d9..cce28cae975 100644 --- a/paddle/fluid/operators/fill_constant_op.h +++ b/paddle/fluid/operators/fill_constant_op.h @@ -50,6 +50,8 @@ class FillConstantKernel : public framework::OpKernel { // handle NaN/Inf first, which cannot be read from stream. if (str_value == "inf") { value = static_cast(std::numeric_limits::infinity()); + } else if (str_value == "-inf") { + value = static_cast(-std::numeric_limits::infinity()); } else if (str_value == "nan") { value = static_cast(std::numeric_limits::quiet_NaN()); } else { diff --git a/python/paddle/fluid/tests/unittests/test_fill_constant_op.py b/python/paddle/fluid/tests/unittests/test_fill_constant_op.py index babfcdb9040..c305f71aa53 100644 --- a/python/paddle/fluid/tests/unittests/test_fill_constant_op.py +++ b/python/paddle/fluid/tests/unittests/test_fill_constant_op.py @@ -340,6 +340,12 @@ class TestFillConstantImperative(unittest.TestCase): res = fluid.layers.fill_constant([1], 'float32', np.inf) self.assertTrue(np.isinf(res.numpy().item(0))) + def test_ninf(self): + with fluid.dygraph.guard(): + res = fluid.layers.fill_constant([1], 'float32', np.NINF) + self.assertTrue(np.isinf(res.numpy().item(0))) + self.assertEqual(np.NINF, res.numpy().item(0)) + class TestFillConstantOpError(unittest.TestCase): def test_errors(self): -- GitLab