Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/MojoLlama/blob/main/bench_fast_profile.py # MojoLlama Archived Mojo/MAX quantizer and serving research repository (read-only history) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `MojoLlama / bench_fast_profile.py` 126 lines · 4363 bytes Raw Blame History 1 #!/usr/bin/env python3 2 """Detailed forward pass profiling for FastEngine.""" 3 import sys, os, time, numpy as np 4 sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) 5 6 from mojollama.kernels.fast_engine import FastEngine 7 8 engine = FastEngine('Llama-3.2-1B-Instruct-Q4_0.gguf', n_threads=32) 9 engine.reset() 10 11 # Warmup 12 engine.forward([128000]) 13 engine.forward([9906]) 14 15 print("\n=== Detailed Layer Profile ===\n") 16 17 # Profile each component of layer 0 18 h = engine._emb_cache[128000].astype(np.float32).copy() 19 n_embd = engine.n_embd 20 21 # Time each op 100 times 22 def bench(label, fn, iters=100): 23 # Warmup 24 for _ in range(5): fn() 25 times = [] 26 for _ in range(iters): 27 t0 = time.perf_counter() 28 fn() 29 t1 = time.perf_counter() 30 times.append((t1-t0)*1e6) 31 times.sort() 32 med = times[iters//2] 33 print(f" {label:40s}: {med:8.1f} µs") 34 return med 35 36 # RMS norm 37 t_rms = bench("RMS norm (C kernel)", lambda: engine.rms_norm(h, 'blk.0.attn_norm.weight')) 38 39 # Q projection 40 t_q = bench("Q proj (C OMP)", lambda: engine.matmul('blk.0.attn_q.weight', h, n_embd)) 41 42 # K projection 43 t_k = bench("K proj (C OMP)", lambda: engine.matmul('blk.0.attn_k.weight', h, n_embd)) 44 45 # V projection 46 t_v = bench("V proj (C OMP)", lambda: engine.matmul('blk.0.attn_v.weight', h, n_embd)) 47 48 # RoPE (both Q and K) 49 q = engine.matmul('blk.0.attn_q.weight', h, n_embd) 50 t_rope = bench("RoPE Q+K", lambda: (engine.apply_rope(q, 0, engine.n_head), 51 engine.apply_rope(q, 0, engine.n_kv_head))) 52 53 # Attention (single position, Python loop) 54 k_new = engine.matmul('blk.0.attn_k.weight', h, n_embd) 55 v_new = engine.matmul('blk.0.attn_v.weight', h, n_embd) 56 57 # Reset KV for attention test 58 engine.kv_k[0] = np.zeros((0, engine.n_kv), dtype=np.float32) 59 engine.kv_v[0] = np.zeros((0, engine.n_kv), dtype=np.float32) 60 t_attn = bench("Attention (1 pos, Python+einsum)", lambda: engine.attention(q, k_new, v_new, 0, 0)) 61 62 # O projection 63 att = np.random.randn(engine.n_embd).astype(np.float32) 64 t_o = bench("O proj (C OMP)", lambda: engine.matmul('blk.0.attn_output.weight', att, n_embd)) 65 66 # FFN norm 67 t_ffn_norm = bench("RMS norm FFN (C kernel)", lambda: engine.rms_norm(h, 'blk.0.ffn_norm.weight')) 68 69 # FFN gate 70 t_gate = bench("FFN gate (C OMP)", lambda: engine.matmul('blk.0.ffn_gate.weight', h, n_embd)) 71 72 # SiLU 73 gate_raw = np.random.randn(engine.n_ff).astype(np.float32) 74 t_silu = bench("SiLU (C kernel)", lambda: engine.silu(gate_raw.copy())) 75 76 # FFN up 77 t_up = bench("FFN up (C OMP)", lambda: engine.matmul('blk.0.ffn_up.weight', h, n_embd)) 78 79 # FFN down 80 gate_up = np.random.randn(engine.n_ff).astype(np.float32) 81 t_down = bench("FFN down (C OMP)", lambda: engine.matmul('blk.0.ffn_down.weight', gate_up, engine.n_ff)) 82 83 # Residual add 84 t_resid = bench("Residual add (numpy)", lambda: h + h) 85 86 # KV cache vstack 87 kv_k = np.random.randn(1, engine.n_kv).astype(np.float32) 88 kv_v = np.random.randn(1, engine.n_kv).astype(np.float32) 89 t_kv_update = bench("KV cache update (vstack)", 90 lambda: np.vstack([np.zeros((1, engine.n_kv), dtype=np.float32), kv_k])) 91 92 # Total estimate 93 total_per_layer = (t_rms*2 + t_q + t_k + t_v + t_rope + t_attn + t_o + 94 t_ffn_norm + t_gate + t_silu + t_up + t_down + t_resid*2 + t_kv_update*2) 95 total_16layers = total_per_layer * engine.n_layers 96 tok_per_sec = 1e6 / total_16layers 97 98 print(f"\n=== Summary ===") 99 print(f"Per layer: {total_per_layer/1000:.2f} ms") 100 print(f"Total (16 layers): {total_16layers/1000:.2f} ms") 101 print(f"Estimated decode: {tok_per_sec:.1f} tok/s") 102 print(f"Target: 85 tok/s (llama.cpp)") 103 print(f"Gap: {(85 - tok_per_sec)/85*100:.1f}%") 104 105 # Breakdown by category 106 matmul_total = t_q + t_k + t_v + t_o + t_gate + t_up + t_down 107 other_total = total_per_layer - matmul_total 108 print(f"\nMatmul total: {matmul_total/1000:.2f} ms ({matmul_total/total_per_layer*100:.1f}%)") 109 print(f"Other (Python): {other_total/1000:.2f} ms ({other_total/total_per_layer*100:.1f}%)") 110 111 # Now measure actual forward pass 112 engine.reset() 113 for tok in [128000, 9906]: 114 engine.forward([tok]) 115 116 times = [] 117 next_tok = 3000 118 for i in range(20): 119 t0 = time.perf_counter() 120 logits = engine.forward([next_tok]) 121 t1 = time.perf_counter() 122 times.append((t1-t0)*1000) 123 next_tok = int(np.argmax(logits)) 124 125 times.sort() 126 print(f"\nActual decode (median): {times[10]:.2f} ms/tok = {1000/times[10]:.1f} tok/s")