这个基准有效吗? torch、numpy、tinygrad 2000 x 2000 矩阵乘法(CPU)(已编辑)

问题描述 投票:0回答:1

我对发布的问题表示歉意。舞台上的一个人已经指出,tinygrad 很懒,我应该实现张量来执行 matmul。所以我更新了基准。我想现在的问题是,为什么tinygrad 速度较慢?我对张量运算和低级编程不太了解。


size = 2000



# Benchmarking NumPy
np_array1 = np.random.rand(size, size)
np_array2 = np.random.rand(size, size)

start_time = time.time()
np_result = np_array1 @ np_array2
np_time = time.time() - start_time

# Benchmarking PyTorch
torch_tensor1 = torch.rand(size, size)
torch_tensor2 = torch.rand(size, size)

start_time = time.time()
torch_result = torch_tensor1 @ torch_tensor2
torch_time = time.time() - start_time

# Benchmarking TinyGrad
tinygrad_tensor1 = Tensor.rand(size, size)
tinygrad_tensor2 = Tensor.rand(size, size)

start_time = time.time()
tinygrad_result = tinygrad_tensor1 @ tinygrad_tensor2
tinygrad_result_realized = tinygrad_result.realize()  # Realize the result
tinygrad_time = time.time() - start_time

# Print the results
print(f"NumPy Time: {np_time:.6f} seconds")
print(f"PyTorch Time: {torch_time:.6f} seconds")
print(f"TinyGrad Time: {tinygrad_time:.6f} seconds")

NumPy Time: 0.146893 seconds
PyTorch Time: 0.072339 seconds
TinyGrad Time: 1.518489 seconds

python numpy pytorch benchmarking matrix-multiplication
1个回答
3
投票

Tinygrad 以“惰性”方式执行运算,因此尚未执行矩阵乘法。将矩阵乘法线更改为:

tg_result = (tg_tensor @ tg_tensor).realize()

tg_result = (tg_tensor @ tg_tensor).numpy()
© www.soinside.com 2019 - 2024. All rights reserved.