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
deterministic
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
CPU/GPU/Ascend
debug_level
CPU/GPU/Ascend
Executive Control
mode
CPU/GPU/Ascend
enable_graph_kernel
Ascend/GPU
graph_kernel_flags
Ascend/GPU
enable_reduce_precision
Ascend
aoe_tune_mode
Ascend
aoe_config
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
ascend_config
Ascend
jit_syntax_level
CPU/GPU/Ascend
gpu_config
GPU
- 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. ‘max_device_memory’ should be set before the program runs.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 or GRAPH_OP_RUN=1 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. The defaults for AI Core and AICPU operators vary on different hardware. For more information, please refer to Ascend Community. Default:
900
.Whether to save intermediate compilation graphs. Default:
0
. Available values are:False or 0: disable saving of intermediate compilation graphs.
1: some intermediate files will be generated during graph compilation.
True or 2: Generate more ir files related to backend process.
3: Generate visualization computing graphs and detailed frontend ir graphs.
When the save_graphs attribute is set as
True
,1
,2
or3
, 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.deterministic (str) –
Whether to enable op run in deterministic mode. The value must be in the range of [‘ON’, ‘OFF’], and the default value is
'OFF'
.”ON”: Enable operator deterministic running mode.
”OFF”: Disable operator deterministic running mode.
When deterministic mode is on, model ops will be deterministic in Ascend. This means that if op run multiple times with the same inputs on the same hardware, it will have the exact same outputs each time. This is useful for debugging models.
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 tofalse
, 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 to2
, 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 toFalse
, the memory reuse function is turned off.
precompile_only (bool) – Whether to only precompile the network. Default:
False
. If set toTrue
, 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 toTrue
, 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 toFalse
, 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 toTrue
, 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 toTrue
, 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
.
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
.aoe_tune_mode (str) – AOE tuning mode setting, which is not set by default. When set to
"online"
, the tuning in online function is turned on. When set to"offline"
, ge graph will be save for offline tuning.aoe_config (dict) –
Set the parameters specific to Ascend Optimization Engine. It is not set by default.
job_type (str): Mode type setting, default value is
"2"
."1"
: subgraph tuning;"2"
: operator tuning.
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 toTrue
, the function’s scalar input can be derived. The default value isFalse
. 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 toTrue
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 compile cache. 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. On Ascend hardware platform, default:
O1
, on other hardware platforms, default:O0
. The value must be in [‘O0’, ‘O1’].O0: priority performance option, disable SOMAS (Safe Optimized Memory Allocation Solver) and some other memory optimizations.
O1: priority memory option, enable SOMAS and some other memory optimizations.
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 graph compilation level is not ‘O0’; This parameter does not take effect when memory_optimize_level is set ‘O1’.
OFF: Turn off the memory Offload function.
ascend_config (dict) –
Set the parameters specific to Ascend hardware platform. It is not set by default. The default value of precision_mode, jit_compile and atomic_clean_policy are experimental parameters, may change in the future.
precision_mode (str): Mixed precision mode setting, and the default value of inference network is
force_fp16
. The value range is as follows:force_fp16: When the operator supports both float16 and float32, select float16 directly.
allow_fp32_to_fp16: For cube operators, use the float16. For vector operators, prefer to keep the origin dtype, if the operator in model can support float32, it will keep original dtype, otherwise it will reduce to float16.
allow_mix_precision: Automatic mixing precision, facing the whole network operator, according to the built-in optimization strategy, automatically reduces the precision of some operators to float16 or bfloat16.
must_keep_origin_dtype: Keep the accuracy of the original drawing.
force_fp32: When the input of the matrix calculation operator is float16 and the output supports float16 and float32, output is forced to float32.
allow_fp32_to_bf16: For cube operators, use the bfloat16. For vector operators, prefer to keep the origin dtype, if the operator in model can support float32, it will keep original dtype, otherwise it will reduce to bfloat16.
allow_mix_precision_fp16: Automatic mixing precision, facing the whole network operator, automatically reduces the precision of some operators to float16 according to the built-in optimization strategy.
allow_mix_precision_bf16: Automatic mixing precision, facing the whole network operator, according to the built-in optimization strategy, automatically reduces the precision of some operators to bfloat16.
jit_compile (bool): Whether to select online compilation. When set to ‘True’, online compilation is prioritized. When set to ‘False’, compiled operator binary files are prioritized to improve compilation performance. The default settings are online compilation for static shape, and compiled operator binary files for dynamic shape.
atomic_clean_policy (int): The policy for cleaning memory occupied by atomic operators in the network. Default:
1
.0: The memory occupied by all atomic operators in the network is cleaned centrally.
1: Memory is not cleaned centrally and each atomic operator in the network is cleaned separately. When the memory of the network exceeds the limit, you may try this cleaning policy, but it may cause performance loss.
matmul_allow_hf32 (bool): Whether to convert FP32 to HF32 for Matmul operators. Default value:
False
. This is an experimental prototype that is subject to change and/or deletion. For detailed information, please refer to Ascend community .conv_allow_hf32 (bool): Whether to convert FP32 to HF32 for Conv operators. Default value:
True
. This is an experimental prototype that is subject to change and/or deletion. For detailed information, please refer to Ascend community .exception_dump (str): Enable exception dump for Ascend operators, providing the input and output data for failing Ascend operators. The value can be
"0"
,"1"
and"2"
. For"0"
, exception dump is turned off; for"1"
, all inputs and outputs will be dumped for AICore and AICPU exception operators; for"2"
, inputs will be dumped for AICore exception operators. Default:"2"
.op_precision_mode (str): Path to config file of op precision mode. For detailed information, please refer to Ascend community .
ge_options (dict): Set options for CANN. The options are divided into two categories: global and session. This is an experimental prototype that is subject to change and/or deletion. For detailed information, please refer to Ascend community . The configuration options in ge_options may be duplicated with the options in ascend_config. If the same configuration options are set in both ascend_config and ge_options, the one set in ge_options shall prevail.
global (dict): Set global options.
session (dict): Set session options.
parallel_speed_up_json_path(Union[str, None]): The path to the parallel speed up json file, configuration can refer to parallel_speed_up.json . If its value is None or ‘’, it does not take effect. Default None.
recompute_comm_overlap (bool): Enable overlap between recompute ops and communication ops if True. Default: False.
matmul_grad_comm_overlap (bool): Enable overlap between dw matmul and tensor parallel communication ops if True. Default: False.
enable_task_opt (bool): Enable the optimization of the number of tasks for each communication if True. Default: False.
enable_grad_comm_opt (bool): Enable overlap between dx ops and data parallel communication ops if True. Currently, do not support LazyInline <https://www.mindspore.cn/docs/en/master/api_python/mindspore/mindspore.lazy_inline.html> Default: False.
enable_opt_shard_comm_opt (bool): Enable overlap between forward ops and optimizer parallel allgather communication if True. Currently, do not support LazyInline <https://www.mindspore.cn/docs/en/master/api_python/mindspore/mindspore.lazy_inline.html> Default: False.
compute_communicate_fusion_level (int): Enable the fusion between compute and communicate. Default:
0
.0: Disable fusion.
1: Apply fusion to forward nodes.
2: Apply fusion to backward nodes.
3: Apply fusion to all nodes.
host_scheduling_max_threshold(int): The max threshold to control whether the dynamic shape process is used when run the static graph, the default value is 0. When the number of operations in the static graph is less than the max threshold, this graph will be executed in dynamic shape process. In large model scenarios, this approach can save stream resources. If the number of operations in the static graph is greater than the maximum threshold, this graph will be executed in original static process.
jit_syntax_level (int) –
Set JIT syntax level for graph compiling, triggered by GRAPH_MODE and @jit decorator. The value must be
STRICT
orLAX
. Default:LAX
. All levels support all backends.STRICT
: Only basic syntax is supported, and execution performance is optimal. Can be used for MindIR load and export.LAX
: Compatible with all Python syntax as much as possible. However, execution performance may be affected and not optimal. Cannot be used for MindIR load and export due to some syntax that may not be able to be exported.
debug_level (int) –
Set config for debugging. Default value:
RELEASE
.RELEASE
: Used for normally running, and some debug information will be discard to get a better compiling performance.DEBUG
: Used for debugging when errors occur, more information will be record in compiling process.
gpu_config (dict) –
Set the parameters specific to gpu hardware platform. It is not set by default. Currently, only setting conv_fprop_algo and conv_dgrad_algo and conv_wgrad_algo and conv_allow_tf32 and matmul_allow_tf32 are supported on GPU hardware platform.
conv_fprop_algo (str): Specifies convolution forward algorithm and the default value is ‘normal’, The value range is as follows:
normal: Use the heuristic search algorithm.
performance: Use the trial search algorithm.
implicit_gemm: This algorithm expresses the convolution as a matrix product without actually explicitly forming the matrix that holds the input tensor data.
implicit_precomp_gemm: This algorithm expresses convolution as a matrix product without actually explicitly forming the matrix that holds the input tensor data, but still needs some memory workspace to precompute some indices in order to facilitate the implicit construction of the matrix that holds the input tensor data.
gemm: This algorithm expresses the convolution as an explicit matrix product. A significant memory workspace is needed to store the matrix that holds the input tensor data.
direct: This algorithm expresses the convolution as a direct convolution (for example, without implicitly or explicitly doing a matrix multiplication).
fft: This algorithm uses the Fast-Fourier Transform approach to compute the convolution. A significant memory workspace is needed to store intermediate results.
fft_tiling: This algorithm uses the Fast-Fourier Transform approach but splits the inputs into tiles. A significant memory workspace is needed to store intermediate results but less than fft algorithm for large size images.
winograd: This algorithm uses the Winograd Transform approach to compute the convolution. A reasonably sized workspace is needed to store intermediate results.
winograd_nonfused: This algorithm uses the Winograd Transform approach to compute the convolution. A significant workspace may be needed to store intermediate results.
conv_dgrad_algo (str): Specifies convolution data grad algorithm and the default value is ‘normal’, The value range is as follows:
normal: Use the heuristic search algorithm.
performance: Use the trial search algorithm.
algo_0: This algorithm expresses the convolution as a sum of matrix products without actually explicitly forming the matrix that holds the input tensor data. The sum is done using the atomic add operation, thus the results are non-deterministic.
algo_1: This algorithm expresses the convolution as a matrix product without actually explicitly forming the matrix that holds the input tensor data. The results are deterministic.
fft: This algorithm uses a Fast-Fourier Transform approach to compute the convolution. A significant memory workspace is needed to store intermediate results. The results are deterministic.
fft_tiling: This algorithm uses the Fast-Fourier Transform approach but splits the inputs into tiles. A significant memory workspace is needed to store intermediate results but less than fft for large size images. The results are deterministic.
winograd: This algorithm uses the Winograd Transform approach to compute the convolution. A reasonably sized workspace is needed to store intermediate results. The results are deterministic.
winograd_nonfused: This algorithm uses the Winograd Transform approach to compute the convolution. A significant workspace may be needed to store intermediate results. The results are deterministic.
conv_wgrad_algo (str): Specifies convolution filter grad algorithm and the default value is ‘normal’, The value range is as follows:
normal: Use the heuristic search algorithm.
performance: Use the trial search algorithm.
algo_0: This algorithm expresses the convolution as a sum of matrix products without actually explicitly forming the matrix that holds the input tensor data. The sum is done using the atomic add operation, thus the results are non-deterministic.
algo_1: This algorithm expresses the convolution as a matrix product without actually explicitly forming the matrix that holds the input tensor data. The results are deterministic.
fft: This algorithm uses a Fast-Fourier Transform approach to compute the convolution. A significant memory workspace is needed to store intermediate results. The results are deterministic.
algo_3: This algorithm is similar to algo_0 but uses some small workspace to precompute some indices. The results are also non-deterministic.
winograd_nonfused: This algorithm uses the Winograd Transform approach to compute the convolution. A significant workspace may be needed to store intermediate results. The results are deterministic.
fft_tiling: This algorithm uses the Fast-Fourier Transform approach but splits the inputs into tiles. A significant memory workspace is needed to store intermediate results but less than fft for large size images. The results are deterministic.
conv_allow_tf32 (bool): The flag below controls to allow Tensor core TF32 computation on CUDNN and the default value is
True
.matmul_allow_tf32 (bool): The flag below controls to allow Tensor core TF32 computation on CUBLAS and the default value is
False
.
- 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(aoe_tune_mode="online") >>> ms.set_context(aoe_config={"job_type": "2"}) >>> 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(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') >>> ms.set_context(deterministic='ON') >>> ms.set_context(ascend_config={"precision_mode": "force_fp16", "jit_compile": True, ... "atomic_clean_policy": 1, "op_precision_mode": "./op_precision_config_file", ... "ge_options": {"global": {"ge.opSelectImplmode": "high_precision"}, ... "session": {"ge.exec.atomicCleanPolicy": "0"}}}) >>> ms.set_context(jit_syntax_level=ms.STRICT) >>> ms.set_context(debug_level=ms.DEBUG) >>> ms.set_context(gpu_config={"conv_fprop_algo": "performance", "conv_allow_tf32": True, ... "matmul_allow_tf32": True})