核心内容摘要
保障安全与效率:详解沼气工程核心设备——双膜储气柜
Q Cache: Visual Attention is Valuable in Less than Half of Decode Layers for Multimodal Large Language ModelAuthors:Jiedong Zhuang, Lu Lu, Ming Dai, Rui Hu, Jian Chen, Qiang Liu, Haoji HuDeep-Dive Summary:这份学术论文介绍了一种名为Q Cache的新方法旨在解决多模态大语言模型MLLMs中因视觉 token 过多导致的推理成本高昂、KV cache 占用大等问题。
摘要 (Abstract)多模态大语言模型MLLMs在推理时面临视觉 token 激增带来的计算负担和内存瓶颈。
现有的 token 剪枝技术虽能减少 token 数量但往往会破坏 KV cache 的完整性导致长文本生成任务失败。
本文通过深入研究发现超过一半的解码层decode layers在语义上具有相似的注意力模式。
基于此作者提出了Lazy Attention机制允许相邻层共享注意力模式并设计了轻量级的Q CacheQuery 缓存来实现跨层的 Query 复用。
实验表明该方法在仅损失约1 % 1\%1%性能的情况下可减少超过35 % 35\%35%的 KV cache 使用量并提升
5
1.
5
5倍的吞吐量。
引言 (Introduction)MLLMs 虽在视觉问答和推理任务中表现出色但视觉编码器生成的海量 token 使得 Transformer 的注意力计算复杂度呈平方级增长并带来了沉重的 KV cache 存储压力。
图 1不同解码层中 K Cache 的简要对比。
本文提出的 Q Cache 显著减少了内存占用且比其他方法更灵活兼容 Flash Attention。
现有的 token 剪枝方法通常需要实时访问注意力矩阵与 Flash Attention 不兼容且在预填充阶段丢弃 token 会影响多轮对话的稳定性。
作者通过 Jensen-Shannon (JS) 散度分析发现见图 2相邻层的注意力模式高度相似这暗示了层级layer-wise优化的潜力。
图 2相邻层之间注意力得分的 JS 散度。
灰色虚线框显示散度接近 0表明相邻解码层共享高度相似的注意力模式。
方法 (Method)
基础回顾 (Preliminary)在预填充阶段第l ll层的自注意力计算公式为Q l X l W Q l ; K l X l W K l ; V l X l W V l , A l S o f t m a x ( Q l ( K l ) ⊤ M D ) ; O l A l V l , ( 1 ) \begin{array}{rl} {\mathbf{Q}^l \mathbf{X}^l\mathbf{W}_\mathbf{Q}^l;\quad \mathbf{K}^l \mathbf{X}^l\mathbf{W}_\mathbf{K}^l;\quad \mathbf{V}^l \mathbf{X}^l\mathbf{W}_\mathbf{V}^l,}\\ {\mathbf{A}^l \mathrm{Softmax}\left(\frac{\mathbf{Q}^l(\mathbf{K}^l)^\top \mathbf{M}}{\sqrt{D}}\right);\quad \mathbf{O}^l \mathbf{A}^l\mathbf{V}^l,} \end{array} \quad (
QlXlWQl;KlXlWKl;VlXlWVl,AlSoftmax(DQl(Kl)⊤M);OlAlVl,(
在解码阶段则需从 KV cache 加载之前的键值对。
Lazy Attention基于相邻层注意力相似度的分析作者将相似层聚合为Lazy Block (LB)。
每个 LB 包含一个标准的解码层和若干个 Lazy Attention 层。
Lazy Attention 分为两种模式Global Lazy Attention (GLA)所有 token文本视觉共享注意力模式。
Visual Lazy Attention (VLA)仅视觉 token 共享注意力模式。
图 3整体框架图。
左侧为优化的 LLM 组件由标准层和 Lazy Blocks 组成右侧展示了 Lazy Block 内部结构通过复用首层的 Q 和 K 来消除冗余计算。
Q CacheQ Cache 是一个动态共享缓存Q Q l c \mathcal{Q} \mathbf{Q}^{l_{c}}QQlc。
在GLA模式下Block 内的所有层共享Q QQ和K KK完全省去了这些层的线性投影计算和K KK缓存存储。
在VLA模式下仅共享视觉部分的Q QQ和K KK保留文本部分的映射操作。
由于视觉 token 数量远超文本这种细粒度共享依然能大幅降低成本。
图 4预填充和解码阶段 GLA 与 VLA 模式下 Q, K, V 缓存流向的可视化。
红框标出了 GLA 和 VLA 的主要差异。
效率分析 (Efficiency Analysis)KV CacheGLA 的整体节省率为n 2 N \frac{n}{2N}2Nnn nn为 Lazy 层数N NN为总层数VLA 的节省率与之接近因为视觉 token 占绝大多数。
FLOPs 与延迟通过省略线性投影计算GLA 和 VLA 都能显著降低计算量。
实验 (Experiments)对比结果在 LLaVA 系列模型上VLA 模式在 GQA、TextVQA 和图像描述COCO, NoCaps等任务中表现稳定优于传统的 token 剪枝方法有效避免了因 token 丢失导致的语境缺失。
兼容性VLA 可以与现有的 token 剪枝方法如 FastV结合进一步提升效率证明了其正交性。
性能提升在 A100 GPU 上测试显示该方法在 2048 输入长度下将最大 batch size 从 21 提升至 32并实现了
5 ×
5 \times
5×的吞吐量增长。
图 5GLA 和 VLA 在参数量、KV cache 占用、最大 Batch Size 和吞吐量方面的提升效果对比。
健全性檢查 (Sanity Check)隨機層策略的性能失效。
我們透過注意力相似度構建「懶惰塊」lazy blocks來對方法進行有效性測試。
具體而言我們透過隨機選擇來分組相鄰的解碼層以構建懶惰塊。
為了公平比較兩種方法中的懶惰塊和懶惰層的數量保持一致。
實驗結果顯示利用注意力相似度指標構建懶惰塊的方法在 LLaVA-v
1.
B/13B 的六個數據集上均顯著優於隨機選擇方法。
如圖 6 所示實驗證明高注意力相似度是 VLAVisual Lazy Attention能夠保持模型性能的主要因素。
表 1現有 MLLM Token 級優化方法在各個數據集上的比較。
SQA 指 ScienceQA-IMG 子集MMB 指 MMBench 的英文子集VQA, TextVQA, VizWiz 分別是 VQA2 ^
TextVQA 和 VizWiz 的驗證子集。
COCO 和 Nocaps 代表 COCO2017 caption 和 Nocaps 的驗證集。
Flickr30K 是其原始數據集的測試集。
GQA、AI2D、MMMU 和 POPE 為完整數據集。
最佳結果以粗體標出。
MethodsGQA↑VQA↑TextVQA↑VizWiz↑AI2D↑SQA↑MMMU↑MMB↑POPE↑COCO↑Nocaps↑Flickr30K↑LLaVA-v
1.
B
61.
9476.
6446.
1054.
3855.
2569.
5135.
3064.
0985.
13110.
43105.
5
89FastV (ECCV
24)
60.
3275.
8645.
5254.
4755.
2468.
7235.
0063.
8184.
97110.
48105.
3
67VTW (AAAI
25)
55.
1466.
2816.
0851.
0055.
3069.
3036.
1063.
9285.
1067.
2295.
7
69HiRED (AAAI
25)
59.
4275.
5445.
6053.
5558.
1268.
1236.
1063.
5784.
99102.
27100.
1
86PrurMerge (ICCV
25)
57.
3972.
7739.
5554.
5054.
1869.
3636.
3061.
4384.
09102.
9699.
6
17SparseVLM (ICML
25)
57.
2473.
2142.
6554.
3954.
9269.
0135.
1064.
0084.
88103.
6998.
8
89GLA (Ours)
61.
9676.
0642.
2153.
4352.
8966.
4630.
3060.
6284.
02105.
75100.
3
74VLA (Ours)
61.
9376.
3845.
7054.
5854.
9869.
4336.
8064.
8885.
13110.
51105.
6
36LLaVA-v
1.
B
63.
2878.
1948.
6956.
6159.
2672.
7834.
9068.
8185.
66115.
55109.
3
56FastV (ECCV
24)
62.
6277.
6648.
3256.
4758.
8472.
8833.
9068.
3085.
26115.
51108.
8
48VTW (AAAI
25)
60.
6175.
3434.
2555.
1159.
2272.
6834.
8068.
4185.
71101.
9796.
3
80HiRED (AAAI
25)
59.
6976.
6347.
9255.
1055.
7372.
3434.
9067.
1885.
58107.
80104.
4
00PrurMerge (ICCV
25)
58.
1673.
8941.
5755.
8157.
9373.
0734.
8065.
2185.
32105.
11101.
0
13SparseVLM (ICML
25)
58.
6974.
9245.
3256.
4657.
1672.
7734.
9067.
5385.
48109.
52103.
9
33GLA (Ours)
62.
9877.
7744.
1455.
9056.
4270.
0631.
7065.
1584.
53110.
84103.
6
10VLA (Ours)
63.
3178.
0347.
8357.
4259.
1972.
5534.
9068.
6185.
73115.
44108.
5
54LLaVA-NEXT-7B
64.
2479.
8964.
8660.
6665.
2570.
1535.
4067.
1086.
4099.
8788.
3
47FastV (ECCV
24)
63.
8779.
6363.
7960.
1965.
0269.
1135.
1066.
3185.
9898.
5486.
4
18VTW (AAAI
25)
55.
8468.
4518.
8855.
0765.
2169.
9135.
2066.
9286.
2182.
5043.
7
81HiRED (AAAI
25)
61.
8278.
5163.
6559.
7162.
7768.
7435.
4066.
9686.
0193.
7984.
2
41PrurMerge (ICCV
25)
60.
4576.
7357.
6560.
3563.
8069.
0035.
5064.
6285.
8794.
1183.
7
29SparseVLM (ICML
25)
59.
8676.
5060.
0060.
4065.
2169.
7835.
1066.
4485.
5394.
7182.
8
22GLA (Ours)
63.
5579.
2760.
1158.
7962.
2366.
4032.
3064.
5685.
0094.
4881.
9
81VLA (Ours)
64.
1279.
6563.
6660.
2464.
3769.
5535.
8067.
0286.
3099.
1786.
3
42LLaVA-NEXT-13B
65.
4380.
9367.
1263.
5670.
2773.
5736.
1069.
3386.
10101.
9388.
1
70FastV (ECCV
24)
64.
7980.
7266.
0063.
2569.
9572.
9835.
9068.
4785.
90101.
2987.
8
13VTW (AAAI
25)
61.
4777.
0143.
2758.
5070.
1272.
4735.
3068.
9486.
0186.
8643.
2
52HiRED (AAAI
25)
60.
7278.
2265.
9662.
7667.
0872.
1635.
9068.
0485.
8396.
0985.
2
41PrurMerge (ICCV
25)
59.
6675.
5859.
3062.
6668.
7073.
4135.
8065.
8085.
5593.
7281.
5
93SparseVLM (ICML
25)
60.
0176.
9963.
7263.
4068.
0973.
2336.
0068.
5185.
7797.
6184.
8
18GLA (Ours)
64.
6179.
3062.
4962.
5667.
2471.
1134.
8065.
8085.
0396.
8383.
2
20VLA (Ours)
65.
3380.
8565.
2663.
5169.
1772.
3636.
1069.
0186.
10101.
9187.
7
29表 2結合本方法與 Token 剪枝方法 FastV 在 LLaVA-v
1.
B 上的測試。
MethodFLOPs (T)Latency (ms)Params (B)KV Cache (GB)Vanilla
30.
88142.
606.
7
01GLA
1
17 ±
4
9%
6
79 ±
5
2%
98 ±
1
5%
35 ±
6
0%VLA
1
49 ±
4
1%
6
21 ±
5
9%
6.
7
36 ±
6
0%表 3結合 FastV 在預填充Prefilling階段對 LLaVA-NEXT-7B 的效率測試。
LLaVA-v
1.
B/13B
1
17 ±
4
9%
6
79 ±
5
2%
98 ±
1
5%
35 ±
6
0%VLA
1
49 ±
4
1%
6
21 ±
5
9%
6.
7
36 ±
6
0%圖 6隨機選擇與注意力相似度指標在部分數據集上的對比。
分析 (Analysis)為什麼 VLA 優於 GLA。
為了揭示 VLA 性能優於 GLA 的原因我們對解碼層的注意力進行了更細緻的分析。
具體而言我們將注意力分解為視覺注意力和文本注意力並視覺化它們在各層間的分佈。
如圖 7 所示我們的發現如下發現 1一些缺乏實際意義的文本 token 獲得了大部分注意力分數且這些位置在所有層中保持一致。
這解釋了圖 2 中大多數相鄰層之間 JS 散度較低的原因——這些異常高注意力 token 的存在主導了計算結果。
圖 7文本和視覺 token 的跨層視覺化。
為了更清晰的呈現視覺 token 採用了平均池化。
具有高注意力相似度的相鄰層用虛線框標出。
發現 2具有實際意義的文本指令 token的注意力在各層間表現出較大的差異。
結合發現 1我們推斷 JS 散度在某種程度上具有誤導性導致某些層錯誤地重用了相似度較低的文本注意力。
我們認為這是 GLA 性能不佳的根本原因。
發現 3視覺 token 的注意力分數顯著低於文本 token但表現出更高的跨層相似性。
這解釋了為什麼 VLA 能獲得更穩定的性能因為 VLA 僅從前一層繼承視覺組件。
由於視覺注意力跨層相似度更高且在總注意力中佔比較小因此對視覺 token 應用懶惰注意力對輸出的影響較小。
LLM 中的注意力相似性。
我們對純文本 LLMChiang et al. 2023; Grattafiori et al. 2024; Yang et al. 2024; Dong et al. 2024進行了注意力分析以幫助理解 MLLM 中的上述現象。
從圖 8數值由ln 2 − S ( l m , l n ) \ln 2 - \mathbf{S}(l_m, l_n)ln2−S(lm,ln)計算得出可以看出懶惰塊在不同的 LLM 中普遍存在但分佈不同。
這表明層間注意力冗餘是一個普遍現象。
值得注意的是Vicuna 展現出的模式與 LLaVA 幾乎完全一致見圖 2這進一步證實了我們的觀點MLLM 的懶惰特性繼承自其基礎 LLM。
懶惰指令微調 (Lazy Instruction Tuning)。
基於上述發現我們大膽假設可以直接在未經視覺-文本微調的 LLM 上實施我們的方法。
在 LLaVA 模型微調的第二階段指令微調階段我們在 Vicuna 模型上引入了 Q cache 和懶惰注意力。
如表 4 所示直接微調原始 Vicuna 權重的結果與默認方法相當這進一步驗證了我們對 LLM 的觀察。
這一結果啟發我們在指令微調階段採用 Q cache 和懶惰注意力方法從而避免後訓練post-training帶來的開銷。
圖 8LLM 中所有層的注意力分數相似度。
數值越大表示相似度越高。
它表明相鄰層具有更相似的注意力模式。
表 4LLaVA-v
1.
B 內 LLM 組件權重的比較。
“i” 表示在預訓練的 LLaVA 中對 Vicuna 應用我們的方法進行後訓練另一行表示直接在 Vicuna 上開發我們的方法進行 LLaVA 指令微調。
LLMTextVQA↑AI2D↑SQA↑MMMU↑MMB↑COCO↑Vicuna ↑
45.
7054.
9869.
4336.
8064.
8
51Vicuna ↑
45.
1254.
9569.
0637.
0065.
3
03結論 (Conclusion)本文提出了一種新型的跨層共享緩存cross-layer shared cache和一種內存友好型注意力機制。
我們首先深入研究了 MLLM 中的注意力特性發現大多數解碼層中的視覺注意力是冗餘的。
受此啟發我們提出了 Q cache 和懶惰注意力lazy attention在不損害準確性的情況下顯著減少了 KV cache 的內存佔用。
此外我們對 MLLM 中的注意力冗餘現象進行了深入分析認為這種「懶惰」特性繼承自預訓練的 LLM。
我們希望這項工作能為設計新穎的緩存和指令微調技術提供靈感激發研究社群更多地致力於理解 MLLM 所展現的有趣現象。
Original Abstract:Multimodal large language models (MLLMs) are plagued by exorbitant inference costs attributable to the profusion of visual tokens within the vision encoder. The redundant visual tokens engenders a substantial computational load and key-value (KV) cache footprint bottleneck. Existing approaches focus on token-wise optimization, leveraging diverse intricate token pruning techniques to eliminate non-crucial visual tokens. Nevertheless, these methods often unavoidably undermine the integrity of the KV cache, resulting in failures in long-text generation tasks. To this end, we conduct an in-depth investigation towards the attention mechanism of the model from a new perspective, and discern that attention within more than half of all decode layers are semantic similar. Upon this finding, we contend that the attention in certain layers can be streamlined by inheriting the attention from their preceding layers. Consequently, we propose Lazy Attention, an efficient attention mechanism that enables cross-layer sharing of similar attention patterns. It ingeniously reduces layer-wise redundant computation in attention. In Lazy Attention, we develop a novel layer-shared cache, Q Cache, tailored for MLLMs, which facilitates the reuse of queries across adjacent layers. In particular, Q Cache is lightweight and fully compatible with existing inference frameworks, including Flash Attention and KV cache. Additionally, our method is highly flexible as it is orthogonal to existing token-wise techniques and can be deployed independently or combined with token pruning approaches. Empirical evaluations on multiple benchmarks demonstrate that our method can reduce KV cache usage by over 35% and achieve
5x throughput improvement, while sacrificing only approximately 1% of performance on various MLLMs. Compared with SOTA token-wise methods, our technique achieves superior accuracy preservation.PDF Link:
2