mindformers.core.set_context

View Source On Gitee
mindformers.core.set_context(run_mode=None, **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. Currently only run_mode belongs to MindFormers context. The kwargs will be passed to MindSpore set_context. The determination computation for training or inference can be controlled through keyword argument. The keyword arguments for enabling/disabling determination computation during training and inference are: train_precision_sync and infer_precision_sync, respectively. The on/off states correspond to boolean values, where True indicates activation and False indicates deactivation. This operation is a one-time action. Repeated attempts will not succeed and will trigger warning logs.

Parameters
  • run_mode (str, optional) – The mode of the model behaviour. Can be one of ['train', 'finetune', 'eval', 'predict']. Default: None.

  • **kwargs – MindSpore context arguments.

Examples

>>> from mindformers import build_context, set_context
>>> config = {'context': {'mode': 'GRAPH_MODE'}, 'parallel':{}}
>>> build_context(config=config)
>>> set_context(max_device_memory='59GB')
>>> set_context(run_mode='predict', infer_precision_sync=True)
WARNING - 'CUSTOM_MATMUL_SHUFFLE' is set to 'off' because infer_precision_sync is True.
WARNING - 'LCCL_DETERMINISTIC' is set to '1' because infer_precision_sync is True.
>>> set_context(run_mode='predict', infer_precision_sync=True)
WARNING - mindspore.set_deterministic has been set, can not be set repeatedly.
Key environment variables: HCCL_DETERMINISTIC: true, TE_PARALLEL_COMPILER: 1,
CUSTOM_MATMUL_SHUFFLE: off, LCCL_DETERMINISTIC: 1