从头理解并用代码实现 LLM 中的 KV 缓存
Understanding and Coding the KV Cache in LLMs from Scratch
Sebastian Raschka 于 2025 年 6 月 17 日发布的技术教程,讲解 LLM 推理中 KV 缓存的工作原理,并给出一个从零实现、便于阅读的代码示例。文中说明 KV 缓存存储推理阶段可复用的 key 与 value 中间计算结果,可显著加快文本生成;代价是增加代码复杂度与内存需求,且不能用于训练。作者提到该主题未包含在其《Building a Large Language Model From Scratch》一书中。