SD3 论文精读 · 第二部分 · 第 4 节、5.2 节、附录 B.2

精读 SD3:模型结构(MM-DiT)

第一部分讲的是"网络要学什么":输入带噪声的 \(z_t\) 和时间 \(t\),输出速度。这一部分按论文原文的顺序,一段一段读第 4 节(Text-to-Image Architecture)、它引用的附录 B.2,以及验证这些设计的 5.2 节。每一节先给出原文位置和关键原句,再逐句解读:这句话在说什么、为什么这么做、数字从哪来、哪里容易读错。论文默认读者已经会的背景(注意力、LayerNorm、adaLN)放在灰框里,需要时再看。

建议的读法:左边开着论文 PDF(或本站的 中英对照版),右边开着这一页,读一段对一段。

论文阅读地图

这部分要读的全部原文位置

按论文顺序排列。页码按 arXiv 2403.03206v1 的 PDF。右栏是本页对应的精读节。

论文位置讲了什么本页
§4 第 1、2 段(p.4 右栏)模型要同时处理文字和图片;沿用 LDM,在预训练自编码器的潜空间里训练;文字用冻结的预训练模型编码,细节见附录 B.2精读 1
附录 B.2 "Latent Image Representation"(p.20)自编码器 \(E,D\);下采样 8;前向过程在潜空间做;潜变量归一化精读 2
附录 B.2 "Text Representation"(p.20 到 21)CLIP-L/14、OpenCLIP bigG/14、T5-v1.1-XXL;\(c_{vec}\in\mathbb R^{2048}\);\(c_{ctxt}\in\mathbb R^{154\times4096}\)精读 3
§4 "Multimodal Diffusion Backbone" 第 1 段(p.4)沿用 DiT;时间步和 \(c_{vec}\) 进调制;pooled 向量太粗,还需要序列 \(c_{ctxt}\)精读 4
§4 "Multimodal Diffusion Backbone" 第 2 段(p.4 到 5)加位置编码,2×2 切块,序列长度 \(\frac12h\cdot\frac12w\);文字和图片嵌入到同一维度后拼接精读 5
Fig. 2a、Fig. 2b 及图注(p.5)全部组件的总览;一个 MM-DiT 块的内部精读 1、4、6
§4,Fig. 2 下方第 1、2 段(p.5)按 DiT 做调制的注意力和 MLP;两种模态两套权重,等价于两个 Transformer 只在注意力处连起来精读 6
§4 最后一段(p.5)深度 \(d\):隐藏维度 \(64d\),MLP \(4\cdot64d\),头数 \(d\)精读 7
§5.2 引言(p.6 到 7)找到了训练方式之后,还要改进网络结构和图文表示精读 8
§5.2.1 + Table 3(p.7)+ Fig. 10(p.21)潜变量通道数 4、8、16 的取舍,选 16精读 8
§5.2.2 + Table 4(p.7)CogVLM 合成 caption,50/50 混合精读 9
§5.2.3 + Fig. 4(p.7 到 9)DiT、CrossDiT、UViT、MM-DiT(2 套、3 套权重)的比较精读 10
附录 B.3 "Optimization" 最后一句(p.21)每个文本编码器独立以 46.4% 置零,约 10% 的步数相当于无条件精读 3
先排一个雷:字母 \(d\) 有两个意思。§4 最后一段和 5.3 节里,\(d\) 是模型深度(MM-DiT 块的个数,比如 \(d=38\))。§5.2.1、附录 B.2、Table 3 里,\(d\) 是潜变量的通道数(比如 \(d=16\))。§4 骨干第 2 段又把通道数写成 \(c\)。Fig. 10 里两个同时出现:横轴 depth 是深度,"4/8/16 chn" 是通道数。另外 Fig. 2b 的调制参数 \(\epsilon\) 和第一部分的噪声 \(\epsilon\) 也只是撞了字母。
全流程动画

MM-DiT 一次前向,逐步拆开看(含 5.2.3 比较的其他结构)

默认是 SD3 的 MM-DiT,从三个输入到输出速度共 29 步。用"结构"下拉框可以切换到 §5.2.3 里一起比较的另外四种:三套权重的 MM-DiT、DiT、CrossDiT、UViT,看它们的全流程哪里不一样。每一步显示正在做的操作、张量的真实形状和论文对应位置。

手机上可以左右滑动画面。

