我的征尘是星辰大海。。。
The dirt and dust from my pilgrimage forms oceans of stars...
-------当记忆的篇章变得零碎,当追忆的图片变得模糊,我们只能求助于数字存储的永恒的回忆
作者:黄教授
手机视频列表
用饭局加微信看穿大模型KVcache
视频
音频
原始脚本
用饭局加微信,看穿大模型 kv cache 接触大模型的人,大多会遇到一个困惑, kv cache 到底是什么?为什么大模型推理一开始很慢?后面生成文字就变快了。 专业术语和算力公式往往把这个问题讲得晦涩难懂。 其实不用复杂的技术原理,用一个生活化的场景就能把 kv 开始的本质讲得明明白白。 我们不妨设想一个常见的场景。 多年未见的老同学组织饭局,许久没联系,大家见面第一件事就是互相添加微信,方便后续联系。 这个看似普通的社交场景和大模型 kv cache 的运行逻辑几乎完全吻合。 第一步,初次全员加微信,耗时又费力,饭局刚开始。 到场的一共有 N 位老同学,因为多年未联系,所有人都没有彼此的微信。 按照最直接的方式,大家需要两两互相添加好友。 从数学角度计算,这个操作的工作量是 n 乘括号 n 减一,括回除以二。 简单来说,每个人都要给除自己之外的 n 减一,一个人发好友申请。 所以 n 个人就要做 n x n 减一次,而你加我我加你属于重复操作,所以最终要除以二,剔除重复的步骤。 但即便如此,整个操作的复杂度依旧是 n 平方级别,到场的同学越多,互相加微信的步骤就会呈指数级增长。 现场会变得非常繁忙、耗时,所有人都要花费大量时间完成这一步。 对应到大模型推理中,这就是没有启用 kv cache 时,初始上下文的计算过程。 我们向大模型提问时,会输入一段问题文本,这段文本就是模型的初始上下文。 在推理初期,模型需要处理这段完整的上下文。 让上下文中每一个字符 token 都和其他所有字符做关联计算。 就像老同学两两加微信一样,所有 token 都要完成一次全量的交互计算。 算力消耗极大,这也是大模型一开始输出文字特别慢的根本原因。 第二步,迟到同学到场,只需增量加好友,就在全员加微信完成后。 有一位同学迟到了,他进入饭局,同样需要添加所有在场同学的微信。 但这时候的操作变得无比简单,他不需要让在场的 n 位同学再互相重新加一遍微信。 只需要自己主动和在场的每一位同学依次添加好友即可,总共只需要操作 n 次,步骤直接从 n 平方级别降到了 n 级别,也就是线性复杂度。 耗时大幅减少,几乎瞬间就能完成。 如果再来一位迟到的同学,逻辑完全一样,他只需要和当前在场的 n 加一位同学每人加一次微信就够了。 依旧是线性的操作量,轻松又快速。 这就是 kv cache 的核心作用。 大模型在完成初始上下文的全量计算后,会把所有 token 的计算结果缓存起来,这就是 kv cache 键值缓存。 后续模型每生成一个新的字符。 Token 就相当于来了一位迟到的同学,不需要再重新做所有 token 的全量关联计算,只需要让这个新 token 和缓存里已有的所有 token 做一次增量计算即可。 历史的计算结果已经被缓存保存,不用重复劳动,只需要做新增部分的简单运算,计算量大幅降低,推理速度自然就快了起来。 总结, kv cache 就是省去重复的加微信动作。 说到底, kv cache 本质上是大模型推理的算力优化神器。 初始阶段,处理用户输入的完整上下文,所有 token 两两关联计算,复杂度 n 平方,耗时久,算力消耗大。 对应全员两两加微信。 后续生成阶段,缓存历史计算结果,每新增一个 token 只做和历史 token 的增量计算,复杂度 n 速度飞快,对应迟到同学单次增量加好友。 正是因为有了 kv cache 大模型才不用在每生成一个字时,都重复做一遍全量的复杂计算,既节省了算力,又大幅提升了文字生成的速度。 让我们能流畅的看到大模型逐字输出回答。 很多看似高深的大模型技术,拆解开来都能在生活中找到对应的简单逻辑。 KV cache 就是最典型的例子。
修正脚本
用饭局加微信,看穿大模型 kv cache。接触大模型的人,大多会遇到一个困惑, kv cache 到底是什么?为什么大模型推理一开始很慢?后面生成文字就变快了。 专业术语和算力公式往往把这个问题讲得晦涩难懂。 其实不用复杂的技术原理,用一个生活化的场景就能把 kv cache 的本质讲得明明白白。 我们不妨设想一个常见的场景。 多年未见的老同学组织饭局,许久没联系,大家见面第一件事就是互相添加微信,方便后续联系。 这个看似普通的社交场景和大模型 kv cache 的运行逻辑几乎完全吻合。 第一步,初次全员加微信,耗时又费力,饭局刚开始。 到场的一共有 N 位老同学,因为多年未联系,所有人都没有彼此的微信。 按照最直接的方式,大家需要两两互相添加好友。 从数学角度计算,这个操作的工作量是 n 乘括号 n 减一,括回除以二。 简单来说,每个人都要给除自己之外的 n 减一个人发好友申请。 所以 n 个人就要做 n x n 减一次,而你加我我加你属于重复操作,所以最终要除以二,剔除重复的步骤。 但即便如此,整个操作的复杂度依旧是 n 平方级别,到场的同学越多,互相加微信的步骤就会呈指数级增长。 现场会变得非常繁忙、耗时,所有人都要花费大量时间完成这一步。 对应到大模型推理中,这就是没有启用 kv cache 时,初始上下文的计算过程。 我们向大模型提问时,会输入一段问题文本,这段文本就是模型的初始上下文。 在推理初期,模型需要处理这段完整的上下文。 让上下文中每一个字符 token 都和其他所有字符做关联计算。 就像老同学两两加微信一样,所有 token 都要完成一次全量的交互计算。 算力消耗极大,这也是大模型一开始输出文字特别慢的根本原因。 第二步,迟到同学到场,只需增量加好友,就在全员加微信完成后。 有一位同学迟到了,他进入饭局,同样需要添加所有在场同学的微信。 但这时候的操作变得无比简单,他不需要让在场的 n 位同学再互相重新加一遍微信。 只需要自己主动和在场的每一位同学依次添加好友即可,总共只需要操作 n 次,步骤直接从 n 平方级别降到了 n 级别,也就是线性复杂度。 耗时大幅减少,几乎瞬间就能完成。 如果再来一位迟到的同学,逻辑完全一样,他只需要和当前在场的 n 加一位同学每人加一次微信就够了。 依旧是线性的操作量,轻松又快速。 这就是 kv cache 的核心作用。 大模型在完成初始上下文的全量计算后,会把所有 token 的计算结果缓存起来,这就是 kv cache 键值缓存。 后续模型每生成一个新的字符token,就相当于来了一位迟到的同学,不需要再重新做所有 token 的全量关联计算,只需要让这个新 token 和缓存里已有的所有 token 做一次增量计算即可。 历史的计算结果已经被缓存保存,不用重复劳动,只需要做新增部分的简单运算,计算量大幅降低,推理速度自然就快了起来。 总结, kv cache 就是省去重复的加微信动作。 说到底, kv cache 本质上是大模型推理的算力优化神器。 初始阶段,处理用户输入的完整上下文,所有 token 两两关联计算,复杂度 n 平方,耗时久,算力消耗大。 对应全员两两加微信。 后续生成阶段,缓存历史计算结果,每新增一个 token 只做和历史 token 的增量计算,复杂度 n,速度飞快,对应迟到同学单次增量加好友。 正是因为有了 kv cache,大模型才不用在每生成一个字时,都重复做一遍全量的复杂计算,既节省了算力,又大幅提升了文字生成的速度。 让我们能流畅地看到大模型逐字输出回答。 很多看似高深的大模型技术,拆解开来都能在生活中找到对应的简单逻辑。 KV cache 就是最典型的例子。
英文翻译
Use a dinner party and WeChat friend requests to understand KV cache in large models. Many people who work with large models encounter a common confusion: what exactly is KV cache? Why does inference start slowly and then speed up when generating text? Technical jargon and computational formulas often make this concept obscure and hard to grasp. Actually, without complex technical principles, we can explain the essence of KV cache clearly using a relatable everyday scenario. Let’s imagine a common situation. A group of old classmates who haven’t seen each other for years organizes a dinner party. Since they’ve lost touch, the first thing everyone does is exchange WeChat contacts to keep in touch later. This seemingly ordinary social scenario almost perfectly mirrors the operational logic of KV cache in large models. Step one: Adding everyone for the first time—time-consuming and laborious, just as the dinner party starts. There are N old classmates present. Because they haven’t connected for years, none of them have each other’s WeChat contacts. The most straightforward way is for everyone to add each other as friends pairwise. Mathematically, the workload of this operation is n * (n - 1) / 2. In simple terms, each person must send friend requests to the other n - 1 people. So n people would perform n * (n - 1) operations, but since “you add me and I add you” is a duplicate action, we divide by 2 to remove redundancy. Even so, the overall complexity remains at the n² level. The more classmates present, the more steps needed to add WeChat contacts, growing exponentially. The scene becomes very busy and time-consuming; everyone spends a lot of time on this step. Corresponding to large model inference, this is the computation process for the initial context when KV cache is not enabled. When we ask a question to a large model, we input a problem text—this is the model’s initial context. During the early stage of inference, the model needs to process this complete context, calculating the associations between every token (character) and all other tokens. Just like classmates adding each other pairwise, all tokens must undergo a full interaction computation. This requires enormous computational power—this is the fundamental reason why large models output text very slowly at the beginning. Step two: A latecomer arrives—just incremental friend-adding. After everyone has added each other, one classmate arrives late. He enters the dinner party and likewise needs to add the WeChat contacts of all classmates present. But now the operation becomes incredibly simple. He doesn’t need to make all the n classmates re-add each other. He simply takes the initiative to add each classmate one by one. The total operation is just n times—the complexity drops from n² to n (linear complexity). Time consumption is greatly reduced, and it can be completed almost instantly. If another latecomer arrives, the logic is exactly the same: he only needs to add each of the current n+1 classmates once. Again, it’s a linear amount of work—easy and fast. This is the core role of KV cache. After the large model completes the full computation of the initial context, it caches the computed results of all tokens—this is the KV cache (key-value cache). When the model later generates a new token, it’s like a latecomer arriving. There’s no need to redo the full association calculation with all tokens. Instead, only an incremental computation between the new token and the already cached tokens is needed. Historical computation results are already cached and don’t need to be repeated. Only the simple increment for the new part is required. The computational load is drastically reduced, and inference speed naturally increases. In summary, KV cache eliminates redundant friend-adding actions. Ultimately, KV cache is essentially a computational optimization tool for large model inference. At the initial stage: processing the complete user input context, all tokens undergo pairwise association computation—complexity n², slow, high computational cost. Corresponds to the pairwise friend-adding for all classmates. During the subsequent generation stage: caching historical computation results. Each new token only does incremental computation with historical tokens—complexity n, very fast. Corresponds to the single incremental friend-adding for a latecomer. Thanks to KV cache, large models don’t have to repeat the full complex computation every time they generate a character. This saves computational power and greatly speeds up text generation, allowing us to see the model output text fluently, word by word. Many seemingly profound large model technologies, when broken down, can find simple corresponding logic in daily life. KV cache is the most typical example.
back to top