MiniMind · Extended Tutorial

离散扩散语言模型:原理与实现Discrete Diffusion Language Models: Principles & Implementation

技术总览Technical Overview

自回归(AR)语言模型将文本生成建模为链式条件概率的逐步采样;扩散语言模型(dLM)则将其建模为从全掩码序列到目标序列的迭代去噪过程。两者的根本区别在于:前者是严格的顺序生成,后者允许全局建模与并行揭示。 Autoregressive LMs decompose text generation as a chain of conditional probabilities; diffusion LMs model it as iterative denoising from a fully masked sequence. The former emphasizes sequential generation, the latter global modeling and parallel reveal.

问题定义Problem

语言生成能否摆脱固定的从左到右的顺序约束,转而优先确定高置信度位置?Can language generation move beyond fixed left-to-right order and prioritize high-confidence positions?

核心答案Core Answer

dLM 以随机掩码、双向预测与多步揭示,替代 AR 的单向 next-token prediction。DLLM replaces AR's unidirectional next-token prediction with random masking, bidirectional prediction, and multi-step reveal.

工程意义Engineering Impact

其意义不在于替代 AR,而在于扩展生成范式:天然支持 infilling、全局重评估与可控的揭示顺序。Rather than simply replacing AR, it extends the generation paradigm: naturally supporting infilling, global re-evaluation, and controllable reveal order.

AR 的本质:一条链AR Essentials: A Chain

p(x) = ∏i=1N p(xi | x<i)

文本被分解为一条严格有序的决策链:第 i 个词元仅依赖其左侧上下文,信息流严格单向。 Text is decomposed into a strictly ordered decision chain: token i depends only on left context; information flows unidirectionally.

  • 训练与推理的目标完全一致:均为 next-token prediction。Training and inference share the same objective: next-token prediction.
  • 每一步仅针对当前位置,缺乏全局重评估机制。Each step targets only the current position with no global re-evaluation.
  • 生成顺序固定为从左到右,逐词元展开。Generation order is fixed: left-to-right, token by token.
你Hi → 好! → 我I → 是am → AI → 助手assistant
图 1. AR 的链式依赖结构:每个词元的生成严格依赖其前序输出。Fig 1. Chain dependency in AR: each token strictly depends on the previous one.

为什么需要 dLMWhy DLLM

串行生成 O(N)Sequential O(N)

生成 N 个词元需要 N 次前向传播。KV Cache 仅能降低单步开销,无法改变串行的本质。Generating 1000 tokens requires ~1000 forward passes. KV cache reduces cost but not the sequential nature.

固定顺序的局限性Fixed-Order Limitation

许多任务天然适合先确定全局结构、再填充局部细节的生成方式。Many tasks benefit from determining global structure first, then filling in details.

Exposure Bias

训练依赖 ground truth 而推理依赖模型自身输出,二者的分布偏差随步数累积。Training relies on ground truth while inference relies on model output; errors accumulate.

顺序生成范式(AR)Sequential Paradigm (AR)

严格从左到右逐词元生成;一旦某位置出错,后续所有条件分布均受污染。Strictly left-to-right; once an error occurs, all subsequent conditional distributions are affected.

全局揭示范式(dLM)Global Reveal Paradigm (DLLM)

基于全局上下文同时评估所有位置的置信度,优先填充最确定的位置;揭示顺序由模型自行决定。Evaluates confidence across all positions globally; fills highest-confidence positions first; order is dynamically determined.

从连续扩散到离散扩散From Continuous to Discrete Diffusion

图像扩散使用连续高斯噪声;文本不存在自然的连续噪声空间,因此 dLM 以 MASK token 作为离散噪声的替代。 Image diffusion uses continuous Gaussian noise; text has no natural continuous noise space, so DLLM uses MASK as a discrete noise proxy.

概念Concept 连续扩散(DDPM)Continuous (DDPM) 离散扩散(MDLM / dLM)Discrete (MDLM / DLLM)
噪声Noise 高斯噪声Gaussian 随机 MASKRandom MASK
去噪目标Denoising target 预测原始像素Predict original pixels 预测被 MASK 的词元Predict masked tokens
前向过程Forward x₀ → xT,越来越模糊x₀ → xT, increasingly blurred x₀ → xT,越来越多位置被掩码x₀ → xT, more positions masked
反向过程Reverse 逐步去噪Gradual denoising 逐步揭示Gradual reveal

dLM 的训练机制Training: How DLLM Learns to Fill Blanks

Step 1 — 采样 tStep 1 — Sample t

t ~ U[0,1)
→

Step 2 — 随机 maskStep 2 — Random mask

按 t 比例随机掩码 assistant 区Randomly mask assistant region by ratio t
→

Step 3 — 双向前向传播Step 3 — Bidirectional forward

预测所有被掩码位置Predict all masked positions
→

