Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
5900129c
P
Paddle
项目概览
PaddlePaddle
/
Paddle
接近 1 年 前同步成功
通知
2292
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
5900129c
编写于
11月 10, 2022
作者:
W
WangZhen
提交者:
GitHub
11月 10, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Get grads types from cpp for adam to speed up (#47769)
Get grads types from cpp for adam to speed up
上级
8d99dd0c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
86 addition
and
21 deletion
+86
-21
paddle/fluid/pybind/eager_functions.cc
paddle/fluid/pybind/eager_functions.cc
+38
-0
paddle/fluid/pybind/eager_utils.cc
paddle/fluid/pybind/eager_utils.cc
+5
-2
paddle/fluid/pybind/eager_utils.h
paddle/fluid/pybind/eager_utils.h
+2
-1
python/paddle/optimizer/adam.py
python/paddle/optimizer/adam.py
+41
-18
未找到文件。
paddle/fluid/pybind/eager_functions.cc
浏览文件 @
5900129c
...
...
@@ -248,6 +248,40 @@ PyObject* eager_api_get_grads_lists(PyObject* self,
EAGER_CATCH_AND_THROW_RETURN_NULL
}
PyObject
*
eager_api_get_grads_types
(
PyObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
EAGER_TRY
auto
tensor_list
=
CastPyArg2VectorOfTensor
(
PyTuple_GET_ITEM
(
args
,
0
),
0
);
std
::
vector
<
int
>
ret
;
for
(
auto
&
tensor
:
tensor_list
)
{
VLOG
(
6
)
<<
"Get grad for tensor: "
<<
tensor
.
name
();
auto
meta
=
egr
::
EagerUtils
::
nullable_autograd_meta
(
tensor
);
if
(
!
meta
||
meta
->
StopGradient
())
{
ret
.
emplace_back
(
-
1
);
continue
;
}
auto
&
grad
=
meta
->
Grad
();
if
(
meta
&&
grad
.
initialized
())
{
if
(
grad
.
is_dense_tensor
()
&&
(
tensor
.
dtype
()
==
paddle
::
experimental
::
DataType
::
FLOAT32
||
tensor
.
dtype
()
==
paddle
::
experimental
::
DataType
::
FLOAT16
))
{
ret
.
emplace_back
(
paddle
::
framework
::
TransToProtoVarType
(
tensor
.
dtype
()));
}
}
else
{
ret
.
emplace_back
(
-
1
);
}
}
return
ToPyObject
(
ret
);
EAGER_CATCH_AND_THROW_RETURN_NULL
}
static
PyObject
*
eager_api_read_next_tensor_list
(
PyObject
*
self
,
PyObject
*
args
,
PyObject
*
kwargs
)
{
...
...
@@ -1067,6 +1101,10 @@ PyMethodDef variable_functions[] = {
(
PyCFunction
)(
void
(
*
)(
void
))
eager_api_get_grads_lists
,
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
{
"get_grads_types"
,
(
PyCFunction
)(
void
(
*
)(
void
))
eager_api_get_grads_types
,
METH_VARARGS
|
METH_KEYWORDS
,
NULL
},
{
"read_next_tensor_list"
,
(
PyCFunction
)(
void
(
*
)(
void
))
eager_api_read_next_tensor_list
,
METH_VARARGS
|
METH_KEYWORDS
,
...
...
paddle/fluid/pybind/eager_utils.cc
浏览文件 @
5900129c
...
...
@@ -721,11 +721,14 @@ PyObject* ToPyObject(const std::vector<paddle::experimental::Tensor>& value,
}
PyObject
*
ToPyObject
(
const
std
::
vector
<
std
::
vector
<
paddle
::
experimental
::
Tensor
>>&
value
)
{
const
std
::
vector
<
std
::
vector
<
paddle
::
experimental
::
Tensor
>>&
value
,
bool
return_py_none_if_not_initialize
)
{
PyObject
*
result
=
PyList_New
((
Py_ssize_t
)
value
.
size
());
for
(
size_t
i
=
0
;
i
<
value
.
size
();
i
++
)
{
PyList_SET_ITEM
(
result
,
static_cast
<
Py_ssize_t
>
(
i
),
ToPyObject
(
value
[
i
]));
PyList_SET_ITEM
(
result
,
static_cast
<
Py_ssize_t
>
(
i
),
ToPyObject
(
value
[
i
],
return_py_none_if_not_initialize
));
}
return
result
;
...
...
paddle/fluid/pybind/eager_utils.h
浏览文件 @
5900129c
...
...
@@ -104,7 +104,8 @@ PyObject* ToPyObject(const std::vector<std::vector<size_t>>& value);
PyObject
*
ToPyObject
(
const
std
::
vector
<
paddle
::
experimental
::
Tensor
>&
value
,
bool
return_py_none_if_not_initialize
=
false
);
PyObject
*
ToPyObject
(
const
std
::
vector
<
std
::
vector
<
paddle
::
experimental
::
Tensor
>>&
value
);
const
std
::
vector
<
std
::
vector
<
paddle
::
experimental
::
Tensor
>>&
value
,
bool
return_py_none_if_not_initialize
=
false
);
PyObject
*
ToPyObject
(
const
platform
::
Place
&
value
);
PyObject
*
ToPyObject
(
const
phi
::
DenseTensor
*
value
);
PyObject
*
ToPyObject
(
const
phi
::
SelectedRows
*
value
);
...
...
python/paddle/optimizer/adam.py
浏览文件 @
5900129c
...
...
@@ -28,6 +28,8 @@ from paddle import _C_ops, _legacy_C_ops
__all__
=
[]
GRAD_TYPES
=
[
int
(
paddle
.
float32
),
int
(
paddle
.
float16
)]
class
Adam
(
Optimizer
):
r
"""
...
...
@@ -644,26 +646,47 @@ class Adam(Optimizer):
lr_dict
=
{
'FP32_LODTensor'
:
[],
'FP16_LODTensor'
:
[]}
if
isinstance
(
parameters_and_grads
,
list
):
for
param_and_grad
in
parameters_and_grads
:
if
param_and_grad
[
1
]
is
None
:
continue
if
param_and_grad
[
0
].
stop_gradient
is
False
:
if
(
param_and_grad
[
0
].
dtype
==
paddle
.
float32
and
param_and_grad
[
1
].
type
==
core
.
VarDesc
.
VarType
.
LOD_TENSOR
):
grad_dict
[
'FP32_LODTensor'
].
append
(
param_and_grad
[
1
])
lr
=
self
.
_create_param_lr
(
param_and_grad
)
if
framework
.
in_dygraph_mode
():
params
=
[
pair
[
0
]
for
pair
in
parameters_and_grads
]
grads_types
=
core
.
eager
.
get_grads_types
(
params
)
for
index
,
tp
in
enumerate
(
grads_types
):
if
tp
==
GRAD_TYPES
[
0
]:
grad_dict
[
'FP32_LODTensor'
].
append
(
parameters_and_grads
[
index
][
1
]
)
lr
=
self
.
_create_param_lr
(
parameters_and_grads
[
index
])
lr_dict
[
'FP32_LODTensor'
].
append
(
lr
)
elif
(
param_and_grad
[
0
].
dtype
==
paddle
.
float16
and
param_and_grad
[
1
].
type
==
core
.
VarDesc
.
VarType
.
LOD_TENSOR
):
grad_dict
[
'FP16_LODTensor'
].
append
(
param_and_grad
[
1
])
lr
=
self
.
_create_param_lr
(
param_and_grad
)
elif
tp
==
GRAD_TYPES
[
1
]:
grad_dict
[
'FP16_LODTensor'
].
append
(
parameters_and_grads
[
index
][
1
]
)
lr
=
self
.
_create_param_lr
(
parameters_and_grads
[
index
])
lr_dict
[
'FP16_LODTensor'
].
append
(
lr
)
else
:
for
param_and_grad
in
parameters_and_grads
:
if
param_and_grad
[
1
]
is
None
:
continue
if
param_and_grad
[
0
].
stop_gradient
is
False
:
if
(
param_and_grad
[
0
].
dtype
==
paddle
.
float32
and
param_and_grad
[
1
].
type
==
core
.
VarDesc
.
VarType
.
LOD_TENSOR
):
grad_dict
[
'FP32_LODTensor'
].
append
(
param_and_grad
[
1
]
)
lr
=
self
.
_create_param_lr
(
param_and_grad
)
lr_dict
[
'FP32_LODTensor'
].
append
(
lr
)
elif
(
param_and_grad
[
0
].
dtype
==
paddle
.
float16
and
param_and_grad
[
1
].
type
==
core
.
VarDesc
.
VarType
.
LOD_TENSOR
):
grad_dict
[
'FP16_LODTensor'
].
append
(
param_and_grad
[
1
]
)
lr
=
self
.
_create_param_lr
(
param_and_grad
)
lr_dict
[
'FP16_LODTensor'
].
append
(
lr
)
else
:
for
param_and_grad
in
parameters_and_grads
[
'params'
]:
if
param_and_grad
[
1
]
is
None
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录