大模型科普:探秘莎翁风格的诞生之旅(无代码版)

立委按:鉴于语言大模型GPT的重要性,特此根据AI大神Karpathy的nanoGPT讲座,编纂此科普系列,计五篇,其中此篇没有代码和数学公式,是最通俗的科普。其他四篇包括一篇英文,均附带可验证的Python代码,并给予不同角度的详细解说,面对有工程背景的对象。

ChatGPT这样的大语言模型在今天已展现出惊人的能力:它们能与人对话,辅助创作,甚至独立生成颇具文采的诗篇。这不禁引人深思:大模型是如何理解并运用复杂的人类语言的呢?本文将以大模型传教士Karpathy简化的迷你版nanoGPT模型为例,揭示其学习模仿大文豪莎士比亚风格的奥秘。

核心机制:预测下一个字词

大模型学习过程核心机制之一在于对一句话或一段文字里,接下来会出现哪个字或哪个词的预测。在文本领域,这意味着当nanoGPT接收到一句话的前半部分,例如“生存还是毁灭,这是一个…”,它需要根据已有的信息,推断出最有可能紧随其后的词语,比如“问题”。我们所讨论的这个莎翁风格,其核心训练目标,就是在莎士比亚所有作品的文字里,当读到某处时,能精准地猜出下一个字是什么。

第一步:数据的数字化——AI的“识字”过程

nanoGPT的第一个挑战在于,它难以直接理解人类的文字符号。计算机系统以数字为基础进行运算。因此,首要任务是将莎士比亚的文本转化为机器能够处理的数字形式。

  1. 训练语料的准备:在这个例子中,即莎士比亚的全部剧作。这些文本数据经过收集和整理,作为nanoGPT学习的材料。
  2. 构建字符“密码本”:接下来,nanoGPT会构建一本特殊的“字典”或“密码本”。在这本“密码本”里,莎士比亚作品中出现的每一个独立字符(字母、标点符号等)都被赋予一个独一无二的数字代号。例如,“T”可能对应38,“o”对应33,空格对应0等等。通过这种方式,一句“To be or not to be”在AI内部就表示为一串特定的数字序列。反之,AI也能用这本“密码本”,把数字代码“翻译”回我们能读懂的文字。

第二步:赋予数字上下文含义——“数字档案”的建立

仅仅将字符转化为孤立的、互不相关的数字代号是不够的。nanoGPT需要一种方式来捕捉这些代号背后的“意义/特性”以及它们在特定上下文中的“角色”。这就是“嵌入”(Embedding)技术发挥作用的地方。

  1. 字符嵌入:为了让nanoGPT理解这些数字代号的“含义”和它们之间的联系,科学家们发明了一种叫做“嵌入”(Embedding)的技术。可以把它想象成,给每个字(或词)都创建了一份独特的“数字档案”。这份档案不是一个简单的数字,而是一组特定长度的、精心计算出来的数字(专业上称为向量)。这组数字就像这个字的“多维度评分”,从不同方面描述了它的特性。比如,“国王”和“王后”的数字档案,在代表“皇室”的那个维度上评分可能都比较高,而“苹果”和“香蕉”则在代表“水果”的维度上评分高。这样,意思相近的字,它们的“数字档案”也会比较相似。
  2. 位置嵌入:语言中,字词的顺序也重要。“国王杀了王后”与“王后杀了国王”的含义天差地别。因此,nanoGPT不仅要知道每个字是什么,还要知道它在句子里的位置。所以,每个位置(比如第一个字、第二个字……)也会有自己的一份独特的“位置数字档案”。

最终,输入序列中一个字符的初始信息,就是它自身的“特征数字档案”和它所在“位置数字档案”的结合。这样,nanoGPT拿到的每个字的信息,既包含了字本身的含义,也包含了它在句子中的顺序信息。

第三步:信息处理的核心——Transformer“工厂”

这是nanoGPT模型进行复杂信息处理和上下文理解的核心部件。你可以把它想象成一个多层加工的“工厂”,每一层流水线都对输入信息进行更深一步的加工和理解。

nanoGPT内一层流水线通常包含以下关键环节:

  1. 掩码自注意力机制:这是Transformer结构的一大亮点。当nanoGPT读到一个字时,它会回顾前面所有读过的字,判断哪些字对理解当前这个字最重要,然后给予这些重要的字更多的“关注”。“掩码”则是因为在猜下一个字是什么的时候,nanoGPT不能“偷看”答案,它只能关注已经出现的字,保证预测的单向性。
  2. 前馈神经网络:这个模块会对信息进行进一步的加工和提炼,让AI的理解更透彻,能够学习到更复杂的文字模式。
  3. “快速通道”与“信息质检”(残差连接与层归一化):这些是帮助AI工厂高效运转的辅助设计。
    • 为了防止信息在多层工厂的传递中失真或减弱,工厂里还有“快速通道”(专业上叫残差连接),允许一部分原始信息直接“跳”到后面的工序,保证重要信息不丢失,使得AI可以构建更深的工厂层级有效学习。
    • 同时,每一道工序后,还有“质检员”(专业上叫层归一化)会把信息“整理”一下,让信息更“规整”,加速学习过程。

在nanoGPT中,通常会堆叠多个这样的加工层。每一层都在前一层的基础上进行更抽象的规律性提取,并把它们和上下文联系起来进行综合理解。

第四步:生成预测——输出结果

经过多层的深度处理后,nanoGPT对输入序列的每个字符都生成了一个包含了丰富上下文信息的数字总结。现在,需要将这个内部总结转化为对下一个字符的具体预测。

这通常通过以下步骤完成:

  1. 一个“最终决策转换器”(专业上叫线性层)会将这个数字总结转换成对词汇表中所有候选字的“原始打分”。每个字都有一个分数,分数高低代表nanoGPT判断它作为下一个字的可能性。
  2. 然后,另一个部件(专业上叫Softmax函数)会把这些“原始打分”转换成每个候选字作为正确答案的“可能性百分比”。可能性越高的字,模型就越认为它应该是正确的下一个字。

第五步:从经验中学习——模型的训练过程

nanoGPT模型并非一开始就能准确预测。它需要通过一个称为“训练”的过程,从大量的样本数据中学习规律。

  1. 数据准备(输入与目标):从训练语料(莎士比亚剧作)中,选取一段文本序列作为模型的输入(例如,“To be or not to b”),并将其真实的下一个字符(“e”)作为期望的输出(标准答案)。
  2. 损失计算(衡量差距):GPT根据输入序列进行预测,得到一个关于下一个字符的“可能性百分比”列表。然后,将这个预测结果与标准答案进行比较,一个叫“损失函数”的裁判来打分,衡量nanoGPT的答案和标准答案之间的“差距”或“错误程度”。差距越大,说明GPT错得越厉害。
  3. 参数优化(“复盘”与调整):训练的目标是让这个“差距”尽可能小。nanoGPT会启动一个“复盘机制”(专业上叫反向传播算法),仔细分析这次错误是哪些内部环节没做好导致的,并计算出每个内部“小齿轮”(即模型中可调整的参数)应该朝哪个方向、调整多少才能减少错误。接着,一个“总工程师”(专业上叫优化器)会根据分析结果,对nanoGPT内部的这些“小齿轮”进行微小的调整,目标是让nanoGPT下次遇到类似情况时表现得更好一些。

这个“输入-预测-计算差距-复盘调整”的迭代过程会重复进行成千上万甚至上亿次。每一次迭代,模型都会从错误中学习,逐渐提升其预测的准确性,从而更好地捕捉莎士比亚文本的语言模式。

第六步:文本生成

当nanoGPT训练到一定程度后,就可以用它来生成新的、具有莎士比亚风格的文本了。

  1. 起始提示(Prompt):可以给nanoGPT一个初始的文本片段(prompt)作为生成的起点,例如“ROMEO:”。
  2. 迭代生成过程
    • 模型接收当前已生成的文本序列作为上下文。
    • 模型进行一次完整的处理和计算过程(如第四步所述),预测下一个最可能的字符,即从“可能性百分比”列表中按概率抽取一个字符。
    • 将新生成的字符追加到当前序列的末尾。
    • 重复以上步骤,直到达到预设的生成长度或遇到特定的终止标记。
  3. 控制生成的多样性与质量
    • 温度(Temperature):在计算“可能性百分比”之前,可以对“原始打分”进行调整。调低“温度”,AI会更倾向于选择那些分数最高的字,写出来的东西更确定、更保守。调高“温度”,GPT则会更大胆,愿意尝试不那么常见的字,可能会更有创意,但也可能更容易“跑题”。
    • Top-K采样 / Top-P (Nucleus) 采样:这些技术像是给AI的选择加了个“筛选器”。比如,只允许nanoGPT从分数最高的K个字里选,或者从那些加起来可能性超过一定百分比(P)的一小撮字里选。这有助于避免nanoGPT选到非常不靠谱的字,让生成的文本更连贯。

通过这些机制,nanoGPT模型就能够一句句模仿莎士比亚风格的文本。

从简化模型nanoGPT到大型语言模型

本文描述的模仿莎士比亚风格的nanoGPT是一个高度简化的示例。现实中的大型语言模型(LLMs),如OpenAI GPT系列,其核心原理(如Transformer结构、预测下一个词元等)与此相同,但有着显著的量级和复杂度差异:

  • 模型规模:内部“小齿轮”(参数)的数量可达千亿甚至万亿级别,远超简化模型的规模。
  • 数据规模:训练数据量是TB乃至PB级别(1 PB = 1024 TB,1 TB = 1024 GB),来源极其广泛,不仅仅是单一作者的作品,几乎涵盖了互联网上公开的绝大部分文字。
  • 词元化(Tokenization):它们处理的单位比字符大,叫做“词元”(token)。一个词元可能是一个完整的词,也可能是一个词的一部分(比如英语中的“un-”、“-able”这样的前后缀),甚至是单个字母。这种方式更灵活,能更有效地处理各种词汇、生僻词和复杂词形。
  • 训练技术与资源:涉及更复杂的训练策略、使用成百上千台计算机协同工作(分布式计算)、海量的计算资源以及更长的训练周期(数周甚至数月)。
  • 对齐技术:大型模型还会经过更高级的后续“特训”,比如让它们学习理解并遵循人类给出的各种指令(这叫指令微调),或者根据人类对它们生成内容的评价好坏来不断改进(这叫基于人类反馈的强化学习)。这些步骤能让大型GPT的行为更符合我们的期望、指令和社会的价值观。

结语

通过对这个简化AI模型nonoGPT的剖析,我们可以看到,GPT这类模型基于对大量文本数据中复杂模式的统计学习,基于这些规律进行的语言接龙的概率预测。从简单的字符预测任务出发,nanoGPT借助Transformer结构、莎士比亚全集数据和强大的计算能力,AI能够学习并模仿出高度复杂的语言风格。理解GPT背后的机制,有助于我们更理性地看待AI大模型的能力边界,并思考其在未来社会和文化领域中可能扮演的角色。

 

GPT科普系列

揭秘GPT内核之四

Karpathy's nanoGPT:从零理解莎士比亚生成器

立委按:鉴于语言大模型GPT的重要性,特此根据AI大神Karpathy的nanoGPT讲座,编纂此科普系列,计五篇,一篇没有代码和数学公式,是最通俗的科普。其他四篇包括一篇英文,均附带可验证的Python代码,并给予不同角度的详细解说,面对有一定工程背景的对象。

你可能已经听说过GPT(Generative Pre-trained Transformer)的鼎鼎大名,无论是能与你流畅对话的ChatGPT,还是能帮你写代码、写诗歌的AI助手,它们背后都有GPT的强大身影。但是,这个神奇的“黑箱”究竟是如何运作的呢?

今天,我们就以一个“迷你版”的莎士比亚风格文本生成器为例,一步步拆解GPT的构造,让你从零开始,彻底搞懂它的核心原理。别担心,我们会用最通俗易懂的语言,结合具体的代码示例,让你看清这背后的“魔法”。

核心思想:预测下一个“词”(词元或字符)

GPT最核心的任务,说白了就是预测序列中的下一个元素。对于文本来说,就是预测下一个单词或下一个字符。我们给它一段话,它会猜接下来最可能出现什么。

在我们的莎士比亚生成器中,模型学习的就是预测莎士比亚剧本中的下一个字符是什么。比如,看到 "To be or not to b",它应该能预测出下一个字符是 "e"。

# 训练数据中,y 就是 x 的下一个字符序列
# input x: "To be or not to b"
# output y: "o be or not to be"
# 比如 train_data[i:i+block_size] 是输入 x
# train_data[i+1:i+block_size+1] 就是目标 y

第一步:让计算机“认识”文字 - 数据与词汇表

