Ri4dH/ramgod
About
Run Mixture-of-Experts LLMs bigger than your RAM on a Mac, 3–5× faster than llama.cpp
AI & Machine LearningMeasured star growth
- in 24h
- 0
- in 7 days
- Collecting history
- in 15 days
- Collecting history
- in 1 month
- Collecting history
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README.md
Originalramgod
Run Mixture-of-Experts LLMs that are bigger than your RAM on a Mac, 3–5× faster than llama.cpp.
Real-time replay of measured per-token timings from one A/B run (gpt-oss-20b, 11.6 GB, on an 8 GB M1). Both produce the same text.
ramgod streams a MoE model's experts from the SSD as they are needed. On an 8 GB MacBook it runs gpt-oss-20b (11.6 GB) at 3.7 tokens/s (5.1× llama.cpp) and Qwen3-30B-A3B (18.6 GB) at 3.5 tokens/s (3.2×). Both models are larger than the machine's RAM, and with default settings llama.cpp can't run either one. The output is the same as llama.cpp's: llama.cpp still does all the math, ramgod only decides when and how the weights get from the SSD into RAM.
Is it for you? ramgod helps when a MoE model is bigger than your free RAM. If the model fits (say gpt-oss-20b on a 16 GB+ Mac), use plain llama.cpp: with all of the model on the GPU it's much faster than anything that has to read from the SSD. On a small Mac the usual alternative is a much smaller quantization (2–3 bit) that fits, which costs answer quality; ramgod runs the 4-bit model as published.
Results
| 8 GB M1, macOS 26, same prompt, greedy | gpt-oss-20b Q4_K_M (11.6 GB) | Qwen3-30B-A3B-Instruct-2507 Q4_K_M (18.6 GB) |
|---|---|---|
| llama.cpp, default settings | fails (out of GPU memory) | fails (out of GPU memory) |
| llama.cpp, best settings found¹ | 0.72 tok/s | 1.12 tok/s |
| ramgod | 3.66 tok/s (5.1×) | 3.54 tok/s (3.2×) |
¹ -ngl 99 --n-cpu-moe 99 --repack 0 -lm mmap (attention on the GPU, experts on the CPU from the memory-mapped file). llama.cpp's own --moe-cache-mib was slower and --lazy on made no difference. See the benchmark notes.
Each number is the median of 3 alternating back-to-back rounds (scripts/engine_ab.py), both models in the same session with only a terminal open (tables in docs/reports/, raw runs in docs/reports/ab-20261127-121259/ and ab-20261127-121930/). Absolute speed moves ±25% with free RAM, for llama.cpp as much as for ramgod, so compare the ratios: 2.8–5.1× on gpt-oss in every A/B session so far, rising as ramgod improved.
Why it's faster
A MoE model only uses a few experts per token (gpt-oss-20b: 4 of 32 per layer), so a model larger than RAM can run well, as long as the right experts are in RAM at the right time. llama.cpp leaves that to the operating system: the expert weights are memory-mapped, and every missing 16 KB page is a separate page fault. That reads the SSD at ~0.4 GB/s, while the same SSD does 4.3 GB/s with large parallel reads.
ramgod hooks llama.cpp's per-layer router output (the ffn_moe_topk tensor) and, before each layer's experts run:
- Reads the chosen experts in large parallel chunks. 8 I/O threads, 4 MB reads, zero-copy (
F_RDADVISE), skipping pages already in RAM (mincore). Because macOS has a unified buffer cache, these reads fill the very pages llama.cpp's mapping uses, so llama.cpp then runs with no I/O stalls. - Predicts the next layer's experts and prefetches them while the current layer computes ("pre-gating"). It applies layer L+1's router to layer L's hidden state, which finds 87–89% of the experts the next layer will pick. This is on by default for models with large experts (gpt-oss: +10–17%) and off for small ones, where it doesn't pay.
- Maps the freshly read pages before llama.cpp touches them. On Apple Silicon a page that just arrived from the SSD faults twice on first use (~1.7 µs, ~50k pages per token), and the math threads would take those faults in the middle of the expert matmuls. Two background threads touch every new page twice right after it's read, which takes compute from 6.4 to 4.4 ms per layer, close to the 4.0 ms it takes with everything already in RAM.
- Locks each layer's most-used experts in RAM and leaves the rest to the page cache. Left alone, macOS kept only ~1.8 GB of gpt-oss's 10 GB of experts and evicted single 16 KB pages scattered across them. ramgod
mlocks the experts each layer uses most (a quarter of RAM by default: 6 per layer on gpt-oss, 14 on Qwen3), and a new expert replaces a locked one only once it has been used more often, so the set doesn't churn. That reads 15–20% fewer bytes per token. Cached experts stay clean pages of the model file: an engine-owned copy got compressed and swapped by macOS (10 GB swapped out in one test). Locking ~2 GB does push a little of other apps' memory to swap (0–77 MB per gpt-oss run, system-wide);--no-pinturns it off.
The full engineering log, including everything that didn't work and why (LFU steering, mlock pinning, an anonymous expert cache, 2-layer lookahead, read sizes and thread counts, and why reads into RAM top out at ~3.1 GB/s on this machine), is in docs/HANDOFF.md.
Quick start
Requirements: an Apple Silicon Mac (tested on an 8 GB M1 with macOS 26), Homebrew, and a MoE model in GGUF format.
brew install llama.cpp
git clone https://github.com/Ri4dH/ramgod && cd ramgod
make # builds build/ramgod against Homebrew's llama.cpp
make test # optional: unit tests (no model needed)
# a model (11.6 GB); any GGUF MoE model works the same way
curl -L -o ~/models/gpt-oss-20b-Q4_K_M.gguf --create-dirs \
https://huggingface.co/unsloth/gpt-oss-20b-GGUF/resolve/main/gpt-oss-20b-Q4_K_M.gguf
./build/ramgod chat -m ~/models/gpt-oss-20b-Q4_K_M.gguf
Other commands:
./build/ramgod run -m MODEL -p "your prompt" -n 128 # one generation + detailed stats
./build/ramgod run -m MODEL --mode none # plain llama.cpp, same settings, for comparison
./build/ramgod bench-fetch -m MODEL # how fast your SSD reads whole experts
python3 scripts/engine_ab.py --model MODEL --rounds 3 # alternating A/B benchmark → docs/reports/
./build/ramgod --help
# optional, gpt-oss: train a small correction for the next-layer predictor (~45 min, 4.5 GB of temporary
# recordings; writes MODEL's .pregate file, which ramgod then loads automatically): +0-8% in our tests
python3 scripts/pregate_train.py record --model MODEL && python3 scripts/pregate_train.py record-chat --model MODEL
python3 scripts/pregate_train.py fit --model MODEL --write
For the best speed, close memory-hungry apps (browsers, Docker) and keep the Mac plugged in: Low Power Mode halves the speed. Every byte of free RAM becomes expert cache.
Supported models
ramgod works with any llama.cpp MoE architecture whose graph exposes ffn_inp-<layer> and ffn_moe_topk-<layer>, which covers most of them. It prints a warning if a model doesn't.
| Model | Status |
|---|---|
| gpt-oss-20b | tested, benchmarked |
| Qwen3-30B-A3B (2507 Instruct) | tested, benchmarked |
| other MoE GGUFs (Qwen3 MoE family, Mixtral, OLMoE, …) | should work, untested. Reports welcome |
Limitations
- macOS on Apple Silicon only for now. It relies on the unified buffer cache,
F_RDADVISEand Metal. - Speeds above are single-sequence decoding. Prompt processing is also faster than llama.cpp's (gpt-oss, 13-token prompt: 2.6 s vs 11 s), but long prompts haven't been optimized.
- It's built and tested against llama.cpp 1.0.0 (Homebrew build 12359). ramgod relies on llama.cpp's internal tensor names, so a future llama.cpp may break it; see Troubleshooting.
- Tested on one machine (an 8 GB M1). Numbers from other Macs are very welcome:
python3 scripts/engine_ab.py --model MODEL --rounds 3prints a table to paste into an issue. - Results depend heavily on free RAM. Benchmarks were run with a terminal plus the benchmark itself open.
Troubleshooting
make prints "note: building against llama.cpp build N, commit X; ramgod is tested with build 12359, commit b1b770d90". Other builds usually work. ramgod checks at run time whether it could hook the model.
ramgod run warns "ramgod couldn't hook this model". Either the model's architecture isn't covered (see Supported models), or your llama.cpp is newer and renamed the graph tensors ramgod hooks. To use the tested llama.cpp instead of Homebrew's, build it once (about a minute, libraries only) and point make at it:
brew install cmake
git clone --depth 1 --branch b12359 https://github.com/ggml-org/llama.cpp ~/llama.cpp-ramgod
cmake -S ~/llama.cpp-ramgod -B ~/llama.cpp-ramgod/build -DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=$HOME/llama.cpp-ramgod/install -DLLAMA_BUILD_TESTS=OFF -DLLAMA_BUILD_EXAMPLES=OFF \
-DLLAMA_BUILD_SERVER=OFF -DLLAMA_BUILD_TOOLS=OFF -DLLAMA_BUILD_APP=OFF
cmake --build ~/llama.cpp-ramgod/build -j && cmake --install ~/llama.cpp-ramgod/build
make clean && make LLAMA=$HOME/llama.cpp-ramgod/install GGML=$HOME/llama.cpp-ramgod/install
If Homebrew's llama.cpp works for you, brew pin llama.cpp keeps brew upgrade from replacing it.
Slow? Close memory-hungry apps, keep the Mac plugged in (Low Power Mode halves the speed), and check steady ... tok/s and wait split in ramgod run's stats.
Related work
- colibri also streams MoE experts from disk, with its own pure-C engine. It targets very large models (up to trillions of parameters) on Linux and Windows PCs with plenty of RAM and GPUs, and supports far more models and hardware than ramgod. On this 8 GB M1, with the same Qwen3-30B-A3B GGUF and prompt, alternating runs: colibri 1.7 tok/s at its best setting and 7.1–7.5 s for the prompt, ramgod 3.4–3.5 tok/s and 3.2 s. Its expert cache is the process's own memory, which macOS compresses and swaps on an 8 GB machine, so larger caches were slower there (0.4–0.9 tok/s). Details: HANDOFF.md.
- llama.cpp's own options for this case,
--n-cpu-moewith mmap (the baseline above),--moe-cache-miband--lazy, are compared in the benchmark notes.
Repository layout
| Path | What |
|---|---|
src/ |
the engine: ramgod.cpp (CLI: run, chat, bench-fetch), engine (llama.cpp setup), hook (router callback), fetcher (parallel reads, prefetch queue), pregate (next-layer predictor), expert_map (GGUF → expert byte ranges), expert_cache (experimental engine-owned cache) |
tests/ |
C++ tests against a synthetic MoE GGUF, Python tests for the simulators |
scripts/ |
benchmarks (engine_ab.py, offload_bench.py) and research tools (expert_trace.cpp, expert_analyze.py, expert_sim.py) |
data/ |
expert-routing traces of gpt-oss-20b (traces/); the sample texts they came from are recreated by scripts/fetch_sample_texts.sh |
docs/ |
HANDOFF.md (engineering log), reports/ (benchmark results), images/ (README figures, made by scripts/make_readme_assets.py from real runs) |
License
PolyForm Noncommercial 1.0.0: free for personal projects, hobby use, research, education and other noncommercial purposes. Commercial use (by companies or individuals, for profit) is not permitted under this license. Contact the author for a commercial license.
ramgod links against llama.cpp (MIT), which is installed separately.
Acknowledgements
Built on llama.cpp / ggml. Ideas from Apple's LLM in a Flash, Pre-gated MoE, PowerInfer, MoE-Infinity and AdapMoE.
Explore related projects
Loading…