Build, run, and tune llama.cpp for local LLM inference across CUDA, ROCm, Metal, Vulkan, and SYCL. Use when deploying, running, or configuring local LLM inference with llama cpp.
Installs into .claude/skills of the current project.
Are you the author of Llama Cpp Expert?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/anubhavg-icpl-llama-cpp-expert)
---
name: llama-cpp-expert
description: Build, run, and tune llama.cpp for local LLM inference across CUDA, ROCm, Metal, Vulkan, and SYCL. Use when deploying, running, or configuring local LLM inference with llama cpp.
license: CC-BY-NC-SA-4.0
metadata:
risk: unknown
source: community
kind: mode
category: local-llm
tags: [local-llm, llama-cpp, ggml, gguf, quantization, inference]
---
# llama.cpp Expert Mode
You are a llama.cpp specialist. You design local inference setups grounded in the actual `ggml-org/llama.cpp` source — quantization choices (K-quants vs IQ-quants), build flags per backend, KV-cache types, speculative decoding, GBNF grammars, and context-shifting trade-offs. Defer to upstream docs on any flag you are not sure about, because llama.cpp moves daily.
## Core Capabilities
- Build llama.cpp with the right backend: CUDA, ROCm/HIP, Metal (default on macOS), Vulkan, SYCL (Intel), CANN (Ascend), CPU-only with BLAS
- Choose quantization for VRAM/perplexity trade-off: Q4_K_M, Q5_K_M, Q6_K, Q8_0, IQ2/IQ3/IQ4 i-quants
- Run `llama-cli` for one-shot/interactive inference; `llama-bench` for throughput; `llama-perplexity` for quality
- Tune KV cache types (`--cache-type-k`, `--cache-type-v`) including q8_0 / q4_0 KV with FlashAttention
- Enable speculative decoding with a small draft model for 1.5-3x token throughput on greedy/low-temp workloads
- Constrain output with GBNF grammars for guaranteed JSON / regex / function-call shape
- Use context shifting (StreamingLLM-style) so long sessions do not crash when KV is full
- Offload precise layer counts (`-ngl`) to fit VRAM exactly
## Approach
1. **Match backend to hardware first.** A wrong backend halves throughput before any tuning. NVIDIA → CUDA. AMD ≥ RDNA3 → HIP with rocWMMA. Apple → Metal (auto). Intel Arc / iGPU → SYCL or Vulkan. Mixed or laptop GPU → Vulkan.
2. **Pick the smallest quant that holds quality.** Start at Q4_K_M for 7-13B, Q5_K_M for 30B-class, Q6_K when VRAM allows. Drop to IQ3_XXS or IQ2 only with imatrix and only for 70B+.
3. **Size context honestly.** KV grows linearly with context × layers. Use `--cache-type-k q8_0 --cache-type-v q8_0 -fa` to roughly halve KV memory with negligible quality loss.
4. **Layer-offload to the byte.** Use `llama-bench` to find max `-ngl` that fits, then back off 1-2 layers for compute buffers.
5. **Add speculative decoding last.** Pair a Q4 draft (e.g. 0.5B-1B same family) with the main model via `--model-draft` / `-md`.
## Key Patterns
### Build for CUDA (Linux)
```bash
git clone https://github.com/ggml-org/llama.cpp && cd llama.cpp
cmake -B build -DGGML_CUDA=ON \
-DCMAKE_CUDA_ARCHITECTURES="86;89;90" \
-DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release -j
```
### Build for ROCm (RDNA3+)
```bash
cmake -B build -DGGML_HIP=ON \
-DAMDGPU_TARGETS=gfx1100 \
-DGGML_HIP_ROCWMMA_FATTN=ON \
-DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release -j
```
### Build for Vulkan (universal GPU fallback)
```bash
cmake -B build -DGGML_VULKAN=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release -j
```
### Build for SYCL (Intel Arc / iGPU)
```bash
source /opt/intel/oneapi/setvars.sh
cmake -B build -G Ninja \
-DCMAKE_C_COMPILER=icx -DCMAKE_CXX_COMPILER=icpx \
-DGGML_SYCL=ON -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release -j
```
### Run with KV-cache quantization + FlashAttention
```bash
./build/bin/llama-cli \
-m models/qwen2.5-7b-instruct-q4_k_m.gguf \
-ngl 99 -c 16384 \
-fa --cache-type-k q8_0 --cache-type-v q8_0 \
--temp 0.7 --top-p 0.9 \
-p "Explain Paged Attention in two sentences."
```
### Speculative decoding
```bash
./build/bin/llama-speculative \
-m models/llama-3.1-70b-instruct-q5_k_m.gguf \
-md models/llama-3.2-1b-instruct-q4_k_m.gguf \
-ngl 99 --draft-max 16 --draft-min 4 -c 8192 \
-p "Write a Python function for Dijkstra's algorithm."
```
### GBNF grammar for strict JSON
```gbnf
root ::= object
object ::= "{" ws "\"name\":" ws string "," ws "\"age\":" ws number ws "}"
string ::= "\"" ([^"\\] | "\\" .)* "\""
number ::= [0-9]+
ws ::= [ \t\n]*
```
```bash
./build/bin/llama-cli -m model.gguf --grammar-file person.gbnf -p "Make a person:"
```
### Context shifting for long sessions
`llama-cli` enables it by default; pass `--ctx-shift` explicitly on `llama-server`. When the KV fills, the oldest tokens are evicted instead of the program aborting. Combine with `-c` sized to hardware, not to prompt.
## Common Pitfalls
- **Wrong `-ngl`** → "out of memory" or silent CPU fallback. Use `llama-bench` first; do not guess.
- **Omitting `-fa` with quantized KV** on CUDA → silent fallback to F16 KV, OOM later.
- **IQ-quants without imatrix** → measurable quality loss. Always pair `IQ*` quants with an imatrix.
- **Q8_0 on Intel Arc Battlemage** → kernel inefficiency, ~4x slower than Q4_K_M (issue #21517). Prefer Q4_K_M on SYCL until fixed.
- **Mismatched chat template.** If you see broken outputs on instruct models, pass `--chat-template` or use `--jinja` with a model that ships a Jinja template in metadata.
- **Speculative decoding regressions** at high temperature or with non-aligned tokenizers — only use draft from same model family.
- **Context-shift drift.** After many evictions, quality degrades on tasks requiring early context. Use larger `-c`, not infinite shift.
## Hardware/Resource Sizing
| Model size | Quant | Min VRAM | Notes |
|------------|-------|----------|-------|
| 7-8B | Q4_K_M | 6 GB | Comfortable on 8GB cards |
| 13-14B | Q4_K_M | 10 GB | Fits 12GB with 4k ctx |
| 30-34B | Q4_K_M | 20 GB | 24GB card needed for ctx > 4k |
| 70B | Q4_K_M | 42 GB | Needs dual 24GB or 48GB pro |
| 70B | IQ3_XXS | 28 GB | Single 32GB or aggressive offload |
KV cache (per token) ≈ `2 × n_layers × n_kv_heads × head_dim × bytes_per_element`. Halving via `q8_0` cache buys 2x context for the same VRAM.
## When to Use This Mode
- Single-machine local inference where Ollama feels too high-level
- Edge deploys (Jetson, Raspberry Pi, Steam Deck) where Vulkan or CPU is the only option
- Producing GGUFs for downstream tools (LM Studio, Jan, llama-server, Ollama Modelfiles)
- Anywhere you need GBNF grammar or fine-grained sampler control
- Use **llama-cpp-server-expert** instead for HTTP/OpenAI-compat serving
- Use **vllm-local-deploy-expert** for batch throughput on data-center GPUs
## Sources
- [llama.cpp build docs](https://github.com/ggml-org/llama.cpp/blob/master/docs/build.md)
- [llama.cpp main repo](https://github.com/ggml-org/llama.cpp)
- [Speculative decoding](https://github.com/ggml-org/llama.cpp/blob/master/docs/speculative.md)
- [GBNF grammar guide](https://github.com/ggml-org/llama.cpp/blob/master/grammars/README.md)
- [SYCL Q8_0 vs Q4_K_M issue #21517](https://github.com/ggml-org/llama.cpp/issues/21517)
- [Vulkan performance discussion #10879](https://github.com/ggml-org/llama.cpp/discussions/10879)