The Complete Guide to Building, Running and Optimizing Local LLM Inference
Introduction: Why Local Inference Matters
Chapter 1: The llama.cpp Revolution
- The Birth of GGML
- Whisper.cpp and the Catalyst Moment
- The GGUF Format
- The Open-Source LLM Landscape
- Why Local Inference Matters
Chapter 2: Architecture Deep Dive
- The GGML Tensor System and Computation Graph
- The GGUF File Format: Structure and Semantics
- Model Loading Pipeline: From Disk to VRAM
- The Inference Engine: Token Generation Loop
Chapter 3: Inside the GGML Tensor Library
- The Define-Then-Execute Computation Model
- The ggml_tensor Structure
- Memory Arenas and Contexts
- Backend Abstraction and Plugin System
- CPU Backend: SIMD Dispatch and Quantized Kernels
- Flash Attention Implementation in GGML
- From GGML to llama.cpp: The Integration Layer
Chapter 4: Custom GGML Backend Development and Novel Hardware
- When to Write a Custom Backend
- The GGML Backend Interface
- Step-by-Step: Implementing a Minimal Backend
- Writing Efficient Custom Kernels
- Debugging and Testing Your Backend
- Contributing to llama.cpp
Chapter 5: The llama.cpp C/C++ API
- Overview of llama.h
- Loading a Model
- Initializing a Context
- Tokenization and Building Batches
- Single-Token Generation and Sampling
- Common Sampler Operations
- Saving and Loading Sessions
- Building Applications with the C API
Chapter 6: Building from Source - Linux
- Prerequisites and Dependency Management
- CMake Configuration Options for Linux
- Building with CPU-Only Support
- Enabling CUDA on Linux
- Enabling ROCm on Linux
- Common Build Errors and Fixes
Chapter 7: Building from Source - macOS and Windows
- macOS: Apple Silicon Metal Builds (The Sweet Spot)
- macOS: Intel Macs and CPU-Only Builds
- Windows: MSVC Build with CMake
- Windows: WSL2 as a Practical Alternative
- Cross-Compilation Considerations
- The XCFramework: Native iOS, visionOS and tvOS Support
Chapter 8: Model Conversion and Quantization
- The Conversion Pipeline: Hugging Face to GGUF
- Quantization Theory: Why Quantize and What Is Lost
- GGML Quantization Schemes Explained
- Choosing the Right Quantization for Your Use Case
- GPTQ and AWQ Model Support
Chapter 9: LoRA Adapters Deep Dive
- What Is LoRA and Why It Matters
- Converting PEFT Adapters to GGUF
- Loading Adapters with the CLI
- Using LoRA in llama-server
- Using LoRA with the C API
- Stacking Multiple Adapters
- Memory and Performance Considerations
- LoRA vs Full Fine-Tuning
Chapter 10: Inference Fundamentals - CLI Tools
- llama-cli: The Interactive Inference Tool
- Key Generation Parameters Explained
- Prompt Formats and Chat Templates
- Context Window Management and KV Cache Tuning
- Streaming Output and Real-Time Token Generation
Chapter 11: Server Mode and API Integration
- Starting and Configuring llama-server
- The OpenAI-Compatible API Specification
- Authentication, CORS and Security Considerations
- Tool Calling and MCP Server Integration
- Integrating with Existing Applications
- Router Mode and Dynamic Model Management
Chapter 12: Server Internals and Scheduling
- How Continuous Batching Works
- The Batch Processing Pipeline
- Decode-Maximal Scheduling
- Request Lifecycle in the Server
- KV Cache and Context Management
- Router Mode Internals
- Performance Characteristics
Chapter 13: Security, Privacy and Air-Gapped Deployments
- The Local Inference Security Model
- Authentication and Authorization
- Network Security and SSL/TLS
- Container Security
- Rate Limiting and DoS Protection
- Air-Gapped Deployment
- Model Supply Chain Security
- Data Privacy and Prompt Redaction
- Defense in Depth Checklist
Chapter 14: Advanced Inference Techniques
- Batch Inference: Multiple Prompts, Throughput Gains
- Speculative Decoding: How It Works, When It Helps
- Grammar-Constrained Generation: JSON, Regex, Structured Output
- Advanced Sampling: Mirostat, Top-A, Tail-Free
- KV Cache Optimizations: Offloading and Sliding Windows
Chapter 15: Embeddings and Multimodal Models
- Embedding Generation with llama.cpp
- Embedding Use Cases: RAG, Semantic Search, Clustering
- Multimodal Model Support: LLaVA, Qwen2-VL and Beyond
- Image Preprocessing and Tokenization for Vision Models
- Practical Multimodal Workflows
Chapter 16: GPU Backends and Hardware Acceleration
- CUDA Backend: NVIDIA GPUs, Memory Management, Performance Tuning
- ROCm Backend: AMD GPUs, Setup and Limitations
- Vulkan Backend: Cross-Platform GPU Acceleration
- Apple Metal: The macOS/iOS Sweet Spot
- Multi-GPU Inference and Distributed Computing
Chapter 17: Performance Tuning and Benchmarking
- llama-bench: The Benchmarking Tool
- Measuring Tokens Per Second Across Hardware Configurations
- Memory Footprint Analysis and Optimization
- Throughput vs Latency Trade-offs in Production
- Profiling Techniques and Identifying Bottlenecks
Chapter 18: Ecosystem Tools and Integrations
- Ollama: The Popular Abstraction Layer
- LM Studio: Desktop GUI with llama.cpp Backend
- koboldcpp: Specialized Backend for Creative Writing
- llama-cpp-python: Python Bindings for Direct Integration
- llama-cpp-rs: Rust Bindings
- WebGPU and Browser-Based Inference
- Choosing the Right Tool
Chapter 19: Real-World Deployment and Production
- Docker Containerization of llama.cpp
- Docker Compose for Production Deployment
- Kubernetes Deployment Patterns
- Monitoring, Logging and Alerting
- CI/CD Pipelines for Model Serving
- Case Studies: Production Deployments at Various Scales
- Troubleshooting Common Production Issues
