未验证 提交 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) {
AutoInitTensorByPyArray(
py_tensor_ptr, kws_map, args, flag_kwargs, args_num);
return 0;
} else if (PyObject_IsInstance(
kw_value, reinterpret_cast<PyObject*>(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<PyObject*>(
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<PyObject*>(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<PyObject*>(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<PyObject*>(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<PyObject*>(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<PyObject*>(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<PyObject*>(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<PyObject*>(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);
......
......@@ -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<PyObject*>(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(
......
......@@ -235,8 +235,7 @@ void SetPythonStack() {
std::shared_ptr<jit::Function> CastPyArg2JitFunction(PyObject* obj,
ssize_t arg_pos) {
if (PyObject_IsInstance(obj,
reinterpret_cast<PyObject*>(g_jit_function_pytype))) {
if (PyObject_TypeCheck(obj, g_jit_function_pytype)) {
return ::pybind11::handle(obj).cast<std::shared_ptr<jit::Function>>();
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
......@@ -255,8 +254,7 @@ std::vector<paddle::Tensor> 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<PyObject*>(p_tensor_type))) {
if (PyObject_TypeCheck(item, p_tensor_type)) {
result.emplace_back(reinterpret_cast<TensorObject*>(item)->tensor);
} else if (item == Py_None) {
// emplace empty Tensor for None
......@@ -275,8 +273,7 @@ std::vector<paddle::Tensor> 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<PyObject*>(p_tensor_type))) {
if (PyObject_TypeCheck(item, p_tensor_type)) {
result.emplace_back(reinterpret_cast<TensorObject*>(item)->tensor);
} else if (item == Py_None) {
// emplace empty Tensor for None
......@@ -292,8 +289,7 @@ std::vector<paddle::Tensor> CastPyArg2VectorOfTensor(PyObject* obj,
}
} else if (obj == Py_None) {
return {};
} else if (PyObject_IsInstance(obj,
reinterpret_cast<PyObject*>(p_tensor_type))) {
} else if (PyObject_TypeCheck(obj, p_tensor_type)) {
return {reinterpret_cast<TensorObject*>(obj)->tensor};
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
......@@ -519,22 +515,17 @@ std::vector<std::vector<size_t>> CastPyArg2VectorOfVectorOfSize_t(
platform::Place CastPyArg2Place(PyObject* obj, ssize_t arg_pos) {
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>();
} else if (PyObject_IsInstance(
obj, reinterpret_cast<PyObject*>(g_cudaplace_pytype))) {
} else if (PyObject_TypeCheck(obj, g_cudaplace_pytype)) {
place = ::pybind11::handle(obj).cast<platform::CUDAPlace>();
} else if (PyObject_IsInstance(
obj, reinterpret_cast<PyObject*>(g_cpuplace_pytype))) {
} else if (PyObject_TypeCheck(obj, g_cpuplace_pytype)) {
place = ::pybind11::handle(obj).cast<platform::CPUPlace>();
} else if (PyObject_IsInstance(
obj, reinterpret_cast<PyObject*>(g_xpuplace_pytype))) {
} else if (PyObject_TypeCheck(obj, g_xpuplace_pytype)) {
place = ::pybind11::handle(obj).cast<platform::XPUPlace>();
} else if (PyObject_IsInstance(
obj, reinterpret_cast<PyObject*>(g_cudapinnedplace_pytype))) {
} else if (PyObject_TypeCheck(obj, g_cudapinnedplace_pytype)) {
place = ::pybind11::handle(obj).cast<platform::CUDAPinnedPlace>();
} else if (PyObject_IsInstance(
obj, reinterpret_cast<PyObject*>(g_customplace_pytype))) {
} else if (PyObject_TypeCheck(obj, g_customplace_pytype)) {
place = ::pybind11::handle(obj).cast<platform::CustomPlace>();
} 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<PyObject*>(g_framework_tensor_pytype))) {
if (PyObject_TypeCheck(obj, g_framework_tensor_pytype)) {
return ::pybind11::handle(obj).cast<phi::DenseTensor>();
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
......@@ -570,8 +560,7 @@ std::vector<phi::DenseTensor> 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<PyObject*>(g_framework_tensor_pytype))) {
if (PyObject_TypeCheck(item, g_framework_tensor_pytype)) {
result.emplace_back(::pybind11::handle(item).cast<phi::DenseTensor>());
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
......@@ -587,8 +576,7 @@ std::vector<phi::DenseTensor> 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<PyObject*>(g_framework_tensor_pytype))) {
if (PyObject_TypeCheck(item, g_framework_tensor_pytype)) {
result.emplace_back(::pybind11::handle(item).cast<phi::DenseTensor>());
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
......@@ -599,17 +587,14 @@ std::vector<phi::DenseTensor> CastPyArg2VectorOfTensorBase(PyObject* obj,
i));
}
}
} else if (PyObject_IsInstance(obj,
reinterpret_cast<PyObject*>(
g_framework_lodtensorarray_pytype))) {
} else if (PyObject_TypeCheck(obj, g_framework_lodtensorarray_pytype)) {
for (auto& tensor :
(::pybind11::handle(obj).cast<framework::LoDTensorArray>())) {
result.emplace_back(tensor);
}
} else if (obj == Py_None) {
return {};
} else if (PyObject_IsInstance(
obj, reinterpret_cast<PyObject*>(g_framework_tensor_pytype))) {
} else if (PyObject_TypeCheck(obj, g_framework_tensor_pytype)) {
return {::pybind11::handle(obj).cast<phi::DenseTensor>()};
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
......@@ -624,7 +609,7 @@ std::vector<phi::DenseTensor> 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<PyObject*>(g_vartype_pytype))) {
if (PyObject_TypeCheck(obj, g_vartype_pytype)) {
dtype =
::pybind11::handle(obj).cast<paddle::framework::proto::VarType::Type>();
} else {
......@@ -973,7 +958,7 @@ paddle::optional<paddle::Tensor> GetOptionalTensorFromArgs(
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>(
reinterpret_cast<TensorObject*>(obj)->tensor);
} else {
......@@ -1007,10 +992,9 @@ static paddle::Tensor& GetTensorFromPyObject(const std::string& op_type,
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;
} else if (PyObject_IsInstance(
obj, reinterpret_cast<PyObject*>(p_string_tensor_type))) {
} else if (PyObject_TypeCheck(obj, p_string_tensor_type)) {
return reinterpret_cast<TensorObject*>(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<PyObject*>(p_tensor_type))) {
if (PyObject_TypeCheck(obj, p_tensor_type)) {
return &(reinterpret_cast<TensorObject*>(obj)->tensor);
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
......@@ -1305,8 +1289,7 @@ std::vector<paddle::Tensor> 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<PyObject*>(p_tensor_type))) {
if (PyObject_TypeCheck(item, p_tensor_type)) {
result.emplace_back(reinterpret_cast<TensorObject*>(item)->tensor);
} else if (allow_none && (item == Py_None)) {
VLOG(4) << "Got None in Tensor list: " << i;
......@@ -1324,8 +1307,7 @@ std::vector<paddle::Tensor> 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<PyObject*>(p_tensor_type))) {
if (PyObject_TypeCheck(item, p_tensor_type)) {
result.emplace_back(reinterpret_cast<TensorObject*>(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<PyObject*>(g_framework_scope_pytype))) {
if (PyObject_TypeCheck(obj, g_framework_scope_pytype)) {
return ::pybind11::handle(obj).cast<paddle::framework::Scope*>();
} else {
PADDLE_THROW(platform::errors::InvalidArgument(
......
......@@ -70,9 +70,9 @@ 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
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,8 +92,8 @@ 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
PyObject_TypeCheck(*obj, g_varbase_pytype) || // NOLINT
(PyObject_TypeCheck(*obj, p_tensor_type) && // NOLINT
(((TensorObject*)(*obj))->tensor.numel() == 1))) { // NOLINT
return true;
}
......@@ -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<imperative::VarBase> 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<std::shared_ptr<imperative::VarBase>> 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<std::shared_ptr<imperative::VarBase>> 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 "
......
......@@ -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<PyObject*>(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<PyObject*>(p_tensor_type)) ||
PyObject_IsInstance(obj,
reinterpret_cast<PyObject*>(p_string_tensor_type))) {
if (PyObject_TypeCheck(obj, p_tensor_type) ||
PyObject_TypeCheck(obj, p_string_tensor_type)) {
return reinterpret_cast<TensorObject*>(obj)->tensor;
} else {
PADDLE_THROW(phi::errors::InvalidArgument(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册