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

196c3b7 sanitize: alpha-testing hygiene (paths, hosts, emails, licence, labels) Drake Stapleton committed 2026-10-26
..
.github/
benchmarks/
engine/
eval/
gguf/
graph/
inference/
kernels/
model/
mojo_engine/
tests/
.coding-memory1253
.dockerignore326
.gitignore28
.workspace-state.md1927
Dockerfile5418
README.md7231
__init__.mojo32
__init__.py189
__main__.py30992
autotune.py30754
backends.py43874
bench_gpt.py945
bridge.py12957
convert_lance.py10348
convert_zaya_mxfp4.py7223
dataset.py42988
detect.py18661
docker-compose.yml5458
docker-entrypoint.sh3437
experiment.py39208
exporter.py47050
extract_lance_text.py3195
hybrid.py16110
hybrid_forward.py8732
llama_backend.py4190
moe-plan.md3178
mojollama_infer.mojo7401
numpy_reference.py7031
prefix_cache.py8464
q4k_compare.py9013
quantizer.py52626
scheduler.py11453
server.mojo11574
server.py39488
speculative.py9346
studio.py86877
trainer.py58071

README.md

MojoLlama

High-throughput CPU LLM inference engine — GGUF native, MoE-optimized, multi-architecture.

mojollama chat -m model.gguf        # Interactive chat
mojollama serve -m model.gguf -p 8080  # API server
mojollama bench -m model.gguf -n 128   # Benchmark

Features

  • Multi-architecture — Llama, Mistral, Qwen2/3, GPT-OSS, Gemma 4, ZAYA1, DeepSeek V2/V3, DBRX, Mixtral, Falcon, Phi, and 50+ more
  • GGUF native — all quant types: Q4_0, Q4_K, Q5_K, Q6_K, Q8_0, MXFP4
  • MoE optimized — C-accelerated fused MoE kernel
  • Hybrid SSM+Attention — Qwen3.6-35B (Mamba-2 + Attention hybrid)
  • Per-layer projections — Gemma 4 support with GeGLU, sliding window, logit softcapping
  • Mixture of Depth — ZAYA1-8B with interleaved ATTN/MoE+MoD, CCA, SSM conv1d
  • C engine — batched inference, PagedAttention, concurrent serving
  • Auto-tune — hardware-specific optimization for OMP threads, batch sizes, concurrency
  • Studio — web UI for chat, training, export, dataset management
  • llama.cpp fallback — built-in llama-server for compatibility
  • MAX backend — GPU acceleration via Modular MAX engine
  • Docker — ready-to-use image on GHCR

Quick Start

Install

# From pip (coming soon)
pip install mojollama

# Or from source
git clone https://github.com/a730/MojoLlama.git
cd MojoLlama/src
pip install -e .

CLI Usage

# Chat with a model
mojollama chat -m /path/to/model.gguf

# Start API server
mojollama serve -m /path/to/model.gguf -p 8080

# Benchmark
mojollama bench -m /path/to/model.gguf -n 256 -t 32

# Quantize a model
mojollama quantize model.gguf -t Q4_K_M

# Auto-tune for your hardware
mojollama autotune -m /path/to/model.gguf --mojollama

# Import from HuggingFace
mojollama convert --hf meta-llama/Llama-3.2-1B --outtype q4_0

# System info
mojollama info

Docker

# Pull pre-built image
docker pull ghcr.io/a730/mojollama:latest

# Run API server
docker run --rm -v /models:/models:ro ghcr.io/a730/mojollama:latest \
    serve -m /models/model.gguf -p 8080

# Or use docker-compose
docker compose up -d

Auto-download models from HuggingFace:

docker run --rm -e HF_REPO=bartowski/TinyLlama-1.1B-GGUF \
    -e HF_FILE=tinyllama-1.1b.Q4_K_M.gguf \
    -e MODEL_PATH=/models/tinyllama-1.1b.Q4_K_M.gguf \
    ghcr.io/a730/mojollama:latest serve

Architecture Support

