algo_parameter_config.py 6.3 KB
Newer Older
Z
zhunaipan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
"""Configuration of parameters for strategy-searching algorithm in auto_parallel"""

import threading
from mindspore._c_expression import CostModelContext
19
from mindspore._checkparam import args_type_check
Z
zhunaipan 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

__all__ = ["get_algo_parameters", "reset_algo_parameters", "set_algo_parameters"]


class _AlgoParameterConfig():
    """
    _AlgoParameterConfig is the configuration of setting parameters used in th algorithm.

    Note:
        Creating a config through instantiating _AlgoParameterConfig object is not recommended.
        Use algo_parameter_config() to get the configuration since _AlgoParameterConfig is singleton.
    """
    _instance = None
    _instance_lock = threading.Lock()

    def __init__(self):
        self._config_handle = CostModelContext.get_instance()

    def check_config_handle(self):
        """
        Check config handle.

        Raises:
            ValueError: If the config handle is none.
        """
        if self._config_handle is None:
            raise ValueError("Config handle is none!!!")

48
    def set_fully_use_devices(self, not_fully):
Z
zhunaipan 已提交
49
        self.check_config_handle()
50
        self._config_handle.set_fully_use_devices(not_fully)
Z
zhunaipan 已提交
51

52
    def get_fully_use_devices(self):
Z
zhunaipan 已提交
53
        self.check_config_handle()
54
        return self._config_handle.get_fully_use_devices()
Z
zhunaipan 已提交
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112

    def set_elementwise_op_strategy_follow(self, element_strategy_follow):
        self.check_config_handle()
        self._config_handle.set_elementwise_op_strategy_follow(element_strategy_follow)

    def get_elementwise_op_strategy_follow(self):
        self.check_config_handle()
        return self._config_handle.get_elementwise_op_strategy_follow()

    def set_tensor_slice_align_enable(self, align_enable):
        self.check_config_handle()
        self._config_handle.set_tensor_slice_align_enable(align_enable)

    def get_tensor_slice_align_enable(self):
        self.check_config_handle()
        return self._config_handle.get_tensor_slice_align_enable()

    def set_tensor_slice_align_size(self, align_size):
        """
        Set tensor slice align size.

        Args:
            align_size (int): The minimum tensor slice shape.

        Raises:
            ValueError: If align_size is not in [1, 1024].
        """
        self.check_config_handle()
        if align_size < 1 or align_size > 1024:
            raise ValueError('Align_size must be in [1, 1024], but got {}'.format(align_size))
        self._config_handle.set_tensor_slice_align_size(align_size)

    def get_tensor_slice_align_size(self):
        self.check_config_handle()
        return self._config_handle.get_tensor_slice_align_size()

    def reset_algo_parameters(self):
        self.check_config_handle()
        self._config_handle.reset_algo_parameters()


_g_algo_parameter_config = None


def _algo_parameter_config():
    """
    Get the global _g_algo_parameter_config. If it is not created, create a new one.

    Returns:
        The global _g_algo_parameter_config.
    """
    global _g_algo_parameter_config
    if _g_algo_parameter_config is None:
        _g_algo_parameter_config = _AlgoParameterConfig()
    return _g_algo_parameter_config


set_algo_parameters_config_func_map = {
113
    "fully_use_devices": _algo_parameter_config().set_fully_use_devices,
Z
zhunaipan 已提交
114 115 116 117 118 119
    "elementwise_op_strategy_follow": _algo_parameter_config().set_elementwise_op_strategy_follow,
    "tensor_slice_align_enable": _algo_parameter_config().set_tensor_slice_align_enable,
    "tensor_slice_align_size": _algo_parameter_config().set_tensor_slice_align_size}


get_algo_parameters_config_func_map = {
120
    "fully_use_devices": _algo_parameter_config().get_fully_use_devices,
Z
zhunaipan 已提交
121 122 123 124 125
    "elementwise_op_strategy_follow": _algo_parameter_config().get_elementwise_op_strategy_follow,
    "tensor_slice_align_enable": _algo_parameter_config().get_tensor_slice_align_enable,
    "tensor_slice_align_size": _algo_parameter_config().get_tensor_slice_align_size}


126
@args_type_check(tensor_slice_align_enable=bool, tensor_slice_align_size=int,
127
                 fully_use_devices=bool, elementwise_op_strategy_follow=bool)
Z
zhunaipan 已提交
128 129 130 131 132
def set_algo_parameters(**kwargs):
    """
    Set algo parameter config.

    Note:
S
simson 已提交
133
        The attribute name is required.
Z
zhunaipan 已提交
134 135

    Args:
S
simson 已提交
136
        tensor_slice_align_enable (bool): Whether to check the shape of tensor slice of MatMul. Default: False
137 138 139
        tensor_slice_align_size (int): The minimum tensor slice shape of MatMul, the value must be in [1, 1024].
            Default: 16
        fully_use_devices (bool): Whether ONLY generating strategies that fully use all available devices. Default: True
S
simson 已提交
140
        elementwise_op_strategy_follow (bool): Whether the elementwise operator has the same strategies as its
Z
zhunaipan 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
            subsequent operators. Default: False

    Raises:
        ValueError: If context keyword is not recognized.
    """
    for key, value in kwargs.items():
        if key not in set_algo_parameters_config_func_map:
            raise ValueError("Set context keyword %s is not recognized!" % key)
        set_func = set_algo_parameters_config_func_map[key]
        set_func(value)


def get_algo_parameters(attr_key):
    """
    Get algo parameter config attributes.

    Note:
S
simson 已提交
158
        Returns the specified attribute value.
Z
zhunaipan 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174

    Args:
        attr_key (str): The key of the attribute.

    Raises:
        ValueError: If context keyword is not recognized.
    """
    if attr_key not in get_algo_parameters_config_func_map:
        raise ValueError("Get context keyword %s is not recognized!" % attr_key)
    get_func = get_algo_parameters_config_func_map[attr_key]
    return get_func()


def reset_algo_parameters():
    """Reset algo parameter attributes."""
    _algo_parameter_config().reset_algo_parameters()