MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / BENCHMARK_CHANGELOG.md
| 1 | # MojoLlama Performance Optimization Changelog |
| 2 | |
| 3 | > Systematic, benchmarked improvements to surpass llama.cpp performance. |
| 4 | > Every change is measured — no guessing. |
| 5 | |
| 6 | ## System |
| 7 | |
| 8 | - **CPU:** AMD Ryzen Threadripper 3970X 32C/64t (AVX2+FMA, no AVX-512) |
| 9 | - **RAM:** 251.6 GB |
| 10 | - **Compiler:** GCC with `-O3 -mavx2 -mfma -mf16c -fopenmp` |
| 11 | |
| 12 | ## Baseline (2026-05-20) |
| 13 | |
| 14 | | Engine | Model | Threads | PP tok/s | TG tok/s | TG median ms | TG p95 ms | |
| 15 | |--------|-------|---------|----------|----------|--------------|-----------| |
| 16 | | v77 | Llama-3.2-1B Q4_0 | 32 | 53.2 | 47.4 | 21.15 | 21.71 | |
| 17 | | v7_moe | Qwen3-30B-A3B Q4_K_M | 32 | — | 20.96* | 47.71* | 192.64* | |
| 18 | | v7_moe | Qwen3-30B-A3B Q4_K_M | 32 | 17.7 | 16.3 | 61.08 | 64.16 | |
| 19 | |
| 20 | *First measurement was with 20 tokens, not representative. 30-token measurement is the real baseline. |
| 21 | |
| 22 | ## Optimization Log |
| 23 | |
| 24 | ### Step 1: P0.1 — Fix MoE atomic bottleneck in `moe_forward_omp` |
| 25 | - **Date:** 2026-05-20 |
| 26 | - **File:** `src/mojollama/kernels/quant_kernels_omp.c` |
| 27 | - **Change:** Tried per-thread buffers (calloc overhead worse) and loop restructure (4x OMP fork/join overhead worse) |
| 28 | - **Result:** REGRESSION — reverted to original. Atomic overhead is negligible vs matmul cost. |
| 29 | |
| 30 | ### Step 2: P0.2 — SIMD exp approximation for SiLU |
| 31 | - **Date:** 2026-05-20 |
| 32 | - **File:** `src/mojollama/kernels/quant_kernels_omp.c` |
| 33 | - **Change:** Added `silu_fast()` using Pade tanh approximation, replaced `expf` in MoE SiLU loop |
| 34 | - **Result:** 16.3 tok/s (baseline). SiLU is too small fraction to matter — matmul dominates. |
| 35 | |
| 36 | ### Step 3: Remove np.clip + np.copyto overhead |
| 37 | - **Date:** 2026-05-20 |
| 38 | - **File:** `src/mojollama/kernels/turbo_engine_v7_moe.py` |
| 39 | - **Change:** Removed 3× `np.clip()` calls, replaced `np.copyto()` with slice assignment |
| 40 | - **Result:** 16.4 tok/s (+0.6%). Small win — clip was ~1% of total. |
| 41 | |
| 42 | ### Step 4: Replace numpy residual with C `residual_add` |
| 43 | - **Date:** 2026-05-20 |
| 44 | - **File:** `src/mojollama/kernels/turbo_engine_v7_moe.py` |
| 45 | - **Change:** Replaced `b_x[:N] = b_r[:N] + b_oproj[:N]` with `simd.residual_add()`. Also tried C RoPE (regression, reverted). |
| 46 | - **Result:** 16.1 tok/s (-1.2%). ctypes call overhead > numpy slice add for 2048 floats. |
| 47 | - **Action:** Reverted. Keep numpy for small vector ops. |
| 48 | |
| 49 | ### Step 5: P1.1 — GQA Attention SIMD softmax + workspace buffer |
| 50 | - **Date:** 2026-05-20 |
| 51 | - **Files:** `src/mojollama/kernels/gqa_attention.c`, `src/mojollama/kernels/turbo_engine_v7_moe.py` |
| 52 | - **Change:** |
| 53 | - Added SIMD softmax (vectorized exp via exp2 decomposition, AVX2 max/exp/accumulate) |
| 54 | - Replaced stack-allocated `scores[4096]` with pre-allocated workspace buffer |
| 55 | - Added `workspace` float pointer parameter to `gqa_attention_decode` |
| 56 | - **Result:** No measurable change at seq_len=512 (attention <1% of total time). Enables longer contexts. |
| 57 | |
| 58 | ### Step 6: P1.2 — gpt-oss-20b support + MXFP4 kernel tuning |
| 59 | - **Date:** 2026-05-20 |
| 60 | - **Model:** `gpt-oss-20b-MXFP4` (24L/2880D/MoE-32x4, MXFP4 quant, 12GB GGUF) |
| 61 | - **Baseline:** 21.2 tok/s (47.14ms median) |
| 62 | - **Changes tried:** |
| 63 | - 4-block unrolling of `mxfp4_row_dot_f32` — no benefit (GCC already unrolls) |
| 64 | - Sequential per-expert gate+up/down — no benefit (OMP fork-join overhead ~7%) |
| 65 | - Q8 quantization of SiLU before down matmul — regression (quant cost > savings) |
| 66 | - **Winning change:** `-march=native` + `OMP_PROC_BIND=close` |
| 67 | - **Result:** 21.6 tok/s (+1.9%) |
| 68 | - **Build:** `gcc -O3 -march=native -fopenmp -shared -fPIC -o quant_kernels_omp.so quant_kernels_omp.c -lm` |
| 69 | - **Benchmark:** `OMP_PROC_BIND=close OMP_PLACES=cores python3 -m src.mojollama.benchmarks.bench_tok -m gpt-oss-20b-MXFP4.gguf` |
| 70 | |
| 71 | ## Latest Results |
| 72 | |
| 73 | | Engine | Model | Threads | PP tok/s | TG tok/s | TG median ms | TG p95 ms | |
| 74 | |--------|-------|---------|----------|----------|--------------|-----------| |
| 75 | | v77 | Llama-3.2-1B Q4_0 | 32 | 54.4 | 48.9 | 20.43 | 20.99 | |
| 76 | | v7_moe | Qwen3-30B-A3B Q4_K_M | 32 | 17.7 | 16.5 | 60.54 | 61.12 | |
| 77 | | v7_moe | gpt-oss-20b-MXFP4 | 32 | 22.2 | 21.6 | 46.14 | 48.66 | |
| 78 | |
| 79 | ## Benchmark Commands |
| 80 | |
| 81 | ```bash |
| 82 | # Single model baseline |
| 83 | python3 src/mojollama/benchmarks/bench_tok.py -m model.gguf -t 32 --measured 30 |
| 84 | |
| 85 | # Thread sweep |
| 86 | python3 src/mojollama/benchmarks/bench_tok.py -m model.gguf -t 1,2,4,8,16,24,32 --save results.json |
| 87 | |
| 88 | # Compare with previous |
| 89 | python3 src/mojollama/benchmarks/bench_tok.py --load benchmarks/baseline.json --load benchmarks/step1.json |
| 90 | |
| 91 | # Concurrency test |
| 92 | python3 src/mojollama/benchmarks/bench_tok.py -m model.gguf --concurrent "1,2,4,8,16" |
| 93 | |
| 94 | # Kernel-level benchmark |
| 95 | python3 src/mojollama/kernels/benchmarks/bench_c_avx2.py |
| 96 | ``` |
| 97 | |
| 98 | ## Rebuild Commands |
| 99 | |
| 100 | ```bash |
| 101 | # Rebuild all C kernels |
| 102 | cd src/mojollama/kernels && gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC -o quant_kernels_omp.so quant_kernels_omp.c -lm |
| 103 | cd src/mojollama/kernels && gcc -O3 -mavx2 -mfma -fopenmp -shared -fPIC -o gqa_attention.so gqa_attention.c -lm |
| 104 | cd src/mojollama/kernels && gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC -o simd_ops.so simd_ops.c -lm |
| 105 | cd src/mojollama/kernels && gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC -o cengine_batch_instr.so cengine_batch_instr.c -lm |
| 106 | ``` |