-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_pytorch_amd.py
283 lines (205 loc) · 7.9 KB
/
main_pytorch_amd.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import os
import torch
import json
import time
import argparse
import yaml
from thop import profile
import matplotlib.pyplot as plt
from matplotlib import rcParams
import pynvml
import threading
import subprocess
# rcParams['font.sans-serif'] = ['SimHei']
import importlib.util
# 监控 GPU 使用情况
def get_gpu_info():
# 调用 rocm-smi 并解析输出
result = subprocess.run(['rocm-smi', '--showmeminfo', 'vram', '--showuse', '--json'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# 打印命令行输出用于调试
print("rocm-smi stdout:", result.stdout.decode('utf-8'))
print("rocm-smi stderr:", result.stderr.decode('utf-8'))
try:
gpu_info = json.loads(result.stdout.decode('utf-8'))
except json.JSONDecodeError as e:
print("JSON decode error:", e)
gpu_info = {}
# 打印解析后的JSON结构
print("Parsed GPU Info:", gpu_info)
if not gpu_info:
return [0, 0, 0, 0]
card_key = list(gpu_info.keys())[0] # 获取第一个卡的键名
# 提取并转换数值
mem_total = int(gpu_info[card_key].get('VRAM Total Memory (B)', 0))
mem_used = int(gpu_info[card_key].get('VRAM Total Used Memory (B)', 0))
usage = int(gpu_info[card_key].get('GPU use (%)', 0))
power_draw = int(gpu_info[card_key].get('Average Graphics Package Power (W)', 0)) # 如果存在的话
mem_info = mem_total / 1024**2, mem_used / 1024**2
return [usage, mem_info[0], mem_info[1], power_draw]
def monitor_gpu_usage():
start_event.wait()
while True:
gpu_info = get_gpu_info()
gpu_usage_list.append(gpu_info)
time.sleep(0.001) # 1 毫秒
# 速度测试
def speed_test(model, input, iterations):
if iterations is None:
elapsed_time = 0
iterations = 100
while elapsed_time < 1:
torch.cuda.synchronize()
torch.cuda.synchronize()
t_start = time.time()
for _ in range(iterations):
model(input)
torch.cuda.synchronize()
torch.cuda.synchronize()
elapsed_time = time.time() - t_start
iterations *= 2
FPS = iterations / elapsed_time
iterations = int(FPS * 6)
print('=========Speed Testing=========')
torch.cuda.synchronize()
torch.cuda.synchronize()
t_start = time.time()
for _ in range(iterations):
model(input)
torch.cuda.synchronize()
torch.cuda.synchronize()
elapsed_time = time.time() - t_start
latency = elapsed_time / iterations * 1000
FPS = 1000 / latency
model_performance.extend([FPS, latency])
# for i in [FPS, latency]:
# model_performance.append(i)
# return FPS, latency
def speed_test_l(model, iterations):
if iterations is None:
elapsed_time = 0
iterations = 100
while elapsed_time < 1:
torch.cuda.synchronize()
torch.cuda.synchronize()
t_start = time.time()
for _ in range(iterations):
model.forward()
torch.cuda.synchronize()
torch.cuda.synchronize()
elapsed_time = time.time() - t_start
iterations *= 2
FPS = iterations / elapsed_time
iterations = int(FPS * 6)
print('=========Speed Testing=========')
torch.cuda.synchronize()
torch.cuda.synchronize()
t_start = time.time()
for _ in range(iterations):
model.forward()
torch.cuda.synchronize()
torch.cuda.synchronize()
elapsed_time = time.time() - t_start
latency = elapsed_time / iterations * 1000
FPS = 1000 / latency
model_performance.extend([FPS, latency])
def load_config(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
config = json.load(file)
return config
def choose_option(options, prompt):
print(prompt)
for key, value in options.items():
print(f"{key}. {value}")
choice = input("输入选项编号:")
while choice not in options:
print("无效的选项,请重新输入。")
choice = input("输入选项编号:")
return options[choice]
def count_parameters_and_flops(model, input):
flops, params = profile(model, inputs=(input,), verbose=False)
return flops / 1e9 * 2, params / 1e6
########################################################################
script_dir = os.path.dirname(os.path.abspath(__file__))
subprocess.run(['python', os.path.join(script_dir, 'update_config.py')], check=True) #刷新模型文件
parser = argparse.ArgumentParser()
parser.add_argument("--cfg", type=str, default="vision/detection/yolov5/pytorch/configs/yolov5x.yaml", help="model.yaml")
parser.add_argument("--iterations", type=int, default=None, help="迭代次数")
opt = parser.parse_args()
config = load_config('config.json')
category = choose_option(config['categories'], "选择AI应用领域:")
application = choose_option(config['applications'][category], f"选择{category} 中的应用场景:")
model = choose_option(config['models'][category][application], f"选择{application} 中的具体模型:")
print(f"你选择了 {category} 领域中的 {application} 场景下的 {model} 模型")
# 模型脚本的路径
model_script_path = category + '/'+ application + '/'+ model+'/pytorch/'+ model+ '.py'
# 动态加载模块
spec = importlib.util.spec_from_file_location("model_module", model_script_path)
model_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(model_module)
# 获取模型类
model_class = getattr(model_module, model)
# 实例化模型
model = model_class()
device = torch.device('cuda')
if category == 'language':
with torch.no_grad():
for _ in range(10):
model.forward()
else:
input = torch.randn(1, 3, 640, 640).to(device)
model.eval()
model.to(device)
with torch.no_grad():
for _ in range(10):
model(input)
# pynvml.nvmlInit()
start_event = threading.Event()
gpu_usage_list = [] ##保存gpu使用情况的时间序列
model_performance = [] ##保存模型推理性能
#################模型推理和GPU监控双线程启动##############
monitor_thread = threading.Thread(target=monitor_gpu_usage)
# monitor_thread.daemon = True
monitor_thread.start() # 启动 GPU 监控线程
iterations = opt.iterations
inference_thread = threading.Thread(target=speed_test(model, input, iterations = iterations))
inference_thread.start() # 启动推理线程
start_event.set() # 触发事件,开始监控和推理
inference_thread.join() # 等待推理完成
# 停止监控(如果需要,可以增加一个标志位来控制循环)
time.sleep(1) # 确保监控线程完成最后的记录
# 打印或处理 GPU 使用情况数据
# print(gpu_usage_list)
# for usage in gpu_usage_list:
# print(usage)
headers = gpu_usage_list[0]
column3 = [row[2] for row in gpu_usage_list[1:]]
column4 = [row[3] for row in gpu_usage_list[1:]]
fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))
ax1.plot(column3, label=headers[2])
ax1.set_title(headers[2])
ax1.set_xlabel("Time (ms)")
ax1.set_ylabel("MiB")
ax1.legend()
ax2.plot(column4, label=headers[3])
ax2.set_title(headers[3])
ax2.set_xlabel("Time (ms)")
ax2.set_ylabel("W")
ax2.legend()
plt.tight_layout()
plt.savefig('savefiles/gpu_usage.png')
###############处理GPU监控结果和模型推理数据#############
if category == 'language':
speed_test_l(model, iterations = None)
FPS, latency = model_performance[0], model_performance[1]
flops, params = model.count_parameters_and_flops()
else:
speed_test(model, input, iterations = None)
FPS, latency = model_performance[0], model_performance[1]
flops, params = count_parameters_and_flops(model, input)
print(f"Total parameters: {params:.2f} million")
# print(f"Trainable parameters: {trainable_params:.2f} million")
print(f"Total GFLOPs: {flops:.2f}")
print(f"FPS: {FPS:.2f}")
print(f"Latency: {latency:.2f} ms")
# 清理缓存
torch.cuda.empty_cache()