diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index 9a535f5fb0492303e87a0e9c8bd71a63b498ce0c..a5c4bb1a804ec00f29f6d1d345b61c2f89824221 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -799,6 +799,10 @@ PYBIND11_MODULE(core_noavx, m) { framework_tensor .def("__array__", [](framework::Tensor &self) { return TensorToPyArray(self); }) + .def("_ptr", + [](const framework::Tensor &self) { + return reinterpret_cast(self.data()); + }) .def("_is_initialized", [](const framework::Tensor &self) { return self.IsInitialized(); }) .def("_get_dims", diff --git a/python/paddle/fluid/tests/unittests/test_tensor.py b/python/paddle/fluid/tests/unittests/test_tensor.py index f8f3eea78a6a2c8e0cf9b68886fd2ce718038023..da792903b7de82df2e3d153800cf83ea46c909ca 100644 --- a/python/paddle/fluid/tests/unittests/test_tensor.py +++ b/python/paddle/fluid/tests/unittests/test_tensor.py @@ -21,6 +21,14 @@ import numpy import numbers +class TestTensorPtr(unittest.TestCase): + def test_tensor_ptr(self): + t = core.Tensor() + np_arr = numpy.zeros([2, 3]) + t.set(np_arr, core.CPUPlace()) + self.assertGreater(t._ptr(), 0) + + class TestTensor(unittest.TestCase): def setUp(self): self.support_dtypes = [