Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
兽拳
Paddle
提交
ccfec1bc
P
Paddle
项目概览
兽拳
/
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ccfec1bc
编写于
3月 26, 2018
作者:
L
Luo Tao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove vars when remove ops
上级
54a85b7b
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
57 addition
and
4 deletion
+57
-4
paddle/fluid/framework/block_desc.cc
paddle/fluid/framework/block_desc.cc
+30
-4
python/paddle/fluid/tests/unittests/test_protobuf_descs.py
python/paddle/fluid/tests/unittests/test_protobuf_descs.py
+27
-0
未找到文件。
paddle/fluid/framework/block_desc.cc
浏览文件 @
ccfec1bc
...
...
@@ -148,14 +148,40 @@ void BlockDesc::RemoveOp(size_t s, size_t e) {
return
;
}
need_update_
=
true
;
std
::
vector
<
std
::
string
>
vars1
;
// input vars from delete ops
for
(
auto
it
=
ops_
.
begin
()
+
s
;
it
!=
ops_
.
begin
()
+
e
;
it
++
)
{
auto
names
=
(
*
it
)
->
InputArgumentNames
();
for
(
auto
n
:
names
)
{
// TODO(typhoonzero): delete vars if no other op use it.
VLOG
(
3
)
<<
"deleting var "
<<
n
;
// delete all output vars
auto
out_names
=
(
*
it
)
->
OutputArgumentNames
();
for
(
auto
n
:
out_names
)
{
vars_
.
erase
(
vars_
.
find
(
n
))
;
}
// collect all input vars from remove ops
auto
in_names
=
(
*
it
)
->
InputArgumentNames
();
vars1
.
insert
(
vars1
.
end
(),
in_names
.
begin
(),
in_names
.
end
());
}
ops_
.
erase
(
ops_
.
begin
()
+
s
,
ops_
.
begin
()
+
e
);
// collect input and output vars from remain ops
std
::
vector
<
std
::
string
>
vars2
;
for
(
auto
it
=
ops_
.
begin
();
it
!=
ops_
.
end
();
it
++
)
{
auto
in_names
=
(
*
it
)
->
InputArgumentNames
();
auto
out_names
=
(
*
it
)
->
OutputArgumentNames
();
vars2
.
insert
(
vars2
.
end
(),
in_names
.
begin
(),
in_names
.
end
());
vars2
.
insert
(
vars2
.
end
(),
out_names
.
begin
(),
out_names
.
end
());
}
// delete input vars if no other op use it.
std
::
vector
<
std
::
string
>
del_vars
;
std
::
sort
(
vars1
.
begin
(),
vars1
.
end
());
std
::
unique
(
vars1
.
begin
(),
vars1
.
end
());
std
::
sort
(
vars2
.
begin
(),
vars2
.
end
());
std
::
unique
(
vars2
.
begin
(),
vars2
.
end
());
// del_vars = vars1 - vars1 ^ vars2
std
::
set_difference
(
vars1
.
begin
(),
vars1
.
end
(),
vars2
.
begin
(),
vars2
.
end
(),
std
::
inserter
(
del_vars
,
del_vars
.
end
()));
for
(
auto
it
=
del_vars
.
begin
();
it
!=
del_vars
.
end
();
it
++
)
{
vars_
.
erase
(
vars_
.
find
(
*
it
));
}
}
std
::
vector
<
OpDesc
*>
BlockDesc
::
AllOps
()
const
{
...
...
python/paddle/fluid/tests/unittests/test_protobuf_descs.py
浏览文件 @
ccfec1bc
...
...
@@ -186,6 +186,33 @@ class TestBlockDesc(unittest.TestCase):
all_ops
.
append
(
block
.
op
(
idx
))
self
.
assertEqual
(
all_ops
,
[
op0
,
op1
,
op2
])
def
test_remove_op
(
self
):
prog
=
core
.
ProgramDesc
()
self
.
assertIsNotNone
(
prog
)
block
=
prog
.
block
(
0
)
self
.
assertIsNotNone
(
block
)
op1
=
block
.
append_op
()
op2
=
block
.
append_op
()
var1
=
block
.
var
(
"var1"
)
var2
=
block
.
var
(
"var2"
)
var3
=
block
.
var
(
"var3"
)
var4
=
block
.
var
(
"var4"
)
op1
.
set_input
(
"X"
,
[
"var1"
,
"var2"
])
op1
.
set_output
(
"Y"
,
[
"var3"
])
op2
.
set_input
(
"X"
,
[
"var1"
])
op2
.
set_output
(
"Y"
,
[
"var4"
])
# remove op1, its input var2 and output var3 will be removed at the same time,
# but its input var1 will not be removed since var1 is also an input for op2.
block
.
remove_op
(
0
,
1
)
all_ops
=
[]
for
idx
in
xrange
(
0
,
block
.
op_size
()):
all_ops
.
append
(
block
.
op
(
idx
))
self
.
assertEqual
(
all_ops
,
[
op2
])
all_vars
=
block
.
all_vars
()
self
.
assertEqual
(
set
(
all_vars
),
{
var1
,
var4
})
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录