未指定分布式策略时SageMaker多实例训练NCCL报错
SageMaker自定义分布式训练NCCL/OFI错误排查方案
问题背景
在SageMaker启动两台ml.g4dn.12xlarge实例,未指定distribution参数,采用自定义分布式配置:读取resourceConfig.json文件,使用NCCL后端初始化torch.distributed.init_process_group,执行张量广播操作时出现如下错误:
libfabric:164:1673312506:efa:cq:rxr_cq_write_tx_error():243<warn> rxr_cq_write_tx_error: err: 21, prov_err: Unknown error -21 (21)
algo-1:164:225 [0] ofi_process_cq:1033 NCCL WARN NET/OFI Request 0x7f4a6c335028 completed with error. RC: 21. Error: unknown error. Completed length: 0, Request: { buffer_index: 255, dev: 0, size: 0, state: CREATED, direction: SEND }
排查与解决步骤
1. 临时切换TCP通信后端验证
ml.g4dn实例默认启用EFA,但自定义分布式场景下可能缺少自动配置逻辑,先切换到TCP后端确认通信链路正常:
- 设置环境变量:在训练启动脚本或entrypoint开头添加:
export NCCL_NET_GDR_LEVEL=0 export NCCL_SOCKET_IFNAME=eth0 export NCCL_NET=socket - 显式指定TCP初始化方式:修改
init_process_group代码,确保基于resourceConfig的节点IP构建TCP地址:import json import torch.distributed as dist with open('/opt/ml/input/config/resourceconfig.json') as f: config = json.load(f) master_addr = config['hosts'][0] master_port = 29500 # 选择未被占用的端口 dist.init_process_group( backend='nccl', init_method=f'tcp://{master_addr}:{master_port}', rank=config['current_host_index'], world_size=len(config['hosts']) * 4 # ml.g4dn.12xlarge含4张GPU,按需调整 )
2. 验证resourceConfig读取正确性
- 在entrypoint中添加日志,确认节点信息读取无误:
print(f"Host list: {config['hosts']}, Current rank: {config['current_host_index']}, World size: {len(config['hosts'])}") - 在两台实例上分别执行
ping <对方私有IP>,确认网络连通性
3. 检查NCCL与PyTorch版本兼容性
- 执行NCCL基础通信测试:
# 先安装nccl-tests git clone https://github.com/NVIDIA/nccl-tests.git cd nccl-tests && make # 跨节点测试 mpirun -np 2 -H {host1_ip}:4,{host2_ip}:4 ./build/all_reduce_perf -b 8 -e 1G -f 2 -g 1 - 确保PyTorch版本与NCCL版本匹配(例如PyTorch 1.12+适配NCCL 2.10+)
4. 规范进程启动逻辑
- 确保每个实例启动的进程数与GPU数量匹配(ml.g4dn.12xlarge为4张GPU,单实例启动4个进程)
- 避免端口冲突:所有进程使用相同的master_port,且该端口未被其他服务占用
5. 若需保留EFA通信的配置修复
- 检查EFA驱动状态:
efa_info -v - 设置EFA专属环境变量:
export FI_EFA_USE_DEVICE_RDMA=1 export NCCL_NET_OFI_USE_CUDA=1 export LD_LIBRARY_PATH=/opt/amazon/efa/lib:$LD_LIBRARY_PATH export NCCL_DEBUG=INFO # 开启NCCL日志排查细节
内容的提问来源于stack exchange,提问作者Ridham Zalavadia
相关产品推荐
相关产品推荐

