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 / IMPROVEMENT_ROADMAP.md

379 lines · 16967 bytes Raw Blame History Source

MojoLlama Improvement Roadmap

Date: 2026-05-20 Baseline: v0.5.0 — 41K Python lines, 12K C lines, 5.6K HTML, 17 Mojo files, 60+ architectures


Executive Summary

MojoLlama has a strong core (C engine with AVX2+OMP, MoE optimization, concurrent serving) but suffers from accumulated tech debt from rapid iteration: 24 stale .so files in legacy-c/, 15+ single-use debug/inspect scripts, no unit test framework, no packaging, and a CI that only compiles C kernels without running tests. The gap between the CODING-SOUL vision (Mojo+MAX native) and the current reality (Python + C + llama.cpp dependency) is the biggest strategic risk.


Phase 1: Clean Foundation (Weeks 1-2) — HIGH IMPACT, LOW EFFORT

1.1 Eliminate Dead Code & Build Artifacts

Impact: HIGH | Effort: S

ActionDetails
Remove kernels/legacy-c/ directory24 stale .so files, 12 abandoned .c files (c_engine.c, cengine2.c, cengine3.c, cengine_batch_clean.c, cengine_batch_full_bak.c, etc.). None are imported by current code.
Remove single-use debug scriptscheck_attr.py, debug_forward.py, inspect_gptoss.py, inspect_mxfp4.py, compare_q_proj.py, q4k_compare.py, bench_gpt.py, run_gdb_test.py — all are one-off debugging tools, not production code.
Remove stale .so from kernels/ rootcengine_batch_instr.so, gqa_attention.so, quant_kernels_omp.so, simd_ops.so — should be built, not committed. Add to .gitignore.
Archive numpy_reference.pyPure-numpy reference for correctness validation — move to tools/ or benchmarks/, not top-level.

1.2 Add .gitignore for Build Artifacts

Impact: HIGH | Effort: S

*.so
*.dylib
__pycache__/
*.pyc
*.egg-info/
.venv/

1.3 Consolidate Test Infrastructure

Impact: HIGH | Effort: M

Current state: 12 test files (1,532 lines total), all are ad-hoc scripts that print() results — no assertions, no pytest, no CI integration. Only test_prefix_cache.py uses proper assert statements.

ActionDetails
Add pytest to dependenciespip install pytest
Convert all test_*.py to pytest formatReplace print("[PASS]") with assert, use @pytest.fixture for engine setup, @pytest.mark.parametrize for model variants
Add conftest.pyShared fixtures: model paths, engine instances, thread counts
Add pytest.ini or pyproject.toml [tool.pytest]Test discovery config
Add test job to .onedev-buildspec.ymlRun pytest src/mojollama/tests/ after kernel compilation

1.4 Add pyproject.toml for Packaging

Impact: HIGH | Effort: S

Current state: No pyproject.toml, no setup.py. Project is not pip-installable.

[project]
name = "mojollama"
version = "0.5.0"
description = "High-throughput CPU LLM inference engine"
requires-python = ">=3.11"
dependencies = ["numpy", "gguf", "regex", "ftfy", "flask"]

[project.scripts]
mojollama = "mojollama.__main__:main"

[build-system]
requires = ["setuptools>=68.0"]
build-backend = "setuptools.backends._legacy:_Backend"

Phase 2: Test Coverage & Reliability (Weeks 3-5) — HIGH IMPACT, MEDIUM EFFORT

2.1 Core Engine Tests

Impact: HIGH | Effort: M

