Skip to main content

llama.cpp — Source Code Analysis

Quick Summary

llama.cpp is a C/C++ inference engine for large language models, implementing 100+ model architectures with a custom tensor compute library (GGML) and 15+ hardware backend accelerators. It uses a lazy compute graph with vtable-dispatched backends, a composable sampler chain for token selection, and the GGUF self-describing binary format for memory-mappable model weights. The project includes an OpenAI-compatible HTTP server with multi-slot concurrent inference.

Documents

FileContents
01-overview.mdProject classification, tech stack, directory map, module diagram
02-startup.mdEntry points, initialization sequence, thread/process model, memory layout
03-api.mdREST API endpoints (OpenAI/Anthropic compatible), C library API, sequence diagrams
04-shutdown.mdSignal handling, graceful shutdown sequence, resource cleanup inventory
05-data-structures.mdCore structs — ggml_tensor, ggml_cgraph, llama_model, llama_hparams, llama_batch, llama_layer, ggml_backend_i, llama_sampler_i
06-storage.mdGGUF binary format specification, metadata keys, loading process, conversion scripts
07-protocol.mdHTTP/SSE transport patterns, streaming response format
08-crosscutting.mdAPI key authentication, Prometheus metrics, slot-based concurrency control
09-extensions.mdGGML backend vtable system, sampler chain, model architecture extension

Key Files to Read First

FileReason
include/llama.hPublic API — all functions callers use (1565 lines)
ggml/include/ggml.hGGML tensor types, ops, and core abstractions
src/llama-model.cppModel loading and forward pass implementation for all architectures
tools/server/server.cppServer entry point, route registration, startup/shutdown
ggml/src/ggml-backend-impl.hBackend vtable definitions — the hardware extension point
src/llama-sampler.cppAll sampling algorithm implementations
ggml/include/gguf.hGGUF format specification and API
src/llama-kv-cells.hKV cache cell tracking — core to understanding context management