더러운 python 해결책 모음

DM wiki
둘러보기로 가기 검색하러 가기
while read requirement; do conda install --yes $requirement || pip install $requirement; done < requirements.txt
  • gpu 메모리 먹는 아가 찾기
import torch
import gc
tensors = []
tensor_sizes = []
# torch tensor objectを全部集める
for obj in gc.get_objects():
   try:
       if torch.is_tensor(obj) or (hasattr(obj, 'data') and torch.is_tensor(obj.data)):
           if str(obj.device) != 'cpu':
               numel = obj.numel()
               if obj.dtype == torch.float32:
                   numel *= 4
               elif obj.dtype == torch.int64:
                   numel *= 8
               elif obj.dtype not in dtypes:
                   dtypes.append(obj.dtype)
                   print(obj.dtype)
                   numel *= 8
               tensor_sizes.append(numel)
               tensors.append(obj)
   except:
       pass
# 大きさ順にsort
size_index = sorted(range(len(tensor_sizes)), key=lambda k: tensor_sizes[k], reverse=True)
# 上位5個のtensorの大きさ
for i in range(0, 20):
   print(f"{tensors[size_index[i]].shape}, {tensor_sizes[size_index[i]] / 1024} KBytes")
   # print(tensors[size_index[i]])
# tensor 全体の大きさ(bytes)
total_sum = 0
for tensor_size in tensor_sizes:
   total_sum += tensor_size
print(f"{total_sum / (1024 * 1024)} MBytes")