@wlzh: 227 个 star、6 小时视频、85 页手写笔记,有人从零手搓了一个 LLM 推理引擎 这个项目叫 samir-vllm,用 C++ 加 CUDA 写成,配套 12 集视频和 85 页手写笔记,目标是把推理这件事从第一性原理讲清楚 它…

X AI KOLs Timeline 工具

摘要

有人从零用 C++ 和 CUDA 手搓了一个名为 samir-vllm 的 LLM 推理引擎,配套 12 集视频(6 小时)和 85 页手写笔记,从加载 safetensors 权重到分页 KV 缓存、连续批处理逐步讲解 Llama 3.2 1B 的推理全流程,代码以 MIT 协议开源。

227 个 star、6 小时视频、85 页手写笔记,有人从零手搓了一个 LLM 推理引擎 这个项目叫 samir-vllm,用 C++ 加 CUDA 写成,配套 12 集视频和 85 页手写笔记,目标是把推理这件事从第一性原理讲清楚 它不讲概念,直接动手,跑的是 Llama 3.2 1B,16 层、2048 上下文全程自己实现 从加载 safetensors 权重、写 embedding 和 RMSNorm 的 CUDA 核函数,到 RoPE、分组查询注意力、SwiGLU、分页 KV 缓存和连续批处理,一步步全讲透 代码 MIT 开源,仓库里给了完整构建命令,照着 README 就能编译跑起来 想搞懂推理引擎内部到底在干什么的,这份材料比只看文档直接得多
查看原文
查看缓存全文

缓存时间: 2026/11/21 08:17

227 个 star、6 小时视频、85 页手写笔记,有人从零手搓了一个 LLM 推理引擎

这个项目叫 samir-vllm,用 C++ 加 CUDA 写成,配套 12 集视频和 85 页手写笔记,目标是把推理这件事从第一性原理讲清楚

它不讲概念,直接动手,跑的是 Llama 3.2 1B,16 层、2048 上下文全程自己实现

从加载 safetensors 权重、写 embedding 和 RMSNorm 的 CUDA 核函数,到 RoPE、分组查询注意力、SwiGLU、分页 KV 缓存和连续批处理,一步步全讲透

代码 MIT 开源,仓库里给了完整构建命令,照着 README 就能编译跑起来

想搞懂推理引擎内部到底在干什么的,这份材料比只看文档直接得多


samir-sarwar/samir-vllm

Source: https://github.com/samir-sarwar/samir-vllm

samir-vllm

Teaching LLM inference from first principles by building a small engine in C++ and CUDA.

The series follows what happens between a prompt and a generated token, from loading model weights to running each part of the transformer on the GPU.

My inference notes

Read Inference from Scratch (PDF) for 80+ pages of my handwritten notes and diagrams. I developed all of the writing in this PDF myself, with NO AI writing.

Build series | 6+ Hours of footage

Part 0: AI inference explained
Part 0 · AI inference explained
Part 1: Loading SafeTensor weights
Part 1 · Loading model weights
Part 2: Embedding gather kernel
Part 2 · Embedding gather
Part 3: RMSNorm kernel
Part 3 · RMSNorm
Part 4: Rotary position embeddings
Part 4 · RoPE
Part 5: Q, K, and V projections with cuBLAS
Part 5 · Q/K/V with cuBLAS
Part 6: Multi-head self-attention
Part 6 · Self-attention
Part 7: Feed-forward network and SwiGLU
Part 7 · Feed-forward network
Part 8: Decode and KV cache
Part 8 · Decode and KV cache
Part 9: Static and continuous batching
Part 9 · Static and continuous batching
Part 10: Paged KV cache
Part 10 · Paged KV cache
Finale: Paged attention
Finale · Paged attention

Companion notes for every video are in the study-notes index, with separate Markdown and PDF folders. You can also read the complete PDF collection.

