🦙 llama.cpp 图解教程llama.cpp Visual Guide 第九部分 · 速查Part 9 · Quick reference 40 / 40
第九部分 · 速查Part 9 · Quick reference

术语表·索引Glossary & index

到这里,全书 40 课讲完了。从"llama.cpp 是什么"出发,你一路走过了 ggml 的张量与计算图、llama 的模型加载与推理主循环、公共 API 与命令行工具、底层 CPU/CUDA 内核,再到投机解码、MoE、多模态、状态空间模型这些进阶机制,最后学会了怎么把一个 HuggingFace 模型转成 GGUF、怎么给这个项目提一个站得住的 PR。这套推理引擎,你已经从上到下走了一遍。

这最后一课不教任何新东西,而是给你一张速查地图:先用一条"学习路径"把九个部分怎么层层递进看清楚,再用一张概念依赖图把核心概念之间"谁建立在谁之上"画出来,最后给一份分类术语表——每个术语一句话定义、标好它在源码里的位置、再附上"点一下跳回讲它的那一课"的链接。往后你忘了某个概念,不必从头翻,来这里一查、一跳就回到现场。

怎么用它:当你在读代码或调试时撞见一个似曾相识的名词(ggml_cgraph?llama_batch?),先在术语表里扫一眼"它大概是什么、在哪定义",需要细看就顺着链接回到对应课。这一课本身很短,价值全在"查得快、跳得准"。 换句话说,前面的课教你"怎么从零理解",这一课帮你"以后怎么快速回忆"。

🌍 宏观理解
把这一课当成全书的索引页。前面三十九课是"线性"的——一课接一课地讲;但你脑子里的知识其实是一张llama_decode 牵着 KV cache,KV cache 活在 llama_context 里,context 靠 ggml_backend 执行 ggml_cgraph,而图又是由 ggml_tensor 连起来的……这些概念彼此咬合,缺一块就转不动。线性的课程没法同时把这张网摊开给你看,于是有了这一课:用一张依赖图把"谁撑着谁"画清楚,再用分类术语表把散落在四十课里的名词收拢成可一眼检索的清单。读懂这张网,你对 llama.cpp 的理解就从"记得每一课讲了啥"升级成了"知道每个零件在整机里的位置"——而后者,才是真正能拿来读源码、查 bug、做贡献的地图。 顺带说一个读法:这张网没有"最重要的那一个零件",只有"谁离硬件近、谁离用户近"——越往下越通用(ggml 那几样几乎任何模型都用),越往上越具体(多模态、SSM 只在特定模型里才出现)。看清这条"通用到专门"的轴,你就知道哪些值得先吃透、哪些可以等用到时再回来查。
🔌 生活类比
前面的课像是带你逐个房间参观一栋大楼:这间是张量、那间是 KV cache、楼上是采样……每间都看仔细了,但你未必拼得出整栋楼的结构图。这一课就是大堂墙上那张楼层索引 + 平面图:平面图(概念依赖图)告诉你哪间撑着哪间、走廊怎么连;楼层索引(分类术语表)则是"想找某个房间?在第几层、门牌多少、点一下直接带你过去"。参观时你靠脚走、靠记忆;但等你要真在这栋楼里干活(改代码、修 bug),靠的就是这张图——它让你不必每次都从一楼重新找起。 也正因为它是一张"图",天然支持非线性地用:你不必从第一课重读,哪里不懂点哪里——这恰恰是参考资料和教程最大的不同。

全书地图:九个部分怎么层层递进

先看整体。这门课的九个部分不是随便排的,而是从外到内、再从内到外:先建立全局印象,再一层层钻进 ggml 和 llama 的内部,摸透了内部再回到工具、内核、进阶机制,最后落到实战与速查。每一部分都站在前一部分的肩膀上——所以哪一课要是觉得跳跃,往前回翻往往就接上了。 一个小提示:如果你是带着某个具体目的来的(比如"我只想搞懂量化"),完全可以直接跳到对应部分;但若想建立整体直觉,按顺序走一遍仍是最省力的路。

全书学习路径:九个部分层层递进,从"llama.cpp 是什么"一路通到"能改、能贡献、能速查"(每站标了对应课号)。
① 宏观全景
L01-03
这是什么、为什么
基础
② 前置基础
L04-07
推理 / 张量 / 量化
引擎
③ ggml 引擎
L08-13
图 / 算子 / GGUF
内部
④ llama 内部
L14-24
加载 / 推理主循环
外围
⑤ API 与工具
L25-30
C API / cli / server
底层
⑥ 底层内核
L31-33
CPU / CUDA / 调度
进阶
⑦ 进阶专题
L34-37
投机 / MoE / 多模态
实战
⑧ 实战贡献
L38-39
转换 / 编译 / 贡献
速查
⑨ 速查
L40
就是这一课