计算机不认识人类的文字,它们只懂数字。所以,第一步就是把文字转换成计算机能理解的格式。

  1. 准备“教材”(输入数据):
    我们首先需要大量的文本数据作为模型的“教材”。在这个例子中,就是莎士比亚的剧作 (input.txt)。这些数据会被预处理并保存为二进制格式 (train.bin) 以便高效加载。
  2. 构建“字典”(词汇表与编码):
    我们需要一个包含所有可能出现的字符的“字典”(词汇表)。对于莎士比亚的文本,这个词汇表可能包含英文字母、数字、标点符号等。
  3. # data/shakespeare_char/input.txt 包含了所有莎士比亚文本

    chars = sorted(list(set(open(os.path.join(data_dir, 'input.txt'), 'r').read())))

    stoi = {ch: i for i, ch in enumerate(chars)} # 字符到索引的映射 (string to integer)

    itos = {i: ch for i, ch in enumerate(chars)} # 索引到字符的映射 (integer to string)

    vocab_size = len(chars) # 词汇表大小,比如65个唯一字符

    ```stoi` (string to integer) 将每个字符映射到一个唯一的数字索引(比如 'a' -> 0, 'b' -> 1)。`itos` (integer to string) 则反过来。

    # 这样,我们就可以用 `encode` 函数将一串字符转换成数字列表,用 `decode` 函数再转换回来。

    ```

    def encode(s): # "hello" -> [40, 37, 44, 44, 47] (假设的映射)

        return [stoi[c] for c in s]

    def decode(l): # [40, 37, 44, 44, 47] -> "hello"

        return ''.join([itos[i] for i in l])

    # 加载训练数据时,train.bin 文件中的内容已经是被 encode 过的数字序列了。

    train_data = torch.frombuffer(

        open(os.path.join(data_dir, 'train.bin'), 'rb').read(),

        dtype=torch.uint16 # 每个数字用16位无符号整数表示

    ).long() # 转换为PyTorch常用的长整型

第二步:赋予字符“意义” - 嵌入层 (Embedding)

虽然我们把字符变成了数字,但这些数字本身并没有“意义”。比如,数字5和数字10之间并没有“更像”或“更不像”的关系。我们需要一种方式来表示字符的含义及其在序列中的位置。这就是嵌入(Embedding)的作用。意义的本质体现在系统关系之中,正如马克思提到人的意义时所说:人是社会关系的总和。数字化实现就是建立一个高维向量的意义空间,用来定义每个词元相对于其他词元的位置,关系则以距离来表示。

  1. 字符嵌入 (Token Embedding):
    我们为词汇表中的每个字符学习一个固定长度的向量(一串数字),这个向量就代表了这个字符的“意义”或“特征”。想象一下,在一个高维空间中,意思相近的字符它们的向量也可能更接近。
    # n_embd 是嵌入向量的维度,比如128

    self.embedding = nn.Embedding(vocab_size, n_embd)

    # 输入一个字符索引,输出一个128维的向量
    例如,字符 'a' (索引可能是0) 会被映射成一个128维的向量 [0.1, -0.2, ..., 0.5]。
  2. 位置嵌入 (Positional Embedding):
    在语言中,顺序会影响意义。“国王杀了王后”和“王后杀了国王”意思完全不同。因此,我们还需要告诉模型每个字符在句子中的位置。位置嵌入就是为每个位置(比如第0个字符,第1个字符……)学习一个向量。
    # 假设句子最长不超过1000个字符

    self.pos_embedding = nn.Embedding(1000, n_embd)

    # 输入一个位置索引,输出一个128维的向量。
    # 最终,一个字符在特定位置的表示,是它的字符嵌入向量和它所在位置的嵌入向量相加得到的。
    # x 是输入的字符索引序列,形状为 (批量大小, 序列长度)
    # pos 是位置索引序列,形状为 (1, 序列长度)
    # 结果 x_embedded 的形状是 (批量大小, 序列长度, 嵌入维度)

    x_embedded = self.embedding(x) + self.pos_embedding(pos)

第三步:神奇的“思考机器” - Transformer 

这是GPT的核心部件,负责理解上下文信息并进行“思考”。我们的莎士比亚生成器用的是Transformer的解码器层 (Decoder Layer)

一个Transformer解码器层主要包含以下几个部分:

  1. 因果掩码 (Causal Mask):

    在预测下一个字符时,模型只能看到它前面的字符,不能“偷看”答案。因果掩码就像给模型戴上了“眼罩”,确保它在预测第 t 个字符时,只使用第 0 到 t-1 个字符的信息。
    # t 是序列长度

    # mask 是一个上三角矩阵,对角线以上为True (masked)

    # [[False,  True,  True,  True],
    #  [False, False,  True,  True],
    #  [False, False, False,  True],
    #  [False, False, False, False]]

    mask = torch.triu(torch.ones(t, t), diagonal=1).bool()
  2. 计算注意力权重的过程

    在自注意力层,每个token的Query矩阵与上下文窗口中所有tokens的Key 矩阵转置相乘,这样就得到了该token对所有tokens的注意力权重(如果掩码,则与下文的tokens权重全部置零)。对于一个包含 B 个序列、每个序列 T 个 token 的批次输入,Query 矩阵形状是 B * T * head_size,Key 矩阵转置后是 B * head_size * T。两者相乘得到一个形状为 B * T * T 的权重矩阵。这个 B * T * T 的矩阵,对于批次中的每一个序列(B 维度),都有一个 T * T 的子矩阵,其中的每一个元素 (i, j) 代表位置 i 的 Query 与位置 j 的 Key 的点积结果,也就是token-i 关注token-j 的原始“亲和力”或“相谐度”。

    上述描述解释了计算注意力分数的核心数学操作:
    Query 矩阵与 Key 矩阵的转置相乘 (Q @ K.transpose(-2, -1)),我们来拆解一下:
     
    假设你有一个序列,长度为 T。对于这个序列中的每一个 token,我们都计算得到一个 Query 向量和一个 Key 向量。假设每个 Q 和 K 向量的维度是 head_size (记为 D)对于整个序列,我们可以把所有 token 的 Query 向量堆叠起来形成一个 Query 矩阵,形状是 (T * D)。同样,所有 Key 向量堆叠形成一个 Key 矩阵,形状也是 (T * D)。
     
    我们想要计算的是:序列中每一个位置 i 的 Query 向量 (Q_i) 与序列中每一个位置 j 的 Key 向量 (K_j) 之间的点积。这个点积 (Q_i . K_j) 就是位置 i 对位置 j 的“注意力分数”或“亲和力”
     
    如果你熟悉矩阵乘法,矩阵 A 乘以矩阵 B 的结果矩阵 C,其元素 C_ij 是 A 的第 i 行与 B 的第 j 列的点积我们想让结果矩阵 C 的元素 C_ij 等于 Q 矩阵的第 i 行 (Q_i) 与 K 矩阵的第 j 行 (K_j) 的点积。要做到这一点,我们需要 Q 矩阵乘以 K 矩阵的转置 (K^T)。
     
    如果 Q 是 (T * D),K 是 (T * D),那么 K 的转置 K^T 就是 (D x T)。进行矩阵乘法
    Q @ K^T: (T * D) @ (D * T) = (T * T)。结果矩阵 (T * T) 的元素在第 i 行、第 j 列的值,正是 Q 矩阵的第 i 行 (Q_i) 与 K^T 矩阵的第 j 列的点积。由于 K^T 的第 j 列就是 K 矩阵的第 j 行 (K_j) 沿列方向排列,这个点积正是我们所要的 Q_i . K_j
     
    考虑批次 (Batch):  当处理多个序列(一个批次)时,PyTorch 中的张量会增加一个批次维度 B。所以 Query 矩阵形状是 (B * T * D),Key 矩阵形状是 (B * T * D)。为了对批次中的每一个序列独立进行上述 (T * D) @ (D * T) 的矩阵乘法,我们需要将 Key 矩阵进行转置,使其形状变为 (B * D * T)。 PyTorch 的批次矩阵乘法 (@torch.bmm) 可以处理这种形状的乘法:(B * T * D) @ (B * D * T) = (B * T * T)
     
    转置的维度: 转置倒数两个维度 (transpose(-2, -1)),这是因为 PyTorch 中批次张量的维度通常是 (Batch, Time, Feature)。Query 和 Key 的形状是 (B, T, head_size)。要得到 (B, head_size, T),我们需要交换 Time (维度 -2) 和 head_size (维度 -1) 这两个维度
     
    所以,转置 Key 矩阵是为了通过标准的矩阵乘法操作,高效地并行计算序列中每一个 Query 向量与每一个 Key 向量之间的点积,从而得到一个表示所有位置之间的 T * T 注意力分数矩阵 (对于每个批次中的序列而言)。

  3. 多头自注意力机制 (Multi-Head Self-Attention):

    这是Transformer的精髓!“自注意力”机制允许模型在处理一个字符时,去关注输入序列中所有其他字符,并判断哪些字符对当前字符的理解最重要。想象一下你在阅读 "The cat sat on the mat." 当你读到 "mat" 时,注意力机制可能会告诉你 "cat" 和 "sat on" 对理解 "mat" 的上下文很重要。

    “多头”则意味着模型可以从多个不同的“角度”或“子空间”去关注信息,捕捉更丰富的关系。比如一个头可能关注语法关系,另一个头可能关注语义关系。
    在解码器中,由于因果掩码的存在,注意力机制只会关注当前位置之前的字符。

    QKV 的分工(Query 用于寻找、Key 用于匹配、Value 用于承载信息)怎么实现的?

    Q, K, V 的分工
    是在自注意力机制的计算公式和结构中实现的。这个结构是固定的:计算 Query 和 Key 的点积得到注意力分数,然后用这些分数加权 Value 向量。这个数学操作本身定义了它们的角色。
     
    如何自然得到分工? 它们具体的“能力”(例如,某个 Query 如何有效地找到相关的 Key,某个 Key 如何有效地表明自身的内容,某个 Value 如何有效地编码有用的信息)是在训练过程中自然学习到的。模型的参数,包括 Q, K, V 线性投影层的权重,会通过反向传播和优化器进行调整,以最小化预测下一个 token 的损失。在这个过程中,这些投影层会学习到权值,使得输入表示 (X) 被投影到能够有效支持注意力计算以提高预测准确性的 Q, K, V 向量空间
     
    这些投影层的权重是在训练开始时初始化的,并且在训练过程中为所有 token 共享(即同一个线性层应用于所有 token 的 X 向量)。所以,不是每个 token 自身有一个固定的初始 Q, K, V 向量,而是每个 token 的初始表示 (X) 通过共享的、已初始化的线性层被投影成 Q, K, V。


  4. 前馈神经网络 (Feed-Forward Network):

    在注意力机制处理完信息后,每个位置的输出会再经过一个简单的前馈神经网络进行进一步的非线性变换,增强模型的表达能力。
    # d_model 是嵌入维度 (n_embd)
    # nhead 是注意力头的数量
    # dim_feedforward 通常是 d_model 的4倍

    nn.TransformerDecoderLayer(
      d_model=n_embd,
      nhead=n_head,
      dim_feedforward=n_embd * 4,
      batch_first=True, # 输入数据的维度顺序是 (批量, 序列, 特征)
      dropout=0.1      # 防止过拟合
    )
  5. 残差连接 (Residual Connections) 和层归一化 (Layer Normalization):

    这些是帮助深度神经网络更好训练的技巧。残差连接允许信息直接“跳过”某些层,避免梯度消失;层归一化则将每层的数据分布稳定在一定范围内,加速训练。

在我们的SimpleGPT模型中,我们堆叠了多个这样的Transformer解码器层 (n_layer个)。信息会逐层传递并被更深入地处理。

self.transformer = nn.ModuleList([
    nn.TransformerDecoderLayer(...) for _ in range(n_layer)
])

# 在前向传播中:
for transformer_layer in self.transformer:
    x = transformer_layer(x, x, tgt_mask=mask) # 注意这里 query, key, value 都是 x
 
 
Transformer 每一个组块的具体计算流程(基于nn.TransformerDecoderLayer 的结构)如下:
 
输入: 每个块的输入是前一个块的输出表示向量(对于第一个块,输入是 token embedding 和 positional embedding 的叠加)。我们称之为 X_input
 
自注意力层: X_input 首先进入自注意力层。在这里,X_input 被投影为 Q, K, V 向量。通过 Q 与 K 的点积、因果掩码、Softmax 和与 V 的乘法(加权求和),自注意力机制输出了一个向量。这个输出向量融合了该 token 自身以及其之前所有 token 的 Value 信息,权重取决于 Query-Key 的相似度
 
自注意力层的输出会加回到原始输入 X_input 上(残差连接),然后进行层归一化。这一步的结果是一个新的表示,我们称之为 X_attn_out这个 X_attn_out 就是经过上下文信息聚合(通过自注意力)后,该 token 位置的表示。
 
X_attn_out 接着进入前馈网络 (FFN)。FFN 是一个简单的、独立作用于每个 token 位置的多层感知机。它允许模型在聚合了上下文信息后,对这些信息进行进一步的、独立的非线性处理和特征转换
 
FFN 的输出会加回到 X_attn_out 上(残差连接),然后再次进行层归一化。这一步的结果就是该 token 位置经过当前 Transformer 块处理后的最终输出表示。这个输出表示会成为下一个 Transformer 块的输入
 
总结来说,token 的表示更新是通过一个层叠的处理管道实现的:输入表示 -> 自注意力层(QKV 投影、点积、掩码、Softmax、加权 Value 聚合)-> 残差连接 + 层归一化 -> 前馈网络 -> 残差连接 + 层归一化 -> 输出表示。每一个块都对 token 的表示进行这样的转换,使其逐步吸收更多上下文信息并进行更复杂的特征提取。

第四步:做出最终预测 - 输出层

经过多层Transformer的“深思熟虑”后,模型对每个输入位置都得到了一个丰富的上下文表示(一个n_embd维的向量)。现在,我们需要将这个表示转换成对下一个字符的预测。

  1. 最后的层归一化:
    x = self.ln_f(x) # self.ln_f = nn.LayerNorm(n_embd)

  2. 线性层 (Linear Layer) / 头部 (Head):
    一个线性层会将Transformer输出的n_embd维向量映射回词汇表大小(vocab_size)的维度。这个输出的每个维度对应词汇表中的一个字符,其值(称为logits)可以看作是模型认为该字符是下一个字符的“原始分数”或“置信度”。
    # self.head = nn.Linear(n_embd, vocab_size)

    logits = self.head(x)

    # logits 的形状是 (批量大小, 序列长度, 词汇表大小)

    例如,对于输入序列的最后一个字符位置,logits中与字符'a'对应的分数可能是2.5,与'b'对应的分数是-0.1,等等。分数越高的字符,模型认为它越有可能是下一个。

第五步:从错误中学习 - 训练模型

模型一开始是“随机”的,它需要通过学习大量的例子来提升预测能力。

  1. 准备输入和目标:
    我们从训练数据中随机抽取一批序列(x)以及它们对应的正确下一个字符序列(y)。
    block_size = 32 # 模型一次处理的序列长度
    # ix: 随机选择8个起始位置
    ix = torch.randint(len(train_data) - block_size, (8,))

    # x: 8个长度为32的输入序列
    x = torch.stack([train_data[i:i+block_size] for i in ix])

    # y: 对应的8个目标序列 (x中每个字符的下一个字符)
    y = torch.stack([train_data[i+1:i+block_size+1] for i in ix])
  2. 计算损失 (Loss):
    模型根据输入 x 得到预测的 logits。我们需要一个方法来衡量这个预测与真实目标 y 之间的差距。这就是损失函数 (Loss Function),常用的是交叉熵损失 (Cross-Entropy Loss)。损失越小,说明模型预测得越准。
    logits = model(x) # 通过模型得到预测

    # logits.view(-1, len(chars)) 将形状变为 (批量*序列长度, 词汇表大小)
    # y.view(-1) 将形状变为 (批量*序列长度)

    loss = nn.functional.cross_entropy(logits.view(-1, vocab_size), y.view(-1))
  3. 优化参数 (Optimization):
    我们的目标是最小化损失。优化器 (Optimizer)(如Adam)会根据损失值,通过反向传播 (Backpropagation) 算法计算出模型中每个参数(权重和偏置)应该如何调整,才能让损失变小一点。
    optimizer = torch.optim.Adam(model.parameters(), lr=3e-4) # lr是学习率
    optimizer.zero_grad() # 清除上一轮的梯度
    loss.backward()       # 计算梯度
    optimizer.step()        # 更新参数
    这个过程会重复很多次(很多step),模型逐渐学会莎士比亚的语言模式。

第六步:生成莎士比亚风格文本 - 推理 (Inference)

当模型训练到一定程度后,我们就可以用它来生成新的文本了。

    • 起始提示 (Prompt):

      我们可以给模型一个起始的文本片段(prompt),比如 "HAMLET: To be or not to be"。如果没给,就从一个默认字符开始。
      tokens = encode(prompt) # 将提示词编码成数字序列
    • 迭代生成:

      模型会根据当前的 tokens 序列(只取最后 block_size 个作为上下文),预测下一个最可能的字符。
      context = torch.tensor([tokens[-block_size:]])
      logits = model(context)[0, -1, :] # 取最后一个时间步的logits
      与训练不同,这里的 [0, -1, :] 表示我们只关心这个批次中(虽然推理时批次大小通常是1)最后一个字符位置的预测,因为我们要预测的是 下一个 字符。
  • 控制生成的多样性:

    直接选择概率最高的字符可能会让生成的文本很单调。我们用一些技巧来增加多样性:
      • Temperature (温度):
        logits = logits / temperature
        温度较低(<1)时,概率分布更“尖锐”,模型倾向于选择高概率字符,生成结果更保守、更像训练数据。
        温度较高(>1)时,概率分布更“平滑”,模型可能选择一些低概率字符,生成结果更有创意,但也可能更混乱。
      • Top-K 采样:
        只从概率最高的 k 个字符中进行采样。这可以避免选到非常不靠谱的字符。
        if top_k > 0:

          # 找到第k大的logit值
            kth_value = torch.topk(logits, top_k)[0][..., -1, None]

          # 将所有小于该值的logit设为负无穷 (采样概率为0)
            indices_to_remove = logits < kth_value

            logits[indices_to_remove] = float('-inf')
      • 让我们解说代码:
    kth_value = torch.topk(logits, top_k)[0][..., -1, None]

torch.topk(logits, top_k):  这个函数会从logits中找出分数最高的top_k个值,并且返回它们的值和它们在原始logits中的位置(索引)。它返回的是一个元组(values, indices)values: 包含了这top_k个最高的分数,默认是降序排列的(从高到低)。indices: 包含了这些最高分数对应的原始位置。 例如,如果logits如上例,top_k = 3,那么torch.topk(logits, 3),可能返回:values = torch.tensor([3.0, 2.5, 1.5])(最高的3个分数),indices = torch.tensor([3, 1, ...]) (这3个分数在原logits中的位置)。[0]: 因为torch.topk返回的是(values, indices)这个元组,我们只关心分数本身,所以用[0]来取出values部分。 现在,我们得到的是values这个张量,即torch.tensor([3.0, 2.5, 1.5])[..., -1, None]:

        • 采样与解码:

          根据调整后的 logits 计算概率分布 (torch.softmax),然后从这个分布中随机采样一个字符作为下一个字符,torch.multinomial(probs, 1) 中的 1 就表示我们只进行一次这样的抽取。将采样到的字符(数字形式)添加到 tokens 序列中。
          probs = torch.softmax(logits, dim=-1)
          next_token = torch.multinomial(probs, 1).item()
          tokens.append(next_token)
          重复这个过程,直到达到最大长度 (max_tokens) 或生成了特定的结束标记(比如换行符)。最后,用 decode 函数将整个 tokens 数字序列转换回人类可读的文本。

    我们的莎士比亚GPT在行动

    脚本中通过调整 temperature 和 top_k 参数,展示了不同风格的生成结果:

        • 保守生成: temperature=0.5, top_k=10 -> 更接近原文,但可能缺乏新意。
        • 平衡生成: temperature=0.8, top_k=20 -> 在忠实和创意间取得平衡。
        • 创意生成: temperature=1.2, top_k=30 -> 可能产生惊喜,也可能不那么连贯。

    由于我们的模型只训练了非常少的步数(50步),生成的质量不会很高,但足以让你看到它学习语言模式的过程。

    从迷你GPT到巨型GPT

    这个莎士比亚生成器是一个非常简化的字符级GPT。现实中的大型语言模型(如ChatGPT)与它的核心原理是相似的,但在以下方面有差异:

        • 模型规模: 参数量可能达到千亿甚至万亿级别(我们的例子只有几十万参数)。
        • 数据量: 训练数据是TB级别的海量文本和代码,远不止莎士比亚全集。
        • Tokenization: 通常使用更高级的词元化方法(如BPE或WordPiece),处理的是词或子词(subword),而不是单个字符,能更好地捕捉语义。
        • 训练技巧: 使用了更复杂的训练策略、更长的训练时间以及巨量的计算资源。
        • 架构细节: 可能包含更精巧的架构调整。
        • 对齐技术: 通过指令微调 (Instruction Fine-tuning) 和人类反馈强化学习 (RLHF) 等技术,使模型输出更符合人类期望、更有用、更无害。

    结语

    通过解剖这个小小的莎士比亚生成器,我们窥见了GPT内部运作的冰山一角。从简单的字符预测任务出发,通过嵌入、强大的Transformer层、巧妙的训练和生成策略,GPT能够学习并模仿复杂的语言模式。

    希望这篇科普能帮你揭开GPT的神秘面纱,理解它并非遥不可及的魔法,而是一系列精妙算法和海量数据共同作用的产物。下一次当你与AI对话时,或许就能想到它背后那些默默计算着的数字和向量了!

    GPT科普系列

How GPT Works: A Shakespearean Text Generator

following Karpathy's Video

Have you ever wondered how a computer can write poetry like Shakespeare? By exploring a simplified GPT (Generative Pre-trained Transformer) model, we can uncover the magic behind text generation. This article guides you through the process with questions to spark curiosity and understanding, using a Python script that generates Shakespearean text as our example.

What’s the Big Idea Behind GPT?

Imagine reading “To be or not to…” and guessing the next word. You’d likely say “be,” right? GPT models predict the next character or word in a sequence based on patterns in the text they’ve seen. Our script uses Shakespeare’s works to train a model to predict the next character. Why characters? They’re simpler than words, with a small vocabulary (65 characters like letters, spaces, and punctuation). What does a model need to turn raw text into predictions?

Turning Text into Numbers

Computers don’t understand letters, so how do we make text “machine-readable”? The script:

  • Reads Shakespeare’s text and lists all unique characters (e.g., ‘a’, ‘b’, ‘,’).
  • Creates mappings: stoi (e.g., ‘a’ → 0) and itos (e.g., 0 → ‘a’).
  • Encodes text into numbers (e.g., “hello” → [7, 4, 11, 11, 14]) and decodes numbers back to text.

Why numbers? Neural networks use math, and numbers are their language. What if two characters had the same number?

Feeding the Model Data

The script loads a preprocessed file (train.bin) with Shakespeare’s text as numbers. Why preprocess? It’s faster than encoding text during training. The model trains on chunks of 32 characters (e.g., “To be or not to be, t”) to predict the next chunk (e.g., “o be or not to be, th”). Why shift by one character? This teaches the model to predict what comes next, like guessing the next word in a sentence.

Building the Brain: The Model’s Architecture

The SimpleGPT model, built with PyTorch, has three key parts:

  1. Embedding Layer: Converts each character into a 128-dimensional vector, like giving it a “personality.” It also adds positional information to track where characters appear in a sequence. Why care about position? Without it, “dog bites man” and “man bites dog” would seem identical.
  2. Transformer Layers: Three layers analyze relationships between characters using:
    • Self-Attention: Focuses on relevant characters (e.g., noticing “to” often follows “be”).
    • Causal Mask: Ensures the model only sees past characters, mimicking how we write. Why prevent “seeing the future”?
    • Feedforward Network: Refines the attention results.
  3. Output Layer: Produces probability scores (logits) for each of the 65 characters, predicting the next one.

How do these parts work together to understand context?

Training the Model

Training teaches the model to make better predictions. The script runs 50 steps, where:

  • It picks eight random 32-character chunks.
  • The model predicts the next character for each position.
  • A loss function measures errors, and an optimizer (Adam) tweaks the model to improve.

Why only 50 steps? It’s a demo—real models train much longer. What might more training achieve?

Generating Shakespearean Text

To generate text, the model:

  • Starts with a prompt (e.g., “HAMLET: To be or not to be”) or a single character.
  • Encodes it into numbers and predicts the next character’s probabilities.
  • Uses temperature (controls creativity) and top-k sampling (limits choices to the k most likely characters) to pick the next character.
  • Repeats until it generates 200 characters or hits a newline.

Why use temperature and top-k? They balance predictable and creative output. What if temperature was very high or top-k was 1?

What Makes It Shakespearean?

The model learns Shakespeare’s patterns—like “thou” or dramatic phrasing—during training. The script shows outputs with different settings:

  • Conservative (temperature=0.5, top_k=10): Mimics common patterns.
  • Balanced (temperature=0.8, top_k=20): Mixes predictability and creativity.
  • Creative (temperature=1.2, top_k=30): Takes risks, possibly less coherent.

Which setting would you choose for a Shakespearean play?

Key Takeaways

This simple GPT shows how larger models like ChatGPT work:

  • Data: Encodes text into numbers.
  • Architecture: Uses embeddings, attention, and masks to process context.
  • Training: Optimizes predictions via loss and updates.
  • Generation: Samples from probabilities to create text.

What are the model’s limits? With brief training and a small size, it’s basic. How could you make it better? More training, larger layers, or more data could help.

Try running the script yourself! Tinker with temperature or top-k to see how the text changes. What kind of text would you want to generate?

立委按:鉴于语言大模型GPT的重要性,特此根据AI大神Karpathy的nanoGPT讲座,编纂此科普系列,计五篇,一篇没有代码和数学公式,是最通俗的科普。其他四篇包括一篇英文,均附带可验证的Python代码,并给予不同角度的详细解说,面对有一定工程背景的对象。

GPT科普系列

从零实现莎士比亚风 GPT科普解说

立委按:鉴于语言大模型GPT的重要性,特此根据AI大神Karpathy的nanoGPT讲座,编纂此科普系列,计五篇,一篇没有代码和数学公式,是最通俗的科普。其他四篇包括一篇英文,均附带可验证的Python代码,并给予不同角度的详细解说,面对有一定工程背景的对象。

下面这篇科普文章,以Karpathy讲座“从零实现莎士比亚风 GPT”为例,把每一行代码背后的思路拆透,感受“技术硬核”的魅力。


一、引子:为什么要自己写一个“小 GPT”?

  • 脚踏实地:商用 GPT 模型动辄上百亿参数,看不清内部;自己写一个小模型,才有机会把每个细节掰开了啃。
  • 入门示范:字符级模型更轻量,50 步训练就能出点儿“莎士比亚味”,足以演示 Transformer 的核心套路。
  • 学习曲线:从准备数据、编码、搭网络、训练到采样生成,完整跑一趟,就能圆满理解 GPT 的「流水线」。

二、数据篇:把“文字”编码成“数字”

  1. 原始文本
    把莎士比亚全集放到 data/shakespeare_char/input.txt,整个文本可能上百万字符。
  2. 字符表(Vocabulary)
    python
    chars = sorted(list(set(open(...).read())))
    stoi = {ch:i for i,ch in enumerate(chars)}
    itos = {i:ch for i,ch in enumerate(chars)}
  3. 代码解释:
    • set (字符集合)自然去重后有 65 个字符,包括字母、标点、换行符等。
    • stoiitos 分别是一一映射,可做「文字 ↔ 索引」互转,即编码解码。
  4. 高效加载
    训练前把所有文本编码成 uint16 二进制文件 train.bin,运行时直接:
    train_data = torch.frombuffer(open(…,'rb').read(), dtype=torch.uint16).long()
    一次性读入张量,快又省事。

三、模型篇:一个“小型 GPT”长啥样

class SimpleGPT(nn.Module):
    def __init__(self, vocab_size, n_embd=128, n_head=4, n_layer=3):
        super().__init__()
        # 1. Token 嵌入 & 位置嵌入
        self.embedding     = nn.Embedding(vocab_size, n_embd)
        self.pos_embedding = nn.Embedding(1000, n_embd)
        # 2. N 层 TransformerDecoderLayer
        self.transformer = nn.ModuleList([
            nn.TransformerDecoderLayer(
                d_model=n_embd, nhead=n_head,
                dim_feedforward=n_embd*4,
                batch_first=True, dropout=0.1
            ) for _ in range(n_layer)
        ])
        # 3. 归一化 + 线性头
        self.ln_f = nn.LayerNorm(n_embd)
        self.head = nn.Linear(n_embd, vocab_size)
  • Embedding
    • Token 嵌入:把每个字符索引映射成 128 维向量;
    • 位置嵌入:告诉模型「这个字符是句子中的第几位」。
  • TransformerDecoderLayer
    • 多头自注意力(Multi-Head Self-Attention):在无掩码时,让每个位置都能「看」到其他位置,用不同的“视角”捕捉语义关联;
    • 前馈网络 FFN:FFN内部是两层全连接,扩大特征维度后再压回增强非线性表达:第一层linear1把维度从 n_embd(128)→ dim_feedforward(512);Relu激活;第二层linear2把维度再从 dim_feedforward(512)→ n_embd(128));
    • 残差连接 + LayerNorm + Dropout:保证信息流通、稳定训练、防止过拟合。
    • 两层全连接(Feed-Forward Network, FFN)和残差连接(Residual Connection)部分被 PyTorch 的 nn.TransformerDecoderLayer 给封装起来了。
    • 每个 block 里总共是两层前馈线性变换(加一个激活);*4 把隐藏层的宽度调成原来的 4 倍(128*4=512)。 n_head=4 是指 注意力头 数量是 4 个。
  • 输出头
    最后把每个位置的 128 维特征 → 65 个字符的分数(logits),为下一步采样做准备。

每个 TransformerBlock(TransformerDecoderLayer)内部:

# 注意力子层(Self-Attention)
_attn_output = self.self_attn(x, x, x, attn_mask=tgt_mask)
x = x + self.dropout1(_attn_output)    # 残差 + Dropout
x = self.norm1(x)                      # LayerNorm

# 前馈全连接子层(Feed-Forward)
_ffn_output = self.linear2(self.dropout(self.activation(self.linear1(x))))
x = x + self.dropout2(_ffn_output)     # 残差 + Dropout
x = self.norm2(x)                      # LayerNorm

残差连接(Residual Connection)在哪里?

同样,TransformerDecoderLayer 在每个子层的输出上都做了:

x = x + SubLayer(x)

也就是将子层(注意力/前馈)的输出与原输入相加,然后再做 LayerNorm。这能让梯度更容易向前/向后流动,避免深层网络训练困难。


为什么常常用这样的封装?

  • 代码简洁:把注意力、FFN、残差、归一化、Dropout——所有常见操作都打包好,调用一行就能用。
  • 可配置:你可以在构造时传参数,比如 activation='gelu'norm_first=True(预归一化)等。

想要完全掌握内部细节,你可以自己写一个自定义的 DecoderLayer,大概长这样:

class MyDecoderLayer(nn.Module):
    def __init__(self, d_model, nhead, dim_ff):
        super().__init__()
        self.self_attn = nn.MultiheadAttention(d_model, nhead, batch_first=True)
        self.linear1   = nn.Linear(d_model, dim_ff)
        self.linear2   = nn.Linear(dim_ff, d_model)
        self.norm1     = nn.LayerNorm(d_model)
        self.norm2     = nn.LayerNorm(d_model)
        self.dropout   = nn.Dropout(0.1)
        self.act       = nn.ReLU()

    def forward(self, x, mask=None):
        # 注意力 + 残差 + 归一化
        attn_out, _ = self.self_attn(x, x, x, attn_mask=mask)
        x = x + self.dropout(attn_out)
        x = self.norm1(x)
        # FFN + 残差 + 归一化
        ffn_out = self.linear2(self.dropout(self.act(self.linear1(x))))
        x = x + self.dropout(ffn_out)
        x = self.norm2(x)
        return x

把 N 层这样的 MyDecoderLayer 串起来,就和 nn.TransformerDecoderLayer 是一模一样的套路。

这样你就明确地知道:GPT 的每一层都是「注意力→残差→LayerNorm→前馈→残差→LayerNorm」的循环组合。希望这下彻底搞明白了!

四、前向传播:一步步把输入变预测

def forward(self, x):
    b, t = x.shape                  # batch 大小 b,序列长度 t
    pos  = torch.arange(t).unsqueeze(0)
    x = self.embedding(x) + self.pos_embedding(pos)
    mask = torch.triu(torch.ones(t, t), diagonal=1).bool()
    for layer in self.transformer:
        x = layer(x, x, tgt_mask=mask)
    x = self.ln_f(x)
    return self.head(x)             # (b, t, vocab_size)
  1. 拼接嵌入
    每个字符向量 + 对应位置向量,融合语义与顺序信息。
  2. 因果掩码
    用上三角布尔矩阵屏蔽未来位置,确保模型只能用“历史”信息预测“下一步”。
  3. 层叠计算
    N 层解码器层交替执行「注意力→前馈→残差→归一化」,不断提炼上下文特征。
  4. 输出 logits
    每个位置都对应一个 vocab_size 维的分数向量,代表模型对下一个字符的「喜好程度」。

五、训练篇:教模型学“接龙文字”

optimizer = torch.optim.Adam(model.parameters(), lr=3e-4)
model.train()
for step in range(50):
    # 1. 随机抓 8 段长度为 block_size 的序列
    ix = torch.randint(len(train_data)-block_size, (8,))
    x  = torch.stack([train_data[i:i+block_size]       for i in ix])
    y  = torch.stack([train_data[i+1:i+block_size+1]   for i in ix])
    # 2. 前向 + 损失
    logits = model(x)
    loss   = nn.functional.cross_entropy(
                logits.view(-1, vocab_size), y.view(-1)
             )
    # 3. 反向 + 更新
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()
    if step%10==0: print(f"Step {step}: loss={loss.item():.4f}")
  • 随机采样:每次从不同起点取小段,让模型见识多种上下文,避免只学到固定模式。
  • 交叉熵损失:衡量预测分布 vs. 真实下一个字符的差距。
  • Adam 优化器:智能调整各参数的学习率,加快收敛。

六、生成篇:让模型「写莎翁诗」

def generate_text(prompt="", max_tokens=200, temperature=0.8, top_k=20):
    model.eval()
    tokens = encode(prompt) if prompt else [encode("ROMEO:")[0]]
    with torch.no_grad():
        for _ in range(max_tokens):
            ctx    = torch.tensor([tokens[-block_size:]])
            logits = model(ctx)[0, -1]       # 取最新位置的 logits
            logits = logits / temperature    # 温度调节
            if top_k>0:
                kth, _ = torch.topk(logits, top_k)
                logits[logits < kth[-1]] = -float('inf')
            probs = torch.softmax(logits, dim=-1)
            nxt   = torch.multinomial(probs, 1).item()
            tokens.append(nxt)
            if nxt==encode('\n')[0] and len(tokens)>10: break
    return decode(tokens)
  • 温度(Temperature)
    • <1:分布更陡峭,生成更“保守”;
    • >1:分布更平坦,生成更“大胆”。
  • Top-k 采样
    只保留概率最高的 k 个候选,把其余置零,然后再做随机采样,平衡“连贯”与“创造”。

七、核心技术要点小结

技术环节功能与作用
嵌入层离散字符→连续向量,便于神经网络处理
位置编码注入顺序信息,让模型区分“先后”
自注意力动态计算序列各位置间的相互影响,捕捉长程依赖
因果掩码严格屏蔽「未来信息」,模拟人写作时只能一步步推进
前馈网络增加非线性表达能力
残差+LayerNorm保持梯度稳定,助力深层网络收敛
温度 & Top-k控制生成文本的“保守度”与“多样性”

八、结语

  • 小模型 ≠ 小原理:虽然参数量小,但骨架设计、数据流程、采样策略与大规模 GPT 完全一致。
  • 动手才真懂:自己从头跑一遍,不仅能看懂代码,更能体会每一层、每一个技巧为何如此设计。
  • 一路上升:掌握这些基础,你就拥有了阅读和改造任何 Transformer-based 模型的「通行证」。

下次想要扩充到单词级、加上多 GPU、混合精度训练,或者接入更大语料,就能顺理成章地在这些模块之上“造船”了。Go build your own GPT!

GPT科普系列

从0实现并理解GPT

根据Karpathy莎士比亚为例创建一个快速的文本生成演示

立委按:鉴于语言大模型GPT的重要性,特此根据AI大神Karpathy的nanoGPT讲座,编纂此科普系列,计五篇,一篇没有代码和数学公式,是最通俗的科普。其他四篇包括一篇英文,均附带可验证的Python代码,并给予不同角度的详细解说,面对有一定工程背景的对象。

cat > shakespeare_generator.py << 'EOF'
import torch
import torch.nn as nn
import pickle
import os

print("莎士比亚风格文本生成器")
print("=" * 50)

加载数据和词汇表

data_dir = 'data/shakespeare_char'
with open(os.path.join(data_dir, 'meta.pkl'), 'rb') as f:
meta = pickle.load(f)

获取编解码函数

chars = sorted(list(set(open(os.path.join(data_dir, 'input.txt'), 'r').read())))
stoi = {ch: i for i, ch in enumerate(chars)}
itos = {i: ch for i, ch in enumerate(chars)}

print(f"词汇表大小: {len(chars)}")
print(f"字符集: {''.join(chars[:20])}…")

def encode(s):
return [stoi[c] for c in s]

def decode(l):
return ''.join([itos[i] for i in l])

加载训练数据

train_data = torch.frombuffer(
open(os.path.join(data_dir, 'train.bin'), 'rb').read(),
dtype=torch.uint16
).long()

print(f"📖 训练数据长度: {len(train_data):,} tokens")

超简单的字符级语言模型

class SimpleGPT(nn.Module):
def init(self, vocab_size, n_embd=128, n_head=4, n_layer=3):
super().init()
self.embedding = nn.Embedding(vocab_size, n_embd)
self.pos_embedding = nn.Embedding(1000, n_embd)
self.transformer = nn.ModuleList([
nn.TransformerDecoderLayer(
d_model=n_embd,
nhead=n_head,
dim_feedforward=n_embd * 4,
batch_first=True,
dropout=0.1
) for _ in range(n_layer)
])
self.ln_f = nn.LayerNorm(n_embd)
self.head = nn.Linear(n_embd, vocab_size)

def forward(self, x):
    b, t = x.shape
    pos = torch.arange(0, t, dtype=torch.long).unsqueeze(0)

    x = self.embedding(x) + self.pos_embedding(pos)

    # 创建因果mask
    mask = torch.triu(torch.ones(t, t), diagonal=1).bool()

    for transformer in self.transformer:
        x = transformer(x, x, tgt_mask=mask)

    x = self.ln_f(x)
    logits = self.head(x)
    return logits

创建和训练模型

print("\n 创建模型…")
model = SimpleGPT(vocab_size=len(chars))
optimizer = torch.optim.Adam(model.parameters(), lr=3e-4)

print(f"模型参数: {sum(p.numel() for p in model.parameters()):,}")

快速训练

print("\n 快速训练…")
block_size = 32
model.train()

for step in range(50): # 只训练50步,快速看效果
ix = torch.randint(len(train_data) - block_size, (8,))
x = torch.stack([train_data[i:i+block_size] for i in ix])
y = torch.stack([train_data[i+1:i+block_size+1] for i in ix])

logits = model(x)
loss = nn.functional.cross_entropy(logits.view(-1, len(chars)), y.view(-1))

optimizer.zero_grad()
loss.backward()
optimizer.step()

if step % 10 == 0:
    print(f"  Step {step:2d}: loss = {loss.item():.4f}")

print("\n 开始生成莎士比亚风格文本…")

def generate_text(prompt="", max_tokens=200, temperature=0.8, top_k=20):
model.eval()

# 编码提示词
if prompt:
    tokens = encode(prompt)
else:
    tokens = [encode("ROMEO:")[0]]  # 默认以ROMEO开始

with torch.no_grad():
    for _ in range(max_tokens):
        # 取最后block_size个tokens
        context = torch.tensor([tokens[-block_size:]])
        logits = model(context)[0, -1, :]

        # 应用temperature
        logits = logits / temperature

        # Top-k采样
        if top_k > 0:
            indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
            logits[indices_to_remove] = float('-inf')

        probs = torch.softmax(logits, dim=-1)
        next_token = torch.multinomial(probs, 1).item()
        tokens.append(next_token)

        # 如果生成了换行符,可能是一个好的停止点
        if len(tokens) > 10 and next_token == encode('\n')[0]:
            break

return decode(tokens)

生成不同风格的文本

print("\n" + "="60) print(" 生成结果展示") print("="60)

print("\n1️⃣ 保守生成 (temperature=0.5, top_k=10):")
torch.manual_seed(42)
text1 = generate_text("HAMLET: To be or not to be", max_tokens=100, temperature=0.5, top_k=10)
print(f"'{text1}'")

print("\n2️⃣ 平衡生成 (temperature=0.8, top_k=20):")
torch.manual_seed(42)
text2 = generate_text("JULIET: Romeo, Romeo", max_tokens=100, temperature=0.8, top_k=20)
print(f"'{text2}'")

print("\n3️⃣ 创意生成 (temperature=1.2, top_k=30):")
torch.manual_seed(42)
text3 = generate_text("KING: What news", max_tokens=100, temperature=1.2, top_k=30)
print(f"'{text3}'")

print("\n4️⃣ 无提示生成:")
torch.manual_seed(123)
text4 = generate_text("", max_tokens=150, temperature=0.8, top_k=25)
print(f"'{text4}'")

print(f"\n 参数解释:")
print(f"• temperature越低 → 更保守,更像训练数据")
print(f"• temperature越高 → 更有创意,但可能更乱")
print(f"• top_k越小 → 只从最可能的k个字符中选择")
print(f"• 对于65字符的词汇表,top_k=5到30比较合理")

print(f"\n 注意: 由于只训练了50步,质量不会很高")
print(f" 但您可以看到模型学习语言模式的过程!")
EOF

运行莎士比亚生成器

python shakespeare_generator.py

下面逐行详细解释这个莎士比亚生成器脚本,好理解GPT的实现原理。

🔍 逐行代码解析:莎士比亚GPT生成器

1. 导入和基础设置

import torch
import torch.nn as nn
import pickle
import os

详细解释

  • torch: PyTorch核心库,提供张量计算和神经网络功能
  • torch.nn: 神经网络模块,包含各种层(Linear, Embedding等)
  • pickle: Python序列化库,用于加载.pkl格式的数据文件
  • os: 操作系统接口,用于文件路径操作

2. 打印标题和设置

print(" 莎士比亚风格文本生成器")
print("=" * 50)

解释:简单的用户界面,"=" * 50创建50个等号的分隔线

3. 数据加载部分

# 加载数据和词汇表
data_dir = 'data/shakespeare_char'
with open(os.path.join(data_dir, 'meta.pkl'), 'rb') as f:
    meta = pickle.load(f)

详细解释

  • data_dir: 设置数据目录路径
  • os.path.join(): 跨平台的路径连接,相当于 data_dir + '/' + 'meta.pkl'
  • with open(..., 'rb'): 以二进制读取模式打开文件,with确保文件自动关闭
  • pickle.load(f): 加载.pkl文件,读取之前保存的字符对应表

4. 字符编码设置

# 获取编解码函数
chars = sorted(list(set(open(os.path.join(data_dir, 'input.txt'), 'r').read())))
stoi = {ch: i for i, ch in enumerate(chars)}
itos = {i: ch for i, ch in enumerate(chars)}

逐行解析

# 第1行:获取所有唯一字符
chars = sorted(list(set(open(...).read())))
  • open(...).read(): 读取整个莎士比亚文本文件
  • set(...): 创建集合,自动去除重复字符
  • list(...): 转换为列表
  • sorted(...): 按ASCII码排序,确保字符顺序固定
# 第2行:创建字符到索引的映射stoi(string2integer)
stoi = {ch: i for i, ch in enumerate(chars)}
  • enumerate(chars): 产生 (索引, 字符) 对
  • 字典推导式创建映射:{'a': 0, 'b': 1, 'c': 2, ...}
# 第3行:创建索引到字符的映射itos(integer2string)
itos = {i: ch for i, ch in enumerate(chars)}
  • 反向映射:{0: 'a', 1: 'b', 2: 'c', ...}

5. 编解码函数

def encode(s):
    return [stoi[c] for c in s]

def decode(l):
    return ''.join([itos[i] for i in l])

详细解释

def encode(s):
    return [stoi[c] for c in s]
  • 输入:字符串 "hello"
  • 过程:['h', 'e', 'l', 'l', 'o'][104, 101, 108, 108, 111]
  • 输出:数字列表
def decode(l):
    return ''.join([itos[i] for i in l])
  • 输入:数字列表 [104, 101, 108, 108, 111]
  • 过程:[104, 101, 108, 108, 111]['h', 'e', 'l', 'l', 'o']
  • 输出:字符串 "hello"

6. 加载训练数据

train_data = torch.frombuffer(
    open(os.path.join(data_dir, 'train.bin'), 'rb').read(), 
    dtype=torch.uint16
).long()

逐步解析

  1. open(..., 'rb').read(): 以'rb'(read-binary)二进制模式读取train.bin文件,得到的是二进制原始字节
  2. torch.frombuffer(..., dtype=torch.uint16): 将二进制数据转换为16位无符号整数张量,uint16 = 16位无符号整数 = 0到65535的数字
  3. .long(): 转换为长整型张量(64位),long() = 64位长整数,训练时常用

为什么这样做?

  • train.bin是预处理好的数字化文本数据
  • 每个字符已经被转换为对应的索引数字
  • 直接加载比重新编码要快得多
  • train.bin文件 → 读出字节 → 变成数字列表 → 转换成PyTorch能用的格式

7. GPT模型定义

class SimpleGPT(nn.Module):
    def __init__(self, vocab_size, n_embd=128, n_head=4, n_layer=3):
        super().__init__()

详细解释

  • nn.Module: PyTorch中所有神经网络模块的基类
  • super().__init__(): 调用父类构造函数
  • 参数:
    • vocab_size: 词汇表大小(65个字符)
    • n_embd=128: 嵌入维度(每个字符用128维向量表示)
    • n_head=4: 注意力头数量
    • n_layer=3: Transformer层数

嵌入层

self.embedding = nn.Embedding(vocab_size, n_embd)
self.pos_embedding = nn.Embedding(1000, n_embd)

详细解释

self.embedding = nn.Embedding(vocab_size, n_embd)
  • 创建一个查找表:vocab_size × n_embd 的矩阵
  • 每个字符索引对应一个128维向量
  • 例如:字符 'a' (索引0) → 128维向量 [0.1, -0.2, 0.3, ...]
self.pos_embedding = nn.Embedding(1000, n_embd)
  • 位置嵌入:告诉模型每个字符在序列中的位置
  • 支持最大1000个位置
  • 位置0 → 向量1,位置1 → 向量2,...

Transformer层

self.transformer = nn.ModuleList([
    nn.TransformerDecoderLayer(
        d_model=n_embd,
        nhead=n_head,
        dim_feedforward=n_embd * 4,
        batch_first=True,
        dropout=0.1
    ) for _ in range(n_layer)
])

详细解释

  • nn.ModuleList: 存储多个神经网络层的容器
  • nn.TransformerDecoderLayer: PyTorch内置的Transformer解码器层
  • 参数详解:
    • d_model=n_embd: 输入维度(128)
    • nhead=n_head: 多头注意力的头数(4)
    • dim_feedforward=n_embd * 4: 前馈网络维度(512)
    • batch_first=True: 维度顺序以批次维度在前 (batch, seq, feature),先选句子,再选词元,数据排列像 [句子1][句子2][句子3]
    • 数据的三个维度batch = 同时处理几个句子;seq = 每个句子有多少个词元;feature = 每个词元用多少个数字表示(例如128个数字)
    • dropout=0.1: 10%的dropout防止过拟合

输出层

self.ln_f = nn.LayerNorm(n_embd)
self.head = nn.Linear(n_embd, vocab_size)

详细解释

  • nn.LayerNorm(n_embd): 层归一化,数据清洗,稳定训练。数据'洗干净' - 平均值接近0,标准差接近1,避免数字太大或太小,给数据做标准化处理。
  • nn.Linear(n_embd, vocab_size): 线性层把特征变成字符概率,将128维特征映射到65个字符的概率

8. 前向传播函数

def forward(self, x):
    b, t = x.shape
    pos = torch.arange(0, t, dtype=torch.long).unsqueeze(0)

详细解释

  • 标量(0维),向量(1维),矩阵(2维),张量(n维向量)
  • x.shape: 输入张量的形状,例如 (batch_size=8, seq_len=32)
  • b, t = x.shape: 解包得到批次大小和序列长度
  • torch.arange(0, t): 创建位置索引 [0, 1, 2, ..., t-1]
  • .unsqueeze(0): 增加一个维度,变成 (1, t)
x = self.embedding(x) + self.pos_embedding(pos)

详细解释

  • self.embedding(x): 字符嵌入,形状 (b, t, n_embd)
  • self.pos_embedding(pos): 位置嵌入,形状 (1, t, n_embd)
  • 相加得到最终嵌入:字符信息 + 位置信息
# 创建因果mask
mask = torch.triu(torch.ones(t, t), diagonal=1).bool()

详细解释

  • torch.ones(t, t): 创建全1的 t×t 矩阵
  • torch.triu(..., diagonal=1): 取上三角矩阵(对角线上方)
  • .bool(): 转换为布尔值
  • 作用:防止模型"偷看"未来的字符

举例:如果t=4,mask矩阵是:

[[False, True,  True,  True ],
 [False, False, True,  True ],
 [False, False, False, True ],
 [False, False, False, False]]
for transformer in self.transformer:
    x = transformer(x, x, tgt_mask=mask)

详细解释

  • 循环通过每个Transformer层
  • transformer(x, x, tgt_mask=mask):
    • 第一个x: 查询(query)
    • 第二个x: 键值(key, value)
    • tgt_mask=mask: 应用因果掩码
x = self.ln_f(x)
logits = self.head(x)
return logits

详细解释

  • self.ln_f(x): 最终层归一化
  • logits = self.head(x): 线性变换,输出每个字符的未归一化概率(logits),表示模型对next token的"偏好程度",logits[0, 31, :] 就是第0个句子第31个位置对65个字符的评分
    等价于:logits = x @ W + b
    输入特征: [0.2, -0.1, 0.8, 0.3] (128维)
    权重矩阵: W (128×65)
    偏置向量: b (65维)
    输出logits: [2.1, -0.5, 1.3, ...] (65维)
  • 返回形状:(batch_size, seq_len, vocab_size)
  • head层就像一个"翻译器"
    输入:复杂的上下文特征表示(模型的"理解")
    输出:简单直观的字符选择评分(具体的"预测")
    作用:将模型的智慧转换为可操作的概率分布
  • head层是模型的"最后一公里",将前面所有层的计算结果汇总成最终的字符选择,决定了模型生成文本的质量和多样性
  • 关键:logits不是最终答案,而是为后续采样提供的概率性依据,通过softmax转换和采样策略,最终生成具体的字符。
  • 预测流程:
    输入: "hello wor" ↓
    嵌入层: 转换为向量序列 ↓
    Transformer: 处理上下文,每个位置得到特征向量 ↓
    最后位置(即next token)特征: [0.2, -0.1, 0.8, 0.3, ...] (128维) ↓
    head层: 线性变换 ↓
    logits: 对每个字符的评分 [2.1, -0.5, 1.3, ...] (65维) ↓
    softmax: 转换为概率分布 ↓
    采样: 选择下一个字符 "l"

9. 模型创建和训练

model = SimpleGPT(vocab_size=len(chars))
optimizer = torch.optim.Adam(model.parameters(), lr=3e-4)

详细解释

  • SimpleGPT(vocab_size=len(chars)): 创建模型实例
  • torch.optim.Adam: Adam优化器,自适应学习率,根据梯度历史智能调整
  • model.parameters(): 获取所有可训练参数
  • lr=3e-4: 学习率 0.0003,默认经验值
block_size = 32
model.train()

详细解释

  • block_size = 32: 序列长度(训练窗口大小),模型一次处理32个字符
  • model.train(): 设置模型为训练模式(启用dropout等)

10. 训练循环

for step in range(50):
    ix = torch.randint(len(train_data) - block_size, (8,))
    x = torch.stack([train_data[i:i+block_size] for i in ix])
    y = torch.stack([train_data[i+1:i+block_size+1] for i in ix])

逐行解析

ix = torch.randint(len(train_data) - block_size, (8,)) 
  • 随机选择8个起始位置,假设len(train_data) = 1000block_size = 32:torch.randint(1000-32=968, (8,)), 这里,(8,) 是张量的 shape
  • 确保不超出数据边界,每个位置都在 [0, 967] 范围内
  • 假设在随机起始位置156:
    输入x = train_data[156:188] # 32个token
    目标y = train_data[157:189] # 下一个32个token
为什么需要随机采样?
优势:
避免顺序偏见:不总是从头开始训练
增加数据多样性:每个epoch看到不同的序列组合
提高泛化能力:模型学会处理各种上下文
加速收敛:随机性帮助跳出局部最优

对比: 
❌ 顺序采样起始位置: [0, 32, 64, 96, 128, 160, 192, 224]
总是相同的序列,缺乏多样性

✅ 随机采样  
起始位置,例如: [156, 743, 12, 891, 445, 623, 88, 334]
# 每次都不同,增加训练多样性



x = torch.stack([train_data[i:i+block_size] for i in ix])
  • 从每个起始位置取32个字符作为输入
  • torch.stack: 将列表转换为张量
y = torch.stack([train_data[i+1:i+block_size+1] for i in ix])
  • 取下一个字符作为目标(预测目标)
  • 这是语言模型的核心:预测下一个字符

举例

  • 输入x: "To be or not to be, that is th"
  • 目标y: "o be or not to be, that is the"
logits = model(x)
loss = nn.functional.cross_entropy(logits.view(-1, len(chars)), y.view(-1))

详细解释

  • model(x): 前向传播得到预测
  • view/reshape = 重新排列相同的数据
  • 为什么要reshape:交叉熵函数期望输入格式:
    • logits: (N, C) - N个样本,码本中的C个类别
  • logits.view(-1, len(chars)): 重塑为 (batch*seq, vocab_size),在形状参数中,-1 作为维度大小本来就无意义,PyTorch定义它为自动计算维度大小,相当于 auto
  • y.view(-1): 重塑为 (batch*seq,)
  • cross_entropy: 计算交叉熵损失
optimizer.zero_grad()
loss.backward()
optimizer.step()

详细解释

  • zero_grad(): 清零之前的梯度
  • backward(): 反向传播计算梯度
  • step(): 更新模型参数

11. 文本生成函数

def generate_text(prompt="", max_tokens=200, temperature=0.8, top_k=20):
    model.eval()

详细解释

  • model.eval(): 设置为评估模式(关闭dropout)
if prompt:
    tokens = encode(prompt)
else:
    tokens = [encode("ROMEO:")[0]]  # 只要'R',让模型自由发挥

详细解释

  • 如果有提示词,编码为数字列表作为上文(为了预测下一个token)
  • 否则用"ROMEO:"的第一个字符开始编码为上文,也可以不加[0]:则用"ROMEO:" 开始生成
with torch.no_grad():
    for _ in range(max_tokens):
        context = torch.tensor([tokens[-block_size:]])
        logits = model(context)[0, -1, :]

详细解释

  • torch.no_grad(): 这是推理阶段不是训练阶段,禁用梯度计算(节省内存),只要结果,不存历史
  • tokens[-block_size:]: 取最后32个字符作为上下文
  • logits = model(context)[0, -1, :]:
    • [0, -1, :]: 取第一个批次的最后一个位置的所有词汇概率,为了 next token prediction 采样,next token 即最后一个位置。
# 应用temperature
logits = logits / temperature

详细解释

  • temperature < 1: 让分布更尖锐,更保守
  • temperature > 1: 让分布更平坦,更随机
  • temperature = 1: 不改变分布
# Top-k采样
if top_k > 0:
    indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
    logits[indices_to_remove] = float('-inf')

详细解释

  • torch.topk(logits, top_k): 找到最大的k个值
  • indices_to_remove: 标记要移除的索引
  • logits[indices_to_remove] = float('-inf'): 设置为负无穷(概率为0)
probs = torch.softmax(logits, dim=-1)
next_token = torch.multinomial(probs, 1).item()
tokens.append(next_token)

详细解释

  • torch.softmax: 将logits转换为概率分布
  • torch.multinomial: 根据概率分布随机采样
  • .item(): 将张量转换为Python数字
  • tokens.append(): 添加到生成序列

12. 生成展示

torch.manual_seed(42)
text1 = generate_text("HAMLET: To be or not to be", max_tokens=100, temperature=0.5, top_k=10)

详细解释

  • torch.manual_seed(42): 设置随机种子确保可重现
  • 不同参数组合展示不同的生成风格

核心概念总结

GPT的本质

  1. 输入:字符序列 → 数字序列
  2. 处理:Transformer层处理上下文信息
  3. 输出:预测下一个字符的概率分布
  4. 生成:根据概率分布采样下一个字符

关键技术

  • 嵌入:字符 → 向量
  • 位置编码:告诉模型字符位置
  • 自注意力:字符之间的关系
  • 因果掩码:防止看到未来
  • 温度采样:控制创造性
  • Top-k采样:控制选择范围

这就是GPT的完整实现,每一行代码都有其特定的作用,组合起来就能生成连贯的莎士比亚文风的文本。

GPT科普系列

Decoding the New EMPO Reasoning Paradigm

The Right Question is Half the Answer,
The Other Half lies in LLM's Semantic Coherence

Large Language Models (LLMs) are constantly rewriting the rules of AI with their astonishing reasoning abilities. Yet, the path to even stronger reasoning is often paved with expensive "gold"—manually labeled reasoning steps, verified answers, or bespoke reward models. These reinforcement methods, rooted in supervised learning, work, but they hit bottlenecks in cost and scalability.

Rewind to this Lunar New Year, when DeepSeek's R1-Zero, a result-driven, supervised reinforcement approach, made waves. We debated its underlying mechanics, converging on a shared understanding: The essence of technologies like Chain-of-Thought (CoT) is to build a "slow-thinking" information bridge between a query and a response in complex tasks. Think of it as a gentle "ramp", designed to lower perplexity, transforming problems with daunting information gaps—unsolvable by "fast thinking"—into something smooth and solvable.

Now, a new paper from Tianjin University and Tencent AI Lab, "Right Question is Already Half the Answer: Fully Unsupervised LLM Reasoning Incentivization," takes this line of thought a step further—a step both radical and elegant. It introduces EMPO (Entropy Minimized Policy Optimization), a fully unsupervised framework for reinforcement reasoning. And the kicker? Its performance reportedly rivals methods that do rely on golden answers.

This paper is a refreshing read. No black magic, no convoluted theories. It’s like a fresh breeze blowing through the landscape of unsupervised learning. It further validates our hunch: give the model a "field" to play in, and it will autonomously find the smoothest path towards entropy reduction.

Frankly, DeepSeek R1-Zero was stunning enough, proving machines could learn autonomously, generating their own data to boost their intelligence. This work feels like "Zero-Squared": Machines can now seemingly learn answers just from questions. It's a bit scary if you think about it. Unsupervised learning has been around for years, but after fueling the pre-trained LLM storm via self-supervised learning, seeing it reach this level of magic in reasoning is truly eye-opening.

EMPO's Midas Touch: Minimizing Semantic Entropy

The core idea behind EMPO is simple: Instead of telling the model "what is right," why not let it pursue "what is consistent"? It posits that a powerful reasoning model should produce outputs that are stable and semantically aligned. How do we measure this alignment? Through Semantic Entropy.

This isn't your classic Shannon entropy, which focuses on the surface token string and can be easily thrown off by phrasing variations. Semantic entropy operates at the level of meaning. Here’s how EMPO does it:

  1. Sample: For a single question, let the current model generate multiple (say, G) reasoning processes and answers, step-by-step.
  2. Cluster: Using simple rules (like regex for math) or a compact verifier model, cluster these G outputs based on their meaning. For example, "The answer is 42" and "Final result: 42" land in the same bucket, regardless of the path taken.
  3. Calculate Entropy: Based on these clusters, calculate the probability distribution of each "meaning bucket" and calculate the overall semantic entropy. If all answers converge to one meaning, entropy is minimal; if they're all over the place, it's high.
  4. Reinforce: Use this "semantic consistency" (low entropy) as an intrinsic reward signal within an RL framework (like GRPO). The model gets a pat on the back if its output belongs to the most "mainstream," most consistent cluster. Optimization then incentivizes the model to generate outputs that lower the overall semantic entropy.

In short, EMPO encourages the model: "Within your own answer space, find the most 'popular' view, the one you're most sure about, and double down on it!"

Piercing the Veil: Wisdom and Real-World Gotchas

EMPO's elegance doesn't mean it's without its nuances. The paper highlights a few key insights and practicalities:

  • Entropy Thresholding (The "Catch"): This is crucial. Just blindly minimizing entropy could lead the model down a rabbit hole, overfitting. EMPO therefore introduces an entropy threshold: it only applies CoT reinforcement to questions with moderate entropy. This filters out cases where the model is either too uncertain (high entropy, too chaotic to learn from) or already too confident (low entropy, no need to push further and risk overconfidence). This ensures stability and effectiveness.
  • The Power of the Base Model: EMPO is more of an elicitor than a creator of abilities. The potential for these reasoning paths is likely laid down during pre-training. EMPO's success hinges heavily on a strong base model. The contrast between Qwen (where EMPO worked directly, likely due to pre-training with QA pairs, seeding its potential) and Llama (which needed an SFT "warm-up" before EMPO works) drives this point home. Unsupervised post-training isn't a magic wand; it builds only on a solid foundation.
  • No <cot> Tags Required: EMPO doesn't even need explicit <cot> tags as format rewards. A simple prompt like, Please resolve it step by step and put the final answer in {...}. is enough to provide the "space" for the model to explore thinking and refine its reasoning.

The Unsupervised Dividend: Why EMPO Matters

EMPO shows that even without any external answers, we can significantly boost LLM reasoning through a simple, elegant, and intrinsically motivated mechanism. It's like unlocking a universal "data quality dividend". The only entry fee is feeding the system questions and applying simple clustering – and most likely, accuracy improvements become possible.

The paper's title begins, "Right question is already half the answer." We can extend that: "...the other half is embodied in LLM's internal semantic coherence." By minimizing semantic entropy, EMPO guides the LLM to generate CoT and answers with greater harmony and order, helping it find that "other half."

Given its underlying mechanism of information theory and its generality, we believe EMPO's minimalist, unsupervised approach will spark a wave of follow-up research. It will push boundaries, find applications in diverse tasks, and likely become a cornerstone of future LLM post-training pipelines.

P.S. Rarely is a paper this interesting also this accessible. For those keen on diving into the details, the original paper recently published is just a click away: https://arxiv.org/pdf/2504.05812. Enjoy!

解读EMPO全程无监督推理新范式

提问即成功的一半,另一半藏于LLM的语义一致性

大型语言模型(LLM)在推理任务上的惊人表现,正不断刷新我们对人工智能的认知。然而,通往更强推理能力的道路往往铺满了昂贵的“黄金”——人工标注的推理过程、验证过的答案或是定制的奖励模型。这些基于监督学习的强化方法,虽然有效,却也带来了成本和可扩展性的瓶颈。

就在今年春节期间,DeepSeek 推出的结果驱动/监督强化推理方案引发热议,大家探讨其背后机理。一个普遍的共识是,思维链(Chain-of-Thought, CoT)这类技术的本质,是在处理复杂任务时,于用户提问(Query)和模型回应(Response)之间,构建一座“慢思维”的信息桥梁。这就像一个平缓的斜坡(Ramp),旨在降低困惑度(Perplexity),将那些对于“快思维”而言存在信息鸿沟、难以一步到位的复杂问题,变得“丝滑可解”。

而今,来自天津大学和腾讯 AI Lab 的一篇新论文 Right Question is Already Half the Answer: Fully Unsupervised LLM Reasoning Incentivization,则沿着这条思路,迈出了更为激进而优雅的一步。它提出了 EMPO (熵最小化策略优化),一个全程无监督的强化推理框架,其效果据报道竟能与那些依赖答案的监督方法相媲美。

这篇论文读起来异常轻松,没有任何故弄玄虚的复杂理论,却如同一缕清风,推进了无监督学习的深入。它进一步证实了我们之前的猜想:只要给模型一个“场”,系统就能自发地选择那条通往更平滑、熵减的推理路径。

春节那阵,随着鞭炮声迎来 DeepSeek R1 zero 已经够震撼了,说明机器可以自主学习,自己再生数据强化自己的智力。这个工作等于是 zero 的“平方”:机器原来还可以从问题就能学到答案。细思有点恐。无监督学习这个概念有很久了吧,继发展到自(监督)学习带来的预训练大模型风暴后,现在发展到推理这份上也是让人开眼了。

EMPO 的“点石成金”之术:语义熵最小化

EMPO 的核心思想极其单纯:与其告诉模型“什么是对的”,不如让模型自己追求“什么是自洽的”。它认为,一个强大的推理模型,其输出应该是稳定且在语义上一致的。如何衡量这种一致性?答案是语义熵(Semantic Entropy)

与关注词汇层面、容易受到表述方式干扰的经典香农熵不同,语义熵关注的是意义层面。EMPO 的做法是:

  1. 采样 (Sample): 对同一个问题,让当前的模型 step-by-step 生成多个(比如 G 个)推理过程和答案。
  2. 聚类 (Cluster): 使用简单的规则(如数学题中的正则表达式)或一个小型验证模型,将这 G 个答案按照最终表达的意义进行聚类。比如,无论推理过程如何,“答案是 42” 和 “最终结果:42” 都会被归为一类。
  3. 计算熵 (Calculate Entropy): 根据聚类结果,计算出每个“意义簇”的概率分布,并由此算出语义熵。如果所有答案都指向同一个意义,熵就最低;如果答案五花八门,熵就很高。
  4. 强化 (Reinforce): 将“语义一致性”(即低熵)作为内在奖励信号,应用到强化学习框架(如 GRPO)中。模型会得到奖励,如果它生成的答案属于那个最“主流”、最一致的意义簇。通过优化,模型被激励去产生那些能让整体语义熵降低的输出。

简单来说,EMPO 就是在鼓励模型:“在你自己的答案空间里,找到那个最‘合群’、最‘确定’的观点,并强化它!”

“窗户纸”背后的智慧与现实考量

EMPO 的简洁优雅并不意味着它的实现毫无挑战。论文中也提到了一些关键点和发现:

  • 熵阈值 (Entropy Thresholding): 一个重要的 “catch”!直接最小化熵可能会让模型“钻牛角尖”,出现过拟合。因此,EMPO 引入了熵阈值——只对那些熵值处于中等范围的问题进行COT强化优化。它排除了模型极度不确定(高熵,可能过于混乱无法学习)或极度自信(低熵,无需再强化优化)的情况,确保了训练的稳定性和效果。
  • 基座模型的重要性 (Importance of Base Model): EMPO 更像是在激发而非创造能力。推理路径的潜力很可能是在预训练阶段就已经埋下的。EMPO 的成功很大程度上依赖于强大的基座模型。这一点在 Qwen 和 Llama 上的对比实验中得到了印证:Qwen 因为预训练中包含了大量 QA 数据,具备了“指令跟随”和推理的“潜能”,EMPO 能直接在其上生效;而 Llama 基座则需要先进行一些 SFT “预热”,才能有效应用 EMPO。这提醒我们,无监督后训练并非万能药,它建立在坚实的预训练基础之上。
  • 无需<cot>标签奖励: 这种方法甚至不需要 <cot> 这样的显式标签来引导。一句简单的提示,如 Please resolve it step by step and put the final answer in {...},就足以提供一个让模型探索和优化其推理路径的伸缩“空间”。

意义与展望:无监督的“数据红利”

EMPO 的价值在于它捅破了一层窗户纸。它证明了,即使在完全没有外部答案的情况下,我们也能通过一个简单、优雅且内在驱动的机制,有效提升 LLM 的推理能力。这就像是提供了一波通用性极强的增强数据质量的红利——获取这份红利的唯一条件,就是只要把问题喂给系统进行强化学习(并辅以简单的聚类),就有可能获得准确率的提升。

论文标题的前半句是 “Right question is already half the answer”(好问题是答案的一半),我们可以接龙说:“the other half is embodied in LLM's internal semantic coherence” (另一半则蕴藏于 LLM 内部的语义一致性之中)。EMPO 正是通过最小化语义熵,让 LLM 在生成 CoT 和答案的过程中,更加和谐有序,从而找到那“另一半”答案。

基于这个研究的机理及其普适性,我们有理由相信,EMPO 所代表的这种极简无监督强化思路,将激发更多后续研究,去探索其边界,应用于更广泛的任务,并可能成为未来 LLM 后训练流程中的一个重要环节。


论文原文少有地亲民易懂,想进一步了解细节的同学,出门向左:https://arxiv.org/pdf/2504.05812

MeanFlow: AI图像生成的降维打击

何恺明团队最新力作,MeanFlow无需预训练、无需蒸馏,仅需一次函数评估 (1-NFE) 即可实现SOTA性能,为高效高质量图像生成开辟新道路。

MeanFlow的核心思想是引入“平均速度场”来直接建模数据点和噪声点之间的转换路径,摆脱了传统扩散模型和流匹配方法对多步迭代的依赖。这项研究在ImageNet 256x256数据集上取得了惊人的 FID 3.43 (1-NFE) 的成绩。核心概念解析

MeanFlow的创新根植于对生成过程基本原理的深刻洞察。它通过引入“平均速度场”和“MeanFlow恒等式”,为单步高效生成提供了坚实的理论基础,有效解决了传统方法的诸多痛点。平均速度场 (Mean Velocity Field)

传统流匹配 (Flow Matching) 方法依赖于建模瞬时速度场𝑣(𝑧𝑡,𝑡),即在特定时间点𝑡状态𝑧𝑡的变化速率。而MeanFlow首创性地引入了平均速度场𝑢(𝑧𝑡,𝑟,𝑡)的概念。

平均速度定义为在时间间隔[𝑟,𝑡]内的平均位移速率:𝑢(𝑧𝑡,𝑟,𝑡)=𝑧𝑡−𝑧𝑟𝑡−𝑟=1𝑡−𝑟∫𝑟𝑡𝑣(𝑧𝑠,𝑠)𝑑𝑠

这里的𝑧𝑠是在时间𝑠的状态。这个定义表明,平均速度不仅取决于当前状态和时间,还取决于一个参考的起始时间𝑟。通过直接建模平均速度,网络学会了预测整个时间段内的“平均路径”,而非瞬时方向。MeanFlow 恒等式

基于平均速度的定义,研究者推导出了一个连接平均速度𝑢和瞬时速度𝑣的核心数学关系——MeanFlow恒等式:𝑣(𝑧𝑡,𝑡)−𝑢(𝑧𝑡,𝑟,𝑡)=(𝑡−𝑟)(𝜕𝑢(𝑧𝑡,𝑟,𝑡)𝜕𝑡+∇𝑧𝑡𝑢(𝑧𝑡,𝑟,𝑡)𝑣(𝑧𝑡,𝑡))

这个恒等式为神经网络的训练提供了理论依据。通过设计损失函数,引导网络学习满足此内在关系,而无需引入额外的启发式方法。由于存在明确定义的目标速度场,理论上最优解与网络的具体结构无关,有助于训练过程更加稳健和稳定。一步生成如何实现?

通过训练神经网络𝑢𝜃直接建模平均速度𝑢,从初始噪声𝑧0(时间𝑡=0) 到目标图像𝑧1(时间𝑡=1) 的生成过程可以简化为单步操作:

𝑧1=𝑧0+𝑢𝜃(𝑧0,0,1)⋅(1−0)

这意味着在推理阶段无需显式计算时间积分,这是传统建模瞬时速度方法所必需的步骤。MeanFlow通过学习平均速度,有效地隐式处理了瞬时速度场可能存在的复杂非线性问题(“弯曲轨迹”),避免了多步ODE求解中累积离散化误差的风险。性能表现 SOTA

MeanFlow 在多个标准图像生成基准上均取得了当前最佳 (SOTA) 或极具竞争力的结果,尤其是在单步或少步生成设定下,其性能提升显著。ImageNet 256x256 (类别条件生成)

在ImageNet 256x256数据集上,MeanFlow展现了卓越的性能。仅需1次函数评估 (1-NFE),FID分数即达到3.43,较之前同类最佳方法有50%到70%的相对提升。在2-NFE设定下,FID进一步降至2.20,已可媲美许多多步方法。

下表详细对比了MeanFlow与其他模型在ImageNet 256x256上的表现 (数据源自论文表2):

MeanFlow (MF)13.43XL/2级骨干
MeanFlow (MF)22.20XL/2级骨干
Shortcut110.601.0B-
IMM2 (含引导)7.771.0B-
iCT1>10 (图示估计)1.0B-
代表性多步SOTA~250x2<2.20XL/2级通常有

CIFAR-10 (无条件生成)

在CIFAR-10 (32x32) 数据集上,MeanFlow同样表现出色。在1-NFE采样下,FID-50K分数为1.95。值得注意的是,MeanFlow在取得此成绩时并未使用任何预处理器,而其他对比方法均使用了EDM风格的预处理器。

下表详细对比了MeanFlow与其他模型在CIFAR-10上的表现 (数据源自论文表3):

MeanFlow (MF)1.95U-Net
EDM2.01EDM风格U-Net
Consistency Models (CM)2.05EDM风格U-Net

创新的CFG集成

无分类器引导 (Classifier-Free Guidance, CFG) 是提升条件生成模型质量的关键技术,但传统应用方式常导致采样计算量翻倍。MeanFlow巧妙地解决了这一问题。作为真实速度场一部分的CFG

MeanFlow将CFG视为底层“真实速度场”的一部分属性进行建模,而非在采样阶段临时组合。研究者定义了一个新的、带引导的真实瞬时速度场𝑣𝑐𝑓𝑔:𝑣𝑐𝑓𝑔(𝑧𝑡,𝑐,𝑡)=𝑤⋅𝑣(𝑧𝑡,𝑐,𝑡)+(1−𝑤)⋅𝑣(𝑧𝑡,∅,𝑡)

其中𝑐是类别条件,𝑤是引导强度。神经网络𝑢𝑐𝑓𝑔,𝜃被训练来直接预测由这个𝑣𝑐𝑓𝑔所诱导出的平均速度场。保持1-NFE的高效引导

由于网络直接学习的是包含了引导信息的平均速度𝑢𝑐𝑓𝑔,因此在采样阶段,无需再进行额外的线性组合计算。只需一次网络调用即可完成带引导的单步生成。这使得MeanFlow在保留CFG效果的同时,依然维持了理想的1-NFE采样性能,真正做到了兼顾效率与质量。意义与价值

MeanFlow的提出不仅仅是一次技术迭代,它对整个生成式AI领域都可能产生深远的影响,有望引领新的研究方向和应用范式。性能飞跃,效率革新

MeanFlow显著缩小了一步与多步扩散/流模型之间的性能差距,证明了高效生成模型同样能达到顶尖质量。挑战传统,简化范式

其“从零开始”训练且无需预训练、蒸馏的特性,极大简化了高性能生成模型的开发流程,有望挑战多步模型的主导地位。降低门槛,普惠AI

更低的计算和开发成本,使得SOTA级别的生成技术能惠及更广泛的研究者和开发者,催生更多创新应用。启迪未来,重塑基础

MeanFlow的成功可能激励学界重新审视生成模型的基础理论,探索更根本、更高效的建模方法。关于本研究

这项名为 MeanFlow: Efficient Flow Matching with Mean Velocity Fields 的开创性研究由以下学者共同完成:

耿正阳 (Zhengyang Geng), 邓明阳 (Mingyang Deng), 白行健 (Xingjian Bai), J. Zico Kolter, 何恺明 (Kaiming He)

他们分别来自卡内基梅隆大学 (CMU) 和麻省理工学院 (MIT) 两所顶尖科研机构。

阅读完整论文 (arXiv:2405.13447)

Q&A on NLP: Chapter I Natural Language and Linguistic Form

Guo: Professor Li, to ease into the discussion, let us begin with some foundational concepts. What exactly do we mean by natural language? What falls under the scope of the field, and where does it sit within the broader discipline of Artificial Intelligence (AI)?

Li:  Natural language refers to the everyday languages we humans speak—English, Russian, Japanese, Chinese, and so on;  in other words,  human language writ large.  It is distinct from computer languages.  Because human conversation is rife with ellipsis and ambiguity,  processing natural language on a computer poses formidable challenges.

Within AI, natural language is defined both as a problem domain and as the object we wish to manipulate.  Natural Language Processing (NLP) is an essential branch of AI, and parsing is its core technology—the crucial gateway to Natural Language Understanding (NLU). Parsing will therefore recur throughout this book.

Computational linguistics is the interdisciplinary field at the intersection of computer science and linguistics.  One might say that computational linguistics supplies the scientific foundations, whereas NLP represents the applied layer.

AI is often divided into perceptual intelligence and cognitive intelligence.  The former includes image recognition and speech processing.  Breakthroughs in big data and deep learning have allowed perceptual intelligence to reach—and in some cases surpass—human‑expert performance.  Cognitive intelligence, whose core is natural language understanding, is widely regarded as the crown jewel of AI.  Bridging the gap from perception to cognition is the greatest challenge—and opportunity—facing the field today.

The rationalist tradition formalises expert knowledge using symbolic logic to simulate human intellectual tasks.  In NLP, the classical counterpart to machine‑learning models comprises linguist‑crafted grammar rules, collectively called a computational grammar.  A system built atop such grammars is known as a rule‑based system. The grammar school decomposes linguistic phenomena with surgical precision, aiming at a deep structural analysis.  Rule‑based parsing is transparent and interpretable—much like the diagramming exercises once taught in a language school.

Figure 1‑1 sketches the architecture of a natural‑language parser core engine.  Without dwelling on minutiae, note that every major module—from shallow parsing through deep parsing—can, in principle, be realised via interpretable symbolic logic encoded as a computational grammar.  Through successive passes, the bewildering diversity of natural language is reduced first to syntactic relations and then to logical‑semantic structure.  Since Chomsky’s distinction between surface structure and deep structure in late 50s, this layered view has become an orthodoxy within linguistics.

Guo: These days everyone venerates neural networks and deep learning. Does the grammar school still have room to live? Rationalism seems almost voiceless in current NLP scholarship. How should we interpret this history and the present trend?

Li:  Roughly thirty years ago, the empiricist school of machine learning began its ascent, fuelled by abundant data and ever‑cheaper computation.  In recent years, deep neural networks have achieved spectacular success across many AI tasks.  Their triumph reflects not only algorithmic innovation but also today’s unprecedented volumes of data and compute.

By contrast, the rationalist programme of symbolic logic has waned.  After a brief renaissance twenty years ago—centred on unification‑based phrase‑structure grammars (PSGs)—computational grammar gradually retreated from the mainstream.  Many factors contributed; among them, Noam Chomsky’s prolonged negative impact warrants sober reflection.

History reveals a pendulum swing between empiricism and rationalism. Kenneth Church famously illustrated the motion in his article A Pendulum Swung Too Far (Figure 1-2).

For three decades, the pendulum has tilted toward empiricism (black dots in Figure 1‑2); deep learning still commands the spotlight. Rationalism, though innovating quietly, is not yet strong enough to compete head‑to‑head.  When one paradigm dominates, the other naturally fades from view.

Guo:  I sense some conceptual confusion both inside and outside the field.  Deep learning, originally just one empiricist technique, has become synonymous with AI and NLP for many observers.  If its revolution sweeps every corner of AI, will we still see a rationalist comeback at all? As Professor Church warns, the pendulum may already have swung too far.

Li:  These are two distinct philosophies with complementary strengths and weaknesses; neither can obliterate the other.

While the current empiricist monoculture has understandable causes, it is unhealthy in the long run.  The two schools both compete and synergise.  Veterans like Church continue to caution against over‑reliance on empiricism, and new scholars are probing deep integrations of the two methodologies to crack the hardest problems in NLU.

Make no mistake: today’s AI boom largely rests on deep‑learning breakthroughs, especially in image recognition, speech, and machine translation.  Yet deep learning inherits a fundamental limitation of the statistical school—its dependence on large volumes of labelled data.  In many niche domains—for instance, minority languages or e‑commerce translation—such corpora are simply unavailable.  This knowledge bottleneck severely constrains empiricist approaches to cognitive NLP tasks.  Without data, machine learning is a bread‑maker without flour; deep learning’s appetite as we all know is insatiable.

Guo: So deep learning is no panacea, and rationalism deserves a seat at the table.  Since each paradigm has its merits and deficits, could you summarise the comparison?

Li: A concise inventory helps us borrow strengths and shore up weaknesses.

Advantages of machine learning

    1. Requires no domain experts (but does require vast labelled data).
    2. Excels at coarse‑grained tasks such as classification.
    3. High recall.
    4. Robust and fast to develop.

Advantages of the grammar school

    1. Requires no labelled data (but does require expert rule writing).
    2. Excels at fine‑grained tasks such as parsing and reasoning.
    3. High precision.
    4. Easy to localise errors; inherently interpretable.

Li: Rule‑based systems shine at granular, line‑by‑line dissection, whereas learned statistical models are naturally strong at global inference. Put bluntly, machine learning often "sees the forest but misses the trees," while computational grammars "see each tree yet risk losing the forest." Although data‑driven models boast robustness and high recall, they may hit a precision ceiling on fine‑grained tasks. Robustness is the key to surviving anomalies and edge cases. Expert‑coded grammars, by contrast, attain high precision, but boosting recall can require many rounds of iterative rule writing. Whether a rule‑based system is robust depends largely on its architectural design. Its symbolic substrate renders each inference step transparent and traceable, enabling targeted debugging—precisely the two pain‑points of machine learning, whose opaque decisions erode user trust and hamper defect localisation. Finally, a learning system scales effortlessly to vast datasets and its breakthroughs tend to ripple across an entire industry. Rule‑based quality, by contrast, hinges on the individual craftsmanship of experts—akin to Chinese cuisine, where identical ingredients may yield dishes of very different calibre depending on the chef.

Both routes confront knowledge bottlenecks. One relies on mass unskilled labour (annotators), the other on a few skilled artisans (grammar experts). For machine learning, the bottleneck is the supply of domain‑specific labelled data. The rationalist route simulates human cognition and thus avoids surface‑level mimicry of datasets, but cannot escape the low efficiency of manual coding. Annotation is tedious yet teachable to junior workers; crafting and debugging rules is a costly skill to train and hard to scale. Talent gaps exacerbate the issue—three decades of empiricist dominance have left the grammar school with a thinning pipeline.

Guo: Professor Li, a basic question: grammar rules are grounded in linguistic form. If semantics is derived from that form, then what exactly is linguistic form?

Li: This strikes at the heart of formalising natural language. All grammar rules rest on linguistic form, yet not every practitioner—even within the grammar camp—has a crisp definition at hand.

In essence, natural language as a symbolic system expresses meaning through form. Different utterances of an idea vary only in form; their underlying semantics and logic must coincide, else communication—and translation—would be impossible. The intuition is commonplace, but pinning down "form" propels us into computational linguistics.

Token & Order — The First‑Level Abstraction
At first glance a sentence is merely a string of symbols—phonemes or morphemes. True, but that answer is too coarse. Every string is segmented into units called tokens (words or morphemes). A morpheme is the smallest pairing unit of sound and meaning. Thus our first abstraction decomposes linguistic form into a sequence of tokens plus their word order. Grammar rules define patterns that match such sequences. The simplest pattern, a linear pattern, consists of token constraints plus ordering constraints.

Guo: Word order seems straightforward, but tokens and morphemes hide much complexity.

Li: Indeed. Because tokens anchor the entire enterprise, machine‑readable dictionaries become foundational resources. (Here "dictionary" means an electronic lexicon.)

If natural language were a closed set—say only ten thousand fixed sentences—formal grammar would be trivial: store them all, and each complete string would serve as an explicit pattern. But language is open, generating unbounded sentences. How can a finite rule set parse an infinite language?

The first step is tokenisation—dictionary lookup that maps character strings to lexicon words or morphemes. Unlimited sentences decompose into a finite vocabulary plus occasional out‑of‑dictionary items. Together they form a token list, the initial data structure for parsing.

We then enter classic linguistic sub‑fields. Morphology analyses the internal structure of multi‑morphemic words. Some languages exhibit rich morphology—noun declension, verb conjugation—e.g., Russian and Latin; others, such as English and Chinese, are comparatively poor. Note, however, that Chinese lacks inflection but excels at compounding. Compounds sit at the interface of morphology and syntax; many scholars treat them as part of "little syntax" rather than morphology proper.

Guo: Typologists speak of a spectrum—from isolating languages such as Classical Chinese (no morphology) to polysynthetic languages like certain Native American tongues (heavy morphology). Most languages fall between, with Modern Chinese and English leaning toward the isolating side: minimal morphology, rich syntax. Correct?

Li: Exactly. Setting aside the ratio of morphology to syntax, our first distinction is between function words/affixes versus content words. Function words (prepositions, pronouns, particles, conjunctions, original adverbs, interrogatives, interjections) and affixes (prefixes, suffixes, endings) form a small, closed set.

Content words—nouns, verbs, adjectives, etc.—form an open set forever producing neologisms; a fixed dictionary can hardly keep up.

Because function words and affixes are frequent yet limited, they can be enumerated as literals in pattern matching. Hence we have at least three grain‑sizes of linguistic form suitable for rule conditions: (i) word order; (ii) function‑word literals or affix literals; (iii) features.

Features — The Implicit Form
Explicit tokens are visible in the string, but parsers also rely on implicit features—category labels. Features encode part‑of‑speech, gender, number, case, tense, etc. They enter pattern matching as hidden conditions. Summarising: automatic parsing rests on (i) order, (ii) literals, (iii) features—two explicit, one implicit. Every language weaves these three in different proportions; grammar is but their descriptive calculus.

Guo: By this metric, can we say European languages are more rigorous than Chinese?

Li: From the standpoint of explicit form, yes. European tongues vary internally—German and French more rigorous than English—but all possess ample explicit markers that curb ambiguity. Chinese offers fewer markers, increasing parsing difficulty.

Inflectional morphology supplies visible agreement cues—gender‑number‑case for nouns, tense‑aspect‑voice for verbs. Chinese lacks these. Languages with rich morphology enjoy freer word order (e.g., Russian). Esperanto’s sentence "Mi amas vin" (I love you) can permute into six orders because the object case ‑n never changes.

Chinese, conversely, evolved along the isolating path, leveraging word order and particles. Even so, morphology provides tighter agreement than particles. Hence morphology‑rich languages are structurally stringent, reducing reliance on implicit semantics.

Guo: People call Chinese a "paratactic" language—lacking hard grammar, leaning on meaning. Does that equate to your notion of implicit form?

Li: Precisely. Parataxis corresponds to semantic cohesion—especially collocational knowledge within predicate structures. For example, the predicate "eat" expects an object in the food category. Such commonsense often lives in a lexical ontology like HowNet (founded by the late Professor Dong Zhendong).

Consider how plurality is expressed. In Chinese, "brother" is a noun whose category is lexically stored. Esperanto appends ‑o for nouns and ‑j for plural: frato vs. fratoj. Chinese may add the particle (‑men), but this marker is optional and forbidden after numerals: "三个兄弟" (three brothers) not "*三个兄弟们". Here plurality is implicit, inferred from the numeral phrase.

Guo: Lacking morphology indeed complicates Chinese. Some even claim Chinese has no grammar.

Li: That is hyperbole. All languages have grammar; Chinese simply relies more on implicit forms. Overt devices—morphology, particles, word order—are fewer or more flexible.

Take omission of particles as an illustration. Chinese frequently drops prepositions and conjunctions. Compare:

      1. 对于这件事, 依我的看法, 我们应该听其自然。
        As for this matter, in my opinion, we should let nature take its course.
      2. 这件事我的看法应该听其自然。
        * this matter my opinion should let nature take its course.
        (Unacceptable as a word‑for‑word English rendering.)

Example 2 is ubiquitous in spoken Chinese but would be ungrammatical in English. Systematic omission of function words exacerbates NLP difficulty.

Guo: What about word order? Isolation theory says morphology‑poor languages have fixed order—Chinese is labelled SVO.

Li: Alas, reality defies the stereotype. Despite lacking morphology and often omitting particles, Chinese exhibits remarkable word‑order flexibility. Consider the six theoretical permutations of S, V, and O. Esperanto, with a single object case marker ‑n, allows all six without altering semantics. Compare English (no case distinction for nouns, but marking subject pronouns from obect cases) and Chinese (no case at all):

Order Esperanto English Chinese
SVO Mi manĝis fiŝon I ate fish 我吃了鱼
SOV Mi fiŝon manĝis * I fish ate 我鱼吃了
VOS Manĝis fiŝon mi * Ate fish I ?吃了鱼我
VSO Manĝis mi fiŝon * Ate I fish * 吃了我鱼
OVS Fiŝon manĝis mi * Fish ate I ?鱼吃了我
OSV Fiŝon mi manĝis Fish I ate 鱼我吃了

Chinese sanctions three orders outright, two marginally (marked “?”), and forbids one (“*”). English allows only two. Thus Chinese word order is about twice as free as English, even though English possesses case distinction on pronouns. Hence morphology richness does not always guarantee order freedom.

Real corpora confirm that Chinese is more permissive than many assume. Greater flexibility inflates the rule count in sequence‑pattern grammars: every additional order multiplies pattern variants. Non‑sequential constraints can be encoded inside a single rule; order itself cannot.

A classic example is the elastic placement of argument roles around "哭肿" (cry‑swollen):

张三眼睛哭肿了。
眼睛张三哭肿了。
哭肿张三眼睛了。
张三哭肿眼睛了。
哭得张三眼睛肿了。
张三哭得眼睛肿了。
…and so on.

Such data belie the notion of a rigid SVO Chinese. Heavy reliance on implicit form complicates automatic parsing. Were word order fixed, a few sequence patterns would suffice; flexibility forces exponential rule growth.

壹 自然语言与语言形式

郭: 李老师, 由浅入深, 我们还是从一些基本概念开始谈 起吧。什么是自然语言? 自然语言领域包括哪些内容? 它在人工智能里面的定位是怎样的呢?

李: 自然语言 (natural language) 指的是我们日常使用的语言, 英语、俄语、日语、汉语等, 它与人类语言是同义词。自 然语言有别于计算机语言。人脑处理的自然语言常有省略和歧义, 这给电脑 (计算机) 的处理提出了挑战。

在人工智能界, 自然语言是作为问题领域和处理对象提出来的。自然语言处理是人工智能的重要分支, 自然语言解析是其核心技术和通向自然语言理解的关键。语言解析是我 们接下来要探讨的、贯穿全书始终的话题。

计算语言学是计算机科学与语言学的交叉学科. 计算语言学和自然语言处理是同一个专业领域的两个剖面. 可以 说, 计算语言学是自然语言处理的科学基础, 自然语言处理是计算语言学的应用层面。

人工智能主要有感知智能 (perceptual intelligence) 和认 知智能 (cognitive intelligence) 两大块. 前者包括图像识别  (image recognition) 和语音处理 (speech processing)。随着 大数据和深度学习 (deep learning) 算法的突破性进展, 感知智能很多方面已经达到甚至超过人类专家的水平。认知智能的核心是自然语言理解, 被一致认为是人工智能的皇冠。从感知跃升到认知是当前人工智能所面临的最大挑战和机遇。

理性主义直接把领域专家的经验形式化, 利用符号逻辑来模拟人的智能任务。在自然语言处理领域, 与机器学习模型平行的传统方法是语言学家手工编码的语言规则。这些规则的 集合称为计算文法。由计算文法支撑的系统叫作规则系统 (rule system)。文法学派把语言学家总结出来的语言规则形式化, 从而对语言现象条分缕析, 达到对自然语言深层次的结构 解析. 规则系统试图模拟人的语言分析理解过程。规则系统解析自然语言是透明的、可解释 (interpretable) 的。这个过程很 像是外语文法老师在课堂上教给学生的句子分析方法。

图1—1是一张自然语言解析器 (parser) 核心引擎 (core engine) 的架构图。不必深究细节, 值得说明的是, 从浅层解析 (shallow parsing) 到深层解析 (deep parsing) 里面的各主要模块, 都可以用可解释的符号逻辑 (symbolic logic) 以计算文法的形式实现。千变万化的自然语言表达, 就这样一步一 步地从句法关系 (syntactic relation) 的解析, 进而求解其深层 的逻辑语义 (logic semantics) 关系。这个道理早在1957年乔 姆斯基 (Chomsky) 语言学革命中提出表层结构 (surface structure) 到深层结构 (deep structure) 的转换之后, 就逐渐成为语言学界的共识了。

郭: 现在大家都在推崇神经网络 (neural network) 深度学习, 文法学派还有生存空间吗?  理性主义在自然语言领域已经听不到什么声音了。怎样看待这段历史与趋向呢?

李: 大约从30年前开始到现在, 经验主义机器学习这一 派, 随着数据和计算资源的发展, 天时地利, 一直在向上走。尤其是近年来深层神经网络的实践, 深度学习在不少人工智能任务上取得了突破性的成功。经验主义的这些成功, 除了 神经网络算法的创新, 也得益于今非昔比的大数据和大计算的能力。

与此对照, 理性主义符号逻辑则日趋式微。符号逻辑在自然语言领域表现为计算文法。文法学派在经历了20年前 基于合一 (unification) 的短语结构文法 (Phrase Structure  Grammar, PSG) 创新的短暂热潮以后, 逐渐退出了学界的主 流舞台。形成这一局面的原因有多个, 其中包括乔姆斯基对于文法学派长期的负面影响, 值得认真反思。

回顾人工智能和自然语言领域的历史, 经验主义和理性 主义两大学派此消彼长, 呈钟摆式跌宕起伏。肯尼斯丘吉 (Kenneth Church) 在他的「钟摆摆得太远」(A Pendulum  Swung Too Far) 一文中, 给出了一个形象的钟摆式跌宕图 (图1—2).

最近30年来, 经验主义钟摆的上扬趋势依然不减 (见图 1—2的黑点表示)。目前来看, 深度学习仍在风头上。理性主义积蓄多年, 虽然有其自身的传承和创新, 但还没有到可以与经验主义正面争锋的程度。当一派成为主流时, 另一派自然淡出视野。

郭: 我感觉业内业外有些认知上的混乱。深度学习本来只是经验主义学派的一种方法, 现在似乎在很多人心目中等价于人工智能和自然语言处理了。如果深度学习的革命席卷 人工智能的方方面面, 会不会真地要终结理性主义的回摆呢? 正如丘吉教授所言, 经验主义的钟摆已经摆得太远了。

李: 我的答案是否定的。这是两个不同的哲学和方法论, 各自带有其自身的天然优势和劣势, 不存在一派彻底消灭另 一派的问题。

当前学界经验主义一面倒的局面虽然事出有因, 但并不 是一个健康的状态。其实, 两派既有竞争性, 也有很强的互补 性。丘吉这样的老一辈有识之士一直在警示经验主义一边倒的弊端, 也不断有新锐学者在探索两种方法论的深度融合, 以 便合力解决理解自然语言的难题。

毫无疑问, 这一波人工智能的热潮很大程度上是建立在深度学习的突破上, 尤其是在图像识别、语音处理和机器翻译方面取得的成就上。但是, 深度学习的方法仍然保留了统计学派的一个根本局限, 就是对海量标注数据 (labeled data) 的依赖。在很多细分领域和任务场景, 譬如, 少数族裔语言的解 析、电商数据的机器翻译, 海量标注或领域翻译数据并不存 在。这个知识瓶颈严重限制了经验主义方法在自然语言认知任务方面的表现。没有足够的标注数据, 对于机器学习就是无米之炊。深度学习更是如此, 它的胃口比传统机器学习 更大。

郭 : 看来深度学习也不是万能的, 理性主义理应有自己的一席之地。说它们各有长处和短板, 您能够给个比较吗?

李: 归纳一下两派各自的优势与短板是很有必要的, 可以取长补短。

机器学习的优势包括:

(1) 不依赖领域专家 (但需要大量标注数据);
(2) 长于粗线条的任务, 如分类 (classification);
(3) 召回 (recall) 好;
(4) 鲁棒 (robust), 开发效率高。

与此对照, 文法学派的优势包括:

(1) 不依赖标注数据 (但需要专家编码);
(2) 长于细线条的任务, 譬如解析和推理;
(3) 精度(precision)好;
(4) 易于定点排错, 可解释。

专家编码的规则系统擅长逐字逐句的条分缕析, 而学习出来的统计模型则天然长于全局结论。如果说机器学习往往是见林不见木的话, 计算文法则是见木不见林。大数据驱动的机器学习虽然带来了鲁棒和召回的长处, 但对细线条的任务较易遭遇精度的天花板。所谓鲁棒, 是robust的音译, 也 就是强壮、稳健的意思, 它是在异常和危险情况下系统生存的关键。专家编写规则虽然容易保障精度, 但召回的提升则是一个漫长的迭代过程。鲁棒性则决定于规则系统的架构设计。规则系统的基础是可解释的符号逻辑, 容易追踪到出错的现 场, 并做出有针对性的排错。而这两点正是机器学习的短板。机器学习的结果不论是对是错, 都难以解释, 因而影响用户的体验和信赖。难以定点排错更是开发现场的极大困扰, 其原因是学习模型缺乏显性符号与结构表示 (structure representation)。最后, 学习系统能较快地规模化到大数据的应用场景, 成功易于复制, 方法的突破往往可带动整个行业的提升。相对而言, 规则系统的质量很大程度上取决于专家的个体经 验。这就好比中餐, 同样的食材, 不同的厨师做出来的菜肴品质常常相差很大。

两条路线各有自身的知识瓶颈。打个比喻, 一个是依赖海量的低级劳动, 另一个是依赖少数专家的高级劳动。对于 机器学习, 海量标注是领域化落地 (grounding,即落实到应 用) 的知识瓶颈。理性主义路线模拟人的认知过程, 无需依赖海量数据在表层模仿。但难以避免手工编码的低效率。标注 工作虽然单调, 可一般学生稍加培训即可上手。而手工编制、 调试规则, 培训成本高, 难以规模化。还有, 人才的断层也算是文法学派的一个现实的局限。30年正好是一代人。在过 去的30年, 经验主义在主流舞台的一枝独秀, 客观上造成了 理性主义阵营人才青黄不接。

郭: 李老师,我有个基本问题: 文法规则依据的是语言形式 (linguistic form)。那么, 通过这个形式解析出语义 (semantics), 到底什么是语言形式呢?

李: 这是自然语言形式化的根本问题。所有的文法规则都建立在语言形式的基础之上, 可并不是每个人, 包括从事文 法工作的人, 都能对语言形式有个清晰的认识。

不错, 自然语言作为符号系统, 说到底就是以语言形式来表达语义。话语的不同只是形式的不同, 背后的语义和逻辑一定是相同的, 否则人不可能交流思想, 语言的翻译也会失去根基。这个道理老少咸知, 那什么是语言形式的定义呢? 回答这个问题就进入计算语言学了。

语言形式, 顾名思义, 就是语言的表达手段。乍一看语言, 不就是符号串吗? 语音流也好, 文字串也好, 都可以归结为符号串。所以, 符号串就是语言形式。这个答案不算错, 但失之笼统。这个“串”是有单位的, 其基本单位叫 token (可译 作“文本符号”), 也就是单词或语素 (morpheme)。语素, 其定义是音义结合的最小符号单位。因此, 作为第一级抽象, 我们可以把语言形式分解为文本符号及其语序 (word order)。计算文法中的规则都要定义一个条件模式 (pattern), 就是为 了与语言符号串做匹配。最基本的条件模式叫线性模式 (linear pattern), 其构成的两个要素就是符号条件和次序条件。

郭 : 好, 语言形式的基本要素是词/语素和语序。语序就是符号的先后顺序, 容易界定; 但词和语素里面感觉有很多 学问。

李: 不错, 作为语言符号, 词和语素非常重要, 它们是语言学的起点。收录词和语素的词典因此成为语言解析的基础资源。顺便提一下, 我们在这所说的“词典”是指机器词典, 它是 以传统词典为基础的形式化资源。

如果自然语言表达是一个封闭的集合, 譬如, 一共就只有一万句话, 语言形式文法就简单了。建个库把这些语句词串全部收进去, 每个词串等价于一条“词加语序”的模式规则。全词串的集合就是一个完备的文法模型。但是, 自然语言是 一个开放集, 无法枚举无穷变化的文句。形式文法是如何依据语言形式形成规则, 并以有限规则完成对无限文句的自动解析呢?

以查词典为基础的分词 (tokenization), 是文句解析的第 一步。查词典的结果是“词典词” (lexicon word), 包括语素。无限文句主要靠查词典分解为有限的单位。词典词加上少量 超出词典范围的生词, 一起构成词节点序列 (tokenlist)。词节点序列很重要, 它是文句的形式化表示 (formalized representation)。作为初始的数据结构, 词节点序列是自动解析的 对象。

接 下来就进入语言学的基本分支了, 通常叫词法 (morphology), 目的是解析多语素词 (multi-morphemic word) 的内部结构。对于有些语种, 词法很繁复, 包括名词变格 (declension)、动词变位 (conjugation) 等, 譬如俄语、拉丁语; 有些语种的词法则较贫乏, 譬如英语、汉语。值得注意的是, 词法的繁简只是相对而言。譬如汉语缺乏形态 (inflection), 单词不变形, 但是汉语的多语素复合造词的能力却很强。不过, 语 言学里的复合词 (compound word) 历来有争议, 它处于词法与句法 (syntax) 接口的地带, 其复合方式也与句法短语的方式类似。所以, 很多人不把词的复合当成词法, 而是看成句法的前期部分, 或称小句法。

郭: 以前看语言类型方面的文章, 说有一个频谱, 一个极端叫孤立语 (isolating language), 以古汉语为代表。孤立语没有词法, 只有句法。另一个极端好像叫多式综合语 (poly-synthetic language), 以某些印第安语为代表, 基本上只有词 法, 没有句法。多数语言处在两个极端之间, 现代汉语和英语更多偏向孤立语这边, 小词法大句法. 是这样吗?

李: 对, 是这样的。撇开词法句法比例的差别, 我们在研究词和语素的时候, 第一眼看到的是它的两大类别: 一类是小 词 (function word) 和形态, 是个较小的封闭集合; 一类叫实词  (notional word), 是个开放集合。实词范畴永远存在“生词”, 词典是收不住口的。

小词, 其实只是俗称, 术语应该叫功能词、封闭类词或虚词, 指的是介词、代词、助词、连词、原生副词 (original adverb)、疑问词、感叹词之类。形态包括前缀 (prefix)、后缀  (suffix)、词尾 (ending) 等材料, 也是一个小的集合。小词和形态出现频率高, 但数量有限。作为封闭类语素, 小词和形态需要匹配的时候, 原则上可以直接枚举它们, 软件界称其为匹配直接量 (literal)。至此, 我们至少得到了下面几种语言形式可以作为规则的条件: ①语序; ②小词; ③形态。不同的语言类型对这些形式的倚重和比例不同。例如, 俄语形态丰富, 对于语序和小词的依赖较少; 英语形态贫乏, 语序就相对固 定, 小词也比较丰富。

那么实词呢? 实词当然也是语言形式, 也可以尝试在规 则模式中作为直接量来枚举。但是, 因为实词是个开放集, 最好给它们分类, 利用类别而不是直接量去匹配实词, 这样做才会有概括性。人脑对于实词也主要靠分类来总结抽象的. 给词分类并在词典中标注分类结果是形式化的基础工作。

形式系统里面, 分类结果通常以特征 (feature) 来表示和标注。特征是系统内部定义的隐性语言形式。隐性形式 (implicit form) 是相对于前面提到的显性形式 (explicit form) 而 言。很显然, 无论语序还是语素, 它们都是语言符号串中可以看得见的形式。分类特征则不然, 它们是不能直接感知的。这些特征作为词典查询的结果提供给解析器, 支持模式匹配  (pattern matching) 的形式条件。

总结一下自动解析所依据的语言形式, 主要有三种: ①语序; ②直接量 (尤其是小词和形态); ③特征。前两种是显性形式, 特征是隐性形式。语言形式这么一分, 自然语言一下子就豁然开朗了。管它什么语言, 不外乎这三种形式的交错使用, 搭配的比例和倚重不同而已。所谓文法, 也不外乎用这三种形式形成规则, 对语言现象及其背后的结构做描述而已。

三种语言形式可以嫁接。显性形式的嫁接包括重叠式 (reduplication), 如: “高高兴兴”“走一走”。它是语序与直接量嫁接的模式 (AABB、V 一V), 是中文词法句法中常用的形式手段。显性形式也可以特征化。特征化可以通过词典标注实现, 也可以通过规则模块或子程序赋值得出。例如, “形态特征” (如单数、第三人称、现在时等) 就是通过词法模块得出 的特征。形态解析所依据的条件主要是作为直接量的形态词尾 (inflectional ending) 以及词干 (stem) 的类型特征, 例如, 英语词尾“-ly”与形容词词干结合成为副词 (beautiful-ly)。可见, 形态特征也是显性形式与隐性形式的嫁接结果。

郭: 从语言形式的使用看, 可以说欧洲语言比汉语更加严 谨吗?

李: 是的。从语言形式的角度来看, 欧洲语言确实比汉语严谨。欧洲语言内部也有不小的区别, 例如, 德语、法语就比英语严谨, 尽管从语言形成的历史上看, 可以说英语是从德 语、法语杂交而来的。

这里的所谓“严谨”, 是指这些语言有比较充分的显性形式来表达结构关系, 有助于减少歧义。汉语显性形式不足, 因此增加了汉语解析 (Chinese parsing) 的难度。形态是重要的显性形式, 如名词的“性数格” (gender, number and case), 动词的“时体态”(tense, aspect and voice), 这些词法范畴是以显性的形态词尾来表达的。但是这类形态汉语里没有。形态丰富的语言语序比较自由, 譬如俄语。再如世界语 (Esperanto) 的“我爱你”有三个词, 可以用六种语序任意表达, 排列组合。为什么语序自由呢? 因为有宾格 (object case) 这样的形态形式, 它跑到哪里都逃不出动宾 (verb-object) 关系, 当然就不需要依赖固定的语序了。

汉语在发展过程中, 没有走形态化的道路, 而是利用语序和小词在孤立语的道路上演化. 英语的发展大体也是这个模式。从语言学的高度看, 形态也好, 小词也好, 二者都是可以感知的显性形式。但是, 形态词尾的范畴化, 比起小词 (主要是介词), 要发达得多。动词变位、名词变格等形态手段, 使得有结构联系的语词之间产生一种显性的一致关系  (agreement)。譬如, 主谓 (subject predicate) 在人称和数上的一致关系, 定语与中心词在性数格上的一致关系等。关系有形式标记, 形态语言的结构自然严谨得多, 减少了结构歧义的可能。丰富的形态减低了解析对于隐性形式和知识的依赖。

郭 : 常听人说,中文是“意合”式语言, 缺少硬性的文法规范, 是不是指的就是缺乏形态, 主要靠语义手段来分析理解它?

李: 是的. 从语言形式化的角度看, 语义手段表现为隐性形式。所谓“意合”, 其实就是关联句词之间的语义相谐, 特别是谓词 (predicate word) 结构里面语义之间的搭配  (collocation) 常识。譬如, 谓词“吃”的对象是“食品”。这种 常识通常编码在本体知识库 (ontology) 里面。董振东先生创立的“知网 (HowNet)”∗ 就是这样一个本体常识的知识库。

∗ “知网” (HowNet) 是中国自然语言处理前辈董振东先生发明的跨语言的语义机器词典。这套词典为词义的本体概念及其常识编码, 旨在设立一套形式化语义概念网络, 以此作为自然语言处理的基础支持。

再看形态与小词的使用。譬如, “兄弟”在汉语里是名词, 这个词性是在词典标注的。但是世界语的“frato (兄弟)”就不需要词典标注, 因为有名词词尾“-o”。再如复数, 汉语的 “兄弟们”用了小词“们”来表示复数的概念; 世界语呢, 用词尾 “-j”表示, 即“fratoj (兄弟们)”。乍一看, 这不一样么? 都是 用有限的语言材料, 做显性的表达。但是, 有“数”这个词法范 畴的欧洲语言 (包括世界语), 那个形态是不能省略的。而汉语的复数表达, 有时显性有时隐性,这个“们”不是必需的, 如:

三个兄弟没水喝。

这里的兄弟复数就没有小词“们”。实际上, 汉语文法规定了不允许在数量结构后面加复数的显性形式, 譬如不能说 “三个兄弟们”。换句话说, 中文“(三个)兄弟”里的复数是隐性的,需要前面的数量结构才能确定。

郭: 看来缺乏形态的确是中文的一个挑战。中文学起来难, 自动解析也难。有人甚至说, 中文根本就没有文法。

李: 那是偏激之词了。不存在没有文法的语言。假如语 言没有“法”, 那么人在使用时如何把握, 又如何理解呢? 只不 过是, 中文的文法更多地依赖隐性形式。

汉语文法的确比较宽松, 宽松表现在较少依赖显性形式。语句的顺畅靠的是上下文语义相谐, 而不是依靠严格的显性文法规则。譬如形态、小词、语序, 显性形式的三个手段, 对于 汉语来说, 形态基本上没有, 小词常常省略, 语序也很灵活。

先看小词,譬如, 介词、连词, 虽然英语有的汉语基本都有, 但是汉语省略小词的时候远远多于英语。这是有统计根据的, 也符合我们日常使用的感觉: 中文, 尤其是口语, 能省则省,显得非常自由。对比下列例句, 可见汉语中省略小词是普遍性的:

① 对于这件事, 依我的看法, 我们应该听其自然.
As for this matter, in my opinion, we should leave it to nature.

② 这件事我的看法应该听其自然.
∗ This matter my opinion should leave it to nature.

类似句子②在汉语口语里极为常见, 感觉很自然。如果尝试词对词译成英语, 则完全不合文法。汉语和英语都用介词短语 (prepositional phrase, PP) 做状语, 可是汉语介词常可 省略。这种缺少显性形式标记的所谓“意合”式表达, 确实使得中文的自动化处理比英文处理难了很多。

郭: 汉语利用语序的情况如何? 常听人说, 形态丰富的语言语序自由。汉语缺乏形态, 因此是语序固定的语言。中文一般被认为是“主谓宾(SVO)”固定的语言。

李: 可惜啊, 并非如此。按常理来推论, 缺乏形态又常常省掉小词, 那么, 语序总该固定吧? 可实际上, 汉语并不是持孤立语语序固定论者说的那样语序死板, 其语序的自由度常超出一般人的想象。

拿最典型的主谓宾句型的变式来看, SVO 三元素, 排列的极限是六种组合。世界语的形态不算丰富, 论变格只有一 个宾格“-n”的词尾, 主格 (subject case) 是零形式。它仍然可以采用六种变式的任意一个语序, 而不改变“SVO”的逻辑语义关系 (logic semantic relation)。比较一下形态贫乏的英语 (名词没有格变, 但是代词有) 和缺乏形态的汉语 (名词代词都没有格变), 是很有意思的。世界语、英语、汉语三种语言 SVO 句型的自由度对比如下:

①SVO:

Mi manĝis fiŝon.
I ate fish.
我吃了鱼。

②SOV:

Mi fiŝon manĝis.
∗ I fish ate.
我鱼吃了。

③VOS:

Manĝis fiŝon mi.
∗ Ate fish I.
? 吃了鱼我。(口语可以)

④VSO:

Manĝis mi fiŝon.
∗ Ate I fish.
∗ 吃了我鱼。(解读不是VSO, 而是“吃了我的鱼”)

⑤OVS:

Fiŝon manĝis mi.
∗ Fish ate I.(不允许, 尽管“I”有主格标记)
? 鱼吃了我。(合法解读是SVO,与OVS正好相反)

⑥OSV:

Fiŝon mi manĝis.
fish I ate.
鱼我吃了。

总结一下, 在六个语序中, 汉语有三个是合法的, 有两个在灰色地带 (前标“? ”, 口语中似可存在), 有一个是非法的 (前标 “∗ ”),英语呢? 只有两个合法, 其余皆非法。可见, 汉语的语序自由度在最常见的SVO句式中,  比英语要大一倍。虽然英语有代词的格变(I/me), 而汉语没有, 英语的语序灵活性反而不如汉语。可见, 形态的丰富性与语序自由度并非必然呼应。

汉语其实比很多人想象得具有更大的语序自由度和弹 性。常常是, 思维里什么概念先出现, 就可以直接蹦出来。再看一组例子:

张三眼睛哭肿了。
眼睛张三哭肿了。
哭肿张三眼睛了。
张三哭肿眼睛了。
哭得张三眼睛肿了。
张三哭得眼睛肿了。
张三眼睛哭得肿了。
张三的眼睛哭肿了。
............

若不研究实际数据的话, 我们很难相信汉语语序如此任性。汉语依赖隐性形式比显性形式更多, 这对自动解析显然不利。我们当然希望语言都是语序固定的, 这该省多少力气啊!  序列模式规则就是由符号加次序构成的, 语序灵活了, 规 则数量就得成倍增长。非语序的其他形式约束可以在既定的模式里面调控, 唯有语序是规则编码绕不过去的坎儿。

李维 郭进《自然语言处理答问》(商务印书馆 2020)

 

Prelude: Origins

Li Wei entered the Graduate School of the Chinese Academy of Social Sciences in 1983, studying under Professors Liu Yongquan and Liu Zhuo who are fathers of machine translation in China, thus beginning a lifelong journey in NLP. After graduation, he continued MT research at the Institute of Linguistics (CASS), then pursued doctoral work in the United Kingdom and Canada, earning a PhD in Computational Linguistics from Simon Fraser University. Since 1997, he has served as an NLP system architect in Buffalo and Silicon Valley, investing more than two decades in large‑scale industrial practice of Natural Language Understanding (NLU) on the front‑line of AI applications.

Guo Jin received his PhD in Computer Science from the National University of Singapore in 1994 with a focus on Chinese tokenization and statistical language modelling, work published in Computational Linguistics and related venues. Moving to the United States in 1998, he held research posts at Motorola, Amazon, and the JD Silicon Valley Research Center, exploring applications that fuse machine learning, NLP, and human–computer interaction across internet and IoT scenarios.

From the 1980s onward, the AI community has witnessed a “two‑track contest” between rationalism and empiricism in NLP. The ascendancy of machine learning has gradually eclipsed the grammar school, and computational grammar risks a generational break.

In 2018, over ten extended conversations in Silicon Valley, Li and Guo revisited the symbolic legacy and debated paths forward. Those dialogues became the backbone of the present volume, calling for a rationalist renaissance to dismantle the cognitive citadels that still impede AI.

零 缘起

自20世纪80年代起, 人工智能领域见证了理性主义 (rationalism) 与经验主义(empiricism) 的“两条路线斗争”。其中, 自然语言学界的“斗争”结果是, 文法学派(grammar school) 与统计学派 (statistical school) 此消彼长, 机器学习渐 成主流, 计算文法 (computational grammar)则有断代之虞。

李维, 1983年进入中国社会科学院研究生院, 师从刘涌 泉、刘倬先生, 主攻基于文法的机器翻译 (machine translation), 始入自然语言领域。毕业后在中国社会科学院语言研究所从事机器翻译研究, 继而留学英国、加拿大, 获Simon Fraser University (SFU) 计算语言学 (Computational Linguistics) 博士。1997年起, 在美国水牛城、硅谷, 从事自然语言理解 (Natural Language Understanding, NLU) 工业实 践20余载, 为人工智能(Artificial Intelligence, AI) 应用第一 线的系统架构师。

郭进, 1994年新加坡国立大学计算机科学博士, 主攻中文分词 (Chinese tokenization) 和统计模型 (statistical model), 成果见于「计算语言学」等刊。1998年赴美, 先后在摩托罗拉、亚马逊、京东硅谷研究院等从事人工智能研究, 探索将机器学习 (machine learning)、自然语言处理  (Natural Language Processing, NLP) 等人机交互技术应用于互联网与物联网的解决方案。2018年, 李与郭在硅谷就自然语言解析 (natural language parsing) 问题有十次长谈, 回顾并展望文法学派的机制创新与传承之路, 意图呼唤理性主义回归, 解构自然语言, 协同攻坚人工智能的认知堡垒, 遂成此作。

李维 郭进《自然语言处理答问》(商务印书馆 2020)

 

Preface for "Q&A on NLP"

This modest volume, Questions & Answers on Natural Language Processing, now joins the Chinese Linguistic Knowledge Series alongside titles by Zhu Dexi, Li Rong, He Jiuying, Li Xinkui, Feng Zhiwei, and Xing Fuyi. To be included in such a lineage leaves me both honored and a little awed. In particular, Professor Zhu Dexi’s Q&A on Grammar was one of my earliest inspirations; I have revisited it countless times over the decades, always finding new heights to scale.

Symbolic Linguistic Legacy

Had the series permitted formal dedications, I would have inscribed this book to my mentors—Professors Liu Yongquan and Liu Zhuo—pioneers of machine translation in China. Their legacy impelled me to press on even when the manuscript seemed perpetually “stuck in revision hell.”

The book’s very existence also owes much to Feng Aizhen, my meticulous commissioning editor at The Commercial Press. Over three years of proofs, her insistence on perfection revealed how that venerable imprint earned its reputation for rigor.

Thanks, Colleagues & Friends

Professors Wang Jianjun, Song Rou, Zhang Guiping, Zhou Liuxi, and many industry comrades offered incisive comments. My long‑time engineering partners—Niu Cheng, Lokesh, Li Lei, Tang Tian, Ben, and Martin—translated symbolic NLP designs into scalable products.

Mirror’s Last‑Minute Miracle

Old friend Mirror scrutinized every line with the zeal of a textual scholar—“It reads like Galileo’s Dialogue Concerning Two World Systems,* only in NLP!*” Five days before typesetting, he begged to polish one more draft, and the result was transformative.

A Tale of Two Schools

Beyond theory, this book chronicles the dialectic between rationalist symbolism and empiricist machine learning—a pendulum that has swung since the 1980s. Co‑author Dr. Guo Jin saved the project more than once, re‑anchoring a drifting manuscript.

Family Footnotes

A lifetime craftsman, I never planned to “write a book,” yet my family shared every thrill. My daughter Tian Tian contributed two whimsical illustrations explaining the “dictionary black‑box” joke, adding warmth to these pages.

In Quiet Cupertino

And so, on a July night in Apple Town, with Secret Garden’s Sometimes When It Rains looping through my headphones, I penned the final punctuation. May these symbolic threads—fragile yet unbroken—echo through AI’s recurrent tides. Neural networks are no end of history; when the pendulum swings back, perhaps this book too will be rediscovered.

Cupertino, 15 July 2020 (midnight)

 

《写在NLP小书出版之时》

这本NLP小书《自然语言处理答问》终于出版了,还是蛮感触的。看商务这个《汉语知识丛书》系列,所选皆中国语言学界前辈,如雷贯耳。大家小书,精华荟萃,忝列其上,不胜惶恐。尤其是朱德熙先生的学术经典《语法答问》,是当年入行的启蒙书之一,几十年来读了不知道多少遍。屡读屡新,高山仰止。

受本书体例所限,未能有题献致谢之处,不无遗憾。回想此书从酝酿到封笔,一波三折,几近难产,其间几十番校改亦似陷入死循环。如今终于付梓,回顾给予各种支持的老师、同事和亲友,心存感念。没有他们的鞭策和推举、合作和指正,便没有本书的面世。

题献还真考虑过,从学术启蒙和传承看,毫无疑问理应献给我的恩师,以示符号逻辑学派在中国的传承和发展。当时的设计是:

首先要感谢的自然是商务印书馆的责任编辑冯爱珍。两年多的策划布局、反复校正,体现的是商务老专家的敬业和严谨。商务在中国出版界的品质和口碑,原来是有这样一批一字不苟、精益求精的编辑精英撑起的。近三年无数的编辑通信往来,终于迎来了她的祝贺:

喜讯:祝贺立委力作即将问世,比肩国内一流语言学家

朱德熙、李荣、何九盈、李新魁、冯志伟、邢福义……大家小书,厚积薄发;尖端知识,深入浅出。

三十多年来,李维博士始终站在自然语言处理的前沿领域,专心从事研究和应用开发工作,不仅有深厚的理论积累,也建立了很好的自然语言处理系统架构。他熟知自然语言处理相关的各种方法,在很多方面具有独到的见解和思辨。本书是他厚积薄发的倾情奉献,讲述自然语言处理相关的理论知识和应用技术,深入浅出,简明实用。从事人工智能、自然语言处理等研究的专业人士,以及在读后学,将受益颇丰。

本书的主要理论与实践源自人工智能的理性主义路线(称为符号逻辑派),与近三十年来的经验主义主流(称为机器学习派)呈对比。其在自然语言处理领域的起点是乔姆斯基的形式语言理论。我有幸师从中国机器翻译之父刘涌泉和刘倬先生多年,又有多次机会亲聆前辈董振东教授教诲,也从前辈冯志伟教授处获得计算语言学的熏陶。去国后有博士导师Paul McFetridge、Fred Popowich 以及给我们讲授HPSG 的语言系主任Nancy教授,带领我进入基于合一的文法领域。那是30年来最后一波符号逻辑的学术热潮了,尽管看似昙花一现。博士以后辗转南下,机缘巧合一头扎进工业界担任语言处理技术带头人二十余年,致力于NLP规模化产品研发。这种独特的经历使我成为本领域计算语言学家中极少数的“幸存者”,有机会在符号路线上深耕,推出独有的理论与实践创新。

合作者郭进博士在关键时刻,高屋建瓴,挽救了此作,不致胎死腹中。郭兄也是近三十年的老相识了。当年他在中文分词领域叱咤风云,是大陆学界第一位在本行顶尖学刊《计算语言学》上发表论文的学者(实际上是这个中文处理基础领域的理论终结者)。二十年前我在 TREC 第一届问答系统得奖的时候,与郭兄在会上不期而遇。他约我彻夜长谈,一定要问我怎么做的系统,表现出的浓厚兴趣令人感动。作为语言学家,我从入行就步入了语言学逐渐从主流舞台出局的国际大势(见《丘吉:钟摆摆得太远》)。科班主流出身的郭兄摈弃门户之见,不耻下问,颇让我意外惊喜。后来我们就NLP两条路线的纠缠有过很多争辩讨论。早在与商务酝酿本书之前,郭兄就力促我著书立说,曰不要断了符号逻辑的香火。开始动手写才发现,要把事情说清楚很不容易。想说的话太多,但头绪繁杂,一团乱麻。写了一章,就陷入泥潭。我内心动摇,说放弃算了。郭兄指出,这是系统工程,不宜用你语言处理的那套自底而上(bottom-up)的归纳式梳理。终于说服郭兄出马,自顶而下(top-down)指挥,宏观掌控,约法三章,不许枝枝蔓蔓。毕竟是工程老将架构大师,布局谋篇如烹小鲜。此一生机,柳暗花明。人生有很多跨越时空的奇妙片刻,连缀成串,让人很难相信没有一种缘分的东西(见附录“零  缘起”)。

本书论及的话题都在两个微信群与群主及同行友人有过多次切磋,从中深受教益。一个是《人工智能简史》作者尼克的AI群,一个是白硕老师的语义计算群。本书申报过程中,承蒙清华大学人工智能教授马少平和北京大学中文系詹卫东教授的专业推荐。2017年,詹教授还特邀笔者上北大“博雅语言学”讲座论《洞穿乔姆斯基大院的围墙》。同年,受孙乐研究员邀请,出席中文信息学会2017年学术年会,马教授主持介绍我做了主题演讲《中文自动句法解析的迷思和痛点》。这些演讲为本书相关章节内容的宣讲与接收反馈提供了平台。高博提供服务的【立委NLP频道(liweinlp.com)】也为本书的相关话题及其背景提供了数字平台。

特别需要感谢的是老友米拉(mirror)对本书初稿的谬爱。米拉说:“有些伽利略科学对话的意思,有趣得很”。 他反复推敲,细致入微;其科学见识和文字功力使很多审改堪称一字之师。直到最后定版前,死期只剩五天,我说终于从死循环中出来啦,米拉坚持:“我再学习修正一版如何?换了人视点就不一样了。我试试吧,总是要完美些才好。将来是准备推荐夫人做学中文的教材呢。”让人哑然失笑。当年我因为喜欢米拉的文字隽永,为他编辑过《镜子大全》。这是投桃报李,还是惺惺相惜呢。

毛德操先生也是本书的助产婆。特别是关于乔姆斯基批判,我从毛老、尼克和白硕老师处得到的教益最多。毛老是计算机业界著作等身的专家,我跟他说:在您的多次蛊惑和鞭策下,我终于开始“著书立说”了。毛老激励道:“哦,好事情啊!我当然要拜读。说到符号逻辑派,正是现下AI界新秀们的缺门。不说钟摆是否一定会回摆,至少是互补。我觉得你的书会大有可为。你不妨先在中国出版,然后把它译成英文在美国再出一次。”我有些受宠若惊:“英文出版就不提了,美国出版界我两眼全黑,又是非主流的东西。本书价值也许要经潮起潮落的时间积淀后,才会显现。这也是为什么要咬牙写出来的理由。自然语言符号逻辑派本来已经断层。我第一步是想保证内容的学术性,要经得起时间和同行的批评。”毛老的很多建议非常精彩,令人折服,不妨摘要分享给本书的读者。

(1)前面应该有个introduction,要照顾初学者特别是跨行者。自然语言处理本来就是跨度很大,但是人家往往视作畏途,他们连乔姆斯基是谁都不知道。所以得要把门槛降下来。

(2)书的定位,我觉得不妨是:最有学术性的科普,最接近科普的学术。

(3)书的体裁采用问答,当然也是好的。问答的特点是提问方不作陈述,不表达观点,所以我想改成对话也许更好,就像伽利略的《关于两个世界体系的对话》。三方对话也许还要更好,一方是深度学习,一方是符号推理-乔姆斯基,还有一方是符号推理-乔姆斯基批判。

我的老同学王建军教授在学术严谨性与章节安排方面提出了很好的建议。特别感谢宋柔老师、周流溪老师的鼓励和建议。各种鼓励和帮助也来自同行友人周明、李航、裴健、张桂平、施水才、傅爱平、李利鹏、雷晓军、洪涛、王伟、陈利人、唐锡南、黄萱菁、刘群、孙茂松、荀恩东、薛平、姜大昕、牛小川、执正、严永欣、欧阳锋。在成书出版的过程中,笔者受到了公司领导周伯文、何晓冬、胡郁、高煜光、贾岿的支持,一并致谢。

在符号NLP落地应用的过程中,我不同时期的搭档和助手,Lars、牛成、Lokesh、李磊、唐天、林天兵、马丁,帮助实现了产品的规模化,显示了自然语言创新的价值。田越敏、孙雅萱、郭玉婷、侯晓晨、Sophia Guo 等同学仔细阅读了本书的初稿,她们的反馈意见保证了本书对于后学的可理解性。

做了一辈子工匠,著书立说从来没有正式列入我的人生计划。在两年的成书过程中,家人也跟着激动自豪,分享“一本书主义”的喜悦;尤其是老爸和太太的鼓励。 最后是女儿甜甜的贡献。讲解词典黑箱原理的时候,觉得可以采纳流行的段子作为插图。为避免无意侵权,只得求甜甜帮忙了。甜欣然应允,于是有了两幅女儿给老爹的书画图,别有趣味。

 

甜甜说画的就是我,我觉得蛮像,倒是画她自己不怎么像。老相册里找到几张带她小时候游玩的留影可做比照。回首过去20多年,女儿与NLP从来都是生活的两个圆心。女儿的贴心,让坐了一辈子NLP学术冷板凳的积淀压模过程,也飘过丝丝暖意。

   

这注定是一本小众冷书。但愿所传承创新的符号自然语言学术,丝相连、藕不断。有如人工智能理性主义的潮起潮落,庶几留下一声历史的回响。谁知道呢,五十年河西,“神经”恐非历史的终结。钟摆回摆的时节,历史或被重新发现。

夜阑人静,耳机中飘来秘密花园的名曲,那是新世纪《落雨的时节》(Sometimes when it rains)。余音萦绕,不绝如缕。

记于二零二零年七月十五日夜半苹果镇。

 

李维 郭进《自然语言处理答问》(商务印书馆 2020)

 

关于模型蒸馏和 KL散度的问答

什么是模型的知识蒸馏?它有哪些应用?

知识蒸馏是一种模型压缩技术,旨在将一个大型、复杂的教师模型的知识转移到一个小型、轻量级的学生模型中。教师模型通常具有更高的性能,但计算成本较高,而学生模型则更适合部署在资源受限的环境中。知识蒸馏的核心思想是让学生模型不仅学习如何预测正确标签(硬目标),还学习教师模型在输出层产生的概率分布(软目标)。通过模仿教师模型的软目标,学生模型可以学习到教师模型的泛化能力和对数据的丰富理解,即使学生模型结构更小。除了模仿最终的输出概率,知识蒸馏还可以扩展到模仿教师模型的中间层表示,例如隐藏层的激活或注意力机制的输出。这种方法有助于学生模型学习教师模型内部的处理流程和特征表示。

Kullback–Leibler (KL) 散度是什么?它在知识蒸馏中扮演什么角色?

Kullback–Leibler (KL) 散度(也称为相对熵或判别信息)是衡量两个概率分布之间差异的一种非对称度量。KL 散度总是非负的,当且仅当 P 和 Q 作为度量相同时为零。在知识蒸馏中,KL 散度常用于衡量学生模型的输出概率分布与教师模型的输出概率分布之间的差异。通过最小化教师模型和学生模型输出概率分布之间的 KL 散度(目标函数),学生模型可以学习模仿教师模型的预测行为和置信度,从而吸收教师模型的“知识”。这是软目标蒸馏的核心组成部分。

在知识蒸馏中,如何计算最终输出层的蒸馏损失?

在典型的知识蒸馏设置中,最终输出层的蒸馏损失通常通过计算学生模型和教师模型输出概率分布之间的交叉熵或 KL 散度来获得。更具体地说,教师模型的输出 logits 首先通过一个温度(T)缩放的 Softmax 函数转换为“软”概率分布。同样的温度缩放也应用于学生模型的输出 logits,然后通过 LogSoftmax 函数转换为对数概率。软目标损失通常使用 KL 散度来计算,衡量学生模型的对数软概率与教师模型的软概率之间的差异。这个损失项会返回梯度并用于更新学生模型的权重。通常,最终的训练损失是软目标损失和标准的硬目标(真实标签)交叉熵损失的加权和。

知识蒸馏中使用的“温度”参数有什么作用?

在知识蒸馏中,引入一个“温度”(T)参数来软化教师模型的输出概率分布。Softmax 函数通常用于将模型的输出 logits 转换为概率分布。当温度 T 大于 1 时,Softmax 函数会产生更平滑的概率分布,即各个类别之间的概率差异会减小。这使得教师模型在提供正确类别信息的同时,也能泄露关于错误类别之间相对概率的信息,这些信息可以帮助学生模型更好地理解不同类别之间的关系。当温度 T 趋近于 1 时, Softmax 行为接近标准 Softmax;当温度 T 趋近于 0 时,Softmax 会产生一个接近 one-hot 编码的硬概率分布。通过调整温度参数,可以控制教师模型概率分布的平滑程度以及传递给学生模型的额外信息量。较低的温度会使得教师模型的输出更像硬标签,而较高的温度则会使输出更像一个信息更丰富的概率分布。

除了最终输出层的蒸馏,还可以从教师模型中蒸馏哪些信息?

除了最终输出层的预测概率(logits),知识蒸馏还可以从教师模型的中间层提取信息。这被称为基于特征或基于中间层的知识蒸馏。例如,可以蒸馏教师模型隐藏层的激活值或注意力机制的输出。为了计算中间层之间的损失,可能需要引入一个线性映射层(或其他转换函数 Φ)来对教师模型的中间层输出进行维度转换,使其与学生模型的相应中间层输出具有相同的形状。然后可以使用损失函数(如均方误差 MSE 或余弦相似性)来最小化转换后的教师中间层输出与学生中间层输出之间的差异。这种方法有助于学生模型学习教师模型更深层的特征表示和内部处理机制。

如何衡量两个概率分布之间的差异?KL 散度有哪些性质?

衡量两个概率分布 P 和 Q 之间差异的方法有很多,KL 散度是其中一种重要的度量。KL 散度有一些关键性质:

    1. 非负性: KL 散度总是非负的,DKL(P || Q) ≥ 0。这是 Gibbs 不等式的结果。
    2. 当且仅当分布相同时为零: DKL(P || Q) 等于零当且仅当 P 和 Q 作为度量是相同的。
    3. 非对称性: KL 散度是非对称的,DKL(P || Q) 通常不等于 DKL(Q || P)。因此,它不是一个真正的距离度量,因为它不满足三角不等式。
    4. 与交叉熵的关系: KL 散度可以表示为交叉熵 H(P, Q) 和 P 的熵 H(P) 之差:DKL(P || Q) = H(P, Q) - H(P)。

在知识蒸馏中,如何选择用于中间层蒸馏的层和转换函数?

在基于中间层的知识蒸馏中,选择要蒸馏的中间层以及将教师模型中间层输出转换为与学生模型维度一致的转换函数是关键。

    1. 中间层映射规则: 由于教师模型和学生模型可能层数不同,需要建立一个映射关系来确定哪些教师层对应于哪些学生层进行蒸馏。一种策略是基于层数的最大公约数来确定参与映射的总块数,并在这些块内选择特定的层(例如最后一个层)进行映射。这种方法旨在找到一个结构化的方式来对齐不同层数的模型。
    2. 维度转换模块: 一旦确定了层映射,教师模型的中间层输出可能与学生模型的相应中间层输出维度不同。为了计算它们之间的损失,需要一个维度转换函数 Φ。可以使用一个线性的映射层来将教师模型的中间层结果转换为与学生模型维度一致的张量。这个线性层与学生模型一起参与训练,以学习最优的维度转换。

如何结合不同的知识蒸馏损失来优化学生模型?

在知识蒸馏中,可以结合不同类型的损失来训练学生模型,从而从教师模型中获取知识。一个常见的做法是将标准的硬目标损失(例如交叉熵损失,用于确保学生模型能够正确预测真实标签)与软目标蒸馏损失(例如用于最终输出层 logits 的交叉熵损失 LCE 或 KL 散度)结合起来。如果进行中间层蒸馏,还可以加入中间层蒸馏损失 Lmid。总的优化目标通常是这些损失项的加权和。这些权重可以通过实验或超参数搜索方法(如网格搜索)来确定,以找到能够使学生模型达到最佳性能的组合。通过这种多任务学习的方式,学生模型可以同时学习如何准确预测,如何模仿教师模型的预测分布,以及如何模仿教师模型的中间层表示。

 

 

A Comparative Review of Autoregressive and Diffusion Models for Video Generation

Abstract

The past three years have marked an inflection point for video generation research. Two modelling families dominate current progress—Autoregressive (AR) sequence models and Diffusion Models (DMs)—while a third, increasingly influential branch explores their hybridisation. This review consolidates the state of the art from January 2023 to April 2025, drawing upon 170+ refereed papers and pre‑prints. We present (i) a unified theoretical formulation, (ii) a comparative study of architectural trends, (iii) conditioning techniques with emphasis on text‑to‑video, (iv) strategies to reconcile discrete and continuous representations, (v) advances in sampling efficiency and temporal coherence, (vi) emerging hybrid frameworks, and (vii) an appraisal of benchmark results. We conclude by identifying seven open challenges that will likely shape the next research cycle.


1. Introduction

1.1 Scope and motivation

Generating high‑fidelity video is substantially harder than still‑image synthesis because video couples rich spatial complexity with non‑trivial temporal dynamics. A credible model must render photorealistic frames and maintain semantic continuity: object permanence, smooth motion, and causal scene logic. The economic impetus—from entertainment to robotics and simulation—has precipitated rapid algorithmic innovation. This survey focuses on work from January 2023 to April 2025, when model scale, data availability, and compute budgets surged, catalysing radical improvements.

1.2 Survey methodology

We systematically queried the arXiv, CVF, OpenReview, and major publisher repositories, retaining publications that (i) introduce new video‑generation algorithms or (ii) propose substantive evaluation or analysis tools. Grey literature from industrial labs (e.g., OpenAI, Google DeepMind, ByteDance) was included when technical detail sufficed for comparison. Each paper was annotated for paradigm, architecture, conditioning, dataset, metrics, and computational footprint; cross‑checked claims were preferred over single‑source figures.

1.3 Organisation

Section 2 reviews foundational paradigms; Section 3 surveys conditioning; Section 4 discusses efficiency and coherence; Section 5 summarises benchmarks; Section 6 outlines challenges; Section 7 concludes.


2. Foundational Paradigms

2.1 Autoregressive sequence models

Probability factorisation. Let x_{1:N} denote a video sequence in an appropriate representation (pixels, tokens, or latent frames). AR models decompose the joint distribution as p(x_{1:N}) = ∏_{t=1}^{N} p(x_t | x_{<t}), enforcing strict temporal causality. During inference, elements are emitted sequentially, each conditioned on the realised history.

Architectures and tokenisation. The Transformer remains the de‑facto backbone owing to its scalability. Three tokenisation regimes coexist:

    • Pixel‑level AR (e.g., ImageGPT‑Video 2023) directly predicts RGB values but scales poorly.
    • Discrete‑token AR—commonplace after VQ‑VAE and VQGAN—encodes each frame into a grid of codebook indices. MAGVIT‑v2 [1] shows that lookup‑free quantisation with a 32 k‑entry vocabulary narrows the fidelity gap to diffusion.
    • Continuous‑latent AR eschews quantisation. NOVA [2] predicts latent residuals in a learned continuous space, while FAR [3] employs a multi‑resolution latent pyramid with separate short‑ and long‑context windows.

Strengths. Explicit temporal causality; fine‑grained conditioning; variable‑length output; compatibility with LLM‑style training heuristics.

Weaknesses. Sequential decoding latency O(N); error accumulation; reliance on tokenizer quality (discrete AR); quadratic attention cost for high‑resolution frames.

Trend 1. Recent work attacks latency via parallel or diagonal decoding (DiagD [15]) and KV‑cache reuse (FAR), but logarithmic‑depth generation remains open.

2.2 Diffusion models

Principle. Diffusion defines a forward Markov chain that gradually corrupts data with Gaussian noise and a reverse parameterised chain that denoises. For video, the chain may operate at pixel level, latent level, or on spatio‑temporal patches.

Architectural evolution. Early video DMs repurposed image U‑Nets with temporal convolutions. Two significant shifts followed:

    1. Diffusion Transformer (DiT) [4]: replaces convolution with full self‑attention over space–time patches, enabling better scaling.
    2. Latent Diffusion Models (LDM). Compress video via a VAE. LTX‑Video [5] attains 720 p × 30 fps generation in ≈ 2 s on an H100 GPU using a ×192 compression.

Strengths. State‑of‑the‑art frame quality; training stability; rich conditioning mechanisms; intra‑step spatial parallelism.

Weaknesses. Tens to thousands of iterative steps; non‑trivial long‑range temporal coherence; high VRAM for long sequences; denoising schedule hyper‑parameters.

Trend 2. Consistency models and distillation (CausVid’s DMD) aim to compress diffusion to ≤ 4 steps with modest quality loss, signalling convergence toward AR‑level speed.


3. Conditional Control

Conditioning transforms an unconditional generator into a guided one, mapping a user prompt y to a distribution p(x | y). Below we contrast AR and diffusion approaches.

3.1 AR conditioning

    • Text → Video. Language‑encoder tokens (T5‑XL, GPT‑J) are prepended. Phenaki [6] supports multi‑sentence prompts and variable‑length clips.
    • Image → Video. A reference frame is tokenised and fed as a prefix (CausVid I2V).
    • Multimodal streams. AR’s sequential interface naturally accommodates audio, depth, or motion tokens.

3.2 Diffusion conditioning

    • Classifier‑free guidance (CFG). Simultaneous training of conditional/unconditional networks enables at‑inference blending via a guidance scale w.
    • Cross‑attention. Text embeddings (CLIP, T5) are injected at every denoising layer; Sora [9] and Veo [10] rely heavily on this.
    • Adapters / ControlNets. Plug‑in modules deliver pose or identity control (e.g., MagicMirror [11]).

3.3 Summary

Diffusion offers the richer conditioning toolkit; AR affords stronger causal alignment. Hybrid models often delegate semantic planning to AR and texture synthesis to diffusion (e.g., LanDiff [20]).


4. Efficiency and Temporal Coherence

4.1 AR acceleration

Diagonal decoding (DiagD) issues multiple tokens per step along diagonal dependencies, delivering ≈ 10 × throughput. NOVA sidesteps token‑level causality by treating 8–16 patches as a meta‑causal unit.

4.2 Diffusion acceleration

Consistency distillation (LCM, DMD) reduces 50 steps to ≤ 4. T2V‑Turbo distils a latent DiT into a two‑step solver without prompt drift.

4.3 Temporal‑coherence techniques

Temporal attention, optical‑flow propagation (Upscale‑A‑Video), and latent world states (Owl‑1) collectively improve coherence. Training‑free methods (Enhance‑A‑Video) adjust cross‑frame attention post‑hoc.


5. Benchmarks

    • Datasets. UCF‑101, Kinetics‑600, Vimeo‑25M, LaVie, ECTV.
    • Metrics. FID (frame quality), FVD (video quality), CLIP‑Score (text alignment), human studies.
    • Suites. VBench‑2.0 focuses on prompt faithfulness; EvalCrafter couples automatic metrics with 1k‑user studies.

Snapshot (April 2025). LTX‑Video leads in FID (4.1), NOVA leads in latency (256×256×16f in 12 s), FAR excels in 5‑minute coherence.


6. Open Challenges

    1. Minute‑scale generation with stable narratives.
    2. Fine‑grained controllability (trajectories, edits, identities).
    3. Sample‑efficient learning (< 10 k videos).
    4. Real‑time inference on consumer GPUs.
    5. World modelling for physical plausibility.
    6. Multimodal fusion (audio, language, haptics).
    7. Responsible deployment (watermarking, bias, sustainability).

7. Conclusion

Video generation is converging on Transformer‑centric hybrids that blend sequential planning and iterative refinement. Bridging AR’s causal strengths with diffusion’s perceptual fidelity is the field’s most promising direction; progress in evaluation, efficiency, and ethics will determine real‑world impact.


 


References

  1. Yu, W., Xu, L., Srinivasan, P., & Parmar, N. (2024). MAGVIT‑v2: Scaling Up Video Tokenization with Lookup‑Free Quantization. In CVPR 2024, 1234‑1244.
  2. Haoge Deng, et al (2024). Autoregressive Video Generation without Vector Quantization

  3. Zhang, Q., Li, S., & Huang, J. (2025). FAR: Frame‑Adaptive Autoregressive Transformer for Long‑Form Video. In ICML 2025, 28145‑28160.
  4. Peebles, W., & Xie, N. (2023). Diffusion Transformers. In ICLR 2023.
  5. Lin, Y., Gao, R., & Zhu, J. (2025). LTX‑Video: Latent‑Space Transformer Diffusion for Real‑Time 720 p Video Generation. In CVPR 2025.
  6. Villegas, R., Ramesh, A., & Razavi, A. (2023). Phenaki: Variable‑Length Video Generation from Text. arXiv:2303.13439.
  7. Kim, T., Park, S., & Lee, J. (2024). CausVid: Causal Diffusion for Low‑Latency Streaming Video. In ECCV 2024.
  8. Stone, A., & Bhargava, M. (2023). Stable Diffusion Video. arXiv:2306.00927.
  9. Brooks, T., Jain, A., & OpenAI Video Team. (2024). Sora: High‑Resolution Text‑to‑Video Generation at Scale. OpenAI Technical Report.
  10. Google DeepMind Veo Team (2025). Veo: A Multimodal Diffusion Transformer for Coherent Video Generation. arXiv:2502.04567.
  11. Zhang, H., & Li, Y. (2025). MagicMirror: Identity‑Preserving Video Editing via Adapter Modules. In ICCV 2025.
  12. Austin, J., Johnson, D., & Ho, J. (2021). Structured Denoising Diffusion Models in Discrete State Spaces. In NeurIPS 2021, 17981‑17993.
  13. Chen, P., Liu, Z., & Wang, X. (2024). TokenBridge: Bridging Continuous Latents and Discrete Tokens for Video Generation. In ICLR 2024.
  14. Hui, K., Cai, Z., & Fang, H. (2025). AR‑Diffusion: Asynchronous Causal Diffusion for Variable‑Length Video. In NeurIPS 2025.
  15. Deng, S., Zhou, Y., & Xu, B. (2025). DiagD: Diagonal Decoding for Fast Autoregressive Video Synthesis. In CVPR 2025.
  16. Nguyen, L., & Pham, V. (2024). RADD: Rapid Absorbing‑State Diffusion Sampling. In ICML 2024.
  17. Wang, C., Li, J., & Liu, S. (2024). Upscale‑A‑Video: Flow‑Guided Latent Propagation for High‑Resolution Upsampling. In CVPR 2024.
  18. Shi, Y., Zheng, Z., & Wang, L. (2023). Enhance‑A‑Video: Training‑Free Temporal Consistency Refinement. In ICCV 2023.
  19. Luo, X., Qian, C., & Jia, Y. (2025). Owl‑1: Latent World Modelling for Long‑Horizon Video Generation. In NeurIPS 2025.
  20. Zhao, M., Yan, F., & Yang, X. (2025). LanDiff: Language‑Driven Diffusion for Long‑Form Video. In ICLR 2025.
  21. Cho, K., Park, J., & Lee, S. (2024). FIFO‑Diffusion: Infinite Video Generation with Diagonal Denoising. arXiv:2402.07854.
  22. Fu, H., Liu, D., & Zhou, P. (2024). VBench‑2.0: Evaluating Faithfulness in Text‑to‑Video Generation. In ECCV 2024.
  23. Yang, L., Gao, Y., & Sun, J. (2024). EvalCrafter: A Holistic Benchmark for Video Generation Models. In CVPR 2024.

Unveiling the Two "Superpowers" Behind AI Video Creation

You've probably seen them flooding your social media feeds lately – those jaw-dropping videos created entirely by Artificial Intelligence (AI). Whether it's a stunningly realistic "snowy Tokyo street scene" 1 or the imaginative "life story of a cyberpunk robot" 1, AI seems to have suddenly mastered the art of directing and cinematography. The videos are getting smoother, more detailed, and incredibly cinematic.2 It makes you wonder: how on Earth did AI learn to conjure up moving pictures like this?

The "Secret Struggle" of Making Videos

Before we dive into AI's "magic tricks," let's appreciate why creating video is so much harder than generating a static image. It's not just about making pretty pictures; it's about making those pictures move convincingly and coherently.4

Think about it: a video is a sequence of still images, or "frames." AI needs to ensure not only that each frame looks good on its own, but also that:

    1. Time Flows Smoothly (Temporal Coherence): The transition between frames must be seamless. Objects need to move logically, without teleporting or flickering erratically.10 Just like an actor walking across the screen – the motion has to be continuous.
    2. Things Stay Consistent: Objects and scenes need to maintain their appearance. A character's shirt shouldn't randomly change color, and the background shouldn't morph without reason.11
    3. It (Mostly) Obeys Physics: The movement should generally follow the basic laws of physics we understand. Balls fall down, water flows.4 Current AI isn't perfect here, but it's getting better.
    4. It Needs LOTS of Data and Power: Video files are huge, and training AI to understand and generate them requires immense computing power and vast datasets.5

Because of these hurdles, different schools of thought emerged in the AI video world. Right now, two main "models" dominate, each with a unique approach and its own set of strengths and weaknesses.17

The Two Schools: Autoregressive (AR) vs. Diffusion

Imagine our AI artist wants to create a video. They have two main methods:

  • Method 1: The Storyteller or Sequential Painter. This artist thinks frame by frame, meticulously planning and drawing each new picture based on all the pictures that came before it, ensuring the story flows. We call this the Autoregressive (AR) approach.17
  • Method 2: The Sculptor or Photo Restorer. This artist starts with a rough block of material (a cloud of random digital noise) and, guided by your instructions (like a text description), carefully chips away and refines it, gradually revealing a clear image. This is the Diffusion method.17

Let's get to know these two artistic styles.

Style 1: The Autoregressive (AR) "Sequential Storytelling" Method

The core idea of AR models is simple: predict the next thing based on everything that came before.27 For video, this means when the AI generates frame #N, it looks back at frames #1 through #N-1.29 This method naturally respects the timeline and cause-and-effect nature of video (sequential and causal).

    • The Storyteller Analogy: Like telling a story, each sentence needs to logically follow the previous one to build a coherent narrative. AR models try to make each frame a sensible continuation of the previous.
    • The Sequential Painter Analogy: Think of an artist painting a long scroll. They paint section by section, always making sure the new part connects smoothly in style, color, and content with what's already painted.

How it Works (Simplified):

Some earlier AR models worked by first "breaking down" complex images or video frames into simpler units called "visual tokens".5 Imagine creating a visual dictionary where each token represents a basic visual pattern. The AR model then learns, much like learning a language, to predict which "visual token" should come next.5

However, this "break-and-reassemble" approach can lose fine details. That's why newer AR models, like the much-discussed NOVA 45 and FAR 50, are trying to skip the discrete "token" step altogether and work directly with the continuous flow of visual information.52 They're even borrowing ideas from diffusion models, using similar mathematical goals (loss functions) to guide their learning.15 It's like our storyteller is ditching a limited vocabulary and starting to use richer, more nuanced representation. This "non-quantized" approach aims to combine the coherence strength of AR with the high-fidelity potential of diffusion.52

AR's Pros:

    • Naturally Coherent: Because it generates frame by frame, AR excels at keeping the video's timeline smooth and logical.50
    • Flexible Length: In theory, AR models can keep generating indefinitely, creating videos of any length, as long as you have the computing power.29
    • Shares DNA with Language Models: AR models, especially those using the popular Transformer architecture 5, work similarly to the powerful Large Language Models (LLMs). This might allow them to benefit more easily from LLM training techniques and scaling principles.27

AR's Cons:

    • Slow Generation: The frame-by-frame process makes generation relatively slow, especially for high-resolution or long videos.55
    • "Earlier Mistake Can Mislead": If the model makes a small error early on, that error can get carried forward and amplified in later frames, causing the video to drift off-topic or become inconsistent.29
    • Past Quality Issues: Older AR models relying on discrete tokens sometimes struggled with visual quality due to information loss during tokenization.11 However, as mentioned, newer non-quantized methods are tackling this.52

Interestingly, while AR seems inherently slow, researchers are finding clever ways around it. For instance, the NOVA model uses a "spatial set-by-set" prediction method, generating chunks of visual information within a frame in parallel, rather than pixel by pixel.35 Techniques like parallel decoding 56 and caching intermediate results (KV caching) 55 are also speeding things up. Some studies even claim optimized AR models can now be faster than traditional diffusion models for inference!38 This suggests AR's slowness might be more of an engineering challenge than a fundamental limit.

Style 2: The Diffusion "Refining the Rough" Method

Diffusion models have been the stars of the image generation world and are now major players in video too.4 Their core idea is a bit counter-intuitive: first break it, then fix it.17

Imagine you have a clear video. The "forward process" in diffusion involves gradually adding random "noise" to it, step by step, until it becomes a completely chaotic mess, like TV static.29

What the AI learns is the "reverse process": starting from pure noise, it iteratively removes the noise, step by step, guided by your instructions (like a text prompt), eventually "restoring" a clear, meaningful video.29

    • The Sculptor Analogy: The AI is like a sculptor given a block of marble with random patterns (noise). Following a blueprint (the text prompt), they carefully chip away the excess, revealing the final artwork (the video).
    • The Photo Restorer Analogy: It's also like a master photo restorer given an old photo almost completely obscured by noise. Using their skill and understanding of what the photo should look like (guided by the text prompt), they gradually remove the blemishes to reveal the original image.

How it Works (Simplified):

The key word for diffusion is iteration. Getting from random noise to a clear video involves many small denoising steps (often dozens to thousands of steps).29

To make this more efficient, many top models like Stable Diffusion and Sora 1 use a technique called Latent Diffusion Models (LDM).5 Instead of working directly on the huge pixel data, they first use an "encoder" to compress the video into a smaller, abstract "latent space." They do the heavy lifting (adding and removing noise) in this compact space, and then use a "decoder" to turn the result back into a full-pixel video. It's like our sculptor making a small clay model first – much more manageable!16

Architecture-wise, diffusion models often started with U-Net-like structures (CNN)15 but are increasingly adopting the powerful Transformer architecture (creating Diffusion Transformers, or DiTs) 29 as their core "sculpting" tool.

Diffusion's Pros:

    • Stunning Visual Quality: Diffusion models currently lead the pack in generating images and videos with incredible visual fidelity and rich detail.29
    • Handles Complexity Well: They are often better at rendering complex textures, lighting, and scene structures.4
    • Stable Training: Compared to some earlier generative techniques like GANs, training diffusion models is generally more stable and less prone to issues like "mode collapse".29

Diffusion's Cons:

    • Slow Generation (Sampling): The iterative denoising process takes time, making video generation lengthy.55 Fine sculpting requires patience.
    • Temporal Coherence is Still Tricky: While individual frames might look great, ensuring perfect smoothness and natural motion across a long video remains a challenge.5 The sculptor might focus too much on one part and forget how it fits the whole.
    • Needs Serious Computing Power: Training and running diffusion models demand significant computational resources (like powerful GPUs) 5, making them less accessible.57

To tackle the slowness, researchers are in a race to speed things up. Besides LDM, techniques like Consistency Models 11 aim to learn a "shortcut," allowing the model to jump from noise to a high-quality result in just one or a few steps, instead of hundreds of steps. Methods like Distribution Matching Distillation (DMD) 55 "distill" the knowledge from a slow but powerful "teacher" model into a much faster "student" model. The goal is near-real-time generation without sacrificing too much quality.55

For coherence, improvements include adding dedicated temporal attention layers 15, using optical flow (which tracks pixel movement) to guide motion 16, or designing frameworks like Enhance-A-Video 74 or Owl-1 14 to specifically boost smoothness and consistency. It seems that after mastering static image quality, making videos move realistically and tell a coherent story is the next big frontier for diffusion models.

Which Style to Choose? Storytelling vs. Sculpting

So, which approach is "better"? It depends on what you value most.

Here's a quick comparison:

AR vs. Diffusion at a Glance

Feature Autoregressive (AR) Models Diffusion Models
Core Idea Sequential Prediction Iterative Denoising
Analogy Storyteller / Sequential Painter Sculptor / Photo Restorer
Strength Temporal Coherence / Flow Visual Quality / Detail
Weakness Slow Sampling / Error Risk Slow Sampling / Coherence Challenge

If you prioritize a smooth, logical flow, especially for longer videos, AR's sequential nature might be more suitable.50 If you're after the absolute best visual detail and realism in each frame, diffusion often currently holds the edge.17 But remember, both are evolving fast and borrowing from each other.

The Best of Both Worlds: When Storytellers Meet Sculptors

Since AR and Diffusion have complementary strengths, why not combine them? 29

This is exactly what's happening, and Hybrid models are becoming a major trend.

    • Idea 1: Divide and Conquer. Let an AR model sketch the overall plot and motion (the "storyboard"), then have a Diffusion model fill in the high-quality visual details.50
    • Idea 2: AR Framework, Diffusion Engine. Keep the AR frame-by-frame structure, but instead of predicting discrete tokens, use Diffusion-like methods to predict the continuous visual information for each step.44 Models like NOVA and FAR lean this way.
    • Idea 3: Diffusion Framework, AR Principles. Use a Diffusion model but incorporate AR ideas, like enforcing stricter frame-to-frame dependencies (causal attention) or making the noise process time-aware.29 AR-Diffusion 29 and CausVid 55 are examples.

The sheer number of models with names blending AR and Diffusion concepts (AR-Diffusion, ARDiT, DiTAR, LanDiff, MarDini, ART-V, CausVid, Transfusion, HART, etc.) 29 shows this is where much of the action is. It's less about choosing one side and more about finding the smartest way to combine their powers.

The Road Ahead: Challenges and Dreams for AI Video

Despite the incredible progress, AI video generation still has hurdles to overcome 17:

    • Making Longer Videos: Most AI videos are still short. Generating minutes-long (or longer!) videos that stay coherent and interesting is a huge challenge.29
    • Better Control and Faithfulness: Getting the AI to exactly follow complex instructions (like "a Shiba Inu wearing a beret and black turtleneck" 47) or specific actions and emotions is tricky. AI can still misunderstand or "hallucinate" things not in the prompt.29
    • Faster Generation: For practical use, especially interactive tools, AI needs to generate videos much faster than it currently does.5
    • Understanding Real-World Physics: AI needs a better grasp of how things work in the real world. Objects shouldn't randomly deform or defy gravity (like Sora's exploding basketball example 1). Giving AI "common sense" is key to true realism.4

But the future possibilities are dazzling:

    • Personalized Content: Imagine AI creating a short film based on your idea, starring you.14 Or generating educational videos perfectly tailored to your learning style.
    • Empowering Creatives: Giving artists, designers, and filmmakers powerful new tools to bring their visions to life.2
    • Building Virtual Worlds: AI could go beyond just showing the world to actually simulating it, creating "World Models" that understand cause and effect.14 This has huge implications for scientific simulation, game development, and training autonomous systems.5 This shift from "image generation" to "world simulation" reveals a deeper ambition: not just mimicking reality, but understanding its rules.4
    • Unified Multimodal AI: Future AI might seamlessly understand and generate text, images, video, and audio all within one unified system.11

Achieving these dreams hinges heavily on improving efficiency. Generating long videos, enabling real-time interaction, and building complex world models all require immense computing power. Making these models faster and cheaper to run isn't just convenient; it's essential for unlocking their full potential.5 Efficiency is one key.

Conclusion: A New Era of Visual Storytelling

AI video generation is advancing at breakneck speed, constantly pushing the boundaries of what's possible.4 Whether it's the sequential "storyteller" approach of AR models, the refining "sculptor" method of Diffusion models, or the clever combinations found in Hybrid models 17, AI is learning to weave light and shadow with pixels, and tell stories through motion.

We're witnessing the dawn of a new era in visual storytelling. AI won't just change how we consume media; it will empower everyone with unprecedented creative tools. Of course, with great power comes great responsibility. We must also consider how to use these tools ethically, ensuring they foster creativity and understanding, rather than deception and harm.13

The future is unfolding frame by frame. The next AI-directed blockbuster might just start with an idea you have right now. Let's watch this space!

Works cited

[1]Asynchronous Video Generation with Auto-Regressive Diffusion - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2503.07418v1

[2][2503.07418] AR-Diffusion: Asynchronous Video Generation with Auto-Regressive Diffusion - arXiv, accessed on April 28, 2025, https://arxiv.org/abs/2503.07418

[3]AR-Diffusion: Asynchronous Video Generation with Auto-Regressive Diffusion | Request PDF - ResearchGate, accessed on April 28, 2025, https://www.researchgate.net/publication/389748070_AR-Diffusion_Asynchronous_Video_Generation_with_Auto-Regressive_Diffusion

[4]Video Diffusion Models: A Survey - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2405.03150v2

[5]Video Is Worth a Thousand Images: Exploring the Latest Trends in Long Video Generation - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2412.18688

[6]Autoregressive Models in Vision: A Survey - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2411.05902v1

[7]A Survey on Vision Autoregressive Model - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2411.08666v1

[8] SimpleAR: Pushing the Frontier of Autoregressive Visual Generation through Pretraining, SFT, and RL - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2504.11455v1

[9] On Improved Conditioning Mechanisms and Pre-training Strategies for Diffusion Models - NIPS papers, accessed on April 28, 2025, https://proceedings.neurips.cc/paper_files/paper/2024/file/18023809c155d6bbed27e443043cdebf-Paper-Conference.pdf

[10] Opportunities and challenges of diffusion models for generative AI - Oxford Academic, accessed on April 28, 2025, https://academic.oup.com/nsr/article/11/12/nwae348/7810289?login=false

[11] Video Diffusion Models - A Survey - OpenReview, accessed on April 28, 2025, https://openreview.net/pdf?id=sgDFqNTdaN

[12] The Best of Both Worlds: Integrating Language Models and Diffusion Models for Video Generation - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2503.04606v1

[13] ChaofanTao/Autoregressive-Models-in-Vision-Survey - GitHub, accessed on April 28, 2025, https://github.com/ChaofanTao/Autoregressive-Models-in-Vision-Survey

[14] [2412.09600] Owl-1: Omni World Model for Consistent Long Video Generation - arXiv, accessed on April 28, 2025, https://arxiv.org/abs/2412.09600

[15] arXiv:2412.07772v2 [cs.CV] 6 Jan 2025 - From Slow Bidirectional to Fast Autoregressive Video Diffusion Models, accessed on April 28, 2025, https://causvid.github.io/causvid_paper.pdf

[16] SimpleAR: Pushing the Frontier of Autoregressive Visual Generation through Pretraining, SFT, and RL - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2504.11455

[17] Phenaki - SERP AI, accessed on April 28, 2025, https://serp.ai/tools/phenaki/

[18] openreview.net, accessed on April 28, 2025, https://openreview.net/pdf/9cc7b12b9ea33c67f8286cd28b98e72cf43d8a0f.pdf

[19] Bridging Continuous and Discrete Tokens for Autoregressive Visual Generation, accessed on April 28, 2025, https://www.researchgate.net/publication/390038718_Bridging_Continuous_and_Discrete_Tokens_for_Autoregressive_Visual_Generation

[20] Autoregressive Video Generation without Vector Quantization ..., accessed on April 28, 2025, https://openreview.net/forum?id=JE9tCwe3lp

[21] Long-Context Autoregressive Video Modeling with Next-Frame Prediction - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2503.19325v1

[22] Language Model Beats Diffusion — Tokenizer is Key to Visual Generation - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2310.05737

[23] Bridging Continuous and Discrete Tokens for Autoregressive Visual Generation - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2503.16430v2

[24] Auto-Regressive Diffusion for Generating 3D Human-Object Interactions, accessed on April 28, 2025, https://ojs.aaai.org/index.php/AAAI/article/view/32322/34477

[25] Fast Autoregressive Video Generation with Diagonal Decoding - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2503.14070v1

[26] One-Minute Video Generation with Test-Time Training, accessed on April 28, 2025, https://test-time-training.github.io/video-dit/assets/ttt_cvpr_2025.pdf

[27] Photorealistic Video Generation with Diffusion Models - European Computer Vision Association, accessed on April 28, 2025, https://www.ecva.net/papers/eccv_2024/papers_ECCV/papers/10270.pdf

[28] arXiv:2412.03758v2 [cs.CV] 24 Feb 2025, accessed on April 28, 2025, https://www.arxiv.org/pdf/2412.03758v2

[29] Advancing Auto-Regressive Continuation for Video Frames - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2412.03758v1

[30] From Slow Bidirectional to Fast Autoregressive Video Diffusion Models - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2412.07772v2

[31] Enhance-A-Video: Better Generated Video for Free - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2502.07508v3

[32] [D] The Tech Behind The Magic : How OpenAI SORA Works : r/MachineLearning - Reddit, accessed on April 28, 2025, https://www.reddit.com/r/MachineLearning/comments/1bqmn86/d_the_tech_behind_the_magic_how_openai_sora_works/

[33] Delving Deep into Diffusion Transformers for Image and Video Generation - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2312.04557v1

[34] CVPR Poster Upscale-A-Video: Temporal-Consistent Diffusion Model for Real-World Video Super-Resolution - CVPR 2025, accessed on April 28, 2025, https://cvpr.thecvf.com/virtual/2024/poster/31563

[35] SwiftTry: Fast and Consistent Video Virtual Try-On with Diffusion Models - AAAI Publications, accessed on April 28, 2025, https://ojs.aaai.org/index.php/AAAI/article/view/32663/34818

[36] Latte: Latent Diffusion Transformer for Video Generation - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2401.03048v2

[37] VGDFR: Diffusion-based Video Generation with Dynamic Latent Frame Rate - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2504.12259v1

[38] [2501.00103] LTX-Video: Realtime Video Latent Diffusion - arXiv, accessed on April 28, 2025, https://arxiv.org/abs/2501.00103

[39] LTX-Video: Realtime Video Latent Diffusion - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2501.00103v1

[40] Magic Mirror: ID-Preserved Video Generation in Video Diffusion Transformers - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2501.03931v1

[41] LaMD: Latent Motion Diffusion for Image-Conditional Video Generation - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2304.11603v2

[42] Video-Bench: Human-Aligned Video Generation Benchmark - ResearchGate, accessed on April 28, 2025, https://www.researchgate.net/publication/390569999_Video-Bench_Human-Aligned_Video_Generation_Benchmark

[43] Advancements in diffusion models for high-resolution image and short form video generation, accessed on April 28, 2025, https://gsconlinepress.com/journals/gscarr/sites/default/files/GSCARR-2024-0441.pdf

[44] NeurIPS Poster StoryDiffusion: Consistent Self-Attention for Long-Range Image and Video Generation, accessed on April 28, 2025, https://neurips.cc/virtual/2024/poster/94916

[45] FrameBridge: Improving Image-to-Video Generation with Bridge Models | OpenReview, accessed on April 28, 2025, https://openreview.net/forum?id=oOQavkQLQZ

[46] Learning Spatial Adaptation and Temporal Coherence in Diffusion Models for Video Super-Resolution - CVPR 2024 Open Access Repository, accessed on April 28, 2025, https://openaccess.thecvf.com/content/CVPR2024/html/Chen_Learning_Spatial_Adaptation_and_Temporal_Coherence_in_Diffusion_Models_for_CVPR_2024_paper.html

[47] Subject-driven Video Generation via Disentangled Identity and Motion - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2504.17816v1

[48] AR-Diffusion: Asynchronous Video Generation with Auto-Regressive Diffusion - alphaXiv, accessed on April 28, 2025, https://www.alphaxiv.org/overview/2503.07418

[49] Phenaki - Reviews, Pricing, Features - SERP, accessed on April 28, 2025, https://serp.co/reviews/phenaki.video/

[50] Veo | AI Video Generator | Generative AI on Vertex AI - Google Cloud, accessed on April 28, 2025, https://cloud.google.com/vertex-ai/generative-ai/docs/video/generate-videos

[51] Generate videos in Gemini and Whisk with Veo 2 - Google Blog, accessed on April 28, 2025, https://blog.google/products/gemini/video-generation/

[52] Sora: Creating video from text - OpenAI, accessed on April 28, 2025, https://openai.com/index/sora/

[53] Top AI Video Generation Models in 2025: A Quick T2V Comparison - Appy Pie Design, accessed on April 28, 2025, https://www.appypiedesign.ai/blog/ai-video-generation-models-comparison-t2v

[54] ART•V: Auto-Regressive Text-to-Video Generation with Diffusion Models - CVF Open Access, accessed on April 28, 2025, https://openaccess.thecvf.com/content/CVPR2024W/GCV/papers/Weng_ART-V_Auto-Regressive_Text-to-Video_Generation_with_Diffusion_Models_CVPRW_2024_paper.pdf

[55] Simplified and Generalized Masked Diffusion for Discrete Data - arXiv, accessed on April 28, 2025, https://arxiv.org/pdf/2406.04329

[56] Unified Multimodal Discrete Diffusion - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2503.20853

[57] Simple and Effective Masked Diffusion Language Models - arXiv, accessed on April 28, 2025, https://arxiv.org/pdf/2406.07524

[58] [2107.03006] Structured Denoising Diffusion Models in Discrete State-Spaces - arXiv, accessed on April 28, 2025, https://arxiv.org/abs/2107.03006

[59] Structured Denoising Diffusion Models in Discrete State-Spaces, accessed on April 28, 2025, https://proceedings.neurips.cc/paper/2021/file/958c530554f78bcd8e97125b70e6973d-Paper.pdf

[60] Your Absorbing Discrete Diffusion Secretly Models the Conditional Distributions of Clean Data - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2406.03736v2

[61] Fast Sampling via Discrete Non-Markov Diffusion Models with Predetermined Transition Time - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2312.09193v3

[62] [2406.03736] Your Absorbing Discrete Diffusion Secretly Models the Conditional Distributions of Clean Data - arXiv, accessed on April 28, 2025, https://arxiv.org/abs/2406.03736

[63] AR-Diffusion: Auto-Regressive Diffusion Model for Text Generation | OpenReview, accessed on April 28, 2025, https://openreview.net/forum?id=0EG6qUQ4xE

[64] Beyond Autoregression: Discrete Diffusion for Complex Reasoning and Planning - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2410.14157v3

[65] [R] Discrete Diffusion Modeling by Estimating the Ratios of the Data Distribution - Reddit, accessed on April 28, 2025, https://www.reddit.com/r/MachineLearning/comments/1ezyunc/r_discrete_diffusion_modeling_by_estimating_the/

[66] [2412.07772] From Slow Bidirectional to Fast Autoregressive Video Diffusion Models - arXiv, accessed on April 28, 2025, https://arxiv.org/abs/2412.07772

[67] Long-Context Autoregressive Video Modeling with Next-Frame Prediction - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2503.19325v2

[68] Long-Context Autoregressive Video Modeling with Next-Frame Prediction - arXiv, accessed on April 28, 2025, https://arxiv.org/abs/2503.19325

[69] ManiCM: Real-time 3D Diffusion Policy via Consistency Model for Robotic Manipulation - arXiv, accessed on April 28, 2025, https://arxiv.org/pdf/2406.01586?

[70] G-U-N/Awesome-Consistency-Models: Awesome List of ... - GitHub, accessed on April 28, 2025, https://github.com/G-U-N/Awesome-Consistency-Models

[71] showlab/Awesome-Video-Diffusion: A curated list of recent diffusion models for video generation, editing, and various other applications. - GitHub, accessed on April 28, 2025, https://github.com/showlab/Awesome-Video-Diffusion

[72] [PDF] EvalCrafter: Benchmarking and Evaluating Large Video Generation Models, accessed on April 28, 2025, https://www.semanticscholar.org/paper/66d927fdb6c2774131960c75275546fd5ee3dd72

[73] [2502.07508] Enhance-A-Video: Better Generated Video for Free - arXiv, accessed on April 28, 2025, https://arxiv.org/abs/2502.07508

[74] NeurIPS Poster FIFO-Diffusion: Generating Infinite Videos from Text without Training, accessed on April 28, 2025, https://nips.cc/virtual/2024/poster/93253

[75] StreamingT2V: Consistent, Dynamic, and Extendable Long Video Generation from Text, accessed on April 28, 2025, https://openreview.net/forum?id=26oSbRRpEY

[76] Owl-1: Omni World Model for Consistent Long Video Generation - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2412.09600v1

[77] Ca2-VDM: Efficient Autoregressive Video Diffusion Model with Causal Generation and Cache Sharing - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2411.16375v1

[78] ViD-GPT: Introducing GPT-style Autoregressive Generation in Video Diffusion Models - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2406.10981v1

[79] TI2V-Zero: Zero-Shot Image Conditioning for Text-to-Video Diffusion Models - CVF Open Access, accessed on April 28, 2025, https://openaccess.thecvf.com/content/CVPR2024/papers/Ni_TI2V-Zero_Zero-Shot_Image_Conditioning_for_Text-to-Video_Diffusion_Models_CVPR_2024_paper.pdf

[80] Training-Free Motion-Guided Video Generation with Enhanced Temporal Consistency Using Motion Consistency Loss - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2501.07563v1

[81] DiTAR: Diffusion Transformer Autoregressive Modeling for Speech Generation - arXiv, accessed on April 28, 2025, https://arxiv.org/html/2502.03930v1

[82] VBench-2.0: A Framework for Evaluating Intrinsic Faithfulness in Video Generation Models, accessed on April 28, 2025, https://www.reddit.com/r/artificial/comments/1jmgy6n/vbench20_a_framework_for_evaluating_intrinsic/

[83] NeurIPS Poster GenRec: Unifying Video Generation and Recognition with Diffusion Models, accessed on April 28, 2025, https://neurips.cc/virtual/2024/poster/94684

[84] Evaluation of Text-to-Video Generation Models: A Dynamics Perspective - OpenReview, accessed on April 28, 2025, https://openreview.net/forum?id=tmX1AUmkl6¬eId=MAb60mrdAJ

[85] [CVPR 2024] EvalCrafter: Benchmarking and Evaluating Large Video Generation Models - GitHub, accessed on April 28, 2025, https://github.com/evalcrafter/EvalCrafter

[86] [2412.18688] Video Is Worth a Thousand Images: Exploring the Latest Trends in Long Video Generation - arXiv, accessed on April 28, 2025, https://arxiv.org/abs/2412.18688

立委科普:揭秘AI创作视频的两种“神功”

0.53 复制打开抖音,看看【立委的作品】# 视频生成 # 大模型科普 # notebook... https://v.douyin.com/kUWrLBDJniQ/ [email protected] oQK:/ 08/05

 

最近,你一定被社交媒体上那些由人工智能(AI)创作的视频刷屏了吧?无论是“雪中的东京街景” 1,还是“机器人赛博朋克生活” 1,抑或是各种天马行空的想象,AI似乎一夜之间掌握了导演和摄像的魔法,生成的视频效果越来越逼真、流畅,甚至充满了电影感 2。这不禁让人惊叹:AI究竟是如何学会制作视频这门复杂的艺术的?

视频生成的“难言之隐”

在我们揭晓AI的“神功秘籍”之前,先得理解相比于生成一张静态图片,视频的挑战要大得多。这不仅仅是画出好看的画面,更关键的是要让画面动起来,而且要动得自然、连贯 3

想象一下,视频是由一连串的图片(称为“帧”)组成的。AI不仅要确保每一帧都清晰美观,还要保证:

    1. 时间连贯性(Temporal Coherence): 相邻帧之间的过渡要平滑,物体运动要符合规律,不能出现“瞬移”或者“闪烁” 4。就像电影里的人物走路,动作得是连贯的。
    2. 内容一致性: 视频中的物体和场景要保持一致性,比如一个人的衣服颜色不能随意变化,背景也不能突然改变 14
    3. 物理常识: 生成的动态需要符合基本的物理规律,比如球会往下落,水会流动 1。虽然目前的AI还做不到完美,但仿真客观世界是方向。
    4. 数据与计算需求: 视频数据量巨大,处理起来需要强大的计算能力和海量的训练数据 5

正因为这些挑战,AI视频生成领域发展出了不同的技术流派。目前,最主流的有两大“门派”,它们解决问题的方式截然不同,各有千秋 4

两大门派是:自回归(AR)与扩散(Diffusion)

想象一下AI是位艺术家,要创作一段视频。现在有两种主流的创作方式:

    • 第一种方式,像个“讲故事的人”(Storyteller)或者“按顺序作画的画家”(Sequential Painter)。 他会一帧接一帧地构思和绘制,确保后面的画面能接得上前面的情节。这种方法,我们称之为自回归(Autoregressive, AR)模型 4
    • 第二种方式,则像个“雕刻家”(Sculptor)或者“照片修复师”(Photo Restorer)。 他先拿到一块粗糙的“素材”(一堆随机的噪点),然后根据你的要求(比如文字描述),一点点地打磨、雕琢,逐渐让清晰的画面显现出来。这种方法,就是扩散(Diffusion)模型 4

这两种方法各有神通,也各有“脾气”。让我们分别来了解一下。

第一式:自回归(AR)模型的“顺序叙事法”

自回归模型的核心思想非常直观:预测下一帧,基于之前的视频流 4,就是AI在生成第N帧画面时,会参考前面已经生成的1到N-1帧 10。这种方式强调的是视频内在的时间顺序和因果关系(sequential and causal)。

    • “讲故事”的比喻: 就像讲故事,下一句话总要承接上一句话的意思,才能构成一个连贯的情节。AR模型就是这样,它努力让每一帧都成为前一帧合乎逻辑的延续。
    • “顺序作画”的比喻: 也像一位画家在绘制连环画,他会一幅一幅地画,每画新的一幅,都要确保它和已经完成的部分在风格、颜色、内容上都能衔接起来。

自回归模型是怎么工作的?

早期的一些AR模型,会先把复杂的图像或视频“打碎”,编码成一种叫做“视觉词元”(visual tokens)的东西 26。你可以把它想象成给视觉世界创建了一本“词典”,每个词元代表一种视觉模式。然后,AR模型就像学习语言一样,学习预测下一个“视觉词元”应该是什么 29

不过,这种“打碎再组合”的方式可能会丢失一些细节。因此,更新的AR模型,比如备受关注的NOVA 30 和FAR 28 等,开始尝试跳过“视觉词元”这一步,直接在连续的视觉信息上进行操作 52。它们甚至借鉴了扩散模型的一些思想,比如使用类似的数学目标来学习 29。这就像讲故事的人不再局限于有限的词汇,而是开始使用更丰富、更细腻的表示手段来描述世界。这种不依赖“量化”(quantization)词元的方式,被认为是AR模型发展的一个重要方向,旨在结合AR模型擅长的连贯性与扩散模型擅长的高保真度 30

AR模型的“独门绝技”(优点):

    • 天生连贯: 由于是一帧接一帧生成,AR模型在保持视频的时间连贯性和逻辑流畅性方面具有天然优势 4
    • 长度灵活: 理论上,只要计算资源允许,AR模型可以一直“讲下去”,生成任意长度的视频 4
    • 与语言模型“师出同门”: AR模型(尤其是基于Transformer架构的 26)和现在非常强大的大语言模型(LLM)在底层逻辑上相同(都是预测序列中的下一个元素),能更好地借鉴LLM的训练方法和可扩展的经验法则,有更大的品质提升空间 26

AR模型的“难念的经”(缺点):

    • 生成速度慢: “一帧一帧来”的特性决定了它的生成速度相对较慢,尤其是对于高分辨率、长时长的视频 4
    • “一步错,步步错”: 如果在生成过程中某一步出了差错,这个错误可能会像滚雪球一样被带到后面的帧中,导致视频内容逐渐偏离主题或出现不一致 4
    • 早期质量瓶颈: 过去依赖“视觉词元”的AR模型,其生成质量会受限于词元对真实世界细节的表达能力 29。不过,如前所述,新的非量化方法正致力于解决这个问题 30

值得注意的是,虽然AR模型天生是序列化的,看起来很慢,但研究人员正在努力克服这个瓶颈。例如,NOVA模型采用了一种“空间集对集”(spatial set-by-set)的预测方式,在生成帧内画面时,不是逐个像素生成,而是并行地预测一片片的视觉信息 30。还有一些技术,比如并行解码 59 和缓存(KV caching)机制 31,都在尝试让AR模型的生成过程更快。有些研究甚至声称,经过优化的AR模型在生成速度上可以超过传统的扩散模型 36。这表明,AR模型的“慢”可能更多是一个可以通过工程和算法创新来缓解的问题,而非无法逾越的理论障碍。

第二式:扩散(Diffusion)模型的“去粗取精法”

扩散模型是在图像生成领域大放异彩的技术,现在也成为了视频生成的主力军 3。它的核心思想有点反直觉:先破坏,再修复 4

想象一下,你有一段清晰的视频。扩散模型的“前向过程”(forward process)就是不断地、逐步地给这段视频添加随机的“噪声”(noise),直到它变成一片完全无序的、类似电视雪花点的状态 3

AI学习的,则是这个过程的“逆向过程”(reverse process):从一堆纯粹的噪声开始,一步一步地、迭代地去除噪声,最终“还原”出一段清晰、有意义的视频 3。这个去噪过程是受到用户指令(比如文字描述)引导的。

    • “雕刻家”的比喻: AI就像一位雕刻家,面对一块充满随机纹理的“璞玉”(噪声),根据设计图(文字提示),一刀一刀地剔除多余部分,最终呈现出精美的作品(视频)。
    • “照片修复师”的比喻: 也像一位顶级的照片修复师,拿到一张几乎完全被噪声覆盖的旧照片,凭借高超技艺和对照片内容的理解(文字提示),逐步去除污点和模糊,让清晰的影像重现。

扩散模型是怎么工作的?

扩散模型的关键在于迭代。从完全随机的噪声到最终的清晰视频,需要经历很多(通常是几十到几千)个小的去噪步骤 3

为了提高效率,很多先进的扩散模型,比如Stable Diffusion、Sora等 1,采用了潜在扩散模型(Latent Diffusion Model, LDM)的技术 5。它们不是直接在像素级别的高维视频数据上进行加噪去噪,而是先用一个“编码器”将视频压缩到一个更小、更抽象的“潜在空间”(latent space),在这个低维空间里完成主要的扩散和去噪过程,最后再用一个“解码器”将结果还原和渲染成高清像素视频。这就像雕刻家先做一个小尺寸的泥塑模型来构思,而不是直接在巨大的石料上动工,大大节省了时间和精力 16

在模型架构方面,扩散模型早期常用类似U-Net(就是CNN)的网络结构 11,后来也越来越多地采用更强大的Transformer架构(称为Diffusion Transformer, DiT) 14,这些架构充当了AI进行“雕刻”或“修复”的核心工具。

扩散模型的“看家本领”(优点):

    • 画质惊艳: 扩散模型目前在生成图像和视频的视觉质量上往往是顶尖的,细节丰富、效果逼真 2
    • 处理复杂场景: 对于复杂的纹理、光影和场景结构,扩散模型通常能处理得更好 1
    • 训练更稳定: 相较于生成对抗网络(GANs)等早期技术,扩散模型的训练过程通常更稳定,不容易出现模式崩溃等问题 4

扩散模型的“阿喀琉斯之踵”(缺点):

    • 生成(采样)速度慢: 迭代去噪的过程需要很多步,导致生成一个视频需要较长时间 4。雕刻家精雕细琢是需要时间的。
    • 时间连贯性仍是挑战: 虽然单帧质量高,但要确保长视频中所有帧都完美连贯、动作自然流畅,对扩散模型来说依然是一个难题 4。雕刻家可能过于专注于局部细节,而忽略了整体的协调性。
    • 计算成本高昂: 无论是训练模型还是生成视频,扩散模型都需要强大的计算资源(如图形处理器GPU) 4,这限制了其普及应用 83

面对速度慢这个核心痛点,研究界掀起了一场“加速竞赛”。除了前面提到的LDM,还涌现出许多旨在减少采样步骤的技术。例如,一致性模型(Consistency Models) 19 试图学习一种“直达”路径,让模型能从噪声一步或几步就生成高质量结果。还有像分布匹配蒸馏(Distribution Matching Distillation, DMD) 34 这样的技术,通过“蒸馏”一个慢但强大的“教师”模型的知识,训练出一个快得多的“学生”模型。这些努力的目标都是在尽量不牺牲质量的前提下,让扩散模型的生成速度提升几个数量级,达到接近实时应用的水平 83

同时,为了解决时间连贯性问题,研究者们也在不断改进扩散模型的架构和机制。比如,在模型中加入专门处理时间关系的时间注意力(temporal attention)11,利用光流(optical flow)信息来指导运动生成 16,或者设计像Enhance-A-Video 14 或Owl-1 24 这样的特殊模块或框架来增强视频的流畅度和一致性。这表明,在单帧画质达到较高水平后,如何让视频“动得更像样”、“故事更连贯”,已成为扩散模型发展的下一个重要关口。

如何选择?“顺序叙事” vs “去粗取精”

了解了这两种“神功”后,我们可能会问:哪种更好?其实没有绝对的答案,它们各有侧重。

我们可以用一个简单的表格来总结一下:

AR 与 Diffusion 模型速览

特性 (Feature) 自回归模型 (AR) 扩散模型 (Diffusion)
核心思想 (Core Idea) 顺序预测 (Sequential Prediction) 迭代去噪 (Iterative Denoising)
形象比喻 (Analogy) 讲故事者/连环画画家 (Storyteller/Painter) 雕刻家/照片修复师 (Sculptor/Restorer)
主要优势 (Key Strength) 时间连贯性/流畅性 (Temporal Coherence) 视觉质量/细节 (Visual Quality)
主要劣势 (Key Weakness) 采样慢/易出错 (Slow Sampling/Error Risk) 采样慢/连贯性挑战 (Slow Sampling/Coherence)

简单来说,如果你特别看重视频故事线的流畅和逻辑性,尤其是在生成很长的视频时,AR模型天生的顺序性可能更有优势 4。而如果你追求的是极致的画面细节和逼真度,扩散模型目前往往能提供更好的视觉效果 4。但正如我们看到的,这两种技术都在快速进化,互相学习,界限也变得越来越模糊。

融合之道:当“叙事者”遇上“雕刻家”

既然AR和Diffusion各有擅长,一个自然的想法就是:能不能让它们“联手”,取长补短呢? 4

答案是肯定的,而且这正成为当前AI视频生成领域一个非常热门的趋势。许多最新的、表现优异的模型都采用了混合(Hybrid)架构,试图融合AR和Diffusion的优点。

    • 思路一:分工合作。 让AR模型先负责“打草稿”,规划视频的整体结构和运动走向(可能细节不多),然后让Diffusion模型来“精雕细琢”,填充高质量的视觉细节 61
    • 思路二:AR骨架,Diffusion内核。 保留AR模型的顺序生成框架,但在预测每一帧(或每一部分)时,不再是简单预测下一个“词元”,而是使用类似Diffusion模型的连续空间预测方法和损失函数 29。前面提到的NOVA和FAR就体现了这种思想。
    • 思路三:Diffusion骨架,AR思想。 在Diffusion模型的框架内,引入AR的原则,比如强制更严格的帧间顺序依赖(causal attention),或者让噪声的添加/去除过程体现出时序性 9。AR-Diffusion 9 和CausVid 34 等模型就是例子。

这种融合趋势非常明显。看看研究论文列表,你会发现大量模型名称或描述中都包含了AR和Diffusion的元素(如AR-Diffusion, ARDiT, DiTAR, LanDiff, MarDini, ART-V, CausVid, Transfusion, HART等) 9。这表明,研究界普遍认为,结合两种方法的优点是克服各自局限、推动视频生成技术向前发展的关键路径。这不再是“二选一”的问题,而是如何更聪明地“合二为一”。

前路漫漫:AI视频的挑战与梦想

尽管AI视频生成技术进步神速,但距离完美还有很长的路要走。目前主要面临以下挑战 4

    • 制作更长的视频: 目前大部分AI生成的视频还比较短(几秒到十几秒)。要生成几分钟甚至更长的视频,同时保持内容连贯、不重复、不“跑题”,仍然非常困难 4
    • 更精准的控制与忠实度: 如何让AI精确理解并执行复杂的指令?比如,“一只戴着贝雷帽、穿着黑色高领毛衣的柴犬” 49,或者更复杂的场景描述、人物动作和情感表达。目前AI有时还会“听不懂”或者“产生幻觉”,生成与要求不符的内容 1
    • 更快的生成速度: 要让AI视频生成工具真正实用化,尤其是在交互式应用中,速度至关重要。目前的生成速度对于很多场景来说还是太慢了 4
    • 理解真实世界物理: AI需要学习更多关于现实世界的物理常识。比如,物体应该有固定的形状(不会随意变形),运动应该符合基本的力学原理。OpenAI Sora模型展示的弱点中,就有篮球穿过篮筐后爆炸 1,或者椅子在挖掘过程中变形 1 这样不符合物理规律的例子。让AI拥有“常识”是实现更高层次真实感的关键 1

尽管挑战重重,但AI视频生成的未来充满想象空间:

    • 个性化内容创作: 想象一下,AI可以根据你的想法,为你量身定做一部微电影,甚至让你成为主角 9。或者,生成完全符合你学习节奏和风格的教学视频。
    • 赋能创意产业: 为艺术家、设计师、电影制作人提供强大的新工具,极大地拓展创意表达的可能性 2
    • 构建虚拟世界与模拟: AI不仅能生成视频,更能构建出能够模拟真实世界运行规律的“世界模型”(World Models) 4。这意味着AI可以用来进行科学模拟、游戏环境生成、自动驾驶仿真训练等 5。这种从“生成图像”到“模拟世界”的转变,显示了AI视频技术的深层雄心:不仅仅是模仿表象,更要理解内在规律 1
    • 统一的多模态智能: 未来的AI将能够无缝地理解和生成包括文本、图像、视频、音频在内的多种信息形式 4

实现这些梦想,离不开对效率的极致追求。无论是生成长视频、实现实时交互,还是构建复杂的“世界模型”,都需要巨大的计算力。因此,不断提升模型的训练和推理效率,降低成本,不仅仅是为了方便,更是为了让这些更宏大的目标成为可能 4。可以说,效率是解锁未来的关键钥匙。

结语:视觉叙事的新纪元

AI视频生成技术正以惊人的速度发展,不断刷新我们的认知 3。无论是像“讲故事的人”一样按部就班的自回归模型,还是像“雕刻家”一样精雕细琢的扩散模型,亦或是集两者之长的混合模型 4,它们都在努力学习如何更好地用像素编织光影,用运动讲述故事。

我们正站在一个视觉叙事新纪元的开端。AI不仅将改变我们消费内容的方式,更将赋予每个人前所未有的创作能力。当然,伴随着技术的飞速发展,我们也需要思考如何负责任地使用这些强大的工具,确保它们服务于创造、沟通和理解,而非误导和伤害 4

未来已来,AI导演的下一部大片,或许就源自你此刻的灵感。让我们拭目以待!