Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
a09fc5f7
MegEngine
项目概览
MegEngine 天元
/
MegEngine
1 年多 前同步成功
通知
403
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看板
提交
a09fc5f7
编写于
1月 23, 2021
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mgb/serialization): disable inplace arith graph opt in graph load
GitOrigin-RevId: d63baf8356d345013886692e464f8e4f49594887
上级
cd7090ac
变更
7
显示空白变更内容
内联
并排
Showing
7 changed file
with
75 addition
and
7 deletion
+75
-7
imperative/python/megengine/jit/tracing.py
imperative/python/megengine/jit/tracing.py
+4
-2
imperative/python/test/unit/test_tracing.py
imperative/python/test/unit/test_tracing.py
+0
-1
src/core/include/megbrain/graph/cg.h
src/core/include/megbrain/graph/cg.h
+8
-0
src/opr/impl/basic_arith.cpp
src/opr/impl/basic_arith.cpp
+2
-1
src/serialization/impl/serializer_oss.cpp
src/serialization/impl/serializer_oss.cpp
+9
-3
src/serialization/include/megbrain/serialization/serializer.h
...serialization/include/megbrain/serialization/serializer.h
+15
-0
src/serialization/test/serializer_oss.cpp
src/serialization/test/serializer_oss.cpp
+37
-0
未找到文件。
imperative/python/megengine/jit/tracing.py
浏览文件 @
a09fc5f7
...
...
@@ -755,8 +755,10 @@ class trace:
h2v
=
{}
graph
=
G
.
Graph
()
# only graph_opt_level takes effect in dump
self
.
_apply_graph_options
(
graph
)
# apply graph_opt_level in dump
if
self
.
_graph_opt_level
is
not
None
:
graph
.
options
.
graph_opt_level
=
self
.
_graph_opt_level
for
i
,
h
in
enumerate
(
self
.
_arg_bindings
):
info
=
self
.
_tinfo
[
h
]
...
...
imperative/python/test/unit/test_tracing.py
浏览文件 @
a09fc5f7
...
...
@@ -244,7 +244,6 @@ def test_goptions_log_sum_exp():
np
.
testing
.
assert_almost_equal
(
g
(
d
,
o
),
val
)
@
pytest
.
mark
.
skip
(
reason
=
"could not use opt_level=0 with dump"
)
def
test_goptions_log_exp
():
@
trace
(
symbolic
=
True
,
opt_level
=
0
,
capture_as_const
=
True
)
def
f
(
x
):
...
...
src/core/include/megbrain/graph/cg.h
浏览文件 @
a09fc5f7
...
...
@@ -355,6 +355,14 @@ class ComputingGraph : public std::enable_shared_from_this<ComputingGraph>,
*/
int16_t
graph_opt_level
=
2
;
/*!
* disable inplace arith transformations during graph
* construction
* it effectively disable level-1 graph optimization
* only for internal use during de-serialization
*/
bool
disable_inplace_arith_opt
=
false
;
/*!
* max size of allreduce packs in MB
* set this option to zero to disable PackAllReducePass
...
...
src/opr/impl/basic_arith.cpp
浏览文件 @
a09fc5f7
...
...
@@ -221,7 +221,8 @@ SymbolVar Elemwise::make(const VarNodeArrayView& inputs, Param param,
trait
.
name
,
cg
::
dump_var_info
(
inputs
).
c_str
());
#if !MGB_BUILD_SLIM_SERVING
if
(
inputs
[
0
]
->
owner_graph
()
->
options
().
graph_opt_level
)
{
auto
&&
options
=
inputs
[
0
]
->
owner_graph
()
->
options
();
if
(
options
.
graph_opt_level
&&
!
(
options
.
disable_inplace_arith_opt
))
{
auto
repl
=
gopt
::
optimize_elemwise_expr_inplace
(
dtp
.
get_vars
(),
param
,
config
);
if
(
repl
)
...
...
src/serialization/impl/serializer_oss.cpp
浏览文件 @
a09fc5f7
...
...
@@ -756,10 +756,16 @@ void GraphLoaderOSS::OprLoadContextImpl::load_single_opr(
GraphLoader
::
LoadResult
GraphLoaderOSS
::
OprLoadContextImpl
::
load_oprs
()
{
// load oprs
const
auto
*
oprs
=
m_loader
->
m_graph
->
oprs
();
{
// inplace arith graph optimization is disabled during opr load
// it tries to restore the same graph as it was dumped
// see test TestSerializer2.LOGEXP for example
GraphLoader
::
ScopedGraphOptDisabler
_
(
m_graph
);
for
(
flatbuffers
::
uoffset_t
i
=
0
;
i
<
oprs
->
size
();
++
i
)
{
m_current_opr
=
oprs
->
Get
(
i
);
load_single_opr
(
m_current_opr
);
}
}
// batched loading device values
m_device_value_loader
.
apply
();
...
...
src/serialization/include/megbrain/serialization/serializer.h
浏览文件 @
a09fc5f7
...
...
@@ -61,6 +61,21 @@ namespace serialization {
const
ComputingGraph
::
OutputSpec
&
outspec
);
};
//! helper to disable inplace arith graph optimization during
//! de-serialization
struct
ScopedGraphOptDisabler
{
bool
option_saved
;
std
::
shared_ptr
<
ComputingGraph
>
cg
;
ScopedGraphOptDisabler
(
std
::
shared_ptr
<
ComputingGraph
>&
cg_p
)
:
option_saved
(
true
),
cg
(
cg_p
)
{
std
::
swap
(
option_saved
,
cg
->
options
().
disable_inplace_arith_opt
);
}
~
ScopedGraphOptDisabler
()
{
cg
->
options
().
disable_inplace_arith_opt
=
option_saved
;
}
};
//! mem_node => tensor_value
using
SharedTensorMapEntry
=
ThinHashMap
<
MemNode
,
std
::
shared_ptr
<
DeviceTensorND
>>
;
...
...
src/serialization/test/serializer_oss.cpp
浏览文件 @
a09fc5f7
...
...
@@ -761,4 +761,41 @@ TEST(TestSerializer2, HasOutputDtype) {
load
();
}
TEST
(
TestSerializer2
,
LOGEXP
)
{
auto
fname
=
GET_OUTPUT_FILE
();
TensorShape
shape
{
2
,
3
};
using
Mode
=
opr
::
Elemwise
::
Mode
;
bool
inplace_opt
=
true
;
auto
dump
=
[
&
]()
{
auto
cn
=
CompNode
::
load
(
"xpu0"
);
auto
host_x
=
std
::
make_shared
<
HostTensorND
>
(
cn
,
shape
);
for
(
size_t
i
=
0
,
it
=
shape
.
total_nr_elems
();
i
<
it
;
++
i
)
host_x
->
ptr
<
float
>
()[
i
]
=
0.0
;
// To avoid NAN
auto
graph
=
ComputingGraph
::
make
();
if
(
!
inplace_opt
)
graph
->
options
().
graph_opt_level
=
0
;
auto
x
=
opr
::
Host2DeviceCopy
::
make
(
*
graph
,
host_x
,
{
"x"
});
auto
y
=
opr
::
Elemwise
::
make
({
x
},
Mode
::
EXP
);
auto
z
=
opr
::
Elemwise
::
make
({
y
},
Mode
::
LOG
);
auto
dumper
=
GraphDumper
::
make
(
OutputFile
::
make_fs
(
fname
.
c_str
()),
GraphDumpFormat
::
FLATBUFFERS
);
auto
rst
=
dumper
->
dump
({
z
.
rename
(
"z"
),
z
});
size_t
expected_nr_opr
=
inplace_opt
?
1
:
3
;
ASSERT_EQ
(
expected_nr_opr
,
rst
.
nr_opr
);
};
auto
load
=
[
&
]()
{
auto
loader
=
GraphLoader
::
make
(
InputFile
::
make_fs
(
fname
.
c_str
()),
GraphDumpFormat
::
FLATBUFFERS
);
auto
rst
=
loader
->
load
();
};
dump
();
load
();
inplace_opt
=
!
inplace_opt
;
dump
();
load
();
}
#endif
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录