这张图也回答了一个常见疑问:"该按什么顺序读?"答案就是从左到右:每一部分都默认你已经消化了它左边的内容。当然,作为速查页,你也完全可以从任意一格跳进去——这正是下面那份术语表存在的意义。 这也是为什么前面每一课的开头,几乎都在用一两句话先接上前一课——课程的"层层递进",在这张地图上一眼就能看明白。

再往深一层看,这九个部分其实是三段式的:第一段(一到三部分)建立"它是什么"的整体印象,让你不至于一上来就淹没在细节里;第二段(四到七部分)一头扎进引擎内部,把张量、计算图、KV cache、采样、内核这些"机器零件"逐个拆开看清;第三段(八到九部分)再从内部走出来,落到"怎么用、怎么改、怎么查"。很多人学一个大项目卡住,往往是因为跳过第一段直接抠细节,或者抠完细节却没回到第三段去真正用它——这门课特意把这三段都给你铺齐了。

概念依赖图:谁建立在谁之上

九个部分是时间上的顺序;但概念之间还有一层逻辑上的依赖——谁是地基、谁建在谁之上。下面这张图把最核心的几个概念按依赖关系摞了起来:最底下是 ggml_tensor(一切数据的基本单位),往上每一层都建立在下一层之上,最顶上才是你最熟悉的"一次 llama_decode"。 注意"依赖"和"调用顺序"不是一回事:运行时是自上而下地发起(decode 调 context、context 调 backend),但构建理解却最好自下而上——先懂张量,才谈得上懂图、懂后端。

举个具体例子,把这张图"走"一遍:你在 llama-cli 里输入一句话,它先经 vocab 切成 token,装进一个 llama_batch,交给 llama_contextllama_decode;context 内部建起一张 ggml_cgraph(里面全是 ggml_tensor 节点),交给某个 ggml_backend 执行,算的过程中读写 KV cache;最后吐出 logits,sampler 从中挑出下一个 token。看——从输入到输出,恰好把图里每个盒子都点了一遍。这就是为什么说"读懂这张图,就读懂了一次推理的骨架"。

核心概念依赖图:竖向箭头 A -> B 读作"A 依赖 B",两侧箭头则是数据流。自下而上:张量组成计算图、后端执行图、context 持有 KV cache、batch/decode 驱动一步推理;vocab 把文本变成 token 喂进来,sampler 从 logits 里挑下一个 token。
概念依赖图:谁建立在谁之上 llama_batch -> llama_decode 一步推理:喂入一批 token、算出 logits llama_context 持有运行时状态(含 KV cache) ggml_backend 执行计算图(CPU / CUDA / ...) ggml_cgraph 计算图:算子连成的 DAG ggml_tensor 张量:数据的基本单位 越往上越接近一次推理 / 越往下越接近数据与硬件 竖向箭头 A -> B 即 A 依赖 B;两侧箭头为数据流入/流出 KV cache 缓存历史 K/V vocab token 与文本互转 sampler 从 logits 选下一个 token 地基是 ggml_tensor;越往上越接近 "一次 llama_decode"。

这张图真正的价值,是把"读源码时该往哪看"画了出来。比如你在调一个和 KV cache 有关的 bug,顺着图就知道该往 llama_context 里找(KV cache 归它持有),而不会跑去 ggml_tensor 那一层瞎转。每一根箭头,都是一条"出了问题该往哪一层追"的线索。

分类术语表:一句话查 + 跳回对应课

下面把全书最常打交道的术语按四类列出来。每条给三样东西:一句话定义(它是什么、管什么)、源码位置(想看真身去哪个文件)、跳转(点一下回到讲它的那一课)。不求全,只求"撞见时能立刻对上号"。 关于"源码位置"那一列:给的是单一最佳落脚点——不是说这个概念只在这一个文件里,而是"想从哪开始读最不会迷路"。比如 KV cache 的实现散在好几个文件,但从 llama-kv-cache.h 的类定义看起准没错。

为什么分成这四类?因为它们恰好对应你读源码时的四种"在哪一层":核心数据结构是 ggml 这台计算器的"数据与图";推理流程是 llama 把一次对话跑起来的那条主线;内核与后端是"同一张图怎么落到不同硬件上";进阶机制与工具则是标准流程之外的那些特殊玩法。撞见一个陌生符号时,先粗判它属于哪一类,往往就已经知道该去哪个目录找了。

① 核心数据结构