ArchitectureStatusFeatures
Llama 2/3/4, Mistral✅ FullStandard dense, GQA, SwiGLU, RoPE
Qwen2, Qwen3✅ FullTNT embedding, partial rotary
GPT-OSS✅ FullMoE, packed experts, bias attn
Qwen3 MoE✅ Full128 experts top-8, shared expert
Qwen3.6-35B✅ FullHybrid SSM+Attention, MXFP4, MoE
Gemma 4✅ FullPer-layer proj, GeGLU, SWA, softcap
ZAYA1-8B✅ FullATTN/CCA/SSM + MoE+MoD interleaved
DeepSeek V2/V3✅MLA, DeepSeekMoE
Mixtral✅Standard MoE
DBRX✅MoE with gated FFN
Falcon🔧 PlannedParallel attention+FFN
Phi-3/4🔧 PlannedFused QKV, long RoPE
Command-R✅Tanh-gated RoPE

Performance

ModelEnginetok/svs llama.cpp
TinyLlama 1.1B Q4_0Dense79.20.89x
GPT-OSS-20B Q4_K_MMoE (Python)25.50.94x
Qwen3.6-35B MXFP4MoE (Python)25.51.57x 🏆
Qwen3.6-35B MXFP4Concurrent (10×3)47 agg—
Gemma 4 4.6B Q4_K_MGemma418.8—
ZAYA1-8B Q4_K_MZAYA (1 thread)0.9—

Benchmarked on AMD Threadripper 3970X (32C/64T, 251GB DDR4), 32 threads unless noted.

Project Structure

src/mojollama/
├── __main__.py            # Unified CLI entry point
├── studio.py              # Web UI and training commands
├── server.py              # API server
├── backends.py            # Backend selector (llama.cpp, MAX, numpy)
├── autotune.py            # Hardware auto-tuner
├── quantizer.py           # GGUF quantization pipeline
├── kernels/               # Inference engine
│   ├── turbo_engine_v7_moe.py  # Main MoE engine
│   ├── turbo_engine_v77.py     # Dense fallback engine
│   ├── cengine_batch_instr.c/.so  # C batched engine
│   ├── quant_kernels_omp.c/.so    # Quantized matmul kernels
│   ├── simd_ops.c/.so             # SIMD ops (RMS norm, SiLU, RoPE)
│   ├── gqa_attention.c/.so        # GQA attention kernel
│   ├── forward/                   # Architecture forward passes
│   │   ├── base.py                # ArchitectureForwardPass ABC
│   │   ├── gemma4.py              # Gemma 4 forward pass
│   │   └── zaya.py                # ZAYA1 forward pass
│   ├── dsv4/                      # DeepSeek V4 engine
│   ├── legacy-engine/             # Archived Python engines
│   └── legacy-c/                  # Archived C kernels
├── benchmarks/            # Benchmark and profiling scripts
├── tests/                 # Test scripts
├── Dockerfile             # Docker image build
├── docker-compose.yml     # Docker compose (all services)
├── docker-entrypoint.sh   # Docker entrypoint with auto-tune
└── .github/workflows/     # GitHub Actions CI/CD

Environment Variables

VariableDefaultDescription
OMP_NUM_THREADSautoOpenMP threads for CPU parallelism
MODEL_PATH—GGUF model path
PORT8080API server port
MOJOLLAMA_DEVICEautoDevice override (cpu/cuda)
HF_TOKEN—HuggingFace token for gated models
HF_REPO—HuggingFace repo for auto-download
HF_FILE—HuggingFace filename for auto-download
AUTO_TUNEtrueRun auto-tune on first model load

Docker Services

ServiceProfileDescription
mojollamadefaultAPI server
chatchatWeb UI chat
benchbenchOne-shot benchmark
quantizequantizeModel quantization
autotuneautotuneHardware tuning
llama-cppfallbackllama.cpp fallback server
maxmaxMAX GPU engine

Development

Build C Kernels

cd src/mojollama/kernels

# Quant matmul OMP kernel
gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \
    -o quant_kernels_omp.so quant_kernels_omp.c -lm

# C batched engine
gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \
    -o cengine_batch_instr.so cengine_batch_instr.c -lm

# SIMD ops
gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \
    -o simd_ops.so simd_ops.c -lm

# GQA attention
gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \
    -o gqa_attention.so gqa_attention.c -lm

Architecture Forward Passes

To add a new architecture:

  1. Create kernels/forward/<arch>.py with a class extending ArchitectureForwardPass
  2. Implement forward(), init_weights(), init_pointers()
  3. Add dispatch in turbo_engine_v7_moe.py __init__
  4. Add needed slots to LayerWeights.__slots__

License

Apache-2.0 (see the repository LICENSE)