From 50f8e974589e87e7785301c34a211bba3eb454d1 Mon Sep 17 00:00:00 2001 From: Weilong Wu Date: Mon, 4 Apr 2022 16:39:24 +0800 Subject: [PATCH] [Eager] Support test_var_base _offset in eager mode (#41369) * [Eager]Polish enable/disable_legacy_dygraph logic * Support _offset in eager mode * Update framework.py * Update framework.py Co-authored-by: Aurelius84 --- paddle/fluid/pybind/eager_method.cc | 15 +++++++++++++++ paddle/fluid/pybind/eager_utils.cc | 2 ++ paddle/fluid/pybind/eager_utils.h | 1 + .../paddle/fluid/tests/unittests/test_var_base.py | 7 ++++++- 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 1a7eb629a0..dfe2fab9fc 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -1344,6 +1344,19 @@ static PyObject* tensor__reset_grad_inplace_version(TensorObject* self, EAGER_CATCH_AND_THROW_RETURN_NULL } +static PyObject* tensor__offset(TensorObject* self, PyObject* args, + PyObject* kwargs) { + EAGER_TRY + auto t = std::dynamic_pointer_cast(self->tensor.impl()); + PADDLE_ENFORCE_EQ( + t->IsInitialized(), true, + platform::errors::InvalidArgument("Tensor %s has not been initialized!", + self->tensor.name())); + + return ToPyObject(t->offset()); + EAGER_CATCH_AND_THROW_RETURN_NULL +} + #if defined(PADDLE_WITH_CUDA) static PyObject* tensor_method__uva(TensorObject* self, PyObject* args, PyObject* kwargs) { @@ -1472,6 +1485,8 @@ PyMethodDef variable_methods[] = { {"_reset_grad_inplace_version", (PyCFunction)(void (*)(void))tensor__reset_grad_inplace_version, METH_VARARGS | METH_KEYWORDS, NULL}, + {"_offset", (PyCFunction)(void (*)(void))tensor__offset, + METH_VARARGS | METH_KEYWORDS, NULL}, #if defined(PADDLE_WITH_CUDA) {"_tensor_uva", (PyCFunction)(void (*)(void))tensor_method__uva, METH_VARARGS | METH_KEYWORDS, NULL}, diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index bdc96e85e4..a6047f36ad 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -426,6 +426,8 @@ PyObject* ToPyObject(int value) { return PyLong_FromLong(value); } PyObject* ToPyObject(uint32_t value) { return PyLong_FromUnsignedLong(value); } +PyObject* ToPyObject(size_t value) { return PyLong_FromLong(value); } + PyObject* ToPyObject(int64_t value) { return PyLong_FromLongLong(value); } PyObject* ToPyObject(float value) { return PyLong_FromDouble(value); } diff --git a/paddle/fluid/pybind/eager_utils.h b/paddle/fluid/pybind/eager_utils.h index bd78342e21..2fe73c24ee 100644 --- a/paddle/fluid/pybind/eager_utils.h +++ b/paddle/fluid/pybind/eager_utils.h @@ -55,6 +55,7 @@ framework::proto::VarType::Type CastPyArg2ProtoType(PyObject* obj, PyObject* ToPyObject(int value); PyObject* ToPyObject(uint32_t value); +PyObject* ToPyObject(size_t value); PyObject* ToPyObject(bool value); PyObject* ToPyObject(int64_t value); PyObject* ToPyObject(float value); diff --git a/python/paddle/fluid/tests/unittests/test_var_base.py b/python/paddle/fluid/tests/unittests/test_var_base.py index b426b0d810..11d77ecc62 100644 --- a/python/paddle/fluid/tests/unittests/test_var_base.py +++ b/python/paddle/fluid/tests/unittests/test_var_base.py @@ -1396,7 +1396,7 @@ class TestVarBaseClear(unittest.TestCase): class TestVarBaseOffset(unittest.TestCase): - def test_offset(self): + def func_offset(self): paddle.disable_static() np_x = np.random.random((3, 8, 8)) x = paddle.to_tensor(np_x, dtype="float64") @@ -1405,6 +1405,11 @@ class TestVarBaseOffset(unittest.TestCase): actual_x = paddle.to_tensor(actual_x) self.assertEqual(actual_x._offset(), expected_offset) + def test_offset(self): + with _test_eager_guard(): + self.func_offset() + self.func_offset() + class TestVarBaseShareBufferTo(unittest.TestCase): def test_share_buffer_To(self): -- GitLab