Foundations to Frontier
Tests
F. NLP and Large Language Models › F8 Inference and Serving

Serving frameworks: vLLM, TGI, TensorRT-LLM, llama.cpp, SGLang

Serving frameworks package the techniques of F8-2 to F8-5 (paged KV caches, continuous batching, quantised kernels, speculative decoding, prefix caching, tensor parallelism) behind an OpenAI-compatible API. vLLM is the general-purpose default. Hugging Face TGI integrates with the Hub. NVIDIA TensorRT-LLM compiles optimised engines for peak NVIDIA performance. SGLang adds RadixAttention prefix caching and fast structured generation. llama.cpp (with Ollama on top) runs GGUF models on CPUs, Apple silicon and consumer GPUs. Which to use, and on what hardware, follows from a roofline capacity estimate: weights plus KV cache must fit, decoding is memory-bound until large batches, and throughput sets cost. This page compares the frameworks and walks through capacity planning. In the exercises you check whether weights fit and estimate decode speed. You then plan ten deployments of 8B and 70B models. A 16-bit 70B does not fit on one 80 GB GPU. On two H100s it leaves room for only 48 sequences (about 1240 tokens/s, $1.35 per million tokens), while FP8 raises this to about 9200 tokens/s at $0.18. A 4-bit 70B runs on a 128 GB laptop at about 6 tokens/s.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Picking the engine and the hardware

Running a language model for real users needs a serving engine: software that loads the model, packs requests together efficiently and answers through a web API. Several popular engines exist, from datacentre tools tuned for big NVIDIA GPUs to llama.cpp, which runs models on a laptop. The right choice depends on where it runs and how many users it serves. A simple calculation of memory and speed tells you whether a model fits and how fast and costly it will be.

LLM lab
7B-class model (32 layers, 32 query heads of 128 dims, fp16) at 8192 tokens: full multi-head KV cache 4.00 GiB; with 8 KV heads (grouped-query attention) 1.00 GiB per sequence.
Why engineers care

Before buying hardware or choosing a deployment, a back-of-the-envelope memory and bandwidth calculation predicts whether a model fits, how fast it will answer and what each answer will cost.

📝 Quick check (Level 0)
0/3 answered
  1. 1. llama.cpp is designed mainly for…
  2. 2. Serving engines typically expose…
  3. 3. Whether a model fits depends on…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • vLLM: PagedAttention, continuous batching, prefix caching, many quantisation formats (GPTQ, AWQ, FP8), speculative decoding, tensor/pipeline parallelism; broad hardware support.
  • TGI (Text Generation Inference): Hugging Face’s server; continuous batching, Hub integration, multiple backends.
  • TensorRT-LLM: NVIDIA’s compiled engines with fused kernels, in-flight batching, FP8/FP4; highest performance on NVIDIA GPUs, more build complexity (served via Triton or trtllm-serve).
  • SGLang: RadixAttention (automatic prefix reuse across requests in a radix tree), fast constrained decoding, a frontend language for multi-call programs.
  • llama.cpp / Ollama: C/C++ inference on CPUs, Metal and CUDA with GGUF quantisation; single-user and edge use.
  • Capacity planning (roofline): weights + KV ≤ memory; single-stream speed ≈ bandwidth / bytes read per token; throughput grows with batch until compute-bound (batch ≈ FLOPs / bandwidth, about 300 on an H100 in 16-bit).
📝 Quick check (Level 1)
0/3 answered
  1. 1. RadixAttention belongs to…
  2. 2. Highest NVIDIA-specific performance usually comes from…
  3. 3. Single-stream decode speed is about…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Deploying a model

Exercise 1.

  • Compute weight memory.
  • Check whether a model fits on n GPUs.
  • Estimate memory-bound decode speed.

Practice.

  • Start with vLLM or SGLang for GPU serving; switch to TensorRT-LLM when squeezing NVIDIA performance matters more than flexibility.
  • Use Ollama or llama.cpp for local development, laptops and air-gapped edge devices.
  • Benchmark your own traffic (prompt and output lengths, concurrency); published numbers rarely transfer.
  • Plan for the KV cache at your context lengths, not just the weights.
# GPU server (OpenAI-compatible)
# vllm serve meta-llama/Llama-3.1-70B-Instruct --tensor-parallel-size 2 --quantization fp8
# python -m sglang.launch_server --model-path Qwen/Qwen2.5-7B-Instruct --port 30000
# Local
# ollama run llama3.1:8b
from openai import OpenAI
client = OpenAI(base_url="http://localhost:8000/v1", api_key="none")
r = client.chat.completions.create(model="meta-llama/Llama-3.1-70B-Instruct",
                                   messages=[dict(role="user", content="Hello")])
