(llamafactory用多张4090卡,训练qwen14B大模型时oom(out of memory)报错,torch.OutOfMemoryError:CUDA out of memory,Tried to allocate 136MB,GPU 5 has a total capacity of 23.64GB which 16.56MB is free,已解决)
1.问题描述
通过export CUDA_VISIBLE_DVICES=0,1,2,3,4,5,6,7指定使用8张显卡,训练qwen2.5-7B大模型时正常,但训练qwen2.5-14B,qwen2.5-32B模型时报错,torch.OutOfMemoryError:CUDA out of memory,Tried to allocate 136MB,GPU 5 has a total capacity of 23.64GB which 16.56MB is free 。 大概意思就是说GPU显存不足,很明显不符合实际,我的8张4090卡总显存是8*24GB,理论上14B的大模型肯定能跑起来。
linux服务器环境: 系统:Ubuntu22; 硬件:本机512G内存,8张4090显卡; python:3.10版本; llamafactory版本:0.9.1.dev0; cuda版本:12.4版本; pytorch版本:2.4.0+cuda121(GPU); 大模型:qwen2.5(7B,14B,32B,72B)Instruct版; 
2.解决方案
在run_train_bash.sh文件中,设置参数如下: model_name_or_path和output_dir 改成你自己的本地大模型和输出路径就行; src/train.py 取决于你src目录下的训练启动文件名,这个是llamafactory自带的; CUDA_VISIBLE_DEVICES:看你有几张卡,需要用几张卡,我的是8张卡; 其它都是训练参数,根据实际需要修改。最重要的是deepspeed ds_config_zero3.json这行, 这个是qwen官网给的。deepspeed 库可以用pip install deepspeed 安装一下。run_train_bash.sh文件中内容:
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 accelerate launch src/train.py \\
––deepspeed ds_config_zero3.json \\
––stage sft \\
––do_train True \\
––model_name_or_path /home/admin1/Qwen2.5
网硕互联帮助中心





评论前必须登录!
注册