术语一句话定义源码位置跳转
ggml_tensor张量:ggml 里数据的基本单位,带形状/类型/数据指针,也是计算图的节点ggml/include/ggml.hL05
ggml_cgraph计算图:把一串算子按依赖连成的 DAG,先建图、再统一执行ggml/src/ggml-impl.hL09 / L10
GGUFllama.cpp 的模型文件格式:自描述的单文件,元数据 + 全部张量都在里面ggml/include/gguf.hL13
ggml_type张量的数据/量化类型(F16、Q4_K 等),决定每个权重怎么存、占多少字节ggml/include/ggml.hL06 / L12

② 推理流程

术语一句话定义源码位置跳转
llama_context一次推理会话:持有 KV cache、计算缓冲、采样状态等全部运行时状态src/llama-context.hL17
llama_batch一次 decode 喂进去的一批 token(或 embedding),附带位置和序列信息include/llama.hL18
KV cache缓存历史 token 的 K/V,让每步只算新 token;显存随序列线性增长src/llama-kv-cache.hL19
llama_vocab词表:token id 与文本互转,含特殊 token 与分词器(SentencePiece/BPE)src/llama-vocab.hL20
llama_sampler采样器:从 logits 按策略(top-k/top-p/温度等)挑出下一个 tokensrc/llama-sampler.cppL21
RoPE旋转位置编码:给 Q/K 注入位置信息的算子,相关超参写在模型元数据里ggml/include/ggml.hL15

③ 内核与后端

术语一句话定义源码位置跳转
ggml_backend后端抽象:一种执行计算图的设备实现(CPU/CUDA/Metal/Vulkan...)ggml/include/ggml-backend.hL31 / L33
算子 (op)计算图的基本运算(matmul、softmax、rope...),每个后端各实现一份ggml/include/ggml.hL11
CPU 后端参考实现,是所有后端的 ground truth;带 SIMD 和多线程优化ggml/src/ggml-cpuL31
后端调度把一张图的算子分派到合适的后端、处理跨后端的数据搬运ggml/include/ggml-backend.hL33

④ 进阶机制与工具

术语一句话定义源码位置跳转
build_moe_ffnMoE:路由器给 token 选 top-k 专家、只算选中的专家再加权合并src/llama-graph.cppL35
common_speculative投机解码:小模型猜一串、大模型一次并行验证,接受匹配前缀来提速common/speculative.hL34
mtmd多模态:clip + projector 把图像编成 embedding,和文本一起 decodetools/mtmd/mtmd.hL36
ggml_ssm_scan状态空间模型:用固定大小的递推状态替代 KV cache,显存 O(1)ggml/include/ggml.hL37
LoRA低秩适配器:不改原权重,挂一个小的低秩增量来做微调src/llama-adapter.hL24
convert_hf_to_gguf把 HF 模型转成 GGUF:认架构 -> 张量改名 -> 写盘的薄 CLI + conversion 包convert_hf_to_gguf.pyL38

怎么用这份速查 + 收个尾

用法很简单:把这一页加进收藏。当你读源码或调 bug 撞见一个名词,先来术语表扫一眼"是什么、在哪定义",需要深入就点链接回到那一课的现场;想理清几个概念的关系,就回头看那张依赖图。它不替代任何一课,只是让你不必为了一个名词把整门课重翻一遍——这正是"速查"二字的全部意义。 也别忘了浏览器自带的页内搜索(Ctrl/Cmd+F):在这一页直接搜一个英文符号名,往往比你回想"它在第几课"还快。也提醒一句:这二十来个词只是骨架,不是全部。书里还讲过不少同样重要、但更专门的概念——比如聊天模板(L22)、语法约束解码(L23)、llama-server 的连续批处理(L28)、量化工具与评测(L29/L30)。它们没进这张速查表,只是因为速查讲究"少而准";真要用到,顺着对应课的标题就能找回去。换句话说,这张表是一份最常用词的索引,而整门课才是完整的词典。 用熟了你会发现,查得越少、记得越牢——好的速查表,最终是为了让你不再需要它。

最后,收个尾。四十课走下来,你已经把 llama.cpp 从"一个能跑大模型的神奇程序",拆成了一台看得见每个零件、说得清每条数据流的机器:模型怎么加载、一个 token 怎么从 prompt 一路走到输出、KV cache 怎么省下重复计算、后端怎么把计算图算到 GPU 上、进阶架构怎么在标准 transformer 之外另辟蹊径、又怎么把这一切转换、编译、再贡献回去。读懂一个真实世界的大型 C++ 项目,从来不是靠一口气读完,而是靠一层层拆、一个个概念啃——而你,已经做到了。接下来,挑一个你真正好奇或在意的点,去读它的源码、去跑它、去改它。这门图解课到此结束,但你和 llama.cpp 的故事,才刚翻到"自己动手"那一页。

