跳到主要内容

推理与大时刻 — 一个诚实到残忍的演示

这一章讲两件事: 生成文字的完整机制——从 logits 到一个 token 要过几道闸门; 以及全书的高潮:作者把训练好的模型真正跑给读者看,包括它翻车的全过程。 这一章值得慢读:那次翻车比任何成功演示都讲得更透「模型到底是什么」。

1. 先看现象:同一台机器,两种活法

训练时(第 10 章)模型一次看完整段话、一次算所有位置的答案;推理时(拿来用)它一次只生成一个 token:

取最后一个位置的 8000 个打分(logits)
→ 过几道采样闸门,挑出 1 个 token
→ 接到文末
→ 整段重新喂进去,再来一遍

图说:第 01 章那张「一次一个词」的图,这里是它的实现层。
「重新喂进去」的代价由 KV 缓存(第 06 章)垫着:过去的键值不重算,只增量追加。

2. 采样闸门:从 8000 个分到 1 个 token

直接永远挑最高分——这一招叫贪婪解码——写得稳定但无聊,写长了还会复读。

第 01 章提过的温度(控制挑词大胆程度的旋钮)在这里正式登场;原书的 TokenGenerator 把市面上的闸门全部装上,按顺序过1:

闸门干什么本书取值
温度打分先除以温度:低于 1 收窄选择(保守),高于 1 放宽(放飞)0.6(演示时 0.3)
top-k只在分数最高的 k 个里挑50
top-p(按机会从高到低排队,凑够 p 就截断)只在队首这一撮里挑0.85
typical 采样留下「信息量接近平均」的候选,砍掉特别意外和特别平庸的1.0(默认关)
重复惩罚/频率惩罚已生成的 token 扣分,越近扣越狠2.4 / 0.6

三道截断闸门(top-k、top-p、typical)各自产生一张「许可名单」,最终取三张名单的交集; 交集空了就退回 top-k,再不行就退回原始概率——原书代码里两层兜底写得明明白白2

温度的体感(第 01 章埋的「手感不均匀」在这里兑现):除以 0.6 意味着分数被整体放大 1.67 倍, 本来 40% 对 12% 的差距会拉大到「一边倒」;除以 1.5 则会把差距压平。 同一个模型,温度就是「稳重/放飞」旋钮,而且同样的拧动幅度,在不同分数分布上手感完全不同

3. 防复读的五层防线

小模型尤其爱复读。原书在采样闸门之外又叠了五层防线,这本书的实现可以当「防复读兵器谱」读3:

防线机制
n-gram 禁令刚出现过的 4 连 token 不许再连着出现
循环检测4生成尾部若以 8~64 的任意周期整段重复,直接停笔
防复制对照表训练文本所有 2~12 连 token 的「前缀→后继」编成对照表;生成时若已连抄 11 个,禁掉第 12 个——不许照抄原文5
重复/频率惩罚已用过的 token 扣分
硬停达到 token 上限、遇到句尾符,停

第三层最有趣,值得停一下:它是拿切词器当反抄袭探测器—— 训练文本的 n-gram 全部登记在案,模型敢整段背书就当场掐断。第 03 章「切词器是整条流水线的字典」这句伏笔,在这里兑现6

4. 主走查:一次真实翻车的完整解剖

一切就绪。作者在交互界面输入。下面是原文输出的节录并拼接(省略号处有删节——两次背诵之间,原文还完整复读了一遍 "The height of his glory" 一段的段落,此处从略)7:

> who is rick

=== Completion ===
weekI HAD always thought Jack Gisburn rather a cheap genius—though a good
fellow enough—so it was no great surprise …(整段背诵第 10 章那篇小说的开头)
…stopped me before Gisburn's "Moon-dancers" suchnot to of ak I HAD always
thought Jack Gisburn rather a cheap genius—…(绕了一圈,再次从头背起)
…We shall not look upon its like again"? Well!—even through the prism of
Hermia's tears I felt able to alitmohad to here-clI've againRivierTjealpect
ght swet, toqw backy of sent againessed in ← 崩成乱码,停笔

