mindspore.context

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

  • mindspore.context.setcontext(**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. GRAPHMODE orPYNATIVE_MODE can be set by _mode attribute and both modes support all backends, defaultmode is PYNATIVE_MODE.

When the save_graphs attribute is set to True, attribute of save_graphs_path is used to set theintermediate 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 moduledescription, 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

  1. Copy>>> context.set_context(mode=context.GRAPH_MODE)
  2. >>> context.set_context(mode=context.PYNATIVE_MODE)
  3. >>> context.set_context(device_target="Ascend")
  4. >>> context.set_context(device_id=0)
  5. >>> context.set_context(save_graphs=True, save_graphs_path="./model.ms")
  6. >>> context.set_context(enable_task_sink=True)
  7. >>> context.set_context(enable_mem_reuse=True)
  8. >>> context.set_context(enable_reduce_precision=True)
  9. >>> context.set_context(save_ms_model=True, save_ms_model_path=".")
  10. >>> context.set_context(enable_gpu_summary=False)
  11. >>> context.set_context(enable_dump=False, save_dump_path=".")
  12. >>> context.set_context(reserve_class_name_in_scope=True)
  13. >>> context.set_context(enable_dynamic_memory=True)
  14. >>> context.set_context(graph_memory_max_size="25GB")
  15. >>> context.set_context(variable_memory_max_size="6GB")
  16. >>> context.set_context(mode=context.GRAPH_MODE,
  17. >>> device_target="Ascend",device_id=0, save_graphs=True,
  18. >>> save_graphs_path="/mindspore")
  • mindspore.context.getcontext(_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.setauto_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”.

  1. -

stand_alone: Only one processor working.

  1. -

data_parallel: Distributing the data across different processors.

  1. -

hybrid_parallel: Achieving data parallelism and model parallelism manually.

  1. -

semi_auto_parallel: Achieving data parallelism and model parallelism bysetting parallel strategies.

  1. -

auto_parallel: Achieving parallelism automatically.

  1. -

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

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

Examples

  1. Copy>>> context.set_auto_parallel_context(device_num=8)
  2. >>> context.set_auto_parallel_context(global_rank=0)
  3. >>> context.set_auto_parallel_context(mirror_mean=True)
  4. >>> context.set_auto_parallel_context(cast_before_mirror=False)
  5. >>> context.set_auto_parallel_context(parallel_mode="auto_parallel")
  6. >>> context.set_auto_parallel_context(parameter_broadcast=False)
  • mindspore.context.getauto_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.