-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_test.py
More file actions
78 lines (64 loc) · 1.66 KB
/
Copy pathsingle_test.py
File metadata and controls
78 lines (64 loc) · 1.66 KB
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
import torch
import psutil
import gc
def print_memory_info():
"""打印当前内存使用情况"""
# GPU 内存
if torch.cuda.is_available():
allocated = torch.cuda.memory_allocated()
reserved = torch.cuda.memory_reserved()
max_allocated = torch.cuda.max_memory_allocated()
print(f"CUDA Memory - Allocated: {allocated / 1024**2:.2f} MB")
print(f"CUDA Memory - Reserved: {reserved / 1024**2:.2f} MB")
print(f"CUDA Memory - Max Allocated: {max_allocated / 1024**2:.2f} MB")
# CPU 内存
process = psutil.Process()
memory_info = process.memory_info()
print(f"CPU Memory - RSS: {memory_info.rss / 1024**2:.2f} MB")
print(f"CPU Memory - VMS: {memory_info.vms / 1024**2:.2f} MB")
print("-" * 50)
print("Initial memory state:")
print_memory_info()
print("Creating tensors x1 and x2...")
x1 = torch.empty(
(
1024,
1024,
),
device="cuda",
dtype=torch.float16,
)
x2 = torch.empty(
(
1024,
1024,
),
device="cuda",
dtype=torch.float16,
)
print("After creating tensors:")
print_memory_info()
print("Creating tuple x3...")
x3 = (x1, x2)
print("After creating tuple:")
print_memory_info()
print("Deleting x1...")
del x3[0]
print("After deleting x1:")
print_memory_info()
print("Deleting x2...")
del x3[1]
print("After deleting x2:")
print_memory_info()
print("Deleting x3...")
del x3
print("After deleting x3:")
print_memory_info()
print("Running garbage collection...")
gc.collect()
print("After garbage collection:")
print_memory_info()
print("Clearing CUDA cache...")
torch.cuda.empty_cache()
print("After clearing CUDA cache:")
print_memory_info()