From 7f8733dfa4bc8edd1cec4fe92cd1e0056b125284 Mon Sep 17 00:00:00 2001 From: hli28146 Date: Wed, 12 Nov 2025 13:47:20 +0800 Subject: [PATCH] fixed huberloss --- S1/34/run_code.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/S1/34/run_code.py b/S1/34/run_code.py index eb312fd..41ad897 100644 --- a/S1/34/run_code.py +++ b/S1/34/run_code.py @@ -31,17 +31,19 @@ def run_benchmark(): output_torch = torch_model(*inputs) output_cuda = cuda_model(*inputs) - # 精度验证 - abs_diff = torch.abs(output_torch - output_cuda) - max_diff = torch.max(abs_diff).item() - mean_diff = torch.mean(abs_diff).item() + # 更严格的精度检查 + abs_diff = (output_torch - output_cuda).abs() + max_diff = abs_diff.max().item() + mean_diff = abs_diff.mean().item() - if max_diff < 1e-4 and mean_diff < 1e-5: - print(f"✅ 精度对齐:最大误差 {max_diff:.6f},平均误差 {mean_diff:.6f}") - precision_flag = True + print(f"最大差异: {max_diff:.6f}") + print(f"平均差异: {mean_diff:.6f}") + + precision_flag = torch.allclose(output_torch, output_cuda, rtol=1e-05, atol=1e-05) + if precision_flag: + print("✅ 精度对齐:两个模型的输出结果非常接近。") else: - print(f"❌ 精度不一致:最大误差 {max_diff:.6f},平均误差 {mean_diff:.6f}") - precision_flag = False + print("❌ 精度不一致!") print("\n-------------------- 性能加速比测试 --------------------") num_iterations = 100