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_kernel_isolate.py

120 lines · 4246 bytes Raw Blame History
1#!/usr/bin/env python3
2"""Minimal C kernel benchmark — isolate the Python overhead."""
3import ctypes, time, numpy as np, os, sys
4
5sys.path.insert(0, '/onedev-workspace/work/src')
6os.chdir('/onedev-workspace/work')
7
8# Load C kernel
9SO = os.path.join(os.path.dirname('/onedev-workspace/work/src/mojollama/kernels/'), "q4_kernel_omp.so")
10lib = ctypes.CDLL(SO)
11lib.q4_matmul_omp.argtypes = [
12 ctypes.POINTER(ctypes.c_uint8),
13 ctypes.POINTER(ctypes.c_float),
14 ctypes.POINTER(ctypes.c_float),
15 ctypes.c_int, ctypes.c_int, ctypes.c_int,
16]
17lib.q4_matmul_omp.restype = None
18
19# Also load AVX2 single-thread kernel
20SO2 = os.path.join(os.path.dirname('/onedev-workspace/work/src/mojollama/kernels/'), "q4_kernel_avx2.so")
21lib2 = ctypes.CDLL(SO2)
22lib2.q4_matmul_avx2.argtypes = [
23 ctypes.POINTER(ctypes.c_uint8),
24 ctypes.POINTER(ctypes.c_float),
25 ctypes.POINTER(ctypes.c_float),
26 ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int,
27]
28lib2.q4_matmul_avx2.restype = None
29
30# Also test the-weight-store approach from omp_engine
31from mojollama.kernels.omp_engine import Model, mm, Q4_TS
32mdl = Model('Llama-3.2-1B-Instruct-Q4_0.gguf')
33
34# Get Q weight
35w_q, tt_q = mdl.get("blk.0.attn_q.weight")
36print(f"Q weight: type={tt_q}, shape={w_q.shape}, dtype={w_q.dtype}, size={len(w_q)} bytes")
37n_embd = mdl.n["n_embd"]
38bpr = n_embd // 32
39block_sz = bpr * 18
40nr_q = len(w_q) // block_sz
41print(f"Q weight: nr={nr_q}, nc={n_embd}, blocks_per_row={bpr}")
42
43# Prepare input
44x = np.random.randn(n_embd).astype(np.float32)
45
46# Test 1: Direct C kernel call (OMP)
47print("\n=== Direct C Kernel (OMP, 32 threads) ===")
48out = np.zeros(nr_q, dtype=np.float32)
49for _ in range(3):
50 lib.q4_matmul_omp(
51 w_q.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)),
52 x.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
53 out.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
54 nr_q, n_embd, 18)
55
56t0 = time.perf_counter()
57for _ in range(50):
58 lib.q4_matmul_omp(
59 w_q.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)),
60 x.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
61 out.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
62 nr_q, n_embd, 18)
63t1 = time.perf_counter()
64print(f"Q4_0 matmul 2048x2048 (OMP t=32): {(t1-t0)/50*1000:.3f} ms per call")
65
66# Test 2: Direct C kernel call (AVX2, single thread)
67print("\n=== Direct C Kernel (AVX2, single thread, full matrix) ===")
68out2 = np.zeros(nr_q, dtype=np.float32)
69for _ in range(3):
70 lib2.q4_matmul_avx2(
71 w_q.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)),
72 x.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
73 out2.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
74 nr_q, n_embd, 0, nr_q)
75
76t0 = time.perf_counter()
77for _ in range(50):
78 lib2.q4_matmul_avx2(
79 w_q.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)),
80 x.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
81 out2.ctypes.data_as(ctypes.POINTER(ctypes.c_float)),
82 nr_q, n_embd, 0, nr_q)
83t1 = time.perf_counter()
84print(f"Q4_0 matmul 2048x2048 (AVX2 single): {(t1-t0)/50*1000:.3f} ms per call")
85
86# Test 3: mm() wrapper overhead
87print("\n=== mm() Wrapper Overhead ===")
88for _ in range(3):
89 mm(mdl.get("blk.0.attn_q.weight"), x, n_embd)
90
91t0 = time.perf_counter()
92for _ in range(50):
93 q = mm(mdl.get("blk.0.attn_q.weight"), x, n_embd)
94t1 = time.perf_counter()
95print(f"mm() wrapper Q proj: {(t1-t0)/50*1000:.3f} ms per call")
96
97# Test 4: Weight lookup overhead
98print("\n=== Weight Lookup Overhead ===")
99t0 = time.perf_counter()
100for _ in range(50):
101 w, tt = mdl.get("blk.0.attn_q.weight")
102t1 = time.perf_counter()
103print(f"Weight lookup: {(t1-t0)/50*1000:.3f} ms")
104
105# Test 5: Output array allocation
106t0 = time.perf_counter()
107for _ in range(50):
108 o = np.zeros(nr_q, dtype=np.float32)
109t1 = time.perf_counter()
110print(f"np.zeros({nr_q}): {(t1-t0)/50*1000:.3f} ms")
111
112# Test 6: ctypes conversion overhead
113w_ptr = w_q.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8))
114x_ptr = x.ctypes.data_as(ctypes.POINTER(ctypes.c_float))
115t0 = time.perf_counter()
116for _ in range(50):
117 w_p = w_q.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8))
118 x_p = x.ctypes.data_as(ctypes.POINTER(ctypes.c_float))
119t1 = time.perf_counter()
120print(f"ctypes conversion: {(t1-t0)/50*1000:.3f} ms")