Step 4 — 只在掩码位置算 lossStep 4 — Loss on masked only

loss = CE / p_mask
图 2. dLM 单步训练流程。Fig 2. Single-step DLLM training pipeline.

单步训练流程示意Single-Step Training Illustration

Step 1 · 采样 tStep 1 · Sample t

从均匀分布采样 t ∼ U[0,1),以此确定 assistant 区域的 mask 比例。Sample t ~ U[0,1) to determine mask intensity for the assistant region.

Step 2 · 随机 mask assistant 区Step 2 · Random mask assistant region

QQQQQ AMAMM MAMM

Prompt 区域保持不变;assistant 区域按比例 t 随机替换为 MASK。Prompt stays intact; assistant region is randomly replaced with MASK by ratio t.

Step 3 · 双向前向传播Step 3 · Bidirectional forward

模型以带 MASK 的完整序列为输入,输出所有被掩码位置的词元分布。Model takes the masked sequence as input and outputs classification distributions for all masked positions.

Step 4 — 只在 M 位置算 lossStep 4 — Loss on M positions only

loss = Σi∈{我,AI,助}i∈{I,AI,assis} CE(predi, targeti) / pmask

仅在 MASK 位置计算损失,并按 pmask 进行重加权。Non-MASK positions are excluded; MASK positions are weighted by pmask.

和 BERT / AR 的关键区别Key Differences from BERT / AR

相较于 BERTvs. BERT

BERT 使用固定的 mask 比例(如 15%);dLM 通过连续参数 t 覆盖从低到高的全部损坏强度。BERT uses a fixed mask ratio; DLLM uses continuous t covering all corruption intensities.

相较于 ARvs. AR

AR 仅学习预测下一个词元;dLM 学习同时恢复多个被掩码位置。AR learns only the next token; DLLM learns to recover multiple positions simultaneously.

训练时的 mask → 预测 → 损失计算详细过程Training: Mask → Predict → Loss Computation

Step 1 · 原始完整序列Step 1 · Original sequence
你Hi 好! 我I 是am AI 助assis 手tant

Prompt + assistant 完整文本Complete prompt + assistant text

Step 2 · 按 t 随机 maskStep 2 · Random mask by t
你Hi 好! M 是am M M 手tant

t=0.6 → assistant 区 60% 被 maskt=0.6 → 60% of assistant region masked

Step 3 · 模型预测Step 3 · Model prediction
你Hi 好! 我I 是am AI 助assis 手tant

双向 forward → 预测所有 M 位置Bidirectional forward → predict all M positions

损失计算:只在 mask 位置Loss: masked positions only

预测词元Predicted

我I AI 助assis

真实词元Ground truth

我I AI 助assis

损失公式:Loss formula: loss = Σi∈{我,AI,助}i∈{I,AI,assis} CE(predi, targeti) / pmask

非 mask 位置不参与损失计算;pmask 平衡不同 t 下的梯度贡献。Non-mask positions excluded; pmask balances gradient contribution across different t.

图 3. dLM 训练流程:mask → 预测 → 损失计算。Fig 3. DLLM training: mask → predict → loss computation.

交互式训练模拟器Interactive Training Simulator

0.0 0.60 1.0
序列状态Sequence State Original sequence
loss 计算详情(仅 mask 位置)Loss details (masked only)
t 值t
0.60
mask 数量# masks
0
p_mask
0.60
CE 总和CE sum
0.00
加权 lossWeighted loss
0.00
拖动 t 值滑块调整 mask 比例,点击步骤按钮查看训练过程。loss = Σ CE / p_mask。Drag the t slider to adjust mask ratio. Click step buttons to inspect training. loss = Σ CE / p_mask.

加权损失公式Weighted Loss Formula

L = Et [ Σi∈masked (1 / pmask) · CE(fθ(xt)i, x0(i)) ] / Nvalid

除以 pmask 的目的是平衡不同 t 下的梯度贡献:t 较小时被 mask 的位置少但每个位置更关键,t 较大时反之。 Dividing by pmask balances gradient contributions across different t: when t is small, fewer masks but each position matters more; when t is large, more masks but each should carry less weight.

loss = F.cross_entropy(logits, labels, reduction='none') loss = (loss[corruption_mask] / p_mask[corruption_mask]).sum() / n_valid

训练数据格式Training Data Format

input_ids: [<user>] [你好] [<assistant>] [我是] [AI] [助手] [<pad>] [<pad>] labels: [-100] [-100] [-100] [我是] [AI] [助手] [-100] [-100] ↑ 只有这些位置参与训练 ↑input_ids: [<user>] [Hello] [<assistant>] [I] [am] [AI] [<pad>] [<pad>] labels: [-100] [-100] [-100] [I] [am] [AI] [-100] [-100] ↑ only these positions are trained ↑
训练Training 推理Inference
步数Steps 1 步 forward1 forward 多步迭代(steps × blocks)Multi-step (steps × blocks)
mask 随机比例(t 控制)Random ratio (t-controlled) 全 MASK → 按置信度逐步揭示Full MASK → reveal by confidence
本质Essence 看到部分信息预测全部Predict all from partial info 多步迭代逐步补全Iterative step-by-step completion

