MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
GitHub aien-dev/MojoLlama#2 (code only)
Apply it
git checkout -b patch-d66cc2eba4e7746b 5545d451b6218f7f84c5fe89d54e01cc03f37b97 curl -fsSL https://interplane.io/drakestapleton/MojoLlama/patches/d66cc2eba4e7746b.patch | git am
Provenance
github-import 1
aien-dev/MojoLlama
2
27a27b74e85b7642f4e7dca71b2fc40f47e2f321
{"kind":"github-import","owner":"aien-dev","repo":"MojoLlama","number":2,"url":"https://github.com/aien-dev/MojoLlama/pull/2","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:40:59Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/README.md b/README.md index 02d8ec5..e9d8133 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ **Highβthroughput CPU LLM inference engine**: GGUF native, MoE-optimized, with a **pure Mojo inference pipeline** and full Studio suite. -     +     --- @@ -17,14 +17,14 @@ MojoLlama is a CPU-first inference engine that provides **two engines** for diff ### 1. Pure Mojo Engine (NEW) A **zero-C, zero-Python** inference pipeline written entirely in Mojo. Uses Mojo's SIMD + `parallelize` for all compute kernels with C libc imports only for I/O (`malloc`, `open`, `read`). Features: - **Q8_0 quantized matmul**: int8βfloat32 SIMD decode with FMA fusion -- **Batched inference**: 1-8 concurrent sequences via comptime dispatch +- **Batched inference**: batches of 2, 4 or 8 sequences, chosen at compile time (`comptime B`, set to 4 in the checked-in sources) - **Full architecture support**: GQA attention, RoPE, RMS norm, SiLU activations - **Running on real models**: - - **TinyLlama-1.1B** f16/Q8_0: **27.2 tok/s** (verified correct output) + - **TinyLlama-1.1B** f16: **27.2 tok/s** on one sequence (verified correct output); Q8_0 with a batch of 4: **88.1 tok/s** - **ZAYA1-8B** Q8_0 (MoE, 80 layers, 16 experts): **28.6 tok/s** (real English text output) ### 2. C Engine (Performance) -The C engine (`cengine_batch_instr.c`) provides AVX2+OMP-accelerated operations for production workloads: +The C engine (`src/mojollama/kernels/cengine_batch_instr.c`) provides AVX2+OMP-accelerated operations. The numbers below were measured through the Python TurboEngine, which calls these C kernels: - **GPT-OSS-20B** Q4_K_M: **25.5 tok/s** (0.94Γ llama.cpp) - **Qwen3.6-35B** MXFP4: **25.5 tok/s** (1.57Γ llama.cpp) with concurrent serving at **47 tok/s aggregate** @@ -33,17 +33,22 @@ The C engine (`cengine_batch_instr.c`) provides AVX2+OMP-accelerated operations ## π Pure Mojo Quick Start ```bash -# Build and run TinyLlama-1.1B f16 (proven benchmark) cd src/mojollama/mojo_engine -bash build.sh -LD_LIBRARY_PATH="..." ./tinyllama_benchmark 32 -# Build and run ZAYA1-8B Q8_0 (28.6 tok/s) +# Build and run ZAYA1-8B Q8_0 (28.6 tok/s); the argument is the worker thread count (default 32) bash build_zaya.sh -LD_LIBRARY_PATH="..." ./zaya_gen_q8 32 +LD_LIBRARY_PATH="$HOME/.local/share/uv/tools/mojo/lib/python3.11/site-packages/modular/lib" ./zaya_gen_q8 32 + +# Build TinyLlama-1.1B Q8_0 with a batch of 4 (88.1 tok/s) +bash build_q8.sh ``` -All matmul kernels are pure Mojo (no C bridge). See `zaya_gen_q8.mojo` (~700 lines) for the complete reference implementation. +Requirements, from the scripts in `src/mojollama/mojo_engine/`: +- Mojo 0.26.2 and `gcc` (used to link). The build scripts hard-code the Mojo library path shown above, which is where a `uv` tool install of Mojo with Python 3.11 puts it. +- Pre-extracted model files in fixed `/tmp` paths. ZAYA1-8B needs `/tmp/weights_zaya/` and `/tmp/vocab_zaya.bin`, which `extract_zaya_q8.py` and `extract_zaya_vocab.py` write from `/tmp/models/ZAYA1-8B-Q8_0.gguf`. TinyLlama needs `/tmp/weights_tl/` and `/tmp/vocab.bin` (and `/tmp/prompt_tokens.bin` for `tinyllama_benchmark`); see `REAL_BENCHMARK_RESULTS.md` in that folder. +- `tinyllama_benchmark.mojo` (the f16 27.2 tok/s figure) has no build script and takes no arguments. `build.sh` builds a different program (`mojo_engine_v6_final.mojo`) and needs a `read_helpers.c` that is not in the repository, so it does not work as is. + +The matmul kernels are pure Mojo (no C bridge); `zaya_gen_q8.mojo` imports `malloc`, `open`, `read`, `lseek` and `close` from libc. See `zaya_gen_q8.mojo` (~700 lines) for the complete reference implementation. The model-extraction steps are Python scripts. --- @@ -51,7 +56,8 @@ All matmul kernels are pure Mojo (no C bridge). See `zaya_gen_q8.mojo` (~700 lin | Model | Engine | Config | tok/s | vs llama.cpp | |-------|--------|--------|-------|-------------| -| **TinyLlama 1.1B** f16 | **Pure Mojo** | B=4 Γ 32 thr | **27.2** | - | +| **TinyLlama 1.1B** f16 | **Pure Mojo** | 1 sequence Γ 32 thr | **27.2** | - | +| **TinyLlama 1.1B** f16 | **Pure Mojo** | B=4 Γ 32 thr | **77.8** | - | | **TinyLlama 1.1B** Q8_0 | **Pure Mojo** | B=4 Γ 32 thr | **88.1** | - | | **ZAYA1-8B** Q8_0 | **Pure Mojo** | 80L, 16 experts, top-1 | **28.6** π | - | | GPT-OSS-20B Q4_K_M | Python TurboEngine | 1 user | 25.5 | **0.94Γ** | @@ -61,11 +67,13 @@ All matmul kernels are pure Mojo (no C bridge). See `zaya_gen_q8.mojo` (~700 lin | TinyLlama 1.1B Q4_0 | Python TurboEngine | 1 user | 79.2 | 0.89Γ | | Qwen3-30B-A3B Q4_K_M | Python TurboEngine | 1 user | 22.5 | 0.89Γ | +**Notes on these numbers.** Hardware is an AMD Threadripper 3970X (32 cores, 251 GB DDR4), 32 threads unless noted (`src/mojollama/README.md`). The 27.2 tok/s TinyLlama f16 row is the aggregate over 7 prompts of 10 generated tokens each. The Pure Mojo rows are recorded in `SUMMARY.md`, `src/mojollama/mojo_engine/TINYLLAMA_QUANT_BENCHMARK.md` and `src/mojollama/mojo_engine/REAL_BENCHMARK_RESULTS.md`. The TinyLlama Q4_0, GPT-OSS, Qwen3.6 TurboEngine and 47 aggregate concurrent figures match `src/mojollama/README.md`. No other file in the repository supports the Qwen3.6 C batch_forward row (17.0), the Qwen3-30B-A3B row (22.5, 0.89Γ), or the 10 Γ 1 and 8 Γ 4 concurrent rows; treat those as unverified. Later records disagree with some rows. `FINAL_STATUS.md` and `.hermes/debug-logs/2026-05-22_mojollama-opt-session.md` (22 May 2026) give GPT-OSS-20B Q4_K_M at 23.6 tok/s (0.86Γ llama.cpp) and Qwen3-30B Q4_K_M at 22.6 tok/s (0.81Γ), and list the C batch forward as not working (segfault on Qwen3.6, and a first-token mismatch with the Python path). Re-measure before relying on the TurboEngine and C batch_forward figures. + ### Pure Mojo Details | File | Model | Format | tok/s | Lines | |------|-------|--------|-------|-------| -| `tinyllama_benchmark.mojo` | TinyLlama-1.1B | f16 | 27.2 | 552 | +| `tinyllama_benchmark.mojo` | TinyLlama-1.1B | f16 | 27.2 | 488 | | `tinyllama_gen_q8.mojo` | TinyLlama-1.1B | Q8_0 B=4 | 88.1 | 552 | | `zaya_gen_q8.mojo` | ZAYA1-8B | Q8_0 | **28.6** | **~700** | @@ -76,9 +84,9 @@ All matmul kernels are pure Mojo (no C bridge). See `zaya_gen_q8.mojo` (~700 lin --- -## π§ C Engine (`kernels/cengine_batch_instr.c`) +## π§ C Engine (`src/mojollama/kernels/cengine_batch_instr.c`) -The C engine is the core performance layer: a single `.c` file (~1000 lines) with AVX2+OMP-accelerated operations: +The C engine is the core performance layer: a single `.c` file (~1,900 lines) with AVX2+OMP-accelerated operations: **Quantized Matmul Types:** | Type | Block Size | Operations | @@ -99,7 +107,7 @@ The C engine is the core performance layer: a single `.c` file (~1000 lines) wit --- -## π§ TurboEngine v7-MoE (`kernels/turbo_engine_v7_moe.py`) +## π§ TurboEngine v7-MoE (`src/mojollama/kernels/turbo_engine_v7_moe.py`) Zero-allocation Python engine with C-accelerated MoE support: @@ -116,7 +124,7 @@ Zero-allocation Python engine with C-accelerated MoE support: Two approaches for multi-user throughput: ### Multiprocessing spawn (best) -`server_concurrent_qwen36.py`: workers share weight pages via OS page cache: +`src/mojollama/benchmarks/server_concurrent_qwen36.py`: workers share weight pages via OS page cache: | Workers Γ OMP thr | Per-worker | Aggregate | CPU util | |-------------------|-----------|-----------|----------| @@ -128,20 +136,22 @@ Two approaches for multi-user throughput: ## πΊοΈ Future Plans +Status below is from the code in this repository. + ### Near-term -- **Batch inference** for ZAYA1-8B (B=4, currently B=1) -- **Q8_0 input quantization** in C engine -- **True continuous batching**: merge B users' MoE routing +- **Batch inference** for ZAYA1-8B (B=4, currently B=1): `zaya_gen_q8.mojo` still sets `comptime B = 1` +- **Q8_0 input quantization** in C engine: a Q8_0 by Q8_0 batch matmul exists in `src/mojollama/kernels/quant_kernels_omp.c`; its use across the C engine paths is not confirmed +- **True continuous batching**: merge B users' MoE routing (listed as planned) ### Medium-term -- **MAX GPU backend**: zero-copy tensor dispatch to NVIDIA GPUs -- **Speculative decoding**: TinyLlama draft + ZAYA target -- **Prefix caching**: LRU with SHA-256 hash +- **MAX GPU backend**: zero-copy tensor dispatch to NVIDIA GPUs. `MAXBackend` in `src/mojollama/backends.py` is documented there as a stub +- **Speculative decoding**: TinyLlama draft + ZAYA target. `src/mojollama/speculative.py` exists, with a TinyLlama draft and a Qwen3-30B-A3B target (not ZAYA) +- **Prefix caching**: LRU with SHA-256 hash. `src/mojollama/prefix_cache.py` implements an LRU cache keyed by a SHA-256 hash; its integration into the serving paths is not confirmed ### Long-term -- **Full GGUF quant pipeline**: imatrix β dynamic 2-bit quantization -- **MojoLlama Studio** as Unsloth+LLaMA-Factory replacement -- **Perplexity evaluation** +- **Full GGUF quant pipeline**: imatrix β dynamic 2-bit quantization. `imatrix` and `dynamic-quantize` commands exist in `studio.py`; the lowest type they select is Q2_K (2.5 bits per weight) +- **MojoLlama Studio** as Unsloth+LLaMA-Factory replacement: `studio.py` exists (27 commands, including `train`); it is not shown to replace either tool +- **Perplexity evaluation**: not built (listed in `ROADMAP.md`) --- @@ -157,11 +167,12 @@ src/mojollama/ β βββ extract_zaya_vocab.py 262K vocab extraction β βββ build_zaya.sh Build script βββ kernels/ # C + Python engines -β βββ cengine_batch_instr.c C engine (~1000 lines) +β βββ cengine_batch_instr.c C engine (~1,900 lines) β βββ turbo_engine_v7_moe.py Python MoE engine β βββ zaya_batch_forward.c ZAYA C reference -βββ server_moe.py HTTP inference server -βββ studio.py CLI Studio (25 commands) +βββ server.py API server +βββ benchmarks/server_moe.py MoE server used by the benchmarks +βββ studio.py CLI Studio (27 commands) βββ model/architectures.py 50+ arch detection ```