Transformer Inference and FlashDecoding Explained
A Juejin AI tutorial walks through autoregressive transformer generation from prompt prefill and KV caching to token-by-token decoding, attention, normalization, residual paths, feed-forward networks and LM-head sampling. It also corrects several misconceptions, including using K rather than V for attention scoring and combining partitioned K/V results through weighted reduction rather than concatenation. FlashDecoding is presented as a decode-time attention optimization that preserves the attention output, though the report supplies no independent performance benchmark.
The source provides a practical, correction-focused explanation of the inference path and places FlashDecoding within that pipeline without introducing a new model, implementation or measured result.
2026-11-12 问答整理。不绑定具体框架,只讲模型本身怎么从「输入」走到「输出一个字」。 主线按单头讲(先把多头放一边),真实多头差异放在最后一节。 记号约定:不用字母
O(期间专门确认过它是数字 0 还是字母 O —— 答案是大写字母 O,取自 Output,不是数字 0),统一写「输出向量」。Q / K / V保留。 为了数字能一路跟到底,用一个简化模型:向量宽度 128、共 4 层、词表 5 万。真实 Qwen3-0.6B 是 1024 宽 / 128 每头 / 28 层 / 词表 151936,流程完全一样。
同期我还把这套内容整理成了一篇更体系化的教程(含 9 张图解和完整纠错清单),可配合食用:《FlashDecoding 零基础学习总结:从 attention 三段式到 split-KV 合并》
全景(一段话先把路铺好)
把 prompt 的所有 token 一次性加载进 transformer → 一次算出全部 QKV、把 KV 存进 block(KV cache)→ 产出第一个字 → 进入 decode,一个字一个字往外吐 → 每一轮:新字查 embedding 表得到输入向量 → 过 4 层(每层都做一次 attention + FFN + 两次残差)→ 最后一层得到最终状态向量 → 跟词表矩阵相乘得到 5 万个分数 → softmax 变成概率 → top-k 抽样或取最高分 → 得到 1 个 token id → 输出这个字 → 这个 id 再查 embedding 表,进入下一轮 decode。