版本比较
CUDA 的 TensorCore 还是太超模了!
正文
增加 8 行删除 8 行
4 行未改动5678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565727 行未改动4 行未改动def test_performance():device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')print(f"Using device: {device}")# 预热for i in range(3):a = torch.randn(2000, 2000, device=device)b = torch.randn(2000, 2000, device=device)c = a @ btorch.cuda.synchronize() if device.type == 'cuda' else None# 测试 1: 大矩阵运算print("\n=== 大矩阵测试 (5000x10000 @ 10000x5000) ===")# GPU 测试start_time = time.time()for i in range(100):a = torch.randn(5000, 10000, device=device)b = torch.randn(10000, 5000, device=device)c = a @ btorch.cuda.synchronize() if device.type == 'cuda' else Nonegpu_time = time.time() - start_timeprint(f"GPU 100 次时间: {gpu_time:.4f} 秒")# CPU 测试 (只做1次,因为太慢了)if device.type == 'cuda':start_time = time.time()a = torch.randn(5000, 10000, device='cpu')b = torch.randn(10000, 5000, device='cpu')c = a @ b # 只做一次cpu_time = time.time() - start_timeprint(f"CPU 单次时间: {cpu_time:.4f} 秒")print(f"GPU 比 CPU 快约: {cpu_time/(gpu_time/100):.1f} 倍")# 测试 2: 中等矩阵运算print("\n=== 中等矩阵测试 (2000x2000 @ 2000x2000) ===")# GPU 测试start_time = time.time()for i in range(100):a = torch.randn(2000, 2000, device=device)b = torch.randn(2000, 2000, device=device)c = a @ btorch.cuda.synchronize() if device.type == 'cuda' else Nonegpu_time_medium = time.time() - start_timeprint(f"GPU 时间: {gpu_time_medium:.4f} 秒")# CPU 测试start_time = time.time()for i in range(100):a = torch.randn(2000, 2000, device='cpu')27 行未改动4 行未改动5678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565727 行未改动4 行未改动def test_performance():device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')print(f"Using device: {device}")# 预热for i in range(3):a = torch.randn(2000, 2000, device=device)b = torch.randn(2000, 2000, device=device)c = a @ btorch.cuda.synchronize() if device.type == 'cuda' else None# 测试 1: 大矩阵运算print("\n=== 大矩阵测试 (5000x10000 @ 10000x5000) ===")# GPU 测试start_time = time.time()for i in range(100):a = torch.randn(5000, 10000, device=device)b = torch.randn(10000, 5000, device=device)c = a @ btorch.cuda.synchronize() if device.type == 'cuda' else Nonegpu_time = time.time() - start_timeprint(f"GPU 100 次时间: {gpu_time:.4f} 秒")# CPU 测试 (只做1次,因为太慢了)if device.type == 'cuda':start_time = time.time()a = torch.randn(5000, 10000, device='cpu')b = torch.randn(10000, 5000, device='cpu')c = a @ b # 只做一次cpu_time = time.time() - start_timeprint(f"CPU 单次时间: {cpu_time:.4f} 秒")print(f"GPU 比 CPU 快约: {cpu_time/(gpu_time/100):.1f} 倍")# 测试 2: 中等矩阵运算print("\n=== 中等矩阵测试 (2000x2000 @ 2000x2000) ===")# GPU 测试start_time = time.time()for i in range(100):a = torch.randn(2000, 2000, device=device)b = torch.randn(2000, 2000, device=device)c = a @ btorch.cuda.synchronize() if device.type == 'cuda' else Nonegpu_time_medium = time.time() - start_timeprint(f"GPU 时间: {gpu_time_medium:.4f} 秒")# CPU 测试start_time = time.time()for i in range(100):a = torch.randn(2000, 2000, device='cpu')27 行未改动