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

常用:

1. 减小batch size

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

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

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

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

不常用:

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

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

7.使用更小的模型

8. 增大GPU显存

10. 使用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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/15940.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

自麻省理工学院和谷歌最新研究:零样本跨语言对齐的新途径

在构建实用的语言模型(LMs)时,使模型与人类偏好对齐是一个不可或缺的阶段。这通常需要大量的标注偏好数据,这些数据对于多种语言来说难以获取,尤其是对于多语种环境,这使得扩展到更多语言变得具有挑战性。本…

YOLO系列模型疑问

YOLO模型V1版本 1、论文里提到bounding box的(x,y)值表示什么意思呢? 原论文: Each bounding box consists of 5 predictions: x, y, w, h, and confidence. The (x, y) coordinates represent the center of the bo…

Golang发送GET请求并设置查询参数

服务端 package mainimport ("encoding/json""net/http""zdpgo_chi""zdpgo_chi/middleware" )func main() {r : zdpgo_chi.NewRouter()r.Use(middleware.RequestID)r.Use(middleware.RealIP)r.Use(middleware.Logger)r.Use(middlewar…

看这两位东北圣女美吗?如何描写美女的大长腿?

看这两位东北圣女美吗?如何描写美女的大长腿? 最近署名为懂球娘娘的一篇描写东北圣女的文章火了,文中描述了海棠朵朵与辛芷蕾这两位娇媚动人的角色。其美艳动人的形象和魅力四溢的描写让人为之倾倒。 这种通过文字展现人物魅力的能力让人佩服…

Hadoop运行wordcount实例任务卡在job running的多种情况及解决方法

第一种:配置问题 这是别人的图片,据楼主排查解决是因为hosts配置问题… 现象:各种无法运行、启动 解决办法: 1、修改日志级别 export HADOOP_ROOT_LOGGERDEBUG,console 查看下详细信息,定位到具体问题解决 第二种&…

每周题解:繁忙的都市

题目链接 繁忙的都市 题目描述 城市 C 是一个非常繁忙的大都市,城市中的道路十分的拥挤,于是市长决定对其中的道路进行改造。城市 C 的道路是这样分布的:城市中有 n n n 个交叉路口,有些交叉路口之间有道路相连,两…

5月26(信息差)

🌍 珠峰登顶“堵车”后冰架断裂 5人坠崖 2人没爬上来! 珠峰登顶“堵车”后冰架断裂 5人坠崖 2人没爬上来! 🎄 Windows 11 Beta 22635.3646 预览版发布:中国大陆地区新增“微软电脑管家”应用 ✨ 成都限购解除即将满…

[图解]产品经理-竞赛题解析:阿布思考法和EA

1 00:00:00,410 --> 00:00:02,330 今天我们来说一道 2 00:00:02,610 --> 00:00:04,690 前些天出的一道竞赛题 3 00:00:07,250 --> 00:00:09,310 怎么样用阿布思考法 4 00:00:09,320 --> 00:00:10,540 来改进EA 5 00:00:11,690 --> 00:00:12,620 题目是这样的…

简述 v-model 双向绑定的原理是什么?

v-model 是 Vue.js 中用于实现表单输入和应用状态(即数据)之间双向绑定的指令。其原理可以概括如下: 基本思想: v-model 是 v-bind 和 v-on 的语法糖,它结合了输入元素的 value 属性和 input 事件。当输入元素的值发生…

Kivy 项目51斩百词 5

MRWord\pages\infopage\info.py def read_random_word(self) def read_random_word(self):"""随机读取一条数据"""sql "SELECT * FROM word WHERE id (SELECT word_id FROM today ORDER BY RANDOM() limit 1)"rows select_data(sq…

Django 里html模板

Django 提供两种方式让程序员自定义html模板。 第一种方法 在项目文件夹里的urls.py进行添加 修改代码如下 from django.contrib import admin from django.urls import path from app01 import views # 得添加这行urlpatterns [path(xxx/, views.home), # 添加这行path(…

Java中print,println,printf的功能以及区别

在Java中,System.out.print, System.out.println, 和 System.out.printf 都是用于在控制台输出的方法,但它们在使用和功能上有所不同。 System.out.print: * 功能:将指定的内容输出到控制台,但不换行。 * 示例:Sy…

TXT文本编辑器:一键提取,多关键字匹配,内容尽在掌控!

在浩如烟海的文档中,寻找关键信息往往是一项繁琐而耗时的任务。你是否曾经为了查找某个关键字而翻遍了整个文件夹,却仍然一无所获?现在,有了TXT文本编辑器,这一切都将变得轻松而高效 这款软件以其简洁明了的操作界面和…

Flutter 中的 FittedBox 小部件:全面指南

Flutter 中的 FittedBox 小部件:全面指南 在Flutter的丰富布局小部件中,FittedBox扮演着一个独特而重要的角色。它是一个灵活的组件,用于将子组件的大小和位置适应到给定的约束条件中。本文将提供FittedBox的全面指南,帮助你了解…

C#9特性整理(部分)

1. 实例化类型推断(Target-typed new) 我们会使用 new 关键字来实例化,但在部分字段和属性声明的时候,这些类型已经是在旁边给出,且不能使用 var 代替的。因此,我们必须这么写: public Person…

最近5星好评的华为的书《常变与长青》

常变与长青 (豆瓣) 作者简介 郭平,1988年加入华为,历任产品开发部项目经理、供应链总经理、总裁办主任、管理工程部总裁、企业发展部总裁、终端公司董事长兼总裁、公司轮值CEO、财经委员会主任、公司副董事长、轮值董事长等职务,现任公…

微信小程序毕业设计-学生知识成果展示与交流系统项目开发实战(附源码+演示视频+LW)

大家好!我是程序猿老A,感谢您阅读本文,欢迎一键三连哦。 💞当前专栏:微信小程序毕业设计 精彩专栏推荐👇🏻👇🏻👇🏻 🎀 Python毕业设计…

接口自动化核心模块Requests详解(二)

一、概述 使用requests进行接口测试时,主要使用get 和post两种方式,两种请求方式的传参模式和方法是完全不一样的 二、传参实战 2.1 post传参的数据格式 使用post进行传参时,有三种数据格式,data(键值对的字典),json(有嵌套的…