Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0175f566
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 2 年 前同步成功
通知
2325
Star
20933
Fork
5424
代码
文件
提交
分支
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看板
未验证
提交
0175f566
编写于
2月 10, 2021
作者:
S
ShenLiang
提交者:
GitHub
2月 10, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[cherry-pick] Solve inconsistent order in each card in dynamic graph (#30965)
* support if else control * fix conflict
上级
aaaae6b4
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
48 addition
and
39 deletion
+48
-39
paddle/fluid/imperative/reducer.cc
paddle/fluid/imperative/reducer.cc
+45
-38
paddle/fluid/imperative/reducer.h
paddle/fluid/imperative/reducer.h
+3
-1
未找到文件。
paddle/fluid/imperative/reducer.cc
浏览文件 @
0175f566
...
@@ -22,11 +22,6 @@ std::shared_ptr<Reducer> Reducer::s_instance_ = NULL;
...
@@ -22,11 +22,6 @@ std::shared_ptr<Reducer> Reducer::s_instance_ = NULL;
// context is used to select the stream for concat
// context is used to select the stream for concat
void
Group
::
ConcatTensors
(
const
platform
::
CUDADeviceContext
&
context
)
{
void
Group
::
ConcatTensors
(
const
platform
::
CUDADeviceContext
&
context
)
{
VLOG
(
3
)
<<
"Before concat, set output tensor size is "
<<
all_length_
;
auto
tensor
=
dense_contents_
.
GetMutable
<
framework
::
LoDTensor
>
();
tensor
->
Resize
(
framework
::
make_ddim
({
all_length_
}))
.
mutable_data
(
context
.
GetPlace
(),
dtype_
);
switch
(
dtype_
)
{
switch
(
dtype_
)
{
case
framework
::
proto
::
VarType
::
FP16
:
case
framework
::
proto
::
VarType
::
FP16
:
ConcatTensorsForAllReduce
<
platform
::
float16
>
(
context
,
dense_tensors_
,
ConcatTensorsForAllReduce
<
platform
::
float16
>
(
context
,
dense_tensors_
,
...
@@ -179,6 +174,9 @@ void Reducer::InitializeDenseGroups(
...
@@ -179,6 +174,9 @@ void Reducer::InitializeDenseGroups(
p_group
->
length_
.
push_back
(
size
);
p_group
->
length_
.
push_back
(
size
);
// for concat operator
p_group
->
dense_tensors_
.
push_back
(
framework
::
Tensor
());
// check the dtype and place, it must be same.
// check the dtype and place, it must be same.
auto
dtype
=
var
->
DataType
();
auto
dtype
=
var
->
DataType
();
auto
place
=
var
->
Place
();
auto
place
=
var
->
Place
();
...
@@ -200,6 +198,7 @@ void Reducer::InitializeDenseGroups(
...
@@ -200,6 +198,7 @@ void Reducer::InitializeDenseGroups(
place_
=
place
;
place_
=
place
;
}
}
}
}
p_group
->
all_length_
=
all_length
;
}
}
// Each parameter will be initialized according to the group information.
// Each parameter will be initialized according to the group information.
...
@@ -234,6 +233,9 @@ void Reducer::InitializeGroups(
...
@@ -234,6 +233,9 @@ void Reducer::InitializeGroups(
}
else
{
}
else
{
// process the dense gradient.
// process the dense gradient.
InitializeDenseGroups
(
variable_indices_
,
&
group
);
InitializeDenseGroups
(
variable_indices_
,
&
group
);
auto
tensor
=
group
.
dense_contents_
.
GetMutable
<
framework
::
LoDTensor
>
();
tensor
->
Resize
(
framework
::
make_ddim
({
group
.
all_length_
}))
.
mutable_data
(
place_
,
group
.
dtype_
);
}
}
// map variables to this group by VariableLocator
// map variables to this group by VariableLocator
...
@@ -295,9 +297,6 @@ void Reducer::PrepareForBackward(
...
@@ -295,9 +297,6 @@ void Reducer::PrepareForBackward(
next_group_
=
0
;
next_group_
=
0
;
std
::
for_each
(
groups_
.
begin
(),
groups_
.
end
(),
[](
Group
&
group
)
{
std
::
for_each
(
groups_
.
begin
(),
groups_
.
end
(),
[](
Group
&
group
)
{
group
.
pending_
=
group
.
variable_indices_
.
size
();
group
.
pending_
=
group
.
variable_indices_
.
size
();
group
.
all_length_
=
0
;
group
.
dense_tensors_
.
clear
();
group
.
dense_tensors_
.
reserve
(
group
.
pending_
);
group
.
sparse_contents_
=
nullptr
;
group
.
sparse_contents_
=
nullptr
;
});
});
...
@@ -423,22 +422,35 @@ void Reducer::MarkVarReady(const size_t var_index, const bool is_used_var) {
...
@@ -423,22 +422,35 @@ void Reducer::MarkVarReady(const size_t var_index, const bool is_used_var) {
auto
group_index
=
var_locator
.
group_index
;
auto
group_index
=
var_locator
.
group_index
;
auto
&
group
=
groups_
[
group_index
];
auto
&
group
=
groups_
[
group_index
];
if
(
is_used_var
)
{
if
(
!
group
.
is_sparse_
)
{
auto
var_warpper
=
vars_
[
var_index
]
->
GradVarBase
()
->
SharedVar
();
// process dense group
if
(
!
group
.
is_sparse_
)
{
auto
inside_group_index
=
var_locator
.
inside_group_index
;
auto
grad
=
var_warpper
->
MutableVar
();
auto
length
=
group
.
length_
[
inside_group_index
];
auto
inside_group_index
=
var_locator
.
inside_group_index
;
auto
&
group_tensor
=
group
.
dense_tensors_
[
inside_group_index
];
auto
length
=
group
.
length_
[
inside_group_index
];
if
(
is_used_var
)
{
auto
var_warpper
=
vars_
[
var_index
]
->
GradVarBase
()
->
SharedVar
();
auto
tensor
=
grad
->
GetMutable
<
framework
::
LoDTensor
>
();
auto
tensor
=
framework
::
Tensor
tmp
;
var_warpper
->
MutableVar
()
->
GetMutable
<
framework
::
LoDTensor
>
();
tmp
.
ShareDataWith
(
*
tensor
).
Resize
({
static_cast
<
int64_t
>
(
length
)});
group_tensor
.
ShareDataWith
(
*
tensor
).
Resize
(
group
.
dense_tensors_
.
push_back
(
std
::
move
(
tmp
));
{
static_cast
<
int64_t
>
(
length
)});
group
.
all_length_
+=
length
;
}
else
{
}
else
{
if
(
!
group_tensor
.
IsInitialized
())
{
group_tensor
.
Resize
({
static_cast
<
int64_t
>
(
length
)});
group_tensor
.
mutable_data
(
place_
,
group
.
dtype_
);
auto
*
dev_ctx
=
platform
::
DeviceContextPool
::
Instance
().
Get
(
place_
);
operators
::
math
::
set_constant
(
*
dev_ctx
,
&
group_tensor
,
0.0
);
}
}
}
else
{
// process sparse group
if
(
is_used_var
)
{
auto
var_warpper
=
vars_
[
var_index
]
->
GradVarBase
()
->
SharedVar
();
group
.
sparse_contents_
=
var_warpper
->
MutableVar
();
group
.
sparse_contents_
=
var_warpper
->
MutableVar
();
}
else
{
group
.
sparse_contents_
=
nullptr
;
}
}
}
}
if
(
--
group
.
pending_
==
0
)
{
if
(
--
group
.
pending_
==
0
)
{
// can start allreduce
// can start allreduce
MarkGroupReady
(
group_index
);
MarkGroupReady
(
group_index
);
...
@@ -478,24 +490,19 @@ void Reducer::MarkGroupReady(size_t group_index) {
...
@@ -478,24 +490,19 @@ void Reducer::MarkGroupReady(size_t group_index) {
<<
"] has no var to allreduce"
;
<<
"] has no var to allreduce"
;
}
}
}
else
{
}
else
{
if
(
!
group
.
dense_tensors_
.
empty
())
{
VLOG
(
3
)
<<
"dense group ["
<<
next_group_
<<
"] start allreduce in ring["
VLOG
(
3
)
<<
"dense group ["
<<
next_group_
<<
run_order
<<
"]"
;
<<
"] start allreduce in ring["
<<
run_order
<<
"]"
;
// Select common commstream to concat tensors
// Select common commstream to concat tensors
// group.dense_tensors ---> group.dense_contents_
// group.dense_tensors ---> group.dense_contents_
group
.
ConcatTensors
(
*
parallel_ctx_
->
GetDeviceContext
(
run_order
));
group
.
ConcatTensors
(
*
parallel_ctx_
->
GetDeviceContext
(
run_order
));
// Start allreduce
// Start allreduce
parallel_ctx_
->
AllReduceByStream
(
parallel_ctx_
->
AllReduceByStream
(
group
.
dense_contents_
,
&
(
group
.
dense_contents_
),
run_order
,
false
);
group
.
dense_contents_
,
&
(
group
.
dense_contents_
),
run_order
,
false
);
// Select common commstream to split tensors
// Select common commstream to split tensors
// group.dense_contents_ ---> group.dense_tensors
// group.dense_contents_ ---> group.dense_tensors
group
.
SplitTensors
(
*
parallel_ctx_
->
GetDeviceContext
(
run_order
));
group
.
SplitTensors
(
*
parallel_ctx_
->
GetDeviceContext
(
run_order
));
}
else
{
VLOG
(
3
)
<<
"The dense group["
<<
next_group_
<<
"] has no var to allreduce"
;
}
}
}
}
}
}
}
...
...
paddle/fluid/imperative/reducer.h
浏览文件 @
0175f566
...
@@ -29,6 +29,7 @@
...
@@ -29,6 +29,7 @@
#include "paddle/fluid/imperative/op_base.h"
#include "paddle/fluid/imperative/op_base.h"
#include "paddle/fluid/imperative/variable_wrapper.h"
#include "paddle/fluid/imperative/variable_wrapper.h"
#include "paddle/fluid/memory/memory.h"
#include "paddle/fluid/memory/memory.h"
#include "paddle/fluid/operators/math/math_function.h"
#include "paddle/fluid/string/string_helper.h"
#include "paddle/fluid/string/string_helper.h"
#if defined(PADDLE_WITH_NCCL)
#if defined(PADDLE_WITH_NCCL)
...
@@ -201,7 +202,8 @@ class Reducer {
...
@@ -201,7 +202,8 @@ class Reducer {
int
nrings_
=
1
;
int
nrings_
=
1
;
// Following variables are to help rebuild group
// Following variables are to help rebuild group
bool
has_rebuilt_group_
{
false
};
// TODO(shenliang03): Support rebuild in the future.
bool
has_rebuilt_group_
{
true
};
std
::
vector
<
std
::
shared_ptr
<
imperative
::
VarBase
>>
rebuild_vars_
;
std
::
vector
<
std
::
shared_ptr
<
imperative
::
VarBase
>>
rebuild_vars_
;
std
::
vector
<
int64_t
>
rebuild_var_indices_
;
std
::
vector
<
int64_t
>
rebuild_var_indices_
;
const
std
::
vector
<
size_t
>
group_size_limits_
;
const
std
::
vector
<
size_t
>
group_size_limits_
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录