From 79c25979773b77902ecb5b2d9f918ad8f9bcaf76 Mon Sep 17 00:00:00 2001 From: wanghuancoder Date: Tue, 7 Dec 2021 12:24:18 +0800 Subject: [PATCH] [Eager] fix cmake generate error, and fix circular import (#37871) * refine a test case, test=develop * rm python, test=develop * refine, test=develop * fix cmake generate error, and fix circular import, test=develop --- paddle/fluid/pybind/eager_method.cc | 2 +- paddle/fluid/pybind/eager_utils.cc | 34 +++++++++++++++++++ paddle/fluid/pybind/eager_utils.h | 2 ++ paddle/pten/core/CMakeLists.txt | 6 ++-- paddle/pten/core/convert_utils.cc | 34 ------------------- paddle/pten/core/convert_utils.h | 1 - .../fluid/eager/eager_tensor_patch_methods.py | 2 +- 7 files changed, 41 insertions(+), 40 deletions(-) diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index f040566260c..e01396a4e3c 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -42,7 +42,7 @@ static PyObject* eager_tensor_method_numpy(EagerTensorObject* self, return Py_None; } auto tensor_dims = self->eagertensor.shape(); - auto numpy_dtype = pten::TensorDtype2NumpyDtype(self->eagertensor.type()); + auto numpy_dtype = TensorDtype2NumpyDtype(self->eagertensor.type()); auto sizeof_dtype = pten::DataTypeSize(self->eagertensor.type()); Py_intptr_t py_dims[paddle::framework::DDim::kMaxRank]; Py_intptr_t py_strides[paddle::framework::DDim::kMaxRank]; diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index 9268fc8e7b9..c8b6f2c06c7 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -17,9 +17,11 @@ limitations under the License. */ #include "paddle/fluid/eager/api/all.h" #include "paddle/fluid/eager/autograd_meta.h" #include "paddle/fluid/memory/allocation/allocator.h" +#include "paddle/fluid/operators/py_func_op.h" #include "paddle/fluid/platform/enforce.h" #include "paddle/fluid/pybind/eager.h" #include "paddle/fluid/pybind/eager_utils.h" +#include "paddle/fluid/pybind/tensor_py.h" #include "paddle/pten/common/data_type.h" #include "paddle/pten/core/convert_utils.h" #include "paddle/pten/core/dense_tensor.h" @@ -37,6 +39,38 @@ extern PyTypeObject* g_xpuplace_pytype; extern PyTypeObject* g_npuplace_pytype; extern PyTypeObject* g_cudapinnedplace_pytype; +int TensorDtype2NumpyDtype(pten::DataType dtype) { + switch (dtype) { + case pten::DataType::BOOL: + return pybind11::detail::npy_api::NPY_BOOL_; + case pten::DataType::INT8: + return pybind11::detail::npy_api::NPY_INT8_; + case pten::DataType::UINT8: + return pybind11::detail::npy_api::NPY_UINT8_; + case pten::DataType::INT16: + return pybind11::detail::npy_api::NPY_INT16_; + case pten::DataType::INT32: + return pybind11::detail::npy_api::NPY_INT32_; + case pten::DataType::INT64: + return pybind11::detail::npy_api::NPY_INT64_; + case pten::DataType::FLOAT16: + return pybind11::detail::NPY_FLOAT16_; + case pten::DataType::FLOAT32: + return pybind11::detail::npy_api::NPY_FLOAT_; + case pten::DataType::FLOAT64: + return pybind11::detail::npy_api::NPY_DOUBLE_; + case pten::DataType::COMPLEX64: + return pybind11::detail::NPY_COMPLEX64; + case pten::DataType::COMPLEX128: + return pybind11::detail::NPY_COMPLEX128; + default: + PADDLE_THROW(paddle::platform::errors::InvalidArgument( + "Unknow pten::DataType, the int value = %d.", + static_cast(dtype))); + return 0; + } +} + bool PyObject_CheckLongOrConvertToLong(PyObject** obj) { if ((PyLong_Check(*obj) && !PyBool_Check(*obj))) { return true; diff --git a/paddle/fluid/pybind/eager_utils.h b/paddle/fluid/pybind/eager_utils.h index 49f56a61c31..f311e62b896 100644 --- a/paddle/fluid/pybind/eager_utils.h +++ b/paddle/fluid/pybind/eager_utils.h @@ -21,6 +21,8 @@ typedef struct { PyObject_HEAD egr::EagerTensor eagertensor; } EagerTensorObject; +int TensorDtype2NumpyDtype(pten::DataType dtype); + bool PyObject_CheckLongOrConvertToLong(PyObject** obj); bool PyObject_CheckFloatOrConvertToFloat(PyObject** obj); bool PyObject_CheckStr(PyObject* obj); diff --git a/paddle/pten/core/CMakeLists.txt b/paddle/pten/core/CMakeLists.txt index 0a2504f5032..e19d0a490ce 100644 --- a/paddle/pten/core/CMakeLists.txt +++ b/paddle/pten/core/CMakeLists.txt @@ -1,9 +1,9 @@ if(WITH_GPU) - cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place gpu_info python) + cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place gpu_info) elseif(WITH_ROCM) - cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place gpu_info python) + cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place gpu_info) else() - cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place python) + cc_library(convert_utils SRCS convert_utils.cc DEPS data_type place) endif() cc_library(kernel_factory SRCS kernel_factory.cc DEPS enforce) diff --git a/paddle/pten/core/convert_utils.cc b/paddle/pten/core/convert_utils.cc index e457c57d59e..211734f3315 100644 --- a/paddle/pten/core/convert_utils.cc +++ b/paddle/pten/core/convert_utils.cc @@ -12,8 +12,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include "paddle/pten/core/convert_utils.h" -#include "paddle/fluid/operators/py_func_op.h" -#include "paddle/fluid/pybind/tensor_py.h" // See Note [ Why still include the fluid headers? ] #include "paddle/fluid/platform/device/gpu/gpu_info.h" @@ -272,36 +270,4 @@ std::string DataType2String(DataType dtype) { } } -int TensorDtype2NumpyDtype(pten::DataType dtype) { - switch (dtype) { - case pten::DataType::BOOL: - return pybind11::detail::npy_api::NPY_BOOL_; - case pten::DataType::INT8: - return pybind11::detail::npy_api::NPY_INT8_; - case pten::DataType::UINT8: - return pybind11::detail::npy_api::NPY_UINT8_; - case pten::DataType::INT16: - return pybind11::detail::npy_api::NPY_INT16_; - case pten::DataType::INT32: - return pybind11::detail::npy_api::NPY_INT32_; - case pten::DataType::INT64: - return pybind11::detail::npy_api::NPY_INT64_; - case pten::DataType::FLOAT16: - return pybind11::detail::NPY_FLOAT16_; - case pten::DataType::FLOAT32: - return pybind11::detail::npy_api::NPY_FLOAT_; - case pten::DataType::FLOAT64: - return pybind11::detail::npy_api::NPY_DOUBLE_; - case pten::DataType::COMPLEX64: - return pybind11::detail::NPY_COMPLEX64; - case pten::DataType::COMPLEX128: - return pybind11::detail::NPY_COMPLEX128; - default: - PADDLE_THROW(paddle::platform::errors::InvalidArgument( - "Unknow pten::DataType, the int value = %d.", - static_cast(dtype))); - return 0; - } -} - } // namespace pten diff --git a/paddle/pten/core/convert_utils.h b/paddle/pten/core/convert_utils.h index e5990eb0a89..32ed753b4b0 100644 --- a/paddle/pten/core/convert_utils.h +++ b/paddle/pten/core/convert_utils.h @@ -48,6 +48,5 @@ pten::LoD TransToPtenLoD(const paddle::framework::LoD& lod); size_t DataTypeSize(DataType dtype); DataType String2DataType(const std::string& str); std::string DataType2String(DataType dtype); -int TensorDtype2NumpyDtype(pten::DataType dtype); } // namespace pten diff --git a/python/paddle/fluid/eager/eager_tensor_patch_methods.py b/python/paddle/fluid/eager/eager_tensor_patch_methods.py index 206c5cf23e6..b61bf78116a 100644 --- a/python/paddle/fluid/eager/eager_tensor_patch_methods.py +++ b/python/paddle/fluid/eager/eager_tensor_patch_methods.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import paddle.fluid.core as core +from .. import core as core def monkey_patch_eagertensor(): -- GitLab