未验证 提交 1b33f5c9 编写于 作者: X xiaoguoguo626807 提交者: GitHub

【dygraph】replace Pyobject_isinstance as pyobject_typecheck (#54297)

* change pyobject_isinstance

* change pyobject_isinstance
上级 6310419b
...@@ -667,15 +667,12 @@ int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { ...@@ -667,15 +667,12 @@ int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) {
AutoInitTensorByPyArray( AutoInitTensorByPyArray(
py_tensor_ptr, kws_map, args, flag_kwargs, args_num); py_tensor_ptr, kws_map, args, flag_kwargs, args_num);
return 0; return 0;
} else if (PyObject_IsInstance( } else if (PyObject_TypeCheck(kw_value, p_tensor_type)) {
kw_value, reinterpret_cast<PyObject*>(p_tensor_type))) {
VLOG(6) << "Calling case5's or case6's initializer"; VLOG(6) << "Calling case5's or case6's initializer";
AutoInitTensorByTensor( AutoInitTensorByTensor(
py_tensor_ptr, kws_map, args, flag_kwargs, args_num); py_tensor_ptr, kws_map, args, flag_kwargs, args_num);
return 0; return 0;
} else if (PyObject_IsInstance(kw_value, } else if (PyObject_TypeCheck(kw_value, g_framework_tensor_pytype)) {
reinterpret_cast<PyObject*>(
g_framework_tensor_pytype))) {
VLOG(6) << "Calling case7's initializer."; VLOG(6) << "Calling case7's initializer.";
AutoInitTensorByTensor(py_tensor_ptr, AutoInitTensorByTensor(py_tensor_ptr,
kws_map, kws_map,
...@@ -693,8 +690,7 @@ int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { ...@@ -693,8 +690,7 @@ int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) {
"right way.")); "right way."));
} }
} else if (kw_dtype != NULL && } else if (kw_dtype != NULL &&
PyObject_IsInstance( PyObject_TypeCheck(kw_dtype, g_vartype_pytype)) {
kw_dtype, reinterpret_cast<PyObject*>(g_vartype_pytype))) {
VLOG(6) << "Calling case2's initializer"; VLOG(6) << "Calling case2's initializer";
PADDLE_ENFORCE_NOT_NULL( PADDLE_ENFORCE_NOT_NULL(
...@@ -770,15 +766,12 @@ int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { ...@@ -770,15 +766,12 @@ int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) {
AutoInitTensorByPyArray( AutoInitTensorByPyArray(
py_tensor_ptr, kws_map, args, flag_kwargs, args_num); py_tensor_ptr, kws_map, args, flag_kwargs, args_num);
return 0; return 0;
} else if (PyObject_IsInstance( } else if (PyObject_TypeCheck(arg0_ptr, p_tensor_type)) {
arg0_ptr, reinterpret_cast<PyObject*>(p_tensor_type))) {
VLOG(6) << "Calling case5's or case6's initializer."; VLOG(6) << "Calling case5's or case6's initializer.";
AutoInitTensorByTensor( AutoInitTensorByTensor(
py_tensor_ptr, kws_map, args, flag_kwargs, args_num); py_tensor_ptr, kws_map, args, flag_kwargs, args_num);
return 0; return 0;
} else if (PyObject_IsInstance( } else if (PyObject_TypeCheck(arg0_ptr, g_framework_tensor_pytype)) {
arg0_ptr,
reinterpret_cast<PyObject*>(g_framework_tensor_pytype))) {
VLOG(6) << "Calling case7's initializer."; VLOG(6) << "Calling case7's initializer.";
AutoInitTensorByTensor(py_tensor_ptr, AutoInitTensorByTensor(py_tensor_ptr,
kws_map, kws_map,
...@@ -816,8 +809,7 @@ int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { ...@@ -816,8 +809,7 @@ int TensorInit(PyObject* self, PyObject* args, PyObject* kwargs) {
} else if (args_num == (Py_ssize_t)5) { } else if (args_num == (Py_ssize_t)5) {
if (!flag_kwargs) { if (!flag_kwargs) {
PyObject* arg0_ptr = PyTuple_GET_ITEM(args, 0); PyObject* arg0_ptr = PyTuple_GET_ITEM(args, 0);
if (PyObject_IsInstance(arg0_ptr, if (PyObject_TypeCheck(arg0_ptr, g_vartype_pytype)) {
reinterpret_cast<PyObject*>(g_vartype_pytype))) {
VLOG(6) << "Calling case2's initializer."; VLOG(6) << "Calling case2's initializer.";
paddle::framework::proto::VarType::Type dtype = paddle::framework::proto::VarType::Type dtype =
CastPyArg2ProtoType(PyTuple_GET_ITEM(args, 0), 0); CastPyArg2ProtoType(PyTuple_GET_ITEM(args, 0), 0);
...@@ -1007,9 +999,7 @@ int StringTensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { ...@@ -1007,9 +999,7 @@ int StringTensorInit(PyObject* self, PyObject* args, PyObject* kwargs) {
AutoInitStringTensorByPyArray( AutoInitStringTensorByPyArray(
py_tensor_ptr, kws_map, args, flag_kwargs, args_num); py_tensor_ptr, kws_map, args, flag_kwargs, args_num);
return 0; return 0;
} else if (PyObject_IsInstance( } else if (PyObject_TypeCheck(kw_value, p_string_tensor_type)) {
kw_value,
reinterpret_cast<PyObject*>(p_string_tensor_type))) {
VLOG(6) << "Calling case5's or case6's string initializer"; VLOG(6) << "Calling case5's or case6's string initializer";
AutoInitStringTensorByStringTensor( AutoInitStringTensorByStringTensor(
py_tensor_ptr, kws_map, args, flag_kwargs, args_num); py_tensor_ptr, kws_map, args, flag_kwargs, args_num);
...@@ -1057,9 +1047,7 @@ int StringTensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { ...@@ -1057,9 +1047,7 @@ int StringTensorInit(PyObject* self, PyObject* args, PyObject* kwargs) {
AutoInitStringTensorByPyArray( AutoInitStringTensorByPyArray(
py_tensor_ptr, kws_map, args, flag_kwargs, args_num); py_tensor_ptr, kws_map, args, flag_kwargs, args_num);
return 0; return 0;
} else if (PyObject_IsInstance( } else if (PyObject_TypeCheck(arg0_ptr, p_string_tensor_type)) {
arg0_ptr,
reinterpret_cast<PyObject*>(p_string_tensor_type))) {
VLOG(6) << "Calling case5's or case6's string initializer."; VLOG(6) << "Calling case5's or case6's string initializer.";
AutoInitStringTensorByStringTensor( AutoInitStringTensorByStringTensor(
py_tensor_ptr, kws_map, args, flag_kwargs, args_num); py_tensor_ptr, kws_map, args, flag_kwargs, args_num);
...@@ -1076,8 +1064,7 @@ int StringTensorInit(PyObject* self, PyObject* args, PyObject* kwargs) { ...@@ -1076,8 +1064,7 @@ int StringTensorInit(PyObject* self, PyObject* args, PyObject* kwargs) {
// 2 position args // 2 position args
if (!flag_kwargs) { if (!flag_kwargs) {
PyObject* arg0_ptr = PyTuple_GET_ITEM(args, 0); PyObject* arg0_ptr = PyTuple_GET_ITEM(args, 0);
if (PyObject_IsInstance( if (PyObject_TypeCheck(arg0_ptr, p_string_tensor_type)) {
arg0_ptr, reinterpret_cast<PyObject*>(p_string_tensor_type))) {
VLOG(6) << "Calling case6's string initializer."; VLOG(6) << "Calling case6's string initializer.";
AutoInitStringTensorByStringTensor( AutoInitStringTensorByStringTensor(
py_tensor_ptr, kws_map, args, flag_kwargs, args_num); py_tensor_ptr, kws_map, args, flag_kwargs, args_num);
......
...@@ -76,7 +76,7 @@ extern void InitTensorWithNumpyValue(TensorObject* self, ...@@ -76,7 +76,7 @@ extern void InitTensorWithNumpyValue(TensorObject* self,
extern PyTypeObject* p_tensor_type; extern PyTypeObject* p_tensor_type;
Py_ssize_t GetSliceIndexFromPyObject(PyObject* obj) { Py_ssize_t GetSliceIndexFromPyObject(PyObject* obj) {
if (PyObject_IsInstance(obj, reinterpret_cast<PyObject*>(p_tensor_type))) { if (PyObject_TypeCheck(obj, p_tensor_type)) {
VLOG(6) << "Call GetSliceIndexFromTensor in Eager"; VLOG(6) << "Call GetSliceIndexFromTensor in Eager";
paddle::Tensor tensor = CastPyArg2Tensor(obj, 0); paddle::Tensor tensor = CastPyArg2Tensor(obj, 0);
PADDLE_ENFORCE_EQ( PADDLE_ENFORCE_EQ(
......
...@@ -235,8 +235,7 @@ void SetPythonStack() { ...@@ -235,8 +235,7 @@ void SetPythonStack() {
std::shared_ptr<jit::Function> CastPyArg2JitFunction(PyObject* obj, std::shared_ptr<jit::Function> CastPyArg2JitFunction(PyObject* obj,
ssize_t arg_pos) { ssize_t arg_pos) {
if (PyObject_IsInstance(obj, if (PyObject_TypeCheck(obj, g_jit_function_pytype)) {
reinterpret_cast<PyObject*>(g_jit_function_pytype))) {
return ::pybind11::handle(obj).cast<std::shared_ptr<jit::Function>>(); return ::pybind11::handle(obj).cast<std::shared_ptr<jit::Function>>();
} else { } else {
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
...@@ -255,8 +254,7 @@ std::vector<paddle::Tensor> CastPyArg2VectorOfTensor(PyObject* obj, ...@@ -255,8 +254,7 @@ std::vector<paddle::Tensor> CastPyArg2VectorOfTensor(PyObject* obj,
PyObject* item = nullptr; PyObject* item = nullptr;
for (Py_ssize_t i = 0; i < len; i++) { for (Py_ssize_t i = 0; i < len; i++) {
item = PyList_GetItem(obj, i); item = PyList_GetItem(obj, i);
if (PyObject_IsInstance(item, if (PyObject_TypeCheck(item, p_tensor_type)) {
reinterpret_cast<PyObject*>(p_tensor_type))) {
result.emplace_back(reinterpret_cast<TensorObject*>(item)->tensor); result.emplace_back(reinterpret_cast<TensorObject*>(item)->tensor);
} else if (item == Py_None) { } else if (item == Py_None) {
// emplace empty Tensor for None // emplace empty Tensor for None
...@@ -275,8 +273,7 @@ std::vector<paddle::Tensor> CastPyArg2VectorOfTensor(PyObject* obj, ...@@ -275,8 +273,7 @@ std::vector<paddle::Tensor> CastPyArg2VectorOfTensor(PyObject* obj,
PyObject* item = nullptr; PyObject* item = nullptr;
for (Py_ssize_t i = 0; i < len; i++) { for (Py_ssize_t i = 0; i < len; i++) {
item = PyTuple_GetItem(obj, i); item = PyTuple_GetItem(obj, i);
if (PyObject_IsInstance(item, if (PyObject_TypeCheck(item, p_tensor_type)) {
reinterpret_cast<PyObject*>(p_tensor_type))) {
result.emplace_back(reinterpret_cast<TensorObject*>(item)->tensor); result.emplace_back(reinterpret_cast<TensorObject*>(item)->tensor);
} else if (item == Py_None) { } else if (item == Py_None) {
// emplace empty Tensor for None // emplace empty Tensor for None
...@@ -292,8 +289,7 @@ std::vector<paddle::Tensor> CastPyArg2VectorOfTensor(PyObject* obj, ...@@ -292,8 +289,7 @@ std::vector<paddle::Tensor> CastPyArg2VectorOfTensor(PyObject* obj,
} }
} else if (obj == Py_None) { } else if (obj == Py_None) {
return {}; return {};
} else if (PyObject_IsInstance(obj, } else if (PyObject_TypeCheck(obj, p_tensor_type)) {
reinterpret_cast<PyObject*>(p_tensor_type))) {
return {reinterpret_cast<TensorObject*>(obj)->tensor}; return {reinterpret_cast<TensorObject*>(obj)->tensor};
} else { } else {
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
...@@ -519,22 +515,17 @@ std::vector<std::vector<size_t>> CastPyArg2VectorOfVectorOfSize_t( ...@@ -519,22 +515,17 @@ std::vector<std::vector<size_t>> CastPyArg2VectorOfVectorOfSize_t(
platform::Place CastPyArg2Place(PyObject* obj, ssize_t arg_pos) { platform::Place CastPyArg2Place(PyObject* obj, ssize_t arg_pos) {
platform::Place place; platform::Place place;
if (PyObject_IsInstance(obj, reinterpret_cast<PyObject*>(g_place_pytype))) { if (PyObject_TypeCheck(obj, g_place_pytype)) {
place = ::pybind11::handle(obj).cast<platform::Place>(); place = ::pybind11::handle(obj).cast<platform::Place>();
} else if (PyObject_IsInstance( } else if (PyObject_TypeCheck(obj, g_cudaplace_pytype)) {
obj, reinterpret_cast<PyObject*>(g_cudaplace_pytype))) {
place = ::pybind11::handle(obj).cast<platform::CUDAPlace>(); place = ::pybind11::handle(obj).cast<platform::CUDAPlace>();
} else if (PyObject_IsInstance( } else if (PyObject_TypeCheck(obj, g_cpuplace_pytype)) {
obj, reinterpret_cast<PyObject*>(g_cpuplace_pytype))) {
place = ::pybind11::handle(obj).cast<platform::CPUPlace>(); place = ::pybind11::handle(obj).cast<platform::CPUPlace>();
} else if (PyObject_IsInstance( } else if (PyObject_TypeCheck(obj, g_xpuplace_pytype)) {
obj, reinterpret_cast<PyObject*>(g_xpuplace_pytype))) {
place = ::pybind11::handle(obj).cast<platform::XPUPlace>(); place = ::pybind11::handle(obj).cast<platform::XPUPlace>();
} else if (PyObject_IsInstance( } else if (PyObject_TypeCheck(obj, g_cudapinnedplace_pytype)) {
obj, reinterpret_cast<PyObject*>(g_cudapinnedplace_pytype))) {
place = ::pybind11::handle(obj).cast<platform::CUDAPinnedPlace>(); place = ::pybind11::handle(obj).cast<platform::CUDAPinnedPlace>();
} else if (PyObject_IsInstance( } else if (PyObject_TypeCheck(obj, g_customplace_pytype)) {
obj, reinterpret_cast<PyObject*>(g_customplace_pytype))) {
place = ::pybind11::handle(obj).cast<platform::CustomPlace>(); place = ::pybind11::handle(obj).cast<platform::CustomPlace>();
} else { } else {
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
...@@ -550,8 +541,7 @@ platform::Place CastPyArg2Place(PyObject* obj, ssize_t arg_pos) { ...@@ -550,8 +541,7 @@ platform::Place CastPyArg2Place(PyObject* obj, ssize_t arg_pos) {
} }
phi::DenseTensor CastPyArg2FrameworkTensor(PyObject* obj, ssize_t arg_pos) { phi::DenseTensor CastPyArg2FrameworkTensor(PyObject* obj, ssize_t arg_pos) {
if (PyObject_IsInstance( if (PyObject_TypeCheck(obj, g_framework_tensor_pytype)) {
obj, reinterpret_cast<PyObject*>(g_framework_tensor_pytype))) {
return ::pybind11::handle(obj).cast<phi::DenseTensor>(); return ::pybind11::handle(obj).cast<phi::DenseTensor>();
} else { } else {
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
...@@ -570,8 +560,7 @@ std::vector<phi::DenseTensor> CastPyArg2VectorOfTensorBase(PyObject* obj, ...@@ -570,8 +560,7 @@ std::vector<phi::DenseTensor> CastPyArg2VectorOfTensorBase(PyObject* obj,
PyObject* item = nullptr; PyObject* item = nullptr;
for (Py_ssize_t i = 0; i < len; i++) { for (Py_ssize_t i = 0; i < len; i++) {
item = PyList_GetItem(obj, i); item = PyList_GetItem(obj, i);
if (PyObject_IsInstance( if (PyObject_TypeCheck(item, g_framework_tensor_pytype)) {
item, reinterpret_cast<PyObject*>(g_framework_tensor_pytype))) {
result.emplace_back(::pybind11::handle(item).cast<phi::DenseTensor>()); result.emplace_back(::pybind11::handle(item).cast<phi::DenseTensor>());
} else { } else {
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
...@@ -587,8 +576,7 @@ std::vector<phi::DenseTensor> CastPyArg2VectorOfTensorBase(PyObject* obj, ...@@ -587,8 +576,7 @@ std::vector<phi::DenseTensor> CastPyArg2VectorOfTensorBase(PyObject* obj,
PyObject* item = nullptr; PyObject* item = nullptr;
for (Py_ssize_t i = 0; i < len; i++) { for (Py_ssize_t i = 0; i < len; i++) {
item = PyTuple_GetItem(obj, i); item = PyTuple_GetItem(obj, i);
if (PyObject_IsInstance( if (PyObject_TypeCheck(item, g_framework_tensor_pytype)) {
item, reinterpret_cast<PyObject*>(g_framework_tensor_pytype))) {
result.emplace_back(::pybind11::handle(item).cast<phi::DenseTensor>()); result.emplace_back(::pybind11::handle(item).cast<phi::DenseTensor>());
} else { } else {
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
...@@ -599,17 +587,14 @@ std::vector<phi::DenseTensor> CastPyArg2VectorOfTensorBase(PyObject* obj, ...@@ -599,17 +587,14 @@ std::vector<phi::DenseTensor> CastPyArg2VectorOfTensorBase(PyObject* obj,
i)); i));
} }
} }
} else if (PyObject_IsInstance(obj, } else if (PyObject_TypeCheck(obj, g_framework_lodtensorarray_pytype)) {
reinterpret_cast<PyObject*>(
g_framework_lodtensorarray_pytype))) {
for (auto& tensor : for (auto& tensor :
(::pybind11::handle(obj).cast<framework::LoDTensorArray>())) { (::pybind11::handle(obj).cast<framework::LoDTensorArray>())) {
result.emplace_back(tensor); result.emplace_back(tensor);
} }
} else if (obj == Py_None) { } else if (obj == Py_None) {
return {}; return {};
} else if (PyObject_IsInstance( } else if (PyObject_TypeCheck(obj, g_framework_tensor_pytype)) {
obj, reinterpret_cast<PyObject*>(g_framework_tensor_pytype))) {
return {::pybind11::handle(obj).cast<phi::DenseTensor>()}; return {::pybind11::handle(obj).cast<phi::DenseTensor>()};
} else { } else {
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
...@@ -624,7 +609,7 @@ std::vector<phi::DenseTensor> CastPyArg2VectorOfTensorBase(PyObject* obj, ...@@ -624,7 +609,7 @@ std::vector<phi::DenseTensor> CastPyArg2VectorOfTensorBase(PyObject* obj,
paddle::framework::proto::VarType::Type CastPyArg2ProtoType(PyObject* obj, paddle::framework::proto::VarType::Type CastPyArg2ProtoType(PyObject* obj,
ssize_t arg_pos) { ssize_t arg_pos) {
paddle::framework::proto::VarType::Type dtype; paddle::framework::proto::VarType::Type dtype;
if (PyObject_IsInstance(obj, reinterpret_cast<PyObject*>(g_vartype_pytype))) { if (PyObject_TypeCheck(obj, g_vartype_pytype)) {
dtype = dtype =
::pybind11::handle(obj).cast<paddle::framework::proto::VarType::Type>(); ::pybind11::handle(obj).cast<paddle::framework::proto::VarType::Type>();
} else { } else {
...@@ -973,7 +958,7 @@ paddle::optional<paddle::Tensor> GetOptionalTensorFromArgs( ...@@ -973,7 +958,7 @@ paddle::optional<paddle::Tensor> GetOptionalTensorFromArgs(
return paddle::none; return paddle::none;
} }
if (PyObject_IsInstance(obj, reinterpret_cast<PyObject*>(p_tensor_type))) { if (PyObject_TypeCheck(obj, p_tensor_type)) {
return paddle::make_optional<paddle::Tensor>( return paddle::make_optional<paddle::Tensor>(
reinterpret_cast<TensorObject*>(obj)->tensor); reinterpret_cast<TensorObject*>(obj)->tensor);
} else { } else {
...@@ -1007,10 +992,9 @@ static paddle::Tensor& GetTensorFromPyObject(const std::string& op_type, ...@@ -1007,10 +992,9 @@ static paddle::Tensor& GetTensorFromPyObject(const std::string& op_type,
return emptytensor; return emptytensor;
} }
if (PyObject_IsInstance(obj, reinterpret_cast<PyObject*>(p_tensor_type))) { if (PyObject_TypeCheck(obj, p_tensor_type)) {
return reinterpret_cast<TensorObject*>(obj)->tensor; return reinterpret_cast<TensorObject*>(obj)->tensor;
} else if (PyObject_IsInstance( } else if (PyObject_TypeCheck(obj, p_string_tensor_type)) {
obj, reinterpret_cast<PyObject*>(p_string_tensor_type))) {
return reinterpret_cast<TensorObject*>(obj)->tensor; return reinterpret_cast<TensorObject*>(obj)->tensor;
} else { } else {
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
...@@ -1187,7 +1171,7 @@ paddle::Tensor* GetTensorPtrFromArgs(const std::string& op_type, ...@@ -1187,7 +1171,7 @@ paddle::Tensor* GetTensorPtrFromArgs(const std::string& op_type,
return &emptytensor; return &emptytensor;
} }
if (PyObject_IsInstance(obj, reinterpret_cast<PyObject*>(p_tensor_type))) { if (PyObject_TypeCheck(obj, p_tensor_type)) {
return &(reinterpret_cast<TensorObject*>(obj)->tensor); return &(reinterpret_cast<TensorObject*>(obj)->tensor);
} else { } else {
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
...@@ -1305,8 +1289,7 @@ std::vector<paddle::Tensor> GetTensorListFromPyObject(PyObject* obj, ...@@ -1305,8 +1289,7 @@ std::vector<paddle::Tensor> GetTensorListFromPyObject(PyObject* obj,
PyObject* item = nullptr; PyObject* item = nullptr;
for (Py_ssize_t i = 0; i < len; i++) { for (Py_ssize_t i = 0; i < len; i++) {
item = PyList_GetItem(obj, i); item = PyList_GetItem(obj, i);
if (PyObject_IsInstance(item, if (PyObject_TypeCheck(item, p_tensor_type)) {
reinterpret_cast<PyObject*>(p_tensor_type))) {
result.emplace_back(reinterpret_cast<TensorObject*>(item)->tensor); result.emplace_back(reinterpret_cast<TensorObject*>(item)->tensor);
} else if (allow_none && (item == Py_None)) { } else if (allow_none && (item == Py_None)) {
VLOG(4) << "Got None in Tensor list: " << i; VLOG(4) << "Got None in Tensor list: " << i;
...@@ -1324,8 +1307,7 @@ std::vector<paddle::Tensor> GetTensorListFromPyObject(PyObject* obj, ...@@ -1324,8 +1307,7 @@ std::vector<paddle::Tensor> GetTensorListFromPyObject(PyObject* obj,
PyObject* item = nullptr; PyObject* item = nullptr;
for (Py_ssize_t i = 0; i < len; i++) { for (Py_ssize_t i = 0; i < len; i++) {
item = PyTuple_GetItem(obj, i); item = PyTuple_GetItem(obj, i);
if (PyObject_IsInstance(item, if (PyObject_TypeCheck(item, p_tensor_type)) {
reinterpret_cast<PyObject*>(p_tensor_type))) {
result.emplace_back(reinterpret_cast<TensorObject*>(item)->tensor); result.emplace_back(reinterpret_cast<TensorObject*>(item)->tensor);
} else if (allow_none && (item == Py_None)) { } else if (allow_none && (item == Py_None)) {
VLOG(4) << "Got None in Tensor list: " << i; VLOG(4) << "Got None in Tensor list: " << i;
...@@ -1548,8 +1530,7 @@ paddle::experimental::IntArray CastPyArg2IntArray(PyObject* obj, ...@@ -1548,8 +1530,7 @@ paddle::experimental::IntArray CastPyArg2IntArray(PyObject* obj,
} }
paddle::framework::Scope* CastPyArg2ScopePtr(PyObject* obj) { paddle::framework::Scope* CastPyArg2ScopePtr(PyObject* obj) {
if (PyObject_IsInstance( if (PyObject_TypeCheck(obj, g_framework_scope_pytype)) {
obj, reinterpret_cast<PyObject*>(g_framework_scope_pytype))) {
return ::pybind11::handle(obj).cast<paddle::framework::Scope*>(); return ::pybind11::handle(obj).cast<paddle::framework::Scope*>();
} else { } else {
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
......
...@@ -70,9 +70,9 @@ bool PyObject_CheckBool(PyObject** obj) { return PyBool_Check(*obj); } ...@@ -70,9 +70,9 @@ bool PyObject_CheckBool(PyObject** obj) { return PyBool_Check(*obj); }
bool PyObject_CheckLongOrToLong(PyObject** obj) { bool PyObject_CheckLongOrToLong(PyObject** obj) {
if ((PyLong_Check(*obj) && !PyBool_Check(*obj)) || if ((PyLong_Check(*obj) && !PyBool_Check(*obj)) ||
PyObject_IsInstance(*obj, (PyObject*)g_vartype_pytype) || // NOLINT PyObject_TypeCheck(*obj, g_vartype_pytype) || // NOLINT
PyObject_IsInstance(*obj, (PyObject*)g_varbase_pytype) || // NOLINT PyObject_TypeCheck(*obj, g_varbase_pytype) || // NOLINT
(PyObject_IsInstance(*obj, (PyObject*)p_tensor_type) && // NOLINT (PyObject_TypeCheck(*obj, p_tensor_type) && // NOLINT
(((TensorObject*)(*obj))->tensor.numel() == 1))) { // NOLINT (((TensorObject*)(*obj))->tensor.numel() == 1))) { // NOLINT
return true; return true;
} }
...@@ -92,8 +92,8 @@ bool PyObject_CheckLongOrToLong(PyObject** obj) { ...@@ -92,8 +92,8 @@ bool PyObject_CheckLongOrToLong(PyObject** obj) {
bool PyObject_CheckFloatOrToFloat(PyObject** obj) { bool PyObject_CheckFloatOrToFloat(PyObject** obj) {
// sometimes users provide PyLong or numpy.int64 but attr is float // sometimes users provide PyLong or numpy.int64 but attr is float
if (PyFloat_Check(*obj) || PyLong_Check(*obj) || if (PyFloat_Check(*obj) || PyLong_Check(*obj) ||
PyObject_IsInstance(*obj, (PyObject*)g_varbase_pytype) || // NOLINT PyObject_TypeCheck(*obj, g_varbase_pytype) || // NOLINT
(PyObject_IsInstance(*obj, (PyObject*)p_tensor_type) && // NOLINT (PyObject_TypeCheck(*obj, p_tensor_type) && // NOLINT
(((TensorObject*)(*obj))->tensor.numel() == 1))) { // NOLINT (((TensorObject*)(*obj))->tensor.numel() == 1))) { // NOLINT
return true; return true;
} }
...@@ -110,9 +110,9 @@ bool PyObject_CheckFloatOrToFloat(PyObject** obj) { ...@@ -110,9 +110,9 @@ bool PyObject_CheckFloatOrToFloat(PyObject** obj) {
bool PyObject_CheckComplexOrToComplex(PyObject** obj) { bool PyObject_CheckComplexOrToComplex(PyObject** obj) {
if (PyComplex_Check(*obj) || PyLong_Check(*obj) || PyFloat_Check(*obj) || if (PyComplex_Check(*obj) || PyLong_Check(*obj) || PyFloat_Check(*obj) ||
PyObject_IsInstance(*obj, (PyObject*)g_vartype_pytype) || // NOLINT PyObject_TypeCheck(*obj, g_vartype_pytype) || // NOLINT
PyObject_IsInstance(*obj, (PyObject*)g_varbase_pytype) || // NOLINT PyObject_TypeCheck(*obj, g_varbase_pytype) || // NOLINT
PyObject_IsInstance(*obj, (PyObject*)p_tensor_type)) { // NOLINT PyObject_TypeCheck(*obj, p_tensor_type)) { // NOLINT
return true; return true;
} }
// consider numpy cfloat & numpy cdouble? // consider numpy cfloat & numpy cdouble?
...@@ -821,8 +821,7 @@ void CastPyArg2AttrBlock(PyObject* obj, ...@@ -821,8 +821,7 @@ void CastPyArg2AttrBlock(PyObject* obj,
::pybind11::detail::instance* inst = ::pybind11::detail::instance* inst =
(::pybind11::detail::instance*)obj; // NOLINT (::pybind11::detail::instance*)obj; // NOLINT
if (!PyObject_IsInstance((PyObject*)inst, // NOLINT if (!PyObject_TypeCheck((PyObject*)inst, g_blockdesc_pytype)) { // NOLINT
(PyObject*)g_blockdesc_pytype)) { // NOLINT
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
"%s(): argument (position %d) must be " "%s(): argument (position %d) must be "
"BlockDesc, but got %s", "BlockDesc, but got %s",
...@@ -951,8 +950,7 @@ std::shared_ptr<imperative::VarBase> GetVarBaseFromArgs( ...@@ -951,8 +950,7 @@ std::shared_ptr<imperative::VarBase> GetVarBaseFromArgs(
return nullptr; return nullptr;
} }
if (!PyObject_IsInstance((PyObject*)inst, // NOLINT if (!PyObject_TypeCheck((PyObject*)inst, g_varbase_pytype)) { // NOLINT
(PyObject*)g_varbase_pytype)) { // NOLINT
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
"%s(): argument '%s' (position %d) must be Tensor, but got " "%s(): argument '%s' (position %d) must be Tensor, but got "
"%s", "%s",
...@@ -1002,8 +1000,7 @@ std::vector<std::shared_ptr<imperative::VarBase>> GetVarBaseListFromArgs( ...@@ -1002,8 +1000,7 @@ std::vector<std::shared_ptr<imperative::VarBase>> GetVarBaseListFromArgs(
::pybind11::detail::instance* item = nullptr; ::pybind11::detail::instance* item = nullptr;
for (Py_ssize_t i = 0; i < len; i++) { for (Py_ssize_t i = 0; i < len; i++) {
item = (::pybind11::detail::instance*)PyList_GetItem(list, i); item = (::pybind11::detail::instance*)PyList_GetItem(list, i);
if (!PyObject_IsInstance((PyObject*)item, // NOLINT if (!PyObject_TypeCheck((PyObject*)item, g_varbase_pytype)) { // NOLINT
(PyObject*)g_varbase_pytype)) { // NOLINT
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
"%s(): argument '%s' (position %d) must be list of Tensors, but " "%s(): argument '%s' (position %d) must be list of Tensors, but "
"got list of " "got list of "
...@@ -1032,8 +1029,7 @@ std::vector<std::shared_ptr<imperative::VarBase>> GetVarBaseListFromArgs( ...@@ -1032,8 +1029,7 @@ std::vector<std::shared_ptr<imperative::VarBase>> GetVarBaseListFromArgs(
::pybind11::detail::instance* item = nullptr; ::pybind11::detail::instance* item = nullptr;
for (Py_ssize_t i = 0; i < len; i++) { for (Py_ssize_t i = 0; i < len; i++) {
item = (::pybind11::detail::instance*)PyTuple_GetItem(list, i); // NOLINT item = (::pybind11::detail::instance*)PyTuple_GetItem(list, i); // NOLINT
if (!PyObject_IsInstance((PyObject*)item, // NOLINT if (!PyObject_TypeCheck((PyObject*)item, g_varbase_pytype)) { // NOLINT
(PyObject*)g_varbase_pytype)) { // NOLINT
PADDLE_THROW(platform::errors::InvalidArgument( PADDLE_THROW(platform::errors::InvalidArgument(
"%s(): argument '%s' (position %d) must be list of Tensors, but " "%s(): argument '%s' (position %d) must be list of Tensors, but "
"got list of " "got list of "
......
...@@ -23,18 +23,18 @@ namespace pybind { ...@@ -23,18 +23,18 @@ namespace pybind {
PyTypeObject* p_tensor_type = nullptr; PyTypeObject* p_tensor_type = nullptr;
PyTypeObject* p_string_tensor_type = nullptr; PyTypeObject* p_string_tensor_type = nullptr;
// PyTypeObject* PyFloat_Type;
PyTypeObject* PyLong_Type;
bool PyCheckTensor(PyObject* obj) { bool PyCheckTensor(PyObject* obj) {
if (!p_tensor_type) { if (!p_tensor_type) {
return false; return false;
} }
return PyObject_IsInstance(obj, reinterpret_cast<PyObject*>(p_tensor_type)); return PyObject_TypeCheck(obj, p_tensor_type);
} }
paddle::Tensor CastPyArg2Tensor(PyObject* obj, Py_ssize_t arg_pos) { paddle::Tensor CastPyArg2Tensor(PyObject* obj, Py_ssize_t arg_pos) {
if (PyObject_IsInstance(obj, reinterpret_cast<PyObject*>(p_tensor_type)) || if (PyObject_TypeCheck(obj, p_tensor_type) ||
PyObject_IsInstance(obj, PyObject_TypeCheck(obj, p_string_tensor_type)) {
reinterpret_cast<PyObject*>(p_string_tensor_type))) {
return reinterpret_cast<TensorObject*>(obj)->tensor; return reinterpret_cast<TensorObject*>(obj)->tensor;
} else { } else {
PADDLE_THROW(phi::errors::InvalidArgument( PADDLE_THROW(phi::errors::InvalidArgument(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册