mindspore.context

The context of mindspore, used to configure the current execution environment, including execution mode, execution backend and other feature switchs.

mindspore.context.set_context(**kwargs)[source]

Set context for running environment.

Context should be configured before running your program. If there is no configuration, the “Ascend” device target will be used by default. GRAPH_MODE or PYNATIVE_MODE can be set by mode attribute and both modes support all backends, default mode is PYNATIVE_MODE.

When the save_graphs attribute is set to 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. As for other configurations and arguments, please refer to the corresponding module description, the configuration is optional and can be enabled when needed.

Note

Attribute name is required for setting attributes. If need to config graph max memory size and variable max memory size, one must make sure:

The sum of graph_memory_max_size and variable_memory_max_size should be less than total memory size of a device, while the total memory is supposed to be no more than 256GB.

Parameters
  • mode (int) – Running in GRAPH_MODE(0) or PYNATIVE_MODE(1). Default: PYNATIVE_MODE.

  • device_target (str) – The target device to run, support “Ascend”, “GPU”, “CPU”. Default: “Ascend”.

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

  • enable_ir_fusion (bool) – Whether to enable ir fusion. Default: True.

  • save_graphs (bool) – Whether to save graphs. Default: False.

  • enable_hccl (bool) – Whether to enable hccl. Default: False.

  • enable_loop_sink (bool) – Whether to enable loop sink. Default: False.

  • enable_task_sink (bool) – Whether to enable task sink. Default: True.

  • enable_mem_reuse (bool) – Whether to enable memory reuse. Default: True.

  • save_ms_model (bool) – Whether to save model converted by graph. Default: False.

  • save_ms_model_path (str) – Path to save converted model. Default: “.”

  • enable_gpu_summary (bool) – Whether to enable gpu summary. Default: True.

  • save_graphs_path (str) – Path to save graphs. Default: “.”

  • enable_auto_mixed_precision (bool) – Whether to enable auto mixed precision. Default: True.

  • reserve_class_name_in_scope (bool) – Whether to save the network class name in the scope. Default: True.

  • enable_reduce_precision (bool) – Whether to enable precision reduction. Default: True.

  • enable_dump (bool) – Whether to enable dump. Default: False.

  • save_dump_path (str) – Set path to dump data. Default: “.”.

  • enable_dynamic_memory (bool) – Whether to enable dynamic memory. Default: False.

  • graph_memory_max_size (str) – Set graph memory max size. Default: “26GB”.

  • variable_memory_max_size (str) – Set variable memory max size. Default: “5GB”.

Raises

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

Examples

>>> context.set_context(mode=context.GRAPH_MODE)
>>> context.set_context(mode=context.PYNATIVE_MODE)
>>> context.set_context(device_target="Ascend")
>>> context.set_context(device_id=0)
>>> context.set_context(save_graphs=True, save_graphs_path="./model.ms")
>>> context.set_context(enable_task_sink=True)
>>> context.set_context(enable_mem_reuse=True)
>>> context.set_context(enable_reduce_precision=True)
>>> context.set_context(save_ms_model=True, save_ms_model_path=".")
>>> context.set_context(enable_gpu_summary=False)
>>> context.set_context(enable_dump=False, save_dump_path=".")
>>> context.set_context(reserve_class_name_in_scope=True)
>>> context.set_context(enable_dynamic_memory=True)
>>> context.set_context(graph_memory_max_size="25GB")
>>> context.set_context(variable_memory_max_size="6GB")
>>> context.set_context(mode=context.GRAPH_MODE,
>>>                     device_target="Ascend",device_id=0, save_graphs=True,
>>>                     save_graphs_path="/mindspore")
mindspore.context.get_context(attr_key)[source]

Gets context attribute value according to the input key.

Parameters

attr_key (str) – The key of the attribute.

Returns

Object, The value of given attribute key.

Raises

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

mindspore.context.set_auto_parallel_context(**kwargs)[source]

Set auto parallel context.

Note

Attribute name is required for setting attributes.

Parameters
  • device_num (int) – Available device number, the value must be in [1, 4096]. Default: 1.

  • global_rank (int) – Global rank id, the value must be in [0, 4095]. Default: 0.

  • mirror_mean (bool) – Whether to perform mean operator after all-reduce of mirror. Default: False.

  • cast_before_mirror (bool) – Insert Mirror Op after the cast if this flag is True. Default: True.

  • parallel_mode (str) –

    There are five kinds of parallel modes, “stand_alone”, “data_parallel”, “hybrid_parallel”, “semi_auto_parallel” and “auto_parallel”. Default: “stand_alone”.

    • stand_alone: Only one processor working.

    • data_parallel: Distributing the data across different processors.

    • hybrid_parallel: Achieving data parallelism and model parallelism manually.

    • semi_auto_parallel: Achieving data parallelism and model parallelism by setting parallel strategies.

    • auto_parallel: Achieving parallelism automatically.

  • parameter_broadcast (bool) – Indicating whether to broadcast parameters before training. “stand_alone”, “semi_auto_parallel” and “auto_parallel” do not support parameter broadcast. Default: False.

Raises

ValueError – If input key is not attribute in auto parallel context.

Examples

>>> context.set_auto_parallel_context(device_num=8)
>>> context.set_auto_parallel_context(global_rank=0)
>>> context.set_auto_parallel_context(mirror_mean=True)
>>> context.set_auto_parallel_context(cast_before_mirror=False)
>>> context.set_auto_parallel_context(parallel_mode="auto_parallel")
>>> context.set_auto_parallel_context(parameter_broadcast=False)
mindspore.context.get_auto_parallel_context(attr_key)[source]

Gets auto parallel context attribute value according to the key.

Parameters

attr_key (str) – The key of the attribute.

Returns

Returns attribute value according to the key.

Raises

ValueError – If input key is not attribute in auto parallel context.

mindspore.context.reset_auto_parallel_context()[source]

Reset auto parallel context attributes to the default values:

  • device_num: 1.

  • global_rank: 0.

  • mirror_mean: False.

  • cast_before_mirror: True.

  • parallel_mode: “stand_alone”.

  • parameter_broadcast: False.