mindspore.runtime.set_cpu_affinity

View Source On Gitee
mindspore.runtime.set_cpu_affinity(enable_affinity, affinity_cpu_list=None, module_to_cpu_dict=None)[source]

Enable thread-level core binding to allocate specific CPU cores for key MindSpore modules (main thread, pynative, runtime, and minddata), preventing performance instability caused by CPU core contention among MindSpore threads.

Note

  • Flexible Core Binding Configuration:

    1. When affinity_cpu_list is not specified, the process automatically determines the CPU affinity range based on available CPU cores, NUMA nodes, and device resources in the environment.

    2. When affinity_cpu_list is specified, the process manually binds to the CPU range defined in affinity_cpu_list.

    3. When module_to_cpu_dict is not specified, the default bind-core strategy assigns the CPU cores to the "main" module.

    4. When module_to_cpu_dict is specified, the process manually binds each module to CPU ranges as defined in module_to_cpu_dict.

  • The automated bind-core strategy generation scenario invokes system commands to obtain CPU, NUMA node, and device resources on the environment, and some commands cannot be executed successfully due to environment differences; the automated bind-core strategy generated will vary according to the resources available on the environment:

    1. cat /sys/fs/cgroup/cpuset/cpuset.cpus, to obtain the available CPU resources on the environment; if the execution of this command fails, the bind-core function will not take effect.

    2. npu-smi info -m, get the available NPU resources on the environment; if the execution of this command fails, the bind-core strategy will be generated only based on the available CPU resources, without considering the device affinity.

    3. npu-smi info -t board -i {NPU_ID} -c {CHIP_ID}, get NPU details based on the logical ID of the device; if the execution of this command fails, the bind-core strategy is generated based on the available CPU resources only, regardless of device affinity.

    4. lspci -s {PCIe_No} -vvv, get the hardware information of the device on the environment; if the execution of this command fails, the bind-core strategy is generated only based on the available CPU resources, without considering the device affinity.

    5. lscpu, get information about CPUs and NUMA nodes on the environment; if the execution of this command fails, only the available CPU resources are used to generate the bind-core strategy, without considering the device affinity.

Parameters
  • enable_affinity (bool) – Enables/disables thread-level core binding.

  • affinity_cpu_list (list, optional) – Manually specifies the CPU affinity range for the process. Format: ["cpuidX-cpuidY"] (e.g., ["0-3", "8-11"]). Default: None (uses auto-generated binding strategy based on system resources). Passing an empty list [] behaves the same as None.

  • module_to_cpu_dict (dict, optional) – Customizes core binding for specific modules. Valid keys (module names) are "main", "runtime", "pynative", "minddata". Valid value is a list of int indices representing CPU cores (e.g., {"main": [0,1], "minddata": [6,7]}). Default: None (automatically binds core for module "main"). Passing an empty dict {} behaves the same as None.

Raises
  • TypeError – The enable_affinity parameter is not a boolean.

  • TypeError – The affinity_cpu_list parameter is neither a list nor None.

  • TypeError – An element in affinity_cpu_list is not a string.

  • ValueError – An element in affinity_cpu_list does not follow the ["cpuidX-cpuidY"] format.

  • TypeError – The module_to_cpu_dict parameter is neither a dictionary nor None.

  • TypeError – A key in module_to_cpu_dict is not a string.

  • TypeError – A value in module_to_cpu_dict is not a list.

  • ValueError – An element in module_to_cpu_dict values is not a non-negative integer.

  • RuntimeError – In custom core binding scenarios, the specified CPU cores for a device are unavailable in the environment.

  • RuntimeError – The mindspore.runtime.set_cpu_affinity API is called repeatedly.

Examples

>>> import mindspore as ms
>>> ms.set_device("Ascend", 1)
>>> ms.runtime.set_cpu_affinity(True)
>>>
>>> import mindspore as ms
>>> ms.set_device("Ascend", 1)
>>> ms.runtime.set_cpu_affinity(True, ["10-19", "23-40"])
>>>
>>> import mindspore as ms
>>> ms.set_device("Ascend", 1)
>>> ms.runtime.set_cpu_affinity(True, ["10-19", "23-40"], {"main": [0,1,2,3], "runtime": [4,5,6]})