Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
PaddleDetection
提交
8fa1d84d
P
PaddleDetection
项目概览
s920243400
/
PaddleDetection
与 Fork 源项目一致
Fork自
PaddlePaddle / PaddleDetection
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleDetection
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
8fa1d84d
编写于
9月 20, 2018
作者:
G
gongweibao
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add
上级
cb8c3798
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
188 addition
and
13 deletion
+188
-13
tools/test_generator.py
tools/test_generator.py
+188
-13
未找到文件。
tools/test_generator.py
浏览文件 @
8fa1d84d
...
@@ -23,6 +23,8 @@ from paddle.fluid.proto import framework_pb2
...
@@ -23,6 +23,8 @@ from paddle.fluid.proto import framework_pb2
from
paddle.fluid.framework
import
OpProtoHolder
,
Variable
from
paddle.fluid.framework
import
OpProtoHolder
,
Variable
from
paddle.fluid.layer_helper
import
LayerHelper
from
paddle.fluid.layer_helper
import
LayerHelper
g_filer_attrs
=
[
'op_role'
,
'op_role_var'
,
'op_namescope'
,
'dtype'
]
def
_convert_
(
name
):
def
_convert_
(
name
):
"""
"""
...
@@ -46,6 +48,8 @@ def _get_inputs(op_type):
...
@@ -46,6 +48,8 @@ def _get_inputs(op_type):
for
ipt
in
op_proto
.
inputs
:
for
ipt
in
op_proto
.
inputs
:
inputs
[
ipt
.
name
]
=
""
inputs
[
ipt
.
name
]
=
""
return
inputs
def
_get_outputs
(
op_type
):
def
_get_outputs
(
op_type
):
op_proto
=
OpProtoHolder
.
instance
().
get_op_proto
(
op_type
)
op_proto
=
OpProtoHolder
.
instance
().
get_op_proto
(
op_type
)
...
@@ -53,26 +57,178 @@ def _get_outputs(op_type):
...
@@ -53,26 +57,178 @@ def _get_outputs(op_type):
for
ipt
in
op_proto
.
outputs
:
for
ipt
in
op_proto
.
outputs
:
outputs
[
ipt
.
name
]
=
""
outputs
[
ipt
.
name
]
=
""
return
outputs
def
get_input_comments
(
op_type
):
return
""
def
_get_attrs
(
op_type
):
op_proto
=
OpProtoHolder
.
instance
().
get_op_proto
(
op_type
)
return
op_proto
.
attrs
def
get_output_comments
(
op_type
):
return
""
def
get_indent_space
(
indent
,
space_num
=
4
):
ret
=
""
for
i
in
range
(
0
,
indent
*
space_num
):
ret
+=
" "
return
ret
def
get_input_comments
(
op_type
,
indent
=
2
):
ret
=
""
inputs
=
_get_inputs
(
op_type
)
for
t
in
inputs
:
ret
+=
get_indent_space
(
2
)
+
"input(${%s_type}): ${%s_comment}
\n
"
%
(
_convert_
(
t
),
_convert_
(
t
))
for
t
in
_get_attrs
(
op_type
):
if
t
.
name
in
g_filer_attrs
:
continue
ret
+=
get_indent_space
(
2
)
+
"input(${%s_type}): ${%s_comment}
\n
"
%
(
_convert_
(
t
.
name
),
_convert_
(
t
.
name
))
return
ret
def
get_output_comments
(
op_type
,
indent
=
2
):
ret
=
""
for
t
in
_get_outputs
(
op_type
):
ret
+=
get_indent_space
(
2
)
+
"output(${%s_type}): ${%s_comment}
\n
"
%
(
_convert_
(
t
),
_convert_
(
t
))
return
ret
def
get_func_args
(
op_type
):
def
get_func_args
(
op_type
):
return
""
ret
=
""
inputs
=
_get_inputs
(
op_type
)
for
t
in
inputs
:
ret
+=
"%s,"
%
_convert_
(
t
)
for
t
in
_get_attrs
(
op_type
):
if
t
.
name
in
g_filer_attrs
:
continue
default
=
re
.
findall
(
"\(.+\, default (.+)\(?\)"
,
t
.
comment
)
if
len
(
default
)
>
0
:
#print(default[0])
ret
+=
"{}={},"
.
format
(
_convert_
(
t
.
name
),
default
[
0
])
continue
ret
+=
"%s=,"
%
_convert_
(
t
.
name
)
return
ret
.
strip
(
','
)
def
get_inputs
(
op_type
):
def
get_inputs
(
op_type
):
ret
=
"inputs={"
inputs
=
_get_inputs
(
op_type
)
for
t
in
inputs
:
ret
+=
"{}={},"
.
format
(
t
,
_convert_
(
t
))
ret
=
ret
.
strip
(
","
)
ret
+=
"}"
if
ret
==
"inputs={}"
:
return
""
return
""
return
ret
"""
def get_input_dtype(op_type):
dtype = None
for ipt in _get_inputs():
name = _convert_(ipt.name)
val = kwargs.pop(name, [])
if not isinstance(val, list) and not isinstance(val, tuple):
val = [val]
if len(val) == 0:
val = [args[0]]
args = args[1:]
for each in val:
if not isinstance(each, Variable):
raise ValueError("input of {0} must be variable".format(
op_type))
if dtype is None:
dtype = each.dtype
elif dtype != each.dtype:
raise ValueError(
"operator {0} must input same dtype. {1} vs {2}".format(
op_type, dtype, each.dtype))
return dtype
"""
def
get_outputs
(
op_type
):
def
get_outputs
(
op_type
):
ret
=
"outputs={"
inputs
=
_get_outputs
(
op_type
)
for
t
in
inputs
:
ret
+=
"{}={},"
.
format
(
t
,
_convert_
(
t
))
ret
=
ret
.
strip
(
","
)
ret
+=
"}"
if
ret
==
"inputs={}"
:
return
""
return
""
return
ret
"""
attr_names = sorted(op.attr_names)
attrs_str = ""
for i in range(0, len(attr_names)):
name = attr_names[i]
attr_type = op.desc.attr_type(name)
if attr_type == core.AttrType.BLOCK:
a = "{name} = block[{value}]".format(
name=name, type=attr_type, value=op.block_attr_id(name))
attrs_str += a
if i != len(attr_names) - 1:
attrs_str += ", "
continue
if attr_type == core.AttrType.BLOCKS:
a = "{name} = blocks{value}".format(
name=name, type=attr_type, value=op.blocks_attr_ids(name))
attrs_str += a
if i != len(attr_names) - 1:
attrs_str += ", "
continue
a = "{name} = {value}".format(
name=name, type=attr_type, value=op.desc.attr(name))
attrs_str += a
if i != len(attr_names) - 1:
attrs_str += ", "
"""
def
get_attrs
(
op_type
):
ret
=
"attrs={"
for
t
in
_get_attrs
(
op_type
):
if
t
.
name
in
g_filer_attrs
:
continue
ret
+=
"%s=%s,"
%
(
t
.
name
,
_convert_
(
t
.
name
))
ret
=
ret
.
strip
(
","
)
ret
+=
"}"
return
ret
def
get_outvars
(
op_type
,
indent
=
1
):
ret
=
""
for
t
in
_get_outputs
(
op_type
):
ret
+=
get_indent_space
(
indent
)
+
"%s = helper.create_tmp_variable(dtype=helper.input_dtype())
\n
"
%
(
_convert_
(
t
))
ret
=
ret
.
strip
(
'
\n
'
)
return
ret
def
get_op_py
(
op_type
):
def
get_op_py
(
op_type
):
input_comments
=
get_input_comments
(
op_type
)
input_comments
=
get_input_comments
(
op_type
)
...
@@ -80,32 +236,51 @@ def get_op_py(op_type):
...
@@ -80,32 +236,51 @@ def get_op_py(op_type):
args
=
get_func_args
(
op_type
)
args
=
get_func_args
(
op_type
)
inputs
=
get_inputs
(
op_type
)
inputs
=
get_inputs
(
op_type
)
outputs
=
get_outputs
(
op_type
)
outputs
=
get_outputs
(
op_type
)
attrs
=
get_attrs
(
op_type
)
out_vars
=
get_outvars
(
op_type
)
code
=
"""
code
=
"""
\
@templatedoc()
@templatedoc()
def {op_type}({args}):
def {op_type}({args}):
\"\"\"
\"\"\"
{op_type}
{op_type}
{comment}
Args:
Args:
{input_comments}
{input_comments}
Returns:
Returns:
{output_comments}
{output_comments}
\"\"\"
\"\"\"
helper = LayerHelper('{op_type}', **locals())
{generated_outvar}
helper.append_op(
helper.append_op(
type='{op_type}',
type='{op_type}',
{inputs},
{inputs},
{outputs})
{outputs},
{attrs})
return out
"""
.
format
(
"""
.
format
(
input_comments
=
input_comments
,
comment
=
"${comment}"
,
input_comments
=
input_comments
.
strip
(
'
\n
'
),
output_comments
=
output_comments
,
output_comments
=
output_comments
,
args
=
args
,
args
=
args
,
generated_outvar
=
out_vars
,
op_type
=
op_type
,
op_type
=
op_type
,
inputs
=
inputs
,
inputs
=
inputs
,
outputs
=
outputs
)
outputs
=
outputs
,
attrs
=
attrs
)
return
code
return
code
print
(
get_op_py
(
"uniform_random_batch_size_like"
))
print
(
get_op_py
(
"uniform_random_batch_size_like"
))
#print(get_op_py("gaussian_random"))
#print(get_op_py("sampling_id"))
#print(get_op_py("gaussian_random_batch_size_like"))
#print(get_op_py("sum"))
#print(get_op_py("slice"))
#print(get_op_py("shape"))
#get_meta("linear_chain_crf")
#get_meta("linear_chain_crf")
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录