A100单机多卡大模型训练踩坑记录(CUDA环境、多GPU卡住且显存100%)

踩坑1:服务器只装了 CUDA Driver 没装 CUDA Toolkit

系统:Ubuntu-18.04
deepspeed 跑百亿模型训练时,报关于 CUDA_HOME 的错误。

AssertionError: CUDA_HOME does not exist, unable to compile CUDA op(s)

执行 echo $CUDA_HOMEnvcc -V 发现没装 cuda(只装了显卡驱动因此 nvidia-smi 执行没问题,而 torch.cuda.is_available() 返回 True 是因为 pip install 环境时,cudatoolkit 之类的已经装到了 python 环境里)。

于是按提示执行 sudo apt install nvidia-cuda-toolkit,这个命令不会给你把 cuda 装到 /usr/local/cuda,而是分散到很多地方,而且装的是 9.1 版本,跟 deepspeed 环境支持版本不兼容:
在这里插入图片描述
因此找运维看看能不能给装个 cuda 11.3,于是运维甩来 nvidia 官网的安装命令(来自这个链接):
在这里插入图片描述
然而,前两种安装方式(deb)都遇到问题(要么是 apt-key 那步公钥问题,要么最后一步报错)

$ sudo apt-get -y install cuda

Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 cuda : Depends: cuda-12-1 (>= 12.1.1) but it is not going to be installed
E: Unable to correct problems, you have held broken packages.

于是用runfile方式本地安装成功!执行:

$ wget https://developer.download.nvidia.com/compute/cuda/11.3.0/local_installers/cuda_11.3.0_465.19.01_linux.run
$ # wget https://developer.download.nvidia.com/compute/cuda/11.4.0/local_installers/cuda_11.4.0_470.42.01_linux.run
$ sudo sh cuda_11.3.0_465.19.01_linux.run

在这里插入图片描述
安装时提示检测到装了驱动,直接选continue,在下一步取消选择 driver 那项,只选 cuda toolkit 的相关那些进行安装(参考资料
安装完后,输出如下:

===========
= Summary =
===========

Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-11.3/
Samples:  Installed in /u01/isi/, but missing recommended libraries

Please make sure that
 -   PATH includes /usr/local/cuda-11.3/bin
 -   LD_LIBRARY_PATH includes /usr/local/cuda-11.3/lib64, or, add /usr/local/cuda-11.3/lib64 to /etc/ld.so.conf and run ldconfig as root

To uninstall the CUDA Toolkit, run cuda-uninstaller in /usr/local/cuda-11.3/bin
***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 465.00 is required for CUDA 11.3 functionality to work.
To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:
    sudo <CudaInstaller>.run --silent --driver

Logfile is /var/log/cuda-installer.log

此时,/usr/local/cuda-11.3/ 成功出现。
在这里插入图片描述

最后按提示配环境变量(没用sudo用户配,配的个人用户)。在 ~/.bashrc 中追加( :$PATH 的含义是追加到环境变量 $PATH 的末尾,最后 LD_LIBRARY_PATH 那行非必要,我是因为不加的话 deepspeed 报错才加上):

export PATH=/usr/local/cuda-11.3/bin:$PATH  
export LD_LIBRARY_PATH=/usr/local/cuda-11.3/lib64:$LD_LIBRARY_PATH
export CUDA_HOME=/usr/local/cuda-11.3
export LD_LIBRARY_PATH=/u01/xxxxxxx/miniconda3/envs/xxxx/lib/python3.8/site-packages/nvidia/cublas/lib/:$LD_LIBRARY_PATH

此时执行 nvcc -V 就正常了!(其实后面又换了个11.4的版本,步骤一样)
在这里插入图片描述

踩坑2:超过2张卡就直接卡住不动 且显存占用100%

不管是跑 deepspeed 还是只用 python -m torch.distributed.launch --nproc_per_node 4 train.py 跑数据并行,单卡或者两卡都没事,只要大于3卡,直接卡死,而且显存占用一直100%。
在这里插入图片描述
折磨了大半天,一篇知乎文章拯救了我。根据文章,是由于BIOS里IO虚拟化默认启动了PCI访问控制服务(ACS)导致GPU间无法直接通过P2P方式通信。最快的方案,执行之前添加环境变量:

export NCCL_P2P_DISABLE=1

执行命令训练百亿模型实验,成功。

export TOKENIZERS_PARALLELISM=false
export NCCL_P2P_DISABLE=1
deepspeed --num_gpus=8 \
    --module training.trainer \
    --data-path data/train_20230426.json \
    --input-model checkpoints/xxx \
    --deepspeed config/stage2.json \
    --epochs 3 \
    --local-output-dir results \
    --per-device-train-batch-size 2 \
    --per-device-eval-batch-size 2 \
    --logging-steps 5 \
    --save-steps 100 \
    --save-total-limit 10 \
    --eval-steps 100 \
    --warmup-steps 100 \
    --test-size 500 \
    --lr 2e-5 \
    --seed 515

踩坑3:安装cudnn

发现cudnn也没装,从官网登陆下载后执行:

sudo dpkg -i cudnn-local-repo-ubuntu1804-8.4.0.27_1.0-1_amd64.deb

在这里插入图片描述

  • 2
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用PyTorch的`DataParallel`来实现单机多卡训练模型。`DataParallel`会自动将模型复制到每个可用的GPU并行计算,并在反向传播时进行梯度的累积和同步。 下面是一个简单的示例代码,展示了如何使用`DataParallel`来进行单机多卡训练模型: ```python import torch import torch.nn as nn from torch.utils.data import DataLoader # 定义模型 class MyModel(nn.Module): def __init__(self): super(MyModel, self).__init__() self.fc = nn.Linear(10, 1) def forward(self, x): return self.fc(x) # 创建模型实例 model = MyModel() # 设置设备 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = model.to(device) # 多卡训练 if torch.cuda.device_count() > 1: model = nn.DataParallel(model) # 使用DataParallel包装模型 # 定义数据集和数据加载器 dataset = YourDataset() # 自定义数据集 dataloader = DataLoader(dataset, batch_size=64, shuffle=True) # 定义优化器和损失函数 optimizer = torch.optim.SGD(model.parameters(), lr=0.001) criterion = nn.MSELoss() # 训练过程 for epoch in range(num_epochs): for inputs, labels in dataloader: inputs = inputs.to(device) labels = labels.to(device) # 前向传播 outputs = model(inputs) loss = criterion(outputs, labels) # 反向传播和优化 optimizer.zero_grad() loss.backward() optimizer.step() print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item()}") # 保存模型 torch.save(model.state_dict(), "model.pth") ``` 在上述示例中,如果有多个可用的GPU,则`DataParallel`会自动将模型复制到每个可用的GPU并行计算。你可以通过`torch.cuda.device_count()`函数来检查可用的GPU数量。在训练过程中,你只需要像单卡训练一样使用模型即可,`DataParallel`会自动处理数据和梯度的同步。 请确保你的代码在使用`DataParallel`之前将模型移动到正确的设备上,并在训练过程中将数据和标签移动到相同的设备上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值