Project P10
Own LLM inference engine
What makes an LLM server fast, and how much of that speed can we reproduce ourselves on an 8 GB GPU while preserving the output?
- stages
- 9
- duration
- 16 h
- concepts
- 23
- available in English
- 9 / 9
- completed by you
0 of 9
Where it fits
Leads to
This completes the path.
Overview
Build an inference engine for the GPT you developed and for small open Llama-style models. Start with a simple generation loop, measurement harness, and roofline analysis. Add a KV cache, CUDA Graphs, paged KV storage and its kernel, continuous batching with chunked prefill, speculative decoding with statistical verification of distribution preservation, weight-only quantization with a Triton kernel, and an attention kernel for decoding. Finish with a chat API-compatible HTTP server that supports streaming and constrained outputs for the agent in Project 5.
Why it matters
Inference accounts for most of the compute spent on production LLMs and is where engineering makes a visible difference. Building the engine turns memory bandwidth, GPU kernels, request scheduling, and the exact guarantees of speculative decoding into concrete engineering problems. These tools make it possible to evaluate, debug, and extend real servers.
Stages 9
- 01Baseline, metrics and rooflineLoad an open Llama-architecture model into a custom implementation with logit parity, build a naive generation and metrics harness, and analyze prefill and decode through a roofline model.
- 02The KV cacheImplement a preallocated KV cache per layer with separate prefill and decoding paths, correct RoPE positions and GQA support; verify token-by-token greedy-generation equality with and without caching, and measure speedup and memory.
- 03Launch overhead and CUDA GraphsMeasure decode launch overhead, compile the step and capture CUDA Graphs using fixed shapes and preallocated cache, then compare throughput with a bandwidth reference.
- 04Paged KV cache and paged attentionReorganize KV cache into fixed-size blocks with per-sequence tables, prefix sharing and copy-on-write, then implement attention through the table using PyTorch gather and a Triton kernel.
- 05Continuous batching and schedulingBuild an iteration-level scheduler that admits and completes requests, chunks prefill and preempts with recomputation, then compare latency and throughput under Poisson load with static batching.
- 06Speculative decoding with a distribution guaranteeImplement speculative sampling with small draft and target models sharing a tokenizer, verify distribution preservation statistically, and compare measured speedup with acceptance-based predictions.
- 07Weight-only quantization and its kernelImplement grouped INT8 and INT4 quantization, activation-based GPTQ-style calibration and a Triton matrix-vector product that dequantizes on the fly; measure quality and speed.
- 08Decoding attention kernel for long contextImplement a Triton decode-attention kernel that splits the sequence into parallel chunks with online softmax and final reduction, supports paged cache and GQA, and compare it with PyTorch attention.
- 09Server, constrained outputs and final benchmarkWrap the engine in a chat-compatible HTTP server with streaming, cancellation and automaton-constrained decoding, then benchmark latency, throughput, memory and agent performance against references.
Final deliverable
Run your inference engine locally
Build an engine with a paged cache, continuous batching, speculative decoding, int4 quantization, and constrained outputs for the local agent. Include a correctness test suite and a reproducible performance report.
Check what you already know
- Load the weights of a Llama model into your implementation and match the reference logits
- Use a roofline analysis to explain why batch-one decoding is limited by memory bandwidth
- Implement a KV cache and demonstrate output parity with generation without the cache
- Implement a paged KV cache with a block table and its attention kernel
- Implement speculative sampling and test statistically that it preserves the target model's distribution
- Write a Triton matrix multiplication kernel that dequantizes int4 weights on the fly