GPUCodeForces/S1/uucoco_#11/run_code.py

80 lines
2.4 KiB
Python
Raw Normal View History

2025-11-18 21:50:54 +08:00
import torch
import time
from IntraClassCorrelation_torch import Model, get_inputs, get_init_inputs
from IntraClassCorrelation_cuda import ModelNew
def run_benchmark():
if not torch.cuda.is_available():
2025-11-18 21:56:49 +08:00
print("CUDA 不可用")
2025-11-18 21:50:54 +08:00
return
2025-11-18 21:56:49 +08:00
device = torch.device("cuda")
2025-11-18 21:50:54 +08:00
2025-11-18 21:56:49 +08:00
# 准备输入数据
inputs = [x.cuda(device=device) for x in get_inputs()]
init_inputs = [x.cuda(device=device) if isinstance(x, torch.Tensor) else x for x in get_init_inputs()]
# 初始化模型
2025-11-18 21:50:54 +08:00
torch_model = Model(*init_inputs).cuda()
cuda_model = ModelNew(*init_inputs).cuda()
torch_model.eval()
cuda_model.eval()
print("-------------------- 精度对齐验证 --------------------")
with torch.no_grad():
2025-11-18 21:56:49 +08:00
# 预热GPU
_ = torch_model(*inputs)
_ = cuda_model(*inputs)
# 正式测试
2025-11-18 21:50:54 +08:00
output_torch = torch_model(*inputs)
output_cuda = cuda_model(*inputs)
2025-11-18 21:56:49 +08:00
# 精度验证
abs_diff = torch.abs(output_torch - output_cuda)
max_diff = torch.max(abs_diff).item()
mean_diff = torch.mean(abs_diff).item()
if max_diff < 1e-4 and mean_diff < 1e-5:
print(f"✅ 精度对齐:最大误差 {max_diff:.6f},平均误差 {mean_diff:.6f}")
precision_flag = True
2025-11-18 21:50:54 +08:00
else:
2025-11-18 21:56:49 +08:00
print(f"❌ 精度不一致:最大误差 {max_diff:.6f},平均误差 {mean_diff:.6f}")
precision_flag = False
2025-11-18 21:50:54 +08:00
print("\n-------------------- 性能加速比测试 --------------------")
num_iterations = 100
2025-11-18 21:56:49 +08:00
# 预热GPU
for _ in range(10):
_ = torch_model(*inputs)
_ = cuda_model(*inputs)
# PyTorch模型计时
2025-11-18 21:50:54 +08:00
torch.cuda.synchronize()
start_time = time.time()
for _ in range(num_iterations):
_ = torch_model(*inputs)
torch.cuda.synchronize()
torch_time = (time.time() - start_time) / num_iterations
2025-11-18 21:56:49 +08:00
# 自定义CUDA内核计时
2025-11-18 21:50:54 +08:00
torch.cuda.synchronize()
start_time = time.time()
for _ in range(num_iterations):
_ = cuda_model(*inputs)
torch.cuda.synchronize()
cuda_time = (time.time() - start_time) / num_iterations
2025-11-18 21:56:49 +08:00
print(f"PyTorch内置Swish平均执行时间: {torch_time:.6f}")
print(f"自定义CUDA Swish平均执行时间: {cuda_time:.6f}")
speedup = torch_time / cuda_time if cuda_time > 0 else 0
print(f"加速比 (Speedup): {speedup:.2f}x")
2025-11-18 21:50:54 +08:00
return precision_flag, speedup
if __name__ == "__main__":
precision_flag, speedup = run_benchmark()