-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
86 lines (66 loc) · 2.31 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import gc
import torch
def list_live_tensors():
tensors = {}
gc.collect()
torch.cuda.empty_cache()
for obj in gc.get_objects():
try:
if torch.is_tensor(obj) or (hasattr(obj, 'data') and torch.is_tensor(obj.data)):
d = str(obj.size()) + ", " + str(obj.dtype) + ", " + str(obj.device)
if d in tensors.keys():
tensors[d] += 1
else:
tensors[d] = 1
except:
pass
print("-----------")
for k, v in tensors.items():
print(f"{v} : {k}")
snapshot = {}
def set_snapshot():
global snapshot
snapshot = {}
for obj in gc.get_objects():
try:
if torch.is_tensor(obj) or (hasattr(obj, 'data') and torch.is_tensor(obj.data)):
d = str(obj.size()) + ", " + str(obj.dtype) + ", " + str(obj.device)
if d in snapshot.keys():
snapshot[d] += 1
else:
snapshot[d] = 1
except:
pass
def diff_snapshot():
global snapshot
new_tensors = {}
snapshot_copy = snapshot.copy()
for obj in gc.get_objects():
try:
if torch.is_tensor(obj) or (hasattr(obj, 'data') and torch.is_tensor(obj.data)):
d = str(obj.size()) + ", " + str(obj.dtype) + ", " + str(obj.device)
if d in snapshot_copy:
if snapshot_copy[d] == 1:
del snapshot_copy[d]
else:
snapshot_copy[d] -= 1
else:
if d in new_tensors:
new_tensors[d] += 1
else:
new_tensors[d] = 1
except:
pass
print("-----------")
print("-- New tensors")
for k, v in new_tensors.items(): print(f"{v} : {k}")
print("-----------")
print("-- Removed tensors")
for k, v in snapshot_copy.items(): print(f"{v} : {k}")
def print_vram_usage():
torch.cuda.reset_peak_memory_stats("cuda:0")
mem_this = torch.cuda.max_memory_allocated("cuda:0")
print(f"Peak memory: {mem_this / (1024 ** 2):,.2f} MB")
def print_vram_usage_peak():
mem_this = torch.cuda.max_memory_allocated("cuda:0")
print(f"Peak memory: {mem_this / (1024 ** 2):,.2f} MB")