我的征尘是星辰大海。。。
The dirt and dust from my pilgrimage forms oceans of stars...
-------当记忆的篇章变得零碎,当追忆的图片变得模糊,我们只能求助于数字存储的永恒的回忆
作者:黄教授
手机视频列表
为什么大模型处理不了长文本
视频
音频
原始脚本
为什么 Transformer 处理不了超长文本?根源不是位置编码,而是全关联的取舍。 很多人读完 Attention is all you need ,总误以为 Transformer 的上下文长度限制是位置编码 Position Encoding 导致的,觉得是它记不住太长的顺序,才让模型只能处理2K4K的 token。 但实际做过超长文本任务,比如处理整本书,长代码就会明白。 真正卡脖子的是自注意力机制,Self-Attention,全关联计算的设计逻辑。 位置编码只是补短板的工具,从来不是设限制的元凶。 要彻底理解这一点,得先搞懂,自注意力为什么非要暴力计算所有 token 关联。 这种设计的优势和代价又是什么?一,自注意力的核心不是并行,是全关联地毯式搜索。 自注意力能让 Transformer 超越 RNN,关键不是并行计算,而是它能一次性捕捉序列中所有 token 的关联,哪怕两个词隔了100个词,也不会像 RNN 那样记混或忘记。 举个直观的例子,小李上周在图书馆借了一本AI 入门,今天他把书还了,因为这本书里的案例太旧,不符合他的课程需求。 这里这本书既关联前面的一本AI 入门,明确指代对象,也关联后面的案例太旧,补充不还书的原因。 如果用 RNN 逐字计算,到这本书时,前面 AI 入门的信息早被后续内容稀释,很可能认不出两者是同一本书。 而自注意力会直接计算这本书与所有 Token 的关联,哪怕隔得远,也能精准抓住关键联系。 这种全关联的逻辑直接决定了它的复杂度是 O N 平方,N 是上下文长度。 当 N 等于1000时,要算1000×1000=1000000组 token 间关联。 当 N 等于1万时,关联组数会暴涨到1亿。 普通16GB 显存的 GPU,连存储1亿组关联的注意力矩阵都做不到,更别说计算了。 这里要纠正一个常见误解,并行计算是自注意力的实现优势,不是限制来源。 它能让 ON²的计算更快跑完,比如用 GPU 同时算100万组关联。 但没解决 n 变大后复杂度平方级暴涨的根本问题。 哪怕改成 CPU 串行计算,1亿组关联依然会算到超时,只是慢的方式不同。 二,为什么非要全关联?怕的是漏了关键信息。 有人会问,得地得这类词明明不重要,甚至省略了也不影响理解,为什么不挑重点算,非要全量关联?答案很简单,哪些词重要是事后才知道的,模型训练前无法提前定义。 比如小红喜欢养花,花的颜色是粉色,粉色让她想起小时候的裙子。 这里粉色既关联花的颜色,也关联小时候的裙子。 若只算前后10个词,万一粉色和裙子隔了11个词,就漏了关键语义。 自注意力的思路是宁滥勿缺,与其读哪些词重要,不如全算一遍。 哪怕90%的关联都是第一依赖度,比如第一和其他词的关联度接近0。
修正脚本
为什么 Transformer 处理不了超长文本?根源不是位置编码,而是全关联的取舍。 很多人读完 Attention is all you need ,总误以为 Transformer 的上下文长度限制是位置编码 Position Encoding 导致的,觉得是它记不住太长的顺序,才让模型只能处理2K、4K的 token。 但实际做过超长文本任务,比如处理整本书、长代码就会明白。 真正卡脖子的是自注意力机制,Self-Attention,全关联计算的设计逻辑。 位置编码只是补短板的工具,从来不是设限制的元凶。 要彻底理解这一点,得先搞懂,自注意力为什么非要暴力计算所有 token 关联。 这种设计的优势和代价又是什么?一,自注意力的核心不是并行,是全关联地毯式搜索。 自注意力能让 Transformer 超越 RNN,关键不是并行计算,而是它能一次性捕捉序列中所有 token 的关联,哪怕两个词隔了100个词,也不会像 RNN 那样记混或忘记。 举个直观的例子,小李上周在图书馆借了一本AI 入门,今天他把书还了,因为这本书里的案例太旧,不符合他的课程需求。 这里这本书既关联前面的一本AI 入门,明确指代对象,也关联后面的案例太旧,补充还书的原因。 如果用 RNN 逐字计算,到这本书时,前面 AI 入门的信息早被后续内容稀释,很可能认不出两者是同一本书。 而自注意力会直接计算这本书与所有 Token 的关联,哪怕隔得远,也能精准抓住关键联系。 这种全关联的逻辑直接决定了它的复杂度是 O N 平方,N 是上下文长度。 当 N 等于1000时,要算1000×1000=1000000组 token 间关联。 当 N 等于1万时,关联组数会暴涨到1亿。 普通16GB 显存的 GPU,连存储1亿组关联的注意力矩阵都做不到,更别说计算了。 这里要纠正一个常见误解,并行计算是自注意力的实现优势,不是限制来源。 它能让 ON²的计算更快跑完,比如用 GPU 同时算100万组关联。 但没解决 n 变大后复杂度平方级暴涨的根本问题。 哪怕改成 CPU 串行计算,1亿组关联依然会算到超时,只是慢的方式不同。 二,为什么非要全关联?怕的是漏了关键信息。 有人会问,得地得这类词明明不重要,甚至省略了也不影响理解,为什么不挑重点算,非要全量关联?答案很简单,哪些词重要是事后才知道的,模型训练前无法提前定义。 比如小红喜欢养花,花的颜色是粉色,粉色让她想起小时候的裙子。 这里粉色既关联花的颜色,也关联小时候的裙子。 若只算前后10个词,万一粉色和裙子隔了11个词,就漏了关键语义。 自注意力的思路是宁滥勿缺,与其挑哪些词重要,不如全算一遍。 哪怕90%的关联都是低依赖度,比如它和其他词的关联度接近0。
英文翻译
Why can't Transformers handle extremely long texts? The root cause is not positional encoding, but the choice of full attention. Many people, after reading "Attention is All You Need," mistakenly believe that the context length limit of Transformers is due to positional encoding—thinking that it cannot remember long sequences, thus restricting the model to only 2K or 4K tokens. But those who have actually worked on ultra-long text tasks, such as processing entire books or long code, will understand. The real bottleneck is the self-attention mechanism—Self-Attention—and its design logic of full pairwise computation. Positional encoding is merely a tool to compensate for shortcomings; it was never the culprit that imposes limitations. To fully grasp this, you first need to understand why self-attention insists on violently computing associations between all tokens. What are the advantages and costs of this design? First, the core of self-attention is not parallelism, but a full-attention地毯式 (exhaustive) search. What allows Transformers to surpass RNNs is not parallel computation, but the ability to capture associations between all tokens in a sequence at once. Even if two words are separated by 100 words, they won’t get mixed up or forgotten like in an RNN. For a concrete example: "Xiao Li borrowed an AI introduction book from the library last week. Today he returned the book because the cases in this book are too outdated and do not meet his course requirements." Here, "this book" both relates to the earlier "AI introduction book" (clearly referring to it) and to the later "cases are too outdated" (explaining the reason for returning it). If using an RNN to compute word by word, by the time we reach "this book," the information about "AI introduction book" from earlier would have been diluted by subsequent content, making it hard to recognize they refer to the same book. Self-attention, on the other hand, directly computes the association between "this book" and all tokens—even if far apart, it can precisely capture the key connection. This full-attention logic directly determines its complexity: O(N²), where N is the context length. When N=1000, it needs to compute 1000×1000=1,000,000 token pairs. When N=10,000, the number of pairs skyrockets to 100 million. An ordinary GPU with 16GB VRAM cannot even store the attention matrix for 100 million pairs, let alone compute it. A common misconception to correct: parallel computation is an implementation advantage of self-attention, not the source of the limitation. It allows O(N²) calculations to run faster—for example, using a GPU to simultaneously compute 1 million pairs. But it does not solve the fundamental problem of quadratic explosion as N increases. Even if changed to CPU serial computation, 100 million pairs would still time out—just in a different way of being slow. Second, why must it be full attention? The fear is missing key information. Some might ask: function words like "de," "de," "de" are clearly unimportant; even omitting them doesn’t affect understanding. Why not compute only the important ones instead of full pairwise association? The answer is simple: which words are important is only known after the fact; it cannot be predefined before model training. For example: "Xiao Hong likes growing flowers. The color of the flowers is pink. Pink reminds her of a skirt from her childhood." Here, "pink" relates both to "the color of the flowers" and to "a skirt from childhood." If we only compute associations within a 10-token window, and if "pink" and "skirt" are separated by 11 tokens, a key semantic link would be missed. Self-attention’s philosophy is "better safe than sorry"—rather than selecting which words matter, it computes everything. Even if 90% of the associations have low dependency—for instance, the association between it and other words is close to zero.
back to top