TestWhatCoverage Gap
test_cengine_matmul.pyQ4_0, Q4_K, Q5_K, Q6_K, Q8_0, MXFP4 dequant + matmul against numpy referenceZERO — no quant kernel tests exist
test_batch_forward.pyFull forward pass on TinyLlama, verify logits match llama.cpp within tolerancePartial — test_tinyllama.py runs but doesn't assert correctness
test_moe_dispatch.pyExpert routing correctness, top-k selection, shared expert pathZERO — GPT-OSS and Qwen3.6 MoE paths untested
test_ssm_decode.pyMamba-2 selective scan kernel (Qwen3.6 hybrid layers)ZERO — SSM path is the most complex and least tested
test_paged_attention.pyKV cache allocation, page table mapping, physical-logical mappingZERO — critical for concurrent serving
test_rms_norm.pyRMS norm + fused clamp against numpy referenceZERO
test_rope.pyRoPE positional embeddings (full, partial for Qwen3.6)ZERO

2.2 Integration Tests

Impact: HIGH | Effort: L

TestWhat
test_server_api.pyAll 12 API endpoints: /v1/chat/completions, /v1/completions, /v1/models, /health, /api/metrics, etc.
test_concurrent_serving.pyMulti-worker spawn, shared mmap weights, aggregate throughput
test_quantizer.pyAll 35 quant types, round-trip dequant error < threshold
test_architecture_detect.pyAll 60+ architectures load without error from GGUF metadata

2.3 Property-Based Testing

Impact: MED | Effort: M

ActionDetails
Add hypothesis libraryProperty-based testing for quantization: @given(weights=arrays(...))
Fuzz GGUF parserRandom GGUF-like binary data, verify parser doesn't crash
Fuzz token sequencesRandom token sequences through forward pass, no NaN/inf allowed

2.4 Memory Safety Tests

Impact: HIGH | Effort: M

ActionDetails
Add Valgrind/ASAN to CIRun C engine under AddressSanitizer: gcc -fsanitize=address
Test for known heap corruptionREADME mentions "Qwen3.6 MXFP4 cleanup crash — heap corruption on exit" — write a regression test
Leak detectionRun forward pass 1000x, verify RSS doesn't grow

Phase 3: Performance Optimization (Weeks 6-8) — HIGH IMPACT, LARGE EFFORT

3.1 Profile-Guided Optimization

Impact: HIGH | Effort: M

Current state: profile_qwen36.py exists but results are not documented or acted upon.

ActionDetails
Run per-component profilingcProfile + line_profiler on Qwen3.6-35B, GPT-OSS-20B, TinyLlama
Publish flamegraphpy-spy or perf output as SVG in repo
Identify top 3 bottlenecksData-driven, not guessed

Expected bottlenecks (educated guess, needs verification):

  1. Python→C ctypes call overhead in turbo_engine_v7_moe.py (24 ctypes calls per layer for MoE)
  2. Q8_0 input quantization in Python before C matmul (should be in C)
  3. MoE expert dispatch routing (softmax + top-k in Python)

3.2 Move Q8_0 Quantization to C

Impact: HIGH | Effort: M

README explicitly lists this as near-term: "Q8_0 input quantization in C engine batch_matmul functions — match quant_kernels_omp performance (2-4× per-matmul improvement)"

ActionDetails
Add quantize_q8_0_avx2() to cengine_batch_instr.cSIMD quantization of F32→Q8_0
Update batch_forward() to accept F32 input, quantize internallyEliminates Python quantization step
Benchmark before/afterTarget: 2-4× per-matmul improvement

3.3 Reduce ctypes Call Overhead

Impact: HIGH | Effort: L

Current state: moe_forward_omp() already fuses expert calls, but the per-layer loop still does Python→C transitions for attention, norm, RoPE, etc.

ActionDetails
Fuse entire transformer block into single C callblock_forward(layer_id, tokens, kv_cache) replaces 5+ ctypes calls
Pre-compute RoPE cos/sin tables in C, not PythonEliminates per-request table lookup
Batch norm + SiLU + residual into single SIMD passfused_norm_silu_residual()

3.4 Continuous Batching

Impact: HIGH | Effort: XL

README lists this as near-term: "True continuous batching — merge B users' MoE routing into single batched matmuls"

ActionDetails
Implement token-level schedulingNot request-level — schedule individual tokens across users
Merge MoE routing across batchSingle top-k computation for all active tokens
Dynamic KV cache managementPagedAttention already exists, needs integration with scheduler