推理:从全掩码到逐步揭示Inference: From Full Mask to Gradual Reveal

dLM 的推理本质上是一个迭代去噪过程:将生成区初始化为全 MASK 序列,然后按置信度逐步揭示词元。 DLLM inference is an iterative denoising process: initialize a fully masked sequence, then gradually reveal tokens by confidence.

初始化画布Initialize Canvas

Q Q Q Q Q | M M M M M M M M
Prompt 区保持不变,生成区初始化为全 MASK。Prompt region stays fixed; generation region initialized as fully masked.

交互式推理模拟器Interactive Inference Simulator

Step 0 / 0 Block 0 / 0
Prompt + 生成区Prompt + Generation
本步置信度排序(模拟)Confidence ranking this step (simulated)
已揭示Revealed
0
剩余 MASKRemaining
0
本步揭示This step
0
总 forward 次数Total forwards
0
调整参数后点击重置,拖动进度条或点击按钮控制推理过程。每步揭示数量 = ceil(剩余 MASK / 剩余步数)。Adjust parameters then click Reset. Drag slider or click buttons to control inference. Tokens per step = ceil(remaining MASK / remaining steps).

一个 block 的去噪过程(静态版)One Block Denoising Process (Static)

初始Initial

Prompt MMMM MMMM

Step 1 — 全序列 forward → 置信度排序 → 揭示 top-kStep 1 — Full forward → confidence ranking → reveal top-k

Prompt M是isMM M助assisMM

Step 2 — 再次 forward → 继续揭示高置信位置Step 2 — Forward again → reveal high-confidence positions

Prompt 我I是isM能can M助assis手tantM

Step 3 — 收敛完成Step 3 — Converged

Prompt 我I是am智AI能assis 的tant助.手。
图 4. dLM 对同一位置反复预测,揭示顺序由置信度而非位置索引决定。Fig 4. DLLM predicts each position multiple times; reveal order is determined by confidence, not position index.
关键点 1Key Point 1

每步揭示的词元数由 remaining / remaining_steps 动态计算,而非固定常数。Tokens revealed per step = remaining / remaining_steps, not a fixed top-k constant.

关键点 2Key Point 2

每步均对全序列做前向传播,而非仅处理当前 block;双向注意力可利用全部已知上下文。Each step processes the full sequence, not just the current block; bidirectional attention leverages all known context.

Block 并行:AR ↔ 全并行的连续旋钮Block Parallelism: AR ↔ Full Parallelism Continuum

block_size 决定了 dLM 的行为更接近自回归还是并行去噪。它并非离散的模式开关,而是质量与速度之间的连续调控旋钮。 block_size determines whether DLLM behaves more like AR or parallel denoising; it's not a discrete mode switch but a continuous knob between quality and speed.

block_size 行为Behavior 速度 / 质量Speed / Quality
= max_new_tokens 全序列一次性去噪Global one-shot denoising 速度最快,质量取决于步数与采样策略Fastest; quality depends on steps & sampling
8 ~ 32 分块并行去噪Local block parallelism 工程实践中常用的折中方案Common engineering trade-off
= 1 退化为逐词元自回归Degenerates to AR 速度最慢,但最接近 AR 的严格顺序生成Slowest; closest to strict sequential generation

A2D:从 AR 到 dLM 的两步转换A2D: Two-Step AR → DLLM Conversion

AR Model

已训练的 AR 权重Pretrained AR weights

→

1. 关闭因果约束1. Disable causal mask

is_causal=True → False
→

2. 扩展词表2. Extend vocabulary

vocab += [MASK]
→

DLLM Model

继承 AR 的全部语言知识,仅需微调Inherits all AR knowledge; fine-tune only

图 5. A2D(AR-to-Diffusion)转换流程。Fig 5. A2D (AR-to-Diffusion) conversion path.

采样与控制:Gumbel-Max、Top-k、CFGSampling & Control: Gumbel-Max, Top-k, CFG

Gumbel-Max 采样Gumbel-Max Sampling

相较 softmax + multinomial,Gumbel-Max 在低精度数值条件下更稳定,适用于推理阶段的离散采样。More stable than softmax + multinomial under low precision; ideal for discrete sampling during inference.

temperature

控制采样随机性:0 趋近贪心解码,1 为标准采样。过高则输出散乱,过低则重复。Controls sampling randomness: 0 → greedy, 1 → standard; too high → incoherent, too low → repetitive.

