Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
X2Paddle
提交
5bcd803c
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看板
提交
5bcd803c
编写于
8月 13, 2020
作者:
S
SunAhong1993
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add models
上级
4c85cdff
变更
5
展开全部
显示空白变更内容
内联
并排
Showing
5 changed file
with
543 addition
and
125 deletion
+543
-125
x2paddle/core/convert_prim.py
x2paddle/core/convert_prim.py
+28
-6
x2paddle/core/program.py
x2paddle/core/program.py
+1
-0
x2paddle/op_mapper/pytorch2paddle/aten.py
x2paddle/op_mapper/pytorch2paddle/aten.py
+397
-90
x2paddle/op_mapper/pytorch2paddle/prim.py
x2paddle/op_mapper/pytorch2paddle/prim.py
+79
-17
x2paddle/op_mapper/pytorch2paddle/pytorch_op_mapper.py
x2paddle/op_mapper/pytorch2paddle/pytorch_op_mapper.py
+38
-12
未找到文件。
x2paddle/core/convert_prim.py
浏览文件 @
5bcd803c
...
...
@@ -73,7 +73,7 @@ def convert_prim(layer, indent=1, init_func=[], forward_func=[]):
elif
layer
.
kernel
==
"prim.min"
:
line
=
"{} = min({})"
.
format
(
layer
.
outputs
[
0
],
list
(
layer
.
inputs
.
values
())[
0
])
elif
layer
.
kernel
==
"prim.add"
:
elif
layer
.
kernel
==
"prim.add
_
"
:
line
=
"{} = {} + {} * {}"
.
format
(
layer
.
outputs
[
0
],
list
(
layer
.
inputs
.
values
())[
0
],
layer
.
attrs
[
"alpha"
],
...
...
@@ -124,11 +124,33 @@ def convert_prim(layer, indent=1, init_func=[], forward_func=[]):
if
list
(
layer
.
inputs
.
values
())[
1
]
is
None
:
item1
=
str
(
layer
.
attrs
[
list
(
layer
.
inputs
.
keys
())[
1
]])
line
=
"{} = {} < {}"
.
format
(
layer
.
outputs
[
0
],
item0
,
item1
)
elif
layer
.
kernel
==
"prim.ne"
:
item0
=
list
(
layer
.
inputs
.
values
())[
0
]
item1
=
list
(
layer
.
inputs
.
values
())[
1
]
line
=
"{} = {} < {}"
.
format
(
layer
.
outputs
[
0
],
item0
,
item1
)
elif
layer
.
kernel
==
"prim.slice"
:
attr
s_str
=
""
for
k
,
v
in
layer
.
attrs
.
items
()
:
attr
s_str
+=
"{}:"
.
format
(
v
)
attrs_str
=
attr
s_str
[:
-
1
]
input
s_str
=
""
for
v
in
list
(
layer
.
inputs
.
values
())[
1
:]
:
input
s_str
+=
"{}:"
.
format
(
v
)
inputs_str
=
input
s_str
[:
-
1
]
line
=
"{} = {}[{}]"
.
format
(
layer
.
outputs
[
0
],
list
(
layer
.
inputs
.
values
())[
0
],
attrs_str
)
list
(
layer
.
inputs
.
values
())[
0
],
inputs_str
)
elif
layer
.
kernel
==
"prim.add"
:
line
=
"{} = {} + {}"
.
format
(
layer
.
outputs
[
0
],
list
(
layer
.
inputs
.
values
())[
0
],
list
(
layer
.
inputs
.
values
())[
1
])
elif
layer
.
kernel
==
"prim.sub"
:
line
=
"{} = {} - {}"
.
format
(
layer
.
outputs
[
0
],
list
(
layer
.
inputs
.
values
())[
0
],
list
(
layer
.
inputs
.
values
())[
1
])
elif
layer
.
kernel
==
"prim.mul"
:
line
=
"{} = {} * {}"
.
format
(
layer
.
outputs
[
0
],
list
(
layer
.
inputs
.
values
())[
0
],
list
(
layer
.
inputs
.
values
())[
1
])
elif
layer
.
kernel
==
"prim.neg"
:
line
=
"{} = -{}"
.
format
(
layer
.
outputs
[
0
],
list
(
layer
.
inputs
.
values
())[
0
])
else
:
print
(
layer
.
kernel
)
line
=
""
forward_func
.
extend
(
gen_codes
([
line
],
indent
=
indent
))
x2paddle/core/program.py
浏览文件 @
5bcd803c
...
...
@@ -297,6 +297,7 @@ class PaddleGraph(object):
for
output_name
in
layer
.
outputs
:
if
not
output_name
.
startswith
(
"x"
):
continue
print
(
layer
.
kernel
)
self
.
outputs
.
append
(
output_name
)
self
.
outputs
=
list
(
set
(
self
.
outputs
))
...
...
x2paddle/op_mapper/pytorch2paddle/aten.py
浏览文件 @
5bcd803c
此差异已折叠。
点击以展开。
x2paddle/op_mapper/pytorch2paddle/prim.py
浏览文件 @
5bcd803c
...
...
@@ -53,6 +53,10 @@ def prim_GetAttr(mapper, graph, node):
node
=
input_node
except
Exception
:
break
if
"."
.
join
(
field_name_list
)
in
mapper
.
pytorch_params
:
mapper
.
pytorch_params
[
output_name
]
=
mapper
.
pytorch_params
[
"."
.
join
(
field_name_list
)]
else
:
part_script
=
mapper
.
script
for
field_name
in
field_name_list
:
if
hasattr
(
part_script
,
field_name
):
...
...
@@ -78,12 +82,13 @@ def prim_ListConstruct(mapper, graph, node):
layer_outputs
=
[
output_name
]
layer_inputs
=
{}
inputs_name
,
inputs_node
=
mapper
.
_get_inputs_name
(
node
)
# 获取当前节点输出的list
current_outputs
=
[
output_name
]
# 处理每个输入
for
i
,
input_name
in
enumerate
(
inputs_name
):
layer_inputs
[
"input{}"
.
format
(
i
)]
=
input_name
# 获取当前节点输入
、输出
的list
# 获取当前节点输入的list
current_inputs
=
list
(
layer_inputs
.
values
())
current_outputs
=
layer_outputs
graph
.
add_layer
(
"prim.list"
,
inputs
=
layer_inputs
,
outputs
=
layer_outputs
)
return
current_inputs
,
current_outputs
...
...
@@ -101,12 +106,13 @@ def prim_RaiseException(mapper, graph, node):
layer_outputs
=
[
output_name
]
layer_inputs
=
{}
inputs_name
,
inputs_node
=
mapper
.
_get_inputs_name
(
node
)
# 获取当前节点输出的list
current_outputs
=
[
output_name
]
# 处理输入0,即%76
mapper
.
_check_input
(
graph
,
inputs_node
[
0
],
inputs_name
[
0
],
layer
_outputs
)
mapper
.
_check_input
(
graph
,
inputs_node
[
0
],
inputs_name
[
0
],
current
_outputs
)
layer_inputs
[
"input"
]
=
inputs_name
[
0
]
# 获取当前节点输入
、输出
的list
# 获取当前节点输入的list
current_inputs
=
list
(
layer_inputs
.
values
())
current_outputs
=
layer_outputs
graph
.
add_layer
(
"prim.exception"
,
inputs
=
layer_inputs
,
outputs
=
layer_outputs
)
...
...
@@ -134,6 +140,9 @@ def prim_Loop(mapper, graph, node):
block
=
list
(
node
.
blocks
())[
0
]
loop_outputs
=
node_outputs
for
i
,
block_input_ivalue
in
enumerate
(
block
.
inputs
()):
if
i
==
0
:
block_input_node_name
=
'_x'
+
str
(
mapper
.
output_index
)
else
:
block_input_node_name
=
'x'
+
str
(
mapper
.
output_index
)
unique_id
=
block_input_ivalue
.
unique
()
if
unique_id
not
in
mapper
.
outputs_info
:
...
...
@@ -226,12 +235,65 @@ def prim_min(mapper, graph, node):
layer_outputs
=
[
output_name
]
layer_inputs
=
{}
inputs_name
,
inputs_node
=
mapper
.
_get_inputs_name
(
node
)
# 获取当前节点输出的list
current_outputs
=
[
output_name
]
# 处理输入0,即%86
mapper
.
_check_input
(
graph
,
inputs_node
[
0
],
inputs_name
[
0
],
layer
_outputs
)
mapper
.
_check_input
(
graph
,
inputs_node
[
0
],
inputs_name
[
0
],
current
_outputs
)
layer_inputs
[
"input"
]
=
inputs_name
[
0
]
# 获取当前节点输入
、输出
的list
# 获取当前节点输入的list
current_inputs
=
list
(
layer_inputs
.
values
())
current_outputs
=
layer_outputs
graph
.
add_layer
(
"prim.min"
,
inputs
=
layer_inputs
,
outputs
=
layer_outputs
)
return
current_inputs
,
current_outputs
def
prim_SetAttr
(
mapper
,
graph
,
node
):
""" 设置attribute信息。
TorchScript示例:
= prim::SetAttr[name="num_batches_tracked"](%260, %277)
参数含义:
%260 (-): 属性名前缀。
%277 (-): 需要设置的值。
"""
output_name
=
mapper
.
_get_outputs_name
(
node
)[
0
]
field_name_list
=
[]
tmp_node
=
node
while
True
:
input_node
=
list
(
tmp_node
.
inputs
())[
0
].
node
()
try
:
field_name_list
.
insert
(
0
,
input_node
.
s
(
'name'
))
tmp_node
=
input_node
except
Exception
:
break
field_name_list
.
append
(
node
.
s
(
'name'
))
inputs_name
,
inputs_node
=
mapper
.
_get_inputs_name
(
node
)
param
=
{
"Tensor"
:
inputs_name
[
1
]}
mapper
.
pytorch_params
[
"."
.
join
(
field_name_list
)]
=
param
return
[],
[
output_name
]
def
prim_shape
(
mapper
,
graph
,
node
):
""" 构造获取shape的PaddleLayer。
TorchScript示例:
%4701 : int[] = prim::shape(%result.1)
参数含义:
%4701 (list): 输出,shape信息。
%result.1 (Tensor): 需要获取shape的值。
"""
output_name
=
mapper
.
_get_outputs_name
(
node
)[
0
]
layer_outputs
=
[
output_name
]
layer_inputs
=
{}
inputs_name
,
inputs_node
=
mapper
.
_get_inputs_name
(
node
)
# 获取当前节点输出的list
current_outputs
=
[
output_name
]
# 处理输入0,即%input.8
mapper
.
_check_input
(
graph
,
inputs_node
[
0
],
inputs_name
[
0
],
current_outputs
)
layer_inputs
[
"input"
]
=
inputs_name
[
0
]
# 获取当前节点输入的list
current_inputs
=
list
(
layer_inputs
.
values
())
graph
.
add_layer
(
"prim.shape"
,
inputs
=
layer_inputs
,
outputs
=
layer_outputs
)
return
current_inputs
,
current_outputs
x2paddle/op_mapper/pytorch2paddle/pytorch_op_mapper.py
浏览文件 @
5bcd803c
...
...
@@ -32,8 +32,28 @@ class PyTorchOpMapper(OpMapper):
self
.
output_index
=
0
self
.
dygraph_name_id
=
{}
# 动态图__init__输出名字中的id,key为kernel类型,value为id
# 转换
self
.
check_op
(
decoder
.
graph
)
self
.
graph
,
_
=
self
.
traverse
(
decoder
.
graph
)
def
check_op
(
self
,
script_graph
):
def
_update_op_list
(
graph
):
for
node
in
graph
.
nodes
():
op_list
.
append
(
node
.
kind
())
for
block
in
node
.
blocks
():
_update_op_list
(
block
)
op_list
=
list
()
_update_op_list
(
script_graph
)
op_list
=
list
(
set
(
op_list
))
unsupported_op_list
=
[]
for
op
in
op_list
:
func_name
=
op
.
replace
(
'::'
,
'_'
)
if
not
(
hasattr
(
prim
,
func_name
)
or
hasattr
(
aten
,
func_name
)):
unsupported_op_list
.
append
(
op
)
if
len
(
unsupported_op_list
)
>
0
:
raise
Exception
(
"The kind {} in model is not supported yet."
.
format
(
unsupported_op_list
))
def
traverse
(
self
,
script_graph
,
parent_layer
=
None
):
# 用于获取graph的输入
def
_update_graph_inputs
(
inputs
,
outputs
):
...
...
@@ -65,9 +85,7 @@ class PyTorchOpMapper(OpMapper):
func
=
getattr
(
aten
,
func_name
)
inputs
,
outputs
=
func
(
self
,
graph
,
node
)
_update_graph_inputs
(
inputs
,
outputs
)
else
:
raise
Exception
(
"The kind {} in model is not supported yet."
.
format
(
node
.
kind
()))
# 转换输出节点
if
hasattr
(
script_graph
,
'returnNode'
):
for
i
,
ivalue
in
enumerate
(
script_graph
.
returnNode
().
inputs
()):
...
...
@@ -97,9 +115,9 @@ class PyTorchOpMapper(OpMapper):
self
.
outputs_info
[
script_unique_id
]
=
output_name
self
.
output_index
+=
1
outputs_name
.
append
(
output_name
)
# if节点没有输出的情况
# if
或loop
节点没有输出的情况
if
len
(
list
(
node
.
outputs
()))
==
0
:
output_name
=
'x'
+
str
(
self
.
output_index
)
output_name
=
'
_
x'
+
str
(
self
.
output_index
)
self
.
output_index
+=
1
outputs_name
.
append
(
output_name
)
return
outputs_name
...
...
@@ -121,12 +139,20 @@ class PyTorchOpMapper(OpMapper):
inputs
=
{},
outputs
=
[
output_name
],
value
=
"params[{}]"
.
format
(
string
(
output_name
)))
else
:
if
isinstance
(
param
,
dict
)
and
"Tensor"
in
param
:
graph
.
add_layer
(
"prim.constant"
,
inputs
=
{},
outputs
=
[
output_name
],
value
=
param
[
"Tensor"
])
else
:
graph
.
add_layer
(
"prim.constant"
,
inputs
=
{},
outputs
=
[
output_name
],
value
=
string
(
param
)
if
isinstance
(
param
,
str
)
else
param
)
value
=
string
(
param
)
if
isinstance
(
param
,
str
)
else
param
)
node_outputs
.
append
(
output_name
)
def
_get_inputs_name
(
self
,
node
):
...
...
@@ -135,9 +161,9 @@ class PyTorchOpMapper(OpMapper):
for
script_input_ivalue
in
node
.
inputs
():
script_input_node
=
script_input_ivalue
.
node
()
script_input_unique_id
=
script_input_ivalue
.
unique
()
input_n
ode_n
ame
=
self
.
outputs_info
[
script_input_unique_id
]
input_name
=
self
.
outputs_info
[
script_input_unique_id
]
inputs_node
.
append
(
script_input_node
)
inputs_name
.
append
(
input_n
ode_n
ame
)
inputs_name
.
append
(
input_name
)
return
inputs_name
,
inputs_node
def
data
(
self
,
graph
,
node
,
uid
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录