MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / bench_kernel_isolate.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Minimal C kernel benchmark — isolate the Python overhead.""" |
| 3 | import ctypes, time, numpy as np, os, sys |
| 4 | |
| 5 | sys.path.insert(0, '/onedev-workspace/work/src') |
| 6 | os.chdir('/onedev-workspace/work') |
| 7 | |
| 8 | # Load C kernel |
| 9 | SO = os.path.join(os.path.dirname('/onedev-workspace/work/src/mojollama/kernels/'), "q4_kernel_omp.so") |
| 10 | lib = ctypes.CDLL(SO) |
| 11 | lib.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 | ] |
| 17 | lib.q4_matmul_omp.restype = None |
| 18 | |
| 19 | # Also load AVX2 single-thread kernel |
| 20 | SO2 = os.path.join(os.path.dirname('/onedev-workspace/work/src/mojollama/kernels/'), "q4_kernel_avx2.so") |
| 21 | lib2 = ctypes.CDLL(SO2) |
| 22 | lib2.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 | ] |
| 28 | lib2.q4_matmul_avx2.restype = None |
| 29 | |
| 30 | # Also test the-weight-store approach from omp_engine |
| 31 | from mojollama.kernels.omp_engine import Model, mm, Q4_TS |
| 32 | mdl = Model('Llama-3.2-1B-Instruct-Q4_0.gguf') |
| 33 | |
| 34 | # Get Q weight |
| 35 | w_q, tt_q = mdl.get("blk.0.attn_q.weight") |
| 36 | print(f"Q weight: type={tt_q}, shape={w_q.shape}, dtype={w_q.dtype}, size={len(w_q)} bytes") |
| 37 | n_embd = mdl.n["n_embd"] |
| 38 | bpr = n_embd // 32 |
| 39 | block_sz = bpr * 18 |
| 40 | nr_q = len(w_q) // block_sz |
| 41 | print(f"Q weight: nr={nr_q}, nc={n_embd}, blocks_per_row={bpr}") |
| 42 | |
| 43 | # Prepare input |
| 44 | x = np.random.randn(n_embd).astype(np.float32) |
| 45 | |
| 46 | # Test 1: Direct C kernel call (OMP) |
| 47 | print("\n=== Direct C Kernel (OMP, 32 threads) ===") |
| 48 | out = np.zeros(nr_q, dtype=np.float32) |
| 49 | for _ 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 | |
| 56 | t0 = time.perf_counter() |
| 57 | for _ 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) |
| 63 | t1 = time.perf_counter() |
| 64 | print(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) |
| 67 | print("\n=== Direct C Kernel (AVX2, single thread, full matrix) ===") |
| 68 | out2 = np.zeros(nr_q, dtype=np.float32) |
| 69 | for _ 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 | |
| 76 | t0 = time.perf_counter() |
| 77 | for _ 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) |
| 83 | t1 = time.perf_counter() |
| 84 | print(f"Q4_0 matmul 2048x2048 (AVX2 single): {(t1-t0)/50*1000:.3f} ms per call") |
| 85 | |
| 86 | # Test 3: mm() wrapper overhead |
| 87 | print("\n=== mm() Wrapper Overhead ===") |
| 88 | for _ in range(3): |
| 89 | mm(mdl.get("blk.0.attn_q.weight"), x, n_embd) |
| 90 | |
| 91 | t0 = time.perf_counter() |
| 92 | for _ in range(50): |
| 93 | q = mm(mdl.get("blk.0.attn_q.weight"), x, n_embd) |
| 94 | t1 = time.perf_counter() |
| 95 | print(f"mm() wrapper Q proj: {(t1-t0)/50*1000:.3f} ms per call") |
| 96 | |
| 97 | # Test 4: Weight lookup overhead |
| 98 | print("\n=== Weight Lookup Overhead ===") |
| 99 | t0 = time.perf_counter() |
| 100 | for _ in range(50): |
| 101 | w, tt = mdl.get("blk.0.attn_q.weight") |
| 102 | t1 = time.perf_counter() |
| 103 | print(f"Weight lookup: {(t1-t0)/50*1000:.3f} ms") |
| 104 | |
| 105 | # Test 5: Output array allocation |
| 106 | t0 = time.perf_counter() |
| 107 | for _ in range(50): |
| 108 | o = np.zeros(nr_q, dtype=np.float32) |
| 109 | t1 = time.perf_counter() |
| 110 | print(f"np.zeros({nr_q}): {(t1-t0)/50*1000:.3f} ms") |
| 111 | |
| 112 | # Test 6: ctypes conversion overhead |
| 113 | w_ptr = w_q.ctypes.data_as(ctypes.POINTER(ctypes.c_uint8)) |
| 114 | x_ptr = x.ctypes.data_as(ctypes.POINTER(ctypes.c_float)) |
| 115 | t0 = time.perf_counter() |
| 116 | for _ 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)) |
| 119 | t1 = time.perf_counter() |
| 120 | print(f"ctypes conversion: {(t1-t0)/50*1000:.3f} ms") |