问的是「rick 是谁」,模型答的是「背课文然后崩溃」。原书把这段输出切成三幕逐段归因8:

第一幕:复读小说开头。 「who is rick」不在训练分布里——模型一辈子只见过一篇小说。 它滑回了离问题最近的「已学盆地」:小说的正文。原书给这个行为起了名字:吸引子 (训练把某些 token 序列的「概率地形」压出了深坑,生成一靠近就滑进去)9。 注意原书的提醒:这不是故障,它证明模型真的学了东西——语法、标点、叙事流全都对10

第二幕:「suchnot to of ak」式的磕绊,然后从头再背。 防复制对照表掐断了「继续背」的路, 模型在学过的空间里改道;可整个空间就一篇小说,改来改去又绕回小说开头。 这一幕的教训是原书点破的:推理期的护栏只能改道,变不出模型没学过的能力11

第三幕:乱码。 重复惩罚开到了 3.4/2.5(故意调猛做演示), 高频 token 被扣到几乎不可能被选中,采样被迫在低概率尾部(模型几乎没学过怎么连的 token)里抓阄—— 输出就成了 "alitmohad to here-clI've" 这样的碎片12

三幕各欠一句「正常世界」的对照

原书给的对照口径:生产系统把惩罚控制在 1.0-1.5(轻推而不是鞭打)13; 答不上「rick 是谁」是因为它只做了预训练,没做第 12 章的指令微调——语言模型只会续写,助手才会回答14; 以及规模账:本模型约 10 万参数、单篇语料,生产模型是千亿参数、万亿 token——差六到九个数量级,不是同一物种15

5. 那真正的「再大一号」长什么样

原书附了一个中间档的实现:同样的架构配方,把规模抬到 2.5 亿参数、语料换成 TinyStories(数千篇合成童书),模型就能即兴写出连贯小故事,还挂了在线演示16。 这张三级台阶是全书「规模→能力」论证的实证骨架17:

本书模型TinyStories 版生产模型
参数~10 万2.5 亿数千亿
语料一篇 4,200 词数千篇童书万亿 token
行为背书+崩坏稳定即兴讲故事问答、推理、写代码

作者还交了底:为什么不把 2.5 亿那版印进书里——按云价训练一遍要 500 到 1 万美元, 教学版在自己电脑上几分钟、零成本18。教学与生产的差距不是聪明程度,是电费。

6. 作者的判断与证据

说法书里的证据我们的标注
翻车是「按预期的正确行为」三幕归因 + 吸引子机制8机制解释与输出逐段对得上
护栏不能造能力第二幕:禁了背诵、模型只能绕回背诵11演示内证,成立
惩罚要温和第三幕崩坏 + 「生产用 1.0-1.5」口径13前半有实证,后者属经验值
多阶段训练是必需「它不做 SFT,所以只会续写不会答」14机制成立,实证在第 12 章

判断(我们的,不是书里的): 这次演示最大的价值是把「幻觉」祛魅了。 大模型的胡编乱造和小模型的胡言乱语,机制是同一个——采样落在了没学好的区域; 区别只是大模型的「没学好区域」小得多,所以乱码看起来像流利的不知情。 原书没有用「幻觉」这个词,但三幕解剖就是幻觉的显微镜版。 如果错,会错在: 如果幻觉的主要来源是训练数据本身的错误(而非采样落点), 「祛魅」的框架就只覆盖了一半——两个来源都被文献支持,这里强调的是原书演示覆盖的那个。

7. 边界与局限

  • 防复读五层防线全是推理期绷带:第 10 章的过拟合(背题)是训练期病,推理期只能遮不能治——原书在教训一节明说「这些机制不能补偿训练缺陷」19;
  • 演示把惩罚故意调到 3.4/2.5,正常使用不会看到第三幕那么惨——这是为教学设计的可控事故20;
  • TinyStories 一节给了在线演示链接,外部链接随时间会失效,数字与行为以论文和仓库为准16;
  • 原书说本模型「约 100,000 参数」,与其给出的「2 层×128 维」配置反推的量级一致,但没有给出精确计数,当数量级用。

