Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
aed681d3
MegEngine
项目概览
MegEngine 天元
/
MegEngine
8 个月 前同步成功
通知
392
Star
4702
Fork
582
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
MegEngine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
aed681d3
编写于
3月 31, 2021
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(imperative/utils): optimize the naming rules
GitOrigin-RevId: 329bac640aa6e2e3c981aa294a361684b982892e
上级
c6bbc478
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
74 addition
and
50 deletion
+74
-50
imperative/python/megengine/jit/tracing.py
imperative/python/megengine/jit/tracing.py
+6
-8
imperative/python/megengine/module/module.py
imperative/python/megengine/module/module.py
+4
-4
imperative/python/megengine/tensor.py
imperative/python/megengine/tensor.py
+2
-2
imperative/python/megengine/utils/naming.py
imperative/python/megengine/utils/naming.py
+44
-30
imperative/python/test/unit/jit/test_tracing.py
imperative/python/test/unit/jit/test_tracing.py
+2
-2
imperative/python/test/unit/utils/test_dump_naming.py
imperative/python/test/unit/utils/test_dump_naming.py
+15
-3
src/opr/impl/io.cpp
src/opr/impl/io.cpp
+1
-1
未找到文件。
imperative/python/megengine/jit/tracing.py
浏览文件 @
aed681d3
...
...
@@ -40,7 +40,7 @@ from ..core.ops.builtin import BackwardGraph, OpDef
from
..core.ops.special
import
Const
from
..core.tensor
import
megbrain_graph
as
G
from
..core.tensor.utils
import
setscalar
from
..utils.naming
import
auto_n
aming
from
..utils.naming
import
AutoN
aming
from
.sublinear_memory_config
import
SublinearMemoryConfig
...
...
@@ -297,9 +297,7 @@ class trace:
h
=
getattr
(
x
,
"_mixin_handle"
,
-
1
)
if
h
<
0
or
(
not
self
.
_capture_as_const
and
self
.
_tinfo
[
h
].
exported
):
h
,
info
=
self
.
_new_handle
()
name
=
(
auto_naming
.
get_scope
()
+
"."
+
(
x
.
c_name
if
x
.
c_name
else
x
.
_name
)
)
name
=
AutoNaming
.
gen_name
(
x
)
info
.
name
=
name
info
.
external
=
True
info
.
device
=
x
.
device
...
...
@@ -845,17 +843,17 @@ class trace:
ivars
.
append
(
h2v
[
h
])
ovars
=
G
.
apply_normal_varnode
(
op
,
*
ivars
)
auto_n
aming
.
record_opnode
(
ovars
[
0
].
op
)
AutoN
aming
.
record_opnode
(
ovars
[
0
].
op
)
assert
len
(
ovars
)
==
len
(
ohandles
)
h2v
.
update
(
zip
(
ohandles
,
ovars
))
for
i
in
ohandles
:
name
=
auto_n
aming
.
get_var_name
(
i
)
name
=
AutoN
aming
.
get_var_name
(
i
)
if
name
is
not
None
:
h2v
[
i
].
name
=
name
auto_n
aming
.
remove_duplicate_names
()
AutoN
aming
.
remove_duplicate_names
()
dest_vars
=
[]
for
i
,
h
in
enumerate
(
self
.
_output_bindings
):
...
...
@@ -1173,7 +1171,7 @@ def apply_const_compiled_mode(value, dtype, device, is_const, no_cache, name):
def
apply_with_tracing
(
op
:
OpDef
,
*
args
:
RawTensor
):
if
hasattr
(
op
,
"scope"
):
op
.
scope
=
auto_n
aming
.
get_scope
()
op
.
scope
=
AutoN
aming
.
get_scope
()
if
active_trace
.
_symbolic
:
outputs
=
apply_symbolic_mode
(
op
,
*
args
)
else
:
...
...
imperative/python/megengine/module/module.py
浏览文件 @
aed681d3
...
...
@@ -16,7 +16,7 @@ from ..logger import get_logger
from
..tensor
import
Parameter
,
Tensor
from
..utils.deprecation
import
deprecated
from
..utils.hook
import
HookHandler
from
..utils.naming
import
auto_n
aming
from
..utils.naming
import
AutoN
aming
logger
=
get_logger
(
__name__
)
...
...
@@ -111,7 +111,7 @@ class Module(metaclass=ABCMeta):
self
.
_forward_hooks
=
OrderedDict
()
# used for profiler and automatic naming
self
.
_name
=
"{anonymous}"
self
.
_name
=
None
@
abstractmethod
def
forward
(
self
,
inputs
):
...
...
@@ -137,7 +137,7 @@ class Module(metaclass=ABCMeta):
return
HookHandler
(
self
.
_forward_hooks
,
hook
)
def
__call__
(
self
,
*
inputs
,
**
kwargs
):
auto_n
aming
.
push_scope
(
self
.
name
if
self
.
name
is
not
None
else
self
.
_name
)
AutoN
aming
.
push_scope
(
self
.
name
if
self
.
name
is
not
None
else
self
.
_name
)
for
hook
in
self
.
_forward_pre_hooks
.
values
():
modified_inputs
=
hook
(
self
,
inputs
)
if
modified_inputs
is
not
None
:
...
...
@@ -151,7 +151,7 @@ class Module(metaclass=ABCMeta):
modified_outputs
=
hook
(
self
,
inputs
,
outputs
)
if
modified_outputs
is
not
None
:
outputs
=
modified_outputs
auto_n
aming
.
pop_scope
()
AutoN
aming
.
pop_scope
()
return
outputs
def
_flatten
(
...
...
imperative/python/megengine/tensor.py
浏览文件 @
aed681d3
...
...
@@ -20,7 +20,7 @@ from .core.tensor.array_method import ArrayMethodMixin
from
.device
import
_valid_device
,
get_default_device
from
.logger
import
get_logger
from
.utils.deprecation
import
deprecated
from
.utils.naming
import
auto_n
aming
from
.utils.naming
import
AutoN
aming
logger
=
get_logger
(
__name__
)
...
...
@@ -168,7 +168,7 @@ class Tensor(_Tensor, ArrayMethodMixin):
@
name
.
setter
def
name
(
self
,
name
):
self
.
c_name
=
name
auto_n
aming
.
record_var_name
(
self
.
_mixin_handle
,
name
)
AutoN
aming
.
record_var_name
(
self
.
_mixin_handle
,
name
)
@
deprecated
(
version
=
"1.0"
,
reason
=
"no need to reuse an existing tensor since 1.0"
)
def
set_value
(
self
,
value
):
...
...
imperative/python/megengine/utils/naming.py
浏览文件 @
aed681d3
...
...
@@ -15,40 +15,57 @@ class AutoNaming:
renamed by the user.
"""
def
__init__
(
self
):
self
.
scope
s
=
[]
self
.
c_ops
=
[]
self
.
name2op
s
=
{}
self
.
handle2names
=
{
}
scopes
=
[]
c_op
s
=
[]
name2ops
=
{}
handle2name
s
=
{}
__cls_attributes__
=
{
"scopes"
,
"c_ops"
,
"name2ops"
,
"handle2names"
}
def
clear
(
self
):
for
var
in
vars
(
self
).
values
():
var
.
clear
()
@
classmethod
def
clear
(
cls
):
for
attr
in
cls
.
__cls_attributes__
:
getattr
(
cls
,
attr
).
clear
()
def
push_scope
(
self
,
scope
):
push_scope
(
scope
)
self
.
scopes
.
append
(
scope
)
@
classmethod
def
push_scope
(
cls
,
scope
):
if
scope
is
not
None
:
push_scope
(
scope
)
cls
.
scopes
.
append
(
scope
)
def
pop_scope
(
self
):
scope
=
self
.
scopes
.
pop
()
pop_scope
(
scope
)
@
classmethod
def
pop_scope
(
cls
):
scope
=
cls
.
scopes
.
pop
()
if
scope
is
not
None
:
pop_scope
(
scope
)
def
get_scope
(
self
):
return
"."
.
join
(
self
.
scopes
)
@
classmethod
def
get_scope
(
cls
):
return
"."
.
join
(
s
for
s
in
cls
.
scopes
if
s
is
not
None
)
def
record_var_name
(
self
,
handle
,
name
):
self
.
handle2names
[
handle
]
=
name
@
classmethod
def
gen_name
(
cls
,
x
)
->
str
:
scope
=
cls
.
get_scope
()
name
=
x
.
c_name
if
x
.
c_name
else
x
.
_name
return
scope
+
"."
+
name
if
len
(
scope
)
else
name
def
get_var_name
(
self
,
handle
):
return
self
.
handle2names
.
pop
(
handle
,
None
)
@
classmethod
def
record_var_name
(
cls
,
handle
,
name
):
cls
.
handle2names
[
handle
]
=
name
def
record_opnode
(
self
,
op
):
ops
=
self
.
name2ops
.
get
(
op
.
name
,
[])
ops
.
append
(
op
)
self
.
name2ops
[
op
.
name
]
=
ops
@
classmethod
def
get_var_name
(
cls
,
handle
):
return
cls
.
handle2names
.
pop
(
handle
,
None
)
def
remove_duplicate_names
(
self
):
for
key
,
ops
in
self
.
name2ops
.
items
():
@
classmethod
def
record_opnode
(
cls
,
op
):
ops
=
cls
.
name2ops
.
get
(
op
.
name
,
[])
if
op
not
in
ops
:
ops
.
append
(
op
)
cls
.
name2ops
[
op
.
name
]
=
ops
@
classmethod
def
remove_duplicate_names
(
cls
):
for
key
,
ops
in
cls
.
name2ops
.
items
():
if
len
(
ops
)
==
1
:
continue
for
i
,
op
in
enumerate
(
ops
):
...
...
@@ -57,7 +74,4 @@ class AutoNaming:
continue
for
var
in
op
.
outputs
:
var
.
name
=
var
.
name
.
replace
(
key
,
op
.
name
)
self
.
name2ops
.
clear
()
auto_naming
=
AutoNaming
()
cls
.
name2ops
.
clear
()
imperative/python/test/unit/jit/test_tracing.py
浏览文件 @
aed681d3
...
...
@@ -28,7 +28,7 @@ from megengine.functional import exp, log
from
megengine.jit
import
exclude_from_trace
,
trace
from
megengine.module
import
Module
from
megengine.random
import
normal
,
uniform
from
megengine.utils.naming
import
auto_n
aming
from
megengine.utils.naming
import
AutoN
aming
@
pytest
.
mark
.
parametrize
(
"trace_mode"
,
[
False
,
True
])
...
...
@@ -141,7 +141,7 @@ def test_dump():
return
a
+
b
# prevent from remaining scope from exception test
auto_n
aming
.
clear
()
AutoN
aming
.
clear
()
a
=
tensor
([
2
])
b
=
tensor
([
4
])
y
=
f
(
a
,
b
).
numpy
()
...
...
imperative/python/test/unit/utils/test_dump_naming.py
浏览文件 @
aed681d3
...
...
@@ -18,11 +18,11 @@ from megengine import Parameter, Tensor
from
megengine.core.tensor
import
megbrain_graph
as
G
from
megengine.jit.tracing
import
trace
from
megengine.quantization.quantize
import
quantize
,
quantize_qat
from
megengine.utils.naming
import
auto_n
aming
from
megengine.utils.naming
import
AutoN
aming
def
_dump_and_load
(
func
,
symbolic
,
keep_opr_name
=
True
):
auto_n
aming
.
clear
()
AutoN
aming
.
clear
()
func
=
trace
(
func
,
symbolic
=
symbolic
,
capture_as_const
=
True
)
x
=
Tensor
(
np
.
ones
(
shape
=
(
2
,
3
)))
func
(
x
).
numpy
()
...
...
@@ -103,6 +103,18 @@ def test_without_module(symbolic):
assert
op
.
name
==
"MUL"
@
pytest
.
mark
.
parametrize
(
"symbolic"
,
[
False
,
True
])
def
test_ignore_top_module
(
symbolic
):
class
Simple
(
M
.
Module
):
def
forward
(
self
,
x
):
return
x
+
x
m
=
Simple
()
op
=
_dump_and_load
(
m
,
symbolic
)[
-
1
]
assert
op
.
name
==
"ADD"
assert
op
.
outputs
[
0
].
name
==
"ADD"
@
pytest
.
mark
.
parametrize
(
"symbolic"
,
[
False
,
True
])
def
test_with_submodule
(
symbolic
):
class
Simple
(
M
.
Module
):
...
...
@@ -196,7 +208,7 @@ def test_not_keep_opr_name():
return
2
*
x
op
=
_dump_and_load
(
f
,
True
,
False
)[
-
1
]
assert
op
.
name
==
"MUL(x,
2
[2])[4]"
assert
op
.
name
==
"MUL(x,
const<2>
[2])[4]"
@
pytest
.
mark
.
parametrize
(
"symbolic"
,
[
False
,
True
])
...
...
src/opr/impl/io.cpp
浏览文件 @
aed681d3
...
...
@@ -419,7 +419,7 @@ void ImmutableTensor::Value::setup(CompNode cn, const HostTensorND &val) {
if
(
one_elem
(
val
.
shape
()))
{
float
v
;
static_cast_dtype
(
&
v
,
val
.
dtype
(),
val
.
raw_ptr
());
m_summary
=
ssprintf
(
"
%.3g
"
,
v
);
m_summary
=
ssprintf
(
"
const<%.3g>
"
,
v
);
if
(
val
.
shape
().
ndim
!=
1
)
{
m_summary
+=
val
.
shape
().
to_string
();
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录