Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
336160cf
P
Paddle
项目概览
PaddlePaddle
/
Paddle
接近 2 年 前同步成功
通知
2323
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看板
未验证
提交
336160cf
编写于
3月 30, 2023
作者:
W
wanghuancoder
提交者:
GitHub
3月 30, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
force sync batch norm grad sequential (#52268)
* force sync batch norm grad sequential
上级
551ff882
变更
9
展开全部
隐藏空白更改
内联
并排
Showing
9 changed file
with
1435 addition
and
4 deletion
+1435
-4
paddle/fluid/eager/api/manual/eager_manual/dygraph_forward_api.h
...fluid/eager/api/manual/eager_manual/dygraph_forward_api.h
+37
-0
paddle/fluid/eager/api/manual/eager_manual/forwards/CMakeLists.txt
...uid/eager/api/manual/eager_manual/forwards/CMakeLists.txt
+1
-0
paddle/fluid/eager/api/manual/eager_manual/forwards/sync_batch_norm_fwd_func.cc
.../manual/eager_manual/forwards/sync_batch_norm_fwd_func.cc
+699
-0
paddle/fluid/eager/api/manual/eager_manual/nodes/CMakeLists.txt
.../fluid/eager/api/manual/eager_manual/nodes/CMakeLists.txt
+1
-0
paddle/fluid/eager/api/manual/eager_manual/nodes/nodes.h
paddle/fluid/eager/api/manual/eager_manual/nodes/nodes.h
+171
-0
paddle/fluid/eager/api/manual/eager_manual/nodes/sync_batch_norm_node.cc
...ger/api/manual/eager_manual/nodes/sync_batch_norm_node.cc
+468
-0
paddle/fluid/eager/api/utils/global_utils.h
paddle/fluid/eager/api/utils/global_utils.h
+15
-0
paddle/fluid/eager/auto_code_generator/generator/eager_gen.py
...le/fluid/eager/auto_code_generator/generator/eager_gen.py
+1
-0
paddle/fluid/eager/backward.cc
paddle/fluid/eager/backward.cc
+42
-4
未找到文件。
paddle/fluid/eager/api/manual/eager_manual/dygraph_forward_api.h
浏览文件 @
336160cf
...
@@ -26,3 +26,40 @@ paddle::Tensor conv2d_ad_func(const paddle::Tensor& input,
...
@@ -26,3 +26,40 @@ paddle::Tensor conv2d_ad_func(const paddle::Tensor& input,
std
::
vector
<
int
>
dilations
,
std
::
vector
<
int
>
dilations
,
int
groups
,
int
groups
,
std
::
string
data_format
);
std
::
string
data_format
);
std
::
tuple
<
paddle
::
Tensor
,
paddle
::
Tensor
&
,
paddle
::
Tensor
&
,
paddle
::
Tensor
,
paddle
::
Tensor
,
paddle
::
Tensor
>
sync_batch_norm__ad_func
(
const
paddle
::
Tensor
&
x
,
paddle
::
Tensor
&
mean
,
// NOLINT
paddle
::
Tensor
&
variance
,
// NOLINT
const
paddle
::
Tensor
&
scale
,
const
paddle
::
Tensor
&
bias
,
bool
is_test
,
float
momentum
,
float
epsilon
,
std
::
string
data_layout
,
bool
use_global_stats
,
bool
trainable_statistics
);
namespace
sparse
{
std
::
tuple
<
paddle
::
Tensor
,
paddle
::
Tensor
&
,
paddle
::
Tensor
&
,
paddle
::
Tensor
,
paddle
::
Tensor
,
paddle
::
Tensor
>
sync_batch_norm__ad_func
(
const
paddle
::
Tensor
&
x
,
paddle
::
Tensor
&
mean
,
// NOLINT
paddle
::
Tensor
&
variance
,
// NOLINT
const
paddle
::
Tensor
&
scale
,
const
paddle
::
Tensor
&
bias
,
bool
is_test
,
float
momentum
,
float
epsilon
,
std
::
string
data_layout
,
bool
use_global_stats
,
bool
trainable_statistics
);
}
// namespace sparse
paddle/fluid/eager/api/manual/eager_manual/forwards/CMakeLists.txt
浏览文件 @
336160cf
set
(
eager_manual_functions
set
(
eager_manual_functions
${
PADDLE_SOURCE_DIR
}
/paddle/fluid/eager/api/manual/eager_manual/forwards/add_n_fwd_func.cc
${
PADDLE_SOURCE_DIR
}
/paddle/fluid/eager/api/manual/eager_manual/forwards/add_n_fwd_func.cc
${
PADDLE_SOURCE_DIR
}
/paddle/fluid/eager/api/manual/eager_manual/forwards/conv2d_fwd_function.cc
${
PADDLE_SOURCE_DIR
}
/paddle/fluid/eager/api/manual/eager_manual/forwards/conv2d_fwd_function.cc
${
PADDLE_SOURCE_DIR
}
/paddle/fluid/eager/api/manual/eager_manual/forwards/sync_batch_norm_fwd_func.cc
PARENT_SCOPE
)
PARENT_SCOPE
)
paddle/fluid/eager/api/manual/eager_manual/forwards/sync_batch_norm_fwd_func.cc
0 → 100644
浏览文件 @
336160cf
此差异已折叠。
点击以展开。
paddle/fluid/eager/api/manual/eager_manual/nodes/CMakeLists.txt
浏览文件 @
336160cf
set
(
eager_manual_nodes
set
(
eager_manual_nodes
${
PADDLE_SOURCE_DIR
}
/paddle/fluid/eager/api/manual/eager_manual/nodes/conv2d_nodes.cc
${
PADDLE_SOURCE_DIR
}
/paddle/fluid/eager/api/manual/eager_manual/nodes/conv2d_nodes.cc
${
PADDLE_SOURCE_DIR
}
/paddle/fluid/eager/api/manual/eager_manual/nodes/add_n_node.cc
${
PADDLE_SOURCE_DIR
}
/paddle/fluid/eager/api/manual/eager_manual/nodes/add_n_node.cc
${
PADDLE_SOURCE_DIR
}
/paddle/fluid/eager/api/manual/eager_manual/nodes/sync_batch_norm_node.cc
PARENT_SCOPE
)
PARENT_SCOPE
)
paddle/fluid/eager/api/manual/eager_manual/nodes/nodes.h
浏览文件 @
336160cf
...
@@ -204,3 +204,174 @@ class AddNGradNodeFinal : public egr::GradNodeBase {
...
@@ -204,3 +204,174 @@ class AddNGradNodeFinal : public egr::GradNodeBase {
// Attributes
// Attributes
};
};
class
SyncBatchNormGradNode
:
public
egr
::
GradNodeBase
{
public:
SyncBatchNormGradNode
()
:
egr
::
GradNodeBase
()
{}
SyncBatchNormGradNode
(
size_t
bwd_in_slot_num
,
size_t
bwd_out_slot_num
)
:
egr
::
GradNodeBase
(
bwd_in_slot_num
,
bwd_out_slot_num
)
{}
~
SyncBatchNormGradNode
()
override
=
default
;
virtual
paddle
::
small_vector
<
std
::
vector
<
paddle
::
Tensor
>
,
egr
::
kSlotSmallVectorSize
>
operator
()(
paddle
::
small_vector
<
std
::
vector
<
paddle
::
Tensor
>
,
egr
::
kSlotSmallVectorSize
>&
grads
,
// NOLINT
bool
create_graph
=
false
,
bool
is_new_grad
=
false
)
override
;
std
::
string
name
()
override
{
return
"SyncBatchNormGradNode"
;
}
void
ClearTensorWrappers
()
override
{
x_
.
clear
();
scale_
.
clear
();
bias_
.
clear
();
saved_mean_
.
clear
();
saved_variance_
.
clear
();
reserve_space_
.
clear
();
SetIsTensorWrappersCleared
(
true
);
}
std
::
shared_ptr
<
GradNodeBase
>
Copy
()
const
override
{
auto
copied_node
=
std
::
shared_ptr
<
SyncBatchNormGradNode
>
(
new
SyncBatchNormGradNode
(
*
this
));
return
copied_node
;
}
// SetTensorWrapperX, SetTensorWrapperY, ...
void
SetTensorWrapperx
(
const
paddle
::
Tensor
&
x
)
{
x_
=
egr
::
TensorWrapper
(
x
,
false
);
}
void
SetTensorWrapperscale
(
const
paddle
::
Tensor
&
scale
)
{
scale_
=
egr
::
TensorWrapper
(
scale
,
false
);
}
void
SetTensorWrapperbias
(
const
paddle
::
Tensor
&
bias
)
{
bias_
=
egr
::
TensorWrapper
(
bias
,
false
);
}
void
SetTensorWrappersaved_mean
(
const
paddle
::
Tensor
&
saved_mean
)
{
saved_mean_
=
egr
::
TensorWrapper
(
saved_mean
,
false
);
}
void
SetTensorWrappersaved_variance
(
const
paddle
::
Tensor
&
saved_variance
)
{
saved_variance_
=
egr
::
TensorWrapper
(
saved_variance
,
false
);
}
void
SetTensorWrapperreserve_space
(
const
paddle
::
Tensor
&
reserve_space
)
{
reserve_space_
=
egr
::
TensorWrapper
(
reserve_space
,
false
);
}
// SetAttributes
void
SetAttributemomentum
(
const
float
&
momentum
)
{
momentum_
=
momentum
;
}
void
SetAttributeepsilon
(
const
float
&
epsilon
)
{
epsilon_
=
epsilon
;
}
void
SetAttributedata_layout
(
const
std
::
string
&
data_layout
)
{
data_layout_
=
data_layout
;
}
void
SetAttributeis_test
(
const
bool
&
is_test
)
{
is_test_
=
is_test
;
}
void
SetAttributeuse_global_stats
(
const
bool
&
use_global_stats
)
{
use_global_stats_
=
use_global_stats
;
}
void
SetAttributetrainable_statistics
(
const
bool
&
trainable_statistics
)
{
trainable_statistics_
=
trainable_statistics
;
}
private:
// TensorWrappers
egr
::
TensorWrapper
x_
;
egr
::
TensorWrapper
scale_
;
egr
::
TensorWrapper
bias_
;
egr
::
TensorWrapper
saved_mean_
;
egr
::
TensorWrapper
saved_variance_
;
egr
::
TensorWrapper
reserve_space_
;
// Attributes
float
momentum_
;
float
epsilon_
;
std
::
string
data_layout_
;
bool
is_test_
;
bool
use_global_stats_
;
bool
trainable_statistics_
;
};
namespace
sparse
{
class
SyncBatchNormGradNode
:
public
egr
::
GradNodeBase
{
public:
SyncBatchNormGradNode
()
:
egr
::
GradNodeBase
()
{}
SyncBatchNormGradNode
(
size_t
bwd_in_slot_num
,
size_t
bwd_out_slot_num
)
:
egr
::
GradNodeBase
(
bwd_in_slot_num
,
bwd_out_slot_num
)
{}
~
SyncBatchNormGradNode
()
override
=
default
;
virtual
paddle
::
small_vector
<
std
::
vector
<
paddle
::
Tensor
>
,
egr
::
kSlotSmallVectorSize
>
operator
()(
paddle
::
small_vector
<
std
::
vector
<
paddle
::
Tensor
>
,
egr
::
kSlotSmallVectorSize
>&
grads
,
// NOLINT
bool
create_graph
=
false
,
bool
is_new_grad
=
false
)
override
;
std
::
string
name
()
override
{
return
"SyncBatchNormGradNode"
;
}
void
ClearTensorWrappers
()
override
{
x_
.
clear
();
scale_
.
clear
();
bias_
.
clear
();
saved_mean_
.
clear
();
saved_variance_
.
clear
();
reserve_space_
.
clear
();
SetIsTensorWrappersCleared
(
true
);
}
std
::
shared_ptr
<
GradNodeBase
>
Copy
()
const
override
{
auto
copied_node
=
std
::
shared_ptr
<
SyncBatchNormGradNode
>
(
new
SyncBatchNormGradNode
(
*
this
));
return
copied_node
;
}
// SetTensorWrapperX, SetTensorWrapperY, ...
void
SetTensorWrapperx
(
const
paddle
::
Tensor
&
x
)
{
x_
=
egr
::
TensorWrapper
(
x
,
false
);
}
void
SetTensorWrapperscale
(
const
paddle
::
Tensor
&
scale
)
{
scale_
=
egr
::
TensorWrapper
(
scale
,
false
);
}
void
SetTensorWrapperbias
(
const
paddle
::
Tensor
&
bias
)
{
bias_
=
egr
::
TensorWrapper
(
bias
,
false
);
}
void
SetTensorWrappersaved_mean
(
const
paddle
::
Tensor
&
saved_mean
)
{
saved_mean_
=
egr
::
TensorWrapper
(
saved_mean
,
false
);
}
void
SetTensorWrappersaved_variance
(
const
paddle
::
Tensor
&
saved_variance
)
{
saved_variance_
=
egr
::
TensorWrapper
(
saved_variance
,
false
);
}
void
SetTensorWrapperreserve_space
(
const
paddle
::
Tensor
&
reserve_space
)
{
reserve_space_
=
egr
::
TensorWrapper
(
reserve_space
,
false
);
}
// SetAttributes
void
SetAttributemomentum
(
const
float
&
momentum
)
{
momentum_
=
momentum
;
}
void
SetAttributeepsilon
(
const
float
&
epsilon
)
{
epsilon_
=
epsilon
;
}
void
SetAttributedata_layout
(
const
std
::
string
&
data_layout
)
{
data_layout_
=
data_layout
;
}
void
SetAttributeis_test
(
const
bool
&
is_test
)
{
is_test_
=
is_test
;
}
void
SetAttributeuse_global_stats
(
const
bool
&
use_global_stats
)
{
use_global_stats_
=
use_global_stats
;
}
void
SetAttributetrainable_statistics
(
const
bool
&
trainable_statistics
)
{
trainable_statistics_
=
trainable_statistics
;
}
private:
// TensorWrappers
egr
::
TensorWrapper
x_
;
egr
::
TensorWrapper
scale_
;
egr
::
TensorWrapper
bias_
;
egr
::
TensorWrapper
saved_mean_
;
egr
::
TensorWrapper
saved_variance_
;
egr
::
TensorWrapper
reserve_space_
;
// Attributes
float
momentum_
;
float
epsilon_
;
std
::
string
data_layout_
;
bool
is_test_
;
bool
use_global_stats_
;
bool
trainable_statistics_
;
};
}
// namespace sparse
paddle/fluid/eager/api/manual/eager_manual/nodes/sync_batch_norm_node.cc
0 → 100644
浏览文件 @
336160cf
此差异已折叠。
点击以展开。
paddle/fluid/eager/api/utils/global_utils.h
浏览文件 @
336160cf
...
@@ -40,6 +40,8 @@ class UniqueNameGenerator {
...
@@ -40,6 +40,8 @@ class UniqueNameGenerator {
// TODO(jiabin): Now we are using imperative tracer, move it here when we
// TODO(jiabin): Now we are using imperative tracer, move it here when we
// deprecate imperative.
// deprecate imperative.
class
GradNodeBase
;
class
Controller
{
class
Controller
{
public:
public:
static
Controller
&
Instance
()
{
return
*
controller_
;
}
static
Controller
&
Instance
()
{
return
*
controller_
;
}
...
@@ -119,6 +121,18 @@ class Controller {
...
@@ -119,6 +121,18 @@ class Controller {
void
ClearFinalBackwardHooks
()
{
final_backward_hooks_
.
clear
();
}
void
ClearFinalBackwardHooks
()
{
final_backward_hooks_
.
clear
();
}
void
ClearForceSequentialNodes
()
{
while
(
!
force_sequential_nodes_
.
empty
())
{
force_sequential_nodes_
.
pop
();
}
}
void
PushBackForceSequentialNodes
(
GradNodeBase
*
node
)
{
force_sequential_nodes_
.
push
(
node
);
}
std
::
queue
<
GradNodeBase
*>
GetForceSequentialNodes
()
{
return
force_sequential_nodes_
;
}
private:
private:
Controller
()
=
default
;
Controller
()
=
default
;
static
Controller
*
controller_
;
static
Controller
*
controller_
;
...
@@ -132,6 +146,7 @@ class Controller {
...
@@ -132,6 +146,7 @@ class Controller {
std
::
vector
<
std
::
vector
<
std
::
unordered_map
<
int
,
int
>>>>
std
::
vector
<
std
::
vector
<
std
::
unordered_map
<
int
,
int
>>>>
custom_edges_slot_map_
;
custom_edges_slot_map_
;
std
::
vector
<
std
::
shared_ptr
<
VoidHook
>>
final_backward_hooks_
;
std
::
vector
<
std
::
shared_ptr
<
VoidHook
>>
final_backward_hooks_
;
std
::
queue
<
GradNodeBase
*>
force_sequential_nodes_
;
DISABLE_COPY_AND_ASSIGN
(
Controller
);
DISABLE_COPY_AND_ASSIGN
(
Controller
);
};
};
...
...
paddle/fluid/eager/auto_code_generator/generator/eager_gen.py
浏览文件 @
336160cf
...
@@ -57,6 +57,7 @@ black_ops_list = [
...
@@ -57,6 +57,7 @@ black_ops_list = [
"conv2d_grad_grad"
,
"conv2d_grad_grad"
,
"add_n"
,
"add_n"
,
"add_n_grad"
,
"add_n_grad"
,
"sync_batch_norm_"
,
]
]
...
...
paddle/fluid/eager/backward.cc
浏览文件 @
336160cf
...
@@ -111,6 +111,22 @@ std::vector<paddle::Tensor> RunBackward(
...
@@ -111,6 +111,22 @@ std::vector<paddle::Tensor> RunBackward(
const
std
::
vector
<
paddle
::
Tensor
>&
no_grad_vars
=
{})
{
const
std
::
vector
<
paddle
::
Tensor
>&
no_grad_vars
=
{})
{
VLOG
(
3
)
<<
"Start Backward"
;
VLOG
(
3
)
<<
"Start Backward"
;
std
::
queue
<
GradNodeBase
*>
force_sequential_nodes_forward_queue
=
egr
::
Controller
::
Instance
().
GetForceSequentialNodes
();
egr
::
Controller
::
Instance
().
ClearForceSequentialNodes
();
std
::
deque
<
GradNodeBase
*>
force_sequential_nodes_queue
;
std
::
set
<
GradNodeBase
*>
force_sequential_nodes_set
;
std
::
set
<
GradNodeBase
*>
ready_force_sequential_nodes
;
auto
force_sequential_nodes_size
=
force_sequential_nodes_forward_queue
.
size
();
for
(
size_t
i
=
0
;
i
<
force_sequential_nodes_size
;
++
i
)
{
force_sequential_nodes_set
.
insert
(
force_sequential_nodes_forward_queue
.
front
());
force_sequential_nodes_queue
.
push_front
(
force_sequential_nodes_forward_queue
.
front
());
force_sequential_nodes_forward_queue
.
pop
();
}
// *Gradient Hook should happen at node-level
// *Gradient Hook should happen at node-level
// *Inplace version check should perform at node-level
// *Inplace version check should perform at node-level
// *Cross-batch accumulation happens at forward pass
// *Cross-batch accumulation happens at forward pass
...
@@ -355,12 +371,34 @@ std::vector<paddle::Tensor> RunBackward(
...
@@ -355,12 +371,34 @@ std::vector<paddle::Tensor> RunBackward(
"Node's in-degree cannot be negative."
,
"Node's in-degree cannot be negative."
,
next_node
->
name
()));
next_node
->
name
()));
if
(
node_in_degree_map
[
next_node
]
==
0
)
{
auto
add_next_node_func
=
[
&
node_in_degree_map
,
if
(
dynamic_cast
<
egr
::
GradNodeAccumulation
*>
(
next_node
))
{
&
queue
](
GradNodeBase
*
next_node
)
{
queue
.
push_front
(
std
::
move
(
next_node
));
if
(
node_in_degree_map
[
next_node
]
==
0
)
{
if
(
dynamic_cast
<
egr
::
GradNodeAccumulation
*>
(
next_node
))
{
queue
.
push_front
(
std
::
move
(
next_node
));
}
else
{
queue
.
push_back
(
std
::
move
(
next_node
));
}
}
};
if
(
force_sequential_nodes_set
.
count
(
next_node
))
{
if
(
force_sequential_nodes_queue
.
front
()
==
next_node
)
{
force_sequential_nodes_queue
.
pop_front
();
add_next_node_func
(
next_node
);
while
(
ready_force_sequential_nodes
.
count
(
force_sequential_nodes_queue
.
front
()))
{
ready_force_sequential_nodes
.
erase
(
force_sequential_nodes_queue
.
front
());
add_next_node_func
(
force_sequential_nodes_queue
.
front
());
force_sequential_nodes_queue
.
pop_front
();
}
}
else
{
}
else
{
queue
.
push_back
(
std
::
move
(
next_node
));
ready_force_sequential_nodes
.
insert
(
next_node
);
continue
;
}
}
}
else
{
add_next_node_func
(
next_node
);
}
}
}
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录