LLM Serving Engine Shootout: vLLM vs TensorRT-LLM vs SGLang vs llama.cpp
The economics and performance of generative AI at scale are governed by the efficiency of the LLM Serving Engine. Deploying modern Large Language Models (such as Llama 3 70B, DeepSeek-V3, Mixtral 8x22B, and Qwen 2.5) in production environments is not merely a matter of executing matrix multiplications on a GPU. It is an intricate operating-systems challenge involving dynamic GPU memory management, continuous request scheduling, KV cache prefix reuse, and hardware-specific kernel fusion.
Inference operates in two fundamentally distinct computational regimes:
- Prefill Phase (Prompt Processing): Compute-bound ( attention compute over prompt tokens, saturating GPU Tensor Cores).
- Decode Phase (Token Generation): Memory bandwidth-bound ( autoregressive generation per step, constrained by GPU High Bandwidth Memory / HBM throughput).
Over the past two years, four dominant engines have emerged to solve these hardware bottlenecks:
- vLLM: The pioneer of PagedAttention, eliminating memory fragmentation and standardizing continuous batching.
- TensorRT-LLM: NVIDIA’s compiler-driven enterprise engine with Fused Multi-Head Attention (FMHA), Deep Learning accelerator optimization, and maximum raw throughput on NVIDIA Hopper/Blackwell GPUs.
- SGLang: The specialized runtime featuring RadixAttention (compressed prefix-tree caching) for complex multi-turn conversations, agent workflows, and structured JSON output.
- llama.cpp: The minimalist, dependency-free C/C++ engine dominating CPU, Apple Silicon Metal, and edge hardware via GGUF k-quants.
This architectural deep dive analyzes the inner mechanics, memory layouts, cache eviction graphs, and kernel optimizations of each engine, concluding with mathematical memory bounds, latency benchmarks, and production deployment blueprints.
1. The Physics of LLM Inference: Memory Bandwidth vs Compute
To understand why traditional deep learning frameworks (like raw PyTorch eager execution) fail at production serving, we must examine the Roofline Model of GPU execution.
1.1 The Arithmetic Intensity Gap
Arithmetic intensity () is the ratio of computational operations (FLOPs) to memory traffic (Bytes transferred):
- Prefill Phase: When processing an initial prompt of tokens, the model computes all activations simultaneously. The compute scales quadratically with sequence length while weights are loaded only once. Arithmetic intensity is high (), fully saturating NVIDIA H100 Tensor Cores ( FP16).
- Decode Phase: To generate a single token (), the GPU must read the entire model weight tensor () and the accumulated KV cache from HBM into SRAM registers just to compute a single forward pass. Arithmetic intensity collapses ().
On an NVIDIA H100 GPU with HBM3 bandwidth, the maximum theoretical decode speed for an unquantized 70B model with a batch size of 1 is physically bounded by:
To achieve high throughput, the engine must batch multiple concurrent requests together (), reusing the loaded model weights across tokens simultaneously.
2. The KV Cache Bottleneck: Mathematics & Memory Fragmentation
The Key-Value (KV) Cache avoids recomputing keys and values for historical tokens at every autoregressive step. However, the KV cache grows dynamically with sequence length and rapidly consumes the majority of GPU VRAM.
2.1 KV Cache Memory Equation
For a Transformer model using Multi-Head Attention (MHA), the KV cache memory required per token per request is:
Where:
- : Represents the separate Key and Value tensors.
- : Represents standard FP16 / BF16 floating-point precision ().
- : Number of transformer decoder layers.
- : Number of key-value attention heads (reduced in Grouped-Query Attention / GQA).
- : Dimension per head ().
For Llama 3 70B (, , , using BF16):
For a batch of concurrent requests with a context length of tokens:
This surpasses the total memory of four NVIDIA H100 GPUs solely for intermediate attention state.
2.2 The Memory Fragmentation Crisis
In naïve serving architectures (such as standard HuggingFace Accelerate or basic PyTorch pipelines), memory for the maximum possible sequence length () must be allocated contiguously upfront:
Contiguous pre-allocation results in three catastrophic forms of waste:
- Reserved Over-allocation: Memory reserved for future tokens that the user may never generate.
- Internal Fragmentation: Unused slots within pre-allocated static buffers.
- External Fragmentation: Free memory scattered in non-contiguous fragments too small to satisfy new incoming requests.
3. Deep Dive: vLLM & PagedAttention
Created by Woosuk Kwon et al. at UC Berkeley (2023), vLLM revolutionized LLM infrastructure by introducing PagedAttention, an algorithm directly inspired by the virtual memory and paging subsystems of classic operating systems.
3.1 PagedAttention Mechanics
Instead of requiring contiguous physical memory, PagedAttention partitions the KV cache of each sequence into fixed-size Physical Blocks (typically 16 or 32 tokens per block).
- Logical Blocks: The sequence view seen by the attention computation.
- Physical Blocks: Allocated non-contiguously on-demand from a global GPU memory pool.
- Block Table: Maps logical block indices to physical GPU memory addresses.
During attention computation, the custom CUDA PagedAttention kernel dynamically gathers non-contiguous key-value blocks using the block table:
3.2 Copy-on-Write (CoW) for Parallel Sampling & Beam Search
When an application requests multiple completions from a single prompt (e.g., temperature sampling with or tree-of-thought search), multiple output sequences share the identical prompt prefix.
Through Copy-on-Write, shared prefix memory overhead drops to zero, enabling massive batch scaling for synthetic data generation and reasoning exploration.
4. Deep Dive: SGLang & RadixAttention
While vLLM solved intra-request fragmentation, modern AI architectures (e.g., autonomous coding agents, multi-turn chat, few-shot prompt chaining, and tree search) exhibit massive inter-request prefix sharing.
Created by Lianmin Zheng et al. (2024), SGLang introduces RadixAttention, a runtime that treats the entire GPU KV cache as a dynamic Radix Tree (Compressed Prefix Trie).
4.1 Radix Tree Prefix Cache Mechanics
In traditional engines, once a request finishes, its KV cache is freed. If the user replies in a multi-turn conversation, the engine must recompute the prefill for all historical tokens.
In SGLang:
- When a sequence completes, its physical KV pages are retained in GPU VRAM and indexed in a radix tree where tree edges represent token sequences.
- When a new request arrives, SGLang performs a longest-prefix match on the radix tree.
- If a match of length is found, the engine completely skips prefill for the first tokens, reducing Time to First Token (TTFT) from hundreds of milliseconds to under .
4.2 LRU Eviction with Reference Counting
When GPU memory reaches capacity, SGLang evicts leaf nodes using an eviction policy based on Least Recently Used (LRU) tracking and reference counting:
4.3 Fast Constrained Decoding with Finite State Machines
For structured generation (JSON schemas, regex validation), SGLang compiles the regex into an optimized Finite State Machine (FSM) via Outlines. At each decode step, the FSM masks invalid token logits directly in GPU memory before softmax, ensuring valid JSON generation without CPU sampling roundtrips.
5. Deep Dive: TensorRT-LLM (NVIDIA Enterprise Engine)
Developed directly by NVIDIA engineers, TensorRT-LLM is an enterprise-grade inference compiler and execution runtime tailored specifically for NVIDIA GPU architectures (Ampere, Ada Lovelace, Hopper H100/H200, and Blackwell B200).
5.1 Fused Multi-Head Attention (FMHA) & Kernel Fusion
Standard PyTorch executes attention via separate kernel launches:
- Matrix multiplication ().
- Softmax scaling & normalization.
- Dropout/masking.
- Matrix multiplication ().
Between each step, intermediate tensors are written back to slow global HBM memory. TensorRT-LLM compiles these operations into a single, fused FMHA CUDA kernel that executes entirely within on-chip SRAM (Shared Memory / L1 Cache), completely bypassing HBM for intermediate attention scores (delivering speedups over unfused kernels).
5.2 Native In-Flight Batching (IFB)
TensorRT-LLM implements iteration-level scheduling natively in C++:
- Rather than waiting for an entire batch to finish generating, newly arrived requests are injected into the active batch at the very next decode iteration.
- Completed requests are evacuated immediately, eliminating idle GPU bubbles.
6. Deep Dive: llama.cpp & GGUF Quantization
Developed by Georgi Gerganov, llama.cpp takes the diametrically opposite architectural approach: maximum portability, zero external dependencies, and extreme optimization for commodity hardware (Apple Silicon Metal, x86 AVX-512, ARM NEON, and consumer GPUs).
6.1 GGUF File Format & mmap() Instant Startup
GGUF is a self-contained, binary container format that stores all model weights, tokenizer metadata, and hyperparameter tensors aligned to physical memory page boundaries (e.g., ).
- When
llama.cpploads a GGUF model, it does not read the bytes through standard file I/O into heap memory. - It invokes
mmap(), mapping the file directly into the process virtual address space. - Model startup takes less than 10 milliseconds, allowing models to be loaded and unloaded dynamically on resource-constrained servers.
6.2 The Anatomy of K-Quants (Q4_K_M, Q5_K_M, IQ4_XS)
Standard uniform quantization (like vanilla INT4) applies a single scaling factor across an entire matrix, resulting in catastrophic perplexity degradation on delicate attention projection layers.
llama.cpp uses k-quants (k-means quantization), breaking weight matrices into super-blocks (e.g., 256 weights) subdivided into 8 mini-blocks (32 weights each). Different layers receive tailored precision:
| Quantization Type | Bits / Weight | Perplexity Penalty (Llama 3 70B) | Optimal Use Case |
|---|---|---|---|
| FP16 / BF16 | 16.0 | Baseline () | Training / Reference Ground Truth |
| Q8_0 | 8.5 | (Indistinguishable) | High-accuracy enterprise serving |
| Q5_K_M | 5.5 | (Negligible) | Sweet spot for Apple Silicon / Pro GPUs |
| Q4_K_M | 4.5 | (Minimal) | Industry standard for local LLM inference |
| IQ4_XS | 4.25 | (Very Low) | Memory-constrained edge devices / 8GB VRAM |
| Q2_K | 2.5 | (Noticeable degradation) | Extreme low-memory experiments |
7. The Comprehensive Benchmark Shootout
7.1 Architecture & Feature Comparison Matrix
| Feature / Dimension | vLLM | TensorRT-LLM | SGLang | llama.cpp |
|---|---|---|---|---|
| Primary Target Hardware | NVIDIA / AMD Datacenter GPUs | NVIDIA Hopper / Blackwell | NVIDIA / AMD Datacenter GPUs | Apple Silicon / CPU / Edge / Consumer GPU |
| Core KV Algorithm | PagedAttention (Virtual Tables) | In-Flight Paged KV Buffer | RadixAttention (Prefix Trie) | Ring-buffer KV Cache (ggml-alloc) |
| Prefix Caching | Static Hash Prefix Cache | Static Hash Prefix Cache | Dynamic Radix Trie (Automatic) | State slot save/restore |
| Kernel Implementation | CUDA + OpenAI Triton + C++ | Hand-tuned CUTLASS & FMHA | CUDA + FlashInfer + Triton | Hand-written C + ARM NEON + Metal |
| Structured Output Speed | Moderate (Outlines / Guided) | Moderate (XGrammar / Custom) | Ultra-Fast (FSM Pre-compiled) | Fast (Grammar BNF parser) |
| Engine Setup Complexity | Low (Single pip install vllm) | High (Docker build + compilation) | Low (pip install sglang) | Ultra-Low (Single binary C++ build) |
| Peak Token Throughput | Very High () | Maximum () | Top Tier () | Moderate on CPU; High on Metal |
| Time to First Token (TTFT) | Fast | Ultra-Fast | Fastest on Multi-Turn () | Fast on local hardware |
| Distributed Scaling | Tensor + Pipeline Parallel | Tensor + Pipeline + Expert Par | Tensor + Pipeline Parallel | Multi-CPU RPC / MPI |
7.2 Decision Matrix: Which Engine Should You Deploy?
8. Production Python Benchmarking Suite
The following asynchronous Python load generator measures Time to First Token (TTFT), Inter-Token Latency (ITL), and Total Request Throughput across OpenAI-compatible serving endpoints (vLLM, SGLang, TensorRT-LLM Triton, llama.cpp server):
9. Production SRE Deployment Runbooks
9.1 vLLM Production Deployment (Docker with Multi-GPU Tensor Parallelism)
9.2 SGLang Production Deployment (High-Concurrency Prefix Caching)
10. Academic Bibliography & Further Reading
- Kwon, W., Li, Z., Zhuang, S., Sheng, Y., Zheng, L., Yu, C. H., Gonzalez, J. E., Zhang, H., & Stoica, I. (2023). Efficient Memory Management for Large Language Model Serving with PagedAttention. In Proceedings of the 29th ACM Symposium on Operating Systems Principles (ACM SOSP '23). https://arxiv.org/abs/2309.06180
- Zheng, L., Yin, L., Xie, Z., Sun, C., Huang, J., Yu, C. H., Cao, S., Kozyrakis, C., Stoica, I., Gonzalez, J. E., & Sheng, Y. (2024). SGLang: Efficient Execution of Structured Language Model Programs. https://arxiv.org/abs/2312.07104
- Dao, T., Fu, D. Y., Ermon, S., Rudra, A., & Ré, C. (2022). FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness. In Advances in Neural Information Processing Systems (NeurIPS 2022). https://arxiv.org/abs/2205.14135
- Frantar, E., Ashkboos, S., Hoefler, T., & Alistarh, D. (2022). GPTQ: Accurate Post-Training Quantization for Generative Pre-trained Transformers. https://arxiv.org/abs/2210.17323
- Gerganov, G. (2023). llama.cpp: Port of Facebook's LLaMA model in C/C++. GitHub Repository. https://github.com/ggerganov/llama.cpp
- InitNode Signal Deep Dive: High-Throughput Linux I/O: io_uring & eBPF/XDP vs epoll. InitNode Signal.
References
- [1] Sep 2023Efficient Memory Management for Large Language Model Serving with PagedAttention (Woosuk Kwon et al., ACM SOSP 2023)
- [2] Jan 2024SGLang: Efficient Execution of Structured Language Model Programs (Lianmin Zheng et al., 2024)
- [3] May 2022FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (Tri Dao et al., NeurIPS 2022)
- [4] Oct 2022GPTQ: Accurate Post-Training Quantization for Generative Pre-trained Transformers (Elias Frantar et al., 2022)
- [5] Sep 2026High-Throughput Linux I/O: io_uring & eBPF/XDP vs epoll
Write for InitNode. Earn Proof of Work.
Unlike Medium or Dev.to, InitNode is built exclusively for senior software engineers, infrastructure architects, and systems builders. Every published blueprint is free of paywalls, indexed within seconds, and permanently linked to your verified engineering pedigree.
Climb the Architect Leaderboard and unlock verified reputation badges.
First-class LaTeX math, responsive sequence diagrams, and syntax highlighting.
Automated real-time submission to Google Indexing and IndexNow APIs.
Readers subscribe directly to you; automated email dispatches on release.