Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
a000e9b8
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
a000e9b8
编写于
1月 13, 2023
作者:
H
HongyuJia
提交者:
GitHub
1月 13, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
unify PyCheckTensor function (#49751)
上级
0294ab41
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
19 addition
and
33 deletion
+19
-33
paddle/fluid/pybind/eager_math_op_patch.cc
paddle/fluid/pybind/eager_math_op_patch.cc
+0
-6
paddle/fluid/pybind/eager_method.cc
paddle/fluid/pybind/eager_method.cc
+0
-4
paddle/fluid/pybind/eager_py_layer.cc
paddle/fluid/pybind/eager_py_layer.cc
+12
-12
paddle/fluid/pybind/eager_utils.cc
paddle/fluid/pybind/eager_utils.cc
+5
-5
paddle/fluid/pybind/eager_utils.h
paddle/fluid/pybind/eager_utils.h
+1
-1
paddle/fluid/pybind/imperative.cc
paddle/fluid/pybind/imperative.cc
+1
-4
paddle/fluid/pybind/slice_utils.h
paddle/fluid/pybind/slice_utils.h
+0
-1
未找到文件。
paddle/fluid/pybind/eager_math_op_patch.cc
浏览文件 @
a000e9b8
...
...
@@ -52,12 +52,6 @@ typedef SSIZE_T ssize_t;
namespace
paddle
{
namespace
pybind
{
extern
PyTypeObject
*
p_tensor_type
;
bool
PyCheckTensor
(
PyObject
*
obj
)
{
return
PyObject_IsInstance
(
obj
,
reinterpret_cast
<
PyObject
*>
(
p_tensor_type
));
}
static
bool
PyCheckInteger
(
PyObject
*
obj
)
{
#if PY_VERSION_HEX < 0x03000000
return
(
PyLong_Check
(
obj
)
||
PyInt_Check
(
obj
))
&&
!
PyBool_Check
(
obj
);
...
...
paddle/fluid/pybind/eager_method.cc
浏览文件 @
a000e9b8
...
...
@@ -88,10 +88,6 @@ Py_ssize_t GetSliceIndexFromPyObject(PyObject* obj) {
}
}
bool
PyCheckTensor
(
PyObject
*
obj
)
{
return
PyObject_IsInstance
(
obj
,
reinterpret_cast
<
PyObject
*>
(
p_tensor_type
));
}
static
PyObject
*
tensor_method_numpy
(
TensorObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
...
...
paddle/fluid/pybind/eager_py_layer.cc
浏览文件 @
a000e9b8
...
...
@@ -49,12 +49,12 @@ std::set<paddle::experimental::Tensor*> GetTensorsFromPyObject(PyObject* obj) {
if
(
obj
==
nullptr
)
{
return
result
;
}
if
(
IsEager
Tensor
(
obj
))
{
if
(
PyCheck
Tensor
(
obj
))
{
result
.
insert
(
&
reinterpret_cast
<
TensorObject
*>
(
obj
)
->
tensor
);
// NOLINT
}
else
if
(
PyList_Check
(
obj
))
{
Py_ssize_t
len
=
PyList_Size
(
obj
);
for
(
Py_ssize_t
i
=
0
;
i
<
len
;
i
++
)
{
if
(
IsEager
Tensor
(
PyList_GetItem
(
obj
,
i
)))
{
if
(
PyCheck
Tensor
(
PyList_GetItem
(
obj
,
i
)))
{
result
.
insert
(
&
reinterpret_cast
<
TensorObject
*>
(
PyList_GetItem
(
obj
,
i
))
// NOLINT
->
tensor
);
...
...
@@ -63,7 +63,7 @@ std::set<paddle::experimental::Tensor*> GetTensorsFromPyObject(PyObject* obj) {
}
else
if
(
PyTuple_Check
(
obj
))
{
Py_ssize_t
len
=
PyTuple_Size
(
obj
);
for
(
Py_ssize_t
i
=
0
;
i
<
len
;
i
++
)
{
if
(
IsEager
Tensor
(
PyTuple_GetItem
(
obj
,
i
)))
{
if
(
PyCheck
Tensor
(
PyTuple_GetItem
(
obj
,
i
)))
{
result
.
insert
(
&
reinterpret_cast
<
TensorObject
*>
(
PyTuple_GetItem
(
obj
,
i
))
// NOLINT
->
tensor
);
...
...
@@ -177,7 +177,7 @@ PyObject* pylayer_method_apply(PyObject* cls,
}
else
{
obj
=
PyTuple_GET_ITEM
(
args
,
i
);
}
if
(
IsEager
Tensor
(
obj
))
{
if
(
PyCheck
Tensor
(
obj
))
{
input_tensorbases
.
insert
(
reinterpret_cast
<
TensorObject
*>
(
obj
)
->
tensor
.
impl
().
get
());
auto
autograd_meta
=
egr
::
EagerUtils
::
nullable_autograd_meta
(
...
...
@@ -196,7 +196,7 @@ PyObject* pylayer_method_apply(PyObject* cls,
Py_ssize_t
len
=
PyList_Size
(
obj
);
for
(
Py_ssize_t
j
=
0
;
j
<
len
;
j
++
)
{
PyObject
*
o
=
PyList_GetItem
(
obj
,
j
);
if
(
IsEager
Tensor
(
o
))
{
if
(
PyCheck
Tensor
(
o
))
{
input_tensorbases
.
insert
(
reinterpret_cast
<
TensorObject
*>
(
o
)
->
tensor
.
impl
().
get
());
tensors
.
push_back
(
&
(
reinterpret_cast
<
TensorObject
*>
(
o
)
->
tensor
));
...
...
@@ -219,7 +219,7 @@ PyObject* pylayer_method_apply(PyObject* cls,
Py_ssize_t
len
=
PyTuple_Size
(
obj
);
for
(
Py_ssize_t
j
=
0
;
j
<
len
;
j
++
)
{
PyObject
*
o
=
PyTuple_GetItem
(
obj
,
j
);
if
(
IsEager
Tensor
(
o
))
{
if
(
PyCheck
Tensor
(
o
))
{
input_tensorbases
.
insert
(
reinterpret_cast
<
TensorObject
*>
(
o
)
->
tensor
.
impl
().
get
());
tensors
.
push_back
(
&
(
reinterpret_cast
<
TensorObject
*>
(
o
)
->
tensor
));
...
...
@@ -292,7 +292,7 @@ PyObject* pylayer_method_apply(PyObject* cls,
ctx
->
forward_output_tensor_is_duplicable
.
reserve
(
outputs_size
);
for
(
Py_ssize_t
i
=
0
;
i
<
outputs_size
;
i
++
)
{
PyObject
*
obj
=
PyTuple_GET_ITEM
(
outputs_tuple
,
i
);
if
(
IsEager
Tensor
(
obj
))
{
if
(
PyCheck
Tensor
(
obj
))
{
outputs_tensor
.
push_back
(
{
&
(
reinterpret_cast
<
TensorObject
*>
(
obj
)
->
tensor
)});
outputs_autograd_meta
.
push_back
({
egr
::
EagerUtils
::
autograd_meta
(
...
...
@@ -316,7 +316,7 @@ PyObject* pylayer_method_apply(PyObject* cls,
Py_ssize_t
len
=
PyList_Size
(
obj
);
for
(
Py_ssize_t
j
=
0
;
j
<
len
;
j
++
)
{
PyObject
*
o
=
PyList_GetItem
(
obj
,
j
);
if
(
IsEager
Tensor
(
o
))
{
if
(
PyCheck
Tensor
(
o
))
{
tensors
.
push_back
(
&
(
reinterpret_cast
<
TensorObject
*>
(
o
)
->
tensor
));
if
(
input_tensorbases
.
count
(
reinterpret_cast
<
TensorObject
*>
(
o
)
->
tensor
.
impl
().
get
()))
{
...
...
@@ -344,7 +344,7 @@ PyObject* pylayer_method_apply(PyObject* cls,
Py_ssize_t
len
=
PyTuple_Size
(
obj
);
for
(
Py_ssize_t
j
=
0
;
j
<
len
;
j
++
)
{
PyObject
*
o
=
PyTuple_GetItem
(
obj
,
j
);
if
(
IsEager
Tensor
(
o
))
{
if
(
PyCheck
Tensor
(
o
))
{
tensors
.
push_back
(
&
(
reinterpret_cast
<
TensorObject
*>
(
o
)
->
tensor
));
if
(
input_tensorbases
.
count
(
reinterpret_cast
<
TensorObject
*>
(
o
)
->
tensor
.
impl
().
get
()))
{
...
...
@@ -538,7 +538,7 @@ void call_pack_hook(PyLayerObject* self, PyObject* value) {
for
(
Py_ssize_t
i
=
0
;
i
<
saved_value_size
;
i
++
)
{
PyObject
*
obj
=
PyTuple_GET_ITEM
(
saved_value
,
i
);
if
(
IsEager
Tensor
(
obj
))
{
if
(
PyCheck
Tensor
(
obj
))
{
PyTuple_SET_ITEM
(
packed_value
,
i
,
reinterpret_cast
<
PyObject
*>
(
...
...
@@ -548,7 +548,7 @@ void call_pack_hook(PyLayerObject* self, PyObject* value) {
auto
tmp_list
=
PyList_New
(
len
);
for
(
Py_ssize_t
j
=
0
;
j
<
len
;
j
++
)
{
PyObject
*
o
=
PyList_GetItem
(
obj
,
j
);
if
(
IsEager
Tensor
(
o
))
{
if
(
PyCheck
Tensor
(
o
))
{
PyTuple_SET_ITEM
(
tmp_list
,
j
,
reinterpret_cast
<
PyObject
*>
(
...
...
@@ -565,7 +565,7 @@ void call_pack_hook(PyLayerObject* self, PyObject* value) {
auto
tmp_tuple
=
PyTuple_New
(
len
);
for
(
Py_ssize_t
j
=
0
;
j
<
len
;
j
++
)
{
PyObject
*
o
=
PyTuple_GetItem
(
obj
,
j
);
if
(
IsEager
Tensor
(
o
))
{
if
(
PyCheck
Tensor
(
o
))
{
PyTuple_SET_ITEM
(
tmp_tuple
,
j
,
reinterpret_cast
<
PyObject
*>
(
...
...
paddle/fluid/pybind/eager_utils.cc
浏览文件 @
a000e9b8
...
...
@@ -207,7 +207,7 @@ std::string CastPyArg2AttrString(PyObject* obj, ssize_t arg_pos) {
}
}
bool
IsEager
Tensor
(
PyObject
*
obj
)
{
bool
PyCheck
Tensor
(
PyObject
*
obj
)
{
return
PyObject_IsInstance
(
obj
,
reinterpret_cast
<
PyObject
*>
(
p_tensor_type
));
}
...
...
@@ -1307,7 +1307,7 @@ std::vector<paddle::experimental::Tensor> GetTensorListFromPyObject(
}
paddle
::
experimental
::
Tensor
&
GetTensorFromPyObject
(
PyObject
*
obj
)
{
if
(
!
IsEager
Tensor
(
obj
))
{
if
(
!
PyCheck
Tensor
(
obj
))
{
PADDLE_THROW
(
platform
::
errors
::
InvalidArgument
(
"argument must be "
"Tensor, but got %s"
,
...
...
@@ -1384,7 +1384,7 @@ paddle::experimental::Scalar CastPyArg2Scalar(PyObject* obj,
}
else
if
(
PyFloat_Check
(
obj
))
{
double
value
=
CastPyArg2Double
(
obj
,
op_type
,
arg_pos
);
return
paddle
::
experimental
::
Scalar
(
value
);
}
else
if
(
IsEager
Tensor
(
obj
))
{
}
else
if
(
PyCheck
Tensor
(
obj
))
{
paddle
::
experimental
::
Tensor
&
value
=
GetTensorFromPyObject
(
op_type
,
""
/*arg_name*/
,
obj
,
arg_pos
,
false
/*dispensable*/
);
return
paddle
::
experimental
::
Scalar
(
value
);
...
...
@@ -1715,7 +1715,7 @@ paddle::experimental::Tensor UnPackHook::operator()(
Py_XDECREF
(
args
);
egr
::
Controller
::
Instance
().
SetHasGrad
(
grad_tmp
);
PADDLE_ENFORCE_EQ
(
paddle
::
pybind
::
IsEager
Tensor
(
ret
),
PADDLE_ENFORCE_EQ
(
paddle
::
pybind
::
PyCheck
Tensor
(
ret
),
true
,
paddle
::
platform
::
errors
::
InvalidArgument
(
"paddle.autograd.saved_tensors_hooks only one pair "
...
...
@@ -1740,7 +1740,7 @@ void* UnPackHook::operator()(void* packed_value, void* other) {
Py_XDECREF
(
args
);
egr
::
Controller
::
Instance
().
SetHasGrad
(
grad_tmp
);
PADDLE_ENFORCE_EQ
(
paddle
::
pybind
::
IsEager
Tensor
(
ret
),
PADDLE_ENFORCE_EQ
(
paddle
::
pybind
::
PyCheck
Tensor
(
ret
),
true
,
paddle
::
platform
::
errors
::
InvalidArgument
(
"paddle.autograd.saved_tensors_hooks only one pair "
...
...
paddle/fluid/pybind/eager_utils.h
浏览文件 @
a000e9b8
...
...
@@ -46,7 +46,7 @@ namespace py = ::pybind11;
int
TensorDtype2NumpyDtype
(
phi
::
DataType
dtype
);
bool
IsEager
Tensor
(
PyObject
*
obj
);
bool
PyCheck
Tensor
(
PyObject
*
obj
);
bool
PyObject_CheckLongOrConvertToLong
(
PyObject
**
obj
);
bool
PyObject_CheckFloatOrConvertToFloat
(
PyObject
**
obj
);
...
...
paddle/fluid/pybind/imperative.cc
浏览文件 @
a000e9b8
...
...
@@ -350,9 +350,6 @@ Py_ssize_t GetSliceIndexFromPyObject(PyObject *obj) {
}
}
bool
PyCheckTensor
(
PyObject
*
obj
)
{
return
py
::
isinstance
<
imperative
::
VarBase
>
(
obj
);
}
using
PyNameVarBaseMap
=
std
::
unordered_map
<
std
::
string
,
py
::
handle
>
;
// NOTE(zjl): py::handle is a very light wrapper of PyObject *.
...
...
@@ -872,7 +869,7 @@ void BindImperative(py::module *m_ptr) {
self
->
Name
()));
}
if
(
PyCheckTensor
(
value_obj
.
ptr
()))
{
if
(
py
::
isinstance
<
imperative
::
VarBase
>
(
value_obj
.
ptr
()))
{
auto
value_tensor
=
value_obj
.
cast
<
std
::
shared_ptr
<
imperative
::
VarBase
>>
();
ins
.
insert
({
"ValueTensor"
,
{
value_tensor
}});
...
...
paddle/fluid/pybind/slice_utils.h
浏览文件 @
a000e9b8
...
...
@@ -30,7 +30,6 @@ namespace py = pybind11;
namespace
paddle
{
namespace
pybind
{
static
bool
PyCheckTensor
(
PyObject
*
obj
);
static
Py_ssize_t
GetSliceIndexFromPyObject
(
PyObject
*
obj
);
// Slice related methods
static
bool
PyCheckInteger
(
PyObject
*
obj
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录