llama.cpp
llama.cpp is an open-source C/C++ large language model inference project initiated by Georgi Gerganov, aiming to run LLaMA-family models with minimal dependencies on hardware ranging from servers to Raspberry Pi and even phones. It is the de facto standard of the on-device and local inference ecosystem, and the GGUF format, quantization schemes and a large community toolchain have all grown around it.
Core Principles
It does not depend on PyTorch but implements forward computation, KV cache management, sampling and decoding logic itself in pure C/C++. Computation is performed through ggml, a tensor library supporting multiple backends such as CPU (AVX2, AVX512, NEON), CUDA, Metal, Vulkan and HIP, with the computation graph split automatically according to available hardware at runtime. Precisely because there is no Python runtime and no layers of framework wrapping, its memory footprint and startup speed on the same hardware are usually far better than framework-based inference, with cold start achievable in seconds.
Before entering llama.cpp, models must be converted to GGUF format. GGUF is a single-file format with metadata and tensors together, supporting memory mapping so loading is almost zero-copy, and it can offload some tensors to the GPU by layer, with the number of offloaded layers controlled by the -ngl parameter. Quantization is done during this conversion stage, compressing weights from 16 bits to 2 to 8 bits so that a 7B-class model can run on a device with 4GB of memory.
During inference the core is incremental maintenance of the KV cache: for each generated token, only the last layer needs computation and the historical K/V is reused, avoiding a full forward pass over the whole sequence. The project also implements continuous batching (parallel requests in server mode), speculative decoding (small model drafting, large model verification), prompt cache reuse, and GBNF grammar-constrained output, so it is not merely "able to run" but practical enough on a single machine.
Key Points
Quantization is its signature. Q4_0, Q4_K_M, Q5_K_M, Q6_K and Q8_0 form the common ladder, where those with K are k-quants that quantize in blocks according to tensor importance and generally offer better quality than traditional quantization at the same bit width. The IQ series (IQ1_S to IQ4_XS) introduces importance matrices for extreme compression, suited to edge devices with severe memory constraints. The community default is usually Q4_K_M, balancing size and quality.
On file formats, llama.cpp only accepts GGUF, so PyTorch safetensors must first be converted with convert_hf_to_gguf.py and then quantized. Common entry points include: llama-cli for command-line chat, llama-server providing an OpenAI-compatible HTTP service, llama-bench for performance testing and llama-quantize for quantization. A typical command looks like ./llama-cli -m model.Q4_K_M.gguf -ngl 33 -c 4096. Upper-layer ecosystems such as Ollama, LM Studio and llamafile are mostly built on llama.cpp underneath.
The positioning difference from vLLM should be clear: llama.cpp excels at low resources, cross-platform operation and single-machine use, with particularly good support for CPUs and Apple Silicon; vLLM excels at high-concurrency GPU throughput. The former suits one or two cards or on-device deployment, and the latter suits server-side batch inference. Mixing the two is also common: llama.cpp at the edge, vLLM in the center.
Another easily overlooked capability is embedding and multimodal support. It not only does text generation but can also load BERT-class embedding models for retrieval, and run vision-language models (such as the LLaVA series) and some speech models. In server mode it also provides embeddings and rerank endpoints, so with a vector database one can build a fully local RAG system that depends on no cloud service. This "fully offline" property is critical in data-sensitive industries.
Common Misconceptions
Misconception one is thinking the more aggressive the quantization the better. Extreme quantization such as Q2 and IQ1 loses noticeably on reasoning, code and math tasks, and Q4_K_M or Q5_K_M is the more stable balance for general use. Misconception two is using llama.cpp to withstand high-QPS online traffic; its batching and scheduling are inferior to dedicated engines and GPU utilization suffers noticeably. Misconception three is ignoring the interaction between -ngl and context length: offloading too many layers and opening too long a context directly exhausts VRAM and instead causes frequent fallback to the CPU.
Application Scenarios
Local private deployment, offline document Q&A, laptop and mobile inference, embedded and Raspberry Pi experiments, comparative evaluation of model quantization, and offline batch text processing.