Building High-Performance LLM Inference Engine from Scratch
- About This Book
Introduction: Why Rust for LLM Inference
- The Inference Engine We Will Build: llm-rs
- The LLM Serving Landscape: A Competitive Ecosystem
- Why Rust? The Systems Argument
- How to Read This Book
- Framework Trade-offs: When Rust Makes Sense (and When It Does Not)
- What This Book Is Not
Chapter 1: The Transformer Architecture
- What a Transformer Is and Why It Works
- Encoder-Decoder vs Decoder-Only
- Layer-by-Layer Anatomy
- The Forward Pass as Computation Graph
- Model Configuration in Rust
- Try It Yourself: Building a Model Config
Chapter 2: Tokenization and the Vocabulary
- Byte-Pair Encoding and Its Variants
- Building and Using Tokenizers in Rust
- Prompt Assembly and Token Counting
- Try It Yourself: Implementing a Simple BPE Tokenizer
- Try It Yourself: Building a Prompt Assembler
Chapter 3: Attention Mechanisms Deep Dive
- Scaled Dot-Product Attention Derivation
- Multi-Head Attention and Head Pruning
- FlashAttention and I/O-Aware Computation
- Causal (Masked) Attention for Autoregressive Generation
- Rotary Position Embeddings (RoPE) Detail
- Try It Yourself: Implementing Scaled Dot-Product Attention
- Try It Yourself: Implementing RoPE
Chapter 4: Building the Core Inference Loop
- Setting Up a Minimal Rust Project
- Implementing Linear Layers, RMSNorm, and Activations
- Composing a Complete Transformer Layer
- The Autoregressive Generation Loop
- Benchmarking a Single-Layer Forward Pass
- Try It Yourself: Building a Complete Inference Loop
- Try It Yourself: Sampling Strategies
Chapter 5: KV Caching and Autoregressive Optimization
- Why Naive Autoregressive Inference Is O(n²) in Memory and Compute
- Key-Value Cache Design and Tensor Shapes
- PagedAttention: Memory Management as an Operating System Problem
- Implementing PagedAttention in Rust
- Cache Eviction Strategies
- Try It Yourself: Implementing a Contiguous KV Cache
- Try It Yourself: Implementing PagedAttention
Chapter 6: Quantization for Memory and Speed
- Floating-Point Formats: FP32, FP16, BF16, FP8
- INT8 and INT4 Quantization Schemes
- GGUF/GGML Format and llama.cpp’s Approach
- Implementing Dequantize-and-Multiply in Rust
- Quantization Schemes Compared
- Try It Yourself: Implementing INT4 Quantization
- Try It Yourself: Parsing GGUF Files
Chapter 7: Batching Strategies
- Static vs Dynamic Batching
- The Continuous Batching Scheduler
- Continuous Batching and PagedAttention
- Request Scheduling and Priority
- Throughput-Latency Tradeoffs
- Try It Yourself: Implementing a Scheduler
- Try It Yourself: Prefix Caching with RadixAttention
Chapter 8: CPU Optimization Techniques
- SIMD Vectorization with Rust
- Cache-Conscious Memory Layout
- Parallelism with Rayon and Work-Stealing
- BLAS Integration for Matmul
- Benchmarking Methodology and Results
- Try It Yourself: SIMD Matrix Multiplication
- Try It Yourself: Rayon Parallel Matmul
- Try It Yourself: CPU Profiling with perf
Chapter 9: GPU Acceleration with CUDA and ROCm
- Rust CUDA Bindings: Burn, Candle, llama.cpp
- Building a CUDA Backend for llm-rs
- Writing Custom CUDA Kernels
- Memory Allocation Patterns for GPU Tensors
- Profiling with Nsight
- torch.compile-Style Kernel Fusion
- Try It Yourself: Writing a CUDA Kernel
- Try It Yourself: Implementing a Dequantize-and-Matmul Kernel
- Try It Yourself: GPU Memory Pool
Chapter 10: Model Formats and Serialization
- PyTorch .bin and Safetensors Formats
- GGUF/GGML, ONNX, MLX, and TensorRT
- Serialization in Rust
- Weight Loading and Layout Transformation
- Loading GGUF Models in Rust
- Try It Yourself: Building a Model Loader
- Try It Yourself: GGUF Parser
Chapter 11: Distributed Inference and Serving
- Tensor Parallelism vs Pipeline Parallelism
- Communication Patterns: All-Reduce and All-Gather
- Serving Frameworks: vLLM, TGI, Ollama, Mistral.rs
- Building a Production Rust Inference Server
- Try It Yourself: Building a Multi-GPU Engine
- Try It Yourself: Streaming API
Chapter 12: Production Deployment and Monitoring
- Containerization and Orchestration
- Observability: Metrics, Tracing, Logging
- Fault Tolerance and Recovery
- A/B Testing New Models and Quantizations
- Cost Analysis: Dollars per Million Tokens
- Try It Yourself: Deploying with Docker
- Try It Yourself: Setting Up Prometheus Metrics
- Try It Yourself: A/B Testing Pipeline
Appendix A: Benchmarking Methodology
- Hardware Specifications
- Software Versions
- Benchmarking Procedure
- Reproducing the Results
- Understanding Benchmark Numbers
- Statistical Rigor
Conclusion: Where Rust Fits in the LLM Ecosystem
- The Trade-offs We Made
- Where Rust Shines (and Where It Does Not)
- The Pragmatic Stack
- The Future of Rust in LLM Inference
- What Comes Next
