第 17 章 · Module 5: 采样、引导与应用

Classifier-Free Guidance

靠「调音量」在质量和多样性之间换。


17. Classifier-Free Guidance

主源:CMU 10799 S26 Lecture 7《Guidance & Controllable Generation》(讲师在白板上从条件生成的"瓶颈"一路推到 classifier guidance,chunk #1299–1317)。本章后半段 CFG 部分为教材外补充(黄色标注),用来把这条线接到 Ho & Salimans 2022。

📌 TL;DR

到上一章为止,我们学到的所有 diffusion 模型都是无条件的——按下按钮,扔一张高斯噪声进去,吐出一张"看起来真"的图,但你没法指定它长什么样。本章解决的就是conditional generation:怎么让模型生成"一只戴墨镜的柯基"而不是随机一张图。

讲师的推导分两步走:

  1. 第一性原理:根据 Bayes 定理,条件分数 = 无条件分数 + 一个判别模型的梯度。$$\nabla_x \log p(x\mid c) \;=\; \nabla_x \log p(x) \;+\; \nabla_x \log p(c\mid x)$$ 这就是 Classifier Guidance(Dhariwal & Nichol 2021)——拿一个预训练的无条件 diffusion 加一个分类器,组合出条件生成。
  2. 但分类器要能"看懂噪声"。讲师在 chunk #1317 把锅指出来:现成的 ImageNet 分类器根本不能吃 $x_t$,必须重新训一个带噪分类器。对每一个新的条件(文本、深度图、3D mesh),都要重训一次。这条路太贵。

Classifier-Free Guidance(Ho & Salimans 2022)是对这个困境的优雅一击:把无条件模型和条件模型塞进同一张网络,训练时随机把条件 $c$ 丢掉(dropout,比如 $p=0.1$),采样时用一个外推公式

$$\hat\epsilon \;=\; (1+w)\,\epsilon_\theta(x_t,t,c) \;-\; w\cdot\epsilon_\theta(x_t,t,\varnothing)$$

一个网络、一个训练循环、一行采样代码改动。Stable Diffusion / DALL·E 2 / Imagen 全部在用。$w$(guidance scale)就是这一章标题的那个"音量旋钮"——拧大点 prompt 听话、多样性下降;拧小点反之。

一、上一章承接:DDIM 之后,下一个旋钮是什么?

CMU 这节课的开场,讲师先复习了上一节 DDIM:

「Last time we learned DDIM which is like a fast sampling method. And how we do it is that basically we first from any noisy time step, we get a predicted clean image estimation from that time step. And instead of basically going back to t minus one, we go back to t minus 10 instead so that we can skip steps and everything. So this way we can have like 10x speed up.」(chunk #1299)

课堂上有学生顺嘴问了一个非常自然的问题:

「Now we go like time t to t minus 10, but do we ever like go the opposite way? Can we do this somewhat sarcastically?」(chunk #1300)

讲师笑了一下,回了一句"yes, we could, we're going to talk about it today"——这句"反着跑"其实就是 CFG 的精髓伏笔:在分数空间里向"和条件相反的方向"外推一段,等价于强化条件信号。本章末尾会回来揭这个谜。

讲师顺手把上节课讲过的 EDM 设计空间也更正了一下(chunk #1301–1302):之前他说 EDM 把训练权重压在中间时间步是"因为中间最难",这次纠错——其实是因为中间时间步对应"语义/结构信息"最浓的层级,那里最能撬动 loss 的下降。这条更正本身和 CFG 没关系,但它埋了一个很重要的背景知识:diffusion 的"中间时间步"是语义信息密度最高的区段,而 CFG 在工业实践中也恰好只在中间时间步施加最大的 guidance(即所谓 interval guidance)——讲师在 L7 末尾才会回到这一点。

讲完 DDIM 复习,讲师抛出关键转折(chunk #1302):

「All right, so now that we have seen a lot of different knobs that we can tune for diffusion models, are they perfect? Now, do we already find the perfect generative models? What are we missing here?」

学生七嘴八舌:"attention" → 讲师答这是工程惯性、不是数学必然;最后有人说出了关键词——"conditional generation"。讲师拍板:"Conditional generation, right? So right now we haven't talked about anything about conditional generation yet."(chunk #1303)。整节课的主题由此定型。

🧭 本章在 module 5 的位置:第 16 章给了你"采样加速"的旋钮(DDIM、各类 ODE solver),本章给你"内容控制"的旋钮(guidance)。两个旋钮加在一起,构成了你今天用 Stable Diffusion WebUI 里能直接拖的两个 slider——"steps" 和 "CFG scale"。再加上后面 第 18 章的 ControlNet / negative prompt 这类"细化旋钮",你就把整个商用文生图工具栏的核心 UI 都对应到了 diffusion 的数学结构上。

二、问题设定:我们到目前为止造的是「无条件机器」

讲师转到正题时画了一张图(chunk #1303):

「Right now we haven't talked about anything about conditional generation yet. So far, everything is unconditional. We actually don't know how to do conditional generation yet.」

翻译成我们已经学过的语言:

  • 到目前为止,diffusion 干的事情是:采 $x_T \sim \mathcal{N}(0,I)$,跑 T 步去噪,得到 $x_0 \sim p_{\mathrm{data}}(x)$;
  • $p_{\mathrm{data}}$ 是整个数据集的边缘分布——你没法挑里面的"一只柯基"或"一张梵高风景";
  • 你要的是 $p(x \mid c)$:给定一个 condition $c$(class label / text prompt / 另一张图 / depth map / 3D mesh / ……),从这个条件分布里采样。

讲师顺手列出真实场景里 $c$ 可能是什么样(chunk #1304, #1306):

「You'll have like some labels or like some conditions that you want to condition the model or the samples on. So it can be a text description or it can be like the attribute labels or it could be like another image or something like that. … for example maybe another image or like some 3D mesh or something like that.」

注意"3D mesh"那一句——讲师本人是做 3D 生成的,这种"任何模态都能当 condition"的视角,是后面一切讨论的起点。再翻译成一张"条件 → 用例"对照表,帮你把"$c$"这个抽象符号落到地面上:

  • $c = $ 类别 id:经典 ImageNet 1000 类条件生成;$c \in \{0,1,\dots,999\}$,过一个 embedding lookup 进网络;
  • $c = $ 文本 prompt:"一只戴墨镜的柯基";用 CLIP / T5 text encoder 编成一串 token embedding,再 cross-attend 进 UNet——Stable Diffusion 主线;
  • $c = $ 另一张图:image-to-image 翻译(白天 → 夜晚)、风格迁移、SR;
  • $c = $ 多模态控制信号:depth map、edge map、人体姿态、segmentation mask——ControlNet 一系列工作;
  • $c = $ 几何先验:3D mesh、点云、normal map——讲师本人在 Luma AI 那条线的工作(chunk #1527 提到的 TVM、mean flow,正是 3D 视角下对 CFG 训练稳定性的研究)。

这些条件模态差异巨大,但讲师后面要让你看到的是:它们在 score 空间里全部坍缩成同一个数学动作——给 $\nabla \log p(x_t)$ 加一项 $\nabla \log p(c\mid x_t)$。换句话说,guidance 把"千百种条件生成任务"压成了"一种数学操作"。这就是讲师在课堂上反复强调"why this is the best thing I like about diffusion"(chunk #1309 末段)的真正含义。

三、最朴素的做法:每个条件训一个新模型

课堂上学生先给了一个非常直接的答案:把条件 $c$ 编码成特征向量,和图像一起塞给网络,从头训一个条件模型。讲师承认这是"perfect answer for sure"(chunk #1306),但马上抛出问题:

「But okay, so what what do you guys think is like the major bottleneck for this kind of formulation?」

一个学生答出了关键(chunk #1307):"a lot more training and training data for each condition." 讲师拍板(chunk #1308):

「You need different training for different conditions, right? … For each type of condition, we will have to train a new model on it. … if this model is text conditioned, then we cannot use it to do like class label condition. … if this model is class label condition then we cannot use it for condition on the depth map or something like that. If it's conditioned on another image then we cannot use it for text condition generation.」

把痛点写清楚:

  1. 组合爆炸:N 种 condition × M 种数据域,要训 N×M 个模型;
  2. 数据墙:每一个条件,都需要"图+条件"的成对数据集;
  3. 开放集失败:你训练时没见过的 condition(比如一种你从没遇到过的 attribute),模型一脸懵。
「You can't just like condition on everything you can think of right that's the problem and also even if you could formulate your model to condition on everything that you can think of you also need to collect data for everything. Right. That's really really expensive.」(chunk #1308)
讲师顺手开了一个圈内笑话(chunk #1309):「And this is why like Alexander Wang got his million dollar job now, you know, just collecting data is important apparently.」(指 Scale AI 创始人那笔被 Meta 大额收购的新闻——把"数据收集"卖成了百亿生意。)

另一个学生在 chunk #1309 末尾追问:「Can you use a single model to do all of that? Can you embed all of that into it?」讲师答:"this is what people are trying to do"——这是omni-model路线(一个网络吃所有模态)。但他马上转向另一条路:

「But actually we can have another option here. And this is why this is like the best thing that I like about diffusion.」(chunk #1309)

讲师停顿了一下才往下走——这是整节课的"换挡时刻",提示你下面要讲的不是"训更大模型"而是"巧妙地重用已有的小模型"。这也是 guidance 这个家族在工程上最迷人的地方:不靠堆参数,靠改采样公式。

四、关键转折:能不能复用无条件模型?

讲师在 chunk #1310 把整节课的"核心问题"清清楚楚地写在白板上:

「The unconditional model that we learned already has a lot of good information about how to generate a realistic looking image. … An unconditional model already knows how to generate a human face, you just need to know how to control it better. So why can't you just like reuse some of the knowledge in the unconditional model to better control it into a conditional model? Is it possible for us to reuse the unconditional model at all? Is the number one question that we're going to answer today.」

这是整章的中心问题。换句话说:

无条件 diffusion 已经把"长得像真图"这件事学会了。我们能不能只额外加一点点东西,就把它"扭"成条件生成?

讲师马上给了一句神级 hint(chunk #1311):

「Remember how there are two two dudes that we really need to remember from this class? One guy is Gaussian. What's the other guy? Bayes, right? Remember the number one rule in probability theory. You should tattoo Bayes' theorem.」
💡 这门课的两位"祖师爷":Gaussian 决定了所有 forward / reverse step 都是高斯;Bayes 决定了所有"换底(change of conditioning)"的操作都能写出来。本章的 trick 就是把 Bayes 套在 score function 上。

五、Classifier Guidance:用 Bayes 把条件分数拆开

5.1 出发点:diffusion 的 score 视角

回忆一下我们在第 11 章学过的:diffusion model 本质上是在学score function——对数概率密度的梯度。讲师原话(chunk #1311):

「We know that diffusion model is basically just modeling the score function which is basically just the gradient of the log probability.」

无条件 diffusion 学的是:

$$s_\theta(x_t,t) \;\approx\; \nabla_{x_t}\log p(x_t).$$

我们想要的是条件分数:

$$s(x_t,t,c) \;=\; \nabla_{x_t}\log p(x_t\mid c).$$

5.2 Bayes 一刀切下去

对条件概率应用 Bayes:

Step 1. 写出 Bayes: $$p(x_t\mid c) \;=\; \frac{p(x_t)\,p(c\mid x_t)}{p(c)}.$$ Why:把"条件在 $c$ 下生 $x$"换底成"无条件生 $x$,再乘判别概率"。$p(c)$ 是常数,对 $x_t$ 求梯度直接消掉。
Step 2. 两边取 log: $$\log p(x_t\mid c) \;=\; \log p(x_t) \;+\; \log p(c\mid x_t) \;-\; \log p(c).$$ Why:乘法变加法,方便求梯度。
Step 3. 对 $x_t$ 求梯度,常数 $\log p(c)$ 消失: $$\boxed{\;\nabla_{x_t}\log p(x_t\mid c) \;=\; \underbrace{\nabla_{x_t}\log p(x_t)}_{\text{无条件 score (已有)}} \;+\; \underbrace{\nabla_{x_t}\log p(c\mid x_t)}_{\text{判别器梯度}}\;}$$ Why:这就是 Classifier Guidance 的核心公式。讲师原话(chunk #1312):「the conditional probability can also be written as a combination of the unconditional probability times the conditional probability of the label given the model or the image.」

第二项 $\nabla_{x_t}\log p(c\mid x_t)$ 长什么样?讲师解释(chunk #1312–1313):

「This thing is actually a discriminative model … the probability of the label given the input image. So if your condition is a class label then it's a classifier. If the condition is the image caption then it could be a CLIP model. So basically you can now break the conditional generation score into an unconditional score which we can already obtain by pre-training unconditional model plus a gradient from a discriminative model.」

整理成一句话:

条件 score = 无条件 score(diffusion 提供)+ 判别器对 $x_t$ 的梯度(任何一个会算 $p(c\mid x)$ 的判别模型都行)。

5.3 采样:把"原本喂的 score"换成"加了判别器梯度的 score"

「All you need to do to do classifier guidance is literally just replace this unconditional score with the conditional score that we just derived by Bayes. That's it. And you just run your sampling as normal and you get a conditional sample.」(chunk #1313–1314)

具体到一行代码:原本 reverse step 里你拿 $s_\theta(x_t,t)$ 去更新,现在改成

$$\tilde s(x_t,t,c) \;=\; s_\theta(x_t,t) \;+\; \gamma\cdot\nabla_{x_t}\log p_\phi(c\mid x_t)$$

其中 $\gamma$ 是 guidance scale(原论文里写 $s$ 或 $w$,本章为避免和 score $s$ 冲突,用 $\gamma$)。$\gamma=1$ 严格对应 Bayes 推导;$\gamma>1$ 是过度加权判别器,把条件信号放大,质量↑多样性↓。

ℹ️ 判别器梯度怎么算?讲师回答(chunk #1315):「Usually back prop right through the model but you take the gradient with respect to the image instead of the parameter and then you get a gradient.」
换句话说:跑一次分类器前向得到 $\log p_\phi(c\mid x_t)$,做一次反向传播,但不更新参数,而是把 x_t.grad 取出来当作引导方向。和 adversarial example 用的"对图像求梯度"技术完全一样。

六、Classifier Guidance 的应用与局限

6.1 应用:一个 unconditional 模型 = 无数个条件任务

讲师举了一组 OG 论文里的例子(chunk #1316–1317):

「They just train an unconditional score model and then just apply exactly that thing and then you can get class conditioned on CIFAR, and then this is like inpainting, and this is like colorization. … you can just use the same unconditional model to perform a lot of conditional generation tasks now.」

这就是 Song & Ermon 2020 的核心戏法——同一个 unconditional score model,配上不同的"判别项",就能做:

  • 类别条件生成(判别项 = ImageNet 分类器);
  • 文本条件生成(判别项 = CLIP score);
  • inpainting(判别项 = "已知像素与目标一致"的对数概率);
  • colorization(判别项 = "灰度图与目标一致");
  • super-resolution(判别项 = "下采样后等于低分辨率输入")。

6.2 致命缺陷:分类器必须"看懂噪声"

但讲师马上把锅指出来(chunk #1317):

「This thing is problematic, right? Because the classifier actually need to be able to process noisy image and need to be able to recognize like which noise level it's at. So this thing is very problematic. This thing just means that we need to train a noisy classifier for the classifier guidance diffusion. You cannot just use off-the-shelf classifier anymore. So you cannot just like pull a pre-trained classifier from like GitHub or something like that. You need to actually specifically train this.」

把这条限制具象化:

  • diffusion 反向过程里,$x_t$ 是高斯噪声严重污染的图像($t$ 越大噪声越大);
  • 常规 ImageNet 分类器在 $\sigma=0$ 的干净图上训的,喂它 $x_t$ 就崩了——分类器自己都看不懂,对 $x$ 求出来的梯度根本不指向数据流形;
  • 所以要专门训一个"noisy classifier":把训练数据按所有 $t \in [0,T]$ 的 noise schedule 加噪,每个噪声水平都要分类正确;
  • 对每一种新 condition(不只是类别,还可能是 text、depth、segmentation map),都要重训一个对应的 noisy 判别模型;
  • 对 open-vocabulary 文本条件(比如 SD 那种"任意 prompt"),根本不存在这样一个 noisy 分类器——CLIP 也是在干净图上训的。
⚠️ 课堂上学生提的另一个尖锐问题(chunk #1315–1316):「I guess that only works for those conditionals actually occurs in your training data. You cannot do some interpolation to generate some other thing.」——也就是说,分类器没见过的标签,引导失灵。讲师当时答"I'll continue going, it'll get answered"——下一节课才完整回答,而那个答案就是 CFG。

七、(教材外补充)Classifier-Free Guidance:把两个网络压成一个

🟡 教材外补充:课堂源 chunk 在"noisy classifier 是个问题"这里就停了(chunk #1317 是这一节最后一段)。下一节才完整推出 CFG。为了让本章自洽,下面这几节是基于 Ho & Salimans 2022 原论文 + Stable Diffusion 实战经验补充的,不在主源 chunk 内。

7.1 一句话的 idea

既然 Classifier Guidance 的麻烦在"另外训一个 noisy 分类器",能不能干脆不要那个分类器?Ho & Salimans 2022 的答案是:

把无条件模型 $\epsilon_\theta(x_t,t,\varnothing)$ 和条件模型 $\epsilon_\theta(x_t,t,c)$ 合并进同一张网络,训练时随机丢掉条件,采样时用两次前向的差来构造引导方向。

7.2 训练:条件 dropout

一段普通 DDPM 训练循环里,只改一行:

  1. 从 dataset 里取一对 $(x_0, c)$;
  2. 采 $t \sim \mathcal{U}\{1,\dots,T\}$,采 $\epsilon \sim \mathcal{N}(0,I)$,构造 $x_t$;
  3. 以概率 $p_{\text{drop}}$(通常 0.1~0.2),把 $c$ 替换为 special token $\varnothing$;
  4. loss = $\|\epsilon - \epsilon_\theta(x_t, t, c)\|^2$(其中 $c$ 可能已经被换成 $\varnothing$)。

这样训出来的网络同时是无条件和条件的——你给它 $c$ 它就生条件分数,你给它 $\varnothing$ 它就生无条件分数。一个网络,一个 loss,一次训练。

7.3 采样:score 空间的外推

设 guidance scale 为 $w \ge 0$(SD WebUI 里那个 "CFG Scale"),采样时每一步用:

$$\boxed{\;\hat\epsilon \;=\; (1+w)\cdot\epsilon_\theta(x_t,t,c) \;-\; w\cdot\epsilon_\theta(x_t,t,\varnothing)\;}$$

等价地,写成 score 形式:

$$\hat s(x_t,c) \;=\; s(x_t\mid c) \;+\; w\cdot\bigl(s(x_t\mid c) - s(x_t)\bigr).$$

注意几个关键值:

  • $w=0$ → $\hat\epsilon = \epsilon_\theta(x_t,t,c)$,纯条件采样;
  • $w=-1$ → $\hat\epsilon = \epsilon_\theta(x_t,t,\varnothing)$,纯无条件采样;
  • $w>0$ → 把 $\epsilon$ 向"条件"方向外推,远离"无条件"——这就是开头那个学生问的"opposite way"的真正含义;
  • $w$ 在 Stable Diffusion 里典型设 $w \in [5, 12]$。
CFG 在 score 空间:把条件 score 向"远离无条件"方向外推 $x_t$ 无条件 $s(x_t)$ 条件 $s(x_t\mid c)$ $\hat s = s(c) + w\cdot(s(c)-s)$ (CFG 外推,$w > 0$) $s(c)-s$(差向量) 外推 = 沿 "条件指向、远离无条件" 的方向走 $(1+w)$ 倍

7.4 为什么外推 = 锐化条件分布

从分布层面看,CFG 等价于在隐式分布上做指数加权。把外推公式逐步推导一下:

Step 1. 把 $\epsilon$ 换成 score(差一个 $-\sigma_t$ 因子,符号无关结论): $$\hat s(x_t,c) \;=\; (1+w)\, s(x_t\mid c) \;-\; w\,s(x_t).$$ Why:DDPM 学的 $\epsilon$ 就是 $-\sigma_t \nabla_{x_t}\log p$。换成 score 形式更容易看清"对哪个分布积分"。
Step 2. 反推这个 $\hat s$ 是哪个对数密度的梯度: $$\hat s(x_t,c) \;=\; \nabla_{x_t}\Bigl[(1+w)\log p(x_t\mid c) - w\log p(x_t)\Bigr].$$ Why:梯度是线性算子,可以把系数提出来。
Step 3. 整理 log 表达式,写回分布: $$\tilde p(x\mid c) \;\propto\; p(x\mid c)^{\,1+w}\cdot p(x)^{-w} \;=\; p(x\mid c)\cdot\Bigl(\tfrac{p(x\mid c)}{p(x)}\Bigr)^{\!w}.$$ Why:把指数运算还原成连乘,得到一个隐式分布——CFG 采样事实上就是在这个分布上做 Langevin / probability flow。

把 $\bigl(\tfrac{p(x\mid c)}{p(x)}\bigr)^w$ 看作"对条件信号的指数放大"——$w$ 越大,越偏向"高条件似然 / 低无条件似然"的区域,也就是越典型、越像 prompt 描述的样本。这等价于把分布"温度"降低,采样集中到高密度 mode。

反过来看:$\frac{p(x\mid c)}{p(x)}$ 这一项本身就是 Bayes 里的"似然比"——它在贝叶斯统计中等价于证据强度。CFG 的直觉因此可以重新表述为:

"把每个样本的条件证据强度提高到 $w+1$ 次方再归一化采样"——证据越强的 $x$,越倾向于被采出来。

这也解释了为什么 $w$ 太大会"过曝/烤焦":当 $w \to \infty$,$\tilde p$ 退化成 $\delta$ 在"似然比最高那一点"上——只剩一张图,所有 prompt 都被采成同一个外观。多样性彻底崩塌。

💡 "温度旋钮"的类比:本章 blurb 写"靠调音量在质量和多样性之间换"——$w$ 就是那个音量。$w \uparrow$ 越听话、越浓缩、越饱和;$w \downarrow$ 越多样、越意外、也越偏题。Stable Diffusion 的"CFG 7"是工程上跑出来的甜蜜区。
CFG scale 扫描:分布如何被锐化 $w=0$(纯条件) 多样性高、prompt fidelity 一般 $w=3$(中等引导) SD 默认 7 附近,质量/多样性平衡 $w=15$(过强) collapse 到 mode,over-saturate / 烤焦 $w$ 越大,采样越集中到"最像 prompt"的几张图——少而精;过大则过曝/失真

八、把"无条件 + 条件"塞进一个网络的工程细节

8.1 条件 token 怎么"标记为空"?

在 SD 这类文本条件模型里,"$\varnothing$"通常实现为空字符串经 CLIP text encoder 编码后的向量——即把 prompt 替换为 "" 重新跑一次 text encoder。在 class-conditional 模型里,$\varnothing$ 是一个专门的"null class index"——比如本来有 1000 类,多加一个 id=1000 当作 null,embedding 表也多一行。

训练时的"条件 dropout"就是按 $p_{\text{drop}}$ 把上述 condition 替换成 $\varnothing$ 后再算 loss。注意几个关键细节:

  • 替换是 input 层面的,不是 attention mask 层面的。直接把 $c$ 换成 $\varnothing$,模型不需要任何架构改动;
  • dropout 是 per-sample 的,同一 batch 里有些样本有条件、有些没条件,模型必须同时学好两个分支;
  • 训练时不需要"两次前向"——只在采样时才需要分别跑 $c$ 和 $\varnothing$。训练阶段对计算图是无感的。

8.2 采样要跑两次前向

每个去噪步骤都要算 $\epsilon_\theta(x_t,t,c)$ 和 $\epsilon_\theta(x_t,t,\varnothing)$ 两次,再线性组合。这意味着 CFG 把单步采样成本翻倍。

实践中常把它打包进 batch:把 $(x_t, c)$ 和 $(x_t, \varnothing)$ 拼成 batch size 2 跑一次 forward,再 split 输出。代价仍是 2×,但内存友好——而且现代 GPU 在 batch=2 上的 throughput 几乎和 batch=1 相同。所以"CFG 让推理慢两倍"这个说法在 SD 这类已经被 batch 占满的场景里基本不成立,实际慢 1.3–1.5 倍。

有几种工业上常见的"省一半算力"技巧:

  • Guidance distillation(Meng et al. 2023):训一个新网络直接输出 $\hat\epsilon$,省掉那次 unconditional forward。SDXL Turbo / LCM 这一类都在用;
  • Self-attention guidance / PAG:用网络内部某种"模糊版预测"代替 unconditional 分支,避免显式跑第二次;
  • Interval guidance(Kynkäänniemi et al. 2024):只在中间时间步施加 CFG,开头结尾不施加,FID 提升、采样略快——这正好印证了开头讲师讲的"medium time steps 最关键"。

8.3 工程伪代码

# Classifier-Free Guidance: 训练 + 采样(伪代码)
# 训练:DDPM loss + 条件 dropout
def train_step(x0, c, model, p_drop=0.1):
    B = x0.size(0)
    t = torch.randint(0, T, (B,))                      # 采时间步
    eps = torch.randn_like(x0)                         # 采噪声
    xt = sqrt_alpha_bar[t] * x0 + sqrt_one_minus_alpha_bar[t] * eps

    # 关键一行:以概率 p_drop 把 condition 替换成 null
    mask = (torch.rand(B) < p_drop)
    c_in = c.clone()
    c_in[mask] = NULL_TOKEN                            # 文本: ""; 类别: null_class_id

    eps_pred = model(xt, t, c_in)                      # 同一个网络,可能拿到 c 也可能拿到 null
    loss = F.mse_loss(eps_pred, eps)
    return loss


# 采样:每步两次前向,线性外推
@torch.no_grad()
def cfg_sample(model, c, w=7.0, T=50, shape=(1,3,64,64)):
    x = torch.randn(shape)                             # x_T ~ N(0, I)
    for t in reversed(range(T)):
        t_b = torch.full((shape[0],), t, dtype=torch.long)

        # 两次前向:一次给 c,一次给 null
        eps_c   = model(x, t_b, c)                     # 条件分支
        eps_unc = model(x, t_b, NULL_TOKEN)            # 无条件分支

        # CFG 外推:把 eps 推向 c、远离 null
        eps_hat = (1 + w) * eps_c - w * eps_unc

        # 然后照常做 DDPM / DDIM 一步反向
        x = ddim_step(x, eps_hat, t)
    return x
🛠️ 实战 tips:
• $p_{\text{drop}}$ 用 0.1~0.2 都行;太高会损害条件性能,太低 unconditional 分支学不好。
• Stable Diffusion v1 的 $w=7.5$ 是当年 LAION 数据集 + CLIP encoder 上扫出来的甜蜜点。
• 不同 sampler 下的最优 $w$ 不一样,DDIM 和 DPM-Solver 通常可以用更小的 $w$ 拿到同等效果。
• Negative prompt 的实现就是把"$\varnothing$"换成"你不想要的 prompt":$\hat\epsilon = (1+w)\epsilon(c_+) - w\epsilon(c_-)$。

九、CFG 为什么是"工业标准"

把 CFG 和 Classifier Guidance 放一起对比一下:

维度 Classifier Guidance (2021) Classifier-Free Guidance (2022)
需要的网络数2(diffusion + noisy classifier)1(diffusion 本身兼任两者)
额外训练成本必须训"带噪分类器",每个条件域单独训在原训练循环里加一行 dropout,零额外成本
支持 open-vocabulary 文本?不行(CLIP 没在 $x_t$ 上训过)行(text encoder 任意接)
采样成本每步 1× diffusion + 1× classifier backprop每步 2× diffusion forward
实证 FID / IS / CLIP score提升明显同等或更好;几乎所有 SOTA 模型默认开启
代表系统ADM-G (Dhariwal & Nichol)Stable Diffusion / DALL·E 2 / Imagen / SDXL

对工业界来说,CFG 的胜利不是"理论更精妙",而是"训练 pipeline 不变、推理改两行、效果暴涨"——这是工程上 ROI 最高的一类 trick。

九点五、深入:Bayes 推导里几个被一带而过的微妙点

讲师在课堂上走 Bayes 走得很快,这里把几个值得停下来咀嚼的点拆开讲。

9.5.1 为什么 $\log p(c)$ 可以直接扔掉?

$p(c)$ 是先验类频,对 $x$ 来说是常数——求梯度 $\nabla_x$ 直接为零。但要注意:这不代表 $p(c)$ 在采样时不存在。它只是不参与"指方向"。如果两个类的 $p(c)$ 差很多(比如长尾类),你拿到的 $\nabla \log p(c\mid x)$ 在梯度方向上仍是正确的,但绝对幅度未必是你想要的"理想 guidance strength"——这就是为什么实践中总要乘一个 $\gamma$ 或 $w$ 来手动调强度。

9.5.2 $\nabla_x \log p(c\mid x)$ 在采样时是哪个 $x$?

读者最容易混淆的一点:classifier 接的是 $x_t$(带噪),不是 $x_0$(干净)。因为我们要把这个梯度加到 $\epsilon_\theta(x_t,t)$ 上一起跑下一步反向去噪,所以必须在同一空间 $x_t$ 上做。这就是 chunk #1317 那个"noisy classifier"问题的根源——分类器必须吃 $x_t$,否则梯度根本对不上 reverse step 的尺度。

9.5.3 guidance scale $\gamma$ 是怎么"凭空多出来"的?

严格 Bayes 推出的式子里没有 $\gamma$,是 $\gamma=1$ 的特殊情况。Dhariwal & Nichol 2021 经验上发现:把分类器梯度乘 $\gamma > 1$ 能极大提升 FID。直觉解释是:分类器学到的 $p_\phi(c\mid x_t)$ 通常比真实 $p(c\mid x_t)$ 更平(欠自信),乘 $\gamma$ 是在补这个 gap。从分布层面:guided 采样等价于从 $p(x\mid c) p(c\mid x)^{\gamma-1}$ 取样,$\gamma-1$ 就是对似然比的指数加权——这和 CFG 那个 $(p(x\mid c)/p(x))^w$ 是同一类数学结构。

ℹ️ 历史脚注:Classifier Guidance 的论文标题叫《Diffusion Models Beat GANs on Image Synthesis》(Dhariwal & Nichol 2021)。让 diffusion 第一次在 FID 上超过 BigGAN 的关键技巧就是 $\gamma > 1$ 的 classifier guidance。整个 diffusion 火起来,guidance 是头号功臣,比 DDPM 本身的发明还重要。

九点八、把 CFG 当成 reward-tuned sampling 的视角

有一个现代视角可以让 CFG 的本质更显然:CFG 就是"基于 reward 的采样修正"。

把 $\log p(c\mid x)$ 看成"$x$ 满足条件 $c$ 的得分"——也就是 reward。我们想从一个修正分布 $p_{\beta}(x) \propto p(x) \exp(\beta R(x))$ 中采样,其中 $R(x) = \log p(c\mid x)$,$\beta$ 是温度参数。对它求 score:

$$\nabla_x \log p_\beta(x) \;=\; \nabla_x \log p(x) \;+\; \beta\,\nabla_x R(x).$$

这就是 classifier guidance。而 CFG 是同样的事情、但用 $\log p(x\mid c) - \log p(x)$ 这个"隐式 reward"代替了显式 classifier——你不需要单独训一个 reward model,diffusion 本身的"条件减无条件"分支已经隐含了这个 reward。

这个视角让 CFG 和近年的 reward fine-tuning(DPOK、Diffusion-DPO、ReFL)连成了一条线:

  • CFG:inference-time 修正,reward 隐含在 conditional vs unconditional 分支差里;
  • Classifier guidance:inference-time 修正,reward 显式来自分类器;
  • RLHF for diffusion:training-time 修正,reward 来自人工偏好;

本质都是"无条件分布 + reward → 修正分布"。CFG 的便宜之处在于它不需要任何额外训练——reward 已经在你 dropout 训练时一并学到了。

十、回到课堂:把"反着跑"的悬念解开

本章开头讲师顺嘴的那句"go the opposite way, sarcastically",到这里就清楚了:

  • $\epsilon_\theta(x_t,t,c)$ 是"指向 prompt 的方向";
  • $\epsilon_\theta(x_t,t,\varnothing)$ 是"指向真实数据流形(但不挑 prompt)的方向";
  • CFG 把第一个减去第二个,留下的差就是"纯粹的 prompt 信号",然后把它放大 $w$ 倍加回去;
  • 所以你确实在"沿条件方向过冲地走"——就像 DDIM 是"在时间轴上跳着走",CFG 是"在条件轴上跳着走"。
🟡 教材外补充——CFG 为什么在 flow matching 里也能用:第 13–14 章学的 flow matching / rectified flow 同样有 $v_\theta(x_t,t,c)$ 和 $v_\theta(x_t,t,\varnothing)$ 两个分支。CFG 公式直接套到 velocity 上:$\hat v = (1+w) v(c) - w\,v(\varnothing)$。这就是 SD3、Flux 这一代模型上仍然有"CFG scale"slider 的原因——它和 DDPM 没有强耦合,只要你有"无条件 + 条件"两个分支就能用。

十点五、一个常被问的细节:CFG 训练里 $p_{\text{drop}}$ 到底怎么选?

$p_{\text{drop}}$ 这个超参看似不起眼,实际是决定 CFG 行为的关键之一。回忆训练目标:

  • 当样本被 dropout,模型在学 $\epsilon_\theta(x_t,t,\varnothing)$ → 无条件分支;
  • 当样本保留条件,模型在学 $\epsilon_\theta(x_t,t,c)$ → 条件分支。

所以 $p_{\text{drop}}$ 就是两个分支在训练样本上的占比。极端情况:

  • $p_{\text{drop}} = 0$:纯条件训练,模型完全不会做无条件 → CFG 用不了;
  • $p_{\text{drop}} = 1$:纯无条件训练,模型完全不看 $c$ → 退化成普通 unconditional diffusion;
  • $p_{\text{drop}} \in [0.1, 0.2]$:条件分支占 80%–90% 样本,无条件分支占 10%–20%。这是 Ho & Salimans 原论文和 SD 训练惯例。

为什么这么不对称?因为无条件分支"更容易学"——它只需要学好整体数据分布,不必区分细粒度 prompt。所以 10% 的样本就足够把它教会;剩下 90% 留给更难的条件分支。这是工程上"任务难易决定数据预算"的一个漂亮例子。

⚠️ 常见踩坑:自己训 CFG 时如果发现"开了 CFG 效果反而比 $w=0$ 差"——很可能是 $p_{\text{drop}}$ 设错了(比如忘加这一行,模型从没见过 $\varnothing$,无条件分支输出垃圾,再去减它就是在加噪声)。验证方法:单独跑一次 $w=-1$(纯无条件),看输出是否像"通用真实图像"。如果输出像噪声或纯黑,说明无条件分支没训出来。

十点八、CFG 引发的"心理学"现象:为什么 $w=7$ 这么受欢迎?

Stable Diffusion 默认 CFG=7.5,DALL·E 2 默认 5–10,Imagen 用 3–10。这些数字不是从理论推出来的,是用户偏好实验扫出来的甜蜜区。一个有意思的现象:

  • FID(看像不像真图)通常在 $w \in [1, 3]$ 最低;
  • CLIP score(看像不像 prompt)随 $w$ 单调上升;
  • 人类偏好评分在 $w \in [5, 10]$ 达到峰值——人比 FID 更看重"听话",但又不想要"烤焦"。

这暴露了一个本章的核心张力:FID 偏爱多样、自然;用户偏爱听话、典型;中间永远要权衡。"$w$ 旋钮"的存在让这个权衡留在推理时,而不是被锁在训练里——这是 CFG 工程上最优雅的地方:用户在网页上拖个 slider 就能改风格倾向,根本不用重训。

十一、嵌入源课讲解片段

CMU 10799 L7(chunk #1310 起):讲师从"能不能复用 unconditional 模型?"的核心问题开始,配上 Bayes 一步推到 classifier guidance。这一段是本章主源最精彩的 4 分钟。

十二、本章 Takeaways

  1. 条件生成的痛点是组合爆炸。讲师原话(chunk #1308):"for each type of condition, we will have to train a new model"——这条墙让"一个 condition 一个模型"路线难以扩展到开放世界。
  2. Bayes 是条件生成的"通用解码器"。$\log p(x\mid c) = \log p(x) + \log p(c\mid x) - \log p(c)$。对 $x$ 求导,常数消失,剩下"无条件 score + 判别器梯度"。讲师让你"tattoo Bayes' theorem"绝对不是开玩笑。
  3. Classifier Guidance 的代价是 noisy classifier。off-the-shelf 分类器看不懂 $x_t$,你必须按 noise schedule 重训一份,对每种条件单独训。对 open-vocabulary 文本几乎不可行。
  4. CFG 用"条件 dropout + 两次前向"绕开了上面所有问题。训练加一行代码,采样加一次 forward,得到的 $\hat\epsilon = (1+w)\epsilon(c) - w\epsilon(\varnothing)$ 等价于在 score 空间向条件方向外推。
  5. $w$ 是"音量旋钮"。$w=0$ 纯条件、典型多样;$w$ 大 prompt fidelity ↑、多样性 ↓、过大会"烤焦"。SD 工业经验值 $w \in [5,12]$。
  6. 分布层面 CFG = 锐化:等价于从 $p(x\mid c)^{1+w}/p(x)^w$ 采样,把 $\frac{p(x\mid c)}{p(x)}$ 这个"似然比"指数加权,集中到典型 mode。
  7. CFG 不依赖 diffusion 数学骨架,flow matching、rectified flow、consistency model 全都能套同样的外推公式——所以它从 2022 一直活到今天。

下一章我们会继续在 guidance 这条线上走——第 18 章聊 CFG 的变体与改进(rescaling、auto-guidance、interval guidance),看工程界怎么把"$w$ 调大"和"不烤焦"这对矛盾继续榨干。