9bow
(박정환)
4월 18, 2022, 11:41오후
6
pytorch 공식 저장소의 #16417 이슈가 이 문제와 관련한 성지;;인 것 같습니다.
opened 10:01AM - 27 Jan 19 UTC
closed 06:56AM - 28 Jan 19 UTC
needs reproduction
## CUDA Out of Memory error but CUDA memory is almost empty
I am currently tr… aining a lightweight model on very large amount of textual data (about 70GiB of text).
For that I am using a machine on a cluster (['grele' of the grid5000 cluster network](https://www.grid5000.fr/mediawiki/index.php/Nancy:Hardware#grele_.28production_queue.29)).
I am getting after 3h of training this very strange CUDA Out of Memory error message:
`RuntimeError: CUDA out of memory. Tried to allocate 12.50 MiB (GPU 0; 10.92 GiB total capacity; 8.57 MiB already allocated; 9.28 GiB free; 4.68 MiB cached)`.
According to the message, I have the required space but it does not allocate the memory.
Any idea what might cause this ?
For information, my preprocessing relies on `torch.multiprocessing.Queue` and an iterator over the lines of my source data to preprocess the data on the fly.
### Full stacktrace
```
Traceback (most recent call last):
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/memory_profiler.py", line 1228, in <module>
exec_with_profiler(script_filename, prof, args.backend, script_args)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/memory_profiler.py", line 1129, in exec_with_profiler
exec(compile(f.read(), filename, 'exec'), ns, ns)
File "run.py", line 293, in <module>
main(args, save_folder, load_file)
File "run.py", line 272, in main
trainer.all_epochs()
File "/home/emarquer/papud-bull-nn/trainer/trainer.py", line 140, in all_epochs
self.single_epoch()
File "/home/emarquer/papud-bull-nn/trainer/trainer.py", line 147, in single_epoch
tracker.add(*self.single_batch(data, target))
File "/home/emarquer/papud-bull-nn/trainer/trainer.py", line 190, in single_batch
result = self.model(data)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/home/emarquer/papud-bull-nn/model/model.py", line 54, in forward
emb = self.emb(input)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/sparse.py", line 118, in forward
self.norm_type, self.scale_grad_by_freq, self.sparse)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/functional.py", line 1454, in embedding
return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: CUDA out of memory. Tried to allocate 12.50 MiB (GPU 0; 10.92 GiB total capacity; 8.57 MiB already allocated; 9.28 GiB free; 4.68 MiB cached)
```
수많은 사람들이 이슈 보고와 해결 방법들을 공유하고 있는데요, 대략 정리해보면 다음과 같은 경우들이 있는 것 같습니다.
잘못된 에러 메시지 보고 (실제로 메모리가 부족한 케이스)
nn.Sequential
제거
배치 사이즈 줄이기
with torch.no_grad()
추가
GPU를 점유하고 있는 다른 프로세스 제거
Optimizer 변경 (Adam -> SGD)
torch.cuda.empty_cache()
로 캐시 삭제
gc.collect()
로 가비지 컬렉터 수동 실행
(메모리를 점유하고 있는) 다른 변수 확인 & 제거
(Jupyter Notebook의 경우) 커널 재시작
...
덧글이 많아서 2021년 초까지 밖에 못 보긴 했는데요, 위 내용들을 확인하셔서 체크해보시면 좋을 것 같습니다.
아래 몇몇 참고 하시면 좋을 것 같은 덧글들과 내용들을 발췌해봤습니다.
RuntimeError: CUDA out of memory. Tried to allocate 12.50 MiB (GPU 0; 10.92 GiB total capacity; 8.57 MiB already allocated; 9.28 GiB free; 4.68 MiB cached) · Issue #16417 · pytorch/pytorch · GitHub 에서,
Hi guys, I got into this issue many times. Now I'd try to summarize the possible solutions:
If 'CUDA out of memory' error msg pops up even no iteration goes, then check the batch size you set and decrease it.
If 'CUDA out of memory' error msg pops up after some iterations, then check all the variables in the computing graph, and detach the unnecessary variables. I usually run into this situation, e.g., I did not detach the inputs of my measure/loss functions
which are not involved in backward propagation.
A practical way to tell what's happening is using nvidia-smi
command every few seconds to check whether the CUDA memory usage is increasing. If it is, I think you may run into the second situation.
RuntimeError: CUDA out of memory. Tried to allocate 12.50 MiB (GPU 0; 10.92 GiB total capacity; 8.57 MiB already allocated; 9.28 GiB free; 4.68 MiB cached) · Issue #16417 · pytorch/pytorch · GitHub 에서,
# Evaluate mode
model_x.eval()
model_y.eval()
with torch.no_grad():
# <validation>
# <...>
# Train mode
model_x.train()
model_y.train()
gc.collect()
torch.cuda.empty_cache()
위 9번과 관련해서도 문제를 많이 겪는 것 같았습니다. 아래 덧글들을 함께 참고하시면 좋을 것 같구요.
opened 10:01AM - 27 Jan 19 UTC
closed 06:56AM - 28 Jan 19 UTC
needs reproduction
## CUDA Out of Memory error but CUDA memory is almost empty
I am currently tr… aining a lightweight model on very large amount of textual data (about 70GiB of text).
For that I am using a machine on a cluster (['grele' of the grid5000 cluster network](https://www.grid5000.fr/mediawiki/index.php/Nancy:Hardware#grele_.28production_queue.29)).
I am getting after 3h of training this very strange CUDA Out of Memory error message:
`RuntimeError: CUDA out of memory. Tried to allocate 12.50 MiB (GPU 0; 10.92 GiB total capacity; 8.57 MiB already allocated; 9.28 GiB free; 4.68 MiB cached)`.
According to the message, I have the required space but it does not allocate the memory.
Any idea what might cause this ?
For information, my preprocessing relies on `torch.multiprocessing.Queue` and an iterator over the lines of my source data to preprocess the data on the fly.
### Full stacktrace
```
Traceback (most recent call last):
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/memory_profiler.py", line 1228, in <module>
exec_with_profiler(script_filename, prof, args.backend, script_args)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/memory_profiler.py", line 1129, in exec_with_profiler
exec(compile(f.read(), filename, 'exec'), ns, ns)
File "run.py", line 293, in <module>
main(args, save_folder, load_file)
File "run.py", line 272, in main
trainer.all_epochs()
File "/home/emarquer/papud-bull-nn/trainer/trainer.py", line 140, in all_epochs
self.single_epoch()
File "/home/emarquer/papud-bull-nn/trainer/trainer.py", line 147, in single_epoch
tracker.add(*self.single_batch(data, target))
File "/home/emarquer/papud-bull-nn/trainer/trainer.py", line 190, in single_batch
result = self.model(data)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/home/emarquer/papud-bull-nn/model/model.py", line 54, in forward
emb = self.emb(input)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/sparse.py", line 118, in forward
self.norm_type, self.scale_grad_by_freq, self.sparse)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/functional.py", line 1454, in embedding
return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: CUDA out of memory. Tried to allocate 12.50 MiB (GPU 0; 10.92 GiB total capacity; 8.57 MiB already allocated; 9.28 GiB free; 4.68 MiB cached)
```
opened 10:01AM - 27 Jan 19 UTC
closed 06:56AM - 28 Jan 19 UTC
needs reproduction
## CUDA Out of Memory error but CUDA memory is almost empty
I am currently tr… aining a lightweight model on very large amount of textual data (about 70GiB of text).
For that I am using a machine on a cluster (['grele' of the grid5000 cluster network](https://www.grid5000.fr/mediawiki/index.php/Nancy:Hardware#grele_.28production_queue.29)).
I am getting after 3h of training this very strange CUDA Out of Memory error message:
`RuntimeError: CUDA out of memory. Tried to allocate 12.50 MiB (GPU 0; 10.92 GiB total capacity; 8.57 MiB already allocated; 9.28 GiB free; 4.68 MiB cached)`.
According to the message, I have the required space but it does not allocate the memory.
Any idea what might cause this ?
For information, my preprocessing relies on `torch.multiprocessing.Queue` and an iterator over the lines of my source data to preprocess the data on the fly.
### Full stacktrace
```
Traceback (most recent call last):
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/memory_profiler.py", line 1228, in <module>
exec_with_profiler(script_filename, prof, args.backend, script_args)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/memory_profiler.py", line 1129, in exec_with_profiler
exec(compile(f.read(), filename, 'exec'), ns, ns)
File "run.py", line 293, in <module>
main(args, save_folder, load_file)
File "run.py", line 272, in main
trainer.all_epochs()
File "/home/emarquer/papud-bull-nn/trainer/trainer.py", line 140, in all_epochs
self.single_epoch()
File "/home/emarquer/papud-bull-nn/trainer/trainer.py", line 147, in single_epoch
tracker.add(*self.single_batch(data, target))
File "/home/emarquer/papud-bull-nn/trainer/trainer.py", line 190, in single_batch
result = self.model(data)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/home/emarquer/papud-bull-nn/model/model.py", line 54, in forward
emb = self.emb(input)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py", line 489, in __call__
result = self.forward(*input, **kwargs)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/sparse.py", line 118, in forward
self.norm_type, self.scale_grad_by_freq, self.sparse)
File "/home/emarquer/miniconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/functional.py", line 1454, in embedding
return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: CUDA out of memory. Tried to allocate 12.50 MiB (GPU 0; 10.92 GiB total capacity; 8.57 MiB already allocated; 9.28 GiB free; 4.68 MiB cached)
```
또는 메인 메모리(RAM)가 부족하지 않은지도 함께 체크해보시면 좋을 것 같습니다.
opened 04:01AM - 14 Jun 20 UTC
closed 04:07PM - 10 Nov 20 UTC
module: cuda
module: memory usage
triaged
i am training binary classification model on gpu using pytorch, and i get cuda m… emory error , but i have enough free memory as the message say:
error :
`RuntimeError: CUDA out of memory. Tried to allocate 50.00 MiB (GPU 0; 6.00 GiB total capacity; 693.78 MiB already allocated; 3.91 GiB free; 18.22 MiB cached)`
it is trying to allocate 50 MB but i have 3.91 GB free, so what is the problem ???
note:
gpu: gtx 1060 6 GB
torch: 1.2.0
cc @ngimel
쉽지 않으시겠지만 위 이슈의 덧글들을 읽어보시면서 해당되는 내용이 있다면 하나씩 시도해보시면 좋을 것 같습니다.
혹시 해결하시게 되면 다른 분들을 위해 공유 부탁드립니다.
4개의 좋아요