如果你问"那具体第一步做什么?"——这里有几个低门槛的入口:把 llama-cli 加上 -v 跑一遍,对着刷屏的日志,认一认前面学过的加载、建图、decode、采样各个阶段;或者挑一个最小的 test-* 用例,读懂它、再故意把它改坏,看测试怎么报错;又或者去仓库的 issue 列表里找一个标着 "good first issue" 的小问题练手。重要的从来不是第一步多大,而是真的迈出去——你手里已经有了这张地图,不会迷路。祝你玩得开心。

✅ 关键要点
  • 全书九部分层层递进:宏观 -> 前置基础 -> ggml 引擎 -> llama 推理内部 -> API 与工具 -> 底层内核 -> 进阶专题 -> 实战贡献 -> 速查。
  • 概念依赖:ggml_tensor 是地基 -> ggml_cgraph 由张量连成 -> ggml_backend 执行图 -> llama_context 持有 KV cache -> llama_batch/decode 驱动一步推理;vocab 进、sampler 出。
  • 术语表按四类组织:核心数据结构、推理流程、内核与后端、进阶机制与工具;每条给"一句话定义 + 源码位置 + 跳转课号"。
  • 用法:撞见陌生名词先来这查"是什么 + 在哪",要深入就点链接回到对应课——查得快、跳得准是这页唯一的目标。
  • 表外还有更专门的词(聊天模板 L22、语法 L23、llama-server L28、量化工具与评测 L29/L30 等)——速查只收最常用的。
💡 设计洞察
走到终点,值得回头看一眼这四十课其实在反复讲同一件事:一个庞大的系统,是怎么被拆成一层层各司其职、又彼此咬合的小零件的。ggml 把"算什么"(计算图)和"用什么算"(后端)分开;llama 把"模型是什么"(架构/权重)和"怎么跑一步"(context/batch)分开;工具层把"库能力"和"命令行体验"分开……每一处分层,都是同一种智慧:让每个零件只懂自己那摊事,靠清晰的接口和别人对接。这正是你能"一课只啃一个概念"却最终拼出整机的原因——因为系统本身就是这么设计的。把这张概念依赖图记在心里,你手里就有了一把通用的钥匙:面对任何一个陌生的大型系统,先问"它分了哪几层、每层对谁负责、接口在哪",你就总能找到看懂它的入口。这门课会结束,但这套拆解系统的眼光,会一直跟着你。 这,也许才是这门课留给你最值钱的东西——它比任何一个具体的函数名都耐用。

🧪 自测 · 想一想为什么这么设计

1. 在这一课的概念依赖图里,谁是最底层的“地基”——其它概念都建立在它之上?
  1. llama_context
  2. GGUF 文件格式
  3. ggml_tensor(张量,数据的基本单位)
  4. sampler
看答案与解析 点击展开
答案:C。依赖图自下而上:最底是 ggml_tensor(数据的基本单位),张量连成 ggml_cgraph,后端执行图,llama_context 持有 KV cache 并驱动 decode,最上面才是“一次 llama_decode”。所以张量是地基——不理解它,上面的图、后端、context 都无从谈起。这也是建议“自下而上”理解的原因:先懂张量,才谈得上懂图和后端。
2. ggml_cgraph(计算图)的真实结构体定义在哪个文件?
  1. ggml/src/ggml-impl.h(公开头 ggml.h 里只有前向声明)
  2. 就在公开头 ggml/include/ggml.h 里
  3. ggml/include/gguf.h
  4. src/llama-graph.cpp
看答案与解析 点击展开
答案:A。这是个常见的“找不到定义”的坑:公开头 ggml/include/ggml.h 里 ggml_cgraph 只是前向声明,真正的结构体定义在内部头 ggml/src/ggml-impl.h。很多 ggml 类型都这么做——对外暴露不透明指针、把字段藏进内部头,这样实现能改而不破坏 ABI。术语表的“源码位置”列就是帮你避开这种坑、直接指到该看的文件。
3. KV cache 在依赖图里“挂在”谁身上——也就是由谁持有?
  1. sampler
  2. ggml_backend
  3. llama_context(一次推理会话的运行时状态都归它)
  4. ggml_tensor
