llama.cpp
llama.cpp provides C/C++ model inference across CPU and accelerator backends. Its core connects a model file, tokenization, a computation graph, device buffers and a sampling loop. A server can wrap this library, but HTTP is not required for local generation.
Local application
A CLI or application calls the library API with a prompt and generation settings. The application owns the outer generation loop and handles output pieces, end-of-generation tokens and length limits. Network transport is optional.
- Local application TokenizerPrompt text
One pass through the system.
Select a step to highlight the components involved in the diagram.
Why it is built this way.
Quantization saves weight memory, with a quality cost to measure
Lower-bit GGUF tensors can make a model practical on smaller hardware. Quality and speed depend on tensor types, the model and kernel support. Context cache and temporary execution buffers remain separate costs.
Offloading balances capacity against transfer overhead
Keeping work on an accelerator can improve throughput when memory permits. Splitting execution across CPU and accelerator can fit a larger model, but device transfers and slower portions of the graph can dominate latency.
Prompt processing and decoding stress different resources
Prefill processes a batch of prompt tokens; autoregressive decoding repeatedly consumes newly selected tokens and existing context. Measure both phases with realistic context lengths, rather than treating one tokens-per-second result as universal.
Read the implementation.
src/llama-context.cppTrace context state, token processing and graph execution coordination.
ggml/src/ggml-backend.cppFollow backend buffers, graph scheduling and cross-device copies.
src/llama-sampler.cppRead how sampler chains turn scores into token choices.
Go deeper with the official docs
- llama.cpp: project and supported backends
- Minimal local generation example
- ggml backend scheduler implementation
- libllama context implementation
Educational diagrams, synthesized from the linked project documentation. Components and boundaries vary by version and deployment.