interplane.Get Omega
Read in your language

MojoLlama

Archived Mojo/MAX quantizer and serving research repository (read-only history)

Brought by drakestapleton

branch main Branches Tags History

MojoLlama / bench_ops.mojo

87 lines · 2932 bytes Raw Blame History
1# ===----------------------------------------------------------------------=== #
2# MojoLlama — Ops benchmark (v3)
3# ===----------------------------------------------------------------------=== #
4
5from std.python import Python
6from std.math import rsqrt, exp
7from std.time import perf_counter
8
9
10fn silu_scalar(x: Float32) -> Float32:
11 return x / (1 + exp(-x))
12
13
14fn rmsnorm_element(x: Float32, weight: Float32, eps: Float32) -> Float32:
15 return x / rsqrt(x * x + eps) * weight
16
17
18fn main() raises:
19 var np = Python.evaluate("__import__('numpy')")
20
21 print("MojoLlama — Ops Benchmark")
22 print("=" * 50)
23
24 var n = 2048
25 var x_py = np.random.randn(n).astype(np.float32)
26 var out_py = np.zeros(n, np.float32)
27
28 # Benchmark SiLU - Mojo
29 print("\n1. SiLU (2048 x 1000)")
30 var t0 = perf_counter()
31 for _ in range(1000):
32 for i in range(n):
33 var val = Float32(py=x_py.__getitem__(i))
34 out_py.__setitem__(i, value=silu_scalar(val))
35 var t1 = perf_counter()
36 var mojo_ms = (t1 - t0) * 1000.0
37 print("Mojo: " + String(mojo_ms) + "ms")
38
39 # Benchmark SiLU - NumPy
40 t0 = perf_counter()
41 var np_silu = Python.evaluate("lambda x: x / (1 + __import__('numpy').exp(-x))")
42 for _ in range(1000):
43 _ = np_silu(x_py)
44 t1 = perf_counter()
45 var np_ms = (t1 - t0) * 1000.0
46 print("NumPy: " + String(np_ms) + "ms")
47 if np_ms > 0.0 and mojo_ms > 0.0:
48 print("Speedup: " + String(np_ms / mojo_ms) + "x")
49
50 # Benchmark RMSNorm
51 print("\n2. RMSNorm (1x2048 x 500)")
52 var n2 = Python.evaluate("2048")
53 var b1 = Python.evaluate("1")
54 var x2_py = np.random.randn(b1, n2).astype(np.float32)
55 var w_py = np.ones(n2, np.float32)
56 var eps_f = Float32(1.0e-6)
57 var out2_py = np.zeros(b1, n2).astype(np.float32)
58
59 # Mojo
60 t0 = perf_counter()
61 for _ in range(500):
62 var sum_sq = Float32(0.0)
63 for j in range(2048):
64 var val = Float32(py=x2_py.__getitem__(0).__getitem__(j))
65 sum_sq += val * val
66 var inv_std = rsqrt(sum_sq / 2048.0 + eps_f)
67 for j in range(2048):
68 var xv = Float32(py=x2_py.__getitem__(0).__getitem__(j))
69 var wv = Float32(py=w_py.__getitem__(j))
70 out2_py.__getitem__(0).__setitem__(j, value=xv * inv_std * wv)
71 t1 = perf_counter()
72 var rn_mojo_ms = (t1 - t0) * 1000.0
73
74 # NumPy
75 t0 = perf_counter()
76 for _ in range(500):
77 var v = np.mean((x2_py.astype(np.float64)) ** 2, Python.evaluate("-1"), Python.evaluate("True"))
78 _ = x2_py / (v + 1.0e-6).__call__("sqrt") * w_py
79 t1 = perf_counter()
80 var rn_np_ms = (t1 - t0) * 1000.0
81
82 print("Mojo: " + String(rn_mojo_ms) + "ms")
83 print("NumPy: " + String(rn_np_ms) + "ms")
84 if rn_np_ms > 0.0 and rn_mojo_ms > 0.0:
85 print("Speedup: " + String(rn_np_ms / rn_mojo_ms) + "x")
86
87 print("\nDone.")