Accelerate documentation
在 Intel CPU 上进行训练
在 Intel CPU 上进行训练
工作原理 用于 CPU 的训练优化
Accelerate 完全支持 Intel CPU,您只需通过配置启用即可。
场景 1:无分布式 CPU 训练的加速
在您的机器上运行 accelerate config:
$ accelerate config
-----------------------------------------------------------------------------------------------------------------------------------------------------------
In which compute environment are you running?
This machine
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Which type of machine are you using?
No distributed training
Do you want to run your training on CPU only (even if a GPU / Apple Silicon device is available)? [yes/NO]:yes
Do you wish to optimize your script with torch dynamo?[yes/NO]:NO
Do you want to use DeepSpeed? [yes/NO]: NO
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Do you wish to use FP16 or BF16 (mixed precision)?
bf16This will generate a config file that will be used automatically to properly set the default options when doing
accelerate launch my_script.py --args_to_my_script
例如,以下是如何运行 NLP 示例 examples/nlp_example.py(从仓库根目录)并使用由 accelerate config 生成的 default_config.yaml
compute_environment: LOCAL_MACHINE
distributed_type: 'NO'
downcast_bf16: 'no'
machine_rank: 0
main_training_function: main
mixed_precision: bf16
num_machines: 1
num_processes: 1
rdzv_backend: static
same_network: true
tpu_env: []
tpu_use_cluster: false
tpu_use_sudo: false
use_cpu: trueaccelerate launch examples/nlp_example.py
accelerator.prepare目前仅支持同时准备多个模型(且不包含优化器),或为训练准备单个模型-优化器对。其他尝试(例如两个模型-优化器对)将引发详细的错误。要绕过此限制,请考虑为每个模型-优化器对分别使用accelerator.prepare。
场景 2:分布式 CPU 训练加速 我们使用 Intel oneCCL 进行通信,并结合 Intel® MPI 库,在 Intel® 架构上提供灵活、高效且可扩展的集群消息传递。您可以参考 此处 获取安装指南
在您的机器(node0)上运行 accelerate config:
$ accelerate config
-----------------------------------------------------------------------------------------------------------------------------------------------------------
In which compute environment are you running?
This machine
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Which type of machine are you using?
multi-CPU
How many different machines will you use (use more than 1 for multi-node training)? [1]: 4
-----------------------------------------------------------------------------------------------------------------------------------------------------------
What is the rank of this machine?
0
What is the IP address of the machine that will host the main process? 36.112.23.24
What is the port you will use to communicate with the main process? 29500
Are all the machines on the same local network? Answer `no` if nodes are on the cloud and/or on different network hosts [YES/no]: yes
Do you want accelerate to launch mpirun? [yes/NO]: yes
Please enter the path to the hostfile to use with mpirun [~/hostfile]: ~/hostfile
Enter the number of oneCCL worker threads [1]: 1
Do you wish to optimize your script with torch dynamo?[yes/NO]:NO
How many processes should be used for distributed training? [1]:16
-----------------------------------------------------------------------------------------------------------------------------------------------------------
Do you wish to use FP16 or BF16 (mixed precision)?
bf16例如,以下是如何运行 NLP 示例 examples/nlp_example.py(从仓库根目录)以进行分布式 CPU 训练。
由 accelerate config 生成的 default_config.yaml
compute_environment: LOCAL_MACHINE
distributed_type: MULTI_CPU
downcast_bf16: 'no'
machine_rank: 0
main_process_ip: 36.112.23.24
main_process_port: 29500
main_training_function: main
mixed_precision: bf16
mpirun_config:
mpirun_hostfile: /home/user/hostfile
num_machines: 4
num_processes: 16
rdzv_backend: static
same_network: true
tpu_env: []
tpu_use_cluster: false
tpu_use_sudo: false
use_cpu: true设置以下环境变量,并使用 intel MPI 启动训练
在 node0 中,你需要创建一个包含每个节点 IP 地址的配置文件(例如 hostfile),并将该配置文件的路径作为参数传入。
如果你选择让 Accelerate 启动 mpirun,请确保你的 hostfile 位置与配置中的路径一致。
$ cat hostfile
xxx.xxx.xxx.xxx #node0 ip
xxx.xxx.xxx.xxx #node1 ip
xxx.xxx.xxx.xxx #node2 ip
xxx.xxx.xxx.xxx #node3 ipaccelerate launch examples/nlp_example.py
您也可以直接使用 mpirun 命令启动分布式训练,需要在 node0 上运行以下命令,这样 16DDP 将在 node0、node1、node2、node3 上启用,并使用 BF16 混合精度。使用此方法时,用于多 CPU 训练的 Python 脚本、Python 环境和 accelerate 配置文件必须在所有机器上可用。
export MASTER_ADDR=xxx.xxx.xxx.xxx #node0 ip
mpirun -f hostfile -n 16 -ppn 4 accelerate launch examples/nlp_example.py