8. 可带走的

  1. 推理 = 取最后位置打分 → 过闸门 → 挑一个 → 接回去 → 重来;KV 缓存垫着「重来」的成本;
  2. 温度管「稳重/放飞」,top-k/top-p/typical 是三张许可名单取交集,交集空有兜底;
  3. 防复读五层:n-gram 禁令、循环检测、防复制对照表、惩罚、硬停——全是拿来用期的绷带;
  4. 防复制对照表 = 拿切词器登记训练文本 n-gram,模型照抄原文当场掐断;
  5. 吸引子:训练把概率地形压出深坑,分布外(没见过的那种)的提示(喂给模型的输入文字)会把生成滑进坑里——复读是「忠实于训练」的表现;
  6. 护栏只能改道,变不出没学过的能力;
  7. 惩罚开太猛 = 把采样逼进低概率尾部 = 乱码;生产口径 1.0-1.5;
  8. 只做预训练的模型只会续写;「会回答问题」是第 12 章之后的事;
  9. 规模三级台阶(10 万 → 2.5 亿 → 数千亿)是「规模换能力」最直观的证据链。

9. 原文地图

主题原书章原文位置
TokenGenerator 定义TokenGenerator for Inferencetext/74-fm-tokengenerator-for-inference.txt:5(搜「inference engine」)
采样默认值与叠闸门TokenGenerator for Inferencetext/74-fm-tokengenerator-for-inference.txt:61(搜「temperature」) · text/74-fm-tokengenerator-for-inference.txt:73(搜「repetition_penalty」) · text/74-fm-tokengenerator-for-inference.txt:99(搜「top_k」)
温度机制TokenGenerator for Inferencetext/74-fm-tokengenerator-for-inference.txt:719(搜「Temperature (0.6」)
三名单取交集与兜底TokenGenerator for Inferencetext/74-fm-tokengenerator-for-inference.txt:401(搜「Combine masks」) · text/74-fm-tokengenerator-for-inference.txt:407(搜「Fallback」)
typical 采样TokenGenerator for Inferencetext/74-fm-tokengenerator-for-inference.txt:319(搜「_typical_topk_topp_sample」)
五层防复读TokenGenerator for Inferencetext/74-fm-tokengenerator-for-inference.txt:771(搜「anti-repetition guardrails」)
防复制索引The Big Moment—Prompting Our Modeltext/75-fm-the-big-moment-prompting-our-model.txt:25(搜「build_anti_copy_indices」) · text/75-fm-the-big-moment-prompting-our-model.txt:59(搜「ANTI_N_MAX」)
循环检测The Big Moment—Prompting Our Modeltext/75-fm-the-big-moment-prompting-our-model.txt:73(搜「_detect_variable_period_loop」)
演示参数(3.4/2.5)The Big Moment—Prompting Our Modeltext/75-fm-the-big-moment-prompting-our-model.txt:111(搜「3.4」)
翻车输出原文The Big Moment—Prompting Our Modeltext/75-fm-the-big-moment-prompting-our-model.txt:191(搜「who is rick」) · text/75-fm-the-big-moment-prompting-our-model.txt:201(搜「alitmohad」)
防复制的洞察(11 连禁 12)Understanding Inference Code and Model Behaviortext/76-fm-understanding-inference-code-and-model-behavior.txt:47(搜「11 consecutive tokens」)
第一幕:吸引子Understanding the Output—An Educational Demonstrationtext/77-fm-understanding-the-output-an-educational-demonstr.txt:25(搜「attractor」) · text/77-fm-understanding-the-output-an-educational-demonstr.txt:25(搜「navigates to the nearest」)
它证明模型学了东西Understanding the Output—An Educational Demonstrationtext/77-fm-understanding-the-output-an-educational-demonstr.txt:29(搜「learned something」)
第二幕:护栏只能改道Understanding the Output—An Educational Demonstrationtext/77-fm-understanding-the-output-an-educational-demonstr.txt:47(搜「guide but not fundamentally change」)
第三幕:低概率尾部Understanding the Output—An Educational Demonstrationtext/77-fm-understanding-the-output-an-educational-demonstr.txt:51(搜「low-probability」) · text/77-fm-understanding-the-output-an-educational-demonstr.txt:65(搜「1.0–1.5」)
只做预训练所以不会答What This Demonstrates About Language Model Designtext/78-fm-what-this-demonstrates-about-language-model-desi.txt:69(搜「Instruction Fine-Tuning」)
规模与数据的数量级What This Demonstrates About Language Model Designtext/78-fm-what-this-demonstrates-about-language-model-desi.txt:7(搜「100,000 total parameters」) · text/78-fm-what-this-demonstrates-about-language-model-desi.txt:11(搜「billions to trillions」)
五条教训Practical Lessons from This Exampletext/80-fm-practical-lessons-from-this-example.txt:5(搜「Match Model Scale」) · text/80-fm-practical-lessons-from-this-example.txt:9(搜「Inference Constraints Have Limits」)
TinyStories 版与演示链接Exploring Even Further—A Real Trained Model: TinyStories GPT-4 Version Implementationtext/81-fm-exploring-even-further-a-real-trained-model-tiny.txt:11(搜「nanostorychat」) · text/81-fm-exploring-even-further-a-real-trained-model-tiny.txt:49(搜「250 million」)
为什么不印进书(成本)Why This Implementation Wasn’t Included in the Booktext/82-fm-why-this-implementation-wasn-t-included-in-the-b.txt:7(搜「$500」) · text/82-fm-why-this-implementation-wasn-t-included-in-the-b.txt:25(搜「100,000 times」)
达成了什么Appreciating What Was Achievedtext/83-fm-appreciating-what-was-achieved.txt:3(搜「Complete Training Pipeline」)

Footnotes

  1. 出处:「TokenGenerator for Inference」(generate 签名与采样代码)(text/74-fm-tokengenerator-for-inference.txt:61,搜「temperature: float = 0.6」;:73,搜「repetition_penalty: float = 2.4」;:99,搜「top_k: int = 50」;:307,搜「_typical_topk_topp_sample」)。默认值与闸门顺序照录代码。

  2. 出处:text/74-fm-tokengenerator-for-inference.txt:401(搜「Combine masks」)与 :407(搜「Fallback」)。原文:keep = 三张掩码的按位与;filtered 全空则退回 top-k 掩码,再退回原始概率。

  3. 出处:「TokenGenerator for Inference」末段(text/74-fm-tokengenerator-for-inference.txt:771,搜「anti-repetition guardrails」)。原文:「extensive anti-repetition guardrails (windowed penalties, n-gram bans, loop detection, dataset anti-copy, and self anti-copy)」——多层防线确保小模型也能输出流畅、多样、不重复。

  4. 出处:「Understanding Inference Code and Model Behavior」(text/76-fm-understanding-inference-code-and-model-behavior.txt:83,搜「variable-period loop detection」)。原文:检测生成尾部是否以 8-64 间任意周期整段重复,捕捉多尺度的退化循环。

  5. 出处:「Understanding Inference Code and Model Behavior」(text/76-fm-understanding-inference-code-and-model-behavior.txt:47,搜「11 consecutive tokens」)。原文:若模型生成的 11 个连续 token 与训练序列完全一致,索引允许禁掉第 12 个——把生成推向新内容而非背诵。

  6. 出处:text/75-fm-the-big-moment-prompting-our-model.txt:25(搜「build_anti_copy_indices」)与第 03 章「作者的判断」一节的伏笔(text/38-fm-analysis-of-the-output.txt:161,搜「life cycle」为佐证)。索引以切词器编码训练文本、登记 2-12 元前缀的后继集合。

  7. 出处:「The Big Moment—Prompting Our Model」(Output 段)(text/75-fm-the-big-moment-prompting-our-model.txt:191,搜「who is rick」;:195,搜「cheap genius」;:197,搜「suchnot」;:201,搜「alitmohad」)。原文输出为节录底稿:week 前缀+整段背诵、中途 "suchnot to of ak" 改道重背(其后原文另有一次完整复读,拆解节录时从略)、尾部乱码停笔。

  8. 出处:「Understanding the Output—An Educational Demonstration」(text/77-fm-understanding-the-output-an-educational-demonstr.txt:11,搜「Three Revealing Behaviors」;:55,搜「attractor basin」;:81,搜「guide but not fundamentally change」;:109,搜「low-probability」)。原文分三段:滑向训练分布的吸引子;推理机制只能引导不能创造能力;重惩罚把采样推进模型几乎没学过的低概率区。 2

  9. 出处:text/77-fm-understanding-the-output-an-educational-demonstr.txt:25(搜「attractor basin」)。原文:「an "attractor basin"—a region of probability space where the model naturally gravitates」。

  10. 出处:text/77-fm-understanding-the-output-an-educational-demonstr.txt:29(搜「learned something」)。原文:这不是随机噪声——连贯、合语法的文本证明它成功学会了语言结构、句法与叙事流。

  11. 出处:text/77-fm-understanding-the-output-an-educational-demonstr.txt:47(搜「guide but not fundamentally change」)。原文:「inference mechanisms can guide but not fundamentally change what a model has learned」;护栏像护栏,改变不了目的地。 2

  12. 出处:text/77-fm-understanding-the-output-an-educational-demonstr.txt:61(搜「3.4 for repetition」)。原文:3.4/2.5 的激进惩罚把模型偏好的高概率 token 大幅压制,迫使采样器从未充分学习的低概率 token 里挑——输出失去连贯。

  13. 出处:text/77-fm-understanding-the-output-an-educational-demonstr.txt:65(搜「1.0–1.5」)。原文:生产系统把惩罚调在温和区间(典型 1.0-1.5),轻推而非压垮学到的分布;另见 text/80-fm-practical-lessons-from-this-example.txt:19(搜「1.0-1.5」)。 2

  14. 出处:「What This Demonstrates About Language Model Design」(text/78-fm-what-this-demonstrates-about-language-model-desi.txt:69,搜「Instruction Fine-Tuning」)。原文:本书模型只做预训练、从未学过「指令→回答」模式,所以它不回答 who is rick、只生成文本;「语言模型预测文本,指令微调后的模型给出有用的回应」(text/80-fm-practical-lessons-from-this-example.txt:23,搜「predicts text」)。 2

  15. 出处:text/78-fm-what-this-demonstrates-about-language-model-desi.txt:7(搜「100,000 total parameters」)与 :13(搜「billions to trillions」)。原文:教学模型 2 层×128 维、约 10 万参数;生产模型 20-100+ 层、2048-12288 维、数十亿到数万亿参数。

  16. 出处:「Exploring Even Further」(text/81-fm-exploring-even-further-a-real-trained-model-tiny.txt:11,搜「nanostorychat」;:55,搜「250 million」)。原文:TinyStories 版约 2.5 亿参数、16 层、在线演示 nanostorychat.com、仓库 github.com/didogrigorov/LLM-story。 2

  17. 出处:text/82-fm-why-this-implementation-wasn-t-included-in-the-b.txt:25(搜「100,000 times」)。原文:教学模型比 2.5 亿参数版少约十万倍计算——这条「规模梯度」直接换算成成本。

  18. 出处:text/82-fm-why-this-implementation-wasn-t-included-in-the-b.txt:7(搜「$500」)。原文:250M 模型在完整 TinyStories 上多卡训练,云成本 500-10,000 美元;H100/A100 每卡时 2-4 美元(:11,搜「$2–4」)。

  19. 出处:「Practical Lessons from This Example」(text/80-fm-practical-lessons-from-this-example.txt:9,搜「Inference Constraints Have Limits」)。原文:「No amount of penalties, bans, or sampling tricks can create capabilities the model hasn't learned」——推理期机制只能为已够格的模型抛光,不能补偿训练缺陷。

  20. 出处:text/75-fm-the-big-moment-prompting-our-model.txt:111(搜「3.4」)。原文代码注释:repetition_penalty=3.4、frequency_penalty=2.5 均标注「↑ stronger deterrent」——刻意调猛以探索边界。