|
|
||
|---|---|---|
| docker | ||
| docs | ||
| patches | ||
| scripts | ||
| systemd | ||
| tools | ||
| ui | ||
| .gitignore | ||
| LLAMA_CPP_COMMIT | ||
| README.md | ||
S80-Optimization
Attempts to optimize llama.cpp for the MTT S80.
This repository makes llama.cpp run fast on the Moore Threads MTT S80 (16 GB, MUSA SDK 4.2, GPU arch mp_21).
It does not contain llama.cpp: the build downloads llama.cpp at a pinned commit, applies one patch and builds it
inside Moore Threads' MUSA 4.2 container.
Results
Decode speed in tokens per second, llama-server, 3 prompts x 256 generated tokens unless noted (status 2026-11-20; methodology and history in docs/RESULTS.md).
Qwen3.8-27B GSQ-RCO IQ3_S (11.3 GB) with MTP speculative decoding (3 draft tokens):
| stock llama.cpp | this repo | |
|---|---|---|
| decode, short context | 3.23 (no MTP) | 10.0-10.2 |
| decode at 11.4k / 44k tokens of context | ~6.3 / - | 10.9 / 10.6 |
| decode at 94.5k tokens of context (128k window) | - | 9.6 |
| prompt processing, 512-token batches | 40 | 102 |
| reading an 11.4k / 44k / 94.5k-token prompt | ~23-30 / - / - | 88 / 66 / 48 (94.5k: 33 min) |
Qwen3.5-9B (ships without an MTP head; tools/graft_mtp.py adds one, see below):
| stock llama.cpp | this repo | + grafted MTP head | |
|---|---|---|---|
| Q8_0 | 10.8 | 17.0 | 25-26.7 |
| Q4_0 | 12.1 | 17.0 | 27.6-29.2 |
| prompt processing, 512-token batches (Q8_0 / Q4_0) | 188 / 207 | 317 / 358 |
Speed with MTP varies by a few percent between runs and more between prompts (23.8-30.5 t/s per prompt for the 9B Q8_0). Q8_0 + MTP is the recommended 9B: about as fast as Q4_0 + MTP and closer to the original model.
Inkling 2 Mini AD IQ3_S (12.4 GB; MoE with 26B total / ~3B active parameters, its middle layers run 3 times, so each token makes 48 layer passes):
| stock kernels (this patch, before the Inkling work) | this repo | |
|---|---|---|
| decode | 3.4 | 14.6 |
| prompt processing (512 tokens, 256-token batches) | ~29 | 130 |
Inkling spends its time on ~150 tiny operations per layer pass (depth mixing, loop controllers, short convolutions,
relative-position bias, clips) rather than on large matmuls; the patch fuses them into ~45 kernels per pass (see
docs/DESIGN.md). KL divergence against the unfused computation: 0.002 (98.4% same top
token), the same as merely reordering additions in this model. Preset inkling (f16 KV cache, 32k context).
Accuracy: the patched kernels change results slightly (int8 tensor cores, quantized activations). KL divergence of
the output distribution against stock llama.cpp: 27B 0.0007-0.0008, 9B Q8_0 0.0005, 9B Q4_0 0.002. At long context,
the attention used for MTP verification adds about as much error as the q4_0 KV cache itself does (KLD 0.0035 vs
0.0041, 98.4% same top token); GGML_MUSA_FA_PVMMA=0 removes that part at the cost of slower long-context
attention (+23% for the verify pass, +80% for the drafts).
What was changed and why: docs/DESIGN.md. Hardware facts and dead ends: docs/FINDINGS.md.
Requirements
- MTT S80 with the Moore Threads kernel driver loaded (
/dev/mtgpu.0,/dev/dri). The 128k context of the 27B was measured with the driver'scompute_only=1option (frees 512 MiB of VRAM); without it, lowerCTXif a 128k start runs out of memory. - Docker, and the S80 MUSA image
registry.mthreads.com/mcconline/musa-pytorch-release-public:rc4.2.0-v2.1.0-S80-py310(the image that ships the complete MUSA 4.2 toolchain for the S80;docker pullit once) - git, network access for the first build (GitHub for llama.cpp, PyPI/apt inside the image build)
Build
scripts/build.sh
This runs scripts/fetch-llama.sh (shallow-fetches llama.cpp 29b52d3 = release b12600, see LLAMA_CPP_COMMIT,
applies patches/llama.cpp-mtt-s80.patch, installs the bundled web-UI assets),
builds the s80-llama:4.2 image from docker/Dockerfile if needed, and compiles llama.cpp into
llama.cpp/build/bin. The first build takes 15-40 minutes depending on the CPU; ccache (.ccache/) makes later
rebuilds fast. After pulling a newer patch: scripts/fetch-llama.sh --reset && scripts/build.sh; when the pinned commit
changes, scripts/build.sh switches llama.cpp/ to it and starts a clean build by itself.
Building on another machine (any x86-64 box with Docker; it needs no GPU) keeps the load off the S80 host:
copy the image there once (docker save s80-llama:4.2 | zstd | ssh user@host 'zstd -d | docker load'), then
S80_BUILD_HOST=user@host scripts/build.sh. The repository is copied to ~/s80-build/ on that machine, built with
all its cores (a full build took ~4 min on a 20-thread i7-12700K) and the binaries are copied back. S80_GRAPHS=ON
compiles MUSA graph capture in (off by default: it does not help the Qwen models).
Optional: scripts/test.sh runs the ggml op tests that cover the patched kernels on the GPU.
Run
scripts/run-server.sh # Qwen3.8-27B GSQ-RCO + MTP, 128k context, port 8080
scripts/run-server.sh qwen9b-mtp # Qwen3.5-9B Q8_0 + grafted MTP head, 262k context, thinking off, port 8111
scripts/run-server.sh qwen9b # same without MTP
scripts/run-server.sh inkling # Inkling 2 Mini IQ3_S, 32k context, port 8080 (reasons before answering)
MODEL=other.gguf scripts/run-server.sh custom
scripts/run-server.sh --stop
Then open http://localhost:<port> (web UI) or use the OpenAI-compatible API at http://localhost:<port>/v1.
Models are read from MODELS_DIR (default /mnt/models/models). Overrides: MODEL (another file for a preset),
CTX, PORT, DRAFTS, EXTRA_ARGS. Only one model fits in the 16 GB at a time.
VRAM in use right after start, with the full context allocated (q4_0 KV cache):
| preset / model | context | used of 16 GB |
|---|---|---|
qwen27-mtp (27B) |
128k | 14.8 GB (15.6 GB peak while reading a 94.5k-token prompt) |
qwen9b-mtp (9B Q8_0 + MTP) |
262k | 11.8 GB |
qwen9b (9B Q8_0) |
262k | 10.8 GB |
qwen9b-mtp with MODEL=Qwen3.5-9B-Q4_0-mtp.gguf |
262k | 8.4 GB |
Reading a long prompt is the slow part (attention cost grows with depth: a 94.5k-token prompt takes 33 min); in chat use the server's prompt cache only processes the new tokens of each turn.
As a service, switching models with one file
systemd/install-user-services.sh
systemctl --user start llama-s80
This installs three user services (no root needed) and a model selector, ~/.config/llama-s80/server.conf:
| service | runs |
|---|---|
llama-s80 |
the preset named by PRESET= in server.conf (qwen27-mtp, qwen9b-mtp, qwen9b or inkling) |
llama-qwen27b |
the 27B + MTP |
llama-qwen9b |
the 9B Q8_0 + MTP |
To switch models, change PRESET and run systemctl --user restart llama-s80, or start one of the model services
directly. The three conflict with each other (starting one stops the others first) and all serve on the same port
(PORT=, default 8080), so clients keep working across a switch. server.conf also takes CTX, DRAFTS, MODEL,
EXTRA_ARGS and the kernel switches below. systemctl --user enable llama-s80 starts it at login;
loginctl enable-linger $USER lets user services run without a login session. A system-wide template is in
systemd/s80-llama@.service.
The flags that matter on the S80
All presets use them:
| flag | why |
|---|---|
--flash-attn on |
uses the S80 attention kernels from this patch (~10x ggml's FlashAttention on this GPU) |
-ctk q4_0 -ctv q4_0 |
4.5-bit KV cache: 3.5x more context. Do not use q8_0 (FlashAttention gives NaN with it on mp_21; refused) |
-bs |
sample on the GPU. CPU sampling over Qwen's 248k vocabulary costs ~10 ms per token on a slow host CPU |
--spec-type draft-mtp --spec-draft-n-max 3 |
MTP speculative decoding (models with an MTP head) |
env LLAMA_MTP_DRAFT_VOCAB=65536 |
the MTP draft head only scores the first 64k token ids (+2%; verification still uses the full vocabulary) |
Every patched kernel has an environment switch to turn it off, for comparison or if something looks wrong, e.g.
GGML_MUSA_Q4_MMA=0 (tensor-core matmuls), GGML_MUSA_FA_MMA=0 / GGML_MUSA_FA_PVMMA=0 (tensor-core attention /
its int8 P*V part), GGML_MUSA_FA_TILED=0 (tiled prompt attention). scripts/run-server.sh passes any
GGML_MUSA_* variable into the container: GGML_MUSA_FA_PVMMA=0 scripts/run-server.sh. Full list:
docs/DESIGN.md.
Adding MTP to a model that was converted without it
Most Qwen3.5-9B GGUFs dropped the model's MTP (multi-token prediction) block. tools/graft_mtp.py copies an
MTP-only block (e.g. a4lg/Qwen3.5-9B-MTP-ONLY-GGUF, Q8_0)
into an existing GGUF of the same base model, byte for byte, and sets block_count/nextn_predict_layers:
docker run --rm -v "$PWD:/work:ro" -v /mnt/models:/mnt/models --user "$(id -u):$(id -g)" s80-llama:4.2 \
python3 -I /work/tools/graft_mtp.py /mnt/models/models/Qwen3.5-9B-Q8_0.gguf \
/mnt/models/downloads/Qwen3.5-9B-MTP-ONLY-Q8_0.gguf /mnt/models/models/Qwen3.5-9B-Q8_0-mtp.gguf
The original file is not modified; the result is +274 MB. Requires scripts/fetch-llama.sh to have run, for gguf-py.
The same head works on the Q4_0 GGUF (Qwen3.5-9B-Q4_0-mtp.gguf in the tables above).
Layout
| path | content |
|---|---|
patches/ |
the llama.cpp patch (one file, applies to the pinned commit) |
scripts/ |
fetch, build, test, run, benchmark wrappers |
systemd/ |
user services with a model selector (install-user-services.sh), system-wide template |
docker/ |
build/runtime image |
ui/ |
web-UI assets for the pinned llama.cpp (so the build needs no Hugging Face download) |
tools/graft_mtp.py |
adds an MTP head to a GGUF |
tools/kbench/ |
benchmarks: per-tensor matmul timing, 3-prompt MTP benchmark, long-context, KLD validation |
tools/shim/ |
LD_PRELOAD profilers: per-kernel GPU time, host launch/sync time, per-llama_decode timeline + CPU sampler |
tools/mma/ |
standalone tensor-core experiments (MMA layout probes, per-quant-type prototypes, MMA/load microbenchmarks) |
tools/isa/ |
working below the SDK: machine-code dumps, hand-edited MIR pipeline, microbenchmarks |
docs/ |
results, design, findings, ISA notes |