728x90
반응형
기록용.
1. Model GPU 사용량 체크
print('Memory Usage:')
print('Max Alloc:', round(torch.cuda.max_memory_allocated(0)/1024**3, 1), 'GB')
print('Allocated:', round(torch.cuda.memory_allocated(0)/1024**3,1), 'GB')
# MB 단위 : print(torch.cuda.memory_allocated() / 1024 /1024)
print('Cached: ', round(torch.cuda.memory_cached(0)/1024**3,1), 'GB')
⚠️nvidia-smi는 reserved memory를 포함한 값이기 때문에 실제 모델의 GPU 사용량을 보여주는 것이라 보기 힘들다.
(관련 stackoverflow)
=> 참고 자료 추가 (https://pytorch.org/blog/understanding-gpu-memory-1/)
2. Inference time
딥러닝 모델의 GPU 연산이 비동기적으로 수행되기 때문에 time.time()을 쓰면 실제 GPU 작업이 끝나기도 전에 CPU 내에서 측정을 종료할 수 있음.
따라서 정확한 시간 측정을 위해서는torch.cuda에서 제공하는 Event 클래스를 활용.
import torch
model.eval()
model = model.cuda()
starter = torch.cuda.Event(enable_timing=True)
ender = torch.cuda.Event(enable_timing=True)
with torch.no_grad(): # Gradient 연산 비활성화
starter.record()
output = model(input_tensor.cuda())
ender.record()
torch.cuda.synchronize() # Gpu 연산이 완료될 때까지 대기
infer_time = starter.elapsed_time(ender)
print("Elapsed time: {} s".format(infer_time * 1e-3)) # milliseconds to second
728x90
반응형
'머신러닝' 카테고리의 다른 글
[mmdetection/ mmrotate] Loss Nan (0) | 2023.03.28 |
---|---|
자연어 처리 도움되는 사이트 (1) | 2020.07.20 |
YOLO (0) | 2019.04.25 |