From 983fcb56211197491750e000154bea7904dbd0cb Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Tue, 26 Apr 2022 13:01:23 +0800 Subject: [PATCH] [Eager] Support numpy.ndarry in CastNumpy2Scalar (#42136) (#42213) --- paddle/fluid/pybind/eager_utils.cc | 15 ++++++++++++++- python/paddle/fluid/tests/unittests/test_bfgs.py | 8 +++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index b0e62cdc51..452aa0ce2d 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -1019,7 +1019,20 @@ paddle::experimental::Scalar CastNumpy2Scalar(PyObject* obj, PyTypeObject* type = obj->ob_type; auto type_name = std::string(type->tp_name); VLOG(1) << "type_name: " << type_name; - if (type_name == "numpy.float64") { + if (type_name == "numpy.ndarray" && PySequence_Check(obj)) { + PyObject* item = nullptr; + item = PySequence_GetItem(obj, 0); + if (PyObject_CheckFloatOrToFloat(&item)) { + float value = static_cast(PyFloat_AsDouble(item)); + return paddle::experimental::Scalar(value); + } else { + PADDLE_THROW(platform::errors::InvalidArgument( + "%s(): argument (position %d) is numpy.ndarry, the inner elements " + "must be " + "numpy.float32/float64 now, but got %s", + op_type, arg_pos + 1, type_name)); // NOLINT + } + } else if (type_name == "numpy.float64") { double value = CastPyArg2Double(obj, op_type, arg_pos); return paddle::experimental::Scalar(value); } else if (type_name == "numpy.float32") { diff --git a/python/paddle/fluid/tests/unittests/test_bfgs.py b/python/paddle/fluid/tests/unittests/test_bfgs.py index c89f7205f0..8a9f9f72aa 100644 --- a/python/paddle/fluid/tests/unittests/test_bfgs.py +++ b/python/paddle/fluid/tests/unittests/test_bfgs.py @@ -20,6 +20,7 @@ import paddle import paddle.nn.functional as F from paddle.incubate.optimizer.functional.bfgs import minimize_bfgs +from paddle.fluid.framework import _test_eager_guard np.random.seed(123) @@ -117,7 +118,7 @@ class TestBfgs(unittest.TestCase): results = test_static_graph(func, x0, dtype='float64') self.assertTrue(np.allclose(0.8, results[2])) - def test_rosenbrock(self): + def func_rosenbrock(self): # The Rosenbrock function is a standard optimization test case. a = np.random.random(size=[1]).astype('float32') minimum = [a.item(), (a**2).item()] @@ -136,6 +137,11 @@ class TestBfgs(unittest.TestCase): results = test_dynamic_graph(func, x0) self.assertTrue(np.allclose(minimum, results[2])) + def test_rosenbrock(self): + with _test_eager_guard(): + self.func_rosenbrock() + self.func_rosenbrock() + def test_exception(self): def func(x): return paddle.dot(x, x) -- GitLab