大模型训练中CUDA out of memory的几种解决方法

常用:

  1. 减小batch size

  2. 如果需要较大的batch size来达到预期训练指标效果,则使用gradient accumulation来累积梯度,达到等价效果的同时,占用显存小;

  3. 使用低精度做训练。(mixed precision training). FP16+FP32

  4. 减小训练数据样本的长度

  5. Model并行或者Pipeline并行,把模型拆分至多卡

不常用:

  1. 释放闲置内存;torch.cuda.empty_cache()

  2. 缓解PyTorch的显存碎片;PYTORCH_CUDA_ALLOC_CONF和max_split_size_mb

7.使用更小的模型

  1. 增大GPU显存

  2. 使用nvidia-smi看到该GPU上的其他进程;可杀则杀;

Perplexity 查询结果:

Here are several solutions to resolve the "CUDA out of memory" error when training large language models (LLMs) using SFT (Supervised Fine-Tuning):

  1. Reduce batch size: Lowering the batch size during training can significantly reduce the memory requirements. For example, you could set the batch size to 1 and use gradient accumulation to accumulate gradients over multiple batches before updating the model weights.
  2. Use gradient accumulation: Gradient accumulation allows you to accumulate gradients over multiple batches before updating the model weights. This effectively increases the batch size without increasing the memory usage. Be sure to divide the loss by the number of gradient accumulation steps to maintain the correct gradient scale.
  3. Enable mixed precision training: Training the model in mixed precision (FP16) can reduce the memory usage compared to full precision (FP32). PyTorch provides the torch.cuda.amp package for automatic mixed precision training.
  4. Reduce input sequence length: If your input texts are long, consider truncating or splitting them into shorter sequences to reduce the memory usage during tokenization and model inference.
  5. Free GPU memory: Call torch.cuda.empty_cache() to free up any unused GPU memory allocated by PyTorch. This might not help much, but it's worth a try.
  6. Set PYTORCH_CUDA_ALLOC_CONF environment variable: You can try setting the PYTORCH_CUDA_ALLOC_CONF environment variable with max_split_size_mb to avoid memory fragmentation. Experiment with different values based on your GPU memory.
  7. Use a smaller model: If possible, consider using a smaller version of the LLM that has fewer parameters. Smaller models generally require less memory during training.
  8. Increase GPU memory: If you have access to a GPU with more memory, consider using it to accommodate the memory requirements of the LLM training.
  9. Distribute training across multiple GPUs: Splitting the training across multiple GPUs can reduce the memory usage per GPU. However, this approach requires additional setup and coordination.
  10. Monitor GPU memory usage: Use nvidia-smi to monitor the GPU memory usage and identify any other processes consuming GPU memory. Terminate unnecessary processes to free up memory
相关推荐
测试员周周1 分钟前
【Agent评测】别只会跑 RAGAS!RAG 评测想要 CI 门禁,试试 DeepEval 断言打分
人工智能
sinat_3239603713 分钟前
老游戏怎么被重写:读 OpenRCT2
人工智能
汤姆宝库18 分钟前
推理级联的实现:置信度估计与FrugalGPT式路由
人工智能
gptAI_plus21 分钟前
GPT 写 SQL:避免订单金额被重复统计
人工智能
吕炘嵘21 分钟前
会背答案,不会修 Bug——一次 0.6B模型Java 微调复盘
人工智能
Thomas.Sir23 分钟前
第017讲:AI模型开发|可行性评分卡与一票否决:把主观判断结构化:数据不及格,其余满分也必须 No-Go
人工智能·ai
JonaHu33 分钟前
GPT Image 2.6 商品图横竖版适配:安全区、留白与裁切预检
图像处理·人工智能·python·aigc
AAA修煤气灶刘哥43 分钟前
一口气拍 30 秒,MAGI-2 能接下 Seedance 2.5 的活吗?
人工智能·llm
AI 编程助手GPT1 小时前
GPT 写 SQL:避免订单金额被重复统计
jvm·人工智能·sql·gpt
AAA修煤气灶刘哥1 小时前
QQ 音乐怎么在 25 毫秒内,决定一句话该交给谁?
人工智能·算法·llm