mindspore.set_context

mindspore.set_context(**kwargs)[source]

Set context for running environment.

Context should be configured before running your program. If there is no configuration, it will be automatically set according to the device target by default.

Note

Attribute name is required for setting attributes. The mode is not recommended to be changed after net was initialized because the implementations of some operations are different in graph mode and pynative mode. Default: PYNATIVE_MODE.

Some configurations are device specific, see the below table for details:

Function Classification

Configuration Parameters

Hardware Platform Support

System Configuration

device_id

CPU/GPU/Ascend

device_target

CPU/GPU/Ascend

max_device_memory

GPU/Ascend

variable_memory_max_size

Ascend

mempool_block_size

GPU/Ascend

op_timeout

Ascend

Debug Configuration

save_graphs

CPU/GPU/Ascend

save_graphs_path

CPU/GPU/Ascend

enable_dump

Ascend

save_dump_path

Ascend

print_file_path

Ascend

env_config_path

CPU/GPU/Ascend

precompile_only

CPU/GPU/Ascend

reserve_class_name_in_scope

CPU/GPU/Ascend

pynative_synchronize

GPU/Ascend

Executive Control

mode

CPU/GPU/Ascend

enable_graph_kernel

Ascend/GPU

graph_kernel_flags

Ascend/GPU

enable_reduce_precision

Ascend

auto_tune_mode

Ascend

check_bprop

CPU/GPU/Ascend

max_call_depth

CPU/GPU/Ascend

grad_for_scalar

CPU/GPU/Ascend

enable_compile_cache

CPU/GPU/Ascend

inter_op_parallel_num

CPU/GPU/Ascend

runtime_num_threads

CPU/GPU/Ascend

compile_cache_path

CPU/GPU/Ascend

disable_format_transform

GPU

support_binary

CPU/GPU/Ascend

memory_optimize_level

CPU/GPU/Ascend

memory_offload

GPU/Ascend

