코딩하는 애옹😸

[PyTorch] Troubleshooting 본문

부스트캠프 AI Tech 3기/Study

[PyTorch] Troubleshooting

DevYe 2022. 1. 27. 22:31
728x90
반응형

PyTorch Troubleshooting

GPUtil 사용하기

  • GPU의 상태를 보여줌

  • iter마다 메모리가 늘어나는지 확인

    !pip install GPUtil  
    
    import GPUtil  
    GPUtil.showUtilization()

1. empty_cache() 써보기

torch.cuda.empty_cache()

  • 사용되지 않은 GPU상 cache 정리
  • Loop 전에 실행하면 이전의 학습에 남아있던 메모리들에 의해 영향을 받을 확률이 적음

2. Training loop에 tensor로 축적되는 변수 확인

  • tensor로 처리된 변수는 GPU 상에 메모리 사용
  • 해당 변수 loop 안에 연산이 있을 때 GPU에 computation graph를 생성 (메모리 잠식)

3. 가능 batch 사이즈 실험

  • batch size를 1로 해서 실험
    oom = False  
    try:  
    run_model(batch_size)  
    except RuntimeError: # Out of memory 
    oom = True  
    if oom:  
    for _ in range(batch_size):  
       run_model(1)

4. torch.no_grad() 사용

  • inference 시점에서는 torch.no_grad() 사용하기
  • backward로 인해 쌓이는 메모리에서 자유로움
with torch.no_grad():  
    for data, target in test_loader:  
       output = network(data)  
       test_loss += F.nll_loss(output, target, size_average=False).item()  
       pred = output.data.max(1, keepdim=True)[1]  
       correct += pred.eq(target.data.view_as(pred)).sum()

5. 기타

  • colab에서는 너무 큰 사이즈는 실행하지 말 것 (LSTM, ... )
  • torchsummary 등으로 사이즈 맞출 것 (CNN)
반응형

'부스트캠프 AI Tech 3기 > Study' 카테고리의 다른 글

Semantic Segmentation 대회에서 사용하는 방법  (0) 2022.05.09
AI와 저작권법  (0) 2022.02.16
[PyTorch] Multi-GPU  (0) 2022.01.27
[PyTorch] 모델 불러오기  (0) 2022.01.26
[PyTorch] AutoGrad  (0) 2022.01.25
Comments