Phase 4: Code Quality & Architecture (Weeks 9-12) — MED IMPACT, LARGE EFFORT

4.1 Unified Engine Interface

Impact: MED | Effort: L

Current state: Three separate engines with incompatible APIs:

ActionDetails
Define Engine ABCload(), forward(), reset(), get_logits(), get_vocab()
Refactor all engines to implement EngineSingle interface for server, studio, benchmarks
Factory functioncreate_engine(model_path) → Engine auto-detects MoE/dense/SSM

4.2 Remove llama.cpp Dependency

Impact: HIGH | Effort: XL

Current state: server.py hardcodes paths to llama.cpp binaries:

LLAMA_SERVER_PATH = "/tmp/llama.cpp/build/bin/llama-server"
CONVERTER_PATH = "/tmp/llama.cpp/convert_hf_to_gguf.py"

backends.py routes to llama.cpp as the primary CPU backend.

ActionDetails
Make llama.cpp optional fallbackAutoBackend should prefer native C engine, fall back to llama.cpp
Bundle GGUF converterReplace convert_hf_to_gguf.py dependency with native converter in quantizer.py
Remove hardcoded pathsUse shutil.which() or config file

4.3 Error Handling & Input Validation

Impact: MED | Effort: M

Current state: Minimal error handling. server.py returns 500 on any exception. turbo_engine_v7_moe.py has no input validation.

ActionDetails
Add custom exception hierarchyMojoLlamaError, ModelError, QuantError, BackendError
Validate all user inputs in serverModel path exists, GGUF format valid, quant type supported
Graceful degradationIf C engine fails, fall back to numpy (slow but correct)
Add request timeoutPrevent hanging on malformed prompts

4.4 Type Hints & Docstrings

Impact: LOW | Effort: L

Current state: 41K Python lines, ~0% type hint coverage. Module docstrings exist but function-level docs are sparse.

ActionDetails
Add mypy to CIStart with --ignore-missing-imports, gradually tighten
Add type hints to public APIsserver.py, studio.py, engine interfaces
Add docstrings to all public functionsUse Google or NumPy style consistently

Phase 5: Feature Gaps (Weeks 13-16) — MED IMPACT, LARGE EFFORT

5.1 Complete GGUF Quant Pipeline

Impact: MED | Effort: L

Current state: quantizer.py has 35 quant types but no imatrix support.

ActionDetails
Implement importance matrix generationmojollama imatrix -m model.gguf -d calibration.txt
IQ2/IQ3/IQ4 quantizationImport-aware quantization for better quality at low bits
Per-tensor quant type selectionMix Q4_K for attention, Q2_K for FFN

5.2 Speculative Decoding

Impact: MED | Effort: L

Current state: speculative.py exists but is not integrated into server.

ActionDetails
Integrate into server--draft-model tinyllama.gguf flag
Shared vocabulary optimizationCurrent 21% acceptance rate → target 40%+
Benchmark vs baselineMeasure speedup on different model pairs

5.3 Prefix Caching Integration

Impact: MED | Effort: S

Current state: prefix_cache.py exists with good tests but is not wired into server.

ActionDetails
Wire into server request pipelineCheck cache before forward pass
Multi-tenant prefix cachePer-user or shared cache
Benchmark cache hit rateReal-world conversation patterns

5.4 Mojo Kernel Integration

Impact: HIGH (strategic) | Effort: XL

Current state: 17 Mojo files exist but are not compiled or used. bridge.py is a "placeholder" per CODING-SOUL.

ActionDetails
Compile Mojo kernels with nightlyq4_matmul.mojo, norms.mojo, attention.mojo
FFI bridge from Python to MojoReplace C engine hot paths with Mojo equivalents
Benchmark Mojo vs CVerify Mojo SIMD matches or beats AVX2 C
MAX GPU backendWhen MAX becomes installable, integrate graph compiler

