当前,随着大模型参数规模的不断扩大,多机多卡分布式训练的重要性日益凸显。本指南旨在依托华为昇腾计算卡,提供一个双机16卡训练的详尽示例,更好地应对大模型的训练挑战。本次指南主要讲解如何准备双机训练的环境,以及如何通过accelerate实现双机多卡训练。
一、 环境及配置
本次指南使用的依赖如下:
依赖 | 版本 |
openmind | 最新版本 |
openmind-hub | 最新版本 |
accelerate | openmind依赖版本 |
transformers | openmind依赖版本 |
相关安装可查看指南openMind Library安装:https://modelers.cn/docs/zh/openmind-library/install.html
二、权重,数据集和微调脚本
本次指南以如下权重,数据集和脚本作为示例:
类别 | 链接 | |
权重 | PyTorch-NPU/open_llama_7b | https://modelers.cn/models/PyTorch-NPU/open_llama_7b |
数据集 | alpaca_data | https://github.com/tatsu-lab/stanford_alpaca/blob/main/alpaca_data.json |
微调脚本 | train_sft.py | https://modelers.cn/models/PyTorch-NPU/open_llama_7b/blob/main/examples/train_sft.py |
三、双机配置
● 请在运行多机多卡之前,确认以下环境已经准备就绪:
● 双机是同一网络集群。
● 数据、权重和代码相同,且在双机上处于相同路径下。
● 双机互相免密登录已配置。
● 双机卡间通信正常。
设置双机互相免密登录
● 假设目前存在服务器xx.xx.xx.149和xx.xx.xx.151, 首先在149上生成SSH公钥和私钥。
ssh-keygen -t rsa
● 将149产生的私钥复制到151对应路径下。
scp root@xx.xx.xx.149: .ssh/id_rsa.pub ./ssh/authorized_keys
● 验证在151是否可以免密登录149。
#不需要密码直接成功
ssh root@xx.xx.xx.149
● 对149免密登录151进行同样操作。
配置双机卡间通信
可参考准备多机多卡训练配置双机卡间通信情况:https://www.hiascend.com/document/detail/zh/CANNCommunityEdition/80RC1alpha003/devguide/moddevg/ptmigr/AImpug_0026.html
四、配置Accelerate
可以通过修改accelerate的配置完成双机运行的部署,以下为配置文件config.yaml内容, 请注意修改其中的main_process_ip,num_machines,num_processes:
compute_environment: LOCAL_MACHINE
debug: false
distributed_type: MULTI_NPU
downcast_bf16: 'no'
gpu_ids: all
machine_rank: 0
main_process_ip: xx.xx.xx.149 # 主节点IP,用户需结合实际情况进行配置
main_process_port: xxxx # 主节点端口,用户需结合实际情况进行配置
main_training_function: main
mixed_precision: bf16
num_machines: 2 # 多节点个数
num_processes: 16 # 多节点个数 * 各节点卡数
rdzv_backend: static
same_network: true
tpu_env: []
tpu_use_cluster: false
tpu_use_sudo: false
use_cpu: false
五、启动双机多卡训练
运行下面的命令启动双机多卡脚本:
if [ -d ./test/output ];then
rm -rf ./test/output
mkdir -p ./test/output
else
mkdir -p ./test/output
fi
accelerate launch --config_file config.yaml train_sft.py \
--model_name_or_path "open_llama_7b" \
--data_path ./alpaca_data.json \
--bf16 True \
--output_dir ./test/output \
--max_steps 200 \
--per_device_train_batch_size 1 \
--evaluation_strategy "no" \
--save_strategy "steps" \
--save_steps 2000 \
--save_total_limit 1 \
--learning_rate 2e-5 \
--weight_decay 0. \
--warmup_ratio 0.03 \
--lr_scheduler_type "cosine" \
--fsdp "full_shard auto_wrap" \
--fsdp_transformer_layer_cls_to_wrap 'LlamaDecoderLayer' \
--logging_steps 1 > ./test/output/train.log 2>&1 &
六、结语
本次实践是在魔乐社区进行,朋友们可以试试,也欢迎分享你们的经验,一起交流:https://modelers.cn/