From ed96baeed19b4e11b6cbc2dcc6776245ba5fab13 Mon Sep 17 00:00:00 2001 From: RedContritio Date: Tue, 31 Jan 2023 07:55:22 +0000 Subject: [PATCH 1/2] check tensor numel in PyObject_CheckLongOrToLong --- paddle/fluid/pybind/op_function_common.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/pybind/op_function_common.cc b/paddle/fluid/pybind/op_function_common.cc index 5cdd9a0fa06688..4f9d6b26492709 100644 --- a/paddle/fluid/pybind/op_function_common.cc +++ b/paddle/fluid/pybind/op_function_common.cc @@ -30,6 +30,7 @@ #include "paddle/fluid/imperative/tracer.h" #include "paddle/fluid/imperative/type_defs.h" #include "paddle/fluid/operators/ops_extra_info.h" +#include "paddle/fluid/pybind/eager.h" #include "paddle/fluid/pybind/imperative.h" #include "paddle/phi/common/complex.h" @@ -70,7 +71,8 @@ bool PyObject_CheckLongOrToLong(PyObject** obj) { if ((PyLong_Check(*obj) && !PyBool_Check(*obj)) || PyObject_IsInstance(*obj, (PyObject*)g_vartype_pytype) || // NOLINT PyObject_IsInstance(*obj, (PyObject*)g_varbase_pytype) || // NOLINT - PyObject_IsInstance(*obj, (PyObject*)p_tensor_type)) { // NOLINT + (PyObject_IsInstance(*obj, (PyObject*)p_tensor_type) && // NOLINT + (((TensorObject*)(*obj))->tensor.numel() == 1))) { // NOLINT return true; } From 8e6e6d74d4ca12983f2f47ab2e1c6f86030a0d4a Mon Sep 17 00:00:00 2001 From: RedContritio Date: Tue, 31 Jan 2023 07:55:34 +0000 Subject: [PATCH 2/2] add unittest --- .../paddle/fluid/tests/unittests/test_flip.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/python/paddle/fluid/tests/unittests/test_flip.py b/python/paddle/fluid/tests/unittests/test_flip.py index 1807199821eb74..2bcc8a3dd4fc28 100644 --- a/python/paddle/fluid/tests/unittests/test_flip.py +++ b/python/paddle/fluid/tests/unittests/test_flip.py @@ -198,6 +198,23 @@ def test_grad(self): self.func(p) +class TestFlipError(unittest.TestCase): + def test_axis(self): + paddle.enable_static() + + def test_axis_rank(): + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.flip(input, axis=[[0]]) + + self.assertRaises(TypeError, test_axis_rank) + + def test_axis_rank2(): + input = fluid.data(name='input', dtype='float32', shape=[2, 3]) + output = paddle.flip(input, axis=[[0, 0], [1, 1]]) + + self.assertRaises(TypeError, test_axis_rank2) + + if __name__ == "__main__": paddle.enable_static() unittest.main()