Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
5fc5a8ed
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看板
未验证
提交
5fc5a8ed
编写于
3月 04, 2021
作者:
J
Jason
提交者:
GitHub
3月 04, 2021
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #516 from SunAhong1993/develop
fix one caffe bug, one tf bug, one pytorch bug and two onnx bug
上级
58bdbdc5
4a913c7c
变更
9
显示空白变更内容
内联
并排
Showing
9 changed file
with
69 addition
and
14 deletion
+69
-14
x2paddle/core/program.py
x2paddle/core/program.py
+2
-2
x2paddle/op_mapper/dygraph/caffe2paddle/caffe_op_mapper.py
x2paddle/op_mapper/dygraph/caffe2paddle/caffe_op_mapper.py
+1
-1
x2paddle/op_mapper/dygraph/onnx2paddle/opset9/opset.py
x2paddle/op_mapper/dygraph/onnx2paddle/opset9/opset.py
+11
-2
x2paddle/op_mapper/dygraph/pytorch2paddle/aten.py
x2paddle/op_mapper/dygraph/pytorch2paddle/aten.py
+36
-0
x2paddle/op_mapper/static/onnx2paddle/opset9/opset.py
x2paddle/op_mapper/static/onnx2paddle/opset9/opset.py
+12
-3
x2paddle/optimizer/optimizer.py
x2paddle/optimizer/optimizer.py
+1
-1
x2paddle/optimizer/pytorch_code_optimizer/hierachical_tree.py
...ddle/optimizer/pytorch_code_optimizer/hierachical_tree.py
+2
-2
x2paddle/optimizer/pytorch_code_optimizer/layer_code_generator.py
.../optimizer/pytorch_code_optimizer/layer_code_generator.py
+2
-1
x2paddle/optimizer/pytorch_code_optimizer/module_graph.py
x2paddle/optimizer/pytorch_code_optimizer/module_graph.py
+2
-2
未找到文件。
x2paddle/core/program.py
浏览文件 @
5fc5a8ed
...
@@ -508,9 +508,9 @@ class PaddleGraph(object):
...
@@ -508,9 +508,9 @@ class PaddleGraph(object):
],
],
indent
=
0
)
indent
=
0
)
comment_list
=
list
()
comment_list
=
list
()
comment_list
.
append
(
"#
共{}个输入
"
.
format
(
len
(
self
.
inputs_info
)))
comment_list
.
append
(
"#
There are {} inputs.
"
.
format
(
len
(
self
.
inputs_info
)))
for
k
,
v
in
self
.
inputs_info
.
items
():
for
k
,
v
in
self
.
inputs_info
.
items
():
comment_list
.
append
(
"# {}:
形状为{},类型为
{}。"
.
format
(
k
,
v
[
0
],
v
[
1
]))
comment_list
.
append
(
"# {}:
shape-{},type-
{}。"
.
format
(
k
,
v
[
0
],
v
[
1
]))
self
.
run_func
.
extend
(
self
.
run_func
.
extend
(
gen_codes
(
gen_codes
(
comment_list
,
comment_list
,
...
...
x2paddle/op_mapper/dygraph/caffe2paddle/caffe_op_mapper.py
浏览文件 @
5fc5a8ed
...
@@ -571,7 +571,7 @@ class CaffeOpMapper(OpMapper):
...
@@ -571,7 +571,7 @@ class CaffeOpMapper(OpMapper):
if
params
.
HasField
(
'negative_slope'
)
and
params
.
negative_slope
!=
0
:
if
params
.
HasField
(
'negative_slope'
)
and
params
.
negative_slope
!=
0
:
negative_slope
=
float
(
params
.
negative_slope
)
negative_slope
=
float
(
params
.
negative_slope
)
layer_attrs
=
{
'
alpha
'
:
negative_slope
}
layer_attrs
=
{
'
negative_slope
'
:
negative_slope
}
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
"paddle.nn.LeakyReLU"
,
"paddle.nn.LeakyReLU"
,
inputs
=
{
"input"
:
input
.
name
},
inputs
=
{
"input"
:
input
.
name
},
...
...
x2paddle/op_mapper/dygraph/onnx2paddle/opset9/opset.py
浏览文件 @
5fc5a8ed
...
@@ -445,6 +445,8 @@ class OpSet9():
...
@@ -445,6 +445,8 @@ class OpSet9():
layer_outputs
=
[
nn_op_name
,
output_name
]
layer_outputs
=
[
nn_op_name
,
output_name
]
if
is_pads_attr
:
if
is_pads_attr
:
paddings
=
[]
paddings
=
[]
if
len
(
pads
)
==
10
and
sum
(
pads
)
==
0
:
pads
=
pads
[
0
:
6
]
if
len
(
pads
)
in
[
2
,
4
,
6
]:
if
len
(
pads
)
in
[
2
,
4
,
6
]:
if
data_shape
:
if
data_shape
:
assume_pad
|=
data_shape
and
2
*
(
len
(
data_shape
)
-
2
)
==
len
(
pads
)
# NCHW
assume_pad
|=
data_shape
and
2
*
(
len
(
data_shape
)
-
2
)
==
len
(
pads
)
# NCHW
...
@@ -728,11 +730,14 @@ class OpSet9():
...
@@ -728,11 +730,14 @@ class OpSet9():
inputs
=
{
'x'
:
name_trans
,
inputs
=
{
'x'
:
name_trans
,
'index'
:
indices
.
name
},
'index'
:
indices
.
name
},
outputs
=
[
node
.
name
])
outputs
=
[
node
.
name
])
new_perm
=
[
0
]
*
len
(
perm
)
for
i
in
range
(
len
(
perm
)):
new_perm
[
perm
[
i
]]
=
i
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
'paddle.transpose'
,
'paddle.transpose'
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
perm
=
perm
)
perm
=
new_
perm
)
if
len
(
indices_shape
)
<
1
:
if
len
(
indices_shape
)
<
1
:
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
'paddle.squeeze'
,
'paddle.squeeze'
,
...
@@ -809,11 +814,15 @@ class OpSet9():
...
@@ -809,11 +814,15 @@ class OpSet9():
'index'
:
indices_reshape
},
'index'
:
indices_reshape
},
outputs
=
[
node
.
name
])
outputs
=
[
node
.
name
])
input_transpose
=
node
.
name
+
'_transpose'
input_transpose
=
node
.
name
+
'_transpose'
new_perm
=
[
0
]
*
len
(
perm
)
for
i
in
range
(
len
(
perm
)):
new_perm
[
perm
[
i
]]
=
i
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
'paddle.transpose'
,
'paddle.transpose'
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
input_transpose
],
outputs
=
[
input_transpose
],
perm
=
perm
)
perm
=
new_perm
)
perm
=
new_perm
val_x_shape
=
val_x
.
out_shapes
[
0
]
val_x_shape
=
val_x
.
out_shapes
[
0
]
reshaped_shape
=
[]
reshaped_shape
=
[]
for
i
in
perm
:
for
i
in
perm
:
...
...
x2paddle/op_mapper/dygraph/pytorch2paddle/aten.py
浏览文件 @
5fc5a8ed
...
@@ -3353,6 +3353,42 @@ def aten_pow(mapper, graph, node):
...
@@ -3353,6 +3353,42 @@ def aten_pow(mapper, graph, node):
return
current_inputs
,
current_outputs
return
current_inputs
,
current_outputs
def
aten_prelu
(
mapper
,
graph
,
node
):
""" 构造prelu激活的PaddleLayer。
TorchScript示例:
%result.3 : aten::prelu(%input.150, %999)
参数含义:
%result.3 (Tensor): 输出,prelu后的结果。
%input.150 (Tensor): 需要prelu的Tensor。
%999 (Tnsor): 权重。
"""
scope_name
=
mapper
.
normalize_scope_name
(
node
)
op_name
=
name_generator
(
"relu"
,
mapper
.
nn_name2id
)
output_name
=
mapper
.
_get_outputs_name
(
node
)[
0
]
layer_outputs
=
[
op_name
,
output_name
]
layer_inputs
=
{}
inputs_name
,
inputs_node
=
mapper
.
_get_inputs_name
(
node
)
# 获取当前节点输出的list
current_outputs
=
[
output_name
]
# 处理输入0,即%result.150
mapper
.
_check_input
(
graph
,
inputs_node
[
0
],
inputs_name
[
0
],
current_outputs
,
scope_name
)
layer_inputs
[
"x"
]
=
inputs_name
[
0
]
# 处理输入1,即%999
weight
=
mapper
.
pytorch_params
[
inputs_name
[
1
]]
mapper
.
paddle_params
[
op_name
+
"._weight"
]
=
weight
# 获取当前节点输入的list
current_inputs
=
list
(
layer_inputs
.
values
())
graph
.
add_layer
(
"paddle.nn.PReLU"
,
inputs
=
layer_inputs
,
outputs
=
layer_outputs
,
scope_name
=
scope_name
,
num_parameters
=
weight
.
shape
[
0
])
return
current_inputs
,
current_outputs
def
aten_relu
(
mapper
,
graph
,
node
):
def
aten_relu
(
mapper
,
graph
,
node
):
""" 构造ReLU激活的PaddleLayer。
""" 构造ReLU激活的PaddleLayer。
...
...
x2paddle/op_mapper/static/onnx2paddle/opset9/opset.py
浏览文件 @
5fc5a8ed
...
@@ -407,6 +407,8 @@ class OpSet9():
...
@@ -407,6 +407,8 @@ class OpSet9():
if
is_pads_attr
:
if
is_pads_attr
:
paddings
=
[]
paddings
=
[]
paddle_op
=
'paddle.nn.functional.pad'
paddle_op
=
'paddle.nn.functional.pad'
if
len
(
pads
)
==
10
and
sum
(
pads
)
==
0
:
pads
=
pads
[
0
:
6
]
if
len
(
pads
)
in
[
2
,
4
,
6
]:
if
len
(
pads
)
in
[
2
,
4
,
6
]:
if
data_shape
:
if
data_shape
:
assume_pad
|=
data_shape
and
2
*
(
len
(
data_shape
)
-
2
)
==
len
(
pads
)
# NCHW
assume_pad
|=
data_shape
and
2
*
(
len
(
data_shape
)
-
2
)
==
len
(
pads
)
# NCHW
...
@@ -424,7 +426,7 @@ class OpSet9():
...
@@ -424,7 +426,7 @@ class OpSet9():
(
2
,
-
1
)).
transpose
().
astype
(
"int32"
)
(
2
,
-
1
)).
transpose
().
astype
(
"int32"
)
paddings
=
np
.
flip
(
paddings
,
axis
=
0
).
flatten
().
tolist
()
paddings
=
np
.
flip
(
paddings
,
axis
=
0
).
flatten
().
tolist
()
layer_attrs
[
'pad'
]
=
paddings
layer_attrs
[
'pad'
]
=
paddings
layer_attrs
[
'data_format'
]
=
data_format
layer_attrs
[
'data_format'
]
=
string
(
data_format
)
else
:
else
:
if
data_shape
:
if
data_shape
:
assume_pad
|=
data_shape
and
2
*
len
(
data_shape
)
==
len
(
pads
)
# NCHW
assume_pad
|=
data_shape
and
2
*
len
(
data_shape
)
==
len
(
pads
)
# NCHW
...
@@ -694,11 +696,14 @@ class OpSet9():
...
@@ -694,11 +696,14 @@ class OpSet9():
inputs
=
{
'x'
:
name_trans
,
inputs
=
{
'x'
:
name_trans
,
'index'
:
indices
.
name
},
'index'
:
indices
.
name
},
outputs
=
[
node
.
name
])
outputs
=
[
node
.
name
])
new_perm
=
[
0
]
*
len
(
perm
)
for
i
in
range
(
len
(
perm
)):
new_perm
[
perm
[
i
]]
=
i
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
'paddle.transpose'
,
'paddle.transpose'
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
node
.
name
],
outputs
=
[
node
.
name
],
perm
=
perm
)
perm
=
new_
perm
)
if
len
(
indices_shape
)
<
1
:
if
len
(
indices_shape
)
<
1
:
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
'paddle.squeeze'
,
'paddle.squeeze'
,
...
@@ -770,11 +775,15 @@ class OpSet9():
...
@@ -770,11 +775,15 @@ class OpSet9():
'index'
:
indices_reshape
},
'index'
:
indices_reshape
},
outputs
=
[
node
.
name
])
outputs
=
[
node
.
name
])
input_transpose
=
node
.
name
+
'_transpose'
input_transpose
=
node
.
name
+
'_transpose'
new_perm
=
[
0
]
*
len
(
perm
)
for
i
in
range
(
len
(
perm
)):
new_perm
[
perm
[
i
]]
=
i
self
.
paddle_graph
.
add_layer
(
self
.
paddle_graph
.
add_layer
(
'paddle.transpose'
,
'paddle.transpose'
,
inputs
=
{
"x"
:
node
.
name
},
inputs
=
{
"x"
:
node
.
name
},
outputs
=
[
input_transpose
],
outputs
=
[
input_transpose
],
perm
=
perm
)
perm
=
new_perm
)
perm
=
new_perm
val_x_shape
=
val_x
.
out_shapes
[
0
]
val_x_shape
=
val_x
.
out_shapes
[
0
]
reshaped_shape
=
[]
reshaped_shape
=
[]
for
i
in
perm
:
for
i
in
perm
:
...
...
x2paddle/optimizer/optimizer.py
浏览文件 @
5fc5a8ed
...
@@ -60,7 +60,7 @@ class GraphOptimizer(object):
...
@@ -60,7 +60,7 @@ class GraphOptimizer(object):
def
optimize
(
self
,
graph
):
def
optimize
(
self
,
graph
):
for
pass_name
in
self
.
passes
:
for
pass_name
in
self
.
passes
:
pass_
=
PassManager
.
lookup
(
pass_name
)()
pass_
=
PassManager
.
lookup
(
pass_name
)()
if
pass_name
.
endswith
(
"_eliminate_pass"
):
if
pass_name
.
endswith
(
"_eliminate_pass"
)
or
pass_name
.
endswith
(
"_conv2d_add_fuse_pass"
)
:
pass_
.
apply
(
graph
)
pass_
.
apply
(
graph
)
else
:
else
:
while
True
:
while
True
:
...
...
x2paddle/optimizer/pytorch_code_optimizer/hierachical_tree.py
浏览文件 @
5fc5a8ed
...
@@ -383,9 +383,9 @@ class HierarchicalTree(Tree):
...
@@ -383,9 +383,9 @@ class HierarchicalTree(Tree):
input_data_name
=
', '
.
join
(
self
.
pd_graph
.
inputs
)
input_data_name
=
', '
.
join
(
self
.
pd_graph
.
inputs
)
run_func_list
=
list
()
run_func_list
=
list
()
run_func_list
.
append
(
"def main({}):"
.
format
(
input_data_name
))
run_func_list
.
append
(
"def main({}):"
.
format
(
input_data_name
))
run_func_list
.
append
(
" #
共{}个输入
"
.
format
(
len
(
self
.
pd_graph
.
inputs_info
)))
run_func_list
.
append
(
" #
There are {} inputs.
"
.
format
(
len
(
self
.
pd_graph
.
inputs_info
)))
for
k
,
v
in
self
.
pd_graph
.
inputs_info
.
items
():
for
k
,
v
in
self
.
pd_graph
.
inputs_info
.
items
():
run_func_list
.
append
(
" # {}:
形状为{},类型为
{}。"
.
format
(
k
,
v
[
0
],
v
[
1
]))
run_func_list
.
append
(
" # {}:
shape-{},type-
{}。"
.
format
(
k
,
v
[
0
],
v
[
1
]))
run_func_list
.
extend
(
run_func_list
.
extend
(
[
" paddle.disable_static()"
,
[
" paddle.disable_static()"
,
" params = paddle.load('{}/model.pdparams')"
.
format
(
osp
.
abspath
(
save_dir
)),
" params = paddle.load('{}/model.pdparams')"
.
format
(
osp
.
abspath
(
save_dir
)),
...
...
x2paddle/optimizer/pytorch_code_optimizer/layer_code_generator.py
浏览文件 @
5fc5a8ed
...
@@ -27,6 +27,7 @@ NN_KERNEL_NAME = {"paddle.nn.BatchNorm": "bn",
...
@@ -27,6 +27,7 @@ NN_KERNEL_NAME = {"paddle.nn.BatchNorm": "bn",
"paddle.nn.Linear"
:
"linear"
,
"paddle.nn.Linear"
:
"linear"
,
"paddle.nn.Conv2DTranspose"
:
"conv"
,
"paddle.nn.Conv2DTranspose"
:
"conv"
,
"paddle.nn.LSTM"
:
"lstm"
,
"paddle.nn.LSTM"
:
"lstm"
,
"paddle.nn.PReLU"
:
"prelu"
,
"paddle.nn.ReLU"
:
"relu"
,
"paddle.nn.ReLU"
:
"relu"
,
"paddle.nn.ReLU6"
:
"relu"
,
"paddle.nn.ReLU6"
:
"relu"
,
"paddle.nn.Softmax"
:
"softmax"
,
"paddle.nn.Softmax"
:
"softmax"
,
...
@@ -41,7 +42,7 @@ NN_KERNEL_NAME = {"paddle.nn.BatchNorm": "bn",
...
@@ -41,7 +42,7 @@ NN_KERNEL_NAME = {"paddle.nn.BatchNorm": "bn",
"paddle.nn.GELU"
:
"gelu"
,
"paddle.nn.GELU"
:
"gelu"
,
"paddle.nn.Hardtanh"
:
"tanh"
,
"paddle.nn.Hardtanh"
:
"tanh"
,
"paddle.nn.LeakyReLU"
:
"leakly_relu"
}
"paddle.nn.LeakyReLU"
:
"leakly_relu"
}
NN_KERNEL_WITH_PARAMS
=
list
(
NN_KERNEL_NAME
.
keys
())[:
7
]
NN_KERNEL_WITH_PARAMS
=
list
(
NN_KERNEL_NAME
.
keys
())[:
8
]
def
rename_layers
(
layers
,
param_tree
=
None
,
is_rename_module
=
False
):
def
rename_layers
(
layers
,
param_tree
=
None
,
is_rename_module
=
False
):
""" 对子模块的输入输出等进行重命名。
""" 对子模块的输入输出等进行重命名。
...
...
x2paddle/optimizer/pytorch_code_optimizer/module_graph.py
浏览文件 @
5fc5a8ed
...
@@ -349,9 +349,9 @@ class ModuleGraph(object):
...
@@ -349,9 +349,9 @@ class ModuleGraph(object):
input_data_name
=
', '
.
join
(
self
.
pd_graph
.
inputs
)
input_data_name
=
', '
.
join
(
self
.
pd_graph
.
inputs
)
run_func_list
=
list
()
run_func_list
=
list
()
run_func_list
.
append
(
"def main({}):"
.
format
(
input_data_name
))
run_func_list
.
append
(
"def main({}):"
.
format
(
input_data_name
))
run_func_list
.
append
(
" #
共{}个输入
"
.
format
(
len
(
self
.
pd_graph
.
inputs_info
)))
run_func_list
.
append
(
" #
There are {} inputs.
"
.
format
(
len
(
self
.
pd_graph
.
inputs_info
)))
for
k
,
v
in
self
.
pd_graph
.
inputs_info
.
items
():
for
k
,
v
in
self
.
pd_graph
.
inputs_info
.
items
():
run_func_list
.
append
(
" # {}:
形状为{},类型为{}。
"
.
format
(
k
,
v
[
0
],
v
[
1
]))
run_func_list
.
append
(
" # {}:
shape-{},type-{}.
"
.
format
(
k
,
v
[
0
],
v
[
1
]))
run_func_list
.
extend
(
run_func_list
.
extend
(
[
" paddle.disable_static()"
,
[
" paddle.disable_static()"
,
" params = paddle.load('{}/model.pdparams')"
.
format
(
osp
.
abspath
(
save_dir
)),
" params = paddle.load('{}/model.pdparams')"
.
format
(
osp
.
abspath
(
save_dir
)),
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录