1.首先查看自己电脑上的网卡类型,使用如下命令:
lspci | grep -i ethernet
输出如下:
31:00.0 Ethernet controller: Intel Corporation 82599ES 10-Gigabit SFI/SFP+ Network Connection (rev 01)
31:00.1 Ethernet controller: Intel Corporation 82599ES 10-Gigabit SFI/SFP+ Network Connection (rev 01)
4b:00.0 Ethernet controller: Intel Corporation I350 Gigabit Network Connection (rev 01)
4b:00.1 Ethernet controller: Intel Corporation I350 Gigabit Network Connection (rev 01)
由于服务器上面没有Mellanox网卡,那就不能使用InfiniBand进行通信,所以只能使用TCP/IP进行通信,所以需要手动禁用NCCL的IB(InfiniBand)支持,并指定用TCP/IP进行通信:
export NCCL_IB_DISABLE=1
你现在的目标是在三台服务器上使用 DeepSpeed 进行多机多卡的 BERT 微调。你已经有了模型和数据,接下来的步骤包括配置 DeepSpeed 环境、设置分布式训练、启动多机训练任务。以下是具体步骤:
1. 确保环境准备
在所有服务器上安装 DeepSpeed 及其依赖:
pip install deepspeed transformers torch
此外,还需要 torch.distributed
进行多机通信,建议安装 torch
版本匹配你的 GPU:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
检查 NCCL
版本:
nccl-tests/build/all_reduce_perf -b 8 -e 256M -f 2 -g 1
2. 确保各个服务器之间可以互相通信
DeepSpeed 依赖 torch.distributed
进行多机多卡训练,需要所有服务器之间能够无密码 SSH 登录。
(1)配置 SSH 免密
在主节点(rank 0)上执行:
ssh-keygen -t rsa # 生成密钥
ssh-copy-id user@node1 # 复制到其他节点
ssh-copy-id user@node2
并在每个节点上 ssh node1
进行测试,确保可以无密码登录。
(2)检查主机名解析
确保 /etc/hosts
里包含所有服务器的 IP 和主机名:
192.168.1.1 node0
192.168.1.2 node1
192.168.1.3 node2
也可以使用 hostfile
配置,格式如下:
node0 slots=2
node1 slots=2
node2 slots=2
(3)检查 NCCL 通信
在所有服务器上执行:
export NCCL_DEBUG=INFO
export NCCL_SOCKET_IFNAME=eth0 # 确保这里使用的是正确的网卡
export NCCL_IB_DISABLE=1 # 如果没有 InfiniBand 设备
export NCCL_P2P_DISABLE=1
然后运行:
python -m torch.distributed.run --nnodes=3 --nproc_per_node=2 --rdzv_backend=c10d --rdzv_endpoint=node0:29500 test_nccl.py
如果 NCCL 通信正常,就可以继续。
3. 配置 DeepSpeed
创建 ds_config.json
:
{
"train_batch_size": 64,
"train_micro_batch_size_per_gpu": 8,
"zero_optimization": {
"stage": 1
},
"fp16": {
"enabled": true
},
"gradient_accumulation_steps": 2
}
如果你的模型比较大,可以尝试 zero_optimization.stage=2
或 stage=3
来减少 GPU 显存占用。
4. 启动分布式训练
假设你有 train.py
作为训练脚本,你可以使用 DeepSpeed 启动训练:
方法 1:手动指定节点
deepspeed --hostfile=hostfile train.py --deepspeed ds_config.json
或直接指定:
deepspeed --master_addr=node0 --master_port=29500 --num_nodes 3 --num_gpus 2 train.py --deepspeed ds_config.json
方法 2:使用 PyTorch 的 torch.distributed.launch
python -m torch.distributed.launch --nproc_per_node=2 --nnodes=3 --node_rank=0 --master_addr="node0" --master_port=29500 train.py
在 node1
和 node2
上,将 node_rank
改为 1
和 2
。
5. 训练脚本修改
在 train.py
中,添加 DeepSpeed
初始化:
import deepspeed
import torch
import torch.distributed as dist
def setup():
dist.init_process_group(backend="nccl")
def main():
setup()
model = ... # 加载BERT模型
model, optimizer, _, _ = deepspeed.initialize(
model=model,
model_parameters=model.parameters(),
config="ds_config.json"
)
train_dataloader = ...
for epoch in range(num_epochs):
for batch in train_dataloader:
loss = model(batch)
model.backward(loss)
model.step()
if __name__ == "__main__":
main()
6. 监控训练
DeepSpeed 训练时,可以使用 NVIDIA-SMI
监控 GPU 使用情况:
watch -n 1 nvidia-smi
或者用 tensorboard
记录训练日志:
tensorboard --logdir runs/
7. 常见问题
(1)训练过程中 NCCL 超时
尝试:
export NCCL_DEBUG=INFO
export NCCL_SOCKET_IFNAME=eth0
export NCCL_IB_DISABLE=1
export NCCL_P2P_DISABLE=1
并确保 eth0
是正确的网卡。
(2)DeepSpeed 启动时报错
如果报 RuntimeError: Address already in use
,确保 29500
端口没有被占用:
netstat -tulnp | grep 29500
如果被占用,换一个端口。
(3)训练速度慢
- 开启
fp16
训练 - 调整
gradient_accumulation_steps
- 使用
ZeRO Stage 2/3
以减少显存占用
总结
- 确保所有服务器可以 SSH 免密登录,并正确解析主机名
- 配置
DeepSpeed
,确保ds_config.json
设置正确 - 启动训练时使用
deepspeed
或torch.distributed.launch
- 监控
NCCL
通信,避免网络或端口问题 - 调整
ZeRO
优化、fp16
训练,以提升训练效率
这样,你就可以在三台服务器上顺利进行 BERT 多机多卡微调了!你目前进展到哪一步了?