MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / _final_check.py
| 1 | """Final verification: check correctness and benchmark.""" |
| 2 | import sys, os, time |
| 3 | import numpy as np |
| 4 | sys.path.insert(0, 'src') |
| 5 | |
| 6 | from mojollama.kernels.turbo_engine_v7_moe import TurboEngineV7MoE |
| 7 | |
| 8 | print("=== Loading model ===") |
| 9 | engine = TurboEngineV7MoE('/onedev-workspace/work/Qwen3-30B-A3B-Q4_K_M.gguf', n_threads=32) |
| 10 | |
| 11 | # Verify basic properties |
| 12 | print(f"\n=== Configuration ===") |
| 13 | print(f" head_dim: {engine.head_dim} (expect 128)") |
| 14 | print(f" NKH = {engine.n_kv_head} * {engine.head_dim} = {engine.n_kv_head * engine.head_dim} (expect 512)") |
| 15 | print(f" NK = {engine.n_head} * {engine.head_dim} = {engine.n_head * engine.head_dim} (expect 4096)") |
| 16 | print(f" Q norm: {engine._layers[0].has_q_norm}") |
| 17 | print(f" K norm: {engine._layers[0].has_k_norm}") |
| 18 | |
| 19 | # Test with deterministic output |
| 20 | engine.reset() |
| 21 | out0 = engine.forward(0) |
| 22 | top5 = np.argsort(out0)[-5:][::-1] |
| 23 | print(f"\n Token 0 top-5 IDs: {top5.tolist()}") |
| 24 | print(f" Token 0 top-5 logits: {out0[top5].tolist()}") |
| 25 | print(f" No NaN: {not np.any(np.isnan(out0))}") |
| 26 | |
| 27 | # Multi-token generation |
| 28 | engine.reset() |
| 29 | tokens = [] |
| 30 | tok = 0 |
| 31 | for i in range(20): |
| 32 | logits = engine.forward(tok) |
| 33 | tok = int(np.argmax(logits)) |
| 34 | tokens.append(tok) |
| 35 | print(f" Generated 20 tokens (IDs): {tokens}") |
| 36 | print(f" No NaN: {all(not np.any(np.isnan(engine.forward(t))) for t in tokens[:3])}") |
| 37 | |
| 38 | # Full benchmark |
| 39 | print(f"\n=== Performance benchmark ===") |
| 40 | times = [] |
| 41 | engine.reset() |
| 42 | for _ in range(10): |
| 43 | t0 = time.perf_counter() |
| 44 | engine.forward(0) |
| 45 | times.append(time.perf_counter() - t0) |
| 46 | |
| 47 | t_prompt = np.median(sorted(times)) |
| 48 | print(f" Prompt (first token): {t_prompt*1000:.1f} ms (small KV)") |
| 49 | |
| 50 | # Generation benchmark (single-token, warm + timed) |
| 51 | engine.reset() |
| 52 | for i in range(10): |
| 53 | engine.forward(i) |
| 54 | |
| 55 | times_gen = [] |
| 56 | tok = 1 |
| 57 | for _ in range(10): |
| 58 | t0 = time.perf_counter() |
| 59 | logits = engine.forward(tok) |
| 60 | times_gen.append(time.perf_counter() - t0) |
| 61 | tok = int(np.argmax(logits)) |
| 62 | |
| 63 | times_gen.sort() |
| 64 | med_gen = np.median(times_gen) |
| 65 | print(f" Generation (median of 10): {med_gen*1000:.1f} ms/tok = {1/med_gen:.1f} tok/s") |
| 66 | print(f" Min: {min(times_gen)*1000:.1f} Max: {max(times_gen)*1000:.1f}") |
| 67 | print(f"\n vs llama.cpp baseline: 26.9 tok/s (Q4_K_M, t=32)") |
| 68 | print(f" Speed ratio: {(1/med_gen) / 26.9 * 100:.1f}%") |