2023-08-20 00:07:23 +08:00
|
|
|
import numpy as np
|
|
|
|
from tinygrad.helpers import getenv
|
2024-01-02 06:58:48 +08:00
|
|
|
from tinygrad import dtypes, Tensor
|
2024-03-28 04:43:09 +08:00
|
|
|
dtype_in = dtypes.half if getenv("HALF") else dtypes.bfloat16 if getenv("BFLOAT16") else dtypes.float
|
|
|
|
acc_dtype = dtypes.half if getenv("ACC_HALF") else dtypes.bfloat16 if getenv("ACC_BFLOAT16") else None
|
2024-09-14 02:01:33 +08:00
|
|
|
if getenv("INT"):
|
|
|
|
dtype_in = dtypes.int8
|
|
|
|
acc_dtype = dtypes.int32
|
2023-09-29 09:04:28 +08:00
|
|
|
N = getenv("N", 4096)
|
2024-04-17 11:40:31 +08:00
|
|
|
M = getenv("M", N)
|
|
|
|
K = getenv("K", N)
|
2023-09-29 09:04:28 +08:00
|
|
|
CNT = getenv("CNT", 10)
|
2024-03-28 04:43:09 +08:00
|
|
|
ATOL = getenv("ATOL", 1e-4)
|
|
|
|
RTOL = getenv("RTOL", 3e-2)
|
2024-03-22 12:23:36 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-04-17 11:40:31 +08:00
|
|
|
a, b = Tensor.rand(M, K, dtype=dtype_in).realize(), Tensor.rand(K, N, dtype=dtype_in).realize()
|
2024-03-22 12:23:36 +08:00
|
|
|
for i in range(CNT):
|
|
|
|
if i > 0 and getenv("RAND", 0) != 0:
|
2024-04-17 11:40:31 +08:00
|
|
|
a, b = Tensor.rand(M, K, dtype=dtype_in).realize(), Tensor.rand(K, N, dtype=dtype_in).realize()
|
2024-03-22 12:23:36 +08:00
|
|
|
c = a.matmul(b, acc_dtype=acc_dtype).realize()
|
|
|
|
comp = a.numpy().astype(np.float32) @ b.numpy().astype(np.float32)
|
|
|
|
nc = c.numpy()
|
2024-04-17 11:40:31 +08:00
|
|
|
try:
|
|
|
|
np.testing.assert_allclose(nc, comp, atol=ATOL, rtol=RTOL)
|
|
|
|
except AssertionError as e:
|
|
|
|
if getenv("DEBUG_VALUES") > 0:
|
|
|
|
indices = np.where(~np.isclose(nc, comp, rtol=RTOL, atol=ATOL))
|
|
|
|
non_matching_elements_nc = nc[indices]
|
|
|
|
non_matching_elements_comp = comp[indices]
|
|
|
|
print(indices)
|
|
|
|
print("result :", non_matching_elements_nc)
|
|
|
|
print("ground truth:", non_matching_elements_comp)
|
|
|
|
raise e
|