import numpy as np
k = 4 n = 3
x = np.array([1.0, 2.0, 3.0, 4.0], dtype=np.float32)
W_data = np.array([
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, ], dtype=np.float32)
W = W_data.reshape(n, k).T print(f"W = \n{W}")
y = x @ W
print(f"x @ W = {y}")
out = np.zeros(n, dtype=np.float32)
for j in range(n):
for i in range(k):
out[j] += x[i] * W_data[i + j * k]
print(f"Our formula = {out}")
print(f"Match: {np.allclose(y, out)}")
print("\n=== More realistic test ===")
k = 896 n = 896
np.random.seed(42)
x = np.random.randn(k).astype(np.float32)
W_data = np.random.randn(k * n).astype(np.float32)
W = W_data.reshape(n, k).T
y_numpy = x @ W
print(f"NumPy result: min={y_numpy.min():.4f}, max={y_numpy.max():.4f}")
print(f"NumPy first 5: {y_numpy[:5]}")
out = np.zeros(n, dtype=np.float32)
for j in range(n):
for i in range(k):
out[j] += x[i] * W_data[i + j * k]
print(f"Our formula: min={out.min():.4f}, max={out.max():.4f}")
print(f"Our first 5: {out[:5]}")
print(f"Match: {np.allclose(y_numpy, out)}")
print("\n=== Testing if GGUF uses different layout ===")
out2 = np.zeros(n, dtype=np.float32)
for j in range(n):
for i in range(k):
out2[j] += x[i] * W_data[j + i * n] print(f"Alt formula: min={out2.min():.4f}, max={out2.max():.4f}")
print(f"Alt first 5: {out2[:5]}")
print(f"Alt matches numpy: {np.allclose(y_numpy, out2)}")