pytorch 공식 저장소의 #16417 이슈가 이 문제와 관련한 성지;;인 것 같습니다.
https://github.com/pytorch/pytorch/issues/16417
수많은 사람들이 이슈 보고와 해결 방법들을 공유하고 있는데요, 대략 정리해보면 다음과 같은 경우들이 있는 것 같습니다.
- 잘못된 에러 메시지 보고 (실제로 메모리가 부족한 케이스)
nn.Sequential
제거- 배치 사이즈 줄이기
with torch.no_grad()
추가- GPU를 점유하고 있는 다른 프로세스 제거
- Optimizer 변경 (Adam -> SGD)
torch.cuda.empty_cache()
로 캐시 삭제gc.collect()
로 가비지 컬렉터 수동 실행- (메모리를 점유하고 있는) 다른 변수 확인 & 제거
- (Jupyter Notebook의 경우) 커널 재시작
- ...
덧글이 많아서 2021년 초까지 밖에 못 보긴 했는데요, 위 내용들을 확인하셔서 체크해보시면 좋을 것 같습니다.
아래 몇몇 참고 하시면 좋을 것 같은 덧글들과 내용들을 발췌해봤습니다.
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.
# 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번과 관련해서도 문제를 많이 겪는 것 같았습니다. 아래 덧글들을 함께 참고하시면 좋을 것 같구요.
https://github.com/pytorch/pytorch/issues/16417#issuecomment-646729501
https://github.com/pytorch/pytorch/issues/16417#issuecomment-740551156
또는 메인 메모리(RAM)가 부족하지 않은지도 함께 체크해보시면 좋을 것 같습니다.
https://github.com/pytorch/pytorch/issues/40002
쉽지 않으시겠지만 위 이슈의 덧글들을 읽어보시면서 해당되는 내용이 있다면 하나씩 시도해보시면 좋을 것 같습니다.
혹시 해결하시게 되면 다른 분들을 위해 공유 부탁드립니다.