What works so far

  • A C++17 / CUDA project, built with CMake and linked with ICU for Unicode-aware tokenization.
  • Direct model.safetensors loading: parse metadata, validate tensor offsets, and copy the raw BF16 weights to GPU memory. There is also a memory-mapped loader implementation to avoid an extra CPU-side copy.
  • A fixed Llama 3.2 1B Instruct weight layout, with direct pointers to the embedding, normalization, attention, and MLP tensors across all 16 layers.
  • A local BPE tokenizer that supports Unicode, special tokens, encode/decode, and the Instruct chat prompt format; its expected output is covered by a dedicated test executable.
  • A CUDA embedding-gather kernel and prefill path that copies prompt token IDs to the GPU and allocates activations.
  • A CUDA RMSNorm kernel for the 2,048-wide hidden state: it reduces in FP32 and applies the learned BF16 weights throughout the forward pass.
  • Dense causal prefill now runs all 16 layers: Q/K/V projection, RoPE, grouped-query attention, output projection, the attention residual, post-attention RMSNorm, SwiGLU MLP, and the MLP residual.
  • The final RMSNorm and tied embedding/LM-head projection produce FP32 logits; GPU argmax selects each next token.
  • A shared BF16 KV page pool with a block table per sequence and layer. Each physical page holds 16 tokens of K and V; prefill fills prompt pages and decode appends one new token at its absolute RoPE position.
  • Greedy response generation with Instruct chat formatting, end-of-turn/end-of-text stopping, and configurable output length within the 2,048-token context.
  • Persistent scratch buffers and one cuBLAS handle shared by prefill and decode. Q storage is reused for output/MLP projections, and normalization storage is reused for attention output.
  • Batched decode with runtime-sized buffers, packed projections, and separate KV page mappings and attention lengths per sequence. Request count does not enlarge the KV pool.
  • Static batching and a continuous scheduler that admit queued prompts according to available page budgets, then return their pages and budgets on completion.
  • Direct paged decode attention with FP32 online softmax. Each query head streams its sequence’s cached pages and accumulates weighted values without gathering a contiguous prefix or storing attention scores.

Part 8 explains Prefill, Decode, and the KV Cache. Part 9 follows with Static and Continuous Batching, including the current paged cache and online-softmax decode attention.

Current scope

The Llama 3.2 1B inference path includes dense causal prefill, paged decode attention, and static or continuous batching. Model dimensions and the 2,048-token context remain fixed for this learning engine. The KV pool defaults to 128 MiB and can be set with --kv-cache-mib N; active request capacity follows the workload’s page budgets rather than a fixed two-request limit.

Admission guarantees room for each request’s maximum processed prefix: prompt_tokens + allowed_new_tokens - 1, rounded up to 16-token pages in each of the 16 layers. The allowed output budget is clamped to the context limit, and the final predicted token is returned without being processed again. These admission credits are separate from physical pages, which are assigned lazily. Early completion releases both the assigned pages and the remaining credits.

Requests wait in FIFO order when their budgets cannot fit alongside active requests. Active requests have room to finish, so future page-boundary growth cannot exhaust the pool or deadlock generation. A request whose budget exceeds the entire pool is rejected with a clear error. This conservative policy avoids preemption and recomputation; a large requested output budget can limit concurrency even if the model eventually stops early.

--stats reports the pool size, peak active requests, largest decode batch, peak assigned pages, admission waits, and final free-page count. Prefill still processes one prompt at a time between decode iterations.

BF16 computations can round differently at different batch sizes. When vocabulary logits are nearly tied, greedy output tokens can therefore differ between individual and batched runs. Bitwise output equality across batch sizes is not guaranteed.

Build

Requirements

  • A CUDA-capable NVIDIA GPU and CUDA Toolkit
  • CMake 3.24+
  • A C++17 compiler
  • ICU development libraries
  • The nlohmann/json header
  • Model weights at models/llama-3.2-1b-instruct/model.safetensors
  • The matching tokenizer at models/llama-3.2-1b-instruct/tokenizer.model (copy it from models/Llama-3.2-1B-Instruct/original/tokenizer.model)
mkdir -p models/llama-3.2-1b-instruct
cp models/Llama-3.2-1B-Instruct/original/tokenizer.model models/llama-3.2-1b-instruct/
cmake -S . -B build
cmake --build build -j
./build/samir-vllm "Explain the KV cache in two sentences." 128
# Continuous batching (default): admit requests whose page budgets fit.
./build/samir-vllm "Say hello." 64 "Explain the KV cache." "What is RoPE?"
# Static batching: wait for the current group before admitting more.
./build/samir-vllm --static "Say hello." 64 "Explain the KV cache." "What is RoPE?"
# More than two requests share the same default 128 MiB pool.
./build/samir-vllm --stats "Explain paging." 64 "Explain GQA." "Explain RoPE." "Explain softmax."
# A smaller pool queues requests when their declared budgets cannot fit together.
./build/samir-vllm --kv-cache-mib 8 --stats "Explain paging." 64 "Explain GQA." "Explain RoPE."
./build/tokenizer-test models/llama-3.2-1b-instruct/tokenizer.model

