在当今人工智能快速发展的时代,RockyLinux分布式训练已成为提升模型训练效率的关键技术。本文将面向初学者,详细讲解如何在 Rocky Linux 操作系统上搭建一套完整的深度学习环境搭建流程,并实现多节点间的分布式训练。即使你是 Linux 新手,也能轻松跟做!

你需要至少两台安装了 Rocky Linux 8 或 9 的服务器(物理机或虚拟机均可),并确保以下条件:
sudo dnf update -ysudo dnf install -y epel-releasesudo dnf install -y git wget htop vim net-tools openssh-server首先添加 NVIDIA 官方仓库:
sudo dnf config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/rhel8/x86_64/cuda-rhel8.reposudo dnf module install -y nvidia-driver:latest-dkmssudo dnf install -y cuda-toolkit-12-3重启后验证驱动是否加载:
nvidia-smiwget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.shbash Miniconda3-latest-Linux-x86_64.sh -b -p $HOME/miniconda3source ~/.bashrcconda init bash假设你的主节点 IP 为 192.168.1.10,从节点为 192.168.1.11 和 192.168.1.12。
在主节点生成 SSH 密钥:
ssh-keygen -t rsa -b 4096 -N "" -f ~/.ssh/id_rsa将公钥复制到所有从节点:
ssh-copy-id user@192.168.1.11ssh-copy-id user@192.168.1.12测试免密登录:
ssh user@192.168.1.11 'hostname'在所有节点创建统一的 Conda 环境:
conda create -n dist_train python=3.10 -yconda activate dist_trainpip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121pip install opencv-python numpy pandas创建一个简单的 train.py 文件,使用 PyTorch 的 torch.distributed 模块:
import torchimport torch.distributed as distimport osdef setup(): dist.init_process_group("nccl") rank = dist.get_rank() print(f"Rank {rank} initialized.") return rankdef cleanup(): dist.destroy_process_group()if __name__ == "__main__": rank = setup() # 示例:每个 GPU 创建一个张量并进行 all-reduce tensor = torch.ones(2, 2).cuda() * rank print(f"Rank {rank} before all_reduce: {tensor}") dist.all_reduce(tensor, op=dist.ReduceOp.SUM) print(f"Rank {rank} after all_reduce: {tensor}") cleanup()在主节点上创建主机文件 hostfile.txt:
192.168.1.10 slots=1192.168.1.11 slots=1192.168.1.12 slots=1使用 torchrun 启动训练(假设每台机器1个GPU):
conda activate dist_traintorchrun \ --nnodes=3 \ --nproc_per_node=1 \ --node_rank=0 \ --master_addr="192.168.1.10" \ --master_port=29500 \ train.py注意:在其他节点上需设置对应的 --node_rank=1 和 --node_rank=2。更推荐使用 torch.distributed.run 结合 hostfile 自动调度(高级用法可参考 PyTorch 官方文档)。
--master_port(如 29501)export NCCL_DEBUG=INFOsudo firewall-cmd --add-port=22/tcp --permanent通过以上步骤,你已经成功搭建了一套基于 RockyLinux多机训练 的分布式深度学习环境。这套环境适用于 PyTorch分布式训练 任务,能显著加速大型模型的训练过程。后续可进一步集成 Horovod、DeepSpeed 等框架以支持更大规模训练。
希望这篇教程能帮助你在 AI 工程化道路上迈出坚实一步!如有疑问,欢迎在评论区交流。
本文由主机测评网于2025-12-07发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://www.vpshk.cn/2025124069.html