全部 29 步(点任意一步跳过去)
    粉色是文字流(\(c\)),蓝色是图片流(\(x\)),绿色是条件向量 \(y\),灰色方框是操作。张量画成长条:高度大致代表 token 个数(按平方根缩放),宽度代表每个 token 的维数(按对数缩放)。形状里的 \(h=64d\) 是隐藏维度,\(N_x\) 是图片 token 数,154 是文字 token 数。标着"论文未写"的细节,是论文没有给出的实现选择,这里只说明位置,不作断言。
    精读 1 · §4 第 1、2 段 + Fig. 2a

    总体设定:网络吃什么、吐什么

    读完这两段,你应该能说出 SD3 网络的三个输入、一个输出,以及哪些部件是冻结的。

    原文 · §4 第 1 段

    "For text-conditional sampling of images, our model has to take both modalities, text and images, into account. We use pretrained models to derive suitable representations and then describe the architecture of our diffusion backbone."

    1. "take both modalities … into account":第一部分的速度网络只看 \(z_t\) 和 \(t\)。文生图要多一个条件,网络变成 \(v_\Theta(z_t,t,c)\),\(c\) 是 caption。"两种模态"就是文字和图片。
    2. "pretrained models to derive suitable representations":文字和图片都先交给别人训好的模型转换成数字表示,SD3 自己只训练中间的"扩散骨干"(diffusion backbone)。所以本节分两步讲:先讲表示(精读 2、3),再讲骨干(精读 4 到 7)。
    原文 · §4 第 2 段

    "Our general setup follows LDM (Rombach et al., 2022) for training text-to-image models in the latent space of a pretrained autoencoder. … we also … encode the text conditioning c using pretrained, frozen text models. Details can be found in Appendix B.2."

    1. "follows LDM":LDM 就是 Stable Diffusion 1 的论文。它的做法是"先用自编码器把图压小,在压小的空间里做扩散"。SD3 整体沿用这个框架,所以第一部分里的 \(x_0\) 其实是潜变量,不是像素。
    2. "pretrained, frozen text models":frozen 的意思是训练 SD3 时这些文本模型的参数不动。好处是省算力,而且它们的输出对同一句话永远一样,可以提前算好存起来(第三部分 5.3.1、附录 E.1)。
    3. "Details can be found in Appendix B.2":正文只有一句话,细节全在附录。所以要读懂这一节,必须跳到附录 B.2,下面两节就是读它。

    先看一眼全貌:Fig. 2a 的形状变化

    图 1 · 一张图在网络里的形状变化(按 Fig. 2a 和附录 B.2 的数字)

    每点一次"下一步",数据往后走一格。方块大小大致代表"有多少个数"。最后输出的速度和输入潜变量形状完全一样,因为采样时要做 \(z\leftarrow z+v\,dt\),两者要逐个数相加。
    Fig. 2a 从上往下怎么读
    • 左上:Caption 进 CLIP-G/14、CLIP-L/14、T5 XXL 三个编码器。两个 CLIP 的 pooled 输出(左边竖条 "Pooled")往下走;三个编码器的序列输出拼成右边那个 "77 + 77 tokens"、"4096 channel" 的矩形(精读 3)。
    • 左中:Pooled → MLP,Timestep → Sinusoidal Encoding → MLP,两者 ⊕ 得到 \(y\)(精读 4)。
    • 中间:序列矩形 → Linear → \(c\)(文字 token)。右上:Noised Latent → Patching → Linear → ⊕ Positional Embedding → \(x\)(图片 token,精读 5)。
    • 中下:\(c\)、\(x\)、\(y\) 进 MM-DiT-Block 1 到 \(d\)(精读 6)。
    • 右下:Modulation → Linear → Unpatching → Output。Output 是速度:§3 Rectified Flow 段说 "The network output directly parameterizes the velocity \(v_\Theta\)"。
    自测:SD3 训练时,哪些参数在更新,哪些不动?

    不动:自编码器(\(E\) 和 \(D\))、CLIP-L、OpenCLIP bigG、T5-XXL。更新:Fig. 2a 里所有紫色的层,也就是两个 MLP、几个 Linear、位置编码之外的投影,以及 \(d\) 个 MM-DiT 块。论文里的 "8B" 只数这些要训练的部分(精读 7)。

    精读 2 · 附录 B.2 "Latent Image Representation"

    图像怎么表示:潜空间

    读完这段,你应该能说清 \(E\)、\(D\)、\(h,w,d\) 的意思,前向过程在哪个空间做,以及为什么要归一化。

    原文 · 附录 B.2 第 1 段(p.20)

    "We follow LDM … and use a pretrained autoencoder to represent RGB images \(X\in\mathbb R^{H\times W\times3}\) in a smaller latent space \(x=E(X)\in\mathbb R^{h\times w\times d}\). We use a spatial downsampling factor of 8, such that \(h=\frac H8\) and \(w=\frac W8\), and experiment with different values for \(d\) in Section 5.2.1."

    1. "pretrained autoencoder":自编码器 = 编码器 \(E\)(压缩)+ 解码器 \(D\)(还原),训练目标是"压了再解要和原图像"。它是在 SD3 之前单独训好的。
    2. "\(X\in\mathbb R^{H\times W\times3}\)":大写 \(X\) 是像素图,高 \(H\)、宽 \(W\)、RGB 三通道。小写 \(x\) 是压缩后的潜变量,高 \(h\)、宽 \(w\)、\(d\) 个通道。
    3. "downsampling factor of 8":每 8×8 个像素变成潜变量的 1 个格子。1024×1024 的图变成 128×128 的格子。每个格子有 \(d\) 个数,SD3 最终用 \(d=16\)。
    4. "experiment with different values for \(d\)":\(d\) 选多少是一个实验问题,留到 5.2.1 节回答(精读 8)。
    图 2 · 编码器把 8×8 个像素压成 1 个格子,每个格子有 \(d\) 个数
    左边是一张 64×64 的玩具图。编码器每次处理一块 8×8 像素(亮框),变成右边的一个格子,格子里的竖条示意 \(d\) 个数。读数里的重建 FID、PSNR 来自 Table 3;压缩倍数是 \(8\cdot8\cdot3/d\),我算的。
    原文 · 附录 B.2 第 1 段(续)

    "We always apply the forward process from Equation 2 in the latent space, and when sampling a representation \(x\) via Equation 1, we decode it back into pixel space \(X=D(x)\) … We follow Rombach et al. (2022) and normalize the latents by their mean and standard deviation, which are globally computed over a subset of the training data."

    1. "forward process from Equation 2 in the latent space":Eq. 2 是 \(z_t=a_tx_0+b_t\epsilon\),对 RF 就是 \(z_t=(1-t)x_0+t\epsilon\)。这里明确了 \(x_0\) 是潜变量 \(E(X)\),噪声 \(\epsilon\) 的形状也是 \(h\times w\times d\)。
    2. "sampling … via Equation 1 … decode it back":Eq. 1 是 ODE \(dy_t=v_\Theta\,dt\)。生成时在潜空间里从纯噪声走到 \(t=0\),最后只调用一次 \(D\) 把潜变量变回图。
    3. "normalize the latents by their mean and standard deviation":在一部分训练数据上统计潜变量的均值和标准差,每个潜变量减均值、除标准差。原因:路径 \((1-t)x_0+t\epsilon\) 默认 \(x_0\) 和 \(\epsilon\) 数值尺度相当。如果潜变量本身数值很大,同一个 \(t\) 下"信号多少、噪声多少"就和设计的不一样了,第一部分讲的 SNR 和 \(t\) 的对应关系也会失效。
    为什么值得这么做:1024×1024×3 是约 315 万个数,128×128×16 是约 26 万个数,少了 12 倍。像素里很多细节人眼看不出来,交给自编码器处理,扩散骨干只管"画什么"。代价是:自编码器还原不出来的东西,扩散模型怎么学也画不出来。这个"天花板"论证在 5.2.1 节(精读 8)。
    自测:训练 SD3 时,loss 是在像素上算还是在潜变量上算?

    在潜变量上。网络预测的速度和目标速度 \(\epsilon-x_0\) 都是 \(h\times w\times16\) 的潜变量形状。训练时根本不需要解码器 \(D\),只有生成时最后一步才用。

    精读 3 · 附录 B.2 "Text Representation" + B.3

    文字怎么表示:三个编码器,两种输出

    读完这段,你应该能一步步推出 \(c_{vec}\in\mathbb R^{2048}\) 和 \(c_{ctxt}\in\mathbb R^{154\times4096}\) 的每个数字。

    原文 · 附录 B.2 第 2 段(p.20)

    "… for all experiments, we use a combination of CLIP models and a encoder-decoder text model. Specifically, we encode c with the text encoders of both a CLIP L/14 model … as well as an OpenCLIP bigG/14 model …"

    1. "CLIP models":CLIP 是用"图片和配文"对比学习训练的:匹配的图文向量靠近,不匹配的远离。所以它的文字向量天然知道"这句话对应什么画面"。CLIP 有图像编码器和文本编码器,这里只用文本编码器。L/14 和 bigG/14 是两个大小不同的版本。
    2. "a encoder-decoder text model":指 T5,一个只在文字上训练的语言模型,比 CLIP 更懂长句、复杂描述和要拼写的字。SD3 只用它的编码器部分。
    原文 · 附录 B.2 第 2 段(续)

    "We concatenate the pooled outputs, of sizes 768 and 1280 respectively, to obtain a vector conditioning \(c_{vec}\in\mathbb R^{2048}\). We also concatenate the penultimate hidden representations channel-wise to a CLIP context conditioning \(c^{CLIP}_{ctxt}\in\mathbb R^{77\times2048}\)."

    1. "pooled outputs, of sizes 768 and 1280":编码器能把整句话压成一个向量,叫 pooled 输出。CLIP-L 的是 768 维,bigG 的是 1280 维。首尾相接:\(768+1280=2048\),这就是 \(c_{vec}\)。它代表整句话的大意,但细节(谁是红的、谁在左边)都糊在一起了。
    2. "penultimate hidden representations":penultimate 是"倒数第二层"。编码器给每个 token 一个向量,CLIP 最多处理 77 个 token(短句补齐到 77)。论文没解释为什么取倒数第二层(SDXL 也是这么取的)。
    3. "channel-wise":按通道方向拼:同一个 token 的两个向量(768 维和 1280 维)首尾相接,所以是 \(77\times(768+1280)=77\times2048\)。token 数不变。
    原文 · 附录 B.2 第 2 段(末)

    "Next, we encode c also to the final hidden representation, \(c^{T5}_{ctxt}\in\mathbb R^{77\times4096}\), of the encoder of a T5-v1.1-XXL model. Finally, we zero-pad \(c^{CLIP}_{ctxt}\) along the channel axis to 4096 dimensions to match the T5 representation and concatenate it along the sequence axis with \(c^{T5}_{ctxt}\) to obtain the final context representation \(c_{ctxt}\in\mathbb R^{154\times4096}\). These two caption representations, \(c_{vec}\) and \(c_{ctxt}\), are used in two different ways as described in Section 4."

    1. "final hidden representation … T5-v1.1-XXL":T5 取最后一层,也是 77 个 token,每个 4096 维。
    2. "zero-pad … to 4096":CLIP 部分只有 2048 维,右边补 2048 个 0,变成 \(77\times4096\),和 T5 一样宽。补零不加信息,只为形状对齐。
    3. "concatenate it along the sequence axis":这次按 token 方向(上下)拼:\(77+77=154\) 个 token,每个 4096 维。Fig. 2a 里 "77 + 77 tokens" 就是这个意思。
    4. "used in two different ways as described in Section 4":伏笔。\(c_{vec}\) 进调制(精读 4),\(c_{ctxt}\) 作为 token 进注意力(精读 5、6)。
    图 3 · 按上面三段原文,一步步拼出 \(c_{vec}\) 和 \(c_{ctxt}\)

    矩形的宽代表维数,高代表 token 个数,比例按真实数字画。对照 Fig. 2a 左上角看,是同一张图。
    原文 · 附录 B.3 "Optimization" 最后一句(p.21)

    "For unconditional diffusion guidance …, we set the outputs of each of the three text encoders independently to zero with a probability of 46.4%, such that we roughly train an unconditional model in 10% of all steps."

    1. "unconditional diffusion guidance":即 classifier-free guidance(CFG):采样时同时算"有文字"和"没文字"两个速度,往"有文字"的方向多推一点,图会更贴合文字。所以训练时网络也得会处理"没文字"的输入。
    2. "independently … 46.4% … 10% of all steps":三个编码器各自独立地以 46.4% 的概率被置零,三个同时为零的概率是 \(0.464^3\approx0.0999\),约 10%。好处是推理时可以只用一部分编码器(比如去掉 T5 省显存),第三部分 5.3.3 节 "Flexible Text Encoders" 讲。注意 5.3.3 节写的是 46.3%,和这里差 0.1,是论文里的小出入。
    自测:如果只用 \(c_{vec}\),"一只红猫和一只蓝狗"可能出什么问题?

    \(c_{vec}\) 把整句压成一个向量,"红、蓝、猫、狗"混在一起,模型可能画成蓝猫红狗。\(c_{ctxt}\) 保留了每个 token,模型才能把"红"绑定到"猫"上。这就是精读 4 里原文说 pooled 向量 "retains only coarse-grained information" 的意思。

    精读 4 · §4 "Multimodal Diffusion Backbone" 第 1 段 + Fig. 2b 上半

    沿用 DiT:时间和整句大意走"调制"

    读完这段,你应该能说清 DiT 的调制是什么,\(y\) 怎么来的,以及为什么光有调制不够。

    原文 · §4 骨干第 1 段(p.4)

    "Our architecture builds upon the DiT (Peebles & Xie, 2023) architecture. DiT only considers class conditional image generation and uses a modulation mechanism to condition the network on both the timestep of the diffusion process and the class label. Similarly, we use embeddings of the timestep \(t\) and \(c_{vec}\) as inputs to the modulation mechanism."

    背景:一个 Transformer 块(论文默认你会)
    Transformer 处理一串 token(每个是一个向量)。一个块 = 注意力 + MLP,前面各有 LayerNorm,后面各有残差:\(h\leftarrow h+\text{Attn}(\text{LN}(h))\),\(h\leftarrow h+\text{MLP}(\text{LN}(h))\)。LayerNorm 把每个 token 的向量减均值、除标准差;MLP 是每个 token 自己算的两层全连接;残差让每块只在原来基础上"加一点修改"。注意力是唯一让不同 token 交换信息的地方,下一节详细讲。
    1. "builds upon the DiT":DiT(Diffusion Transformer)是把扩散模型的 UNet 换成 Transformer 的工作。SD1、SDXL 用的都是 UNet,SD3 改用 Transformer 一族。
    2. "DiT only considers class conditional image generation":DiT 原本在 ImageNet 上做"给一个类别(比如 207 号:金毛犬)画一张图",条件只是一个类别编号,没有句子。
    3. "a modulation mechanism":DiT 的调制叫 adaLN:LayerNorm 之后,不直接往下走,先乘一个向量、加一个向量,这两个向量由条件算出来。条件不同,每一层的"放大哪些特征、偏移多少"就不同。
    4. "we use embeddings of the timestep \(t\) and \(c_{vec}\)":SD3 把 DiT 的"类别"换成了 \(c_{vec}\)(整句大意)。Fig. 2a 左下:\(t\) 先做正弦编码再过 MLP,\(c_{vec}\) 过另一个 MLP,两者相加得到 \(y\)。
    图 4 · 拖动 \(t\):时间编码变了,调制把特征"拉伸、平移、开关"
    上面一行是 \(t\) 的正弦编码(32 维,颜色表示正负),用的是 Transformer 和 DiT 的标准公式:一组不同频率的 sin、cos,每个 \(t\) 有独特的"指纹"。下面灰色是某个 token 经过 LayerNorm 的特征分布,蓝色是调制后的 \(\alpha\cdot\text{LN}+\beta\),右边是门 \(\gamma\)。\(\alpha,\beta,\gamma\) 随 \(t\) 怎么变是我编的示意,真实网络里由学到的线性层决定。

    Fig. 2b 里的六个调制参数

    Fig. 2b 最上方:\(y\) → SiLU → Linear,分出每一块、每个模态各六个向量(图里左边带下标 \(c\) 的给文字流,右边带下标 \(x\) 的给图片流):

    参数在 Fig. 2b 的位置作用
    \(\alpha,\ \beta\)第一个 Layernorm 下面 "Mod: \(\alpha\cdot\bullet+\beta\)"注意力之前缩放、平移
    \(\gamma\)注意力输出的 Linear 下面那个 ∗门:注意力的结果加回残差前乘多少
    \(\delta,\ \epsilon\)第二个 Layernorm 下面 "Mod: \(\delta\cdot\bullet+\epsilon\)"MLP 之前缩放、平移
    \(\zeta\)MLP 下面那个 ∗门:MLP 的结果加回残差前乘多少
    原文 · §4 骨干第 1 段(末)

    "However, as the pooled text representation retains only coarse-grained information about the text input (Podell et al., 2023), the network also requires information from the sequence representation \(c_{ctxt}\)."

    1. "retains only coarse-grained information":调制是对所有 token 统一乘、统一加,它只能传"全局"信息:现在是第几步、整句话大概是什么。"左上角画一只红猫"这种和位置、词与词关系有关的细节,调制传不了。
    2. "also requires … \(c_{ctxt}\)":所以 154 个文字 token 必须以 token 的身份进入网络,通过注意力和图片 token 交换信息。怎么进,就是下一段和精读 6 要讲的。
    自测:如果某一块的 \(\gamma\) 全是 0,这块的注意力还起作用吗?

    不起作用。注意力输出乘 \(\gamma\) 才加回残差,\(\gamma=0\) 等于什么都没加。因为 \(\gamma\) 由 \(y\) 算出,网络可以学会在某些 \(t\)、某些句子下开,另一些情况下关。

    精读 5 · §4 "Multimodal Diffusion Backbone" 第 2 段

    切块:图片和文字都变成一串 token

    读完这段,你应该能算出任意分辨率下的 token 数,并说清"common dimensionality"和"concatenate"指什么。

    原文 · §4 骨干第 2 段(p.4 到 5)

    "We construct a sequence consisting of embeddings of the text and image inputs. Specifically, we add positional encodings and flatten 2 × 2 patches of the latent pixel representation \(x\in\mathbb R^{h\times w\times c}\) to a patch encoding sequence of length \(\frac12\cdot h\cdot\frac12\cdot w\). After embedding this patch encoding and the text encoding \(c_{ctxt}\) to a common dimensionality, we concatenate the two sequences."

    1. "a sequence consisting of embeddings of the text and image inputs":目标是一串 token,前一部分是文字,后一部分是图片。Transformer 只认"一串向量",没有"二维"的概念。
    2. "flatten 2 × 2 patches":把潜变量里相邻的 2×2 个格子打包成一个 token。每个 token 里有 \(2\cdot2\cdot16=64\) 个数。为什么不是每格一个 token?因为注意力的计算量和长度的平方成正比,打包后长度变成 1/4,计算量变成 1/16。这是 DiT 的做法,5.3.3 节也说 "We train on 2 × 2 patches"。
    3. "\(x\in\mathbb R^{h\times w\times c}\)":这里通道数写成了 \(c\)(附录 B.2 写成 \(d\)),同一个东西。也别和 caption 的 \(c\) 混淆。
    4. "length \(\frac12\cdot h\cdot\frac12\cdot w\)":1024×1024 → 潜变量 128×128 → \(64\times64=4096\) 个 token。
    5. "add positional encodings":排成一串之后,网络分不清哪个 token 原来在左上、哪个在右下,所以给每个 token 加上一个表示坐标的向量。具体是 2D 频率编码,换分辨率和长宽比时怎么改在 5.3.2 节(第三部分)。
    6. "embedding … to a common dimensionality":图片 token 是 64 维,文字 token 是 4096 维,各用一个线性层映射到同样的宽度(隐藏维度 \(64d\),精读 7)。Fig. 2a 里图片那路的 "Linear" 和文字那路的 "Linear" 就是这两个。
    7. "we concatenate the two sequences":154 个文字 token + 4096 个图片 token,拼成 4250 个 token 的一串。但注意下一段马上说两部分用两套权重,拼接只发生在注意力里(精读 6)。
    图 5 · 切块并排成一串(玩具:8×8 的潜变量)
    8×8 的潜变量按 2×2 切成 16 块,按行排成一串。每个 token 下面的(行,列)就是位置编码要告诉网络的信息。
    图片分辨率潜变量图片 token+ 文字 token
    256×25632×32×16256154
    512×51264×64×161024154
    1024×1024128×128×164096154

    分辨率翻倍,图片 token 变 4 倍,注意力计算量约变 16 倍。这就是 5.3.2 节先在 256×256 上预训练、最后才微调到 1024 的原因。

    自测:一张 768×1344 的图有多少个图片 token?

    潜变量 \(96\times168\),切 2×2 后 \(48\times84=4032\) 个。和 1024×1024 的 4096 个差不多,这正是第三部分"像素总数约等于 \(S^2\)"的分桶思路。

    精读 6 · Fig. 2 下方第 1、2 段 + Fig. 2b

    MM-DiT:两套权重,一次联合注意力

    读完这段,你应该能对着 Fig. 2b 讲出一个块里每个方框,并说清"等价于两个 Transformer"是什么意思。

    原文 · §4,Fig. 2 下方第 1 段(p.5)

    "… we concatenate the two sequences. We then follow DiT and apply a sequence of modulated attention and MLPs."

    "modulated attention and MLPs" 就是精读 4 的调制版 Transformer 块。要读懂这一节,先补一个背景:注意力。

    背景:注意力(论文默认你会)
    每个 token 用三个矩阵算出三个向量:Query(我想找什么)、Key(我有什么)、Value(我给出的内容)。token \(i\) 的 Q 和每个 token \(j\) 的 K 做点积,除以 \(\sqrt{d_k}\),softmax 变成加起来为 1 的权重,再按权重把所有 V 加起来:\(\operatorname{softmax}(QK^\top/\sqrt{d_k})V\)。点积大 = "对上了" = 多听它。多头注意力就是把向量切成几份,各做一次。下面的图可以点不同的 token 看它"听谁"。
    图 6 · 点一个 token,看它听谁的(玩具数字,Q、K 都是 2 维)
    左边细箭头是每个 token 的 K,粗箭头是选中 token 的 Q。右边是 \(q\cdot k_j/\sqrt2\) 和 softmax 后的权重,真实计算。文字 token(粉)和图片 token(蓝)在同一次注意力里,图片块可以直接"听"文字,这就是联合注意力。
    原文 · §4,Fig. 2 下方第 2 段(p.5)

    "Since text and image embeddings are conceptually quite different, we use two separate sets of weights for the two modalities. As shown in Figure 2b, this is equivalent to having two independent transformers for each modality, but joining the sequences of the two modalities for the attention operation, such that both representations can work in their own space yet take the other one into account."

    1. "conceptually quite different":文字 token 来自 T5/CLIP,图片 token 来自自编码器的潜变量,数值分布、含义完全不同。让同一套矩阵同时处理两者是强人所难。
    2. "two separate sets of weights":Fig. 2b 左列(\(c\))和右列(\(x\))结构一模一样,但每个方框各有自己的参数:各自的 Layernorm、调制(\(\alpha_c\) 和 \(\alpha_x\) 分开)、算 Q/K/V 的 Linear、注意力之后的 Linear、MLP。
    3. "equivalent to having two independent transformers":把 Fig. 2b 中间的 Attention 盖住,左右两列就是两个互不相干的 Transformer。
    4. "joining the sequences … for the attention operation":唯一的连接点是 Attention:左列的 Q、K、V 和右列的 Q、K、V 在 token 方向拼起来(图里 ⊙),做一次注意力。于是每个文字 token 能听所有图片 token,每个图片 token 也能听所有文字 token。出来后按位置拆开,各回各的 Linear。
    5. "work in their own space yet take the other one into account":两全:各用各的权重(自己的空间),又在注意力里互相看见。而且文字 token 也被逐层更新(文字的意思可以随"画到哪了"而调整),这是和交叉注意力最大的区别(精读 10)。
    图 7 · 注意力矩阵:每一格是"行 token 听列 token"
    玩具:3 个文字 token(粉)、6 个图片 token(蓝)。涂色的格子是这种结构会算的注意力,颜色表示那一行的 Q 用哪套权重算。切换三种结构对比:MM-DiT 和 DiT 的矩阵一样满,区别在权重分两套;CrossDiT 没有文字行,文字不被更新。
    对着 Fig. 2b 从上往下走一遍(以图片流为例,文字流完全对称)
    • \(y\) → SiLU → Linear → 六个调制向量 \(\alpha_x,\beta_x,\gamma_x,\delta_x,\epsilon_x,\zeta_x\)。
    • \(x\) → Layernorm → Mod: \(\alpha_x\cdot\bullet+\beta_x\) → Linear 得到 Q、K、V →(可选)Q、K 各过 RMS-Norm。
    • 和文字流的 Q、K、V 拼接 → Attention → 拆出图片部分 → Linear → 乘 \(\gamma_x\) → 加回残差(⊕)。
    • → Layernorm → Mod: \(\delta_x\cdot\bullet+\epsilon_x\) → MLP → 乘 \(\zeta_x\) → 加回残差。
    • 图注:"Concatenation is indicated by ⊙ and element-wise multiplication by ∗. The RMS-Norm for Q and K can be added to stabilize training runs." 后半句的 QK-norm 在 5.3.2 节(第三部分)。
    自测:一个 MM-DiT 块里,文字和图片 token 在哪几个操作上"用的是同一个东西"?

    只有注意力那一步的 softmax 是在拼起来的整串上一起做的,以及两者都用同一个 \(y\) 算调制(但通过不同的 Linear 得到 \(\alpha_c\) 和 \(\alpha_x\))。其余所有层的权重都是两套。

    精读 7 · §4 最后一段

    模型大小:只用一个旋钮 \(d\)

    读完这段,你应该能从 \(d\) 推出隐藏维度、头数,并粗算出 2B 和 8B。

    原文 · §4 最后一段(p.5)

    "For our scaling experiments, we parameterize the size of the model in terms of the model's depth \(d\), i.e. the number of attention blocks, by setting the hidden size to \(64\cdot d\) (expanded to \(4\cdot64\cdot d\) channels in the MLP blocks), and the number of attention heads equal to \(d\)."

    1. "For our scaling experiments":第三部分要画"模型越大 loss 越低"的曲线,需要一个单一的大小旋钮。
    2. "depth \(d\), i.e. the number of attention blocks":\(d\) = Fig. 2a 里 MM-DiT-Block 的个数。
    3. "hidden size to \(64\cdot d\)":每个 token 在网络内部的宽度。\(d=24\) 时 1536,\(d=38\) 时 2432。精读 5 里"common dimensionality"就是这个数。
    4. "\(4\cdot64\cdot d\) channels in the MLP":MLP 中间层是隐藏维度的 4 倍,Transformer 的常规设置。
    5. "number of attention heads equal to \(d\)":隐藏维度 \(64d\) 切成 \(d\) 份,每个头固定 64 维。
    图 8 · 拖动深度 \(d\),看模型尺寸(参数是我按 Fig. 2b 粗估)
    粗估:每块每个模态有 Q、K、V、输出四个 \(h\times h\) 矩阵(\(4h^2\))、MLP(\(8h^2\))、调制(\(y\) 到六个向量,\(6h^2\)),共 \(18h^2\);两个模态 \(36h^2\);乘以 \(d\)。不含输入输出层和文本编码器。结果 \(d=24\) 约 2.0B,\(d=38\) 约 8.1B,和论文的 "2B (d=24)"(Fig. 5 图注)、"8B"(Table 5 的 depth=38)对得上。竖线是论文训练过的深度。
    注意宽度也跟着变:加深的同时也在加宽,所以参数约正比于 \(d^3\):\(d\) 翻倍,参数约 8 倍。8B 只是 MM-DiT 本身,推理时还要加冻结的 T5-XXL(4.7B,5.3.3 节)、两个 CLIP 和自编码器。论文没有给每个深度的参数表,上面的粗估是我算的。
    自测:\(d=30\) 的模型隐藏维度、头数、MLP 宽度各是多少?

    隐藏维度 \(64\times30=1920\),30 个头(每头 64 维),MLP 中间 \(4\times1920=7680\)。粗估参数 \(36\times1920^2\times30\approx4.0\)B。

    精读 8 · §5.2 引言 + §5.2.1 + Table 3 + Fig. 10

    自编码器:通道数越多越好吗

    读完这段,你应该能说清"天花板"论证、Table 3 每一行的意思,以及为什么 Fig. 10 是选 16 通道的关键证据。

    原文 · §5.2 引言(p.6 到 7)

    "… the final performance of our algorithm depends not only on the training formulation, but also on the parameterization via a neural network and the quality of the image and text representations we use. In the following sections, we describe how we improve all these components before scaling our final method in Section 5.3."

    1. "not only on the training formulation":5.1 节(第一部分)只解决了"怎么训练"(RF + logit-normal)。
    2. "parameterization via a neural network … image and text representations":还剩三样:网络结构(5.2.3)、图像表示(5.2.1)、文字表示(5.2.2)。5.2 的三个小节就是逐个改进它们,然后 5.3 才放大。所以 5.2 是"配方定稿"。
    原文 · §5.2.1(p.7)

    "The reconstruction quality of this autoencoder provides an upper bound on the achievable image quality after latent diffusion training. … we find that increasing the number of latent channels \(d\) significantly boosts reconstruction performance, see Table 3. Intuitively, predicting latents with higher \(d\) is a more difficult task, and thus models with increased capacity should be able to perform better for larger \(d\) … We confirm this hypothesis in Figure 10 …"

    1. "upper bound on the achievable image quality":天花板论证。扩散模型就算完美地生成了潜变量,最后也要经过解码器 \(D\)。\(D\) 还原不了的细节,最终的图里就不会有。
    2. "increasing the number of latent channels \(d\) significantly boosts reconstruction":见下表,4 → 8 → 16 通道,四个指标全部单调变好。
    3. "predicting latents with higher \(d\) is a more difficult task":代价。通道多了,潜变量里信息更多,扩散模型要学的分布更复杂。
    4. "models with increased capacity should be able to perform better for larger \(d\)":作者的假设:小模型吃不下 16 通道,大模型吃得下。
    5. "We confirm this hypothesis in Figure 10":Fig. 10 在附录 p.21,横轴是 flow 模型的深度,三条线是 4、8、16 通道,纵轴是生成图的 FID。图注:"the flow model trained on the 16-channel autoencoder space needs more model capacity … At depth \(d=22\), the gap between 8-chn and 16-chn becomes negligible. We opt for the 16-chn model as we ultimately aim to scale to much larger model sizes."
    Table 3(下采样 \(f=8\))4 通道8 通道16 通道
    FID ↓(重建图和原图的分布距离)2.411.561.06
    Perceptual Similarity ↓(感知距离)0.850.680.45
    SSIM ↑(结构相似度,最大 1)0.750.790.86
    PSNR ↑(信噪比,dB)25.1226.4028.62
    压缩倍数 \(8\cdot8\cdot3/d\)(我算的)48×24×12×
    两种 FID 别混:Table 3 的 FID 是重建 FID,比较"原图"和"压了再解的图",衡量自编码器。Fig. 10 的 FID 是生成 FID,衡量整个扩散模型画得好不好。Table 3 只说明天花板高,Fig. 10 才说明大模型能不能够到天花板。SD1、SDXL 用的是 4 通道。
    自测:如果有人只看 Table 3 就说"16 通道肯定最好",哪里不对?

    Table 3 只说明重建更好。Fig. 10 显示小模型在 16 通道上的生成 FID 反而更差,到深度 22 左右才追平 8 通道。选 16 的理由是"我们要做很大的模型",这是一个押注规模的选择。

    精读 9 · §5.2.2 + Table 4

    合成 caption:一半原始,一半 CogVLM

    读完这段,你应该能说清为什么要合成 caption、为什么是 50/50、Table 4 每一行怎么读。

    原文 · §5.2.2(p.7)

    "Betker et al. (2023) demonstrated that synthetically generated captions can greatly improve text-to-image models trained at scale. This is due to the oftentimes simplistic nature of the human-generated captions … which overly focus on the image subject and usually omit details describing the background or composition of the scene, or, if applicable, displayed text."

    1. "Betker et al. (2023)":这是 DALL-E 3 的论文,它的主要发现就是"重写训练配文"。
    2. "overly focus on the image subject":网上的配文通常只说主体("一只猫"),不写背景、构图、画面里的文字。模型从没见过"详细描述 ↔ 画面"的对应,就学不会听复杂的提示。
    原文 · §5.2.2(续)

    "… use an off-the-shelf, state-of-the-art vision-language model, CogVLM, to create synthetic annotations … As synthetic captions may cause a text-to-image model to forget about certain concepts not present in the VLM's knowledge corpus, we use a ratio of 50 % original and 50 % synthetic captions."

    1. "CogVLM":一个看图说话的模型,给每张训练图写一段详细描述。
    2. "forget about certain concepts not present in the VLM's knowledge corpus":CogVLM 不认识的东西(比如某些专有名词、人物、作品名),它写的配文里就不会出现。只用合成配文,模型就学不到这些词。保留一半原始配文是为了留住它们。
    3. "50 % original and 50 % synthetic":论文没有比较其他比例,50/50 是一个合理的折中,不是实验选出来的最优值。
    图 9 · Table 4:GenEval 各项成功率(%)
    原文实验设置:"we train two \(d=15\) MM-DiT models for 250k steps, one on only original captions and the other on the 50/50 mix"。GenEval 用检测模型自动判断生成图是否满足要求(物体个数、颜色、位置等)。
    1. 总分 43.27 → 49.78。
    2. 涨得最多:颜色归属 11.75 → 24.75(翻倍多),位置 6.50 → 18.00,两个物体 41.41 → 52.53,计数 33.44 → 41.56。这些都要求理解句子结构(哪个颜色属于哪个物体、谁在谁左边),正是详细配文教得会的。
    3. 略降:颜色 71.54 → 68.09,单个物体 95.00 → 93.75。幅度小,单个物体本来就接近满分。
    证据强度:两个小模型各训一次,只看 GenEval 一个自动指标,没有随机种子重复。足够支持"混合比只用原始好",但不能说明 50/50 最优。
    自测:为什么不全用合成 caption?

    CogVLM 不认识的概念在合成配文里会消失,模型就学不到这些词和画面的对应,等于"忘掉"它们。保留一半原始配文可以留住这些概念。

    精读 10 · §5.2.3 + Fig. 4

    骨干网络比较:为什么是 MM-DiT

    读完这段,你应该能说清参赛的五种结构各是什么、在比什么,以及 Fig. 4 支持了哪些结论、没支持哪些。

    原文 · §5.2.3 第 1 段(p.7)

    "… we follow the experimental setup from Section 5.1 and compare text-to-image performance on CC12M of DiT, CrossDiT (DiT but with cross-attending to the text tokens instead of sequence-wise concatenation (Chen et al., 2023)) and our MM-DiT. For MM-DiT, we compare models with two sets of weights and three sets of weights, where the latter handles the CLIP and T5 tokens … separately. Note that DiT (w/ concatenation of text and image tokens as in Section 4) can be interpreted as a special case of MM-DiT with one shared set of weights for all modalities. Finally, we consider the UViT architecture as a hybrid between the widely used UNets and transformer variants."

    1. "follow the experimental setup from Section 5.1":和第一部分 5.1 一样的小规模设置(附录 B.3:batch 1024、AdamW、学习率 \(10^{-4}\)),这里只用 CC12M 一个数据集。
    2. "DiT":文字和图片 token 拼成一串,一套权重。原文点明它就是"只有一套权重的 MM-DiT"。
    3. "CrossDiT … cross-attending to the text tokens":Chen et al. (2023) 是 PixArt-α。图片 token 先自注意力,再做一次"图片问、文字答"的交叉注意力。文字只提供 K、V,从头到尾不被更新。SD1、SDXL 的 UNet 也是交叉注意力的思路。
    4. "two sets of weights and three sets of weights":两套 = 文字一套、图片一套(精读 6)。三套 = 把文字再拆成 CLIP 一套、T5 一套。
    5. "UViT … a hybrid between the widely used UNets and transformer variants":Hoogeboom et al. (2023) 的结构,UNet 的骨架里放 Transformer 块。
    原文 · §5.2.3 第 2 段(p.9)

    "We analyze the convergence behavior of these architectures in Figure 4: Vanilla DiT underperforms UViT. The cross-attention DiT variant CrossDiT achieves better performance than UViT, although UViT seems to learn much faster initially. Our MM-DiT variant significantly outperforms the cross-attention and vanilla variants. We observe only a small gain when using three parameter sets instead of two (at the cost of increased parameter count and VRAM usage), and thus opt for the former option for the remainder of this work."

    1. "convergence behavior … Figure 4":Fig. 4 画的是随训练步数(到 500k 步)的变化:左图验证 loss(图里写 "average over levels 1-7",即在 \(t\) 的几个等距档位上分别算 loss 再平均),右图左轴 FID、右轴 CLIP score。图注:"MM-DiT performs favorably across all metrics"。
    2. "Vanilla DiT underperforms UViT":一套权重硬塞两种模态,效果最差的一档。
    3. "CrossDiT … better than UViT, although UViT … faster initially":交叉注意力比 UViT 好,但 UViT 前期学得快,曲线有交叉。
    4. "MM-DiT … significantly outperforms":两套权重 + 联合注意力,三个指标都最好。和 DiT 比说明"分两套权重"有用;和 CrossDiT 比说明"文字也参与更新、双向注意"有用。
    5. "only a small gain when using three parameter sets … opt for the former":"the former" 指两套。三套的收益抵不过多出来的参数和显存。
    证据强度:Fig. 4 是小规模、单次训练的曲线,没有误差范围。"三套收益小"是看曲线的定性判断,论文没给数字。另外这些结构参数量不完全相同(MM-DiT 两套权重参数更多),论文没有做"同参数量"的对照。
    看这五种结构的完整流程:页面开头的全流程动画可以切换结构:
    自测:在 CrossDiT 里,第 10 层的文字 token 和第 1 层的有什么区别?

    没有区别。交叉注意力里文字只提供 K、V,不被更新,每一层看到的都是同一份文字表示。MM-DiT 里文字 token 每层都经过注意力和 MLP,会被图片信息更新。

    总结

    串起来:一次完整的前向

    合上论文,能对着 Fig. 2a 讲出每个方框,并说出它来自哪一段原文。

    1. 图片:训练图 → 冻结的 \(E\) → 归一化的 16 通道潜变量 \(x_0\)(附录 B.2、§5.2.1)。
    2. 加噪:按第一部分抽 \(t\)(logit-normal)和 \(\epsilon\),\(z_t=(1-t)x_0+t\epsilon\)(Eq. 2、§3)。
    3. 图片 token:\(z_t\) 切 2×2 → Linear → 加位置编码 → \(x\)(§4 骨干第 2 段)。
    4. 文字 token:caption(一半原始一半 CogVLM,§5.2.2)→ CLIP-L、CLIP-G、T5 → \(c_{ctxt}\in\mathbb R^{154\times4096}\) → Linear → \(c\)(附录 B.2)。
    5. 调制向量:\(t\) 正弦编码过 MLP + \(c_{vec}\) 过 MLP → \(y\)(§4 骨干第 1 段)。
    6. \(d\) 个 MM-DiT 块:两套权重,联合注意力(Fig. 2 下方、Fig. 2b、§5.2.3)。
    7. 输出:图片流 → Modulation → Linear → Unpatching → 速度 \(v_\Theta\)(Fig. 2a 右下)。
    8. loss:和 \(\epsilon-x_0\) 的平方误差(第一部分)。
    图 10 · Fig. 2a 的数据流(点"播放"跟着数据走)
    按 Fig. 2a 的布局重画:粉色是输入输出,绿色是固定的处理,蓝色是要训练的层。三个亮点分别代表文字、时间、图片的信息,在 MM-DiT 块里汇合。
    这一部分在全文中的位置:MM-DiT 是论文的第二个贡献(§1 的贡献列表)。5.2 的三个改进(16 通道、50/50 caption、MM-DiT)加上 5.1 的 RF + logit-normal,就是第三部分 5.3 要放大到 8B 的配方。
    最终自测:用三句话讲清 MM-DiT。

    图片切成 2×2 的 token,文字用两个 CLIP 和 T5 编码成 154 个 token,时间和整句大意 \(c_{vec}\) 合成 \(y\) 去调制每一层。每个块里文字和图片各用一套权重,但把两边的 Q、K、V 拼起来做一次联合注意力,所以两种模态互相看见、一起逐层更新。在 CC12M 的小规模比较中,它的 loss、CLIP、FID 都好于一套权重的 DiT 和文字不更新的 CrossDiT,而再细分成三套权重收益很小。