Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
1b33f5c9
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
1b33f5c9
编写于
6月 02, 2023
作者:
X
xiaoguoguo626807
提交者:
GitHub
6月 02, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
【dygraph】replace Pyobject_isinstance as pyobject_typecheck (#54297)
* change pyobject_isinstance * change pyobject_isinstance
上级
6310419b
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
52 addition
and
88 deletion
+52
-88
paddle/fluid/pybind/eager.cc
paddle/fluid/pybind/eager.cc
+9
-22
paddle/fluid/pybind/eager_method.cc
paddle/fluid/pybind/eager_method.cc
+1
-1
paddle/fluid/pybind/eager_utils.cc
paddle/fluid/pybind/eager_utils.cc
+23
-42
paddle/fluid/pybind/op_function_common.cc
paddle/fluid/pybind/op_function_common.cc
+14
-18
paddle/utils/pybind.cc
paddle/utils/pybind.cc
+5
-5
未找到文件。
paddle/fluid/pybind/eager.cc
浏览文件 @
1b33f5c9
...
...
@@ -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
);
...
...
paddle/fluid/pybind/eager_method.cc
浏览文件 @
1b33f5c9
...
...
@@ -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
(
...
...
paddle/fluid/pybind/eager_utils.cc
浏览文件 @
1b33f5c9
...
...
@@ -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
(
...
...
paddle/fluid/pybind/op_function_common.cc
浏览文件 @
1b33f5c9
...
...
@@ -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 "
...
...
paddle/utils/pybind.cc
浏览文件 @
1b33f5c9
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录