mindspore.context

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

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.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.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.

  • strategy_ckpt_load_file: “”.

  • strategy_ckpt_save_file: “”.

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

Set auto parallel context.

Note

Attribute name is required for setting attributes. If a program has tasks with different parallel modes, then before setting new parallel mode for next task, interface mindspore.context.reset_auto_parallel_context() needs to be called to reset the configuration.

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. “stand_alone” do not support mirror_mean. Default: False.

  • cast_before_mirror (bool) – Insert Mirror Op after the cast if this flag is True. “stand_alone”, “data_parallel” and “hybrid_parallel” do not support cast_before_mirror. 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.

  • auto_parallel_search_mode (str) –

    There are two kinds of search modes, “recursive_programming” and “dynamic_programming”. Default: “dynamic_programming”.

    • recursive_programming: Recursive programming search mode.

    • dynamic_programming: Dynamic programming search mode.

  • 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.

  • strategy_ckpt_load_file (str) – The path to load parallel strategy checkpoint. Default: ‘’

  • strategy_ckpt_save_file (str) – The path to save parallel strategy checkpoint. Default: ‘’

  • full_batch (bool) – Whether to load the whole batch on each device. 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)
>>> context.set_auto_parallel_context(strategy_ckpt_load_file="./strategy_stage1.ckpt")
>>> context.set_auto_parallel_context(strategy_ckpt_save_file="./strategy_stage1.ckpt")
mindspore.context.set_context(**kwargs)[source]

Sets 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.

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.

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

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

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

  • 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) – When the program is executed on Ascend, operators can dump data here. The root dump path is configured in /home/HwHiAiUser/ide_daemon/ide_daemon.cfg. So the real dump path is “{configured root dump path}/{save_dump_path}”. Default: “.”.

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

  • enable_profiling (bool) – Whether to open profiling. Default: False.

  • profiling_options (str) –

    Sets profiling collection options, operators can profiling data here. Profiling collection options, the values are as follows, supporting the collection of multiple data.

    • training_trace: collect iterative trajectory data, that is, the training task and software information of the AI software stack, to achieve performance analysis of the training task, focusing on data enhancement, forward and backward calculation, gradient aggregation update and other related data.

    • task_trace: collect task trajectory data, that is, the hardware information of the HWTS/AICore of the Ascend 910 processor, and analyze the information of start and end of the task.

    • op_trace: collect single operator performance data.

    The profiling can choose training_trace, task_trace, training_trace and task_trace combination and separated by colons; single operator can choose op_trace, op_trace cannot be combined with training_trace and task_trace. Default: “training_trace”.

  • check_bprop (bool) – Whether to check bprop. Default: False.

  • max_device_memory (str) – Sets the maximum memory available for device, currently only supported on GPU. The format is “xxGB”. Default: “1024GB”.

  • print_file_path (str) – The path of print data to save. If this parameter is set, print data is saved to a file by default, and turn off printing to the screen.

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_reduce_precision=True)
>>> context.set_context(save_ms_model=True, save_ms_model_path=".")
>>> context.set_context(enable_dump=True, save_dump_path=".")
>>> context.set_context(reserve_class_name_in_scope=True)
>>> 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")
>>> context.set_context(enable_profiling=True, profiling_options="training_trace")
>>> context.set_context(max_device_memory="3.5GB")
>>> context.set_context(print_file_path="print.pb")