Building an LLM Inference Engine from Scratch: A Deep Dive into vLLM and SGLang Optimizations
Developer Eason Huang has built a custom LLM inference engine from scratch to demystify the performance secrets behind vLLM and SGLang, achieving 2,143 tokens p
When serving Large Language Models (LLMs), one of the most critical performance metrics is generation throughput measured in tokens per second. Over recent years, high-performance inference engines like vLLM and SGLang have dramatically boosted memory efficiency and execution speed. However, fully understanding the exact internal architectures and optimization techniques driving these speedups remains a challenge for many engineers. To demystify these mechanisms, developer Eason Huang built a custom LLM inference engine completely from scratch, recording every single optimization step in dedicated git commits.

Image source: https://x.com/GuyOnWifi/status/2127794447152795796
Rather than just sharing the final benchmark, this project provides a transparent, step-by-step walkthrough of how a naive implementation running at 150 tokens per second (tok/s) was scaled up to 2,143 tok/s. This throughput reaches approximately 98% of the maximum performance delivered by industry-standard open-source engines like vLLM and SGLang, all tested on consumer hardware featuring an NVIDIA RTX 3060 GPU and a Qwen3-0.6B model.
Why Build from Scratch: Understanding Inference Bottlenecks
Open-source inference engines often abstract away complex memory management strategies and custom hardware kernels. As a result, when developers inspect massive production codebases, it is easy to lose sight of how individual optimizations contribute to overall performance.
By stripping away external library black boxes and starting from the most primitive text generation loop, the developer tackled performance bottlenecks head-on. In the initial naive loop, every newly generated token re-ran the full attention sequence across all 28 layers. Because causal attention guarantees that past tokens' keys and values never change, caching and reusing those pre-computed tensors marked the true starting point of the optimization journey.
A Five-Step Journey Proven by Git Commits
The public GitHub repository (GuyOnWifi/scheduler) and accompanying technical write-up isolate each optimization into its own git commit, allowing developers to trace the exact evolutionary path of performance gains:
- Introducing FlashAttention Kernels (222 → 577 tok/s): Traditional softmax attention materializes full $n \times n$ matrices that require costly round-trips to High Bandwidth Memory (HBM). By adopting FlashAttention, which avoids building the full attention matrix in memory, throughput received an immediate boost.
- Implementing Paged KV Cache (577 → 1,315 tok/s): This represents the breakthrough in memory management. Traditional designs pre-allocated fixed 2048-token slots per request, causing massive internal fragmentation. Paged KV allocates memory dynamically in 16-token pages as requests grow, allowing significantly more concurrent requests to run safely.
- Continuous Batching Scheduler: Static batching forces requests to wait for the slowest item in the batch. A continuous batching scheduler swaps in a brand-new request the exact moment any active request finishes, sharply reducing time-to-first-token latency.
- CUDA Graphs and torch.compile (1,451 → 2,143 tok/s): Once GPU execution speeds up significantly, CPU overhead becomes the primary bottleneck. CUDA graphs replay an entire decode step as a single launch, while PyTorch compilation optimizations reduce per-token decode latency down to milliseconds.
Implications and Takeaways for Engineers
This project offers an exceptional roadmap for system engineers looking to cut through the complexity of modern LLM infrastructure. Instead of getting lost inside hundreds of thousands of lines of enterprise codebase, building a minimal engine from the ground up provides a concrete mental model of how inference memory and execution schedulers interact.
While these implementations and benchmarks were verified on a local RTX 3060 running a compact model, deploying similar patterns to multi-node production clusters involves distinct scaling challenges. Even so, systems developers eager to understand the inner machinery of modern LLM inference will find the commit history and detailed explanations to be an invaluable weekend study guide.
Sources
- Eason Huang Technical Blog: Building an LLM Inference Engine from Scratch
- GitHub Repository: GuyOnWifi/scheduler
- Original X Announcement: @GuyOnWifi's X Post