Leanpub Header

Skip to main content

Inside DeepInfer

How an LLM Inference Engine Works

Inside DeepInfer
This book is 80% completeLast updated on 2026-10-24

A generation loop fits in a few lines of Python, yet serving it to many users at once raises hard questions: who owns the cached state, when memory can be reused, and when a guessed token is worth checking. This book answers them through small experiments you can run on your laptop and the source code of DeepInfer, the open-source engine behind DeepSeek's production service.

Minimum price

$0.99

$20.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
About

About

About the Book

A generation loop fits in a few lines of Python: ask the model for scores, choose a token, append it, repeat. Serving that loop to many users at once is a different problem. Which requests should share a pass over the model's weights? Who owns their cached state, and when can its memory be reused? How can the CPU prepare the next step before the GPU has finished the last one? When are guessed tokens worth checking, and when is approximate state good enough?

Inside DeepInfer answers these questions by reading DeepInfer, the open-source inference engine that DeepSeek describes as the core of its large-scale production service. Each chapter begins with a small experiment whose result you can predict, then follows the same mechanism through DeepInfer's Python and C++ source, printed alongside the explanation.

Along the way you will follow a request through:

- batching costs, attention and the key/value cache

- block allocation, state ownership and preemption

- overlapping CPU and GPU work

- mixture-of-experts dispatch and expert load balancing

- speculative decoding and how much verification work to spend

- approximate reconstruction of cached state

- correctness guarantees: batch invariance, deterministic sampling and structured output

- the service that routes requests between prefill and decode machines and shared cache storage

Two appendices cover compression and indexing, and map the repository, so you can keep reading the code on your own.

You don't need a GPU cluster. The book's 28 experiments run on an ordinary computer with Python, and all but one use only the standard library. They are available at https://github.com/jzstark/inside-deepinfer-code. You should be comfortable reading Python; elementary probability helps with the chapters on sampling.

Throughout, the book separates what the released code shows, what DeepSeek's documentation describes and what a teaching experiment only models. It is pinned to a fixed DeepInfer commit, and its source checker tells you when your checkout differs from the edition the book describes.

This is an independent book. It is not affiliated with, sponsored by or endorsed by DeepSeek.

Author

About the Author

Jianxin Zhao

Jianxin Zhao is a computer scientist and open-source developer whose work spans numerical computing, distributed machine learning, and AI inference optimization. He holds a PhD in Computer Science from the University of Cambridge and has conducted postdoctoral research at the Beijing Institute of Technology and the Karlsruhe Institute of Technology.

A core developer of the Owl scientific computing and deep learning framework since 2017 and its project lead since 2020, he has contributed to numerical operators, computation graphs, automatic differentiation, and memory optimization. His recent work focuses on multimodal model inference, performance analysis, and reproducible benchmarking. He has co-authored books on scientific computing, numerical systems architecture, enterprise analytics, and autonomous driving. His writing connects research principles with practical engineering, helping readers understand how AI systems work and how to improve them.

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub