MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / README.md
| 1 | # MojoLlama |
| 2 | |
| 3 | **High-throughput CPU LLM inference engine** — GGUF native, MoE-optimized, multi-architecture. |
| 4 | |
| 5 | ```bash |
| 6 | mojollama chat -m model.gguf # Interactive chat |
| 7 | mojollama serve -m model.gguf -p 8080 # API server |
| 8 | mojollama bench -m model.gguf -n 128 # Benchmark |
| 9 | ``` |
| 10 | |
| 11 | ## Features |
| 12 | |
| 13 | - **Multi-architecture** — Llama, Mistral, Qwen2/3, GPT-OSS, Gemma 4, ZAYA1, DeepSeek V2/V3, DBRX, Mixtral, Falcon, Phi, and 50+ more |
| 14 | - **GGUF native** — all quant types: Q4_0, Q4_K, Q5_K, Q6_K, Q8_0, MXFP4 |
| 15 | - **MoE optimized** — C-accelerated fused MoE kernel |
| 16 | - **Hybrid SSM+Attention** — Qwen3.6-35B (Mamba-2 + Attention hybrid) |
| 17 | - **Per-layer projections** — Gemma 4 support with GeGLU, sliding window, logit softcapping |
| 18 | - **Mixture of Depth** — ZAYA1-8B with interleaved ATTN/MoE+MoD, CCA, SSM conv1d |
| 19 | - **C engine** — batched inference, PagedAttention, concurrent serving |
| 20 | - **Auto-tune** — hardware-specific optimization for OMP threads, batch sizes, concurrency |
| 21 | - **Studio** — web UI for chat, training, export, dataset management |
| 22 | - **llama.cpp fallback** — built-in llama-server for compatibility |
| 23 | - **MAX backend** — GPU acceleration via Modular MAX engine |
| 24 | - **Docker** — ready-to-use image on GHCR |
| 25 | |
| 26 | ## Quick Start |
| 27 | |
| 28 | ### Install |
| 29 | |
| 30 | ```bash |
| 31 | # From pip (coming soon) |
| 32 | pip install mojollama |
| 33 | |
| 34 | # Or from source |
| 35 | git clone https://github.com/a730/MojoLlama.git |
| 36 | cd MojoLlama/src |
| 37 | pip install -e . |
| 38 | ``` |
| 39 | |
| 40 | ### CLI Usage |
| 41 | |
| 42 | ```bash |
| 43 | # Chat with a model |
| 44 | mojollama chat -m /path/to/model.gguf |
| 45 | |
| 46 | # Start API server |
| 47 | mojollama serve -m /path/to/model.gguf -p 8080 |
| 48 | |
| 49 | # Benchmark |
| 50 | mojollama bench -m /path/to/model.gguf -n 256 -t 32 |
| 51 | |
| 52 | # Quantize a model |
| 53 | mojollama quantize model.gguf -t Q4_K_M |
| 54 | |
| 55 | # Auto-tune for your hardware |
| 56 | mojollama autotune -m /path/to/model.gguf --mojollama |
| 57 | |
| 58 | # Import from HuggingFace |
| 59 | mojollama convert --hf meta-llama/Llama-3.2-1B --outtype q4_0 |
| 60 | |
| 61 | # System info |
| 62 | mojollama info |
| 63 | ``` |
| 64 | |
| 65 | ### Docker |
| 66 | |
| 67 | ```bash |
| 68 | # Pull pre-built image |
| 69 | docker pull ghcr.io/a730/mojollama:latest |
| 70 | |
| 71 | # Run API server |
| 72 | docker run --rm -v /models:/models:ro ghcr.io/a730/mojollama:latest \ |
| 73 | serve -m /models/model.gguf -p 8080 |
| 74 | |
| 75 | # Or use docker-compose |
| 76 | docker compose up -d |
| 77 | ``` |
| 78 | |
| 79 | Auto-download models from HuggingFace: |
| 80 | ```bash |
| 81 | docker run --rm -e HF_REPO=bartowski/TinyLlama-1.1B-GGUF \ |
| 82 | -e HF_FILE=tinyllama-1.1b.Q4_K_M.gguf \ |
| 83 | -e MODEL_PATH=/models/tinyllama-1.1b.Q4_K_M.gguf \ |
| 84 | ghcr.io/a730/mojollama:latest serve |
| 85 | ``` |
| 86 | |
| 87 | ## Architecture Support |
| 88 | |
| 89 | | Architecture | Status | Features | |
| 90 | |-------------|--------|----------| |
| 91 | | Llama 2/3/4, Mistral | ✅ Full | Standard dense, GQA, SwiGLU, RoPE | |
| 92 | | Qwen2, Qwen3 | ✅ Full | TNT embedding, partial rotary | |
| 93 | | GPT-OSS | ✅ Full | MoE, packed experts, bias attn | |
| 94 | | Qwen3 MoE | ✅ Full | 128 experts top-8, shared expert | |
| 95 | | Qwen3.6-35B | ✅ Full | Hybrid SSM+Attention, MXFP4, MoE | |
| 96 | | Gemma 4 | ✅ Full | Per-layer proj, GeGLU, SWA, softcap | |
| 97 | | ZAYA1-8B | ✅ Full | ATTN/CCA/SSM + MoE+MoD interleaved | |
| 98 | | DeepSeek V2/V3 | ✅ | MLA, DeepSeekMoE | |
| 99 | | Mixtral | ✅ | Standard MoE | |
| 100 | | DBRX | ✅ | MoE with gated FFN | |
| 101 | | Falcon | 🔧 Planned | Parallel attention+FFN | |
| 102 | | Phi-3/4 | 🔧 Planned | Fused QKV, long RoPE | |
| 103 | | Command-R | ✅ | Tanh-gated RoPE | |
| 104 | |
| 105 | ## Performance |
| 106 | |
| 107 | | Model | Engine | tok/s | vs llama.cpp | |
| 108 | |-------|--------|-------|-------------| |
| 109 | | TinyLlama 1.1B Q4_0 | Dense | 79.2 | 0.89x | |
| 110 | | GPT-OSS-20B Q4_K_M | MoE (Python) | **25.5** | **0.94x** | |
| 111 | | Qwen3.6-35B MXFP4 | MoE (Python) | 25.5 | 1.57x 🏆 | |
| 112 | | Qwen3.6-35B MXFP4 | Concurrent (10×3) | **47 agg** | — | |
| 113 | | Gemma 4 4.6B Q4_K_M | Gemma4 | 18.8 | — | |
| 114 | | ZAYA1-8B Q4_K_M | ZAYA (1 thread) | 0.9 | — | |
| 115 | |
| 116 | Benchmarked on AMD Threadripper 3970X (32C/64T, 251GB DDR4), 32 threads unless noted. |
| 117 | |
| 118 | ## Project Structure |
| 119 | |
| 120 | ``` |
| 121 | src/mojollama/ |
| 122 | ├── __main__.py # Unified CLI entry point |
| 123 | ├── studio.py # Web UI and training commands |
| 124 | ├── server.py # API server |
| 125 | ├── backends.py # Backend selector (llama.cpp, MAX, numpy) |
| 126 | ├── autotune.py # Hardware auto-tuner |
| 127 | ├── quantizer.py # GGUF quantization pipeline |
| 128 | ├── kernels/ # Inference engine |
| 129 | │ ├── turbo_engine_v7_moe.py # Main MoE engine |
| 130 | │ ├── turbo_engine_v77.py # Dense fallback engine |
| 131 | │ ├── cengine_batch_instr.c/.so # C batched engine |
| 132 | │ ├── quant_kernels_omp.c/.so # Quantized matmul kernels |
| 133 | │ ├── simd_ops.c/.so # SIMD ops (RMS norm, SiLU, RoPE) |
| 134 | │ ├── gqa_attention.c/.so # GQA attention kernel |
| 135 | │ ├── forward/ # Architecture forward passes |
| 136 | │ │ ├── base.py # ArchitectureForwardPass ABC |
| 137 | │ │ ├── gemma4.py # Gemma 4 forward pass |
| 138 | │ │ └── zaya.py # ZAYA1 forward pass |
| 139 | │ ├── dsv4/ # DeepSeek V4 engine |
| 140 | │ ├── legacy-engine/ # Archived Python engines |
| 141 | │ └── legacy-c/ # Archived C kernels |
| 142 | ├── benchmarks/ # Benchmark and profiling scripts |
| 143 | ├── tests/ # Test scripts |
| 144 | ├── Dockerfile # Docker image build |
| 145 | ├── docker-compose.yml # Docker compose (all services) |
| 146 | ├── docker-entrypoint.sh # Docker entrypoint with auto-tune |
| 147 | └── .github/workflows/ # GitHub Actions CI/CD |
| 148 | ``` |
| 149 | |
| 150 | ## Environment Variables |
| 151 | |
| 152 | | Variable | Default | Description | |
| 153 | |----------|---------|-------------| |
| 154 | | `OMP_NUM_THREADS` | auto | OpenMP threads for CPU parallelism | |
| 155 | | `MODEL_PATH` | — | GGUF model path | |
| 156 | | `PORT` | 8080 | API server port | |
| 157 | | `MOJOLLAMA_DEVICE` | auto | Device override (cpu/cuda) | |
| 158 | | `HF_TOKEN` | — | HuggingFace token for gated models | |
| 159 | | `HF_REPO` | — | HuggingFace repo for auto-download | |
| 160 | | `HF_FILE` | — | HuggingFace filename for auto-download | |
| 161 | | `AUTO_TUNE` | true | Run auto-tune on first model load | |
| 162 | |
| 163 | ## Docker Services |
| 164 | |
| 165 | | Service | Profile | Description | |
| 166 | |---------|---------|-------------| |
| 167 | | `mojollama` | default | API server | |
| 168 | | `chat` | chat | Web UI chat | |
| 169 | | `bench` | bench | One-shot benchmark | |
| 170 | | `quantize` | quantize | Model quantization | |
| 171 | | `autotune` | autotune | Hardware tuning | |
| 172 | | `llama-cpp` | fallback | llama.cpp fallback server | |
| 173 | | `max` | max | MAX GPU engine | |
| 174 | |
| 175 | ## Development |
| 176 | |
| 177 | ### Build C Kernels |
| 178 | |
| 179 | ```bash |
| 180 | cd src/mojollama/kernels |
| 181 | |
| 182 | # Quant matmul OMP kernel |
| 183 | gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \ |
| 184 | -o quant_kernels_omp.so quant_kernels_omp.c -lm |
| 185 | |
| 186 | # C batched engine |
| 187 | gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \ |
| 188 | -o cengine_batch_instr.so cengine_batch_instr.c -lm |
| 189 | |
| 190 | # SIMD ops |
| 191 | gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \ |
| 192 | -o simd_ops.so simd_ops.c -lm |
| 193 | |
| 194 | # GQA attention |
| 195 | gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \ |
| 196 | -o gqa_attention.so gqa_attention.c -lm |
| 197 | ``` |
| 198 | |
| 199 | ### Architecture Forward Passes |
| 200 | |
| 201 | To add a new architecture: |
| 202 | 1. Create `kernels/forward/<arch>.py` with a class extending `ArchitectureForwardPass` |
| 203 | 2. Implement `forward()`, `init_weights()`, `init_pointers()` |
| 204 | 3. Add dispatch in `turbo_engine_v7_moe.py` `__init__` |
| 205 | 4. Add needed slots to `LayerWeights.__slots__` |
| 206 | |
| 207 | ## License |
| 208 | |
| 209 | Apache-2.0 (see the repository LICENSE) |