pytorch使用過程中遇到的錯(cuò)誤處理之內(nèi)存溢出問題
內(nèi)存溢出
在使用 pytorch 訓(xùn)練的模型進(jìn)行推理操作時(shí),
出現(xiàn)以下錯(cuò)誤:
RuntimeError: CUDA out of memory. Tried to allocate 416.00 MiB (GPU 0; 2.00 GiB total capacity; 1.32 GiB already allocated; 0 bytes free; 1.34 GiB reserved in total by PyTorch)
從上述報(bào)錯(cuò)信息中可以看出, GPU0 共有 2GiB 容量,已經(jīng)分配出去 1.32 GiB , 0 bytes 可用,PyTorch占用 1.34 GiB 。
使用下述命令查看GPU的使用情況:
> nvidia-smi Wed Jul 13 15:20:18 2022 +-----------------------------------------------------------------------------+ | NVIDIA-SMI 512.95 Driver Version: 512.95 CUDA Version: 11.6 | |-------------------------------+----------------------+----------------------+ | GPU Name TCC/WDDM | Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |===============================+======================+======================| | 0 NVIDIA GeForce ... WDDM | 00000000:01:00.0 Off | N/A | | N/A 39C P0 N/A / N/A | 0MiB / 2048MiB | 2% Default | | | | N/A | +-------------------------------+----------------------+----------------------+ +-----------------------------------------------------------------------------+ | Processes: | | GPU GI CI PID Type Process name GPU Memory | | ID ID Usage | |=============================================================================| | No running processes found | +-----------------------------------------------------------------------------+
發(fā)現(xiàn)并沒有進(jìn)程占用GPU資源。
然后使用 torch 包內(nèi)的命令查看內(nèi)存占用情況,
結(jié)果如下:
> print(torch.cuda.memory.memory_summary()) |===========================================================================| | PyTorch CUDA memory summary, device ID 0 | |---------------------------------------------------------------------------| | CUDA OOMs: 0 | cudaMalloc retries: 0 | |===========================================================================| | Metric | Cur Usage | Peak Usage | Tot Alloc | Tot Freed | |---------------------------------------------------------------------------| | Allocated memory | 0 B | 0 B | 0 B | 0 B | | from large pool | 0 B | 0 B | 0 B | 0 B | | from small pool | 0 B | 0 B | 0 B | 0 B | |---------------------------------------------------------------------------| | Active memory | 0 B | 0 B | 0 B | 0 B | | from large pool | 0 B | 0 B | 0 B | 0 B | | from small pool | 0 B | 0 B | 0 B | 0 B | |---------------------------------------------------------------------------| | GPU reserved memory | 0 B | 0 B | 0 B | 0 B | | from large pool | 0 B | 0 B | 0 B | 0 B | | from small pool | 0 B | 0 B | 0 B | 0 B | |---------------------------------------------------------------------------| | Non-releasable memory | 0 B | 0 B | 0 B | 0 B | | from large pool | 0 B | 0 B | 0 B | 0 B | | from small pool | 0 B | 0 B | 0 B | 0 B | |---------------------------------------------------------------------------| | Allocations | 0 | 0 | 0 | 0 | | from large pool | 0 | 0 | 0 | 0 | | from small pool | 0 | 0 | 0 | 0 | |---------------------------------------------------------------------------| | Active allocs | 0 | 0 | 0 | 0 | | from large pool | 0 | 0 | 0 | 0 | | from small pool | 0 | 0 | 0 | 0 | |---------------------------------------------------------------------------| | GPU reserved segments | 0 | 0 | 0 | 0 | | from large pool | 0 | 0 | 0 | 0 | | from small pool | 0 | 0 | 0 | 0 | |---------------------------------------------------------------------------| | Non-releasable allocs | 0 | 0 | 0 | 0 | | from large pool | 0 | 0 | 0 | 0 | | from small pool | 0 | 0 | 0 | 0 | |===========================================================================|
從結(jié)果中看到,沒有內(nèi)存被占用。
再次運(yùn)行代碼依舊報(bào)錯(cuò),難道是代碼自身所需的內(nèi)存過大而導(dǎo)致失???
但是我們的代碼只是推理代碼,不應(yīng)該占用這么高的內(nèi)存,經(jīng)過查詢,發(fā)現(xiàn)在推理模型時(shí),應(yīng)該在主代碼部分添加torch.no_grad()以防止推理過程中對(duì)梯度進(jìn)行追蹤。
追蹤梯度時(shí)會(huì)占用大量的內(nèi)存。
解決辦法
如下:
with torch.no_grad():
outputs = model(samples) #主代碼總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python list轉(zhuǎn)矩陣的實(shí)例講解
今天小編就為大家分享一篇python list轉(zhuǎn)矩陣的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
Python使用MYSQLDB實(shí)現(xiàn)從數(shù)據(jù)庫(kù)中導(dǎo)出XML文件的方法
這篇文章主要介紹了Python使用MYSQLDB實(shí)現(xiàn)從數(shù)據(jù)庫(kù)中導(dǎo)出XML文件的方法,涉及Python使用MYSQLDB操作數(shù)據(jù)庫(kù)及XML文件的相關(guān)技巧,需要的朋友可以參考下2015-05-05
Python實(shí)現(xiàn)快速查找并替換Excel中的數(shù)據(jù)
Excel中的查找替換是一個(gè)非常實(shí)用的功能,能夠幫助用戶快速完成大量數(shù)據(jù)的整理和處理工作,避免手動(dòng)逐一修改數(shù)據(jù)的麻煩,提高工作效率,所以本文給大家介紹了Python實(shí)現(xiàn)快速查找并替換Excel中的數(shù)據(jù),需要的朋友可以參考下2024-06-06
kaggle+mnist實(shí)現(xiàn)手寫字體識(shí)別
這篇文章主要為大家詳細(xì)介紹了kaggle+mnist實(shí)現(xiàn)手寫字體識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
django drf框架自帶的路由及最簡(jiǎn)化的視圖
這篇文章主要介紹了django-drf框架自帶的路由以及最簡(jiǎn)化的視圖,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09
淺談Python數(shù)據(jù)類型判斷及列表腳本操作
下面小編就為大家?guī)硪黄獪\談Python數(shù)據(jù)類型判斷及列表腳本操作。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
linux centos 7.x 安裝 python3.x 替換 python2.x的過程解析
這篇文章主要介紹了linux centos 7.x 安裝 python3.x 替換 python2.x的過程解析,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12

