From ccafd2e577c31971358597fee4867ec3ec7e910b Mon Sep 17 00:00:00 2001 From: ronnywang <524019753@qq.com> Date: Sun, 24 Apr 2022 14:51:35 +0800 Subject: [PATCH] [CustomDevice] add eager mode support (#42034) --- paddle/fluid/pybind/eager.cc | 5 ++++- paddle/fluid/pybind/eager_utils.cc | 8 +++++++- paddle/fluid/pybind/pybind.cc | 9 ++++++--- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/paddle/fluid/pybind/eager.cc b/paddle/fluid/pybind/eager.cc index 8695928205b..6601c8e8e3e 100644 --- a/paddle/fluid/pybind/eager.cc +++ b/paddle/fluid/pybind/eager.cc @@ -146,10 +146,13 @@ void InitTensorWithNumpyValue(TensorObject* self, const py::object& array, zero_copy); } else if (platform::is_npu_place(place)) { SetTensorFromPyArray(impl_ptr, array, place, zero_copy); + } else if (platform::is_custom_place(place)) { + SetTensorFromPyArray(impl_ptr, array, place, + zero_copy); } else { PADDLE_THROW(platform::errors::InvalidArgument( "Place should be one of " - "CPUPlace/XPUPlace/CUDAPlace/CUDAPinnedPlace/NPUPlace")); + "CPUPlace/XPUPlace/CUDAPlace/CUDAPinnedPlace/NPUPlace/CustomPlace")); } } diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index 78db1a6f1b9..b3912748433 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -46,6 +46,7 @@ extern PyTypeObject* g_cpuplace_pytype; extern PyTypeObject* g_xpuplace_pytype; extern PyTypeObject* g_npuplace_pytype; extern PyTypeObject* g_cudapinnedplace_pytype; +extern PyTypeObject* g_customplace_pytype; extern PyTypeObject* g_framework_tensor_pytype; extern PyTypeObject* g_framework_lodtensorarray_pytype; extern PyTypeObject* g_custom_op_kernel_ctx_pytype; @@ -377,10 +378,15 @@ platform::Place CastPyArg2Place(PyObject* obj, ssize_t arg_pos) { } else if (PyObject_IsInstance( obj, reinterpret_cast(g_cudapinnedplace_pytype))) { place = ::pybind11::handle(obj).cast(); + } else if (PyObject_IsInstance( + obj, reinterpret_cast(g_customplace_pytype))) { + place = ::pybind11::handle(obj).cast(); } else { PADDLE_THROW(platform::errors::InvalidArgument( "argument (position %d) must be " - "one of(Place,CUDAPlace,CPUPlace,XPUPlace,NPUPlace,CUDAPinnedPlace), " + "one " + "of(Place,CUDAPlace,CPUPlace,XPUPlace,NPUPlace,CUDAPinnedPlace," + "CustomPlace), " "but got %s", arg_pos + 1, reinterpret_cast(obj->ob_type)->tp_name)); } diff --git a/paddle/fluid/pybind/pybind.cc b/paddle/fluid/pybind/pybind.cc index 79ed7d9a08d..dc380f83bf7 100644 --- a/paddle/fluid/pybind/pybind.cc +++ b/paddle/fluid/pybind/pybind.cc @@ -193,6 +193,7 @@ PyTypeObject *g_xpuplace_pytype = nullptr; PyTypeObject *g_npuplace_pytype = nullptr; PyTypeObject *g_cudapinnedplace_pytype = nullptr; PyTypeObject *g_mluplace_pytype = nullptr; +PyTypeObject *g_customplace_pytype = nullptr; PyTypeObject *g_framework_tensor_pytype = nullptr; PyTypeObject *g_framework_lodtensorarray_pytype = nullptr; PyTypeObject *g_custom_op_kernel_ctx_pytype = nullptr; @@ -2125,8 +2126,8 @@ All parameter, weight, gradient are variables in Paddle. #endif return devices; }); - py::class_(m, "CustomPlace", - R"DOC( + py::class_ customplace(m, "CustomPlace", + R"DOC( CustomPlace is a descriptor of a device. It represents a custom device on which a tensor will be allocated and a model will run. @@ -2135,7 +2136,9 @@ All parameter, weight, gradient are variables in Paddle. import paddle fake_cpu_place = paddle.CustomPlace("FakeCPU", 0) - )DOC") + )DOC"); + g_customplace_pytype = reinterpret_cast(customplace.ptr()); + customplace .def("__init__", [](platform::CustomPlace &self, const std::string &device_type, int dev_id) { -- GitLab