"더러운 python 해결책 모음"의 두 판 사이의 차이

DM wiki
둘러보기로 이동 검색으로 이동
(새 문서: * torch 프린트 옵션 <syntaxhighlight lang="bash" enclose="div"> torch.set_printoptions(sci_mode=False) </syntaxhighlight> * gpu 메모리 먹는 아가 찾기 <syntaxhighlight l...)
 
15번째 줄: 15번째 줄:
       if torch.is_tensor(obj) or (hasattr(obj, 'data') and torch.is_tensor(obj.data)):
       if torch.is_tensor(obj) or (hasattr(obj, 'data') and torch.is_tensor(obj.data)):
           if str(obj.device) != 'cpu':
           if str(obj.device) != 'cpu':
               tensor_sizes.append(obj.size())
               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)
               tensors.append(obj)
   except:
   except:
22번째 줄: 31번째 줄:
size_index = sorted(range(len(tensor_sizes)), key=lambda k: tensor_sizes[k], reverse=True)
size_index = sorted(range(len(tensor_sizes)), key=lambda k: tensor_sizes[k], reverse=True)
# 上位5個のtensorの大きさ
# 上位5個のtensorの大きさ
for i in range(0, 5):
for i in range(0, 20):
   print(tensor_sizes[size_index[i]])
   print(f"{tensors[size_index[i]].shape}, {tensor_sizes[size_index[i]] / 1024} KBytes")
   # print(tensors[size_index[i]])
   # print(tensors[size_index[i]])
# tensor 全体の大きさ(bytes)
# tensor 全体の大きさ(bytes)
total_sum = 0
total_sum = 0
for tensor_size in tensor_sizes:
for tensor_size in tensor_sizes:
  current_num = 1
   total_sum += tensor_size
  for num in tensor_size:
print(f"{total_sum / (1024 * 1024)} MBytes")
      current_num *= num
 
   total_sum += current_num
print(str(total_sum * 4))
</syntaxhighlight>
</syntaxhighlight>

2019년 12월 26일 (목) 13:39 판

  • torch 프린트 옵션
torch.set_printoptions(sci_mode=False)
  • 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")