Phase 6: Deployment & DX (Weeks 17-18) — MED IMPACT, MEDIUM EFFORT

6.1 CI/CD Improvements

Impact: HIGH | Effort: M

Current state: .onedev-buildspec.yml has 4 jobs but:

ActionDetails
Add test job to buildspecRun pytest after kernel compilation, block merge on failure
Add lint jobruff check src/, ruff format --check src/
Add benchmark regression jobRun bench_batch_gptoss.py, compare against baseline, fail if >10% regression
Build Docker on dev tooCatch Docker issues before merge to main
Add matrix buildsTest on AVX2, AVX512, NEON (if CI runners support it)

6.2 Developer Experience

Impact: MED | Effort: M

ActionDetails
Add Makefile at project rootmake build, make test, make lint, make bench, make docker
Add justfile or noxfile.pyCross-platform task runner
Add pre-commit hooksruff, mypy, pytest on git commit
Add CONTRIBUTING.mdHow to build, test, submit PRs
Add architecture decision records (ADRs)docs/adrs/ — why C engine over llama.cpp, why multiprocessing over threading, etc.

6.3 Documentation

Impact: MED | Effort: L

GapAction
No API docsGenerate with pdoc or sphinx — publish to ~site/api/
No architecture docsdocs/architecture.md — engine design, data flow, memory model
No quantization guidedocs/quantization.md — which quant type for which use case
No troubleshooting guidedocs/troubleshooting.md — common errors, heap corruption, OOM
README is benchmark-heavyAdd quick-start tutorial, model compatibility table, FAQ

6.4 Distribution

Impact: MED | Effort: L

ActionDetails
Publish to PyPIpip install mojollama (with pre-built wheels for Linux x86_64)
Build wheels with C extensionscibuildwheel for manylinux2014
Homebrew formulabrew install mojollama for macOS
Static binaryPyInstaller or cx_Freeze for single-file distribution
Desktop appComplete desktop/ Electron app (exists but incomplete)

Priority Matrix

PriorityItemImpactEffortPhase
P0Remove dead code + .gitignoreHIGHS1
P0Add pytest + CI test jobHIGHM1-2
P0Add pyproject.tomlHIGHS1
P1C engine quant kernel testsHIGHM2
P1Server API integration testsHIGHM2
P1Move Q8_0 quant to CHIGHM3
P1Fix heap corruption bugHIGHS2
P2Unified Engine interfaceMEDL4
P2Remove llama.cpp dependencyHIGHXL4
P2Error handling + validationMEDM4
P2CI lint + benchmark jobsHIGHM6
P3Mojo kernel compilationHIGHXL5
P3Continuous batchingHIGHXL3
P3PyPI + wheel distributionMEDL6
P4Type hints + docstringsLOWL4
P4Speculative decoding integrationMEDL5
P4Prefix caching integrationMEDS5

Risk Assessment

RiskLikelihoodImpactMitigation
Mojo pointer APIs don't stabilizeMediumHighKeep C engine as fallback (CODING-SOUL already plans this)
MAX graph compiler never shipsLowMediumC engine is already competitive; MAX is optional
llama.cpp removes GGUF compatibilityVery LowHighGGUF spec is open; we can maintain our own reader
Heap corruption in MXFP4 path is systemicMediumHighASAN testing in CI, isolate MXFP4 code path
Python GIL limits concurrent servingLowMediumAlready using multiprocessing; consider multiprocessing → ray migration

Metrics to Track

MetricCurrentTargetHow
Test coverage~5% (ad-hoc scripts)60%+pytest --cov
CI pass rateN/A (no tests)95%+OneDev build status
Qwen3.6-35B tok/s25.5 (1 user)35+ (1 user)bench_batch_qwen36.py
Concurrent throughput47 agg (10 users)70+ agg (10 users)bench_concurrent_qwen36.py
Memory leaksKnown (heap corruption)ZeroASAN + RSS monitoring
Dead code files40+0git ls-files audit
Time to first inference4s (worker load)<2smmap optimization