Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
fd72e9c7
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看板
提交
fd72e9c7
编写于
10月 12, 2017
作者:
Y
Yang Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pass multiple unit test
上级
a31ff363
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
70 addition
and
114 deletion
+70
-114
paddle/framework/prune.cc
paddle/framework/prune.cc
+1
-8
paddle/framework/prune_test.cc
paddle/framework/prune_test.cc
+69
-106
未找到文件。
paddle/framework/prune.cc
浏览文件 @
fd72e9c7
...
...
@@ -43,7 +43,7 @@ void Prune(const ProgramDesc& input, ProgramDesc& output, int id) {
// TODO(tonyyang-svail):
// - will change to use multiple blocks for RNN op and Cond Op
auto
&
block
=
input
.
blocks
(
0
);
auto
&
block
=
input
.
blocks
(
id
);
auto
&
ops
=
block
.
ops
();
bool
expect_feed
=
true
;
...
...
@@ -67,13 +67,6 @@ void Prune(const ProgramDesc& input, ProgramDesc& output, int id) {
auto
&
op_desc
=
*
op_iter
;
if
(
op_desc
.
is_target
()
||
HasDependentVar
(
op_desc
,
dependent_vars
))
{
// erase its output to the dependency graph
for
(
auto
&
var
:
op_desc
.
outputs
())
{
for
(
auto
&
argu
:
var
.
arguments
())
{
dependent_vars
.
erase
(
argu
);
}
}
// insert its input to the dependency graph
for
(
auto
&
var
:
op_desc
.
inputs
())
{
for
(
auto
&
argu
:
var
.
arguments
())
{
...
...
paddle/framework/prune_test.cc
浏览文件 @
fd72e9c7
...
...
@@ -28,105 +28,24 @@ namespace framework {
using
DeviceContext
=
platform
::
DeviceContext
;
class
RowWiseAdd
OpMaker
:
public
OpProtoAndCheckerMaker
{
class
OneOne
OpMaker
:
public
OpProtoAndCheckerMaker
{
public:
RowWiseAdd
OpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
OneOne
OpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"Input X of Add"
);
AddInput
(
"b"
,
"Bias of Add"
);
AddOutput
(
"Out"
,
"Out of Add"
);
AddComment
(
"Add Op"
);
AddInput
(
"input"
,
"input"
);
AddOutput
(
"output"
,
"output"
);
AddComment
(
"Op has one input and one output"
);
}
};
class
RowWiseAddGradMaker
:
public
SingleGradOpDesc
Maker
{
class
TwoOneOpMaker
:
public
OpProtoAndChecker
Maker
{
public:
using
SingleGradOpDescMaker
::
SingleGradOpDescMaker
;
protected:
std
::
unique_ptr
<
OpDescBind
>
Apply
()
const
override
{
auto
grad_op
=
new
OpDescBind
();
grad_op
->
SetInput
(
GradVarName
(
"Out"
),
OutputGrad
(
"Out"
));
grad_op
->
SetOutput
(
GradVarName
(
"X"
),
InputGrad
(
"X"
));
grad_op
->
SetOutput
(
GradVarName
(
"b"
),
InputGrad
(
"b"
));
grad_op
->
SetType
(
"rowwise_add_grad"
);
return
std
::
unique_ptr
<
OpDescBind
>
(
grad_op
);
}
};
class
MulOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
MulOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"A"
);
AddInput
(
"Y"
,
"B"
);
AddOutput
(
"Out"
,
"Out"
);
AddAttr
<
int
>
(
"x_num_col_dims"
,
""
).
SetDefault
(
1
).
EqualGreaterThan
(
1
);
AddAttr
<
int
>
(
"y_num_col_dims"
,
""
).
SetDefault
(
1
).
EqualGreaterThan
(
1
);
AddComment
(
"Mul"
);
}
};
class
SigmoidOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
SigmoidOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
TwoOneOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"X"
);
AddOutput
(
"Out"
,
"Y"
);
AddComment
(
"Sigmoid"
);
}
};
class
NoGradOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
NoGradOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"X input"
);
AddOutput
(
"Out"
,
"Y output"
);
AddComment
(
"NoGradOp, same input output. no Grad"
);
}
};
class
ManyOutputOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
ManyOutputOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"x"
,
"x"
);
AddOutput
(
"y"
,
"y"
);
AddOutput
(
"z"
,
"z"
);
AddComment
(
""
);
}
};
class
FillZeroOpMaker
:
public
OpProtoAndCheckerMaker
{
public:
FillZeroOpMaker
(
OpProto
*
proto
,
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"x"
);
AddOutput
(
"Y"
,
"out"
);
AddComment
(
""
);
}
};
class
SumOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
SumOpMaker
(
framework
::
OpProto
*
proto
,
framework
::
OpAttrChecker
*
op_checker
)
:
OpProtoAndCheckerMaker
(
proto
,
op_checker
)
{
AddInput
(
"X"
,
"the input tensors of sum operator."
).
AsDuplicable
();
AddOutput
(
"Out"
,
"the output tensor of sum operator."
);
AddComment
(
""
);
}
};
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
(
""
);
AddInput
(
"input_1"
,
"input_1"
);
AddInput
(
"input_2"
,
"input_2"
);
AddOutput
(
"output"
,
"output"
);
AddComment
(
"Op has two inputs and one output"
);
}
};
...
...
@@ -135,18 +54,8 @@ class MultInOutOpMaker : public OpProtoAndCheckerMaker {
namespace
f
=
paddle
::
framework
;
namespace
ops
=
paddle
::
operators
;
using
EnforceNotMet
=
paddle
::
platform
::
EnforceNotMet
;
REGISTER_OPERATOR
(
rowwise_add
,
f
::
NOP
,
f
::
RowWiseAddOpMaker
,
f
::
RowWiseAddGradMaker
);
REGISTER_OPERATOR
(
rowwise_add_grad
,
f
::
NOP
);
REGISTER_OP
(
mul
,
f
::
NOP
,
f
::
MulOpMaker
,
mul_grad
,
f
::
NOP
);
REGISTER_OP
(
sigmoid
,
f
::
NOP
,
f
::
SigmoidOpMaker
,
sigmoid_grad
,
f
::
NOP
);
REGISTER_OP_WITHOUT_GRADIENT
(
nograd
,
f
::
NOP
,
f
::
NoGradOpMaker
);
REGISTER_OP_WITHOUT_GRADIENT
(
fill_zeros_like
,
f
::
NOP
,
f
::
FillZeroOpMaker
);
REGISTER_OP
(
sum
,
f
::
NOP
,
f
::
SumOpMaker
,
sum_grad
,
f
::
NOP
);
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
);
REGISTER_OP_WITHOUT_GRADIENT
(
one_one
,
f
::
NOP
,
f
::
OneOneOpMaker
);
REGISTER_OP_WITHOUT_GRADIENT
(
two_one
,
f
::
NOP
,
f
::
TwoOneOpMaker
);
void
AddOp
(
const
std
::
string
&
type
,
const
f
::
VariableNameMap
&
inputs
,
const
f
::
VariableNameMap
&
outputs
,
f
::
AttributeMap
attrs
,
...
...
@@ -184,7 +93,7 @@ TEST(Prune, one_operator) {
f
::
ProgramDescBind
&
program
=
f
::
ProgramDescBind
::
Instance
(
program_desc
);
f
::
BlockDescBind
*
block
=
program
.
Block
(
0
);
AddOp
(
"
mul"
,
{{
"X"
,
{
"a"
}},
{
"Y"
,
{
"w1"
}}},
{{
"O
ut"
,
{
"b"
}}},
{},
block
);
AddOp
(
"
one_one"
,
{{
"input"
,
{
"a"
}}},
{{
"outp
ut"
,
{
"b"
}}},
{},
block
);
f
::
ProgramDesc
*
pdesc
=
program
.
Proto
();
f
::
ProgramDesc
pruned
;
...
...
@@ -197,4 +106,58 @@ TEST(Prune, one_operator) {
PADDLE_ENFORCE_EQ
(
pruned
.
blocks
(
0
).
ops_size
(),
1
);
}
TEST
(
Prune
,
simple_optimize
)
{}
TEST
(
Prune
,
forward
)
{
f
::
ProgramDesc
*
program_desc
=
GetNewProgramDesc
();
f
::
ProgramDescBind
&
program
=
f
::
ProgramDescBind
::
Instance
(
program_desc
);
f
::
BlockDescBind
*
block
=
program
.
Block
(
0
);
AddOp
(
"one_one"
,
{{
"input"
,
{
"a"
}}},
{{
"output"
,
{
"b"
}}},
{},
block
);
AddOp
(
"one_one"
,
{{
"input"
,
{
"b"
}}},
{{
"output"
,
{
"c"
}}},
{},
block
);
AddOp
(
"one_one"
,
{{
"input"
,
{
"c"
}}},
{{
"output"
,
{
"d"
}}},
{},
block
);
AddOp
(
"one_one"
,
{{
"input"
,
{
"d"
}}},
{{
"output"
,
{
"e"
}}},
{},
block
);
f
::
ProgramDesc
*
pdesc
=
program
.
Proto
();
for
(
int
i
=
0
;
i
<
pdesc
->
blocks
(
0
).
ops_size
();
++
i
)
{
f
::
ProgramDesc
pruned
;
pdesc
->
mutable_blocks
(
0
)
->
mutable_ops
(
i
)
->
set_is_target
(
true
);
Prune
(
*
pdesc
,
pruned
,
0
);
PADDLE_ENFORCE_EQ
(
pruned
.
blocks
(
0
).
ops_size
(),
i
+
1
);
}
}
TEST
(
Prune
,
multi_input_op
)
{
f
::
ProgramDesc
*
program_desc
=
GetNewProgramDesc
();
f
::
ProgramDescBind
&
program
=
f
::
ProgramDescBind
::
Instance
(
program_desc
);
f
::
BlockDescBind
*
block
=
program
.
Block
(
0
);
AddOp
(
"one_one"
,
{{
"input"
,
{
"a0"
}}},
{{
"output"
,
{
"b0"
}}},
{},
block
);
AddOp
(
"one_one"
,
{{
"input"
,
{
"a1"
}}},
{{
"output"
,
{
"b1"
}}},
{},
block
);
AddOp
(
"one_one"
,
{{
"input"
,
{
"a2"
}}},
{{
"output"
,
{
"b2"
}}},
{},
block
);
AddOp
(
"three_one"
,
{{
"input"
,
{
"b0"
,
"b1"
,
"b2"
}}},
{{
"output"
,
{
"c"
}}},
{},
block
);
f
::
ProgramDesc
*
pdesc
=
program
.
Proto
();
pdesc
->
mutable_blocks
(
0
)
->
mutable_ops
(
3
)
->
set_is_target
(
true
);
f
::
ProgramDesc
pruned
;
Prune
(
*
pdesc
,
pruned
,
0
);
PADDLE_ENFORCE_EQ
(
pruned
.
blocks
(
0
).
ops_size
(),
4
);
}
TEST
(
Prune
,
multi_output_op
)
{
f
::
ProgramDesc
*
program_desc
=
GetNewProgramDesc
();
f
::
ProgramDescBind
&
program
=
f
::
ProgramDescBind
::
Instance
(
program_desc
);
f
::
BlockDescBind
*
block
=
program
.
Block
(
0
);
AddOp
(
"one_two"
,
{{
"input"
,
{
"a"
}}},
{{
"output"
,
{
"b"
,
"c"
}}},
{},
block
);
AddOp
(
"one_one"
,
{{
"input"
,
{
"b"
}}},
{{
"output"
,
{
"b1"
}}},
{},
block
);
AddOp
(
"one_one"
,
{{
"input"
,
{
"c"
}}},
{{
"output"
,
{
"c1"
}}},
{},
block
);
f
::
ProgramDesc
*
pdesc
=
program
.
Proto
();
pdesc
->
mutable_blocks
(
0
)
->
mutable_ops
(
2
)
->
set_is_target
(
true
);
f
::
ProgramDesc
pruned
;
Prune
(
*
pdesc
,
pruned
,
0
);
PADDLE_ENFORCE_EQ
(
pruned
.
blocks
(
0
).
ops_size
(),
2
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录