Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
MegEngine 天元
MegEngine
提交
27aa648b
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看板
提交
27aa648b
编写于
11月 07, 2021
作者:
M
Megvii Engine Team
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix(mge): control compute_mode by context
GitOrigin-RevId: f3968309f4f0db97b57fee1a65f54de7e366b5dc
上级
dfb9d980
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
242 addition
and
16 deletion
+242
-16
imperative/python/megengine/config.py
imperative/python/megengine/config.py
+3
-0
imperative/python/megengine/core/_config.py
imperative/python/megengine/core/_config.py
+205
-0
imperative/python/megengine/functional/debug_param.py
imperative/python/megengine/functional/debug_param.py
+34
-16
未找到文件。
imperative/python/megengine/config.py
0 → 100644
浏览文件 @
27aa648b
from
.core._config
import
*
__import__
(
"mprop"
).
init
()
imperative/python/megengine/core/_config.py
0 → 100644
浏览文件 @
27aa648b
# -*- coding: utf-8 -*-
# MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
#
# Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import
os
from
contextlib
import
contextmanager
__compute_mode
=
"default"
__conv_format
=
"default"
_benchmark_kernel
=
False
_deterministic_kernel
=
False
_async_level
=
os
.
getenv
(
"MEGENGINE_INTERP_ASYNC_LEVEL"
,
2
)
__all__
=
[
"benchmark_kernel"
,
"deterministic_kernel"
,
"async_level"
,
"_compute_mode"
,
"_conv_format"
,
"_override"
,
]
@
property
def
benchmark_kernel
(
mod
):
r
"""Whether or not run possible algorithms on real device to find the best one. The default option is false,
which means use heuristic to choose the fastest algorithm.
Examples:
.. code-block::
import megengine as mge
mge.config.benchmark_kernel = True
"""
return
_benchmark_kernel
@
benchmark_kernel
.
setter
def
benchmark_kernel
(
mod
,
option
:
bool
):
global
_benchmark_kernel
_benchmark_kernel
=
option
@
property
def
deterministic_kernel
(
mod
):
r
"""Whether or not the fastest algorithm choosed is reproducible. The default option is false,
which means the algorithm is not reproducible.
Examples:
.. code-block::
import megengine as mge
mge.config.deterministic_kernel = True
"""
return
_deterministic_kernel
@
deterministic_kernel
.
setter
def
deterministic_kernel
(
mod
,
option
:
bool
):
global
_deterministic_kernel
_deterministic_kernel
=
option
@
property
def
async_level
(
mod
)
->
int
:
r
"""Get or set config whether raise error exactly when invoking op. The default level is 2,
which means both device and user side errors are async.
Examples:
.. code-block::
import megengine as mge
mge.config.async_level = 2
"""
return
_async_level
@
async_level
.
setter
def
async_level
(
mod
,
level
:
int
):
global
_async_level
_async_level
=
level
@
property
def
_compute_mode
(
mod
):
r
"""Get or set the precision of intermediate results. The default option is "default",
which means that no special requirements will be placed on. When set to 'float32', it
would be used for accumulator and intermediate result, but only effective when input and
output are of float16 dtype.
Examples:
.. code-block::
import megengine as mge
mge.config._compute_mode = "default"
"""
return
__compute_mode
@
_compute_mode
.
setter
def
_compute_mode
(
mod
,
_compute_mode
:
str
):
global
__compute_mode
__compute_mode
=
_compute_mode
@
property
def
_conv_format
(
mod
):
r
"""Get or set convolution data/filter/output layout format. The default option is "default",
which means that no special format will be placed on. There are all layout definitions
``NCHW`` layout: ``{N, C, H, W}``
``NHWC`` layout: ``{N, H, W, C}``
``NHWCD4`` layout: ``{N, H, (C + 3) / 4, W, 4}``
``NHWCD4I`` layout: with ``align_axis = 2``
``NCHW4`` layout: ``{N, C/4, H, W, 4}``
``NCHW88`` layout: ``{N, C/8, H, W, 8}``
``CHWN4`` layout: ``{C/4, H, W, N, 4}``
``NCHW64`` layout: ``{N, C/64, H, W, 64}``
Examples:
.. code-block::
import megengine as mge
mge.config._conv_format = "NHWC"
"""
return
__conv_format
@
_conv_format
.
setter
def
_conv_format
(
mod
,
format
:
str
):
global
__conv_format
__conv_format
=
format
def
_reset_execution_config
(
benchmark_kernel
=
None
,
deterministic_kernel
=
None
,
async_level
=
None
,
compute_mode
=
None
,
conv_format
=
None
,
):
global
_benchmark_kernel
,
_deterministic_kernel
,
_async_level
,
__compute_mode
,
__conv_format
orig_flags
=
(
_benchmark_kernel
,
_deterministic_kernel
,
_async_level
,
__compute_mode
,
__conv_format
,
)
if
benchmark_kernel
is
not
None
:
_benchmark_kernel
=
benchmark_kernel
if
deterministic_kernel
is
not
None
:
_deterministic_kernel
=
deterministic_kernel
if
async_level
is
not
None
:
_async_level
=
async_level
if
compute_mode
is
not
None
:
__compute_mode
=
compute_mode
if
conv_format
is
not
None
:
__conv_format
=
conv_format
return
orig_flags
@
contextmanager
def
_override
(
benchmark_kernel
=
None
,
deterministic_kernel
=
None
,
async_level
=
None
,
compute_mode
=
None
,
conv_format
=
None
,
):
r
"""A context manager that users can opt in by attaching the decorator to set
the config of the global variable.
Examples:
.. code-block::
import megengine as mge
@mge.config._override(
benchmark_kernel = True,
deterministic_kernel = Fasle,
async_level=2,
compute_mode="float32",
conv_format="NHWC",
)
def train():
"""
orig_flags
=
_reset_execution_config
(
benchmark_kernel
,
deterministic_kernel
,
async_level
,
compute_mode
,
conv_format
,
)
try
:
yield
finally
:
# recover the previous values
_reset_execution_config
(
*
orig_flags
)
def
_get_actual_op_param
(
function_param
,
config_param
):
return
function_param
if
config_param
==
"default"
else
config_param
imperative/python/megengine/functional/debug_param.py
浏览文件 @
27aa648b
...
...
@@ -8,26 +8,38 @@
# "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
import
os
from
..core
import
_config
from
..core.ops
import
builtin
from
..logger
import
get_logger
from
..utils.deprecation
import
deprecated
Strategy
=
builtin
.
ops
.
Convolution
.
Strategy
_execution_strategy
=
os
.
getenv
(
"MEGENGINE_EXECUTION_STRATEGY"
,
"HEURISTIC"
)
if
os
.
getenv
(
"MEGENGINE_CONV_EXECUTION_STRATEGY"
)
!=
None
:
get_logger
().
warning
(
"Environment variable `MEGENGINE_CONV_EXECUTION_STRATEGY` is deprecated, please use `MEGENGINE_EXECUTION_STRATEGY`"
)
_valid_string_option
=
{
"REPRODUCIBLE"
:
Strategy
.
REPRODUCIBLE
,
"HEURISTIC"
:
Strategy
.
HEURISTIC
,
"PROFILE"
:
Strategy
.
PROFILE
,
}
def
get_execution_strategy
()
->
Strategy
:
r
"""Returns the execution strategy of :class:`~module..Conv2d` and :func:`~.matmul`
See :func:`~.set_execution_strategy` for possible return values
"""
return
_execution_strategy
strategy
=
Strategy
(
0
)
if
_config
.
_benchmark_kernel
:
strategy
|=
Strategy
.
PROFILE
else
:
strategy
|=
Strategy
.
HEURISTIC
if
_config
.
_deterministic_kernel
:
strategy
|=
Strategy
.
REPRODUCIBLE
return
strategy
def
set_execution_strategy
(
option
):
...
...
@@ -50,7 +62,6 @@ def set_execution_strategy(option):
* 'HEURISTIC' uses heuristic to choose the fastest algorithm.
* 'PROFILE' runs possible algorithms on real device to find the best one.
* 'PROFILE_HEURISTIC' uses profiling result and heuristic to choose the fastest algorithm.
* 'PROFILE_REPRODUCIBLE' uses the fastest of profiling result that is also reproducible.
* 'HEURISTIC_REPRODUCIBLE' uses heuristic to choose the fastest algorithm that is also reproducible.
...
...
@@ -58,29 +69,33 @@ def set_execution_strategy(option):
It can also be set through the environment variable 'MEGENGINE_EXECUTION_STRATEGY'.
"""
valid_string_option
=
{
"REPRODUCIBLE"
:
Strategy
.
REPRODUCIBLE
,
"HEURISTIC"
:
Strategy
.
HEURISTIC
,
"PROFILE"
:
Strategy
.
PROFILE
,
}
global
_execution_strategy
# pylint: disable=global-statement
if
isinstance
(
option
,
Strategy
):
_execution_strategy
=
option
_config
.
_benchmark_kernel
=
(
True
if
option
&
_valid_string_option
[
"PROFILE"
]
!=
Strategy
(
0
)
else
False
)
_config
.
_deterministic_kernel
=
(
True
if
option
&
_valid_string_option
[
"REPRODUCIBLE"
]
!=
Strategy
(
0
)
else
False
)
return
assert
isinstance
(
option
,
str
)
strategy_tmp
=
Strategy
(
0
)
_config
.
_benchmark_kernel
=
False
_config
.
_deterministic_kernel
=
False
for
opt
in
option
.
split
(
"_"
):
if
not
opt
in
valid_string_option
:
if
not
opt
in
_
valid_string_option
:
raise
ValueError
(
"Valid option can only be one of {}, or combine them with '_'."
.
format
(
valid_string_option
.
keys
()
_
valid_string_option
.
keys
()
)
)
strategy_tmp
=
strategy_tmp
|
valid_string_option
[
opt
]
_execution_strategy
=
strategy_tmp
_config
.
_benchmark_kernel
|=
_valid_string_option
[
opt
]
==
Strategy
.
PROFILE
_config
.
_deterministic_kernel
|=
(
_valid_string_option
[
opt
]
==
Strategy
.
REPRODUCIBLE
)
@
deprecated
(
version
=
"1.3"
,
reason
=
"use get_execution_strategy() instead"
)
...
...
@@ -91,3 +106,6 @@ def get_conv_execution_strategy() -> str:
@
deprecated
(
version
=
"1.3"
,
reason
=
"use set_execution_strategy() instead"
)
def
set_conv_execution_strategy
(
option
:
str
):
return
set_execution_strategy
(
option
)
set_execution_strategy
(
os
.
getenv
(
"MEGENGINE_EXECUTION_STRATEGY"
,
"HEURISTIC"
))
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录