Parameters
  • device_id (int) – ID of the target device, the value must be in [0, device_num_per_host-1], while device_num_per_host should be no more than 4096. Default: 0.

  • device_target (str) – The target device to run, support “Ascend”, “GPU”, and “CPU”. If device target is not set, the version of MindSpore package is used.

  • max_device_memory (str) – Set the maximum memory available for devices. The format is “xxGB”. Default: “1024GB”. The actual used memory size is the minimum of the available memory of the device and max_device_memory.

  • variable_memory_max_size (str) – This parameter is deprecated, and will be removed in a future version. Please use parameter ‘max_device_memory’ instead.

  • mempool_block_size (str) – Set the size of the memory pool block in PyNative mode for devices. The format is “xxGB”. Default: “1GB”. Minimum size is “1G”. The actual used memory block size is the minimum of the available memory of the device and mempool_block_size.

  • op_timeout (int) – Set the maximum duration of executing an operator in seconds. If the execution time exceeds this value, system will terminate the task. 0 means endless wait. Default: 0.

  • save_graphs (bool) – Whether to save graphs. Default: False. When the save_graphs attribute is set as True, attribute of save_graphs_path is used to set the intermediate compilation graph storage path. By default, the graphs are saved in the current directory.

  • save_graphs_path (str) – Path to save graphs. Default: “.”. If the specified directory does not exist, the system will automatically create the directory. During distributed training, graphs will be saved to the directory of save_graphs_path/rank_${rank_id}/. rank_id is the ID of the current device in the cluster.

  • enable_dump (bool) – This parameters is deprecated, and will be deleted in the next version.

  • save_dump_path (str) – This parameters is deprecated, and will be deleted in the next version.

  • print_file_path (str) – The path of saving print data. If this parameter is set, print data is saved to a file by default, and print_file_path is not set, the screen will be displayed. If the saved file already exists, the timestamp suffix will be added to the file. Saving data to a file solves the problem of data loss in screen printing when a large amount of data is generated. If it is not set, an error will be reported: prompt to set the upper absolute path.

  • env_config_path (str) –

    Config path for DFX. Through mindspore.set_context(env_config_path=”./mindspore_config.json”)

    configure RDR:

    • enable: controls whether the RDR is enabled to collect the key data during training and save key data in the fault scenario. When set to true, the RDR will be turned on. When set to false, the RDR will be turned off.

    • mode: sets the mode of RDR on exporting data. When set to 1, the RDR only exports data in the fault scenario. When set to 2, the RDR exports data in the fault scenario and the normal end scenario. Default: 1.

    • path: sets the path where RDR saves data. The current path must be absolute.

    Memory reuse:

    • mem_Reuse: controls whether the memory reuse function is turned on. When set to True,

    • the memory reuse function is turned on. When set to False, the memory reuse function is turned off.

  • precompile_only (bool) – Whether to only precompile the network. Default: False. If set to True, the network will only be compiled, not executed.

  • reserve_class_name_in_scope (bool) –

    Whether to save the network class name in the scope. Default: True. Each node has a scope. A scope of a subnode is the name of its parent node. If reserve_class_name_in_scope is set to True, the class name will be saved after keyword ‘net-’ in the scope. For example:

    Default/net-Net1/net-Net2 (reserve_class_name_in_scope=True)

    Default/net/net (reserve_class_name_in_scope=False)

  • pynative_synchronize (bool) – Whether to enable synchronous execution of the device in PyNative mode. Default: False. When the value is set to False, the operator is executed asynchronously on the device. When an error occurs in the execution of the operator, the specific error script code location cannot be located, when the value is set to True, the operator is executed synchronously on the device. It will reduce the execution performance of the program. At this time, when an error occurs in the execution of the operator, the location of the error script code can be located according to the call stack of the error.

  • mode (int) – Running in GRAPH_MODE(0) or PYNATIVE_MODE(1). Both modes support all backends. Default: PYNATIVE_MODE.

  • enable_graph_kernel (bool) – Whether to enable graph kernel fusion to optimize network execution performance. Default: False. Indicates whether to enable image-computing convergence to optimize network execution performance. If enable_graph_kernel is set to True, acceleration can be enabled. For details of graph kernel fusion, please check Enabling Graph Kernel Fusion.

  • graph_kernel_flags (str) –

    Optimization options of graph kernel fusion, and the priority is higher when it conflicts with enable_graph_kernel. Only for experienced users. For example, mindspore.set_context(graph_kernel_flags=”–opt_level=2 –dump_as_text”). Some general options:

    • opt_level: Set the optimization level. Default: 2. Graph kernel fusion can be enabled equivalently by setting opt_level greater than 0. Available values are:

      • 0: disables graph kernel fusion;

      • 1: enables the basic fusion of operators;

      • 2: includes all optimizations of level 1, and turns on more optimizations such as CSE, arithmetic simplification and so on;

      • 3: includes all optimizations of level 2, and turns on more optimizations such as SitchingFusion, ParallelFusion and so on. Optimizations of this level are radical and unstable in some scenarios. Be caution when using this level.

    • dump_as_text: dumps detail info as text files. Default: false.

    More options can refer to the implementation code.

  • enable_reduce_precision (bool) – Whether to enable precision reduction. If the operator does not support the user-specified precision, the precision will be changed automatically. Default: True.

  • auto_tune_mode (str) –

    The mode of auto tune when op building, get the best tiling performance. Default: NO_TUNE. The value must be in [‘RL’, ‘GA’, ‘RL,GA’].

    • RL: Reinforcement Learning tune.

    • GA: Genetic Algorithm tune.

    • RL,GA: When both RL and GA optimization are enabled, the tool automatically selects RL or GA based on different types of operators in the network model. The sequence of RL and GA is not differentiated. (Automatic selection).

    For more information about the enable operator tuning tool settings, please check Enable the operator optimization tool.

  • check_bprop (bool) – Whether to check back propagation nodes. The checking ensures that the shape and dtype of back propagation node outputs is the same as input parameters. Default: False.

  • max_call_depth (int) – Specify the maximum depth of function call. Must be positive integer. Default: 1000. The max_call_depth parameter needs to be set when the nested call is too deep or the number of subgraphs is too large. If max_call_depth is set larger than before, the system max stack depth should be set larger too, otherwise a core dumped exception may be raised because of system stack overflow.

  • grad_for_scalar (bool) – Whether to get gradient for scalar. Default: False. When grad_for_scalar is set to True, the function’s scalar input can be derived. The default value is False. Because the back-end does not support scaling operations currently, this interface only supports simple operations that can be deduced by the front-end.

  • enable_compile_cache (bool) – Whether to save or load the cache of the graph compiled by front-end. After enable_compile_cache is set to True, during the first execution, a hardware-independent compilation cache is generated and exported to a MINDIR file. When the network is executed again, if enable_compile_cache is still set to True and the network scripts are not changed, the compile cache is loaded. Note that only limited automatic detection for the changes of python scripts is supported by now, which means that there is a correctness risk. Default: False. This is an experimental prototype that is subject to change and/or deletion.

  • compile_cache_path (str) – Path to save the cache of the graph compiled by front-end. Default: “.”. If the specified directory does not exist, the system will automatically create the directory. The cache will be saved to the directory of compile_cache_path/rank_${rank_id}/. The rank_id is the ID of the current device in the cluster.

  • inter_op_parallel_num (int) – The thread number of op parallel at the same time. Default value is 0, which means use the default num.

  • runtime_num_threads (int) – The thread pool number of cpu kernel used in runtime, which must bigger than or equal to 0. Default value is 30, if you run many processes at the same time, you should set the value smaller to avoid thread contention.

  • disable_format_transform (bool) – Whether to disable the automatic format transform function from NCHW to NHWC. When the network training performance of fp16 is worse than fp32, disable_format_transform can be set to True to try to improve training performance. Default: False.

  • support_binary (bool) – Whether to support run .pyc or .so in graph mode. If want to support run .so or .pyc in graph mode, coulde set ‘support_binary’ to be True, and run once .py file. It would save the source of the interfaces would be compiled by MindSpore to the interfaces definition .py file that should be guaranteed to be writable. Then compile the .py file to the .pyc or .so file, and could run in Graph mode.

  • memory_optimize_level (str) –

    The memory optimize level. Default: O0. The value must be in [‘O0’, ‘O1’].

    • O0: priority performance option, disable SOMAS (Safe Optimized Memory Allocation Solver).

    • O1: priority memory option, enable SOMAS.

  • memory_offload (str) –

    Whether to enable the memory offload function. When it is enabled, the idle data will be temporarily copied to the host side in the case of insufficient device memory. The value must be in the range of [‘ON’, ‘OFF’], and the default value is ‘OFF’.

    • ON: Enable the memory Offload function. On Ascend hardware platform, this parameter does not take effect when the environment variable “GRAPH_OP_RUN=1” is not set; This parameter does not take effect when memory_optimize_level is set ‘O1’.

    • OFF: Turn off the memory Offload function.