看答案与解析 点击展开
答案:C。KV cache 是 llama_context 持有的运行时状态之一(context 还持有计算缓冲、采样状态等)。所以调一个和 KV cache 有关的 bug,顺着依赖图就知道该往 llama_context 里找,而不是去底层的 ggml_tensor 或 ggml_backend 瞎转。这正是依赖图的实用价值:每根箭头都是“出了问题该往哪一层追”的线索。
💭 发散思考(没有标准答案,动手或动脑想想)
  • 这一课是全书的索引。请合上它,自己默写一遍这张“地图”:(1) 用一句话分别概括九个部分各解决什么问题(从宏观全景到速查);(2) 凭记忆画出核心概念的依赖链——从 ggml_tensor 一路到一次 llama_decode,中间经过哪些概念、谁持有 KV cache;(3) 挑三个你印象最深的术语,不看表,说出它大概在哪个文件、属于四类中的哪一类。能流畅做完这三件事,你对 llama.cpp 的整体结构就真正内化了。

Here we are - all 40 lessons done. Starting from "what is llama.cpp", you have walked through ggml's tensors and compute graphs, llama's model loading and inference loop, the public API and command-line tools, the low-level CPU/CUDA kernels, on through advanced mechanisms like speculative decoding, MoE, multimodality, and state-space models, and finally how to convert a HuggingFace model to GGUF and how to open a PR this project will accept. You have been through this inference engine top to bottom.

This last lesson teaches nothing new; it hands you a quick-reference map: first a "learning path" making clear how the nine parts build up step by step, then a concept dependency map drawing "who builds on whom" among the core concepts, and finally a categorized glossary - each term a one-line definition, marked with where it lives in the source, plus a "click to jump back to the lesson that covers it" link. Forget a concept later and you need not page through from the start; come here, look it up, jump back to the scene.

How to use it: when reading code or debugging you hit a half-familiar term (ggml_cgraph? llama_batch?), first scan the glossary for "roughly what it is, where it is defined", and follow the link back to its lesson when you need detail. This lesson itself is short; its whole value is "look up fast, jump accurately". In other words, the earlier lessons teach "how to understand from scratch"; this one helps you "recall quickly later".

🌍 Big picture
Treat this lesson as the book's index page. The previous thirty-nine lessons are "linear" - one after another; but the knowledge in your head is really a web: llama_decode pulls on the KV cache, the KV cache lives inside llama_context, the context relies on ggml_backend to execute a ggml_cgraph, and the graph in turn is wired from ggml_tensors... these concepts interlock, and missing one piece stalls the whole thing. A linear course cannot spread that web out for you all at once, so here is this lesson: a dependency map drawing "who holds up whom" clearly, and a categorized glossary gathering terms scattered across forty lessons into a list you can scan at a glance. Read this web and your grasp of llama.cpp upgrades from "remembering what each lesson said" to "knowing where each part sits in the whole machine" - and the latter is the map you actually use to read source, chase bugs, and contribute. A reading tip: this web has no "single most important part", only "who is close to the hardware and who is close to the user" - the lower you go the more universal (the ggml pieces are used by almost any model), the higher you go the more specific (multimodality and SSM appear only in particular models). See this "universal to specialized" axis and you know which to master first and which to come back and look up only when needed.
🔌 Analogy
The earlier lessons were like touring a big building room by room: this one is tensors, that one the KV cache, upstairs is sampling... you saw each room closely, but you may not be able to assemble the building's structural plan. This lesson is the floor index plus floor plan on the lobby wall: the floor plan (the concept dependency map) tells you which room holds up which and how the corridors connect; the floor index (the categorized glossary) is "looking for a room? which floor, which number, click to be taken there". On the tour you go on foot and by memory; but when you actually have to work in this building (change code, fix bugs), you rely on this map - it spares you starting over from the ground floor every time. And precisely because it is a "map", it naturally supports non-linear use: you need not reread from lesson one - click wherever you are stuck. That is exactly what sets reference material apart from a tutorial.

The whole-book map: how the nine parts build up

Start with the whole. The nine parts of this course are not in random order; they go from outside in, then inside out: first build a global picture, then drill layer by layer into ggml and llama internals, and once the internals are clear, come back out to tools, kernels, advanced mechanisms, and finally practice and quick reference. Each part stands on the shoulders of the one before - so if a lesson feels like a jump, paging back usually reconnects it. A small tip: if you came with a specific goal (say "I only want to get quantization"), feel free to jump straight to that part; but to build whole-picture intuition, going through in order is still the least-effort path.

