Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
12dc7fb1
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看板
提交
12dc7fb1
编写于
11月 19, 2020
作者:
S
SunAhong1993
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
for sript
上级
e1d7b069
变更
13
展开全部
显示空白变更内容
内联
并排
Showing
13 changed file
with
317 addition
and
1512 deletion
+317
-1512
x2paddle/convert.py
x2paddle/convert.py
+2
-6
x2paddle/core/program.py
x2paddle/core/program.py
+12
-11
x2paddle/decoder/pytorch_decoder.py
x2paddle/decoder/pytorch_decoder.py
+3
-2
x2paddle/op_mapper/dygraph/onnx2paddle/opset9/opset.py
x2paddle/op_mapper/dygraph/onnx2paddle/opset9/opset.py
+1
-1
x2paddle/op_mapper/dygraph/pytorch2paddle/aten.py
x2paddle/op_mapper/dygraph/pytorch2paddle/aten.py
+26
-21
x2paddle/op_mapper/dygraph/pytorch2paddle/prim2code.py
x2paddle/op_mapper/dygraph/pytorch2paddle/prim2code.py
+7
-3
x2paddle/op_mapper/dygraph/pytorch2paddle/pytorch_op_mapper.py
...dle/op_mapper/dygraph/pytorch2paddle/pytorch_op_mapper.py
+2
-2
x2paddle/optimizer/fusion/dygraph/adaptive_pool2d_fuser.py
x2paddle/optimizer/fusion/dygraph/adaptive_pool2d_fuser.py
+88
-46
x2paddle/optimizer/fusion/dygraph/batchnorm2d_fuser.py
x2paddle/optimizer/fusion/dygraph/batchnorm2d_fuser.py
+27
-33
x2paddle/optimizer/fusion/dygraph/fc_fuser.py
x2paddle/optimizer/fusion/dygraph/fc_fuser.py
+18
-22
x2paddle/optimizer/fusion/dygraph/interpolate_bilinear_fuser.py
...le/optimizer/fusion/dygraph/interpolate_bilinear_fuser.py
+114
-1360
x2paddle/optimizer/optimizer.py
x2paddle/optimizer/optimizer.py
+6
-3
x2paddle/optimizer/pattern_matcher.py
x2paddle/optimizer/pattern_matcher.py
+11
-2
未找到文件。
x2paddle/convert.py
浏览文件 @
12dc7fb1
...
@@ -209,7 +209,7 @@ def onnx2paddle(model_path, save_dir, paddle_type, params_merge=False):
...
@@ -209,7 +209,7 @@ def onnx2paddle(model_path, save_dir, paddle_type, params_merge=False):
mapper
.
save_inference_model
(
save_dir
,
params_merge
)
mapper
.
save_inference_model
(
save_dir
,
params_merge
)
def
pytorch2paddle
(
module
,
save_dir
,
jit_type
,
input_examples
):
def
pytorch2paddle
(
module
,
save_dir
,
jit_type
,
input_examples
=
None
):
# check pytorch installation and version
# check pytorch installation and version
try
:
try
:
import
torch
import
torch
...
@@ -232,7 +232,7 @@ def pytorch2paddle(module, save_dir, jit_type, input_examples):
...
@@ -232,7 +232,7 @@ def pytorch2paddle(module, save_dir, jit_type, input_examples):
if
jit_type
==
"trace"
:
if
jit_type
==
"trace"
:
model
=
TraceDecoder
(
module
,
input_examples
)
model
=
TraceDecoder
(
module
,
input_examples
)
else
:
else
:
model
=
ScriptDecoder
(
module
)
model
=
ScriptDecoder
(
module
,
input_examples
)
mapper
=
PyTorchOpMapper
(
model
)
mapper
=
PyTorchOpMapper
(
model
)
mapper
.
paddle_graph
.
build
()
mapper
.
paddle_graph
.
build
()
print
(
"Model optimizing ..."
)
print
(
"Model optimizing ..."
)
...
@@ -324,10 +324,6 @@ def main():
...
@@ -324,10 +324,6 @@ def main():
if
args
.
params_merge
:
if
args
.
params_merge
:
params_merge
=
True
params_merge
=
True
onnx2paddle
(
args
.
model
,
args
.
save_dir
,
args
.
paddle_type
,
params_merge
)
onnx2paddle
(
args
.
model
,
args
.
save_dir
,
args
.
paddle_type
,
params_merge
)
elif
args
.
framework
==
"pytorch"
:
assert
args
.
model
is
not
None
,
"--model should be defined while translating pytorch model"
pytorch2paddle
(
args
.
model
,
args
.
save_dir
,
args
.
jit_type
,
args
.
input_files
)
elif
args
.
framework
==
"paddle2onnx"
:
elif
args
.
framework
==
"paddle2onnx"
:
assert
args
.
model
is
not
None
,
"--model should be defined while translating paddle model to onnx"
assert
args
.
model
is
not
None
,
"--model should be defined while translating paddle model to onnx"
paddle2onnx
(
args
.
model
,
args
.
save_dir
,
opset_version
=
args
.
onnx_opset
)
paddle2onnx
(
args
.
model
,
args
.
save_dir
,
opset_version
=
args
.
onnx_opset
)
...
...
x2paddle/core/program.py
浏览文件 @
12dc7fb1
...
@@ -281,12 +281,13 @@ class PaddleGraph(object):
...
@@ -281,12 +281,13 @@ class PaddleGraph(object):
else
:
else
:
self
.
gen_dygraph_code
(
save_dir
)
self
.
gen_dygraph_code
(
save_dir
)
self
.
dump_dygraph_parameter
(
save_dir
)
self
.
dump_dygraph_parameter
(
save_dir
)
# 动转静
if
len
(
self
.
inputs_info
)
>
0
:
input_shapes
=
list
()
input_shapes
=
list
()
input_types
=
list
()
input_types
=
list
()
for
input_name
in
self
.
inputs
:
for
input_name
in
self
.
inputs
:
input_shapes
.
append
(
self
.
inputs_info
[
input_name
][
0
])
input_shapes
.
append
(
self
.
inputs_info
[
input_name
][
0
])
input_types
.
append
(
self
.
inputs_info
[
input_name
][
1
])
input_types
.
append
(
self
.
inputs_info
[
input_name
][
1
])
# 如果input_files非空,则导出推理模型;其值类似[[None, 3, 224, 224]]
self
.
dygraph2static
(
save_dir
,
input_shapes
,
input_types
)
self
.
dygraph2static
(
save_dir
,
input_shapes
,
input_types
)
def
gen_static_code
(
self
,
code_dir
):
def
gen_static_code
(
self
,
code_dir
):
...
@@ -424,9 +425,7 @@ class PaddleGraph(object):
...
@@ -424,9 +425,7 @@ class PaddleGraph(object):
if
self
.
edges_out
.
get
(
layer_id
,
0
)
==
0
:
if
self
.
edges_out
.
get
(
layer_id
,
0
)
==
0
:
for
i
,
output_name
in
enumerate
(
layer
.
outputs
):
for
i
,
output_name
in
enumerate
(
layer
.
outputs
):
if
(
"paddle.nn"
in
layer
.
kernel
and
"functional"
not
in
layer
.
kernel
)
or
\
if
(
"paddle.nn"
in
layer
.
kernel
and
"functional"
not
in
layer
.
kernel
):
(
layer
.
kernel
==
"paddle.to_tensor"
and
layer
.
attrs
[
"data"
].
startswith
(
"params["
))
or
\
"paddle.fluid.dygraph"
in
layer
.
kernel
:
if
i
==
0
:
if
i
==
0
:
continue
continue
if
output_name
not
in
self
.
outputs
:
if
output_name
not
in
self
.
outputs
:
...
@@ -512,6 +511,8 @@ class PaddleGraph(object):
...
@@ -512,6 +511,8 @@ class PaddleGraph(object):
return_code
=
"return {}"
.
format
(
", "
.
join
(
self
.
outputs
))
return_code
=
"return {}"
.
format
(
", "
.
join
(
self
.
outputs
))
self
.
forward_func
.
extend
(
gen_codes
([
return_code
],
indent
=
2
))
self
.
forward_func
.
extend
(
gen_codes
([
return_code
],
indent
=
2
))
for
code_line
in
self
.
forward_func
:
for
code_line
in
self
.
forward_func
:
if
"assert [1, 1] == 1 or [1, 1] == [1, 1], 'The [1, 1] must be [1, [1, 1]]!'"
in
code_line
:
continue
f
.
write
(
code_line
)
f
.
write
(
code_line
)
for
code_line
in
self
.
run_func
:
for
code_line
in
self
.
run_func
:
f
.
write
(
code_line
)
f
.
write
(
code_line
)
...
@@ -593,7 +594,7 @@ class PaddleGraph(object):
...
@@ -593,7 +594,7 @@ class PaddleGraph(object):
line
=
line
.
strip
(
", "
)
line
=
line
.
strip
(
", "
)
line
+=
")"
line
+=
")"
if
layer
.
kernel
==
"self.create_parameter"
:
if
layer
.
kernel
==
"self.create_parameter"
:
self
.
init_func
.
extend
(
gen_codes
([
"self."
+
line
],
indent
=
indent
))
self
.
init_func
.
extend
(
gen_codes
([
"self."
+
line
],
indent
=
2
))
self
.
forward_func
.
extend
(
gen_codes
([
"{} = self.{}"
.
format
(
layer
.
outputs
[
0
],
self
.
forward_func
.
extend
(
gen_codes
([
"{} = self.{}"
.
format
(
layer
.
outputs
[
0
],
layer
.
outputs
[
0
])],
indent
=
indent
))
layer
.
outputs
[
0
])],
indent
=
indent
))
else
:
else
:
...
...
x2paddle/decoder/pytorch_decoder.py
浏览文件 @
12dc7fb1
...
@@ -41,10 +41,10 @@ class ScriptDecoder(Decoder):
...
@@ -41,10 +41,10 @@ class ScriptDecoder(Decoder):
script_path (str): ScriptModule保存路径。
script_path (str): ScriptModule保存路径。
model_path (str): PyTorchModule保存路径。
model_path (str): PyTorchModule保存路径。
"""
"""
def
__init__
(
self
,
module
):
def
__init__
(
self
,
module
,
input_examples
=
None
):
self
.
script
=
torch
.
jit
.
script
(
module
)
self
.
script
=
torch
.
jit
.
script
(
module
)
self
.
graph
=
self
.
_optimize_graph
(
self
.
script
.
inlined_graph
)
self
.
graph
=
self
.
_optimize_graph
(
self
.
script
.
inlined_graph
)
self
.
input_examples
=
None
self
.
input_examples
=
input_examples
class
TraceDecoder
(
Decoder
):
class
TraceDecoder
(
Decoder
):
""" PyTorchModule后使用trace方式转换为ScriptModule。
""" PyTorchModule后使用trace方式转换为ScriptModule。
...
@@ -66,3 +66,4 @@ class TraceDecoder(Decoder):
...
@@ -66,3 +66,4 @@ class TraceDecoder(Decoder):
self
.
graph
=
self
.
_optimize_graph
(
self
.
script
.
inlined_graph
)
self
.
graph
=
self
.
_optimize_graph
(
self
.
script
.
inlined_graph
)
self
.
input_examples
=
input_examples
self
.
input_examples
=
input_examples
x2paddle/op_mapper/dygraph/onnx2paddle/opset9/opset.py
浏览文件 @
12dc7fb1
...
@@ -1180,7 +1180,7 @@ class OpSet9():
...
@@ -1180,7 +1180,7 @@ class OpSet9():
scale
=
beta
)
scale
=
beta
)
add_inputs
=
{
"x"
:
val_mm
,
"y"
:
var_beta
}
add_inputs
=
{
"x"
:
val_mm
,
"y"
:
var_beta
}
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
"paddle.add
d
"
,
"paddle.add"
,
inputs
=
add_inputs
,
inputs
=
add_inputs
,
outputs
=
[
node
.
layer_name
])
outputs
=
[
node
.
layer_name
])
...
...
x2paddle/op_mapper/dygraph/pytorch2paddle/aten.py
浏览文件 @
12dc7fb1
...
@@ -83,16 +83,21 @@ def aten_adaptive_avg_pool2d(mapper, graph, node):
...
@@ -83,16 +83,21 @@ def aten_adaptive_avg_pool2d(mapper, graph, node):
# 处理输入1,即%_output_size.1
# 处理输入1,即%_output_size.1
if
inputs_name
[
1
]
in
mapper
.
attrs
:
if
inputs_name
[
1
]
in
mapper
.
attrs
:
layer_attrs
[
"output_size"
]
=
mapper
.
attrs
[
inputs_name
[
1
]]
layer_attrs
[
"output_size"
]
=
mapper
.
attrs
[
inputs_name
[
1
]]
graph
.
add_layer
(
"paddle.nn.AdaptiveAvgPool2D"
,
inputs
=
layer_inputs
,
outputs
=
layer_outputs
,
scope_name
=
scope_name
,
**
layer_attrs
)
else
:
else
:
mapper
.
_check_input
(
graph
,
inputs_node
[
1
],
inputs_name
[
1
],
mapper
.
_check_input
(
graph
,
inputs_node
[
1
],
inputs_name
[
1
],
current_outputs
,
scope_name
)
current_outputs
,
scope_name
)
layer_inputs
[
"output_size"
]
=
inputs_name
[
1
]
layer_inputs
[
"output_size"
]
=
inputs_name
[
1
]
current_inputs
.
append
(
inputs_name
[
1
])
current_inputs
.
append
(
inputs_name
[
1
])
graph
.
add_layer
(
graph
.
add_layer
(
"paddle.nn.AdaptiveAvgPool2D
"
,
"paddle.nn.functional.adaptive_avg_pool2d
"
,
inputs
=
layer_inputs
,
inputs
=
layer_inputs
,
outputs
=
layer_outputs
,
outputs
=
layer_outputs
[
1
:]
,
scope_name
=
scope_name
,
scope_name
=
scope_name
,
**
layer_attrs
)
**
layer_attrs
)
return
current_inputs
,
current_outputs
return
current_inputs
,
current_outputs
...
@@ -828,7 +833,7 @@ def aten_constant_pad_nd(mapper, graph, node):
...
@@ -828,7 +833,7 @@ def aten_constant_pad_nd(mapper, graph, node):
outputs
=
[
inputs_name
[
0
]
+
"_if"
,
output_name
],
outputs
=
[
inputs_name
[
0
]
+
"_if"
,
output_name
],
scope_name
=
scope_name
)
scope_name
=
scope_name
)
if_layer
=
graph
.
layers
[
list
(
graph
.
layers
.
keys
())[
-
1
]]
if_layer
=
graph
.
layers
[
list
(
graph
.
layers
.
keys
())[
-
1
]]
block
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
block
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
block
.
add_layer
(
block
.
add_layer
(
"prim.sub"
,
"prim.sub"
,
inputs
=
{
"y"
:
inputs_name
[
0
]
+
"_len"
},
inputs
=
{
"y"
:
inputs_name
[
0
]
+
"_len"
},
...
@@ -859,7 +864,7 @@ def aten_constant_pad_nd(mapper, graph, node):
...
@@ -859,7 +864,7 @@ def aten_constant_pad_nd(mapper, graph, node):
outputs
=
[
output_name
],
outputs
=
[
output_name
],
scope_name
=
scope_name
)
scope_name
=
scope_name
)
if_layer
.
add_block
(
block
)
if_layer
.
add_block
(
block
)
block
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
block
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
layer_inputs
[
"input"
]
=
inputs_name
[
0
]
layer_inputs
[
"input"
]
=
inputs_name
[
0
]
block
.
add_layer
(
block
.
add_layer
(
kernel
,
inputs
=
layer_inputs
,
outputs
=
layer_outputs
,
scope_name
=
scope_name
,
**
layer_attrs
)
kernel
,
inputs
=
layer_inputs
,
outputs
=
layer_outputs
,
scope_name
=
scope_name
,
**
layer_attrs
)
...
@@ -1186,7 +1191,7 @@ def aten_dim(mapper, graph, node):
...
@@ -1186,7 +1191,7 @@ def aten_dim(mapper, graph, node):
current_outputs
=
[
output_name
]
current_outputs
=
[
output_name
]
# 处理输入0,即%input.8
# 处理输入0,即%input.8
mapper
.
_check_input
(
graph
,
inputs_node
[
0
],
inputs_name
[
0
],
current_outputs
,
scope_name
)
mapper
.
_check_input
(
graph
,
inputs_node
[
0
],
inputs_name
[
0
],
current_outputs
,
scope_name
)
layer_inputs
[
"input
s
"
]
=
inputs_name
[
0
]
layer_inputs
[
"input"
]
=
inputs_name
[
0
]
# 获取当前节点输入的list
# 获取当前节点输入的list
current_inputs
=
list
(
layer_inputs
.
values
())
current_inputs
=
list
(
layer_inputs
.
values
())
...
@@ -1468,7 +1473,7 @@ def aten_expand(mapper, graph, node):
...
@@ -1468,7 +1473,7 @@ def aten_expand(mapper, graph, node):
outputs
=
[
inputs_name
[
0
]
+
"_if1"
,
inputs_name
[
1
]
+
"_var"
],
outputs
=
[
inputs_name
[
0
]
+
"_if1"
,
inputs_name
[
1
]
+
"_var"
],
scope_name
=
scope_name
)
scope_name
=
scope_name
)
if_layer
=
graph
.
layers
[
list
(
graph
.
layers
.
keys
())[
-
1
]]
if_layer
=
graph
.
layers
[
list
(
graph
.
layers
.
keys
())[
-
1
]]
block
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
block
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
block
.
add_layer
(
block
.
add_layer
(
"paddle.cast"
,
"paddle.cast"
,
inputs
=
{
"x"
:
inputs_name
[
0
]},
inputs
=
{
"x"
:
inputs_name
[
0
]},
...
@@ -1483,7 +1488,7 @@ def aten_expand(mapper, graph, node):
...
@@ -1483,7 +1488,7 @@ def aten_expand(mapper, graph, node):
dtype
=
string
(
"int64"
),
dtype
=
string
(
"int64"
),
default_initializer
=
"paddle.nn.initializer.Constant(value=0.0)"
)
default_initializer
=
"paddle.nn.initializer.Constant(value=0.0)"
)
if_layer
.
add_block
(
block
)
if_layer
.
add_block
(
block
)
block
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
block
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
block
.
add_layer
(
block
.
add_layer
(
"prim.type"
,
"prim.type"
,
inputs
=
{
"input"
:
inputs_name
[
0
]},
inputs
=
{
"input"
:
inputs_name
[
0
]},
...
@@ -1558,7 +1563,7 @@ def aten_expand_as(mapper, graph, node):
...
@@ -1558,7 +1563,7 @@ def aten_expand_as(mapper, graph, node):
outputs
=
[
inputs_name
[
0
]
+
"_if1"
],
outputs
=
[
inputs_name
[
0
]
+
"_if1"
],
scope_name
=
scope_name
)
scope_name
=
scope_name
)
if_layer
=
graph
.
layers
[
list
(
graph
.
layers
.
keys
())[
-
1
]]
if_layer
=
graph
.
layers
[
list
(
graph
.
layers
.
keys
())[
-
1
]]
block
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
block
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
block
.
add_layer
(
block
.
add_layer
(
"prim.type"
,
"prim.type"
,
inputs
=
{
"input"
:
inputs_name
[
1
]},
inputs
=
{
"input"
:
inputs_name
[
1
]},
...
@@ -1571,7 +1576,7 @@ def aten_expand_as(mapper, graph, node):
...
@@ -1571,7 +1576,7 @@ def aten_expand_as(mapper, graph, node):
scope_name
=
scope_name
,
scope_name
=
scope_name
,
dtype
=
inputs_name
[
1
]
+
"_type"
)
dtype
=
inputs_name
[
1
]
+
"_type"
)
if_layer
.
add_block
(
block
)
if_layer
.
add_block
(
block
)
block
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
block
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
if_layer
.
add_block
(
block
)
if_layer
.
add_block
(
block
)
if_layer
.
inputs
[
"input-0"
]
=
inputs_name
[
0
]
if_layer
.
inputs
[
"input-0"
]
=
inputs_name
[
0
]
if_layer
.
inputs
[
"input-1"
]
=
inputs_name
[
1
]
if_layer
.
inputs
[
"input-1"
]
=
inputs_name
[
1
]
...
@@ -1582,7 +1587,7 @@ def aten_expand_as(mapper, graph, node):
...
@@ -1582,7 +1587,7 @@ def aten_expand_as(mapper, graph, node):
outputs
=
[
inputs_name
[
0
]
+
"_if2"
],
outputs
=
[
inputs_name
[
0
]
+
"_if2"
],
scope_name
=
scope_name
)
scope_name
=
scope_name
)
if_layer
=
graph
.
layers
[
list
(
graph
.
layers
.
keys
())[
-
1
]]
if_layer
=
graph
.
layers
[
list
(
graph
.
layers
.
keys
())[
-
1
]]
block
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
block
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
block
.
add_layer
(
block
.
add_layer
(
"fluid.layers.cast"
,
"fluid.layers.cast"
,
inputs
=
{
"x"
:
layer_outputs
[
0
]},
inputs
=
{
"x"
:
layer_outputs
[
0
]},
...
@@ -2420,14 +2425,14 @@ def aten_masked_fill_(mapper, graph, node):
...
@@ -2420,14 +2425,14 @@ def aten_masked_fill_(mapper, graph, node):
outputs
=
[
inputs_name
[
2
]
+
"_if"
],
outputs
=
[
inputs_name
[
2
]
+
"_if"
],
scope_name
=
scope_name
)
scope_name
=
scope_name
)
if_layer
=
graph
.
layers
[
list
(
graph
.
layers
.
keys
())[
-
1
]]
if_layer
=
graph
.
layers
[
list
(
graph
.
layers
.
keys
())[
-
1
]]
block
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
block
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
block
.
add_layer
(
block
.
add_layer
(
"prim.equal"
,
"prim.equal"
,
inputs
=
{
"input"
:
inputs_name
[
1
]
+
"_mask"
},
inputs
=
{
"input"
:
inputs_name
[
1
]
+
"_mask"
},
outputs
=
[
inputs_name
[
2
]
+
"_1"
],
outputs
=
[
inputs_name
[
2
]
+
"_1"
],
scope_name
=
scope_name
)
scope_name
=
scope_name
)
if_layer
.
add_block
(
block
)
if_layer
.
add_block
(
block
)
block
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
block
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
block
.
add_layer
(
block
.
add_layer
(
"prim.mul"
,
"prim.mul"
,
inputs
=
{
"x"
:
inputs_name
[
1
]
+
"_mask"
,
inputs
=
{
"x"
:
inputs_name
[
1
]
+
"_mask"
,
...
@@ -2528,14 +2533,14 @@ def aten_masked_fill(mapper, graph, node):
...
@@ -2528,14 +2533,14 @@ def aten_masked_fill(mapper, graph, node):
outputs
=
[
inputs_name
[
2
]
+
"_if"
],
outputs
=
[
inputs_name
[
2
]
+
"_if"
],
scope_name
=
scope_name
)
scope_name
=
scope_name
)
if_layer
=
graph
.
layers
[
list
(
graph
.
layers
.
keys
())[
-
1
]]
if_layer
=
graph
.
layers
[
list
(
graph
.
layers
.
keys
())[
-
1
]]
block
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
block
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
block
.
add_layer
(
block
.
add_layer
(
"prim.equal"
,
"prim.equal"
,
inputs
=
{
"input"
:
inputs_name
[
1
]
+
"_mask"
},
inputs
=
{
"input"
:
inputs_name
[
1
]
+
"_mask"
},
outputs
=
[
inputs_name
[
2
]
+
"_1"
],
outputs
=
[
inputs_name
[
2
]
+
"_1"
],
scope_name
=
scope_name
)
scope_name
=
scope_name
)
if_layer
.
add_block
(
block
)
if_layer
.
add_block
(
block
)
block
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
block
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
block
.
add_layer
(
block
.
add_layer
(
"prim.mul"
,
"prim.mul"
,
inputs
=
{
"x"
:
inputs_name
[
1
]
+
"_mask"
,
inputs
=
{
"x"
:
inputs_name
[
1
]
+
"_mask"
,
...
@@ -4157,14 +4162,14 @@ def aten_upsample_bilinear2d(mapper, graph, node):
...
@@ -4157,14 +4162,14 @@ def aten_upsample_bilinear2d(mapper, graph, node):
outputs
=
[
inputs_name
[
0
]
+
"_if1"
],
outputs
=
[
inputs_name
[
0
]
+
"_if1"
],
scope_name
=
scope_name
)
scope_name
=
scope_name
)
if_layer
=
graph
.
layers
[
list
(
graph
.
layers
.
keys
())[
-
1
]]
if_layer
=
graph
.
layers
[
list
(
graph
.
layers
.
keys
())[
-
1
]]
block
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
block
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
block
.
add_layer
(
block
.
add_layer
(
"prim.var2list"
,
"prim.var2list"
,
inputs
=
{
"input"
:
inputs_name
[
1
]},
inputs
=
{
"input"
:
inputs_name
[
1
]},
outputs
=
[
inputs_name
[
1
]],
outputs
=
[
inputs_name
[
1
]],
scope_name
=
scope_name
)
scope_name
=
scope_name
)
if_layer
.
add_block
(
block
)
if_layer
.
add_block
(
block
)
block
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
block
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
if_layer
.
add_block
(
block
)
if_layer
.
add_block
(
block
)
if_layer
.
inputs
[
"input-0"
]
=
inputs_name
[
1
]
if_layer
.
inputs
[
"input-0"
]
=
inputs_name
[
1
]
# 处理输入2,即%5421
# 处理输入2,即%5421
...
...
x2paddle/op_mapper/dygraph/pytorch2paddle/prim2code.py
浏览文件 @
12dc7fb1
...
@@ -208,6 +208,10 @@ def prim_if(layer, indent=1, init_func=[], forward_func=[], layer_id=None, diffe
...
@@ -208,6 +208,10 @@ def prim_if(layer, indent=1, init_func=[], forward_func=[], layer_id=None, diffe
line
=
"if {} :"
.
format
(
get_value
(
layer
,
"input"
,
different_attrs
))
line
=
"if {} :"
.
format
(
get_value
(
layer
,
"input"
,
different_attrs
))
forward_func
.
extend
(
gen_codes
([
line
],
indent
=
indent
))
forward_func
.
extend
(
gen_codes
([
line
],
indent
=
indent
))
block
=
layer
.
blocks
[
0
]
block
=
layer
.
blocks
[
0
]
if
len
(
block
.
layers
)
==
0
:
line
=
"pass"
forward_func
.
extend
(
gen_codes
([
line
],
indent
=
indent
+
1
))
else
:
b_init_lines
,
b_forward_lines
=
block
.
gen_dygraph_code
(
indent
=
indent
+
1
)
b_init_lines
,
b_forward_lines
=
block
.
gen_dygraph_code
(
indent
=
indent
+
1
)
init_func
.
extend
(
b_init_lines
)
init_func
.
extend
(
b_init_lines
)
forward_func
.
extend
(
b_forward_lines
)
forward_func
.
extend
(
b_forward_lines
)
...
...
x2paddle/op_mapper/dygraph/pytorch2paddle/pytorch_op_mapper.py
浏览文件 @
12dc7fb1
...
@@ -77,7 +77,7 @@ class PyTorchOpMapper(OpMapper):
...
@@ -77,7 +77,7 @@ class PyTorchOpMapper(OpMapper):
current_node_outputs
.
extend
(
outputs
)
current_node_outputs
.
extend
(
outputs
)
# 初始化
# 初始化
graph
=
PaddleGraph
(
parent_layer
,
graph_type
=
"dygraph"
)
graph
=
PaddleGraph
(
parent_layer
=
parent_layer
,
graph_type
=
"dygraph"
)
if
"TopLevelTracedModule"
in
str
(
type
(
self
.
script
)):
if
"TopLevelTracedModule"
in
str
(
type
(
self
.
script
)):
graph
.
set_script
(
self
.
script
)
graph
.
set_script
(
self
.
script
)
current_node_outputs
=
[]
current_node_outputs
=
[]
...
@@ -277,7 +277,7 @@ class PyTorchOpMapper(OpMapper):
...
@@ -277,7 +277,7 @@ class PyTorchOpMapper(OpMapper):
control_output_id
=
index
-
1
control_output_id
=
index
-
1
output_node_name
=
parent_layer
.
outputs
[
control_output_id
]
output_node_name
=
parent_layer
.
outputs
[
control_output_id
]
current_outputs
=
[
output_node_name
]
current_outputs
=
[
output_node_name
]
self
.
_check_input
(
graph
,
node
,
input_node_name
,
current_outputs
)
self
.
_check_input
(
graph
,
node
,
input_node_name
,
current_outputs
,
scope_name
)
graph
.
add_layer
(
graph
.
add_layer
(
"prim.equal"
,
"prim.equal"
,
inputs
=
{
'input'
:
input_node_name
},
inputs
=
{
'input'
:
input_node_name
},
...
...
x2paddle/optimizer/fusion/dygraph/adaptive_pool2d_fuser.py
浏览文件 @
12dc7fb1
...
@@ -13,6 +13,7 @@
...
@@ -13,6 +13,7 @@
# limitations under the License.
# limitations under the License.
import
numpy
as
np
import
numpy
as
np
import
copy
from
x2paddle.optimizer.pattern_matcher
import
FuseBase
from
x2paddle.optimizer.pattern_matcher
import
FuseBase
from
x2paddle.core.program
import
PaddleGraph
,
PaddleLayer
from
x2paddle.core.program
import
PaddleGraph
,
PaddleLayer
from
x2paddle.core.util
import
*
from
x2paddle.core.util
import
*
...
@@ -21,11 +22,13 @@ from x2paddle.core.util import *
...
@@ -21,11 +22,13 @@ from x2paddle.core.util import *
class
DygraphAdaptivePool2dFuser
(
FuseBase
):
class
DygraphAdaptivePool2dFuser
(
FuseBase
):
def
__init__
(
self
):
def
__init__
(
self
):
super
(
DygraphAdaptivePool2dFuser
,
self
).
__init__
(
graph_type
=
"dygraph"
)
super
(
DygraphAdaptivePool2dFuser
,
self
).
__init__
(
graph_type
=
"dygraph"
)
self
.
patterns
=
list
()
def
build_pattern
(
self
):
def
build_pattern
(
self
):
""" 描述需要替换的adaptive pool2d图结构。
""" 描述需要替换的adaptive pool2d图结构。
adaptive pool2d层模式python实现代码示例:
adaptive pool2d层模式python实现代码示例:
x68 = fluid.layers.shape(input=x60)
模式一:
x68 = prim.shape(input=x60)
x69 = len(x68)
x69 = len(x68)
x70 = x69 <= 2
x70 = x69 <= 2
if x70 :
if x70 :
...
@@ -38,57 +41,64 @@ class DygraphAdaptivePool2dFuser(FuseBase):
...
@@ -38,57 +41,64 @@ class DygraphAdaptivePool2dFuser(FuseBase):
for _x79 in range(x77):
for _x79 in range(x77):
x80 = [6, 6][_x79]
x80 = [6, 6][_x79]
x73.append(x80)
x73.append(x80)
x81 = fluid.layers.adaptive_pool2d(input=x60, pool_size=x73, pool_type='avg')
x81 = paddle.nn.functional.adaptive_avg_pool2d(input=x60, pool_size=x73, pool_type='avg')
模式二:
x64 = x60.shape
x65 = len(x64)
x66 = x65 > 2
if x66 :
pass
else:
raise RaiseException('AssertionError: ')
x69 = self.pool2d3(x60)
"""
"""
def
gen_name
(
id
):
def
gen_name
(
id
):
return
"x"
+
str
(
id
)
return
"x"
+
str
(
id
)
self
.
pattern
.
add_layer
(
# 模式一:
"fluid.layers.shape"
,
pattern
=
PaddleGraph
(
graph_type
=
"dygraph"
)
pattern
.
add_layer
(
"prim.shape"
,
inputs
=
{
'input'
:
"pool-input-0"
},
inputs
=
{
'input'
:
"pool-input-0"
},
outputs
=
[
gen_name
(
1
)])
outputs
=
[
gen_name
(
1
)])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"prim.len"
,
inputs
=
{
"input"
:
gen_name
(
1
)},
outputs
=
[
gen_name
(
6
)])
"prim.len"
,
inputs
=
{
"input"
:
gen_name
(
1
)},
outputs
=
[
gen_name
(
6
)])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"prim.le"
,
inputs
=
{
"x"
:
gen_name
(
6
)},
outputs
=
[
gen_name
(
8
)],
y
=
2
)
"prim.le"
,
inputs
=
{
"x"
:
gen_name
(
6
)},
outputs
=
[
gen_name
(
8
)],
y
=
2
)
self
.
pattern
.
add_layer
(
"prim.if"
,
{
'input'
:
gen_name
(
8
)},
[
gen_name
(
9
)])
pattern
.
add_layer
(
"prim.if"
,
{
'input'
:
gen_name
(
8
)},
[
gen_name
(
9
)])
if_layer
=
self
.
pattern
.
layers
[
list
(
self
.
pattern
.
layers
.
keys
())[
-
1
]]
if_layer
=
pattern
.
layers
[
list
(
pattern
.
layers
.
keys
())[
-
1
]]
pattern_block0
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
pattern_block0
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
pattern_block0
.
add_layer
(
pattern_block0
.
add_layer
(
"prim.exception"
,
"prim.exception"
,
inputs
=
{},
inputs
=
{},
outputs
=
[
gen_name
(
9
)],
outputs
=
[
gen_name
(
9
)],
input
=
"Exception"
)
input
=
"Exception"
)
if_layer
.
add_block
(
pattern_block0
)
if_layer
.
add_block
(
pattern_block0
)
pattern_block1
=
PaddleGraph
(
if_layer
,
graph_type
=
"dygraph"
)
pattern_block1
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
if_layer
.
add_block
(
pattern_block1
)
if_layer
.
add_block
(
pattern_block1
)
self
.
pattern
.
add_layer
(
"prim.list"
,
inputs
=
{},
outputs
=
[
gen_name
(
10
)])
pattern
.
add_layer
(
"prim.list"
,
inputs
=
{},
outputs
=
[
gen_name
(
10
)])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"prim.slice"
,
"prim.slice"
,
inputs
=
{
"input"
:
gen_name
(
1
),
},
inputs
=
{
"input"
:
gen_name
(
1
),
},
outputs
=
[
gen_name
(
12
)],
outputs
=
[
gen_name
(
12
)])
start
=-
1
,
pattern
.
add_layer
(
end
=
100
,
step
=
1
)
self
.
pattern
.
add_layer
(
"prim.len"
,
inputs
=
{
"input"
:
gen_name
(
12
)},
outputs
=
[
gen_name
(
14
)])
"prim.len"
,
inputs
=
{
"input"
:
gen_name
(
12
)},
outputs
=
[
gen_name
(
14
)])
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"prim.list"
,
"prim.list"
,
inputs
=
{
"input1"
:
gen_name
(
14
)},
inputs
=
{
"input1"
:
gen_name
(
14
)},
outputs
=
[
gen_name
(
15
)],
outputs
=
[
gen_name
(
15
)])
input0
=
2
)
pattern
.
add_layer
(
self
.
pattern
.
add_layer
(
"prim.min"
,
inputs
=
{
"input"
:
gen_name
(
15
)},
outputs
=
[
gen_name
(
16
)])
"prim.min"
,
inputs
=
{
"input"
:
gen_name
(
15
)},
outputs
=
[
gen_name
(
16
)])
self
.
pattern
.
add_layer
(
"prim.loop"
,
{
'input'
:
gen_name
(
16
)},
pattern
.
add_layer
(
"prim.loop"
,
{
'input'
:
gen_name
(
16
)},
[
gen_name
(
17
),
gen_name
(
18
)])
[
gen_name
(
17
),
gen_name
(
18
)])
loop_layer
=
self
.
pattern
.
layers
[
list
(
self
.
pattern
.
layers
.
keys
())[
-
1
]]
loop_layer
=
pattern
.
layers
[
list
(
pattern
.
layers
.
keys
())[
-
1
]]
pattern_block
=
PaddleGraph
(
loop_layer
,
graph_type
=
"dygraph"
)
pattern_block
=
PaddleGraph
(
loop_layer
,
graph_type
=
"dygraph"
)
pattern_block
.
add_layer
(
pattern_block
.
add_layer
(
"prim.getitem"
,
"prim.getitem"
,
inputs
=
{
"index"
:
gen_name
(
18
)},
inputs
=
{
"index"
:
gen_name
(
18
)},
outputs
=
[
gen_name
(
19
)],
outputs
=
[
gen_name
(
19
)])
list
=
[
6
,
6
])
pattern_block
.
add_layer
(
pattern_block
.
add_layer
(
"prim.append"
,
"prim.append"
,
inputs
=
{
"list"
:
gen_name
(
10
),
inputs
=
{
"list"
:
gen_name
(
10
),
...
@@ -97,13 +107,44 @@ class DygraphAdaptivePool2dFuser(FuseBase):
...
@@ -97,13 +107,44 @@ class DygraphAdaptivePool2dFuser(FuseBase):
loop_layer
.
inputs
[
"input-0"
]
=
gen_name
(
10
)
loop_layer
.
inputs
[
"input-0"
]
=
gen_name
(
10
)
loop_layer
.
add_block
(
pattern_block
)
loop_layer
.
add_block
(
pattern_block
)
pool_attrs
=
{
'pool_type'
:
string
(
"avg"
)}
pool_attrs
=
{
'pool_type'
:
string
(
"avg"
)}
self
.
pattern
.
add_layer
(
pattern
.
add_layer
(
"
fluid.layers.adaptive
_pool2d"
,
"
paddle.nn.functional.adaptive_avg
_pool2d"
,
inputs
=
{
'input'
:
"pool-input-0"
,
inputs
=
{
'input'
:
"pool-input-0"
,
"pool_size"
:
gen_name
(
10
)},
"pool_size"
:
gen_name
(
10
)},
outputs
=
[
gen_name
(
21
)],
outputs
=
[
gen_name
(
21
)],
**
pool_attrs
)
**
pool_attrs
)
self
.
pattern
.
build
(
inputs
=
{
"input-0"
:
"pool-input-0"
,
})
pattern
.
build
(
inputs
=
{
"input-0"
:
"pool-input-0"
,
})
self
.
patterns
.
append
(
pattern
)
# 模式二:
pattern
=
PaddleGraph
(
graph_type
=
"dygraph"
)
pattern
.
add_layer
(
"prim.shape"
,
inputs
=
{
'input'
:
"pool-input-0"
},
outputs
=
[
gen_name
(
0
)])
pattern
.
add_layer
(
"prim.len"
,
inputs
=
{
"input"
:
gen_name
(
0
)},
outputs
=
[
gen_name
(
1
)])
pattern
.
add_layer
(
"prim.gt"
,
inputs
=
{
"x"
:
gen_name
(
1
)},
outputs
=
[
gen_name
(
2
)],
y
=
2
)
pattern
.
add_layer
(
"prim.if"
,
{
'input'
:
gen_name
(
2
)},
[
gen_name
(
3
)])
if_layer
=
pattern
.
layers
[
list
(
pattern
.
layers
.
keys
())[
-
1
]]
pattern_block0
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
if_layer
.
add_block
(
pattern_block0
)
pattern_block1
=
PaddleGraph
(
parent_layer
=
if_layer
,
graph_type
=
"dygraph"
)
pattern_block1
.
add_layer
(
"prim.exception"
,
inputs
=
{},
outputs
=
[
gen_name
(
4
)],
input
=
"Exception"
)
if_layer
.
add_block
(
pattern_block1
)
pattern
.
add_layer
(
"paddle.nn.AdaptiveAvgPool2D"
,
inputs
=
{
"input"
:
"pool-input-0"
},
outputs
=
[
"pool1"
,
gen_name
(
5
)])
pattern
.
build
(
inputs
=
{
"input-0"
:
"pool-input-0"
,
"input-1"
:
"pool-input-0"
,})
self
.
patterns
.
append
(
pattern
)
def
insert_new_layer
(
self
,
graph
,
parameters
,
matches
):
def
insert_new_layer
(
self
,
graph
,
parameters
,
matches
):
parameters
=
graph
.
parameters
parameters
=
graph
.
parameters
...
@@ -114,20 +155,21 @@ class DygraphAdaptivePool2dFuser(FuseBase):
...
@@ -114,20 +155,21 @@ class DygraphAdaptivePool2dFuser(FuseBase):
def
gen_new_layer
(
self
,
parameters
,
matches
):
def
gen_new_layer
(
self
,
parameters
,
matches
):
layers_id
=
list
(
matches
.
keys
())
layers_id
=
list
(
matches
.
keys
())
if
matches
[
layers_id
[
-
1
]].
kernel
==
"paddle.nn.functional.adaptive_avg_pool2d"
:
layer
=
matches
[
layers_id
[
11
]]
layer
=
matches
[
layers_id
[
11
]]
pool_size
=
layer
.
attrs
[
"list"
]
pool_size
=
layer
.
attrs
[
"list"
]
layer
=
matches
[
layers_id
[
0
]]
layer
=
matches
[
layers_id
[
0
]]
input_name
=
layer
.
inputs
[
"input"
]
input_name
=
layer
.
inputs
[
"input"
]
layer
=
matches
[
layers_id
[
-
1
]]
layer
=
matches
[
layers_id
[
-
1
]]
output_name
=
layer
.
outputs
[
0
]
output_name
=
layer
.
outputs
[
0
]
pool_type
=
layer
.
attrs
[
"pool_type"
]
attrs
=
dict
()
attrs
=
dict
()
attrs
[
"pool_size"
]
=
pool_size
attrs
[
"output_size"
]
=
pool_size
attrs
[
"pool_type"
]
=
pool_type
new_layer
=
PaddleLayer
(
new_layer
=
PaddleLayer
(
layers_id
[
0
],
layers_id
[
0
],
"fluid.layers.adaptive
_pool2d"
,
"paddle.nn.functional.adaptive_avg
_pool2d"
,
inputs
=
{
"input"
:
input_name
},
inputs
=
{
"input"
:
input_name
},
outputs
=
[
output_name
],
outputs
=
[
output_name
],
**
attrs
)
**
attrs
)
else
:
new_layer
=
copy
.
deepcopy
(
matches
[
layers_id
[
-
1
]])
return
new_layer
return
new_layer
x2paddle/optimizer/fusion/dygraph/batchnorm2d_fuser.py
浏览文件 @
12dc7fb1
...
@@ -25,33 +25,33 @@ class DygraphBatchNorm2dFuser(FuseBase):
...
@@ -25,33 +25,33 @@ class DygraphBatchNorm2dFuser(FuseBase):
def
build_pattern
(
self
):
def
build_pattern
(
self
):
""" 描述需要替换的batchnorm2d图结构。
""" 描述需要替换的batchnorm2d图结构。
batchnorm2d层模式python实现代码示例:
batchnorm2d层模式python实现代码示例:
x
336 = fluid.layers.shape(input=x334)
x
2011 = x2009.shape
x
336 = len(x336
)
x
2011 = len(x2011
)
x
337 = x336
!= 4
x
2012 = x2011
!= 4
if x
337
:
if x
2012
:
raise RaiseException('Exception')
raise RaiseException('Exception')
if False :
if False :
x
351 = fluid.layers.shape(input=x334)
x
2026 = x2009.shape
x
352 = x351
[0]
x
2027 = x2026
[0]
x
353 = len(x351
)
x
2028 = len(x2026
)
x
354 = x353
- 2
x
2029 = x2028
- 2
x
357 = x352
x
2032 = x2027
for _x
356 in range(x354
):
for _x
2031 in range(x2029
):
x
358 = _x356
+ 2
x
2033 = _x2031
+ 2
x
359 = x351[x358
]
x
2034 = x2026[x2033
]
x
360 = x357 * x359
x
2035 = x2032 * x2034
x
355 = x360
x
2030 = x2035
x
361 = x355
== 1
x
2036 = x2030
== 1
if x
361
:
if x
2036
:
raise RaiseException('Exception')
raise RaiseException('Exception')
x
364 = self.batchnorm7(x334
)
x
2039 = self.batchnorm50(x2009
)
"""
"""
def
gen_name
(
id
):
def
gen_name
(
id
):
return
"x"
+
str
(
id
)
return
"x"
+
str
(
id
)
self
.
pattern
.
add_layer
(
self
.
pattern
.
add_layer
(
"
fluid.layers
.shape"
,
"
prim
.shape"
,
inputs
=
{
'input'
:
"bn-input-0"
},
inputs
=
{
'input'
:
"bn-input-0"
},
outputs
=
[
gen_name
(
0
)])
outputs
=
[
gen_name
(
0
)])
self
.
pattern
.
add_layer
(
self
.
pattern
.
add_layer
(
...
@@ -60,20 +60,20 @@ class DygraphBatchNorm2dFuser(FuseBase):
...
@@ -60,20 +60,20 @@ class DygraphBatchNorm2dFuser(FuseBase):
"prim.ne"
,
inputs
=
{
"x"
:
gen_name
(
0
)},
outputs
=
[
gen_name
(
1
)],
y
=
4
)
"prim.ne"
,
inputs
=
{
"x"
:
gen_name
(
0
)},
outputs
=
[
gen_name
(
1
)],
y
=
4
)
self
.
pattern
.
add_layer
(
"prim.if"
,
{
'input'
:
gen_name
(
1
)},
[
gen_name
(
2
)])
self
.
pattern
.
add_layer
(
"prim.if"
,
{
'input'
:
gen_name
(
1
)},
[
gen_name
(
2
)])
if_layer1
=
self
.
pattern
.
layers
[
list
(
self
.
pattern
.
layers
.
keys
())[
-
1
]]
if_layer1
=
self
.
pattern
.
layers
[
list
(
self
.
pattern
.
layers
.
keys
())[
-
1
]]
pattern_block0
=
PaddleGraph
(
if_layer1
,
graph_type
=
"dygraph"
)
pattern_block0
=
PaddleGraph
(
parent_layer
=
if_layer1
,
graph_type
=
"dygraph"
)
pattern_block0
.
add_layer
(
pattern_block0
.
add_layer
(
"prim.exception"
,
"prim.exception"
,
inputs
=
{},
inputs
=
{},
outputs
=
[
gen_name
(
3
)],
outputs
=
[
gen_name
(
3
)],
input
=
"Exception"
)
input
=
"Exception"
)
if_layer1
.
add_block
(
pattern_block0
)
if_layer1
.
add_block
(
pattern_block0
)
pattern_block1
=
PaddleGraph
(
if_layer1
,
graph_type
=
"dygraph"
)
pattern_block1
=
PaddleGraph
(
parent_layer
=
if_layer1
,
graph_type
=
"dygraph"
)
if_layer1
.
add_block
(
pattern_block1
)
if_layer1
.
add_block
(
pattern_block1
)
self
.
pattern
.
add_layer
(
"prim.if"
,
{},
[
gen_name
(
4
)],
input
=
False
)
self
.
pattern
.
add_layer
(
"prim.if"
,
{},
[
gen_name
(
4
)],
input
=
False
)
if_layer2
=
self
.
pattern
.
layers
[
list
(
self
.
pattern
.
layers
.
keys
())[
-
1
]]
if_layer2
=
self
.
pattern
.
layers
[
list
(
self
.
pattern
.
layers
.
keys
())[
-
1
]]
pattern_block0
=
PaddleGraph
(
if_layer2
,
graph_type
=
"dygraph"
)
pattern_block0
=
PaddleGraph
(
parent_layer
=
if_layer2
,
graph_type
=
"dygraph"
)
pattern_block0
.
add_layer
(
pattern_block0
.
add_layer
(
"
fluid.layers
.shape"
,
"
prim
.shape"
,
inputs
=
{
'input'
:
"bn-input-0"
},
inputs
=
{
'input'
:
"bn-input-0"
},
outputs
=
[
gen_name
(
5
)])
outputs
=
[
gen_name
(
5
)])
pattern_block0
.
add_layer
(
pattern_block0
.
add_layer
(
...
@@ -93,7 +93,7 @@ class DygraphBatchNorm2dFuser(FuseBase):
...
@@ -93,7 +93,7 @@ class DygraphBatchNorm2dFuser(FuseBase):
outputs
=
[
gen_name
(
8.1
),
gen_name
(
10
)])
outputs
=
[
gen_name
(
8.1
),
gen_name
(
10
)])
loop_layer
=
pattern_block0
.
layers
[
list
(
pattern_block0
.
layers
.
keys
())[
loop_layer
=
pattern_block0
.
layers
[
list
(
pattern_block0
.
layers
.
keys
())[
-
1
]]
-
1
]]
pattern_block0_block0
=
PaddleGraph
(
loop_layer
,
graph_type
=
"dygraph"
)
pattern_block0_block0
=
PaddleGraph
(
parent_layer
=
loop_layer
,
graph_type
=
"dygraph"
)
pattern_block0_block0
.
add_layer
(
pattern_block0_block0
.
add_layer
(
"prim.add"
,
inputs
=
{
"x"
:
gen_name
(
10
)},
outputs
=
[
gen_name
(
11
)],
y
=
2
)
"prim.add"
,
inputs
=
{
"x"
:
gen_name
(
10
)},
outputs
=
[
gen_name
(
11
)],
y
=
2
)
pattern_block0_block0
.
add_layer
(
pattern_block0_block0
.
add_layer
(
...
@@ -119,27 +119,24 @@ class DygraphBatchNorm2dFuser(FuseBase):
...
@@ -119,27 +119,24 @@ class DygraphBatchNorm2dFuser(FuseBase):
"prim.if"
,
inputs
=
{
"input"
:
gen_name
(
14
)},
outputs
=
[
gen_name
(
15
)])
"prim.if"
,
inputs
=
{
"input"
:
gen_name
(
14
)},
outputs
=
[
gen_name
(
15
)])
if_layer21
=
pattern_block0
.
layers
[
list
(
pattern_block0
.
layers
.
keys
())[
if_layer21
=
pattern_block0
.
layers
[
list
(
pattern_block0
.
layers
.
keys
())[
-
1
]]
-
1
]]
pattern_block0_block0
=
PaddleGraph
(
if_layer21
,
graph_type
=
"dygraph"
)
pattern_block0_block0
=
PaddleGraph
(
parent_layer
=
if_layer21
,
graph_type
=
"dygraph"
)
pattern_block0_block0
.
add_layer
(
pattern_block0_block0
.
add_layer
(
"prim.exception"
,
"prim.exception"
,
inputs
=
{},
inputs
=
{},
outputs
=
[
gen_name
(
15
)],
outputs
=
[
gen_name
(
15
)],
input
=
"Exception"
)
input
=
"Exception"
)
if_layer21
.
add_block
(
pattern_block0_block0
)
if_layer21
.
add_block
(
pattern_block0_block0
)
pattern_block0_block1
=
PaddleGraph
(
if_layer21
,
graph_type
=
"dygraph"
)
pattern_block0_block1
=
PaddleGraph
(
parent_layer
=
if_layer21
,
graph_type
=
"dygraph"
)
if_layer21
.
add_block
(
pattern_block0_block1
)
if_layer21
.
add_block
(
pattern_block0_block1
)
if_layer2
.
add_block
(
pattern_block0
)
if_layer2
.
add_block
(
pattern_block0
)
pattern_block1
=
PaddleGraph
(
if_layer2
,
graph_type
=
"dygraph"
)
pattern_block1
=
PaddleGraph
(
parent_layer
=
if_layer2
,
graph_type
=
"dygraph"
)
if_layer2
.
add_block
(
pattern_block1
)
if_layer2
.
add_block
(
pattern_block1
)
if_layer2
.
inputs
[
"input-0"
]
=
"bn-input-0"
if_layer2
.
inputs
[
"input-0"
]
=
"bn-input-0"
self
.
pattern
.
add_layer
(
self
.
pattern
.
add_layer
(
"paddle.nn.BatchNorm"
,
"paddle.nn.BatchNorm"
,
inputs
=
{
"input"
:
"bn-input-0"
},
inputs
=
{
"input"
:
"bn-input-0"
},
outputs
=
[
gen_name
(
16
),
gen_name
(
17
)],
outputs
=
[
gen_name
(
16
),
gen_name
(
17
)],
is_test
=
True
,
is_test
=
True
)
num_channels
=
160
,
momentum
=
0.1
,
epsilon
=
0.001
)
self
.
pattern
.
build
(
inputs
=
{
"input-0"
:
"bn-input-0"
})
self
.
pattern
.
build
(
inputs
=
{
"input-0"
:
"bn-input-0"
})
def
insert_new_layer
(
self
,
graph
,
parameters
,
matches
):
def
insert_new_layer
(
self
,
graph
,
parameters
,
matches
):
...
@@ -148,9 +145,6 @@ class DygraphBatchNorm2dFuser(FuseBase):
...
@@ -148,9 +145,6 @@ class DygraphBatchNorm2dFuser(FuseBase):
graph
.
layers
[
new_layer_id
]
=
new_layer
graph
.
layers
[
new_layer_id
]
=
new_layer
matches
.
pop
(
new_layer_id
)
matches
.
pop
(
new_layer_id
)
# for layer in matches.values():
# print(layer.outputs)
# print("-------")
def
gen_new_layer
(
self
,
parameters
,
matches
):
def
gen_new_layer
(
self
,
parameters
,
matches
):
layers_id
=
list
(
matches
.
keys
())
layers_id
=
list
(
matches
.
keys
())
...
...
x2paddle/optimizer/fusion/dygraph/fc_fuser.py
浏览文件 @
12dc7fb1
...
@@ -31,14 +31,14 @@ class DygraphFcFuser(FuseBase):
...
@@ -31,14 +31,14 @@ class DygraphFcFuser(FuseBase):
x134 = x133 == 2
x134 = x133 == 2
if x134 :
if x134 :
classifier_6_weight = self.classifier_6_weight
classifier_6_weight = self.classifier_6_weight
x136 =
fluid.layers
.transpose(x=classifier_6_weight, perm=[1, 0])
x136 =
paddle
.transpose(x=classifier_6_weight, perm=[1, 0])
classifier_6_bias = self.classifier_6_bias
classifier_6_bias = self.classifier_6_bias
x137 = paddle.addmm(input=classifier_6_bias, x=x128, y=x136, beta=1, alpha=1)
x137 = paddle.addmm(input=classifier_6_bias, x=x128, y=x136, beta=1, alpha=1)
x135 = x137
x135 = x137
else:
else:
classifier_6_weight = self.classifier_6_weight
classifier_6_weight = self.classifier_6_weight
x138 =
fluid.layers
.transpose(x=classifier_6_weight, perm=[1, 0])
x138 =
paddle
.transpose(x=classifier_6_weight, perm=[1, 0])
x139 =
fluid.layers
.matmul(x=x128, y=x138)
x139 =
paddle
.matmul(x=x128, y=x138)
classifier_6_bias = self.classifier_6_bias
classifier_6_bias = self.classifier_6_bias
x140 = x139 + 1 * classifier_6_bias
x140 = x139 + 1 * classifier_6_bias
x135 = x140
x135 = x140
...
@@ -48,7 +48,7 @@ class DygraphFcFuser(FuseBase):
...
@@ -48,7 +48,7 @@ class DygraphFcFuser(FuseBase):
return
"x"
+
str
(
id
)
return
"x"
+
str
(
id
)
self
.
pattern
.
add_layer
(
self
.
pattern
.
add_layer
(
"
fluid.layers
.shape"
,
"
prim
.shape"
,
inputs
=
{
'input'
:
"fc-input-0"
},
inputs
=
{
'input'
:
"fc-input-0"
},
outputs
=
[
gen_name
(
2
)])
outputs
=
[
gen_name
(
2
)])
self
.
pattern
.
add_layer
(
self
.
pattern
.
add_layer
(
...
@@ -61,22 +61,20 @@ class DygraphFcFuser(FuseBase):
...
@@ -61,22 +61,20 @@ class DygraphFcFuser(FuseBase):
self
.
pattern
.
add_layer
(
"prim.if"
,
{
'input'
:
gen_name
(
3
)},
[
gen_name
(
4
)])
self
.
pattern
.
add_layer
(
"prim.if"
,
{
'input'
:
gen_name
(
3
)},
[
gen_name
(
4
)])
self
.
pattern
.
outputs
.
append
(
gen_name
(
4
))
self
.
pattern
.
outputs
.
append
(
gen_name
(
4
))
if_layer1
=
self
.
pattern
.
layers
[
list
(
self
.
pattern
.
layers
.
keys
())[
-
1
]]
if_layer1
=
self
.
pattern
.
layers
[
list
(
self
.
pattern
.
layers
.
keys
())[
-
1
]]
pattern_block0
=
PaddleGraph
(
if_layer1
,
graph_type
=
"dygraph"
)
pattern_block0
=
PaddleGraph
(
parent_layer
=
if_layer1
,
graph_type
=
"dygraph"
)
pattern_block0
.
add_layer
(
pattern_block0
.
add_layer
(
"
fluid.dygraph.base.to_variable
"
,
"
self.create_parameter
"
,
inputs
=
{},
inputs
=
{},
outputs
=
[
gen_name
(
5
)],
outputs
=
[
gen_name
(
5
)])
value
=
"params[{}]"
.
format
(
string
(
gen_name
(
5
))))
pattern_block0
.
add_layer
(
pattern_block0
.
add_layer
(
"
fluid.layers
.transpose"
,
"
paddle
.transpose"
,
inputs
=
{
"x"
:
gen_name
(
5
)},
inputs
=
{
"x"
:
gen_name
(
5
)},
outputs
=
[
gen_name
(
6
)],
outputs
=
[
gen_name
(
6
)],
perm
=
[
1
,
0
])
perm
=
[
1
,
0
])
pattern_block0
.
add_layer
(
pattern_block0
.
add_layer
(
"
fluid.dygraph.base.to_variable
"
,
"
self.create_parameter
"
,
inputs
=
{},
inputs
=
{},
outputs
=
[
gen_name
(
7
)],
outputs
=
[
gen_name
(
7
)])
value
=
"params[{}]"
.
format
(
string
(
gen_name
(
7
))))
pattern_block0
.
add_layer
(
pattern_block0
.
add_layer
(
"paddle.addmm"
,
"paddle.addmm"
,
inputs
=
{
"input"
:
gen_name
(
7
),
inputs
=
{
"input"
:
gen_name
(
7
),
...
@@ -90,14 +88,13 @@ class DygraphFcFuser(FuseBase):
...
@@ -90,14 +88,13 @@ class DygraphFcFuser(FuseBase):
pattern_block0
.
add_layer
(
pattern_block0
.
add_layer
(
"prim.equal"
,
inputs
=
{
'input'
:
gen_name
(
8
)},
outputs
=
[
gen_name
(
4
)])
"prim.equal"
,
inputs
=
{
'input'
:
gen_name
(
8
)},
outputs
=
[
gen_name
(
4
)])
if_layer1
.
add_block
(
pattern_block0
)
if_layer1
.
add_block
(
pattern_block0
)
pattern_block1
=
PaddleGraph
(
if_layer1
,
graph_type
=
"dygraph"
)
pattern_block1
=
PaddleGraph
(
parent_layer
=
if_layer1
,
graph_type
=
"dygraph"
)
pattern_block1
.
add_layer
(
pattern_block1
.
add_layer
(
"
fluid.dygraph.base.to_variable
"
,
"
self.create_parameter
"
,
inputs
=
{},
inputs
=
{},
outputs
=
[
gen_name
(
5
)],
outputs
=
[
gen_name
(
5
)])
value
=
"params[{}]"
.
format
(
string
(
gen_name
(
5
))))
pattern_block1
.
add_layer
(
pattern_block1
.
add_layer
(
"
fluid.layers
.transpose"
,
"
paddle
.transpose"
,
inputs
=
{
"x"
:
gen_name
(
5
)},
inputs
=
{
"x"
:
gen_name
(
5
)},
outputs
=
[
gen_name
(
6
)],
outputs
=
[
gen_name
(
6
)],
perm
=
[
1
,
0
])
perm
=
[
1
,
0
])
...
@@ -108,10 +105,9 @@ class DygraphFcFuser(FuseBase):
...
@@ -108,10 +105,9 @@ class DygraphFcFuser(FuseBase):
outputs
=
[
gen_name
(
9
)])
outputs
=
[
gen_name
(
9
)])
if_layer1
.
inputs
[
"input-1"
]
=
"fc-input-0"
if_layer1
.
inputs
[
"input-1"
]
=
"fc-input-0"
pattern_block1
.
add_layer
(
pattern_block1
.
add_layer
(
"
fluid.dygraph.base.to_variable
"
,
"
self.create_parameter
"
,
inputs
=
{},
inputs
=
{},
outputs
=
[
gen_name
(
12
)],
outputs
=
[
gen_name
(
12
)])
value
=
"params[{}]"
.
format
(
string
(
gen_name
(
12
))))
pattern_block1
.
add_layer
(
pattern_block1
.
add_layer
(
"prim.add_"
,
"prim.add_"
,
inputs
=
{
"x"
:
gen_name
(
9
),
inputs
=
{
"x"
:
gen_name
(
9
),
...
@@ -137,9 +133,9 @@ class DygraphFcFuser(FuseBase):
...
@@ -137,9 +133,9 @@ class DygraphFcFuser(FuseBase):
layer
=
matches
[
layers_id
[
3
]]
layer
=
matches
[
layers_id
[
3
]]
output_name
=
layer
.
outputs
[
0
]
output_name
=
layer
.
outputs
[
0
]
layer
=
matches
[
layers_id
[
4
]]
layer
=
matches
[
layers_id
[
4
]]
weight_name
=
layer
.
attrs
[
"value"
][
8
:
-
2
]
weight_name
=
layer
.
outputs
[
0
]
layer
=
matches
[
layers_id
[
6
]]
layer
=
matches
[
layers_id
[
6
]]
bias_name
=
layer
.
attrs
[
"value"
][
8
:
-
2
]
bias_name
=
layer
.
outputs
[
0
]
attrs
=
dict
()
attrs
=
dict
()
attrs
[
"in_features"
]
=
parameters
[
weight_name
].
shape
[
1
]
attrs
[
"in_features"
]
=
parameters
[
weight_name
].
shape
[
1
]
attrs
[
"out_features"
]
=
parameters
[
weight_name
].
shape
[
0
]
attrs
[
"out_features"
]
=
parameters
[
weight_name
].
shape
[
0
]
...
...
x2paddle/optimizer/fusion/dygraph/interpolate_bilinear_fuser.py
浏览文件 @
12dc7fb1
此差异已折叠。
点击以展开。
x2paddle/optimizer/optimizer.py
浏览文件 @
12dc7fb1
...
@@ -24,9 +24,12 @@ class GraphOptimizer(object):
...
@@ -24,9 +24,12 @@ class GraphOptimizer(object):
self
.
passes
=
[
"trace_fc_fuse_pass"
]
self
.
passes
=
[
"trace_fc_fuse_pass"
]
else
:
else
:
self
.
passes
=
[
self
.
passes
=
[
"dygraph_constant_fuse_pass"
,
"dygraph_batchnorm2d_fuse_pass"
,
"dygraph_constant_fuse_pass"
,
"dygraph_interpolate_bilinear_fuse_pass"
,
"dygraph_fc_fuse_pass"
,
"dygraph_batchnorm2d_fuse_pass"
,
"dygraph_adaptive_pool2d_fuse_pass"
,
"dygraph_reshape_fuse_pass"
,
"dygraph_interpolate_bilinear_fuse_pass"
,
"dygraph_fc_fuse_pass"
,
"dygraph_adaptive_pool2d_fuse_pass"
,
"dygraph_reshape_fuse_pass"
,
"dygraph_dropout_fuse_pass"
"dygraph_dropout_fuse_pass"
]
]
elif
source_frame
==
"caffe"
:
elif
source_frame
==
"caffe"
:
...
...
x2paddle/optimizer/pattern_matcher.py
浏览文件 @
12dc7fb1
...
@@ -85,7 +85,11 @@ class PatternMatcher(object):
...
@@ -85,7 +85,11 @@ class PatternMatcher(object):
# 判断subgraph中的节点是否被外部图使用到(如若被使用到则无效)
# 判断subgraph中的节点是否被外部图使用到(如若被使用到则无效)
if
layer_id
in
graph
.
edges_out
:
if
layer_id
in
graph
.
edges_out
:
if
pattern_layer_id
not
in
pattern
.
edges_out
:
if
pattern_layer_id
not
in
pattern
.
edges_out
:
if
not
set
(
pattern_layer
.
outputs
).
issubset
(
if
"paddle.nn"
in
layer
.
kernel
and
"functional"
not
in
layer
.
kernel
:
pattern_layer_opt
=
pattern_layer
.
outputs
[
1
:]
else
:
pattern_layer_opt
=
pattern_layer
.
outputs
if
not
set
(
pattern_layer_opt
).
issubset
(
pattern
.
outputs
):
pattern
.
outputs
):
# 若pattern当前layer的输出是pattern的输出,则是正确的
# 若pattern当前layer的输出是pattern的输出,则是正确的
if
pattern_index
==
0
or
is_subblock
:
if
pattern_index
==
0
or
is_subblock
:
...
@@ -97,7 +101,11 @@ class PatternMatcher(object):
...
@@ -97,7 +101,11 @@ class PatternMatcher(object):
if
len
(
graph
.
edges_out
[
layer_id
])
!=
len
(
if
len
(
graph
.
edges_out
[
layer_id
])
!=
len
(
pattern
.
edges_out
[
pattern_layer_id
]):
pattern
.
edges_out
[
pattern_layer_id
]):
# 如果在每个节点edges_in相同的情况下,edges_out数目相同则说明无节点在subgraph外被用到
# 如果在每个节点edges_in相同的情况下,edges_out数目相同则说明无节点在subgraph外被用到
if
not
set
(
pattern_layer
.
outputs
).
issubset
(
if
"paddle.nn"
in
layer
.
kernel
and
"functional"
not
in
layer
.
kernel
:
pattern_layer_opt
=
pattern_layer
.
outputs
[
1
:]
else
:
pattern_layer_opt
=
pattern_layer
.
outputs
if
not
set
(
pattern_layer_opt
).
issubset
(
pattern
.
outputs
):
pattern
.
outputs
):
# 若pattern当前layer的输出是pattern的输出,则是正确的
# 若pattern当前layer的输出是pattern的输出,则是正确的
if
pattern_index
==
0
or
is_subblock
:
if
pattern_index
==
0
or
is_subblock
:
...
@@ -105,6 +113,7 @@ class PatternMatcher(object):
...
@@ -105,6 +113,7 @@ class PatternMatcher(object):
else
:
else
:
subgraph_id2layers
.
pop
(
layer_id
)
subgraph_id2layers
.
pop
(
layer_id
)
continue
continue
# 当为控制流时的处理
# 当为控制流时的处理
if
layer
.
kernel
==
"prim.if"
or
layer
.
kernel
==
"prim.loop"
:
if
layer
.
kernel
==
"prim.if"
or
layer
.
kernel
==
"prim.loop"
:
if
len
(
pattern_layer
.
blocks
)
!=
len
(
layer
.
blocks
):
if
len
(
pattern_layer
.
blocks
)
!=
len
(
layer
.
blocks
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录