Raises

ValueError – If input key is not an attribute in context.

Examples

>>> import mindspore as ms
>>> ms.set_context(mode=ms.PYNATIVE_MODE)
>>> ms.set_context(precompile_only=True)
>>> ms.set_context(device_target="Ascend")
>>> ms.set_context(device_id=0)
>>> ms.set_context(save_graphs=True, save_graphs_path="./model.ms")
>>> ms.set_context(enable_reduce_precision=True)
>>> ms.set_context(enable_graph_kernel=True)
>>> ms.set_context(graph_kernel_flags="--opt_level=2 --dump_as_text")
>>> ms.set_context(reserve_class_name_in_scope=True)
>>> ms.set_context(variable_memory_max_size="6GB")
>>> ms.set_context(check_bprop=True)
>>> ms.set_context(max_device_memory="3.5GB")
>>> ms.set_context(mempool_block_size="1GB")
>>> ms.set_context(print_file_path="print.pb")
>>> ms.set_context(max_call_depth=80)
>>> ms.set_context(env_config_path="./env_config.json")
>>> ms.set_context(auto_tune_mode="GA,RL")
>>> ms.set_context(grad_for_scalar=True)
>>> ms.set_context(enable_compile_cache=True, compile_cache_path="./cache.ms")
>>> ms.set_context(pynative_synchronize=True)
>>> ms.set_context(runtime_num_threads=10)
>>> ms.set_context(inter_op_parallel_num=4)
>>> ms.set_context(disable_format_transform=True)
>>> ms.set_context(memory_optimize_level='O0')
>>> ms.set_context(memory_offload='ON')