After optional --static, --continuous, --kv-cache-mib N, and --stats flags, the first argument is a prompt and the second is max_new_tokens (default: 128). Any remaining arguments are additional prompts; the token budget applies to each request. Every prompt is formatted as an Instruct user message. Generation stops independently at EOS, EOT, or EOM, or the output/context limit, and each completed response is printed with its token IDs and request number.

Prompts are prefilled one at a time, pausing decode. Each decode step packs one token from every active sequence into the shared buffers. Attention reads only that sequence’s cache; requests can have different prompt and output lengths. Continuous batching admits waiting requests when page credits become available, while static batching admits the next group after the current group finishes.

Project layout

src/           Engine entry point, CUDA kernels, and tokenizer implementation
include/       Public kernel and tokenizer headers
tests/         Tokenizer tests
python/        Tokenizer reference script
proper_notes/  Study-notes index, markdown/ sources, and pdf/ reading editions
scripts/       Rebuild the study-note PDFs
notes.md       Working notes from the build

License

The engine source code in src/, include/, tests/, and python/, along with CMakeLists.txt, .gitignore, and this README, is licensed under the MIT License.

The notes in proper_notes/, notes.md, and podLibraryInstall.md, along with handwritten notes, scans, illustrations, photographs of notes, and PDFs provided with this project, are all rights reserved and excluded from MIT. Personal reading and study are permitted. Redistribution, republication, rehosting, resale, or distribution of adapted versions requires prior written permission, whether offered for free or for payment. See LICENSE-NOTES for the full terms and exceptions.

Public repositories can still be viewed and forked within GitHub under GitHub’s Terms of Service.

Model assets and dependencies

Built with Llama. The bundled Llama 3.2 tokenizer and any separately downloaded Llama 3.2 weights retain Meta’s Llama 3.2 Community License and Acceptable Use Policy. They are excluded from MIT. Keep the agreement, policy, and attribution notice with any redistributed Llama materials.

Third-party dependencies, including CUDA, cuBLAS, ICU, and nlohmann/json, retain their respective licenses.

相似文章

@wangxiaoxin88: 想把LLM推理啃透,这套东西真该盯死了看。 有老哥整了一份LLM工程师实战手册time-to-first-token,每天抽半小时,磨10周,一共50堂课,从零搭起一套能上生产的完整技术栈: 先立认知模型,搞懂Decode为啥撑爆显存、P…

X AI KOLs Timeline

一份名为 time-to-first-token 的开源 LLM 工程师实战手册,以 10 周、50 堂课的路线图,从零构建可上生产的 OpenAI 兼容推理服务,涵盖 vLLM 源码、PagedAttention、监控、1000+ 并发压测与量化优化。

@yibie: 推荐这个项目,一个人用纯 C 写了一个推理引擎,让 7440 亿参数的 GLM-5.2 在 25GB 内存的消费级机器上跑起来。没有 GPU,没有 BLAS,没有 Python 运行时——约 1300 行 C 代码。核心洞察很简单:MoE…

X AI KOLs Timeline

Colibri 是一个纯 C 编写的推理引擎,约 1300 行代码,零依赖,能在 25GB 内存的消费级机器上运行 7440 亿参数的 GLM-5.2 MoE 模型,通过流式加载路由专家和高效缓存实现,无需 GPU 或 Python 运行时。

@Rick5767: 据我所知。已经有好几个人在脱离vllm和llama cpp。去从零开始写amd显卡专用的推理引擎。 他们的意思大概是,当他们看到通用引擎只占用了600g显存带宽的200g的时候,天啊,这是在干嘛。 AI编程的普及,大把从前不能做的事情将疯…

X AI KOLs Timeline

作者指出已有开发者脱离vLLM和llama.cpp,从零编写针对AMD显卡的专用推理引擎,原因是通用引擎仅利用了约200GB/600GB的显存带宽。随着AI辅助编程的普及,类似项目将大量涌现,软件开发进入更平等的时代。

@CycleDecoded: 别再用原生 HuggingFace硬扛本地大模型推理了,显存直接爆满,吞吐量慢得像乌龟爬! 伯克利实验室出品的 vLLM,用搞操作系统内存分页(PagedAttention)的那套黑科技,直接把 GPU 显存压榨到了极限,KV Cache…

X AI KOLs Timeline

伯克利 Sky Computing Lab 开源的 vLLM 是一个高性能 LLM 推理与服务库,通过 PagedAttention 和连续批处理大幅提升吞吐量、降低显存浪费,并兼容 OpenAI API 和多种硬件。