Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
ea0259a1
X
X2Paddle
项目概览
PaddlePaddle
/
X2Paddle
大约 1 年 前同步成功
通知
328
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看板
提交
ea0259a1
编写于
11月 18, 2020
作者:
S
SunAhong1993
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix the tf bug
上级
f7cc20f9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
77 addition
and
21 deletion
+77
-21
x2paddle/optimizer/elimination/dygraph/transpose_elimination.py
...le/optimizer/elimination/dygraph/transpose_elimination.py
+9
-8
x2paddle/optimizer/fusion/dygraph/tf_batchnorm_fuser.py
x2paddle/optimizer/fusion/dygraph/tf_batchnorm_fuser.py
+68
-13
未找到文件。
x2paddle/optimizer/elimination/dygraph/transpose_elimination.py
浏览文件 @
ea0259a1
...
...
@@ -101,7 +101,7 @@ class DygraphTransposeElimination(FuseBase):
if
_graph
.
layers
[
out
].
outputs
[
ouput_index
]
in
_graph
.
outputs
:
can_be_optimized
=
False
break
if
_graph
.
layers
[
out
].
attrs
.
get
(
'keepdim'
,
False
):
if
not
_graph
.
layers
[
out
].
attrs
.
get
(
'keepdim'
,
False
):
can_be_optimized
=
False
break
propagate_layers
.
append
(
out
)
...
...
@@ -148,7 +148,7 @@ class DygraphTransposeElimination(FuseBase):
if
_graph
.
layers
[
out
].
outputs
[
output_index
]
in
_graph
.
outputs
:
can_be_optimized
=
False
break
if
_graph
.
layers
[
out
].
attrs
.
get
(
'keepdim'
,
if
not
_graph
.
layers
[
out
].
attrs
.
get
(
'keepdim'
,
False
):
can_be_optimized
=
False
break
...
...
@@ -219,7 +219,7 @@ class DygraphTransposeElimination(FuseBase):
if
_graph
.
layers
[
ipt
].
outputs
[
output_index
]
in
_graph
.
outputs
:
can_be_optimized
=
False
break
if
_graph
.
layers
[
ipt
].
attrs
.
get
(
'keepdim'
,
if
not
_graph
.
layers
[
ipt
].
attrs
.
get
(
'keepdim'
,
False
):
can_be_optimized
=
False
break
...
...
@@ -252,7 +252,7 @@ class DygraphTransposeElimination(FuseBase):
continue
for
l
in
transpose_layers
:
_graph
.
del
ete
_layer
(
l
)
_graph
.
del_layer
(
l
)
optimized_transpose_layers
.
extend
(
transpose_layers
)
optimized_reduce_layers
.
extend
(
reduce_layers
)
...
...
@@ -268,21 +268,22 @@ class DygraphTransposeElimination(FuseBase):
while
strip_transpose
(
opt_graph
):
pass
for
layer_id
in
list
(
set
(
optimized_transpose_layers
)):
self
.
delete_layer_with_associated
(
graph
,
layer_id
)
graph
.
del_layer
(
layer_id
)
for
layer_id
in
list
(
set
(
optimized_reduce_layers
)):
dim
=
graph
.
layers
[
layer_id
].
attrs
.
get
(
'
dim
'
,
None
)
dim
=
graph
.
layers
[
layer_id
].
attrs
.
get
(
'
axis
'
,
None
)
if
dim
is
not
None
:
for
i
in
range
(
len
(
dim
)):
dim
[
i
]
=
[
0
,
2
,
3
,
1
][
dim
[
i
]]
graph
.
layers
[
layer_id
].
attrs
[
'
dim
'
]
=
dim
graph
.
layers
[
layer_id
].
attrs
[
'
axis
'
]
=
dim
for
layer_id
in
list
(
set
(
optimized_concat_layers
)):
axis
=
graph
.
layers
[
layer_id
].
attrs
.
get
(
'axis'
,
0
)
graph
.
layers
[
layer_id
].
attrs
[
'axis'
]
=
[
0
,
2
,
3
,
1
][
axis
]
for
layer_id
in
list
(
set
(
optimized_elementwise_layers
)):
axis
=
graph
.
layers
[
layer_id
].
attrs
.
get
(
'axis'
,
-
1
)
graph
.
layers
[
layer_id
].
attrs
[
'axis'
]
=
[
0
,
2
,
3
,
1
][
axis
]
if
graph
.
layers
[
layer_id
].
kernel
==
"paddle.add"
:
graph
.
layers
[
layer_id
].
kernel
=
"fluid.layers.elementwise_add"
current_transpose_num
=
self
.
get_transpose_num
(
graph
)
print
(
...
...
x2paddle/optimizer/fusion/dygraph/tf_batchnorm_fuser.py
浏览文件 @
ea0259a1
...
...
@@ -24,6 +24,7 @@ class DygraphTFBatchNormFuser(FuseBase):
def
__init__
(
self
):
self
.
bn_index
=
0
super
(
DygraphTFBatchNormFuser
,
self
).
__init__
(
graph_type
=
"dygraph"
)
self
.
patterns
=
list
()
def
build_pattern
(
self
):
""" 描述需要替换的batchnorm图结构。
...
...
@@ -34,57 +35,111 @@ class DygraphTFBatchNormFuser(FuseBase):
def
gen_name
(
id
):
return
"x"
+
str
(
id
)
self
.
pattern
.
add_layer
(
pattern
=
PaddleGraph
(
graph_type
=
"dygraph"
)
pattern
.
add_layer
(
"self.create_parameter"
,
inputs
=
{},
outputs
=
[
gen_name
(
0
)])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"paddle.full"
,
inputs
=
{},
outputs
=
[
gen_name
(
1
)],
shape
=
[
1
])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"paddle.add"
,
inputs
=
{
"x"
:
gen_name
(
0
),
"y"
:
gen_name
(
1
)},
outputs
=
[
gen_name
(
2
)])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"paddle.rsqrt"
,
inputs
=
{
"x"
:
gen_name
(
2
)},
outputs
=
[
gen_name
(
3
)])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"self.create_parameter"
,
inputs
=
{},
outputs
=
[
gen_name
(
4
)])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"paddle.multiply"
,
inputs
=
{
"x"
:
gen_name
(
3
),
"y"
:
gen_name
(
4
)},
outputs
=
[
gen_name
(
5
)])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"self.create_parameter"
,
inputs
=
{},
outputs
=
[
gen_name
(
6
)])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"paddle.multiply"
,
inputs
=
{
"x"
:
gen_name
(
6
),
"y"
:
gen_name
(
5
)},
outputs
=
[
gen_name
(
7
)])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"self.create_parameter"
,
inputs
=
{},
outputs
=
[
gen_name
(
8
)])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"fluid.layers.elementwise_sub"
,
inputs
=
{
"x"
:
gen_name
(
8
),
"y"
:
gen_name
(
7
)},
outputs
=
[
gen_name
(
9
)])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"paddle.multiply"
,
inputs
=
{
"x"
:
"bn-input-0"
,
"y"
:
gen_name
(
5
)},
outputs
=
[
gen_name
(
10
)])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"paddle.add"
,
inputs
=
{
"x"
:
gen_name
(
10
),
"y"
:
gen_name
(
9
)},
outputs
=
[
gen_name
(
11
)])
self
.
pattern
.
build
(
inputs
=
{
"input-0"
:
"bn-input-0"
,
})
pattern
.
build
(
inputs
=
{
"input-0"
:
"bn-input-0"
,
})
self
.
patterns
.
append
(
pattern
)
pattern
=
PaddleGraph
(
graph_type
=
"dygraph"
)
pattern
.
add_layer
(
"self.create_parameter"
,
inputs
=
{},
outputs
=
[
gen_name
(
0
)])
pattern
.
add_layer
(
"paddle.full"
,
inputs
=
{},
outputs
=
[
gen_name
(
1
)],
shape
=
[
1
])
pattern
.
add_layer
(
"paddle.add"
,
inputs
=
{
"x"
:
gen_name
(
0
),
"y"
:
gen_name
(
1
)},
outputs
=
[
gen_name
(
2
)])
pattern
.
add_layer
(
"paddle.rsqrt"
,
inputs
=
{
"x"
:
gen_name
(
2
)},
outputs
=
[
gen_name
(
3
)])
pattern
.
add_layer
(
"self.create_parameter"
,
inputs
=
{},
outputs
=
[
gen_name
(
4
)])
pattern
.
add_layer
(
"paddle.multiply"
,
inputs
=
{
"x"
:
gen_name
(
3
),
"y"
:
gen_name
(
4
)},
outputs
=
[
gen_name
(
5
)])
pattern
.
add_layer
(
"paddle.multiply"
,
inputs
=
{
"x"
:
"bn-input-0"
,
"y"
:
gen_name
(
5
)},
outputs
=
[
gen_name
(
10
)])
pattern
.
add_layer
(
"self.create_parameter"
,
inputs
=
{},
outputs
=
[
gen_name
(
6
)])
pattern
.
add_layer
(
"paddle.multiply"
,
inputs
=
{
"x"
:
gen_name
(
6
),
"y"
:
gen_name
(
5
)},
outputs
=
[
gen_name
(
7
)])
pattern
.
add_layer
(
"self.create_parameter"
,
inputs
=
{},
outputs
=
[
gen_name
(
8
)])
pattern
.
add_layer
(
"fluid.layers.elementwise_sub"
,
inputs
=
{
"x"
:
gen_name
(
8
),
"y"
:
gen_name
(
7
)},
outputs
=
[
gen_name
(
9
)])
pattern
.
add_layer
(
"paddle.add"
,
inputs
=
{
"x"
:
gen_name
(
10
),
"y"
:
gen_name
(
9
)},
outputs
=
[
gen_name
(
11
)])
pattern
.
build
(
inputs
=
{
"input-0"
:
"bn-input-0"
,
})
self
.
patterns
.
append
(
pattern
)
def
insert_new_layer
(
self
,
graph
,
parameters
,
matches
):
new_layers
,
last_layer_id
=
self
.
gen_new_layer
(
matches
,
parameters
,
graph
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录