Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
843d740b
X
X2Paddle
项目概览
PaddlePaddle
/
X2Paddle
大约 1 年 前同步成功
通知
327
Star
698
Fork
167
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
26
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
X
X2Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
26
Issue
26
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
843d740b
编写于
9月 19, 2019
作者:
J
jiangjiajun
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
optimize for elementwise op
上级
fa6ada39
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
55 addition
and
0 deletion
+55
-0
x2paddle/convert.py
x2paddle/convert.py
+1
-0
x2paddle/optimizer/tf_optimizer.py
x2paddle/optimizer/tf_optimizer.py
+54
-0
未找到文件。
x2paddle/convert.py
浏览文件 @
843d740b
...
...
@@ -104,6 +104,7 @@ def tf2paddle(model_path,
# neccesary optimization
optimizer
.
delete_redundance_code
()
# optimizer below is experimental
optimizer
.
optimize_elementwise_op
()
optimizer
.
merge_activation
()
optimizer
.
merge_bias
()
optimizer
.
optimize_sub_graph
()
...
...
x2paddle/optimizer/tf_optimizer.py
浏览文件 @
843d740b
...
...
@@ -16,6 +16,7 @@
from
x2paddle.op_mapper.tf_op_mapper
import
TFOpMapper
from
x2paddle.core.fluid_code
import
Layer
from
x2paddle.core.util
import
*
import
six
import
numpy
import
copy
as
cp
...
...
@@ -104,6 +105,59 @@ class TFOptimizer(object):
del
out_node
.
inputs
[
index
]
del
self
.
graph
.
node_map
[
node_name
]
def
optimize_elementwise_op
(
self
):
elementwise_ops
=
[
'Sub'
,
'Add'
,
'RealDiv'
,
'Maximum'
,
'Mul'
,
'FloorDiv'
,
'GreaterEqual'
]
revertable_ops
=
[
'Add'
,
'Mul'
]
for
node_name
in
self
.
graph
.
topo_sort
:
node
=
self
.
graph
.
get_node
(
node_name
)
if
node
is
None
:
continue
if
node
.
layer_type
in
elementwise_ops
:
if
len
(
node
.
fluid_code
.
layers
)
!=
2
:
continue
if
node
.
fluid_code
.
layers
[
0
].
op
!=
"expand"
:
continue
expand_out
=
node
.
fluid_code
.
layers
[
0
].
output
expand_in
=
node
.
fluid_code
.
layers
[
0
].
inputs
expand_times
=
node
.
fluid_code
.
layers
[
0
].
param_attr
[
"expand_times"
]
x
=
node
.
fluid_code
.
layers
[
1
].
inputs
[
"x"
]
y
=
node
.
fluid_code
.
layers
[
1
].
inputs
[
"y"
]
if
isinstance
(
x
,
six
.
string_types
)
and
node
.
layer_type
in
revertable_ops
:
node
.
fluid_code
.
layers
[
1
].
inputs
[
"y"
]
=
x
node
.
fluid_code
.
layers
[
1
].
inputs
[
"x"
]
=
y
x
=
node
.
fluid_code
.
layers
[
1
].
inputs
[
"x"
]
y
=
expand_in
elif
isinstance
(
y
,
six
.
string_types
):
y
=
expand_in
else
:
continue
x_shape
=
x
.
out_shapes
[
0
]
y_shape
=
y
.
out_shapes
[
0
]
if
len
(
x_shape
)
!=
len
(
y_shape
):
continue
if
len
(
x_shape
)
==
4
:
x_shape
=
[
x_shape
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
y_shape
=
[
y_shape
[
i
]
for
i
in
[
0
,
3
,
1
,
2
]]
continue_flag
=
True
for
i
in
range
(
len
(
x_shape
)):
if
y_shape
[
-
1
*
(
i
+
1
)]
==
1
and
continue_flag
:
expand_times
[
-
1
*
(
i
+
1
)]
=
1
else
:
continue_flag
=
False
if
expand_times
.
count
(
1
)
==
len
(
expand_times
):
node
.
fluid_code
.
layers
[
1
].
inputs
[
"y"
]
=
expand_in
del
node
.
fluid_code
.
layers
[
0
]
def
merge_activation
(
self
):
act_nodes
=
list
()
for
node_name
in
self
.
graph
.
topo_sort
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录