Attention
从抽象层面来理解注意力的 QKV 计算,实际是为原始 token 序列生成了一个新的信息表示。这个过程分为两个步骤:
- QK 计算 token 间的相关性,这种相关性会通过注意力分数来体现
- 基于相关性对 V 中的数据求解加权平均
具体而言,从矩阵的角度来看,Q 的每一行是一个 token 的原始信息,与 $K^T$ 的计算结果则表征了原始的一个 token 对于其他 token 的关注程度(此时如果 K 中行序发生改变,则 $K^T$ 中列序将发生改变,那么 $QK^T$ 的计算结果中一个 token 对其他 token 的关注程度,这个“其他 token”的顺序是发生了变化的);考虑与 V 的计算,由于左乘矩阵每一行依然对应到原始 token,所以与 V 的计算结果每一行依然是原始 token 的信息(如果 K 中行序发生变化,那么 V 的行序也需要对应变化,即只需要保证 $QK^T$ 的列能够正确作为对应 token 信息的权重)
Bottleneck of Attention in Different Tasks
这个分类并不是完整的,可能只是这几类是 Attention 机制比较场景的应用场景。
| 任务/阶段 | 瓶颈类型 |
|---|---|
| Non-Autoregressive Models | Compute Bound |
| LLM Training | Compute Bound |
| LLM Inference | |
| 1. Prefilling 预填充阶段 | Compute Bound |
| 2. Decoding 解码阶段 | Memory Bound |
不同的计算类型决定在不同阶段的优化方向
Classification of Attention
| Major Category | Subcategory | Core Goal | Key Technique | Core Advantage | Main Limitation | Typical Methods |
|---|---|---|---|---|---|---|
| 1. Hardware-efficient Attention | Prefilling 阶段 | 提升计算吞吐量(适配 Compute-bound 场景) | 块划分(tiling)、核融合、低比特量化(INT4/8、FP8)、异步 TensorCore 调度 | 不改变注意力公式,性能无损;适配 GPU 特性 | 依赖硬件架构(多针对 NVIDIA GPU);开发门槛高 | FlashAttention1/2/3、SageAttention 系列 |
| Decoding 阶段 | 加速 KV 缓存 I/O(适配 Memory-bound 场景) | KV 缓存拆分 / 重分配(分页、紧凑存储)、低比特量化、动态调度 | 降低内存碎片,提升 SM 利用率;适配长序列解码 | 兼容性有限(需定制内核) | PagedAttention、FlashDecoding、KVQuant、FlashInfer | |
| 2. Compact Attention | 注意力头共享 / 分组类 | 压缩 KV 缓存存储(减少独立 KV 头) | 全共享(MQA)、分组共享(GQA)、张量积生成(TPA) | 实现简单,计算逻辑无损;适配 LLM 推理 | 过度共享可能降低模型表达能力 | MQA、GQA、TPA |
| 低秩分解 / 特征压缩类 | 压缩 KV 特征维度(存储降维 + 计算升维) | 低秩投影(MLA)、矩阵因子化(MFA)、时空稀疏压缩 | 内存压缩比高,保留多头核心优势 | 升维过程可能引入轻微性能损失 | MLA、MFA | |
| 3. Sparse Attention | Pattern-based | 跳过预定义非关键计算(无需训练) | 固定稀疏掩码(滑动窗口、注意力汇(Attention Sink)、时空分块) | 训练无关,即插即用;适配特定任务(视频 / LLM) | 掩码固定,灵活性不足;高稀疏度易丢信息 | StreamingLLM、DiTFastAttn、STA、NeighborAttn |
| Dynamic sparse | 自适应跳过非关键计算(部分需训练) | Runtime 掩码生成(Top-K 筛选、PCA 降维、聚类、门控预测) | 灵活性高,适配复杂场景;精度损失可控 | 部分需训练 / 微调;动态筛选有额外开销 | SpargeAttn、H2O、SeerAttention、VSA、NSA | |
| 4. Linear Attention | Naive | 复杂度降为 O(N)(无门控) | 核函数替换 Softmax、块-wise 循环累加 | 计算 / 内存效率极高;适配非自回归任务 | 长序列信息保留差;表达能力有限 | Linear Transformer、Lightning Attention |
| 带遗忘门(Forget Gate) | 优化长序列信息保留(控制历史依赖) | 固定 / 输入依赖遗忘门(RetNet、GLA)、通道-wise 衰减(RWKV 系列) | 平衡效率与长序列建模;支持自回归推理 | 门控设计复杂;部分需调参优化 | RetNet、GLA、RWKV4/5/6/7 | |
| 带遗忘 + 选择门(Dual Gates) | 精准控制信息取舍(历史 + 当前) | Delta 规则(DeltaNet)、时变状态空间(Mamba)、门控融合 | 表达能力接近标准注意力;效率无损失 | 实现复杂;硬件适配要求高 | DeltaNet、Mamba、Mamba2、gDeltaNet | |
| 测试时训练型(TTT) | 动态优化隐藏状态(适配场景) | 隐藏状态作为“快权重”,梯度下降更新(线性 / MLP 子网络) | 长序列建模精度高;适配多任务 | 块级优化复杂;部分非线性结构难并行 | TTT-Linear/MLP、Titans、LaCT |
GAT
图注意力网络中多头机制的目的在于让每个头专注于学习不同类型的关系特征,因此只会划分节点的特征数据,而不会对节点或边进行划分,即不划分拓扑结构。
Flash Attnetion
都是模版特化
| |
- mainloop_fwd_sm90_tma_gmma_ws.hpp
| |
- utils.h
| |
The number of executions of function cute::gemm() is determined by std::min(kNumKIters, kMaxKIters).
| |
- gemm.hpp(TiledMMA)
| |
3.1 mainloop_fwd_sm90_tma_gmma_ws.hpp
| |
3.2 mma_sm90.hpp
| |
Considering the simplicity of the description, we don’t copy the wholl content of ss_op_selector() function. The principle of selection is based on the following process:
- data type of accumulator(F16, F32, S32)
- data type of input A and B
- because the apis fix the value of M and K, so it only need to judge is the relationship between the value of N and some preset values, the judgement follows the following order: 256, 248, 240, 232, 224, 216, 208, 200, 192, 184, 176, 168, 160, 152, 144, 136, 128, 120, 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, which are the multiples of 8
Actually, this step will use the value of N. But it isn’t a simple process just only select the mma api according to the value of N.
The actual value of N which will be used is the kBlockN, that is generated by the following code:
| |
At the same time, we can consider the generation method of the actual value of M and K which will be used:
| |
From the above analysis, we can confirm that the kHeadDim is decided by the environmental variable which is used to decide which macro defination will be enabled.
However, the kBlockM and kBlockN is decided by the kBlockMN_RS_IntraWGOverlap variable that is generated by the tile_size_fwd_sm90 function.
| |
The value of kBlockM and kBlockN is decided by the value of headdim and headdim_v, the dimension of actual input data of k and v.
Based on the information currently available, we couldn’t use the m64n16k16 shape api.
- mma_atom.hpp(MMA_Atom)
| |
- mma_traits_sm90_gmma.hpp(MMA_Traits)
| |
- mma_sm90_gmma.hpp(MMAOperation)
| |
示例流程
Sparse Attention
Pattern-based
Dynamic sparse
KV Cache
Cache 的两个核心:不变、有用
- KV Cache 为什么不变
Causal Mask => 历史 KV 不变
目前还没有从数据的角度理解带与不带 mask 时,数据的变化方式,但是考虑到理解 kv cache 并不对解决目前的主要疑惑有什么贡献,所以暂时搁置一下
- KV Cache 为什么有用
自回归推理 => 历史 KV 有用
KV Cache 的开销要从两个方面来理解:I/O 开销;存储开销
“固定模式稀疏注意力"和"动态稀疏注意力"因为都涉及到 mask,所以 I/O 开销是都会减少的。
固定模式稀疏注意力因为 mask 固定,所以只需要存储掩码选中的 KV 数据,存储开销是会降低的;动态稀疏注意力如果 mask 是来源于 q,k,v 的相关性,那就需要保存完整的 KV 数据,存储开销是不会降低的。