The whole-book learning path: the nine parts build up step by step, from "what is llama.cpp" all the way to "can change, can contribute, can look up" (each station tagged with its lesson range).
(1) Overview
L01-03
what it is, and why
basics
(2) Foundations
L04-07
inference / tensors / quant
engine
(3) ggml engine
L08-13
graph / ops / GGUF
internals
(4) llama internals
L14-24
loading / inference loop
around
(5) API & tools
L25-30
C API / cli / server
low-level
(6) Kernels
L31-33
CPU / CUDA / dispatch
advanced
(7) Advanced
L34-37
spec / MoE / multimodal
practice
(8) Contributing
L38-39
convert / build / PR
reference
(9) Quick ref
L40
this very lesson

This map also answers a common question: "in what order should I read?" The answer is left to right: each part assumes you have digested what is to its left. Of course, as a quick-reference page you can also jump into any cell - which is exactly what the glossary below is for. This is also why almost every earlier lesson opens by connecting back to the previous one in a sentence or two - the course's "building up step by step" is visible at a glance on this map.

Look one level deeper and the nine parts are really in three movements: movement one (parts 1-3) builds the "what is it" big picture, so you are not drowned in detail from the start; movement two (parts 4-7) dives into the engine, taking apart the "machine parts" - tensors, compute graph, KV cache, sampling, kernels - one by one; movement three (parts 8-9) comes back out of the internals, landing on "how to use it, change it, look it up". Many people stall on a big project because they skip movement one and dig straight into detail, or finish the detail but never return to movement three to actually use it - this course deliberately lays out all three for you.

The concept dependency map: who builds on whom

The nine parts are an order in time; but among the concepts there is another layer of logical dependency - who is the foundation, who is built on whom. The map below stacks the most core concepts by dependency: at the very bottom is ggml_tensor (the basic unit of all data), each layer above builds on the one below, and only at the very top sits the "one llama_decode" you know best. Note that "dependency" and "call order" are not the same: at runtime things are initiated top-down (decode calls context, context calls the backend), but building and understanding are best done bottom-up - understand tensors before you can talk about graphs and backends.

For a concrete example, "walk" the map once: you type a sentence into llama-cli; it first goes through vocab into tokens, packed into a llama_batch, handed to llama_decode on the llama_context; inside, the context builds a ggml_cgraph (all ggml_tensor nodes), handed to some ggml_backend to execute, reading and writing the KV cache along the way; finally it emits logits and the sampler picks the next token. See - from input to output, that touched every box in the map exactly once. This is why "read this map and you have read the skeleton of one inference".

Core concept dependency map: a vertical arrow A -> B reads "A depends on B", while the side arrows are data flow. Bottom-up: tensors compose the compute graph, the backend executes the graph, the context holds the KV cache, batch/decode drives one inference step; vocab turns text into tokens to feed in, the sampler picks the next token from logits.
Concept dependency map: who builds on whom llama_batch -> llama_decode one step: feed a batch of tokens, get logits llama_context holds runtime state (incl. KV cache) ggml_backend executes the graph (CPU / CUDA / ...) ggml_cgraph compute graph: a DAG of ops ggml_tensor tensor: the basic data unit up = closer to one inference step / down = closer to data and hardware vertical arrow A -> B: A depends on B; side arrows are data flow KV cache caches past K/V vocab tokens to/from text sampler pick next token from logits the foundation is ggml_tensor; the higher you go, the closer to "one llama_decode".

The real value of this map is that it draws "where to look when reading source". Say you are debugging something to do with the KV cache: follow the map and you know to look inside llama_context (which holds the KV cache), rather than wandering off into the ggml_tensor layer. Every arrow is a clue for "which layer to chase when something goes wrong".

Categorized glossary: look up in one line, jump back to the lesson

Below are the terms you deal with most across the book, in four groups. Each entry gives three things: a one-line definition (what it is, what it handles), a source location (which file to read for the real thing), and a jump (click to return to the lesson that covers it). Not exhaustive - just enough to "recognize it the moment you bump into it". About the "source location" column: it gives a single best entry point - not that the concept lives in only that one file, but "where to start reading so you least get lost". The KV cache implementation, for instance, spreads across several files, but starting from the class definition in llama-kv-cache.h is never wrong.

Why these four groups? Because they line up with the four "which layer am I in" you face when reading source: core data structures are the "data and graph" of the ggml calculator; the inference flow is llama's main line that runs one conversation; kernels and backends is "how the same graph lands on different hardware"; and advanced mechanisms and tools are the special plays beyond the standard flow. When you hit an unfamiliar symbol, roughly judging which group it belongs to often already tells you which directory to look in.

(1) Core data structures

