網(wǎng)站首頁 編程語言 正文
關(guān)于torch.load加載預(yù)訓(xùn)練模型時(shí) 造成的 臨時(shí)分配的顯存 不釋放
作者:DeathYmz 更新時(shí)間: 2022-10-29 編程語言今天跑一個(gè)模型的時(shí)候,需要加載部分預(yù)訓(xùn)練模型的參數(shù),這期間遇到使用torch.load 忽略了 map_location參數(shù) 默認(rèn)gpu,這導(dǎo)致這個(gè)變量分配的顯存 不釋放 然后占用大量資源 gpu資源不能很好的利用。
問題講解:
比如我們一般我們會(huì)使用下面方式進(jìn)行加載預(yù)訓(xùn)練參數(shù) 到 自身寫的模型中:
from transformers import RobertaForMultipleChoice
import torch
model = RobertaForMultipleChoice.from_pretrained("roberta-large")
pretrained_model = torch.load("./checkpoints/txt_matching_e1.pth").roberta
pretrained_dict = pretrained_model.state_dict()
model_dict = model.roberta.state_dict()
# pretrained_dict = {k: v for k, v in pretrained_dict.items() if k in model_dict} #去除一些不需要的參數(shù)
model_dict.update(pretrained_dict)
model.roberta.load_state_dict(model_dict)
1. 當(dāng)我們沒有使用參數(shù)時(shí)候 load 默認(rèn)使用了一塊顯卡然后報(bào)錯(cuò)
RuntimeError: CUDA out of memory. Tried to allocate 20.00 MiB (GPU 3; 10.76 GiB total capacity; 350.54 MiB already allocated; 21.81 MiB free; 356.00 MiB reserved in total by PyTorch) If reserved memory is >> allocated memory try setting max_split_size_mb to avoid fragmentation. See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF
torch load 之前gpu使用
torch load 之后 outof memory 了 并且也不釋放
2. 當(dāng)我們沒有使用參數(shù)時(shí)候 load 默認(rèn)使用了一塊顯卡然后報(bào)錯(cuò)
當(dāng)我試試指定顯卡 gpu會(huì)使用2841
pretrained_model = torch.load(“./checkpoints/txt_matching_e1.pth”,map_location=‘cuda:0’).roberta!
(model 直接cuda 的gpu 占用情況)然后把這里面參數(shù)給model,并且model也是用cuda0 然后gpu使用4193
你可能會(huì)想model是不是model load預(yù)訓(xùn)練參數(shù)之后 就這么大了 那么load 和 load 參數(shù)后model 用不同gpu看看。
原理:cuda的內(nèi)存管理機(jī)制
參考解釋博客:Pytorch訓(xùn)練模型時(shí)如何釋放GPU顯存
解決方案:
1. 不占用顯存的使用方法,使用cpu 然后在del 用gc釋放內(nèi)存
model = RobertaForMultipleChoice.from_pretrained("roberta-large")
pretrained_model = torch.load("./checkpoints/txt_matching_e1.pth",map_location='cpu').roberta
pretrained_dict = pretrained_model.state_dict()
model_dict = model.roberta.state_dict()
model_dict.update(pretrained_dict)
model.roberta.load_state_dict(model_dict)
del pretrained_model
import gc
gc.collect()
2. 合理使用, torch.cuda.empty_cache()
這個(gè)需要了解一下python的內(nèi)存管理,引用機(jī)制。
比如我pretrain_model 給model直接加載參數(shù),model和pretrain_model 都在cuda:0上,使用torch.cuda.empty_cache() 不能釋放pretrain_model 的顯存。
當(dāng) 我把model 放到 cuda:1上(本來在cuda:0),這時(shí)候用torch.cuda.empty_cache() 可以釋放。
原文鏈接:https://blog.csdn.net/Miranda_ymz/article/details/127577639
相關(guān)推薦
- 2021-12-14 HTML+jQuery實(shí)現(xiàn)簡(jiǎn)單的登錄頁面_jquery
- 2023-03-01 getopts解析shell腳本命令行參數(shù)的方法_linux shell
- 2022-06-19 dockerfile指令構(gòu)建docker鏡像的示例代碼_docker
- 2022-06-01 C++程序內(nèi)存棧區(qū)與堆區(qū)模型案例分析_C 語言
- 2022-07-07 淺談Redis的異步機(jī)制_Redis
- 2022-04-16 C++中不得不說的map容器_C 語言
- 2022-10-02 React在組件中如何監(jiān)聽redux中state狀態(tài)的改變_React
- 2022-05-04 Entity?Framework使用DbModelBuilder?API創(chuàng)建表結(jié)構(gòu)_實(shí)用技巧
- 欄目分類
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支