Architecture, Engineering, Optimization and Production Deployment
Introduction
Chapter 1: The Case for Small, Private AI
- Defining the Design Space: What “Small” Means
- The Business and Technical Case for Local Inference
- Privacy, Security and Regulatory Drivers
- Measurable Success Criteria and Metrics
- Fundamental Trade-offs: Quality versus Speed versus Size
- When Small AI Is the Wrong Choice
- Chapter Summary
Chapter 2: The Inference Lifecycle
- Model Loading and Weight Deserialization
- Tokenization: Encoding, Decoding and the Vocabulary
- Prompt Construction and Template Engineering
- Prefill: Processing the Input Context
- Autoregressive Decoding and the Generation Loop
- Attention Mechanisms and Key-Value Caching
- Sampling Strategies and Probability Distributions
- Streaming, Batching and Concurrency
- Chapter Summary
Chapter 3: Model Selection and Architectures
- Parameter Counts and Real Resource Requirements
- The Modern Small Model Landscape
- Architecture Patterns
- Context Length and Its Costs
- Instruction-Tuned, Chat and Base Variants
- Model Licensing and Compliance
- Selection Framework: Matching Models to Workloads
- Chapter Summary
Chapter 4: Languages and Runtimes
- Python: Ecosystem Power and Overhead
- C++ and Rust: Control and Performance
- WebAssembly and JavaScript: Browser and Edge
- Hybrid Approaches: Language Bridges and FFI
- Compilation Strategies: AOT, JIT and Native
- Runtime Selection Framework
- Chapter Summary
Chapter 5: Inference Frameworks and Libraries
- llama.cpp and the GGUF Ecosystem
- The Hugging Face Transformers Library
- MLX on Apple Silicon
- TensorRT-LLM and NVIDIA’s Toolchain
- ONNX Runtime and Cross-Platform Execution
- vLLM and Production-Grade Throughput
- Comparison Matrix and Selection Guide
- Chapter Summary
Chapter 6: Hardware, Backends and Acceleration
- CPU Inference: SIMD, AVX2, AVX-512 and Vectorization
- GPU Inference: CUDA, ROCm and Metal
- Memory Bandwidth and the Memory Wall
- Edge Hardware: NPUs, Mobile and Constrained Devices
- Power Consumption and Thermal Constraints
- Hardware Selection for Target Deployments
- Chapter Summary
Chapter 7: Quantization and Reduced Precision
- Why Quantize: The Theory Behind Reduced Precision
- Weight-Only Quantization: Q4, Q5, Q8 and Beyond
- Activation-Aware Quantization and KVCaching
- GGUF Quantization Methods and Group Size
- Calibration, Accuracy Measurement and Artifacts
- Quantization Workflow: From FP16 to Production
- Chapter Summary
Chapter 8: Memory Architecture and Tensor Execution
- Tensor Layouts and Memory Strides
- Model Memory Footprint Calculations
- Memory-Mapped Weights and Lazy Loading
- KV Cache Allocation and Eviction Strategies
- Fragmentation and Memory Pooling
- Operating System-Level Memory Management
- Chapter Summary
Chapter 9: Latency Optimization and Speculative Decoding
- Time-to-First-Token and Inter-Token Latency
- Speculative Decoding: Draft Models and Verification
- Prompt Caching and Prefix Reuse
- Operator Fusion and Graph Optimization
- Speculative Execution and Parallelism
- Measuring Latency Improvements
- Chapter Summary
Chapter 10: Benchmarking and Performance Measurement
- Defining Benchmark Workloads and Prompts
- Time-to-First-Token and Generation Speed
- Throughput, Batching and Saturation
- Memory Profiling and Resource Monitoring
- Reproducible Benchmarking: Hardware, Software and Environment
- Interpreting Results and Comparing Systems
- Chapter Summary
Chapter 11: The Minimal Inference Application
- Environment Setup and Dependency Installation
- Model Acquisition and Verification
- Implementing the Inference Loop
- Prompt Handling and Output Formatting
- Testing and Validation
- Building and Running the Application
- Chapter Summary
Chapter 12: The Inference Engine
- Engine Architecture and Component Design
- Model Registry and Switching
- Sampling Parameters and Configuration
- Streaming Output and Callbacks
- Concurrency and Request Queueing
- Error Handling and Resource Cleanup
- Chapter Summary
Chapter 13: The Private HTTP API
- API Design and Contract Definition
- OpenAI-Compatible Endpoint Implementation
- Authentication and Access Control
- Request Validation and Sanitization
- Rate Limiting and Resource Protection
- Error Codes and Client-Facing Responses
- Chapter Summary
Chapter 14: Streaming, Structured Outputs and Tool Use
- Server-Sent Events and Streaming Architecture
- JSON Schema Enforcement and Structured Generation
- Function Calling and Tool Definitions
- Multi-Turn Conversations and State Management
- Output Validation and Recovery
- Integration with Client Applications
- Chapter Summary
Chapter 15: Embeddings, Semantic Search and RAG
- Embedding Models and Dimensionality
- Vector Storage and Indexing
- Similarity Search and Retrieval Strategies
- RAG Pipeline Architecture
- Chunking, Indexing and Update Strategies
- Complete RAG Implementation
- Chapter Summary
Chapter 16: Threat Models and Security Architecture
- The Private Inference Threat Model
- Network Security and Isolation
- Prompt Injection and Output Poisoning
- Authentication and Authorization Patterns
- Secrets Management and Configuration
- Logging, Auditing and Data Retention
- Chapter Summary
Chapter 17: Supply Chain, Dependencies and Verification
- Trusting Model Weights and Files
- Software Supply Chain Risks
- Reproducible Builds and Verification
- Offline Deployment and Update Strategies
- Artifact Signing and Validation
- Chapter Summary
Chapter 18: Deployment Topologies and Lifecycle Management
- Deployment Platforms: Linux, macOS, Windows, Edge
- Containerization and Docker Images
- Systemd and Service Supervision
- Health Checks and Readiness Probes
- Versioning, Rollback and Blue-Green
- Multi-Instance and Load-Balanced Deployments
- Chapter Summary
Chapter 19: Observability, Maintenance and Troubleshooting
- Metrics Collection and Monitoring
- Request Tracing and Debugging
- Log Aggregation and Analysis
- Common Failure Modes and Diagnostics
- Capacity Planning and Scaling
- Long-Term Maintenance Patterns
- Chapter Summary
Chapter 20: Patterns, Principles and the Future
- Design Patterns for Small Inference Systems
- Universal Principles and Hardware-Specific Rules
- The Economics of Local versus Cloud Inference
- Emerging Architectures and Technologies
- When to Evolve Your Stack
- Final Checklist for Production Deployment
- Closing Thoughts