Top-k 与 CFGTop-k & CFG

Top-k 截断长尾噪声;CFG 通过有条件与无条件两次前向传播的差值来增强 prompt 引导。Top-k truncates long-tail noise; CFG amplifies prompt constraint via conditional/unconditional forward difference.

Gumbel-Max

def add_gumbel_noise(logits, temperature): noise = torch.rand_like(logits, dtype=torch.float64) return logits.exp() / ((-torch.log(noise)) ** temperature)

CFG

logits = logitsuncond + (1 + s) · (logitscond - logitsuncond)

AR 与 dLM 的系统对比AR vs DLLM: Fair Comparison

维度Dimension AR LM Masked Diffusion LM
注意力Attention 单向(causal)Unidirectional causal 双向(bidirectional)Bidirectional
生成顺序Generation order 从左到右,逐词元生成Left-to-right token by token 按置信度排序,优先填充最确定的位置By confidence; most certain first
训练目标Training objective next-token prediction masked-token prediction
推理步数Inference steps O(N) O(T),由步数 × block 数决定O(T), fixed steps / block combination
编辑能力Editing capability 通常需额外机制支持Usually requires extra mechanisms 天然支持 infilling 与局部补全Native infilling / local completion
训练成本Training cost 一般需从零训练Usually from scratch 可从 AR 权重迁移Transferable from AR

结论并非“dLM 已取代 AR”,而是:dLM 将语言生成从严格的顺序链式过程,扩展为可迭代、可全局重评估的推理过程。 The conclusion is not “DLLM replaces AR”, but: DLLM extends language generation from a strict sequential chain into an iterative, globally re-evaluable inference process.

调参建议Tuning Guidelines

block_size
8–32

值越小越稳定但越慢,越大越快但对步数与采样策略更敏感。Smaller = slower but stabler; larger = faster but depends more on steps & sampling.

steps
≥ block_size

步数过少会导致每步揭示过多,补全质量下降。Fewer tokens revealed per step; too few steps leads to coarse completion.

temperature
0.2–0.8

过高则输出散乱,过低则重复;通常需与 top_k 联合调节。Too high = chaotic; too low = repetitive; usually tuned with top_k.

top_k
20–100

截断候选词元空间,平衡多样性与生成稳定性。Controls candidate token space; affects diversity and stability.

cfg_scale
0.5–2.0

增强对 prompt 的忠实度,但过高可能引发模式崩塌。Improves prompt fidelity; too high may cause mode collapse.

项目实践结构Project Structure

目录结构Directory Structure

dllm/ ├── model/model_qwen3_dllm.py # 模型 ├── dataset/lm_dataset.py # 数据集 ├── trainer/train_dllm.py # 训练 └── eval_dllm.py # 推理dllm/ ├── model/model_qwen3_dllm.py # model ├── dataset/lm_dataset.py # dataset ├── trainer/train_dllm.py # training └── eval_dllm.py # inference

训练 / 推理命令Training / Inference Commands

# 训练 CUDA_VISIBLE_DEVICES=0,1 torchrun --nproc_per_node 2 trainer/train_dllm.py \ --learning_rate 1e-4 --freeze_ffn --use_compile 1 --batch_size 16 --epochs 5 # 推理 python eval_dllm.py --block_size 8 --steps 16 --temperature 0.3 --top_k 50# Training CUDA_VISIBLE_DEVICES=0,1 torchrun --nproc_per_node 2 trainer/train_dllm.py \ --learning_rate 1e-4 --freeze_ffn --use_compile 1 --batch_size 16 --epochs 5 # Inference python eval_dllm.py --block_size 8 --steps 16 --temperature 0.3 --top_k 50

开放问题与未来方向Open Problems & Future Directions

长文本 block 策略Long-text block strategy

如何使 block 边界语义化,而非简单的固定长度切分。How to make block boundaries more semantic rather than fixed-length.

与 speculative decoding 结合Combine with speculative decoding

能否将 draft/verify 思路与多步去噪结合,进一步压缩推理开销。Can draft/verify be combined with multi-step denoising to reduce inference cost?

多模态离散扩散Multimodal discrete diffusion

文本、图像、音频统一 token 化后,能否共享同一套扩散生成范式。Can text, image, audio share a unified diffusion paradigm after tokenization?

训练效率Training efficiency

如何以更少的步数与掩码配置,逼近乃至追平 AR 的生成质量。How to match AR quality with fewer steps and mask configurations.

总结Summary

AR 基于链式分解逐词元生成;dLM 基于全局建模迭代揭示。AR generates token by token via chain decomposition; DLLM reveals iteratively via global modeling.

二者并非简单的替代关系:AR 强调顺序一致性,dLM 强调全局建模与迭代修正。 They are not simple substitutes. AR emphasizes sequential consistency; DLLM emphasizes global modeling and iterative refinement.