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 / src / mojollama / README.md

209 lines · 7231 bytes Raw Blame History Rendered
1# MojoLlama
2
3**High-throughput CPU LLM inference engine** — GGUF native, MoE-optimized, multi-architecture.
4
5```bash
6mojollama chat -m model.gguf # Interactive chat
7mojollama serve -m model.gguf -p 8080 # API server
8mojollama 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)
32pip install mojollama
33
34# Or from source
35git clone https://github.com/a730/MojoLlama.git
36cd MojoLlama/src
37pip install -e .
38```
39
40### CLI Usage
41
42```bash
43# Chat with a model
44mojollama chat -m /path/to/model.gguf
45
46# Start API server
47mojollama serve -m /path/to/model.gguf -p 8080
48
49# Benchmark
50mojollama bench -m /path/to/model.gguf -n 256 -t 32
51
52# Quantize a model
53mojollama quantize model.gguf -t Q4_K_M
54
55# Auto-tune for your hardware
56mojollama autotune -m /path/to/model.gguf --mojollama
57
58# Import from HuggingFace
59mojollama convert --hf meta-llama/Llama-3.2-1B --outtype q4_0
60
61# System info
62mojollama info
63```
64
65### Docker
66
67```bash
68# Pull pre-built image
69docker pull ghcr.io/a730/mojollama:latest
70
71# Run API server
72docker 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
76docker compose up -d
77```
78
79Auto-download models from HuggingFace:
80```bash
81docker 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
116Benchmarked on AMD Threadripper 3970X (32C/64T, 251GB DDR4), 32 threads unless noted.
117
118## Project Structure
119
120```
121src/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
180cd src/mojollama/kernels
181
182# Quant matmul OMP kernel
183gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \
184 -o quant_kernels_omp.so quant_kernels_omp.c -lm
185
186# C batched engine
187gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \
188 -o cengine_batch_instr.so cengine_batch_instr.c -lm
189
190# SIMD ops
191gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \
192 -o simd_ops.so simd_ops.c -lm
193
194# GQA attention
195gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \
196 -o gqa_attention.so gqa_attention.c -lm
197```
198
199### Architecture Forward Passes
200
201To add a new architecture:
2021. Create `kernels/forward/<arch>.py` with a class extending `ArchitectureForwardPass`
2032. Implement `forward()`, `init_weights()`, `init_pointers()`
2043. Add dispatch in `turbo_engine_v7_moe.py` `__init__`
2054. Add needed slots to `LayerWeights.__slots__`
206
207## License
208
209Apache-2.0 (see the repository LICENSE)