Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
f0959dc9
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看板
提交
f0959dc9
编写于
4月 26, 2020
作者:
M
Megvii Engine Team
提交者:
Xinran Xu
5月 01, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(mge/examples): add example to use modified sublinear API
GitOrigin-RevId: c6f6c65d0bf2011bb8590aec3285bbac99fc9c34
上级
a2f0e878
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
48 addition
and
52 deletion
+48
-52
python_module/megengine/jit/__init__.py
python_module/megengine/jit/__init__.py
+28
-31
python_module/megengine/jit/sublinear_memory_config.py
python_module/megengine/jit/sublinear_memory_config.py
+12
-2
python_module/test/integration/test_correctness.py
python_module/test/integration/test_correctness.py
+5
-16
python_module/test/unit/jit/test_jit.py
python_module/test/unit/jit/test_jit.py
+3
-3
未找到文件。
python_module/megengine/jit/__init__.py
浏览文件 @
f0959dc9
...
@@ -18,7 +18,7 @@ import megengine._internal as mgb
...
@@ -18,7 +18,7 @@ import megengine._internal as mgb
from
megengine._internal.plugin
import
CompGraphProfiler
from
megengine._internal.plugin
import
CompGraphProfiler
from
..core
import
Tensor
,
graph
,
tensor
from
..core
import
Tensor
,
graph
,
tensor
from
.sublinear_memory_config
import
SublinearMemConfig
from
.sublinear_memory_config
import
SublinearMem
ory
Config
def
sideeffect
(
f
):
def
sideeffect
(
f
):
...
@@ -82,8 +82,8 @@ class trace:
...
@@ -82,8 +82,8 @@ class trace:
:param symbolic: Whether to use symbolic tensor. Default: False
:param symbolic: Whether to use symbolic tensor. Default: False
:param opt_level: Optimization level for compiling trace.
:param opt_level: Optimization level for compiling trace.
:param log_level: Log level.
:param log_level: Log level.
:param
enable_sublinear: Enable sublinear memory optimization. Default: False
:param
sublinear_memory_config: Configuration for sublinear memory optimization.
:param sublinear_mem_config: Configuration for sublinear memory optimization
.
If not None, it enables sublinear memory optimization with given setting
.
:param profiling: Whether to profile compiled trace. Default: False
:param profiling: Whether to profile compiled trace. Default: False
"""
"""
...
@@ -106,16 +106,14 @@ class trace:
...
@@ -106,16 +106,14 @@ class trace:
symbolic
:
bool
=
False
,
symbolic
:
bool
=
False
,
opt_level
:
int
=
None
,
opt_level
:
int
=
None
,
log_level
:
int
=
None
,
log_level
:
int
=
None
,
enable_sublinear
:
bool
=
False
,
sublinear_memory_config
:
SublinearMemoryConfig
=
None
,
sublinear_mem_config
:
SublinearMemConfig
=
None
,
profiling
:
bool
=
False
profiling
:
bool
=
False
):
):
self
.
__wrapped__
=
func
self
.
__wrapped__
=
func
self
.
_symbolic
=
symbolic
self
.
_symbolic
=
symbolic
self
.
_graph_opt_level
=
opt_level
self
.
_graph_opt_level
=
opt_level
self
.
_log_level
=
log_level
self
.
_log_level
=
log_level
self
.
_enable_sublinear
=
enable_sublinear
self
.
_sublinear_memory_config
=
sublinear_memory_config
self
.
_sublinear_mem_config
=
sublinear_mem_config
self
.
_status
=
self
.
_UNSTARTED
self
.
_status
=
self
.
_UNSTARTED
self
.
_args
=
None
self
.
_args
=
None
self
.
_kwargs
=
None
self
.
_kwargs
=
None
...
@@ -287,35 +285,34 @@ class trace:
...
@@ -287,35 +285,34 @@ class trace:
def
_apply_graph_options
(
self
,
cg
):
def
_apply_graph_options
(
self
,
cg
):
# graph opt level
# graph opt level
if
not
(
self
.
_graph_opt_level
is
None
)
:
if
self
.
_graph_opt_level
is
not
None
:
cg
.
set_option
(
"graph_opt_level"
,
self
.
_graph_opt_level
)
cg
.
set_option
(
"graph_opt_level"
,
self
.
_graph_opt_level
)
# log level
# log level
if
not
(
self
.
_log_level
is
None
)
:
if
self
.
_log_level
is
not
None
:
cg
.
set_option
(
"log_level"
,
self
.
_log_level
)
cg
.
set_option
(
"log_level"
,
self
.
_log_level
)
# sublinear
# sublinear
if
self
.
_
enable_sublinear
:
if
self
.
_
sublinear_memory_config
is
not
None
:
cg
.
set_option
(
"enable_sublinear_memory_opt"
,
True
)
cg
.
set_option
(
"enable_sublinear_memory_opt"
,
True
)
if
not
(
self
.
_sublinear_mem_config
is
None
):
cg
.
set_option
(
cg
.
set_option
(
"sublinear_mem_cofig.lb_memory"
,
"sublinear_mem_cofig.lb_memory"
,
self
.
_sublinear_memory_config
.
lb_memory
,
self
.
_sublinear_mem_config
.
lb_memory
,
)
)
cg
.
set_option
(
cg
.
set_option
(
"sublinear_mem_cofig.genetic_nr_iter"
,
"sublinear_mem_cofig.genetic_nr_iter"
,
self
.
_sublinear_memory_config
.
genetic_nr_iter
,
self
.
_sublinear_mem_config
.
genetic_nr_iter
,
)
)
cg
.
set_option
(
cg
.
set_option
(
"sublinear_mem_cofig.genetic_pool_size"
,
"sublinear_mem_cofig.genetic_pool_size"
,
self
.
_sublinear_memory_config
.
genetic_pool_size
,
self
.
_sublinear_mem_config
.
genetic_pool_size
,
)
)
cg
.
set_option
(
cg
.
set_option
(
"sublinear_mem_cofig.thresh_nr_try"
,
"sublinear_mem_cofig.thresh_nr_try"
,
self
.
_sublinear_memory_config
.
thresh_nr_try
,
self
.
_sublinear_mem_config
.
thresh_nr_try
,
)
)
cg
.
set_option
(
cg
.
set_option
(
"sublinear_mem_cofig.num_worker"
,
"sublinear_mem_cofig.num_worker"
,
self
.
_sublinear_memory_config
.
num_worker
,
self
.
_sublinear_mem_config
.
num_worker
,
)
)
# profile
# profile
if
self
.
_profiling
:
if
self
.
_profiling
:
self
.
_profiler
=
CompGraphProfiler
(
cg
)
self
.
_profiler
=
CompGraphProfiler
(
cg
)
...
...
python_module/megengine/jit/sublinear_memory_config.py
浏览文件 @
f0959dc9
...
@@ -10,7 +10,7 @@
...
@@ -10,7 +10,7 @@
from
..core.device
import
get_device_count
from
..core.device
import
get_device_count
class
SublinearMemConfig
:
class
SublinearMem
ory
Config
:
r
"""
r
"""
Configuration for sublinear memory optimization.
Configuration for sublinear memory optimization.
...
@@ -28,7 +28,11 @@ class SublinearMemConfig:
...
@@ -28,7 +28,11 @@ class SublinearMemConfig:
It can also be set through the environmental variable 'MGB_SUBLINEAR_MEMORY_LOWER_BOUND_MB'.
It can also be set through the environmental variable 'MGB_SUBLINEAR_MEMORY_LOWER_BOUND_MB'.
:param num_worker: number of thread workers to search the optimum checkpoints
:param num_worker: number of thread workers to search the optimum checkpoints
in sublinear memory optimization. Default: half of cpu number in the system.
in sublinear memory optimization. Default: half of cpu number in the system.
Note: the value must be greater or equal to one.
It can also be set through the environmental variable 'MGB_SUBLINEAR_MEMORY_WORKERS'.
It can also be set through the environmental variable 'MGB_SUBLINEAR_MEMORY_WORKERS'.
Note that the environmental variable MGB_COMP_GRAPH_OPT must be set to 'enable_sublinear_memory_opt=1'
in order for the above environmental variable to be effective.
"""
"""
def
__init__
(
def
__init__
(
...
@@ -37,10 +41,16 @@ class SublinearMemConfig:
...
@@ -37,10 +41,16 @@ class SublinearMemConfig:
genetic_nr_iter
:
int
=
0
,
genetic_nr_iter
:
int
=
0
,
genetic_pool_size
:
int
=
20
,
genetic_pool_size
:
int
=
20
,
lb_memory
:
int
=
0
,
lb_memory
:
int
=
0
,
num_worker
:
int
=
get_device_count
(
"cpu"
)
/
2
,
num_worker
:
int
=
max
(
1
,
get_device_count
(
"cpu"
)
//
2
)
,
):
):
assert
thresh_nr_try
>=
0
,
"thresh_nr_try must be greater or equal to zero"
self
.
thresh_nr_try
=
thresh_nr_try
self
.
thresh_nr_try
=
thresh_nr_try
assert
genetic_nr_iter
>=
0
,
"genetic_nr_iter must be greater or equal to zero"
self
.
genetic_nr_iter
=
genetic_nr_iter
self
.
genetic_nr_iter
=
genetic_nr_iter
assert
(
genetic_pool_size
>=
0
),
"genetic_pool_size must be greater or equal to zero"
self
.
genetic_pool_size
=
genetic_pool_size
self
.
genetic_pool_size
=
genetic_pool_size
self
.
lb_memory
=
lb_memory
self
.
lb_memory
=
lb_memory
assert
num_worker
>
0
,
"num_worker must be greater or equal to one"
self
.
num_worker
=
num_worker
self
.
num_worker
=
num_worker
python_module/test/integration/test_correctness.py
浏览文件 @
f0959dc9
...
@@ -17,7 +17,7 @@ import megengine as mge
...
@@ -17,7 +17,7 @@ import megengine as mge
import
megengine.functional
as
F
import
megengine.functional
as
F
from
megengine
import
jit
,
tensor
from
megengine
import
jit
,
tensor
from
megengine.functional.debug_param
import
set_conv_execution_strategy
from
megengine.functional.debug_param
import
set_conv_execution_strategy
from
megengine.jit
import
SublinearMemConfig
from
megengine.jit
import
SublinearMem
ory
Config
from
megengine.module
import
AvgPool2d
,
BatchNorm2d
,
Conv2d
,
Linear
,
Module
from
megengine.module
import
AvgPool2d
,
BatchNorm2d
,
Conv2d
,
Linear
,
Module
from
megengine.optimizer
import
SGD
from
megengine.optimizer
import
SGD
from
megengine.test
import
assertTensorClose
from
megengine.test
import
assertTensorClose
...
@@ -132,12 +132,7 @@ def update_model(model_path):
...
@@ -132,12 +132,7 @@ def update_model(model_path):
def
run_test
(
def
run_test
(
model_path
,
model_path
,
use_jit
,
use_symbolic
,
sublinear_memory_config
=
None
,
max_err
=
None
,
use_jit
,
use_symbolic
,
enable_sublinear
=
False
,
sublinear_mem_config
=
None
,
max_err
=
None
,
):
):
"""
"""
...
@@ -168,8 +163,7 @@ def run_test(
...
@@ -168,8 +163,7 @@ def run_test(
train_func
=
jit
.
trace
(
train_func
=
jit
.
trace
(
train_func
,
train_func
,
symbolic
=
use_symbolic
,
symbolic
=
use_symbolic
,
enable_sublinear
=
enable_sublinear
,
sublinear_memory_config
=
sublinear_memory_config
,
sublinear_mem_config
=
sublinear_mem_config
,
)
)
opt
.
zero_grad
()
opt
.
zero_grad
()
...
@@ -199,12 +193,7 @@ def test_correctness():
...
@@ -199,12 +193,7 @@ def test_correctness():
run_test
(
model_path
,
True
,
True
)
run_test
(
model_path
,
True
,
True
)
# sublinear
# sublinear
config
=
SublinearMemConfig
(
genetic_nr_iter
=
10
)
config
=
SublinearMem
ory
Config
(
genetic_nr_iter
=
10
)
run_test
(
run_test
(
model_path
,
model_path
,
True
,
True
,
sublinear_memory_config
=
config
,
max_err
=
1e-5
,
True
,
True
,
enable_sublinear
=
True
,
sublinear_mem_config
=
config
,
max_err
=
1e-5
,
)
)
python_module/test/unit/jit/test_jit.py
浏览文件 @
f0959dc9
...
@@ -18,7 +18,7 @@ import megengine._internal as mgb
...
@@ -18,7 +18,7 @@ import megengine._internal as mgb
import
megengine.module
as
M
import
megengine.module
as
M
from
megengine
import
jit
,
tensor
from
megengine
import
jit
,
tensor
from
megengine.core.tensor
import
Tensor
from
megengine.core.tensor
import
Tensor
from
megengine.jit
import
SublinearMemConfig
from
megengine.jit
import
SublinearMem
ory
Config
from
megengine.test
import
assertTensorClose
from
megengine.test
import
assertTensorClose
...
@@ -190,9 +190,9 @@ def test_dump_bn_fused():
...
@@ -190,9 +190,9 @@ def test_dump_bn_fused():
# Simply verify the options passed down
# Simply verify the options passed down
def
test_sublinear
():
def
test_sublinear
():
config
=
SublinearMemConfig
(
genetic_nr_iter
=
10
)
config
=
SublinearMem
ory
Config
(
genetic_nr_iter
=
10
)
@
jit
.
trace
(
symbolic
=
True
,
enable_sublinear
=
True
,
sublinear_mem
_config
=
config
)
@
jit
.
trace
(
symbolic
=
True
,
sublinear_memory
_config
=
config
)
def
f
(
x
):
def
f
(
x
):
return
x
+
x
return
x
+
x
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录