diff --git a/paddle/fluid/pybind/eager.cc b/paddle/fluid/pybind/eager.cc index c27ad8289611fdf8facff900ca4f518f6b416ff7..a732ed0c1ec1438305bcaec4092185c38b58272b 100644 --- a/paddle/fluid/pybind/eager.cc +++ b/paddle/fluid/pybind/eager.cc @@ -667,15 +667,12 @@ int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { AutoInitTensorByPyArray( py_tensor_ptr, kws_map, args, flag_kwargs, args_num); return 0; - } else if (PyObject_IsInstance( - kw_value, reinterpret_cast(p_tensor_type))) { + } else if (PyObject_TypeCheck(kw_value, p_tensor_type)) { VLOG(6) << "Calling case5's or case6's initializer"; AutoInitTensorByTensor( py_tensor_ptr, kws_map, args, flag_kwargs, args_num); return 0; - } else if (PyObject_IsInstance(kw_value, - reinterpret_cast( - g_framework_tensor_pytype))) { + } else if (PyObject_TypeCheck(kw_value, g_framework_tensor_pytype)) { VLOG(6) << "Calling case7's initializer."; AutoInitTensorByTensor(py_tensor_ptr, kws_map, @@ -693,8 +690,7 @@ int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { "right way.")); } } else if (kw_dtype != NULL && - PyObject_IsInstance( - kw_dtype, reinterpret_cast(g_vartype_pytype))) { + PyObject_TypeCheck(kw_dtype, g_vartype_pytype)) { VLOG(6) << "Calling case2's initializer"; PADDLE_ENFORCE_NOT_NULL( @@ -770,15 +766,12 @@ int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { AutoInitTensorByPyArray( py_tensor_ptr, kws_map, args, flag_kwargs, args_num); return 0; - } else if (PyObject_IsInstance( - arg0_ptr, reinterpret_cast(p_tensor_type))) { + } else if (PyObject_TypeCheck(arg0_ptr, p_tensor_type)) { VLOG(6) << "Calling case5's or case6's initializer."; AutoInitTensorByTensor( py_tensor_ptr, kws_map, args, flag_kwargs, args_num); return 0; - } else if (PyObject_IsInstance( - arg0_ptr, - reinterpret_cast(g_framework_tensor_pytype))) { + } else if (PyObject_TypeCheck(arg0_ptr, g_framework_tensor_pytype)) { VLOG(6) << "Calling case7's initializer."; AutoInitTensorByTensor(py_tensor_ptr, kws_map, @@ -816,8 +809,7 @@ int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { } else if (args_num == (Py_ssize_t)5) { if (!flag_kwargs) { PyObject* arg0_ptr = PyTuple_GET_ITEM(args, 0); - if (PyObject_IsInstance(arg0_ptr, - reinterpret_cast(g_vartype_pytype))) { + if (PyObject_TypeCheck(arg0_ptr, g_vartype_pytype)) { VLOG(6) << "Calling case2's initializer."; paddle::framework::proto::VarType::Type dtype = CastPyArg2ProtoType(PyTuple_GET_ITEM(args, 0), 0); @@ -1007,9 +999,7 @@ int StringTensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { AutoInitStringTensorByPyArray( py_tensor_ptr, kws_map, args, flag_kwargs, args_num); return 0; - } else if (PyObject_IsInstance( - kw_value, - reinterpret_cast(p_string_tensor_type))) { + } else if (PyObject_TypeCheck(kw_value, p_string_tensor_type)) { VLOG(6) << "Calling case5's or case6's string initializer"; AutoInitStringTensorByStringTensor( py_tensor_ptr, kws_map, args, flag_kwargs, args_num); @@ -1057,9 +1047,7 @@ int StringTensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { AutoInitStringTensorByPyArray( py_tensor_ptr, kws_map, args, flag_kwargs, args_num); return 0; - } else if (PyObject_IsInstance( - arg0_ptr, - reinterpret_cast(p_string_tensor_type))) { + } else if (PyObject_TypeCheck(arg0_ptr, p_string_tensor_type)) { VLOG(6) << "Calling case5's or case6's string initializer."; AutoInitStringTensorByStringTensor( py_tensor_ptr, kws_map, args, flag_kwargs, args_num); @@ -1076,8 +1064,7 @@ int StringTensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { // 2 position args if (!flag_kwargs) { PyObject* arg0_ptr = PyTuple_GET_ITEM(args, 0); - if (PyObject_IsInstance( - arg0_ptr, reinterpret_cast(p_string_tensor_type))) { + if (PyObject_TypeCheck(arg0_ptr, p_string_tensor_type)) { VLOG(6) << "Calling case6's string initializer."; AutoInitStringTensorByStringTensor( py_tensor_ptr, kws_map, args, flag_kwargs, args_num); diff --git a/paddle/fluid/pybind/eager_method.cc b/paddle/fluid/pybind/eager_method.cc index 1d61c63c66df6a5064773471d8ab2c6a11214635..78cb53392f5fa098d922b2b77bbb734a6cc0047f 100644 --- a/paddle/fluid/pybind/eager_method.cc +++ b/paddle/fluid/pybind/eager_method.cc @@ -76,7 +76,7 @@ extern void InitTensorWithNumpyValue(TensorObject* self, extern PyTypeObject* p_tensor_type; Py_ssize_t GetSliceIndexFromPyObject(PyObject* obj) { - if (PyObject_IsInstance(obj, reinterpret_cast(p_tensor_type))) { + if (PyObject_TypeCheck(obj, p_tensor_type)) { VLOG(6) << "Call GetSliceIndexFromTensor in Eager"; paddle::Tensor tensor = CastPyArg2Tensor(obj, 0); PADDLE_ENFORCE_EQ( diff --git a/paddle/fluid/pybind/eager_utils.cc b/paddle/fluid/pybind/eager_utils.cc index 3f49622bd049110f03d6c87586f31ee59080c0ce..dd5a06757c3e453a9222788752506d21f8d11515 100644 --- a/paddle/fluid/pybind/eager_utils.cc +++ b/paddle/fluid/pybind/eager_utils.cc @@ -235,8 +235,7 @@ void SetPythonStack() { std::shared_ptr CastPyArg2JitFunction(PyObject* obj, ssize_t arg_pos) { - if (PyObject_IsInstance(obj, - reinterpret_cast(g_jit_function_pytype))) { + if (PyObject_TypeCheck(obj, g_jit_function_pytype)) { return ::pybind11::handle(obj).cast>(); } else { PADDLE_THROW(platform::errors::InvalidArgument( @@ -255,8 +254,7 @@ std::vector CastPyArg2VectorOfTensor(PyObject* obj, PyObject* item = nullptr; for (Py_ssize_t i = 0; i < len; i++) { item = PyList_GetItem(obj, i); - if (PyObject_IsInstance(item, - reinterpret_cast(p_tensor_type))) { + if (PyObject_TypeCheck(item, p_tensor_type)) { result.emplace_back(reinterpret_cast(item)->tensor); } else if (item == Py_None) { // emplace empty Tensor for None @@ -275,8 +273,7 @@ std::vector CastPyArg2VectorOfTensor(PyObject* obj, PyObject* item = nullptr; for (Py_ssize_t i = 0; i < len; i++) { item = PyTuple_GetItem(obj, i); - if (PyObject_IsInstance(item, - reinterpret_cast(p_tensor_type))) { + if (PyObject_TypeCheck(item, p_tensor_type)) { result.emplace_back(reinterpret_cast(item)->tensor); } else if (item == Py_None) { // emplace empty Tensor for None @@ -292,8 +289,7 @@ std::vector CastPyArg2VectorOfTensor(PyObject* obj, } } else if (obj == Py_None) { return {}; - } else if (PyObject_IsInstance(obj, - reinterpret_cast(p_tensor_type))) { + } else if (PyObject_TypeCheck(obj, p_tensor_type)) { return {reinterpret_cast(obj)->tensor}; } else { PADDLE_THROW(platform::errors::InvalidArgument( @@ -519,22 +515,17 @@ std::vector> CastPyArg2VectorOfVectorOfSize_t( platform::Place CastPyArg2Place(PyObject* obj, ssize_t arg_pos) { platform::Place place; - if (PyObject_IsInstance(obj, reinterpret_cast(g_place_pytype))) { + if (PyObject_TypeCheck(obj, g_place_pytype)) { place = ::pybind11::handle(obj).cast(); - } else if (PyObject_IsInstance( - obj, reinterpret_cast(g_cudaplace_pytype))) { + } else if (PyObject_TypeCheck(obj, g_cudaplace_pytype)) { place = ::pybind11::handle(obj).cast(); - } else if (PyObject_IsInstance( - obj, reinterpret_cast(g_cpuplace_pytype))) { + } else if (PyObject_TypeCheck(obj, g_cpuplace_pytype)) { place = ::pybind11::handle(obj).cast(); - } else if (PyObject_IsInstance( - obj, reinterpret_cast(g_xpuplace_pytype))) { + } else if (PyObject_TypeCheck(obj, g_xpuplace_pytype)) { place = ::pybind11::handle(obj).cast(); - } else if (PyObject_IsInstance( - obj, reinterpret_cast(g_cudapinnedplace_pytype))) { + } else if (PyObject_TypeCheck(obj, g_cudapinnedplace_pytype)) { place = ::pybind11::handle(obj).cast(); - } else if (PyObject_IsInstance( - obj, reinterpret_cast(g_customplace_pytype))) { + } else if (PyObject_TypeCheck(obj, g_customplace_pytype)) { place = ::pybind11::handle(obj).cast(); } else { PADDLE_THROW(platform::errors::InvalidArgument( @@ -550,8 +541,7 @@ platform::Place CastPyArg2Place(PyObject* obj, ssize_t arg_pos) { } phi::DenseTensor CastPyArg2FrameworkTensor(PyObject* obj, ssize_t arg_pos) { - if (PyObject_IsInstance( - obj, reinterpret_cast(g_framework_tensor_pytype))) { + if (PyObject_TypeCheck(obj, g_framework_tensor_pytype)) { return ::pybind11::handle(obj).cast(); } else { PADDLE_THROW(platform::errors::InvalidArgument( @@ -570,8 +560,7 @@ std::vector CastPyArg2VectorOfTensorBase(PyObject* obj, PyObject* item = nullptr; for (Py_ssize_t i = 0; i < len; i++) { item = PyList_GetItem(obj, i); - if (PyObject_IsInstance( - item, reinterpret_cast(g_framework_tensor_pytype))) { + if (PyObject_TypeCheck(item, g_framework_tensor_pytype)) { result.emplace_back(::pybind11::handle(item).cast()); } else { PADDLE_THROW(platform::errors::InvalidArgument( @@ -587,8 +576,7 @@ std::vector CastPyArg2VectorOfTensorBase(PyObject* obj, PyObject* item = nullptr; for (Py_ssize_t i = 0; i < len; i++) { item = PyTuple_GetItem(obj, i); - if (PyObject_IsInstance( - item, reinterpret_cast(g_framework_tensor_pytype))) { + if (PyObject_TypeCheck(item, g_framework_tensor_pytype)) { result.emplace_back(::pybind11::handle(item).cast()); } else { PADDLE_THROW(platform::errors::InvalidArgument( @@ -599,17 +587,14 @@ std::vector CastPyArg2VectorOfTensorBase(PyObject* obj, i)); } } - } else if (PyObject_IsInstance(obj, - reinterpret_cast( - g_framework_lodtensorarray_pytype))) { + } else if (PyObject_TypeCheck(obj, g_framework_lodtensorarray_pytype)) { for (auto& tensor : (::pybind11::handle(obj).cast())) { result.emplace_back(tensor); } } else if (obj == Py_None) { return {}; - } else if (PyObject_IsInstance( - obj, reinterpret_cast(g_framework_tensor_pytype))) { + } else if (PyObject_TypeCheck(obj, g_framework_tensor_pytype)) { return {::pybind11::handle(obj).cast()}; } else { PADDLE_THROW(platform::errors::InvalidArgument( @@ -624,7 +609,7 @@ std::vector CastPyArg2VectorOfTensorBase(PyObject* obj, paddle::framework::proto::VarType::Type CastPyArg2ProtoType(PyObject* obj, ssize_t arg_pos) { paddle::framework::proto::VarType::Type dtype; - if (PyObject_IsInstance(obj, reinterpret_cast(g_vartype_pytype))) { + if (PyObject_TypeCheck(obj, g_vartype_pytype)) { dtype = ::pybind11::handle(obj).cast(); } else { @@ -973,7 +958,7 @@ paddle::optional GetOptionalTensorFromArgs( return paddle::none; } - if (PyObject_IsInstance(obj, reinterpret_cast(p_tensor_type))) { + if (PyObject_TypeCheck(obj, p_tensor_type)) { return paddle::make_optional( reinterpret_cast(obj)->tensor); } else { @@ -1007,10 +992,9 @@ static paddle::Tensor& GetTensorFromPyObject(const std::string& op_type, return emptytensor; } - if (PyObject_IsInstance(obj, reinterpret_cast(p_tensor_type))) { + if (PyObject_TypeCheck(obj, p_tensor_type)) { return reinterpret_cast(obj)->tensor; - } else if (PyObject_IsInstance( - obj, reinterpret_cast(p_string_tensor_type))) { + } else if (PyObject_TypeCheck(obj, p_string_tensor_type)) { return reinterpret_cast(obj)->tensor; } else { PADDLE_THROW(platform::errors::InvalidArgument( @@ -1187,7 +1171,7 @@ paddle::Tensor* GetTensorPtrFromArgs(const std::string& op_type, return &emptytensor; } - if (PyObject_IsInstance(obj, reinterpret_cast(p_tensor_type))) { + if (PyObject_TypeCheck(obj, p_tensor_type)) { return &(reinterpret_cast(obj)->tensor); } else { PADDLE_THROW(platform::errors::InvalidArgument( @@ -1305,8 +1289,7 @@ std::vector GetTensorListFromPyObject(PyObject* obj, PyObject* item = nullptr; for (Py_ssize_t i = 0; i < len; i++) { item = PyList_GetItem(obj, i); - if (PyObject_IsInstance(item, - reinterpret_cast(p_tensor_type))) { + if (PyObject_TypeCheck(item, p_tensor_type)) { result.emplace_back(reinterpret_cast(item)->tensor); } else if (allow_none && (item == Py_None)) { VLOG(4) << "Got None in Tensor list: " << i; @@ -1324,8 +1307,7 @@ std::vector GetTensorListFromPyObject(PyObject* obj, PyObject* item = nullptr; for (Py_ssize_t i = 0; i < len; i++) { item = PyTuple_GetItem(obj, i); - if (PyObject_IsInstance(item, - reinterpret_cast(p_tensor_type))) { + if (PyObject_TypeCheck(item, p_tensor_type)) { result.emplace_back(reinterpret_cast(item)->tensor); } else if (allow_none && (item == Py_None)) { VLOG(4) << "Got None in Tensor list: " << i; @@ -1548,8 +1530,7 @@ paddle::experimental::IntArray CastPyArg2IntArray(PyObject* obj, } paddle::framework::Scope* CastPyArg2ScopePtr(PyObject* obj) { - if (PyObject_IsInstance( - obj, reinterpret_cast(g_framework_scope_pytype))) { + if (PyObject_TypeCheck(obj, g_framework_scope_pytype)) { return ::pybind11::handle(obj).cast(); } else { PADDLE_THROW(platform::errors::InvalidArgument( diff --git a/paddle/fluid/pybind/op_function_common.cc b/paddle/fluid/pybind/op_function_common.cc index 86dea0d145afea8d240e8177977f92842ea05146..b4b96c02bb2271009a668d4232a62d9039e8a5b1 100644 --- a/paddle/fluid/pybind/op_function_common.cc +++ b/paddle/fluid/pybind/op_function_common.cc @@ -70,10 +70,10 @@ bool PyObject_CheckBool(PyObject** obj) { return PyBool_Check(*obj); } 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 - (((TensorObject*)(*obj))->tensor.numel() == 1))) { // NOLINT + PyObject_TypeCheck(*obj, g_vartype_pytype) || // NOLINT + PyObject_TypeCheck(*obj, g_varbase_pytype) || // NOLINT + (PyObject_TypeCheck(*obj, p_tensor_type) && // NOLINT + (((TensorObject*)(*obj))->tensor.numel() == 1))) { // NOLINT return true; } @@ -92,9 +92,9 @@ bool PyObject_CheckLongOrToLong(PyObject** obj) { bool PyObject_CheckFloatOrToFloat(PyObject** obj) { // sometimes users provide PyLong or numpy.int64 but attr is float if (PyFloat_Check(*obj) || PyLong_Check(*obj) || - PyObject_IsInstance(*obj, (PyObject*)g_varbase_pytype) || // NOLINT - (PyObject_IsInstance(*obj, (PyObject*)p_tensor_type) && // NOLINT - (((TensorObject*)(*obj))->tensor.numel() == 1))) { // NOLINT + PyObject_TypeCheck(*obj, g_varbase_pytype) || // NOLINT + (PyObject_TypeCheck(*obj, p_tensor_type) && // NOLINT + (((TensorObject*)(*obj))->tensor.numel() == 1))) { // NOLINT return true; } if (std::string(((PyTypeObject*)(*obj)->ob_type)->tp_name) // NOLINT @@ -110,9 +110,9 @@ bool PyObject_CheckFloatOrToFloat(PyObject** obj) { bool PyObject_CheckComplexOrToComplex(PyObject** obj) { if (PyComplex_Check(*obj) || PyLong_Check(*obj) || PyFloat_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_TypeCheck(*obj, g_vartype_pytype) || // NOLINT + PyObject_TypeCheck(*obj, g_varbase_pytype) || // NOLINT + PyObject_TypeCheck(*obj, p_tensor_type)) { // NOLINT return true; } // consider numpy cfloat & numpy cdouble? @@ -821,8 +821,7 @@ void CastPyArg2AttrBlock(PyObject* obj, ::pybind11::detail::instance* inst = (::pybind11::detail::instance*)obj; // NOLINT - if (!PyObject_IsInstance((PyObject*)inst, // NOLINT - (PyObject*)g_blockdesc_pytype)) { // NOLINT + if (!PyObject_TypeCheck((PyObject*)inst, g_blockdesc_pytype)) { // NOLINT PADDLE_THROW(platform::errors::InvalidArgument( "%s(): argument (position %d) must be " "BlockDesc, but got %s", @@ -951,8 +950,7 @@ std::shared_ptr GetVarBaseFromArgs( return nullptr; } - if (!PyObject_IsInstance((PyObject*)inst, // NOLINT - (PyObject*)g_varbase_pytype)) { // NOLINT + if (!PyObject_TypeCheck((PyObject*)inst, g_varbase_pytype)) { // NOLINT PADDLE_THROW(platform::errors::InvalidArgument( "%s(): argument '%s' (position %d) must be Tensor, but got " "%s", @@ -1002,8 +1000,7 @@ std::vector> GetVarBaseListFromArgs( ::pybind11::detail::instance* item = nullptr; for (Py_ssize_t i = 0; i < len; i++) { item = (::pybind11::detail::instance*)PyList_GetItem(list, i); - if (!PyObject_IsInstance((PyObject*)item, // NOLINT - (PyObject*)g_varbase_pytype)) { // NOLINT + if (!PyObject_TypeCheck((PyObject*)item, g_varbase_pytype)) { // NOLINT PADDLE_THROW(platform::errors::InvalidArgument( "%s(): argument '%s' (position %d) must be list of Tensors, but " "got list of " @@ -1032,8 +1029,7 @@ std::vector> GetVarBaseListFromArgs( ::pybind11::detail::instance* item = nullptr; for (Py_ssize_t i = 0; i < len; i++) { item = (::pybind11::detail::instance*)PyTuple_GetItem(list, i); // NOLINT - if (!PyObject_IsInstance((PyObject*)item, // NOLINT - (PyObject*)g_varbase_pytype)) { // NOLINT + if (!PyObject_TypeCheck((PyObject*)item, g_varbase_pytype)) { // NOLINT PADDLE_THROW(platform::errors::InvalidArgument( "%s(): argument '%s' (position %d) must be list of Tensors, but " "got list of " diff --git a/paddle/utils/pybind.cc b/paddle/utils/pybind.cc index 24f4c2bd22a8d82d1ad596219e90d4411891f8ce..b120414fd96cfd334becbb4c13e51b8ecf59c38a 100644 --- a/paddle/utils/pybind.cc +++ b/paddle/utils/pybind.cc @@ -23,18 +23,18 @@ namespace pybind { PyTypeObject* p_tensor_type = nullptr; PyTypeObject* p_string_tensor_type = nullptr; - +// PyTypeObject* PyFloat_Type; +PyTypeObject* PyLong_Type; bool PyCheckTensor(PyObject* obj) { if (!p_tensor_type) { return false; } - return PyObject_IsInstance(obj, reinterpret_cast(p_tensor_type)); + return PyObject_TypeCheck(obj, p_tensor_type); } paddle::Tensor CastPyArg2Tensor(PyObject* obj, Py_ssize_t arg_pos) { - if (PyObject_IsInstance(obj, reinterpret_cast(p_tensor_type)) || - PyObject_IsInstance(obj, - reinterpret_cast(p_string_tensor_type))) { + if (PyObject_TypeCheck(obj, p_tensor_type) || + PyObject_TypeCheck(obj, p_string_tensor_type)) { return reinterpret_cast(obj)->tensor; } else { PADDLE_THROW(phi::errors::InvalidArgument(