termone-line definitionsource locationjump
ggml_tensortensor: ggml's basic unit of data, with shape/type/data pointer; also a node of the compute graphggml/include/ggml.hL05
ggml_cgraphcompute graph: a DAG wiring ops by dependency; build the graph first, then execute it as a wholeggml/src/ggml-impl.hL09 / L10
GGUFllama.cpp's model file format: a self-describing single file holding both metadata and all tensorsggml/include/gguf.hL13
ggml_typea tensor's data/quant type (F16, Q4_K, ...), deciding how each weight is stored and how many bytesggml/include/ggml.hL06 / L12

(2) The inference flow

termone-line definitionsource locationjump
llama_contextone inference session: holds the KV cache, compute buffers, sampling state - all runtime statesrc/llama-context.hL17
llama_batchthe batch of tokens (or embeddings) fed into one decode, with position and sequence infoinclude/llama.hL18
KV cachecaches past tokens' K/V so each step computes only the new token; memory grows linearly with lengthsrc/llama-kv-cache.hL19
llama_vocabvocabulary: token ids to/from text, including special tokens and the tokenizer (SentencePiece/BPE)src/llama-vocab.hL20
llama_samplersampler: picks the next token from logits by a strategy (top-k/top-p/temperature, ...)src/llama-sampler.cppL21
RoPErotary position encoding: an op injecting position into Q/K; its hyper-params live in model metadataggml/include/ggml.hL15

(3) Kernels and backends

termone-line definitionsource locationjump
ggml_backendbackend abstraction: a device implementation that executes the compute graph (CPU/CUDA/Metal/Vulkan...)ggml/include/ggml-backend.hL31 / L33
op (operator)a basic graph operation (matmul, softmax, rope...); each backend implements its own versionggml/include/ggml.hL11
CPU backendthe reference implementation, ground truth for all backends; with SIMD and multithreadingggml/src/ggml-cpuL31
backend dispatchassigns a graph's ops to suitable backends and handles cross-backend data movementggml/include/ggml-backend.hL33

(4) Advanced mechanisms and tools

termone-line definitionsource locationjump
build_moe_ffnMoE: the router picks top-k experts per token, computes only the chosen ones, then weight-mergessrc/llama-graph.cppL35
common_speculativespeculative decoding: a small model guesses a run, the big model verifies in parallel, accept the prefixcommon/speculative.hL34
mtmdmultimodal: clip + projector encode an image into embeddings, decoded together with texttools/mtmd/mtmd.hL36
ggml_ssm_scanstate-space models: a fixed-size recurrent state replaces the KV cache, O(1) memoryggml/include/ggml.hL37
LoRAlow-rank adapter: fine-tune by hanging a small low-rank delta on top, without changing original weightssrc/llama-adapter.hL24
convert_hf_to_ggufconvert an HF model to GGUF: recognize arch -> rename tensors -> serialize; a thin CLI + conversion packageconvert_hf_to_gguf.pyL38

How to use this quick reference, and a closing word

Usage is simple: bookmark this page. When reading source or chasing a bug you hit a term, come to the glossary for "what it is, where it is defined", and click the link back to its lesson when you need depth; to untangle how a few concepts relate, look again at that dependency map. It replaces no lesson; it just spares you re-reading the whole course over one term - which is the entire point of "quick reference". And do not forget the browser's in-page search (Ctrl/Cmd+F): searching an English symbol name right on this page is often faster than recalling "which lesson was it in". One more reminder: these twenty-odd terms are only the skeleton, not everything. The book covers many equally important but more specialized concepts too - chat templates (L22), grammar-constrained decoding (L23), llama-server's continuous batching (L28), the quantize tool and evaluation (L29/L30). They are not in this quick table only because quick reference favors "few and precise"; when you actually need one, the lesson title leads you right back. In other words, this table is an index of the most-used terms, while the whole course is the full dictionary. As you grow used to it you will find the less you look up, the better you remember - the best quick reference, in the end, is one that frees you from needing it.

Finally, a closing word. Forty lessons in, you have taken llama.cpp from "a magical program that runs big models" and turned it into a machine whose every part is visible and whose every data flow you can articulate: how a model loads, how one token travels from prompt all the way to output, how the KV cache saves repeated work, how a backend runs the compute graph on a GPU, how advanced architectures find a path beyond the standard transformer, and how to convert, build, and contribute all of it back. Understanding a real-world large C++ project is never about reading it in one breath; it is about peeling it layer by layer and chewing one concept at a time - and you have done exactly that. Next, pick a point you are genuinely curious about or care about, and go read its source, run it, change it. This illustrated course ends here, but your story with llama.cpp has just turned to the page titled "do it yourself".

