Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
4e1f7692
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
4e1f7692
编写于
7月 19, 2022
作者:
W
wanghuancoder
提交者:
GitHub
7月 19, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Eager]release gil when run backward (#44433)
* release gil when run backward
上级
547075e9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
46 addition
and
24 deletion
+46
-24
paddle/fluid/eager/pylayer/py_layer_node.cc
paddle/fluid/eager/pylayer/py_layer_node.cc
+1
-0
paddle/fluid/pybind/eager_functions.cc
paddle/fluid/pybind/eager_functions.cc
+33
-24
paddle/fluid/pybind/eager_utils.h
paddle/fluid/pybind/eager_utils.h
+12
-0
未找到文件。
paddle/fluid/eager/pylayer/py_layer_node.cc
浏览文件 @
4e1f7692
...
...
@@ -34,6 +34,7 @@ GradNodePyLayer::operator()(
kSlotSmallVectorSize
>&
grads
,
// NOLINT
bool
create_graph
,
bool
is_new_grad
)
{
pybind11
::
gil_scoped_acquire
gil
;
VLOG
(
3
)
<<
"Running Eager Backward Node: "
<<
name
();
paddle
::
small_vector
<
std
::
vector
<
paddle
::
experimental
::
Tensor
>
,
...
...
paddle/fluid/pybind/eager_functions.cc
浏览文件 @
4e1f7692
...
...
@@ -119,9 +119,12 @@ static PyObject* eager_api_run_backward(PyObject* self,
EAGER_TRY
auto
tensors
=
CastPyArg2VectorOfTensor
(
PyTuple_GET_ITEM
(
args
,
0
),
0
);
auto
grad_tensors
=
CastPyArg2VectorOfTensor
(
PyTuple_GET_ITEM
(
args
,
1
),
1
);
egr
::
Backward
(
tensors
,
grad_tensors
,
CastPyArg2AttrBoolean
(
PyTuple_GET_ITEM
(
args
,
2
),
2
));
{
eager_gil_scoped_release
guard
;
egr
::
Backward
(
tensors
,
grad_tensors
,
CastPyArg2AttrBoolean
(
PyTuple_GET_ITEM
(
args
,
2
),
2
));
}
RETURN_PY_NONE
EAGER_CATCH_AND_THROW_RETURN_NULL
}
...
...
@@ -138,15 +141,18 @@ static PyObject* eager_api_run_partial_grad(PyObject* self,
auto
only_inputs
=
CastPyArg2AttrBoolean
(
PyTuple_GET_ITEM
(
args
,
5
),
5
);
auto
allow_unused
=
CastPyArg2AttrBoolean
(
PyTuple_GET_ITEM
(
args
,
6
),
6
);
auto
no_grad_vars
=
CastPyArg2VectorOfTensor
(
PyTuple_GET_ITEM
(
args
,
7
),
7
);
std
::
vector
<
paddle
::
experimental
::
Tensor
>
result
=
egr
::
Grad
(
tensors
,
inputs
,
grad_tensors
,
retain_graph
,
create_graph
,
only_inputs
,
allow_unused
,
no_grad_vars
);
std
::
vector
<
paddle
::
experimental
::
Tensor
>
result
;
{
eager_gil_scoped_release
guard
;
result
=
egr
::
Grad
(
tensors
,
inputs
,
grad_tensors
,
retain_graph
,
create_graph
,
only_inputs
,
allow_unused
,
no_grad_vars
);
}
VLOG
(
1
)
<<
" in eager_api_run_partial_grad, after runing egr::Grad"
;
return
ToPyObject
(
result
,
true
/* return_py_none_if_not_initialize */
);
EAGER_CATCH_AND_THROW_RETURN_NULL
...
...
@@ -179,18 +185,21 @@ static PyObject* eager_api_read_next_tensor_list(PyObject* self,
auto
tensor_base_list
=
CastPyArg2VectorOfTensorBase
(
PyTuple_GET_ITEM
(
args
,
0
),
0
);
std
::
vector
<
paddle
::
experimental
::
Tensor
>
tensor_list
;
tensor_list
.
reserve
(
tensor_base_list
.
size
());
auto
func
=
[](
framework
::
Tensor
&
tensor_base
)
{
paddle
::
experimental
::
Tensor
tensor
(
egr
::
Controller
::
Instance
().
GenerateUniqueName
());
auto
autograd_meta
=
egr
::
EagerUtils
::
autograd_meta
(
&
tensor
);
autograd_meta
->
SetPersistable
(
false
);
autograd_meta
->
SetStopGradient
(
true
);
tensor
.
set_impl
(
std
::
make_shared
<
phi
::
DenseTensor
>
(
tensor_base
));
return
tensor
;
};
for
(
auto
&
tensor_base
:
tensor_base_list
)
{
tensor_list
.
emplace_back
(
func
(
tensor_base
));
{
eager_gil_scoped_release
guard
;
tensor_list
.
reserve
(
tensor_base_list
.
size
());
auto
func
=
[](
framework
::
Tensor
&
tensor_base
)
{
paddle
::
experimental
::
Tensor
tensor
(
egr
::
Controller
::
Instance
().
GenerateUniqueName
());
auto
autograd_meta
=
egr
::
EagerUtils
::
autograd_meta
(
&
tensor
);
autograd_meta
->
SetPersistable
(
false
);
autograd_meta
->
SetStopGradient
(
true
);
tensor
.
set_impl
(
std
::
make_shared
<
phi
::
DenseTensor
>
(
tensor_base
));
return
tensor
;
};
for
(
auto
&
tensor_base
:
tensor_base_list
)
{
tensor_list
.
emplace_back
(
func
(
tensor_base
));
}
}
return
ToPyObject
(
tensor_list
);
EAGER_CATCH_AND_THROW_RETURN_NULL
...
...
paddle/fluid/pybind/eager_utils.h
浏览文件 @
4e1f7692
...
...
@@ -253,5 +253,17 @@ std::vector<paddle::framework::Scope*> GetScopePtrListFromArgs(
ssize_t
arg_idx
,
bool
dispensable
);
class
eager_gil_scoped_release
{
public:
eager_gil_scoped_release
()
{
tstate
=
PyEval_SaveThread
();
}
~
eager_gil_scoped_release
()
{
if
(
!
tstate
)
return
;
PyEval_RestoreThread
(
tstate
);
}
private:
PyThreadState
*
tstate
{
nullptr
};
};
}
// namespace pybind
}
// namespace paddle
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录