interplane.Get Omega
Read in your language

MojoLlama

Archived Mojo/MAX quantizer and serving research repository (read-only history)

Brought by drakestapleton

branch main Branches Tags History

MojoLlama / .hermes / plans / 2026-05-19_1527-mojollama-production-engine.md

288 lines · 13092 bytes Raw Blame History Source

MojoLlama: Production-Grade Universal Inference Engine — Implementation Plan

Strategy: Profile-first methodology. Every optimization is guided by measurement. Each phase produces a benchmark-verified improvement before the next begins.

Vision: MojoLlama runs any model (GGUF all-types, MXFP4, HuggingFace Transformers) on any hardware (CPU/GPU) at or above llama.cpp performance, with auto-tuning, integrated Studio UI, and competitive with vLLM/SGLang.

Architecture: C AVX2+FMA+OMP batch engine (cengine_batch.c) as production inference path, Mojo SIMD kernels as R&D path for future GPU/MAX hardware, Python bridge for server/studio integration.

Hardware: AMD Threadripper 3970X (32C/64T), 251GB DDR4, AVX2+FMA+F16C


Phase 0: Fix MoE Engine (blocker)

Goal: Get native MoE engine generating coherent text.

Task 0.1: Set emb_quant=12 in server_moe.py

Task 0.2: Verify single-token correctness


Phase 1: Benchmark Baseline ✅

Goal: Know exactly where we stand on every model.

Task 1.1: Single-token latency benchmark ✅

Enginems/toktok/sRatio
MojoLlama (MoE C engine)45.6 ms21.9 tok/s—
llama.cpp (tg128)39.4 ms25.4 tok/sbaseline
MojoLlama vs llama.cpp——86%

Task 1.2: Concurrent benchmark ✅

Concurrencyp50 lat (ms)p95 lat (ms)Throughput (tok/s)
12,4462,5948.1
24,8724,9648.2
49,7339,9688.1
813,71621,4948.1

Task 1.3: Profile the hot path ✅

Categoryms/tok%
MoE FFN gate/up/down20.04 ms40.8% ← #1 tied
Attention QKV20.09 ms40.9% ← #1 tied
Output projection4.94 ms10.1%
Router3.41 ms6.9%
RMS norms0.61 ms1.2%
Emb lookup0.01 ms0.0%
Total49.11 ms20.4 tok/s

#1 Bottleneck: MoE FFN + Attention (tied at ~41% each)


Phase 2: Performance Optimization ✅

Goal: Match or exceed llama.cpp on all supported quant types.

Task 2.1: AVX2-vectorize Q6_K dequant loop ✅

Task 2.2: Add Q5_K batch matmul ✅

Task 2.3: Add Q3_K and Q2_K batch matmuls ✅

Task 2.4: Add MXFP4 (type 39) support for GPT-OSS-20B ✅

Task 2.5: Fused QKV projection ✅

Task 2.6: RoPE precompute table ✅

End-to-end results (Qwen3-30B-A3B Q4_K_M, 32 threads):

MetricBefore Phase 2After Phase 2Change
Profile: ms/tok49.1 ms47.0 ms-4.3%
Profile: tok/s20.421.3+4.4%
Benchmark: ms/tok45.6 ms44.5 ms-2.4%
Benchmark: tok/s21.922.5+2.7%

Analysis: Gains are modest because the bottleneck is memory bandwidth, not compute. The C engine reads weights from RAM for every matmul (Qwen3 is 18GB), and with 251 GB/s DDR4 bandwidth, dequant+dot is already near-optimal. Further gains require KV cache optimization, operator fusion to reduce memory traffic, or multi-batch to amortize weight loading.


Phase 3: HuggingFace Transformer Support ✅

Goal: Load any HF model, convert to GGUF, run in MojoLlama.

Task 3.1: HF → GGUF conversion pipeline ✅

Task 3.2: Dynamic architecture detection ✅

Task 3.3: All-GGUF-type GGUF reader ✅


Phase 4: Quantization Pipeline

Goal: Quantize any HF model to any GGUF type.

Task 4.1: imatrix generation

Task 4.2: Multi-quant export

Task 4.3: Dynamic quantization (2-bit adaptive)


Phase 5: Auto-Tune System ✅

Goal: Per-model, per-hardware optimal settings.

Task 5.1: Benchmark sweep ✅

Task 5.2: Concurrency tuning ✅

Task 5.3: Persistent config ✅


Phase 6: Studio Integration ✅

Goal: All features accessible from Studio UI.

Task 6.1: Engine selection in Studio ✅

Task 6.2: Benchmark dashboard ✅

Task 6.3: Quantization UI ✅

Task 6.4: Auto-tune UI ✅


Phase 7: vLLM/SGLang Competitive ✅

Goal: Competitive on CPU. Path to GPU parity.

Task 7.1: PagedAttention for KV cache ✅

Task 7.2: Continuous batching (scheduler) ✅

Task 7.3: Prefix caching ✅

Task 7.4: Speculative decoding ✅


Immediate Next Steps

Start with Phase 0 — this unblocks all future work. Then Phase 1 Task 1.3 — profile to find the REAL bottleneck before optimizing anything.

# After Phase 0, profile with:
cd /onedev-workspace/work/src/mojollama && \
OMP_NUM_THREADS=32 python3 -u -c "
import time, numpy as np
# Profile each component of the forward pass
# ...
"