MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / benchmarks / bench_concurrent_mp.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Qwen3.6 MXFP4 concurrent server via multiprocessing with shared mmap weights.""" |
| 3 | import sys, os, time, multiprocessing as mp |
| 4 | import numpy as np |
| 5 | |
| 6 | MODEL = "/tmp/models/qwen3.6-mxfp4.gguf" |
| 7 | N_USERS = 10 |
| 8 | GEN_TOKENS = 50 |
| 9 | N_WARMUP = 10 |
| 10 | |
| 11 | # ── Load model once in parent ── |
| 12 | sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'kernels')) |
| 13 | print(f"Loading Qwen3.6 MXFP4 (shared via mmap)...", flush=True) |
| 14 | |
| 15 | # Use fork context for shared memory |
| 16 | mp.set_start_method('fork', force=True) |
| 17 | |
| 18 | # Pre-load the GGUF data into a shared dict |
| 19 | import gguf |
| 20 | reader = gguf.GGUFReader(MODEL) |
| 21 | |
| 22 | # ── Worker function ── |
| 23 | def worker_fn(worker_id, result_queue): |
| 24 | """Each worker loads a fresh engine from the shared GGUF reader.""" |
| 25 | os.environ['OMP_NUM_THREADS'] = '32' |
| 26 | sys.path.insert(0, '/onedev-workspace/work/src/mojollama/kernels') |
| 27 | |
| 28 | from turbo_engine_v7_moe import TurboEngineV7MoE |
| 29 | |
| 30 | # Each worker loads independently — weights are mmap'd, physical pages shared |
| 31 | e = TurboEngineV7MoE(MODEL, 32) |
| 32 | |
| 33 | # Warmup |
| 34 | for _ in range(N_WARMUP): |
| 35 | e.forward(np.array([[1]], dtype=np.int32)) |
| 36 | e.reset() |
| 37 | |
| 38 | # Benchmark: generate GEN_TOKENS tokens |
| 39 | tok = np.array([[1]], dtype=np.int32) |
| 40 | times = [] |
| 41 | for _ in range(GEN_TOKENS): |
| 42 | t0 = time.perf_counter() |
| 43 | logits = e.forward(tok) |
| 44 | elapsed = time.perf_counter() - t0 |
| 45 | times.append(elapsed) |
| 46 | tok = np.array([[int(np.argmax(logits))]], dtype=np.int32) |
| 47 | |
| 48 | avg_ms = np.mean(times[5:]) * 1000 |
| 49 | tps = 1000 / avg_ms |
| 50 | result_queue.put((worker_id, avg_ms, tps)) |
| 51 | |
| 52 | # ── Spawn workers ── |
| 53 | print(f"Spawning {N_USERS} workers (fork + shared mmap)...", flush=True) |
| 54 | result_queue = mp.Queue() |
| 55 | workers = [] |
| 56 | |
| 57 | t_start = time.perf_counter() |
| 58 | for i in range(N_USERS): |
| 59 | p = mp.Process(target=worker_fn, args=(i, result_queue)) |
| 60 | workers.append(p) |
| 61 | p.start() |
| 62 | |
| 63 | # Collect results |
| 64 | results = {} |
| 65 | for _ in range(N_USERS): |
| 66 | wid, avg_ms, tps = result_queue.get() |
| 67 | results[wid] = (avg_ms, tps) |
| 68 | |
| 69 | # Wait for all |
| 70 | for p in workers: |
| 71 | p.join() |
| 72 | |
| 73 | total_time = time.perf_counter() - t_start |
| 74 | |
| 75 | # ── Report ── |
| 76 | print(f"\n{'='*60}") |
| 77 | print(f" Qwen3.6 MXFP4 — Concurrent ({N_USERS} users, mp.fork)") |
| 78 | print(f"{'='*60}") |
| 79 | avg_tps_all = sum(t for _, t in results.values()) / len(results) |
| 80 | min_tps = min(t for _, t in results.values()) |
| 81 | max_tps = max(t for _, t in results.values()) |
| 82 | best_ms = min(m for m, _ in results.values()) |
| 83 | worst_ms = max(m for m, _ in results.values()) |
| 84 | |
| 85 | for wid, (avg_ms, tps) in sorted(results.items()): |
| 86 | print(f" Worker {wid:2d}: {avg_ms:5.1f}ms → {tps:5.1f} tok/s") |
| 87 | |
| 88 | print(f"{'─'*60}") |
| 89 | print(f" Average: {avg_tps_all:.1f} tok/s per user") |
| 90 | print(f" Best: {max_tps:.1f} tok/s ({best_ms:.1f}ms)") |
| 91 | print(f" Worst: {min_tps:.1f} tok/s ({worst_ms:.1f}ms)") |
| 92 | print(f" Aggregate: {avg_tps_all * N_USERS:.0f} tok/s ({N_USERS} users)") |
| 93 | print(f" Wall time: {total_time:.1f}s") |
| 94 | print(f" Speedup vs single: {avg_tps_all * N_USERS / avg_tps_all:.0f}x") |