Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
e47770bd
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看板
提交
e47770bd
编写于
10月 03, 2017
作者:
F
fengjiayi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Update
上级
9935fdd3
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
57 addition
and
49 deletion
+57
-49
paddle/framework/backward.cc
paddle/framework/backward.cc
+45
-40
paddle/framework/backward.h
paddle/framework/backward.h
+1
-1
paddle/framework/op_desc.cc
paddle/framework/op_desc.cc
+7
-6
paddle/framework/op_registry.cc
paddle/framework/op_registry.cc
+2
-1
paddle/framework/op_registry.h
paddle/framework/op_registry.h
+2
-1
未找到文件。
paddle/framework/backward.cc
浏览文件 @
e47770bd
...
...
@@ -234,18 +234,17 @@ static bool AllGradInSet(const std::vector<std::string>& names,
return
true
;
}
std
::
vector
<
OpDescBind
>
CreatBackwardOps
(
const
std
::
unique_ptr
<
OpDescBind
>&
op_desc_ptr
,
unordered_map
<
std
::
string
>&
no_grad_vars
)
{
const
OpDescBind
&
op_desc
=
*
op_desc_ptr
;
std
::
vector
<
OpDescBind
>
grad_op_descs
;
std
::
vector
<
std
::
unique_ptr
<
OpDescBind
>>
MakeGradOpDescs
(
const
std
::
unique_ptr
<
OpDescBind
>&
op_desc
,
unordered_set
<
std
::
string
>&
no_grad_vars
)
{
std
::
vector
<
std
::
unique_ptr
<
OpDescBind
>>
grad_op_descs
;
// All input gradients of forwarding operator do not need to calculat.
if
(
AllGradInSet
(
op_desc
_
.
InputArgumentNames
(),
kGradVarSuffix
,
if
(
AllGradInSet
(
op_desc
->
InputArgumentNames
(),
kGradVarSuffix
,
no_grad_vars
))
{
return
grad_op_descs
;
// empty vector
}
// All output gradients of forwarding operator do not need to calculate.
const
std
::
vector
<
std
::
string
>&
outputs
=
op_desc
_
.
OutputArugumentNames
();
const
std
::
vector
<
std
::
string
>&
outputs
=
op_desc
->
OutputArugumentNames
();
if
(
AllGradInSet
(
outputs
,
kGradVarSuffix
,
no_grad_vars
))
{
for
(
const
std
::
string
&
name
:
outputs
)
{
no_grad_vars
.
insert
(
GradVarName
(
name
));
...
...
@@ -255,50 +254,54 @@ std::vector<OpDescBind> CreatBackwardOps(
grad_op_descs
=
OpRegistry
::
CreateGradOpDescs
(
op_desc
);
std
::
vector
<
OpDescBind
>
fill_zeros_ops
;
for
(
OpDescBind
&
desc
:
grad_op_descs
)
{
for
(
const
std
::
string
&
in_name
:
desc
.
InputArgumentNames
())
{
std
::
list
<
std
::
unique_ptr
<
OpDescBind
>>
pending_
fill_zeros_ops
;
for
(
auto
&
desc
:
grad_op_descs
)
{
for
(
const
std
::
string
&
in_name
:
desc
->
InputArgumentNames
())
{
if
(
no_grad_vars
.
count
(
in_name
))
{
std
::
string
prefix
=
in_name
.
substr
(
0
,
in_name
.
size
()
-
sizeof
(
kGradVarSuffix
)
/
sizeof
(
char
)
+
1
);
std
::
string
new_name
=
prefix
+
kZeroVarSuffix
;
desc
.
Rename
(
in_name
,
new_name
);
OpDescBind
op_desc_b
ind
(
{
"fill_zeros_like"
,
{{
"X"
,
{
prefix
}}},
{{
"Y"
,
{
new_name
}}},
{}
});
fill_zeros_ops
.
push_back
(
op_desc_bind
);
desc
->
Rename
(
in_name
,
new_name
);
OpDescBind
*
fill_zeros_op
=
new
OpDescB
ind
(
"fill_zeros_like"
,
{{
"X"
,
{
prefix
}}},
{{
"Y"
,
{
new_name
}}},
{
});
pending_fill_zeros_ops
.
push_back
({
fill_zeros_op
}
);
}
}
for
(
const
std
::
string
&
out_name
:
desc
.
Outpu
tName
())
{
for
(
const
std
::
string
&
out_name
:
desc
->
OutputArgumen
tName
())
{
if
(
no_grad_vars
.
count
(
out_name
))
{
desc
.
Rename
(
out_name
,
kEmptyVarName
);
desc
->
Rename
(
out_name
,
kEmptyVarName
);
}
}
}
grad_op_descs
.
insert
(
grad_op_descs
.
begin
(),
fill_zeros_ops
.
begin
(),
fill_zeros_ops
.
end
());
grad_op_descs
.
insert
(
std
::
begin
(
grad_op_descs
),
std
::
begin
(
pending_fill_zeros_ops
),
std
::
end
(
pending_fill_zeros_ops
));
// TODO (fengjiayi): RNN op
return
grad_op_descs
;
}
void
AppendBackwardOps
(
BlockDescBind
&
block_desc
,
void
AppendBackwardOpDescs
(
BlockDescBind
&
block_desc
,
const
std
::
unordered_set
<
std
::
string
>&
no_grad_vars
)
{
std
::
unordered_map
<
std
::
string
,
std
::
vector
<
size_t
>>
dup_out_ops
;
size_t
grad_desc_idx
=
0
;
std
::
deque
<
std
::
unique_ptr
<
OpDescBind
>>
op_descs
=
block_desc
.
ops_
;
std
::
vector
<
std
::
unique_ptr
<
OpDescBind
>>
grad_op_descs
;
for
(
auto
it
=
op_descs
.
rbegin
();
it
!=
op_descs
.
rend
();
++
it
)
{
std
::
vector
<
OpDescBind
>
op_grads
=
CreatBackwardOps
(
*
it
,
no_grad_vars
);
for
(
const
OpDescBind
&
desc
:
op_grads
)
{
for
(
const
std
::
string
&
out_name
:
desc
.
OutputArugumentNames
())
{
std
::
deque
<
std
::
unique_ptr
<
OpDescBind
>>
block_op_descs
=
block_desc
.
ops_
;
std
::
vector
<
std
::
unique_ptr
<
OpDescBind
>>
backward_descs
;
for
(
auto
it
=
block_op_descs
.
rbegin
();
it
!=
block_op_descs
.
rend
();
++
it
)
{
std
::
vector
<
std
::
unique_ptr
<
OpDescBind
>>
op_grads
=
MakeGradOpDescs
(
*
it
,
no_grad_vars
);
for
(
const
auto
&
desc
:
op_grads
)
{
for
(
const
std
::
string
&
out_name
:
desc
->
OutputArugumentNames
())
{
dup_out_ops
[
out_name
].
emplace_back
(
grad_desc_idx
);
}
++
grad_desc_idx
;
}
grad_op_descs
.
insert
(
grad_op_descs
.
end
(),
op_grads
.
begin
(),
op_grads
.
end
());
backward_descs
.
insert
(
backward_descs
.
end
(),
op_grads
.
begin
(),
op_grads
.
end
());
}
// Check whether some variables are written more than once
std
::
list
<
std
::
pair
<
size_t
,
OpDescBind
>>
pending_sum_ops
;
std
::
list
<
std
::
pair
<
size_t
,
std
::
unique_ptr
<
OpDescBind
>
>>
pending_sum_ops
;
for
(
const
auto
&
dup
:
dup_out_ops
)
{
const
std
::
string
&
out_name
=
dup
.
first
;
const
std
::
vector
<
size_t
>
dup_op
=
dup
.
second
;
...
...
@@ -306,25 +309,27 @@ void AppendBackwardOps(BlockDescBind& block_desc,
std
::
vector
<
std
::
string
>
sum_op_inputs
;
for
(
size_t
i
=
0
;
i
<
dup_op
.
size
();
++
i
)
{
std
::
string
new_name
=
out_name
+
"@RENAME@"
+
std
::
to_string
(
i
);
grad_op_descs
[
dup_op
[
i
]].
Rename
(
out_name
,
new_name
);
backward_descs
[
dup_op
[
i
]]
->
Rename
(
out_name
,
new_name
);
sum_op_inputs
.
emplace_back
(
new_name
);
}
pending_sum_ops
.
push_back
(
{
dup_op
.
back
(),
OpDescBind
(
{
"sum"
,
{{
"X"
,
{
sum_op_inputs
}}},
{{
"Out"
,
{
out_name
}}},
{}})});
OpDescBind
*
sum_op
=
new
OpDescBind
(
"sum"
,
{{
"X"
,
sum_op_inputs
}},
{{
"Out"
,
{
out_name
}}},
{});
pending_sum_ops
.
push_back
({
dup_op
.
back
(),
{
sum_op
}});
}
}
pending_sum_ops
.
sort
(
[](
const
std
::
pair
<
size_t
,
OpDescBind
>&
a
,
const
std
::
pair
<
size_t
,
OpDescBind
>&
b
)
{
return
a
.
first
>
b
.
first
;
});
[](
const
std
::
pair
<
size_t
,
std
::
unique_ptr
<
OpDescBind
>>&
a
,
const
std
::
pair
<
size_t
,
std
::
unique_ptr
<
OpDescBind
>>&
b
)
{
return
a
.
first
>
b
.
first
;
});
for
(
auto
&
p
:
pending_sum_ops
)
{
grad_op_descs
.
insert
(
grad_op
_descs
.
begin
()
+
p
.
first
+
1
,
backward_descs
.
insert
(
backward
_descs
.
begin
()
+
p
.
first
+
1
,
std
::
move
(
p
.
second
));
}
// Append grad_op_descs to BlockDescBind::ops_
for
()
{
}
// Append backward_descs to BlockDescBind::ops_
block_op_descs
.
insert
(
std
::
end
(
block_op_descs
),
std
::
begin
(
backward_descs
),
std
::
end
(
backward_descs
));
return
;
}
}
// namespace framework
...
...
paddle/framework/backward.h
浏览文件 @
e47770bd
...
...
@@ -24,7 +24,7 @@ extern std::unique_ptr<OperatorBase> Backward(
const
OperatorBase
&
forwardOp
,
const
std
::
unordered_set
<
std
::
string
>&
no_grad_vars
);
extern
void
AppendBackwardOps
(
extern
void
AppendBackwardOp
Desc
s
(
BlockDescBind
&
block_desc
,
const
std
::
unordered_set
<
std
::
string
>&
no_grad_vars
);
...
...
paddle/framework/op_desc.cc
浏览文件 @
e47770bd
...
...
@@ -49,7 +49,7 @@ std::vector<std::string> OpDescBind::InputNames() const {
return
retv
;
}
std
::
vector
<
std
::
string
>
InputArgumentNames
()
const
{
std
::
vector
<
std
::
string
>
OpDescBind
::
InputArgumentNames
()
const
{
std
::
vector
<
std
::
string
>
retv
;
for
(
auto
&
ipt
:
this
->
inputs_
)
{
retv
.
insert
(
retv
.
end
(),
ipt
.
second
.
begin
(),
ipt
.
second
.
end
());
...
...
@@ -80,7 +80,7 @@ std::vector<std::string> OpDescBind::OutputNames() const {
return
retv
;
}
std
::
vector
<
std
::
string
>
OutputArgumentNames
()
const
{
std
::
vector
<
std
::
string
>
O
pDescBind
::
O
utputArgumentNames
()
const
{
std
::
vector
<
std
::
string
>
retv
;
for
(
auto
&
ipt
:
this
->
outputs_
)
{
retv
.
insert
(
retv
.
end
(),
ipt
.
second
.
begin
(),
ipt
.
second
.
end
());
...
...
@@ -137,12 +137,13 @@ const std::unordered_map<std::string, Attribute> &OpDescBind::GetAttrMap()
return
attrs_
;
}
void
Rename
(
const
std
::
string
&
old_name
,
const
std
::
string
&
new_name
)
{
for
(
std
:
string
&
input
:
inputs_
)
{
void
OpDescBind
::
Rename
(
const
std
::
string
&
old_name
,
const
std
::
string
&
new_name
)
{
for
(
auto
&
input
:
inputs_
)
{
std
::
replace
(
input
.
second
.
begin
(),
input
.
second
.
end
(),
old_name
,
new_name
);
}
for
(
std
::
string
&
output
:
outputs_
)
{
std
::
rep
al
ce
(
output
.
second
.
begin
(),
output
.
second
.
end
(),
old_name
,
for
(
auto
&
output
:
outputs_
)
{
std
::
rep
la
ce
(
output
.
second
.
begin
(),
output
.
second
.
end
(),
old_name
,
new_name
);
}
need_update_
=
true
;
...
...
paddle/framework/op_registry.cc
浏览文件 @
e47770bd
...
...
@@ -57,7 +57,8 @@ std::unique_ptr<OperatorBase> OpRegistry::CreateGradOp(const OperatorBase& op) {
return
std
::
unique_ptr
<
OperatorBase
>
(
BuildGradOp
(
&
op
));
}
static
std
::
vector
<
OpDescBind
>
CreateGradOpDescs
(
const
OpDescBind
&
op_desc
)
{
static
std
::
vector
<
std
::
unique_ptr
<
OpDescBind
>>
OpRegistry
::
CreateGradOpDescs
(
const
OpDescBind
&
op_desc
)
{
auto
&
info
=
OpInfoMap
::
Instance
().
Get
(
op_desc
.
Type
());
return
info
.
grad_op_maker_
(
op_desc
);
}
...
...
paddle/framework/op_registry.h
浏览文件 @
e47770bd
...
...
@@ -69,7 +69,8 @@ class OpRegistry {
static
std
::
unique_ptr
<
OperatorBase
>
CreateGradOp
(
const
OperatorBase
&
op
);
static
std
::
vector
<
OpDescBind
>
CreateGradOpDescs
(
const
OpDescBind
&
op_desc
);
static
std
::
vector
<
std
::
unique_ptr
<
OpDescBind
>>
CreateGradOpDescs
(
const
OpDescBind
&
op_desc
);
};
class
Registrar
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录