🐍 Exercise 1: does it fit, and how fast?
Implement weight memory, the fit check with tensor parallelism and the memory-bound decode speed.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Tool for running models locally with one command:
  2. 2. vLLM flag to split a model over 2 GPUs:
  3. 3. Capacity plans must include…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Roofline for decoding. One decoding step for a batch of B sequences reads the weights once (W bytes) and each sequence’s cache (K bytes each), and does about 2·P·B FLOPs. Its time is max((W + BK)/bandwidth, 2PB/FLOPs). At B = 1 the memory term dominates by a factor of about FLOPs/bandwidth (about 300 for an H100 in 16-bit), so single-stream speed is set by bandwidth and bytes per weight: halving the bits nearly doubles the speed.

Batch is limited by memory. The largest batch is (memory − weights − overhead)/K. A model that barely fits leaves room for few sequences, so its throughput and cost per token are poor even on expensive hardware. Quantising weights (and the cache) frees memory for the batch, which is why FP8 multiplied the 70B throughput.

What the frameworks add. They do not change the roofline; they approach it, through efficient kernels, paging (full memory use), continuous batching (full slots) and prefix caching (skipping repeated prefill).

📝 Quick check (Level 3)
0/3 answered
  1. 1. At batch 1, decoding is limited by…
  2. 2. A model that barely fits has…
  3. 3. Frameworks mainly help by…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Which hardware and precision make an 8B and a 70B model fit, fast and cheap?

History.

  • vLLM (2023), TGI (2023), TensorRT-LLM (2023), SGLang (2024), llama.cpp (2023) and Ollama (2023).
  • S. Williams, A. Waterman and D. Patterson, “Roofline” (2009).

Exercise 2. A roofline capacity planner, not a benchmark.

  • Hardware (memory, bandwidth, dense 16-bit TFLOPS, illustrative price): H100 80 GB (3.35 TB/s, 989, $3/h); A100 80 GB (2.0 TB/s, 312, $1.8/h); RTX 4090 24 GB (1.0 TB/s, 165, $0.5/h); a 128 GB unified-memory laptop (0.4 TB/s, about 30).
  • Models: an 8B and a 70B Llama-like model, 8 KV heads, 1000-token contexts.
  • Assumptions: 60% of peak bandwidth and compute; batch capped at 256; FP8 doubles H100 FLOPs.
deployment fits max batch single stream (tok/s) throughput (tok/s) $ per M tokens
8B bf16, 1×H100 yes 256 125 10 384 0.080
8B FP8, 1×H100 yes 256 249 20 767 0.040
8B bf16, 1×A100 yes 256 74 6 199 0.081
8B int4, RTX 4090 yes 120 137 3 604 0.039
8B int4, laptop yes — 52 — local
70B bf16, 1×H100 no — — — —
70B bf16, 2×H100 yes 48 29 1 239 1.345
70B FP8, 2×H100 yes 256 57 9 193 0.181
70B int4, 1×H100 yes 118 54 3 127 0.267
70B int4, laptop yes — 6 — local
  • Fitting is not enough: 70B in 16-bit on two H100s fits but leaves room for only 48 sequences, so it costs 7× more per token than FP8 on the same GPUs.
  • Precision sets single-stream speed: FP8 doubled the 8B’s tokens per second, because decoding reads half the bytes.
  • Consumer and local hardware is viable for small models: a 4-bit 8B on an RTX 4090 had the lowest cost per token here; a 4-bit 70B runs on a laptop, but at about 6 tokens/s for one user.
  • Caveats: prices and efficiencies are illustrative; real throughput also depends on prompt lengths, prefill, framework and kernels. Benchmark before committing.
🐍 Exercise 2 (research): which hardware and precision make an 8B and a 70B model fit, fast and cheap?
Build a roofline capacity planner and compare deployments of 8B and 70B models across GPUs, precisions and a laptop.

Serving in AI, robotics and aerospace

  • Enterprise assistants: vLLM or TGI behind an internal OpenAI-compatible endpoint keeps data on premises.
  • Edge and field use: llama.cpp on laptops or embedded boards gives offline assistants for field engineers.
  • Mission systems: capacity planning decides whether onboard or ground hardware can run a model within power and latency budgets.

References

  1. W. Kwon et al. (2023). “Efficient memory management for large language model serving with PagedAttention.” SOSP (vLLM).
  2. L. Zheng et al. (2024). “SGLang: efficient execution of structured language model programs.” NeurIPS.
  3. NVIDIA (2023–). “TensorRT-LLM documentation.”
  4. S. Williams, A. Waterman and D. Patterson (2009). “Roofline: an insightful visual performance model for multicore architectures.” Communications of the ACM.