If you ask "so what is the actual first step?" - here are a few low-barrier entry points: run llama-cli with -v and, against the scrolling logs, spot the loading, graph-building, decode, and sampling stages you learned earlier; or pick the smallest test-* case, understand it, then deliberately break it and watch how the test reports; or go to the repo's issue list and find a small one labeled "good first issue" to practice on. What matters is never how big the first step is, but actually taking it - you already hold this map, you will not get lost. Have fun.

✅ Key points
  • The nine parts build up step by step: overview -> foundations -> the ggml engine -> llama inference internals -> API and tools -> low-level kernels -> advanced topics -> practice and contributing -> quick reference.
  • Concept dependency: ggml_tensor is the foundation -> ggml_cgraph is wired from tensors -> ggml_backend executes the graph -> llama_context holds the KV cache -> llama_batch/decode drives one inference step; vocab in, sampler out.
  • The glossary is organized in four groups: core data structures, the inference flow, kernels and backends, advanced mechanisms and tools; each entry gives "one-line definition + source location + jump-to-lesson".
  • Usage: hit an unfamiliar term, come here for "what it is + where it is", and click the link back to its lesson for depth - look up fast, jump accurately is this page's only goal.
  • Beyond the table are more specialized terms (chat templates L22, grammar L23, llama-server L28, quantize tool and eval L29/L30) - the quick reference keeps only the most-used.
💡 Design insight
At the finish line it is worth looking back: these forty lessons have really been telling one story - how a vast system is split into layers of small parts that each mind their own job yet interlock. ggml separates "what to compute" (the compute graph) from "what computes it" (the backend); llama separates "what the model is" (architecture/weights) from "how to run one step" (context/batch); the tools layer separates "library capability" from "command-line experience"... every split is the same wisdom: let each part understand only its own patch, and meet the others through a clear interface. That is exactly why you could "chew one concept per lesson" yet end up assembling the whole machine - because the system itself is built that way. Keep this concept dependency map in mind and you hold a universal key: facing any unfamiliar large system, first ask "what layers does it split into, what is each responsible for, where are the interfaces", and you will always find a way in. This course ends, but this system-decomposing eye will stay with you. That, perhaps, is the most valuable thing this course leaves you - it outlasts any single function name.

🧪 Self-test - think about the design

1. In this lesson's concept dependency map, who is the bottom 'foundation' that the others build on?
  1. llama_context
  2. the GGUF file format
  3. ggml_tensor (the tensor, the basic unit of data)
  4. the sampler
Show answer & explanation click to expand
Answer: C. The map goes bottom-up: at the bottom is ggml_tensor (the basic data unit); tensors compose a ggml_cgraph, the backend executes the graph, llama_context holds the KV cache and drives decode, and only at the top sits 'one llama_decode'. So the tensor is the foundation - without it, the graph, backend, and context above have nothing to stand on. That is why bottom-up understanding is advised: grasp the tensor before graphs and backends.
2. In which file is the real struct of ggml_cgraph (the compute graph) defined?
  1. ggml/src/ggml-impl.h (the public ggml.h has only a forward declaration)
  2. right in the public header ggml/include/ggml.h
  3. ggml/include/gguf.h
  4. src/llama-graph.cpp
Show answer & explanation click to expand
Answer: A. This is a common 'cannot find the definition' trap: in the public ggml/include/ggml.h, ggml_cgraph is only a forward declaration; the real struct lives in the internal header ggml/src/ggml-impl.h. Many ggml types do this - expose an opaque pointer outward, hide the fields in an internal header, so the implementation can change without breaking the ABI. The glossary's 'source location' column is there to steer you past this trap, straight to the file to read.
3. In the map, who does the KV cache 'hang on' - that is, who holds it?
  1. the sampler
  2. ggml_backend
  3. llama_context (it owns the runtime state of one inference session)
  4. ggml_tensor
Show answer & explanation click to expand
Answer: C. The KV cache is one of the runtime states held by llama_context (which also holds compute buffers, sampling state, etc.). So when debugging something to do with the KV cache, the map tells you to look inside llama_context, not to wander into the low-level ggml_tensor or ggml_backend. That is the practical value of the map: every arrow is a clue for 'which layer to chase when something goes wrong'.
💭 Open questions (no single right answer - just think or try)
  • This lesson is the book's index. Close it and write the 'map' from memory: (1) in one sentence each, summarize what problem the nine parts solve (from overview to quick reference); (2) draw the core concepts' dependency chain from memory - from ggml_tensor all the way to one llama_decode, through which concepts, and who holds the KV cache; (3) pick the three terms you remember best and, without the table, say roughly which file each lives in and which of the four groups it belongs to. Do these three fluently and you have truly internalized llama.cpp's overall structure.