• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2022-2023 Huawei Technologies Co., Ltd
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15"""JitConfig for compile."""
16
17class JitConfig:
18    """
19    Jit config for compile.
20
21    Args:
22        jit_level (str, optional): Used to control the compilation optimization level.
23            Supports ["O0", "O1", "O2"]. Default: ``""`` , The framework automatically selects the execution method.
24            Not recommended, it is recommended to use the jit decorator.
25
26            - ``"O0"``: Except for optimizations that may affect functionality, all other optimizations are turned off,
27              adopt KernelByKernel execution mode.
28            - ``"O1"``: Using commonly used optimizations and automatic operator fusion optimizations,
29              adopt KernelByKernel execution mode.
30            - ``"O2"``: Ultimate performance optimization, adopt Sink execution mode.
31
32        exc_mode (str, optional): Control the execution mode of the model.
33            Supports ["auto", "sink", "no_sink"]. Default: ``"auto"`` .
34
35            - ``"auto"``: The framework automatically selects the execution method.
36            - ``"sink"``: Support the network to load and load the entire device at once, and then execute it by
37              input driver, without the need to iterate through each operator to achieve better execution performance.
38              This mode is only supported on the Ascend backend.
39            - ``"no_sink"``: The network model is executed asynchronously one by one using a single operator.
40
41        jit_syntax_level (str, optional): JIT syntax level for graph compiling.
42            The value must be ``"STRICT"`` , ``"LAX"`` or ``""`` . Default to an empty string, which means that this
43            JitConfig configuration will be ignored and the jit_syntax_level of ms.context will be used.
44            For more details about ms.context, refer to
45            `set_context <https://www.mindspore.cn/docs/en/master/api_python/mindspore/mindspore.set_context.html>`_ .
46            Default: ``""`` .
47
48            - ``"STRICT"``: Only basic syntax is supported, and execution performance is optimal. Can be used for MindIR
49              load and export.
50            - ``"LAX"``: Compatible with all Python syntax as much as possible. However, execution performance may be
51              affected and not optimal. Cannot be used for MindIR load and export due to some syntax that may not be
52              able to be exported.
53
54        debug_level (str, optional): Set debugging level for graph compiling.
55            The value must be ``"RELEASE"`` or ``"DEBUG"``. Default value: ``RELEASE``.
56
57            - ``RELEASE``: Used for normally running, and some debug information will be discard to get a better
58              compiling performance.
59            - ``DEBUG``: Used for debugging when errors occur, more information will be record in compiling process.
60
61        infer_boost (str, optional): enable infer boost mode.
62            The value must be ``"on"`` , ``"off"``. Default to an "off", which means that disable infer boost.
63            when infer boost mode is enabled, MindSpore will use high perf kernel lib, use faster runtime make
64            infer speed is best.
65            Note: current infer boost only support `jit_level` == ``"O0"`` and only Atlas A2 series products
66            are supported.
67
68        **kwargs (dict): A dictionary of keyword arguments that the class needs.
69
70    Examples:
71        >>> from mindspore import JitConfig
72        >>>
73        >>> jitconfig = JitConfig(jit_level="O1")
74        >>>
75        >>> # Define the network structure of LeNet5. Refer to
76        >>> # https://gitee.com/mindspore/docs/blob/master/docs/mindspore/code/lenet.py
77        >>> net = LeNet5()
78        >>>
79        >>> net.set_jit_config(jitconfig)
80    """
81    def __init__(self, jit_level="", exc_mode="auto", jit_syntax_level="", debug_level="RELEASE",
82                 infer_boost="off", **kwargs):
83        if jit_level not in ["", "O0", "O1", "O2"]:
84            raise ValueError("For 'jit_level' must be one of ['O0', 'O1', 'O2'].")
85        if exc_mode not in ["auto", "sink", "no_sink"]:
86            raise ValueError("For 'exc_mode' must be one of '['auto', 'sink', 'no_sink']'.")
87        if jit_syntax_level != "" and jit_syntax_level not in ["STRICT", "COMPATIBLE", "LAX"]:
88            raise ValueError("For 'jit_syntax_level' must be one of '['STRICT', 'LAX']'.")
89        if debug_level not in ["RELEASE", "DEBUG"]:
90            raise ValueError("For 'debug_level' must be one of '['RELEASE', 'DEBUG']'.")
91        if infer_boost != "" and infer_boost not in ["on", "off"]:
92            raise ValueError("For 'infer_boost' must be one of '['on', 'off']'.")
93        self.jit_config_dict = kwargs
94        self.jit_config_dict["jit_level"] = jit_level
95        self.jit_config_dict["exc_mode"] = exc_mode
96        self.jit_config_dict["jit_syntax_level"] = jit_syntax_level
97        self.jit_config_dict["debug_level"] = debug_level
98        self.jit_config_dict["infer_boost"] = infer_boost
99