From bb8f292816937a86153bdf5def1236cbe56844b4 Mon Sep 17 00:00:00 2001 From: Megvii Engine Team Date: Fri, 22 Jan 2021 18:00:25 +0800 Subject: [PATCH] refactor(mge/func): change `conv_execution_strategy` to `execution_strategy` GitOrigin-RevId: 7aef9935ee918ddb967e12fb84453a3b2c918e1b --- .../megengine/functional/debug_param.py | 40 ++++++++++++++----- imperative/python/megengine/functional/nn.py | 10 ++--- .../python/megengine/functional/quantized.py | 6 +-- .../integration/test_correctness_mnistnet.py | 6 +-- .../test/integration/test_dp_correctness.py | 4 +- src/opr/impl/search_policy/algo_chooser.cpp | 6 ++- 6 files changed, 46 insertions(+), 26 deletions(-) diff --git a/imperative/python/megengine/functional/debug_param.py b/imperative/python/megengine/functional/debug_param.py index c9e042b05..a257938d1 100644 --- a/imperative/python/megengine/functional/debug_param.py +++ b/imperative/python/megengine/functional/debug_param.py @@ -8,23 +8,31 @@ # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. import os -_conv_execution_strategy = os.getenv("MEGENGINE_CONV_EXECUTION_STRATEGY", "HEURISTIC") +from ..logger import get_logger +from ..utils.deprecation import deprecated +_execution_strategy = os.getenv("MEGENGINE_EXECUTION_STRATEGY", "HEURISTIC") -def get_conv_execution_strategy() -> str: +if os.getenv("MEGENGINE_CONV_EXECUTION_STRATEGY") != None: + get_logger().warning( + "Environment variable `MEGENGINE_CONV_EXECUTION_STRATEGY` is deprecated, please use `MEGENGINE_EXECUTION_STRATEGY`" + ) + + +def get_execution_strategy() -> str: """ - Returns the execuation strategy of :class:`~.Conv2d`. + Returns the execution strategy of :class:`~.Conv2d` and :func:'~.matmul' - See :func:`~.set_conv_execution_strategy` for possible return values + See :func:`~.set_execution_strategy` for possible return values """ - return _conv_execution_strategy + return _execution_strategy -def set_conv_execution_strategy(option: str): +def set_execution_strategy(option: str): """ - Sets the execuation strategy of :class:`~.Conv2d`. + Sets the execution strategy of :class:`~.Conv2d` and :func:'~.matmul' - :param option: Decides how :class:`~.Conv2d` algorithm is chosen. + :param option: Decides how :class:`~.Conv2d` and :func:'~.matmul' algorithms are chosen. Available values: * 'HEURISTIC' uses heuristic to choose the fastest algorithm. @@ -35,7 +43,7 @@ def set_conv_execution_strategy(option: str): The default strategy is 'HEURISTIC'. - It can also be set through the environment variable 'MEGENGINE_CONV_EXECUTION_STRATEGY'. + It can also be set through the environment variable 'MEGENGINE_EXECUTION_STRATEGY'. """ valid_option = ( "HEURISTIC", @@ -47,5 +55,15 @@ def set_conv_execution_strategy(option: str): if not option in valid_option: raise ValueError("Valid option can only be one of {}".format(valid_option)) - global _conv_execution_strategy # pylint: disable=global-statement - _conv_execution_strategy = option + global _execution_strategy # pylint: disable=global-statement + _execution_strategy = option + + +@deprecated(version="1.3", reason="use get_execution_strategy() instead") +def get_conv_execution_strategy() -> str: + return get_execution_strategy() + + +@deprecated(version="1.3", reason="use set_execution_strategy() instead") +def set_conv_execution_strategy(option: str): + return set_execution_strategy(option) diff --git a/imperative/python/megengine/functional/nn.py b/imperative/python/megengine/functional/nn.py index 66c2d173f..e3e52bbd5 100644 --- a/imperative/python/megengine/functional/nn.py +++ b/imperative/python/megengine/functional/nn.py @@ -22,7 +22,7 @@ from ..jit.tracing import is_tracing from ..random import uniform from ..tensor import Tensor from ..utils.tuple_function import _pair, _pair_nonzero -from .debug_param import get_conv_execution_strategy +from .debug_param import get_execution_strategy from .distributed import all_reduce_sum from .elemwise import exp, floor, log, log1p, maximum, minimum, relu from .math import argsort, matmul, max, prod, sum @@ -149,7 +149,7 @@ def conv2d( pad_w=pad_w, dilate_h=dilate_h, dilate_w=dilate_w, - strategy=get_conv_execution_strategy(), + strategy=get_execution_strategy(), mode=conv_mode, compute_mode=compute_mode, sparse=sparse_type, @@ -217,7 +217,7 @@ def conv_transpose2d( pad_w=pad_w, dilate_h=dilate_h, dilate_w=dilate_w, - strategy=get_conv_execution_strategy(), + strategy=get_execution_strategy(), ) weight, inp = utils.convert_inputs(weight, inp) (output,) = apply(op, weight, inp) @@ -282,7 +282,7 @@ def deformable_conv2d( pad_w=pad_w, dilate_h=dilate_h, dilate_w=dilate_w, - strategy=get_conv_execution_strategy(), + strategy=get_execution_strategy(), mode=conv_mode, compute_mode=compute_mode, sparse=sparse_type, @@ -1658,7 +1658,7 @@ def conv1d( pad_w=0, dilate_h=dilate_h, dilate_w=1, - strategy=get_conv_execution_strategy(), + strategy=get_execution_strategy(), mode=conv_mode, compute_mode=compute_mode, sparse=sparse_type, diff --git a/imperative/python/megengine/functional/quantized.py b/imperative/python/megengine/functional/quantized.py index a6b8375a1..53a00d626 100644 --- a/imperative/python/megengine/functional/quantized.py +++ b/imperative/python/megengine/functional/quantized.py @@ -12,7 +12,7 @@ from ..core._imperative_rt.core2 import apply from ..core.ops import builtin from ..tensor import Tensor from ..utils.tuple_function import _pair, _pair_nonzero -from .debug_param import get_conv_execution_strategy +from .debug_param import get_execution_strategy def conv_bias_activation( @@ -65,7 +65,7 @@ def conv_bias_activation( dilate_w=dw, dtype=dtype, format="NCHW", - strategy=get_conv_execution_strategy(), + strategy=get_execution_strategy(), nonlineMode=nonlinear_mode, mode=conv_mode, compute_mode=compute_mode, @@ -125,7 +125,7 @@ def batch_conv_bias_activation( dilate_w=dw, dtype=dtype, format="NCHW", - strategy=get_conv_execution_strategy(), + strategy=get_execution_strategy(), nonlineMode=nonlinear_mode, mode=conv_mode, compute_mode=compute_mode, diff --git a/imperative/python/test/integration/test_correctness_mnistnet.py b/imperative/python/test/integration/test_correctness_mnistnet.py index 6b21cdf3e..a88075a23 100644 --- a/imperative/python/test/integration/test_correctness_mnistnet.py +++ b/imperative/python/test/integration/test_correctness_mnistnet.py @@ -20,7 +20,7 @@ import megengine.functional as F from megengine import jit from megengine.core._trace_option import set_symbolic_shape from megengine.core.tensor.utils import make_shape_tuple -from megengine.functional.debug_param import set_conv_execution_strategy +from megengine.functional.debug_param import set_execution_strategy from megengine.jit import SublinearMemoryConfig from megengine.module import ( AdaptiveAvgPool2d, @@ -242,7 +242,7 @@ def test_correctness(): else: model_name = "mnist_model_with_test_cpu.mge" model_path = os.path.join(os.path.dirname(__file__), model_name) - set_conv_execution_strategy("HEURISTIC_REPRODUCIBLE") + set_execution_strategy("HEURISTIC_REPRODUCIBLE") run_train(model_path, False, False, max_err=1e-5) run_train(model_path, True, False, max_err=1e-5) @@ -265,7 +265,7 @@ def test_correctness_use_adaptive_pooling(): else: model_name = "mnist_model_with_test_cpu.mge" model_path = os.path.join(os.path.dirname(__file__), model_name) - set_conv_execution_strategy("HEURISTIC_REPRODUCIBLE") + set_execution_strategy("HEURISTIC_REPRODUCIBLE") run_train(model_path, False, False, max_err=1e-5, use_adaptive_pooling=True) run_train(model_path, True, False, max_err=1e-5, use_adaptive_pooling=True) diff --git a/imperative/python/test/integration/test_dp_correctness.py b/imperative/python/test/integration/test_dp_correctness.py index 311f044fb..1ef341753 100644 --- a/imperative/python/test/integration/test_dp_correctness.py +++ b/imperative/python/test/integration/test_dp_correctness.py @@ -21,7 +21,7 @@ import megengine.autodiff as ad import megengine.distributed as dist import megengine.functional as F from megengine.device import get_default_device, set_default_device -from megengine.functional.debug_param import set_conv_execution_strategy +from megengine.functional.debug_param import set_execution_strategy from megengine.module import AvgPool2d, BatchNorm2d, Conv2d, Linear, Module from megengine.optimizer import SGD from megengine.tensor import Tensor @@ -198,5 +198,5 @@ def run_test( def test_dp_correctness(): model_name = "mnist_model_with_test.mge" model_path = os.path.join(os.path.dirname(__file__), model_name) - set_conv_execution_strategy("HEURISTIC_REPRODUCIBLE") + set_execution_strategy("HEURISTIC_REPRODUCIBLE") run_test(model_path, False, False, max_err=1e-5) diff --git a/src/opr/impl/search_policy/algo_chooser.cpp b/src/opr/impl/search_policy/algo_chooser.cpp index bbca06505..2dd5f9139 100644 --- a/src/opr/impl/search_policy/algo_chooser.cpp +++ b/src/opr/impl/search_policy/algo_chooser.cpp @@ -336,8 +336,10 @@ void AlgoChooser::profile(ExeContext& ctx, bool require_reproducible) { rst.workspace, rst.time); prof_rst.push_back(rst); } - mgb_assert(!prof_rst.empty(), "no usable convolution algorithm %s", - str_on_inp_shape.c_str()); + std::string msg = ssprintf("no usable %s algorithm %s", + ctx.mgb_opr()->dyn_typeinfo()->name, + str_on_inp_shape.c_str()); + mgb_assert(!prof_rst.empty(), "%s", msg.c_str()); FixedTensorLayouts origin_layouts = ctx.layouts(); typename Opr::Param origin_param = ctx.megdnn_opr()->param(); -- GitLab