pytorch使用過程中遇到的錯誤處理之內(nèi)存溢出問題
內(nèi)存溢出
在使用 pytorch
訓練的模型進行推理操作時,
出現(xiàn)以下錯誤:
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)
從上述報錯信息中可以看出, 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)并沒有進程占用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)存被占用。
再次運行代碼依舊報錯,難道是代碼自身所需的內(nèi)存過大而導致失???
但是我們的代碼只是推理代碼,不應該占用這么高的內(nèi)存,經(jīng)過查詢,發(fā)現(xiàn)在推理模型時,應該在主代碼部分添加torch.no_grad()
以防止推理過程中對梯度進行追蹤。
追蹤梯度時會占用大量的內(nèi)存。
解決辦法
如下:
with torch.no_grad(): outputs = model(samples) #主代碼
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
使用selenium模擬登錄解決滑塊驗證問題的實現(xiàn)
這篇文章主要介紹了使用selenium模擬登錄解決滑塊驗證問題的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-05-05詳解Django+uwsgi+Nginx上線最佳實戰(zhàn)
這篇文章主要介紹了Django+uwsgi+Nginx上線最佳實戰(zhàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03python實現(xiàn)在遍歷列表時,直接對dict元素增加字段的方法
今天小編就為大家分享一篇python實現(xiàn)在遍歷列表時,直接對dict元素增加字段的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01Python列表排序方法reverse、sort、sorted詳解
這篇文章主要介紹了Python列表排序方法reverse、sort、sorted詳解,需要的朋友可以參考下2021-04-04