Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
556e0222
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
404
Star
4705
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看板
提交
556e0222
编写于
12月 29, 2020
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(mge/trace): remove apply on varnode
GitOrigin-RevId: 0185244c1f957a90b4fd7b81d0e0a0a73ea552c2
上级
243a05b4
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
35 addition
and
54 deletion
+35
-54
imperative/python/megengine/core/tensor/megbrain_graph.py
imperative/python/megengine/core/tensor/megbrain_graph.py
+2
-2
imperative/python/megengine/jit/tracing.py
imperative/python/megengine/jit/tracing.py
+14
-16
imperative/python/src/tensor.cpp
imperative/python/src/tensor.cpp
+5
-0
imperative/python/src/tensor.h
imperative/python/src/tensor.h
+1
-0
imperative/python/src/trace.cpp
imperative/python/src/trace.cpp
+0
-25
imperative/python/test/unit/core/test_dtype_quant.py
imperative/python/test/unit/core/test_dtype_quant.py
+2
-2
imperative/python/test/unit/core/test_megbrain_graph.py
imperative/python/test/unit/core/test_megbrain_graph.py
+2
-2
imperative/python/test/unit/test_cgtools.py
imperative/python/test/unit/test_cgtools.py
+7
-7
imperative/python/test/unit/test_tracing.py
imperative/python/test/unit/test_tracing.py
+2
-0
未找到文件。
imperative/python/megengine/core/tensor/megbrain_graph.py
浏览文件 @
556e0222
...
...
@@ -437,7 +437,7 @@ def _unwrap(x):
return
x
def
apply_normal_
op
(
op
:
OpDef
,
*
args
:
VarNode
):
def
apply_normal_
varnode
(
op
:
OpDef
,
*
args
:
VarNode
):
outputs
=
_imperative_rt
.
invoke_op
(
op
,
_unwrap
(
args
))
return
_wrap
(
outputs
)
...
...
@@ -447,7 +447,7 @@ def apply_backward_varnode(op: BackwardGraph, *args: VarNode):
graph
=
args
[
0
].
graph
outputs
=
op
.
interpret
(
op
,
lambda
op
,
args
:
apply_normal_
op
(
op
,
*
args
),
lambda
op
,
args
:
apply_normal_
varnode
(
op
,
*
args
),
graph
.
_make_const_for_backward
,
args
,
)
...
...
imperative/python/megengine/jit/tracing.py
浏览文件 @
556e0222
...
...
@@ -41,7 +41,7 @@ from ..core._imperative_rt.ops import (
)
from
..core._trace_option
import
set_symbolic_shape
from
..core._wrap
import
device
as
as_device
from
..core.ops.builtin
import
OpDef
from
..core.ops.builtin
import
BackwardGraph
,
OpDef
from
..core.ops.special
import
Const
from
..core.tensor
import
megbrain_graph
as
G
from
.sublinear_memory_config
import
SublinearMemoryConfig
...
...
@@ -372,6 +372,7 @@ class trace:
lazy_eval_graph
()
for
r
,
x
in
zip
(
readers
,
lazy_eval_tensors
):
x
().
_handle
=
RawTensor
(
r
.
op
.
get_value
()).
_handle
x
().
_reset_varnode
()
@
contextlib
.
contextmanager
def
_setup
(
self
):
...
...
@@ -580,9 +581,11 @@ class trace:
ivars
.
append
(
info
.
varnode
)
ivars
=
[
RawTensor
(
ivar
)
for
ivar
in
ivars
]
ovars
=
apply
(
op
,
*
ivars
)
ovars
=
[
x
.
_varnode
for
x
in
ovars
]
if
isinstance
(
op
,
BackwardGraph
):
ovars
=
G
.
apply_backward_varnode
(
op
,
*
ivars
)
else
:
ovars
=
G
.
apply_normal_varnode
(
op
,
*
ivars
)
if
require_links
and
len
(
ovars
)
>
0
:
io_links
=
(
ovars
[
0
],)
assert
len
(
ovars
)
==
len
(
ohandles
)
...
...
@@ -768,11 +771,10 @@ class trace:
info
.
bound_data
.
numpy
(),
dtype
=
info
.
dtype
,
device
=
dumped_device
)
ivars
.
append
(
h2v
[
h
])
ivars
=
[
RawTensor
(
ivar
)
for
ivar
in
ivars
]
ovars
=
apply
(
op
,
*
ivars
)
ovars
=
[
x
.
_varnode
for
x
in
ovars
]
ovars
=
G
.
apply_normal_varnode
(
op
,
*
ivars
)
assert
len
(
ovars
)
==
len
(
ohandles
)
h2v
.
update
(
zip
(
ohandles
,
ovars
))
unset_tracing
()
dest_vars
=
[]
for
i
,
h
in
enumerate
(
self
.
_output_bindings
):
...
...
@@ -781,7 +783,6 @@ class trace:
v
.
name
=
output_names
[
i
]
dest_vars
.
append
(
v
)
dest_vars
=
[
G
.
VarNode
(
var
)
for
var
in
dest_vars
]
if
optimize_for_inference
:
dest_vars
=
G
.
optimize_for_inference
(
dest_vars
,
**
kwargs
)
...
...
@@ -1007,7 +1008,6 @@ def assign_raw_tensor(lhs, rhs):
lhs
.
__init__
(
rhs
)
# this hook turns RawTensor into LazyEvalTensor(varnode)
def
apply_symbolic_mode
(
op
:
OpDef
,
*
args
:
RawTensor
):
graph
=
active_trace
.
_lazy_eval_graph
ivars
=
[]
...
...
@@ -1038,13 +1038,11 @@ def apply_symbolic_mode(op: OpDef, *args: RawTensor):
ivars
[
0
]
=
opnode
.
outputs
[
0
]
active_trace
.
_lazy_eval_links
=
(
ivars
[
0
],)
ivars
=
[
RawTensor
(
ivar
.
_node
)
if
hasattr
(
ivar
,
"_node"
)
else
RawTensor
(
ivar
)
for
ivar
in
ivars
]
unset_symbolic
()
outputs
=
apply
(
op
,
*
ivars
)
set_symbolic
()
if
isinstance
(
op
,
BackwardGraph
):
ovars
=
G
.
apply_backward_varnode
(
op
,
*
ivars
)
else
:
ovars
=
G
.
apply_normal_varnode
(
op
,
*
ivars
)
outputs
=
[
RawTensor
(
o
)
for
o
in
ovars
]
if
require_links
:
active_trace
.
_lazy_eval_links
=
(
outputs
[
0
].
_varnode
,)
...
...
imperative/python/src/tensor.cpp
浏览文件 @
556e0222
...
...
@@ -392,6 +392,10 @@ void TensorWrapper::reset(PyObject* tensor) {
m_tensor
=
t
->
m_tensor
;
}
void
TensorWrapper
::
reset_varnode
()
{
m_tensor
->
m_var
=
nullptr
;
}
PyObject
*
TensorWrapper
::
detach
()
{
PyObject
*
self
=
wrap_t
::
pycast
(
this
);
PyTypeObject
*
pytype
=
self
->
ob_type
;
...
...
@@ -687,6 +691,7 @@ void init_tensor(py::module m) {
.
def
<&
TensorWrapper
::
_swap_out
>
(
"_swap_out"
)
.
def
<&
TensorWrapper
::
_swap_in
>
(
"_swap_in"
)
.
def
<&
TensorWrapper
::
_drop
>
(
"_drop"
)
.
def
<&
TensorWrapper
::
reset_varnode
>
(
"_reset_varnode"
)
.
def_getset
<&
TensorWrapper
::
varnode
>
(
"_varnode"
)
.
def_getset
<&
TensorWrapper
::
data_read
,
&
TensorWrapper
::
set_data_read
>
(
"data_read"
)
.
def_getset
<&
TensorWrapper
::
value_read
,
&
TensorWrapper
::
set_value_read
>
(
"value_read"
)
...
...
imperative/python/src/tensor.h
浏览文件 @
556e0222
...
...
@@ -155,6 +155,7 @@ struct TensorWrapper {
void
_swap_out
();
void
_drop
();
PyObject
*
varnode
();
void
reset_varnode
();
PyObject
*
handle
();
void
set_handle
(
PyObject
*
);
...
...
imperative/python/src/trace.cpp
浏览文件 @
556e0222
...
...
@@ -17,30 +17,9 @@ namespace py = pybind11;
namespace
mgb
::
imperative
::
python
{
apply_result_t
apply_tensor_on_var_node
(
ApplyContext
&
ctx
)
{
apply_result_t
outputs
;
cg
::
VarNodeArray
vinputs
(
ctx
.
nargs
);
for
(
size_t
i
=
0
;
i
<
ctx
.
nargs
;
i
++
)
{
vinputs
[
i
]
=
ctx
.
args
[
i
]
->
m_var
;
}
auto
ovars
=
OpDef
::
apply_on_var_node
(
*
ctx
.
op
,
vinputs
);
for
(
size_t
i
=
0
;
i
<
ovars
.
size
();
i
++
)
{
outputs
.
emplace_back
(
std
::
make_shared
<
Tensor
>
(
ovars
[
i
]));
}
return
outputs
;
}
apply_result_t
apply_trace
(
ApplyContext
&
ctx
)
{
apply_result_t
outputs
;
bool
run_apply_on_var_node
=
false
;
for
(
size_t
i
=
0
;
i
<
ctx
.
nargs
;
i
++
)
{
run_apply_on_var_node
|=
((
ctx
.
args
[
i
]
->
m_handle
.
get
()
==
nullptr
)
&
(
ctx
.
args
[
i
]
->
m_var
!=
nullptr
));
}
if
(
ctx
.
backward
)
{
// reach here when symbolic=True or compiled=True
// call megbrain_graph.py apply(BackwardGraph, *args)
...
...
@@ -63,10 +42,6 @@ apply_result_t apply_trace(ApplyContext& ctx) {
return
outputs
;
}
if
(
run_apply_on_var_node
&&
!
is_symbolic
)
{
return
apply_tensor_on_var_node
(
ctx
);
}
py
::
object
pyf
;
if
(
is_compiled
)
{
// run apply in compiled mode, step 2, 3, etc
...
...
imperative/python/test/unit/core/test_dtype_quant.py
浏览文件 @
556e0222
...
...
@@ -112,7 +112,7 @@ def test_quint8_typecvt():
data
=
np
.
random
.
random
(
shape
).
astype
(
np
.
float32
)
*
5
-
1
def
typecvt
(
x
,
dt
=
None
):
(
y
,)
=
G
.
apply_normal_
op
(
ops
.
TypeCvt
(
dtype
=
dt
),
x
)
(
y
,)
=
G
.
apply_normal_
varnode
(
ops
.
TypeCvt
(
dtype
=
dt
),
x
)
return
y
# convert to quint8
...
...
@@ -193,7 +193,7 @@ def test_quint4_typecvt():
data
=
np
.
random
.
random
(
shape
).
astype
(
np
.
float32
)
*
5
-
1
def
typecvt
(
x
,
dt
=
None
):
(
y
,)
=
G
.
apply_normal_
op
(
ops
.
TypeCvt
(
dtype
=
dt
),
x
)
(
y
,)
=
G
.
apply_normal_
varnode
(
ops
.
TypeCvt
(
dtype
=
dt
),
x
)
return
y
# convert to quint4
...
...
imperative/python/test/unit/core/test_megbrain_graph.py
浏览文件 @
556e0222
...
...
@@ -72,7 +72,7 @@ def test_op():
lambda
:
x
,
device
=
x
.
comp_node
,
dtype
=
x
.
dtype
,
graph
=
g
)
neg
=
Elemwise
(
Elemwise
.
Mode
.
NEGATE
)
v
=
mgb_graph
.
apply_normal_
op
(
neg
,
v
)[
0
]
v
=
mgb_graph
.
apply_normal_
varnode
(
neg
,
v
)[
0
]
y
=
Future
()
v
=
mgb_graph
.
output_callback
(
y
.
set_result
,
v
)
f
=
g
.
compile
(
v
)
...
...
@@ -90,7 +90,7 @@ def test_exception():
g
=
mgb_graph
.
Graph
()
x
,
_
=
mgb_graph
.
input_callback
(
throw_exc
,
device
=
"xpux"
,
dtype
=
"float32"
,
graph
=
g
)
neg
=
Elemwise
(
Elemwise
.
Mode
.
NEGATE
)
y
=
mgb_graph
.
OutputNode
(
mgb_graph
.
apply_normal_
op
(
neg
,
x
)[
0
])
y
=
mgb_graph
.
OutputNode
(
mgb_graph
.
apply_normal_
varnode
(
neg
,
x
)[
0
])
f
=
g
.
compile
(
y
.
outputs
[
0
])
try
:
f
.
execute
()
...
...
imperative/python/test/unit/test_cgtools.py
浏览文件 @
556e0222
...
...
@@ -16,7 +16,7 @@ import megengine.module as M
import
megengine.utils.comp_graph_tools
as
cgtools
from
megengine.core.ops.builtin
import
Elemwise
from
megengine.core.tensor
import
megbrain_graph
as
mgb_graph
from
megengine.core.tensor.megbrain_graph
import
apply_normal_
op
from
megengine.core.tensor.megbrain_graph
import
apply_normal_
varnode
from
megengine.core.tensor.utils
import
astensor1d
from
megengine.jit
import
trace
...
...
@@ -34,9 +34,9 @@ def test_replace_vars():
const
=
g
.
make_const
(
1.234
,
device
=
device
)
add_op
=
Elemwise
(
Elemwise
.
Mode
.
ADD
)
mul_op
=
Elemwise
(
Elemwise
.
Mode
.
MUL
)
a_plus_a
=
apply_normal_
op
(
add_op
,
a
.
outputs
[
0
],
a
.
outputs
[
0
])[
0
]
a_plus_a_mul_const
=
apply_normal_
op
(
mul_op
,
a_plus_a
,
const
)[
0
]
rst
=
apply_normal_
op
(
add_op
,
a_plus_a_mul_const
,
a
.
outputs
[
0
])[
0
]
a_plus_a
=
apply_normal_
varnode
(
add_op
,
a
.
outputs
[
0
],
a
.
outputs
[
0
])[
0
]
a_plus_a_mul_const
=
apply_normal_
varnode
(
mul_op
,
a_plus_a
,
const
)[
0
]
rst
=
apply_normal_
varnode
(
add_op
,
a_plus_a_mul_const
,
a
.
outputs
[
0
])[
0
]
(
new
,)
=
cgtools
.
replace_vars
([
rst
.
_node
],
{
const
.
_node
:
a_plus_a
.
_node
})
out
=
mgb_graph
.
OutputNode
(
mgb_graph
.
VarNode
(
new
))
func
=
g
.
compile
(
out
.
outputs
[
0
])
...
...
@@ -56,10 +56,10 @@ def test_replace_oprs():
const
=
g
.
make_const
(
1.25
,
device
=
device
)
add_op
=
Elemwise
(
Elemwise
.
Mode
.
ADD
)
mul_op
=
Elemwise
(
Elemwise
.
Mode
.
MUL
)
a_plus_a
=
apply_normal_
op
(
add_op
,
a
.
outputs
[
0
],
a
.
outputs
[
0
])[
0
]
a_plus_a
=
apply_normal_
varnode
(
add_op
,
a
.
outputs
[
0
],
a
.
outputs
[
0
])[
0
]
old_opr
=
a_plus_a
.
op
a_plus_a_mul_const
=
apply_normal_
op
(
mul_op
,
a_plus_a
,
const
)[
0
]
a_mul_a
=
apply_normal_
op
(
mul_op
,
a
.
outputs
[
0
],
a
.
outputs
[
0
])[
0
]
a_plus_a_mul_const
=
apply_normal_
varnode
(
mul_op
,
a_plus_a
,
const
)[
0
]
a_mul_a
=
apply_normal_
varnode
(
mul_op
,
a
.
outputs
[
0
],
a
.
outputs
[
0
])[
0
]
new_opr
=
a_mul_a
.
op
(
new
,)
=
cgtools
.
replace_oprs
(
[
a_plus_a_mul_const
.
_node
],
{
old_opr
.
_node
:
new_opr
.
_node
}
...
...
imperative/python/test/unit/test_tracing.py
浏览文件 @
556e0222
...
...
@@ -163,6 +163,7 @@ def test_trace_profiler():
assert
out
.
get
(
"profiler"
)
@
pytest
.
mark
.
skip
(
reason
=
"force opt_level=0 when building graph"
)
def
test_goptions
():
@
trace
(
symbolic
=
True
,
opt_level
=
0
,
capture_as_const
=
True
)
def
f
(
x
):
...
...
@@ -181,6 +182,7 @@ def test_goptions():
np
.
testing
.
assert_equal
(
g
(
d
).
numpy
().
item
(),
1.0
)
@
pytest
.
mark
.
skip
(
reason
=
"force opt_level=0 when building graph"
)
def
test_goptions_log_sum_exp
():
@
trace
(
symbolic
=
True
,
opt_level
=
0
,
capture_as_const
=
True
)
def
f
(
x
,
y
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录