Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
4b07686a
P
Paddle
项目概览
Crayon鑫
/
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看板
提交
4b07686a
编写于
10月 05, 2017
作者:
F
fengjiayi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add unit tests
上级
a598ef53
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
209 addition
and
2 deletion
+209
-2
paddle/framework/backward.cc
paddle/framework/backward.cc
+3
-2
paddle/framework/backward_test.cc
paddle/framework/backward_test.cc
+206
-0
未找到文件。
paddle/framework/backward.cc
浏览文件 @
4b07686a
...
...
@@ -289,7 +289,7 @@ std::vector<std::unique_ptr<OpDescBind>> MakeOpGrad(
std
::
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.
const
std
::
vector
<
std
::
string
>&
inputs
=
op_desc
->
InArgumentNames
();
const
std
::
vector
<
std
::
string
>&
inputs
=
op_desc
->
In
put
ArgumentNames
();
if
(
AllGradInSet
(
inputs
,
no_grad_vars
))
{
return
grad_op_descs
;
// empty vector
}
...
...
@@ -323,8 +323,9 @@ std::vector<std::unique_ptr<OpDescBind>> MakeOpGrad(
}
}
}
for
(
auto
&
p
:
pending_fill_zeros_ops
)
{
grad_op_descs
.
push_back
(
std
::
move
(
p
));
grad_op_descs
.
insert
(
grad_op_descs
.
begin
(),
std
::
move
(
p
));
}
return
grad_op_descs
;
}
...
...
paddle/framework/backward_test.cc
浏览文件 @
4b07686a
...
...
@@ -155,6 +155,18 @@ class SumOpMaker : public framework::OpProtoAndCheckerMaker {
}
};
class
MultInOutOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
MultInOutOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"x"
);
AddInput
(
"H"
,
"h"
);
AddOutput
(
"Y"
,
"y"
);
AddOutput
(
"Z"
,
"z"
);
AddComment
(
""
);
}
};
}
// namespace framework
}
// namespace paddle
...
...
@@ -172,6 +184,7 @@ REGISTER_OP(sum, f::NOP, f::SumOpMaker, sum_grad, f::NOP);
REGISTER_OP_WITHOUT_GRADIENT
(
fc
,
f
::
FcOp
,
f
::
FcOpMaker
);
REGISTER_OP
(
many_output_op
,
f
::
NOP
,
f
::
ManyOutputOpMaker
,
many_output_op_grad
,
f
::
NOP
);
REGISTER_OP
(
mult_in_out
,
f
::
NOP
,
f
::
MultInOutOpMaker
,
mult_in_out_grad
,
f
::
NOP
);
TEST
(
Backward
,
simple_op_not_need_grad
)
{
auto
fwd
=
f
::
OpRegistry
::
CreateOp
(
...
...
@@ -488,3 +501,196 @@ TEST(Backward, simple_mult_op) {
EXPECT_EQ
(
grad_op3
->
Output
(
f
::
GradVarName
(
"b"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"b3"
)}));
}
TEST
(
Backward
,
intermedia_var_no_grad
)
{
f
::
ProgramDesc
*
program_desc
=
GetNewProgramDesc
();
f
::
ProgramDescBind
&
program
=
f
::
ProgramDescBind
::
Instance
(
program_desc
);
f
::
BlockDescBind
*
block
=
program
.
Block
(
0
);
f
::
OpDescBind
*
op1
=
block
->
AppendOp
();
op1
->
SetType
(
"rowwise_add"
);
op1
->
SetInput
(
"X"
,
{
"x1"
});
op1
->
SetInput
(
"b"
,
{
"b1"
});
op1
->
SetOutput
(
"Out"
,
{
"out1"
});
f
::
OpDescBind
*
op2
=
block
->
AppendOp
();
op2
->
SetType
(
"mul"
);
op2
->
SetInput
(
"X"
,
{
"x2"
});
op2
->
SetInput
(
"Y"
,
{
"y2"
});
op2
->
SetOutput
(
"Out"
,
{
"out2"
});
f
::
OpDescBind
*
op3
=
block
->
AppendOp
();
op3
->
SetType
(
"rowwise_add"
);
op3
->
SetInput
(
"X"
,
{
"out2"
});
op3
->
SetInput
(
"b"
,
{
"b3"
});
op3
->
SetOutput
(
"Out"
,
{
"out3"
});
f
::
OpDescBind
*
op4
=
block
->
AppendOp
();
op4
->
SetType
(
"mul"
);
op4
->
SetInput
(
"X"
,
{
"out1"
});
op4
->
SetInput
(
"Y"
,
{
"out3"
});
op4
->
SetOutput
(
"Out"
,
{
"out4"
});
AppendBackward
(
program
,
{
"out3"
});
ASSERT_EQ
(
block
->
AllOps
().
size
(),
6UL
);
f
::
OpDescBind
*
grad_op1
=
block
->
AllOps
()[
5
];
EXPECT_EQ
(
grad_op1
->
Type
(),
"rowwise_add_grad"
);
ASSERT_EQ
(
grad_op1
->
InputNames
().
size
(),
1UL
);
ASSERT_EQ
(
grad_op1
->
OutputNames
().
size
(),
2UL
);
EXPECT_EQ
(
grad_op1
->
Input
(
f
::
GradVarName
(
"Out"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"out1"
)}));
EXPECT_EQ
(
grad_op1
->
Output
(
f
::
GradVarName
(
"X"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"x1"
)}));
EXPECT_EQ
(
grad_op1
->
Output
(
f
::
GradVarName
(
"b"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"b1"
)}));
f
::
OpDescBind
*
grad_op4
=
block
->
AllOps
()[
4
];
EXPECT_EQ
(
grad_op4
->
Type
(),
"mul_grad"
);
ASSERT_EQ
(
grad_op4
->
InputNames
().
size
(),
4UL
);
ASSERT_EQ
(
grad_op4
->
OutputNames
().
size
(),
2UL
);
EXPECT_EQ
(
grad_op4
->
Input
(
"X"
),
std
::
vector
<
std
::
string
>
({
"out1"
}));
EXPECT_EQ
(
grad_op4
->
Input
(
"Y"
),
std
::
vector
<
std
::
string
>
({
"out3"
}));
EXPECT_EQ
(
grad_op4
->
Input
(
"Out"
),
std
::
vector
<
std
::
string
>
({
"out4"
}));
EXPECT_EQ
(
grad_op4
->
Input
(
f
::
GradVarName
(
"Out"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"out4"
)}));
EXPECT_EQ
(
grad_op4
->
Output
(
f
::
GradVarName
(
"X"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"out1"
)}));
EXPECT_EQ
(
grad_op4
->
Output
(
f
::
GradVarName
(
"Y"
)),
std
::
vector
<
std
::
string
>
({
f
::
kEmptyVarName
}));
}
TEST
(
Backward
,
var_no_grad
)
{
f
::
ProgramDesc
*
program_desc
=
GetNewProgramDesc
();
f
::
ProgramDescBind
&
program
=
f
::
ProgramDescBind
::
Instance
(
program_desc
);
f
::
BlockDescBind
*
block
=
program
.
Block
(
0
);
f
::
OpDescBind
*
op1
=
block
->
AppendOp
();
op1
->
SetType
(
"mult_in_out"
);
op1
->
SetInput
(
"X"
,
{
"x1"
});
op1
->
SetInput
(
"H"
,
{
"h1"
});
op1
->
SetOutput
(
"Y"
,
{
"y1"
});
op1
->
SetOutput
(
"Z"
,
{
"z1"
});
f
::
OpDescBind
*
op2
=
block
->
AppendOp
();
op2
->
SetType
(
"mult_in_out"
);
op2
->
SetInput
(
"X"
,
{
"y1"
});
op2
->
SetInput
(
"H"
,
{
"z1"
});
op2
->
SetOutput
(
"Y"
,
{
"y2"
});
op2
->
SetOutput
(
"Z"
,
{
"z2"
});
AppendBackward
(
program
,
{
"z1"
});
ASSERT_EQ
(
block
->
AllOps
().
size
(),
5UL
);
f
::
OpDescBind
*
grad_op2
=
block
->
AllOps
()[
2
];
ASSERT_EQ
(
grad_op2
->
Type
(),
"mult_in_out_grad"
);
ASSERT_EQ
(
grad_op2
->
InputNames
().
size
(),
6UL
);
ASSERT_EQ
(
grad_op2
->
OutputNames
().
size
(),
2UL
);
EXPECT_EQ
(
grad_op2
->
Input
(
"X"
),
std
::
vector
<
std
::
string
>
({
"y1"
}));
EXPECT_EQ
(
grad_op2
->
Input
(
"H"
),
std
::
vector
<
std
::
string
>
({
"z1"
}));
EXPECT_EQ
(
grad_op2
->
Input
(
"Y"
),
std
::
vector
<
std
::
string
>
({
"y2"
}));
EXPECT_EQ
(
grad_op2
->
Input
(
"Z"
),
std
::
vector
<
std
::
string
>
({
"z2"
}));
EXPECT_EQ
(
grad_op2
->
Input
(
f
::
GradVarName
(
"Y"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"y2"
)}));
EXPECT_EQ
(
grad_op2
->
Input
(
f
::
GradVarName
(
"Z"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"z2"
)}));
EXPECT_EQ
(
grad_op2
->
Output
(
f
::
GradVarName
(
"X"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"y1"
)}));
EXPECT_EQ
(
grad_op2
->
Output
(
f
::
GradVarName
(
"H"
)),
std
::
vector
<
std
::
string
>
({
f
::
kEmptyVarName
}));
f
::
OpDescBind
*
fill_zero_op
=
block
->
AllOps
()[
3
];
ASSERT_EQ
(
fill_zero_op
->
Type
(),
"fill_zeros_like"
);
ASSERT_EQ
(
fill_zero_op
->
InputNames
().
size
(),
1UL
);
ASSERT_EQ
(
fill_zero_op
->
OutputNames
().
size
(),
1UL
);
EXPECT_EQ
(
fill_zero_op
->
Input
(
"X"
),
std
::
vector
<
std
::
string
>
({
"z1"
}));
EXPECT_EQ
(
fill_zero_op
->
Output
(
"Y"
),
std
::
vector
<
std
::
string
>
({
std
::
string
(
"z1"
)
+
f
::
kZeroVarSuffix
}));
f
::
OpDescBind
*
grad_op1
=
block
->
AllOps
()[
4
];
ASSERT_EQ
(
grad_op1
->
Type
(),
"mult_in_out_grad"
);
ASSERT_EQ
(
grad_op1
->
InputNames
().
size
(),
6UL
);
ASSERT_EQ
(
grad_op1
->
OutputNames
().
size
(),
2UL
);
EXPECT_EQ
(
grad_op1
->
Input
(
"X"
),
std
::
vector
<
std
::
string
>
({
"x1"
}));
EXPECT_EQ
(
grad_op1
->
Input
(
"H"
),
std
::
vector
<
std
::
string
>
({
"h1"
}));
EXPECT_EQ
(
grad_op1
->
Input
(
"Y"
),
std
::
vector
<
std
::
string
>
({
"y1"
}));
EXPECT_EQ
(
grad_op1
->
Input
(
"Z"
),
std
::
vector
<
std
::
string
>
({
"z1"
}));
EXPECT_EQ
(
grad_op1
->
Input
(
f
::
GradVarName
(
"Y"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"y1"
)}));
EXPECT_EQ
(
grad_op1
->
Input
(
f
::
GradVarName
(
"Z"
)),
std
::
vector
<
std
::
string
>
({
std
::
string
(
"z1"
)
+
f
::
kZeroVarSuffix
}));
EXPECT_EQ
(
grad_op1
->
Output
(
f
::
GradVarName
(
"X"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"x1"
)}));
EXPECT_EQ
(
grad_op1
->
Output
(
f
::
GradVarName
(
"H"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"h1"
)}));
}
TEST
(
Backward
,
shared_var
)
{
f
::
ProgramDesc
*
program_desc
=
GetNewProgramDesc
();
f
::
ProgramDescBind
&
program
=
f
::
ProgramDescBind
::
Instance
(
program_desc
);
f
::
BlockDescBind
*
block
=
program
.
Block
(
0
);
f
::
OpDescBind
*
op1
=
block
->
AppendOp
();
op1
->
SetType
(
"rowwise_add"
);
op1
->
SetInput
(
"X"
,
{
"x1"
});
op1
->
SetInput
(
"b"
,
{
"b1"
});
op1
->
SetOutput
(
"Out"
,
{
"out1"
});
f
::
OpDescBind
*
op2
=
block
->
AppendOp
();
op2
->
SetType
(
"mul"
);
op2
->
SetInput
(
"X"
,
{
"out1"
});
op2
->
SetInput
(
"Y"
,
{
"y2"
});
op2
->
SetOutput
(
"Out"
,
{
"out2"
});
f
::
OpDescBind
*
op3
=
block
->
AppendOp
();
op3
->
SetType
(
"rowwise_add"
);
op3
->
SetInput
(
"X"
,
{
"out1"
});
op3
->
SetInput
(
"b"
,
{
"b3"
});
op3
->
SetOutput
(
"Out"
,
{
"out3"
});
AppendBackward
(
program
,
{});
ASSERT_EQ
(
block
->
AllOps
().
size
(),
7UL
);
f
::
OpDescBind
*
grad_op3
=
block
->
AllOps
()[
3
];
ASSERT_EQ
(
grad_op3
->
Type
(),
"rowwise_add_grad"
);
ASSERT_EQ
(
grad_op3
->
InputNames
().
size
(),
1UL
);
ASSERT_EQ
(
grad_op3
->
OutputNames
().
size
(),
2UL
);
EXPECT_EQ
(
grad_op3
->
Input
(
f
::
GradVarName
(
"Out"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"out3"
)}));
EXPECT_EQ
(
grad_op3
->
Output
(
f
::
GradVarName
(
"X"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"out1"
)
+
"@RENAME@0"
}));
EXPECT_EQ
(
grad_op3
->
Output
(
f
::
GradVarName
(
"b"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"b3"
)}));
f
::
OpDescBind
*
grad_op4
=
block
->
AllOps
()[
4
];
ASSERT_EQ
(
grad_op4
->
Type
(),
"mul_grad"
);
ASSERT_EQ
(
grad_op4
->
InputNames
().
size
(),
4UL
);
ASSERT_EQ
(
grad_op4
->
OutputNames
().
size
(),
2UL
);
EXPECT_EQ
(
grad_op4
->
Input
(
"X"
),
std
::
vector
<
std
::
string
>
({
"out1"
}));
EXPECT_EQ
(
grad_op4
->
Input
(
"Y"
),
std
::
vector
<
std
::
string
>
({
"y2"
}));
EXPECT_EQ
(
grad_op4
->
Input
(
"Out"
),
std
::
vector
<
std
::
string
>
({
"out2"
}));
EXPECT_EQ
(
grad_op4
->
Input
(
f
::
GradVarName
(
"Out"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"out2"
)}));
EXPECT_EQ
(
grad_op4
->
Output
(
f
::
GradVarName
(
"X"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"out1"
)
+
"@RENAME@1"
}));
EXPECT_EQ
(
grad_op4
->
Output
(
f
::
GradVarName
(
"Y"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"y2"
)}));
f
::
OpDescBind
*
sum_op
=
block
->
AllOps
()[
5
];
ASSERT_EQ
(
sum_op
->
Type
(),
"sum"
);
ASSERT_EQ
(
sum_op
->
InputNames
().
size
(),
1UL
);
ASSERT_EQ
(
sum_op
->
OutputNames
().
size
(),
1UL
);
EXPECT_EQ
(
sum_op
->
Input
(
"X"
),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"out1"
)
+
"@RENAME@0"
,
f
::
GradVarName
(
"out1"
)
+
"@RENAME@1"
}));
EXPECT_EQ
(
sum_op
->
Output
(
"Out"
),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"out1"
)}));
f
::
OpDescBind
*
grad_op1
=
block
->
AllOps
()[
6
];
ASSERT_EQ
(
grad_op1
->
Type
(),
"rowwise_add_grad"
);
ASSERT_EQ
(
grad_op1
->
InputNames
().
size
(),
1UL
);
ASSERT_EQ
(
grad_op1
->
OutputNames
().
size
(),
2UL
);
EXPECT_EQ
(
grad_op1
->
Input
(
f
::
GradVarName
(
"Out"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"out1"
)}));
EXPECT_EQ
(
grad_op1
->
Output
(
f
::
GradVarName
(
"X"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"x1"
)}));
EXPECT_EQ
(
grad_op1
->
Output
(
f
::
GradVarName
(
"b"
)),
std
::
vector
<
std
::
string
>
({
f
::
GradVarName
(
"b1"
)}));
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录