大模型训练中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
相关推荐
IT_陈寒5 分钟前
SpringBoot自动配置的坑差点让我加班到天亮
前端·人工智能·后端
亿信华辰软件8 分钟前
睿治Agent数据治理平台重磅发布:数据治理大脑+全栈Agent,以AI重构数据治理全流程
大数据·人工智能
源码老李10 分钟前
独立游戏AI音乐指南:用Suno AI让游戏拥有灵魂
人工智能·游戏·ai编程
:mnong24 分钟前
AI 编程理论与实践 — 课程大纲
人工智能
Luca_kill28 分钟前
深度解析 DeerFlow:字节跳动开源的长时程 AI 超级智能体架构
人工智能·智能体·大模型应用·ai架构·deerflow
PNP Robotics39 分钟前
领军军者|PNP机器人包文涛:以具身智能定义机器人的“生命直觉”
人工智能·深度学习·学习·机器学习·机器人
stereohomology44 分钟前
2026年人工智能技术趋势浅度解析
人工智能
2601_958320571 小时前
【小白易懂版】OpenClaw 飞书机器人绑定配置详细教程(含安装包)
人工智能·机器人·飞书·open claw·小龙虾·open claw安装
Chockong1 小时前
05_yolox_s的后处理截断并导出onnx
深度学习·神经网络
AI创界者1 小时前
《2026 视觉革命:深度测评 GPT-Image-2,基于 DMXAPI 实现 4K 超分与批量生图实战》
人工智能