DuoAttention: EFFICIENT LONG-CONTEXT LLM INFERENCE WITH RETRIEVAL AND STREAMING HEADS 翻译

DuoAttention:高效处理长上下文推理的 AI 框架,让 LLMs 如虎添翼! DuoAttention 是一种新型框架,旨在提高大型语言模型(LLMs)处理长上下文时的推理效率。通过区分“检索头”和“流式头”两种注意力头,优化模型的内存使用和计算速度。 阅读详情

Doc2X | 高效 PDF 转换助手
Doc2X 提供精准的 PDF转Docx、PDF转Markdown、PDF转HTML 功能,支持 多栏识别、表格解析、公式编辑,满足多样化文档处理需求。
Doc2X | Efficient PDF Conversion Assistant
Doc2X offers precise PDF to Docx, Markdown, and HTML conversion, supporting multi-column recognition, table parsing, and formula editing for versatile document handling.
👉 了解 Doc2X 的独特功能 | Explore Doc2X Features

原文链接:https://arxiv.org/pdf/2410.10819

DuoAttention: EFFICIENT LONG-CONTEXT LLM INFERENCE WITH RETRIEVAL AND STREAMING HEADS

DuoAttention: 带有检索和流式头的高效长上下文LLM推理

Guangxuan Xiao 1 ∗    {}^{1 * }\; 1∗ Jiaming Tang 1    {}^{1}\; 1 Jingwei Zuo 2    {}^{2}\; 2 Junxian Guo 1 , 3 {}^{1,3} 1,3

肖广轩 1 ∗    {}^{1 * }\; 1∗ 唐嘉明 1    {}^{1}\; 1 左静伟 2    {}^{2}\; 2 郭俊贤 1 , 3 {}^{1,3} 1,3

Shang Yang 1    {}^{1}\; 1 Haotian Tang 1    {}^{1}\; 1 Yao Fu 4    {}^{4}\; 4 Song Han 1 , 5 {}^{1,5} 1,5

杨尚 1    {}^{1}\; 1 唐浩天 1    {}^{1}\; 1 付尧 4    {}^{4}\; 4 韩松 1 , 5 {}^{1,5} 1,5

1 \frac{1}{} 1​ MIT 2 {}^{2} 2 Tsinghua University 3 {}^{3} 3 SJTU 4 {}^{4} 4 University of Edinburgh 5 {}^{5} 5 NVIDIA

1 \frac{1}{} 1​ 麻省理工学院 2 {}^{2} 2 清华大学 3 {}^{3} 3 上海交通大学 4 {}^{4} 4 爱丁堡大学 5 {}^{5} 5 英伟达

https://github.com/mit-han-lab/duo-attention

ABSTRACT

摘要

Deploying long-context large language models (LLMs) is essential but poses significant computational and memory challenges. Caching all Key and Value (KV) states across all attention heads consumes substantial memory. Existing KV cache pruning methods either damage the long-context capabilities of LLMs or offer only limited efficiency improvements. In this paper, we identify that only a fraction of attention heads, a.k.a, Retrieval Heads, are critical for processing long contexts and require full attention across all tokens. In contrast, all other heads, which primarily focus on recent tokens and attention sinks-referred to as Streaming Heads-do not require full attention. Based on this insight, we introduce DuoAttention, a framework that only applies a full KV cache to retrieval heads while using a light-weight, constant-length KV cache for streaming heads, which reduces both LLM’s decoding and pre-filling memory and latency without compromising its long-context abilities. DuoAttention uses a lightweight, optimization-based algorithm with synthetic data to identify retrieval heads accurately. Our method significantly reduces long-context inference memory by up to 2.55 × {2.55} \times 2.55× for MHA and 1.67 × {1.67} \times 1.67× for GQA models while speeding up decoding by up to 2.18 × {2.18} \times 2.18× and 1.50 × {1.50} \times 1.50× and accelerating pre-filling by up to 1.73 × {1.73} \times 1.73× and 1.63 × {1.63} \times 1.63× for MHA and GQA models, respectively, with minimal accuracy loss compared to full attention. Notably, combined with quantization, DuoAttention enables Llama-3-8B decoding with 3.3 million context length on a single A100 GPU. Code is provided in the link

部署长上下文大语言模型(LLMs)是必要的,但面临着显著的计算和内存挑战。缓存所有注意力头中的键和值(KV)状态会消耗大量内存。现有的KV缓存剪枝方法要么损害LLMs的长上下文能力,要么只能提供有限的效率提升。在本文中,我们发现只有一小部分注意力头,即检索头,对于处理长上下文是关键的,并且需要对所有标记进行全注意力处理。相比之下,其他主要关注最近标记和注意力汇聚的头,即流式头,不需要全注意力。基于这一洞察,我们提出了DuoAttention框架,该框架仅对检索头应用全KV缓存,而对流式头使用轻量级的固定长度KV缓存,从而在不损害其长上下文能力的情况下,减少LLM的解码和预填充内存及延迟。DuoAttention使用一种轻量级的基于优化的算法,通过合成数据准确识别检索头。我们的方法显著减少了长上下文推理内存,MHA模型减少了高达 2.55 × {2.55} \times 2.55×,GQA模型减少了高达 1.67 × {1.67} \times 1.67×,同时解码速度分别提高了高达 2.18 × {2.18} \times 2.18×和 1.50 × {1.50} \times 1.50×,预填充速度分别提高了高达 1.73 × {1.73} \times 1.73×和 1.63 × {1.63} \times 1.63×,与全注意力相比,精度损失最小。值得注意的是,结合量化技术,DuoAttention使得Llama-3-8B在单个A100 GPU上能够进行330万上下文长度的解码。代码在链接中提供。

1 INTRODUCTION

1 引言

Large language models (LLMs) (Touvron et al. 2023a b; OpenAI, 2023; Black et al., 2022) are at the forefront of the AI revolution, powering advanced applications such as multi-round dialogues (Schulman et al. 2022; Taori et al., 2023; Chiang et al., 2023), long document summarization (Goyal & Durrett, 2020, Zhang et al., 2023a), and tasks involving mixed modalities like visual and video understanding (Liu et al., 2023b; Lin et al., 2023). These applications often require processing extensive numbers of contextual tokens; for instance, summarizing the entire Harry Potter series could involve analyzing approximately one million tokens. The challenge intensifies with visual language models (VLMs),where a single 224 × 224 {224} \times {224} 224×224 image corresponds to 256 tokens (Liu et al., 2023b), and a three-minute video at 24 FPS generates around 1.1 million tokens.

大型语言模型(LLMs)(Touvron et al. 2023a, b; OpenAI, 2023; Black et al., 2022)处于人工智能革命的前沿,推动了多轮对话(Schulman et al. 2022; Taori et al., 2023; Chiang et al., 2023)、长文档摘要(Goyal & Durrett, 2020, Zhang et al., 2023a)以及涉及混合模态的任务,如视觉和视频理解(Liu et al., 2023b; Lin et al., 2023)等高级应用。这些应用通常需要处理大量上下文标记;例如,总结整个《哈利·波特》系列可能涉及分析约一百万个标记。随着视觉语言模型(VLMs)的出现,这一挑战加剧,其中单个 224 × 224 {224} \times {224} 224×224 图像对应于 256 个标记(Liu et al., 2023b),而三分钟的视频以 24 FPS 生成约 110 万个标记。

A critical issue in deploying LLMs in such applications is the long-context inference problem. The full attention mechanism demands that all tokens attend to every previous token for accurate representation, resulting in linearly increasing decoding and quadratically increasing pre-filling latency as the sequence length grows. Additionally, the Key-Value (KV) Cache technique, which stores keys and values from all preceding tokens, causes memory usage to scale linearly with context length. As sequences lengthen, memory is increasingly consumed by the KV cache, placing a significant computational burden on the attention mechanism. For instance, in the Llama-3-8B (Dubey et al. 2024) model architecture, serving with FP16 KV cache for 1 million tokens would require at least 137    G B {137}\mathrm{\;{GB}} 137GB of memory-exceeding the capacity of a single 80    G B {80}\mathrm{\;{GB}} 80GB GPU. Additionally,the latencies

在这些应用中部署 LLMs 的一个关键问题是长上下文推理问题。完整注意力机制要求所有标记都参与每个先前的标记以进行准确表示,导致随着序列长度的增加,解码延迟线性增加,预填充延迟二次增加。此外,Key-Value(KV)缓存技术存储所有先前标记的键和值,导致内存使用量随上下文长度线性扩展。随着序列变长,KV 缓存消耗的内存越来越多,给注意力机制带来了巨大的计算负担。例如,在 Llama-3-8B(Dubey et al. 2024)模型架构中,使用 FP16 KV 缓存处理 100 万个标记将至少需要 137    G B {137}\mathrm{\;{GB}} 137GB 的内存,超过单个 80    G B {80}\mathrm{\;{GB}} 80GB GPU 的容量。此外,延迟


*Part of the work done during an internship at NVIDIA.

*在NVIDIA实习期间完成的工作的一部分。


Figure 1: Visualization of attention maps in the Llama-2-7B model for the sentence “The best fruit is orange. What is the best fruit? Orange.” shows the distinct roles of retrieval heads (e.g., Layer 15, Head 12) and streaming heads (e.g., Layer 10, Head 1). On the left, retrieval heads capture contextually relevant tokens such as “best,” “fruit,” and “orange,” which are crucial for processing long-context information and, therefore, require a full KV cache. In the middle, streaming heads primarily focus on initial and recent tokens without emphasizing past contextual relevance. On the right, the impact of limiting attention to the sink and recent tokens on long-context passkey retrieval accuracy is shown: modifying retrieval heads severely damages performance, while constraining streaming heads has minimal impacts.

图1:在Llama-2-7B模型中,句子“最好的水果是橙子。最好的水果是什么?橙子。”的注意力图可视化展示了检索头(例如,第15层,第12头)和流式头(例如,第10层,第1头)的不同角色。在左侧,检索头捕获了上下文相关的标记,如“最好”、“水果”和“橙子”,这些标记对于处理长上下文信息至关重要,因此需要完整的KV缓存。在中间,流式头主要关注初始和最近的标记,而不强调过去的上下文相关性。在右侧,显示了将注意力限制在接收器和最近标记上对长上下文通行证检索准确性的影响:修改检索头会严重损害性能,而约束流式头的影响最小。

of pre-filling and decoding with such large contexts are significant, posing substantial challenges to the effective use of LLMs in long-context scenarios.

在如此大的上下文中进行预填充和解码的开销是巨大的,给在长上下文场景中有效使用LLM带来了重大挑战。

Despite numerous efforts to overcome the challenges of attention mechanisms in long-context inference, significant computational and memory issues persist. Architectural modifications, such as Grouped-Query Attention (GQA)(Ainslie et al. 2023), require model pre-training and fail to reduce computational costs. Linear Attention methods (Gu & Dao, 2023; Poli et al. 2023), while less demanding in terms of computation and memory, often underperform in long-context scenarios compared to Transformer models. Approximative attention methods,such as H 2 O {\mathrm{H}}_{2}\mathrm{O} H2​O (Zhang et al. 2023b), StreamingLLM (Xiao et al. 2023b), TOVA (Oren et al. 2024), and FastGen (Ge et al. 2024), often compromise accuracy in long-context applications and are incompatible with essential KV cache optimization techniques like GQA. KV cache quantization (Liu et al., 2024, Hooper et al., 2024), although useful, does not reduce the computation time of the attention mechanism. System-level optimizations, including FlashAttention (Dao et al., 2022; Dao, 2023), FlashDecoding (Hong et al. 2024), and PagedAttention (Kwon et al. 2023), while effective, do not reduce the KV cache size and still require significant computation for extended contexts. These limitations emphasize the need for further advancements to deploy models that handle million-level context lengths.

尽管在克服长上下文推理中注意力机制的挑战方面做出了许多努力,但显著的计算和内存问题仍然存在。架构修改,如分组查询注意力(GQA)(Ainslie等人,2023年),需要模型预训练并且无法降低计算成本。线性注意力方法(Gu & Dao,2023年;Poli等人,2023年)虽然在计算和内存方面要求较低,但在长上下文场景中通常表现不如Transformer模型。近似注意力方法,如 H 2 O {\mathrm{H}}_{2}\mathrm{O} H2​O(Zhang等人,2023b),StreamingLLM(Xiao等人,2023b),TOVA(Oren等人,2024年)和FastGen(Ge等人,2024年),在长上下文应用中常常牺牲准确性,并且与GQA等关键的KV缓存优化技术不兼容。KV缓存量化(Liu等人,2024年,Hooper等人,2024年)虽然有用,但不会减少注意力机制的计算时间。系统级优化,包括FlashAttention(Dao等人,2022年;Dao,2023年),FlashDecoding(Hong等人,2024年)和PagedAttention(Kwon等人,2023年),虽然有效,但不会减少KV缓存的大小,并且仍然需要大量计算来处理扩展的上下文。这些限制强调了进一步发展的必要性,以部署能够处理百万级上下文长度的模型。

In this paper, we introduce a key observation that attention heads in LLMs can be categorized into two distinct types: Retrieval Heads (Wu et al. 2024) and Streaming Heads, as shown in Figure 1. Retrieval Heads, which represent only a fraction of the total, are crucial for processing long contexts and require full attention across all tokens. In contrast, the majority of attention heads, termed Streaming Heads, primarily focus on recent tokens and attention sinks (Xiao et al. 2023b), and can operate effectively with a reduced KV cache that includes only recent tokens and attention sinks.

在本文中,我们提出了一项关键观察结果,即大型语言模型(LLMs)中的注意力头可以分为两种不同类型:检索头(Wu et al. 2024)和流式头,如图1所示。检索头仅占总数的少数,但对于处理长上下文至关重要,需要对所有标记进行全面关注。相比之下,大多数注意力头被称为流式头,主要关注最近的标记和注意力汇聚点(Xiao et al. 2023b),并且可以通过包含最近标记和注意力汇聚点的简化KV缓存有效运作。

Building on the dichotomy of retrieval and streaming heads, we propose DuoAttention, a general, straightforward, and easily integrated approach that significantly accelerates both LLM’s decoding and pre-filling and reduces memory footprints, particularly in long-context scenarios. The core innovation of DuoAttention is a lightweight, optimization-based procedure that identifies non-compressible retrieval heads using synthetic datasets. Unlike existing methods that rely on attention pattern profiling (Wu et al. 2024; Ge et al. 2024; Tang et al. 2024a), DuoAttention directly measures output deviation resulting from token dropping, achieving higher compression rates and improved deployment efficiency. DuoAttention is designed with simplicity and efficiency in mind: each Transformer layer has two KV caches- a full KV cache for crucial retrieval heads and a constant KV cache for streaming heads, which stores only attention sinks and recent tokens. This design allows DuoAttention to dramatically reduce memory usage and improve decoding speed in models like Llama-2/3 and Mistral,achieving up to 2.55 × {2.55} \times 2.55× for MHA and 1.67 × {1.67} \times 1.67× for GQA models while speeding up decoding by up to 2.18 × {2.18} \times 2.18× and 1.50 × {1.50} \times 1.50× and accelerating pre-filling by up to 1.73 × {1.73} \times 1.73× and 1.63 × {1.63} \times 1.63× for MHA and GQA models, respectively, with minimal accuracy loss compared to full attention.

基于检索头和流式头的二分法,我们提出了DuoAttention,这是一种通用、简单且易于集成的方法,能够显著加速LLM的解码和预填充过程,并减少内存占用,特别是在长上下文场景中。DuoAttention的核心创新是一种轻量级的基于优化的程序,用于识别不可压缩的检索头,使用合成数据集进行操作。与依赖于注意力模式分析的现有方法(Wu et al. 2024; Ge et al. 2024; Tang et al. 2024a)不同,DuoAttention直接测量由于标记丢弃导致的输出偏差,从而实现更高的压缩率和改进的部署效率。DuoAttention的设计注重简单性和效率:每个Transformer层有两个KV缓存——一个用于关键检索头的完整KV缓存和一个用于流式头的常量KV缓存,后者仅存储注意力汇聚点和最近的标记。这种设计使得DuoAttention能够大幅减少内存使用并提高解码速度,例如在Llama-2/3和Mistral模型中,分别实现了MHA模型的 2.55 × {2.55} \times 2.55×和GQA模型的 1.67 × {1.67} \times 1.67×的压缩率,同时解码速度分别提高了 2.18 × {2.18} \times 2.18×和 1.50 × {1.50} \times 1.50×,预填充速度分别提高了 1.73 × {1.73} \times 1.73×和 1.63 × {1.63} \times 1.63×,与全注意力相比,精度损失最小。

Moreover, DuoAttention is fully compatible with important optimization techniques like GQA and quantization. We show that when combined with 8-bit weight 4-bit KV cache quantization,

此外,DuoAttention 完全兼容重要的优化技术,如 GQA 和量化。我们展示了当与 8 位权重 4 位 KV 缓存量化结合时,

Figure 2: Overview of DuoAttention: (1) In the retrieval head identification phase, we assign a trainable gate value, α \alpha α ,to each attention head,which blends the outputs of full attention and streaming attention. The training objective is to optimize these values to minimize the deviation from the full attention model’s output, while simultaneously applying a regularization loss to encourage lower gate values. This training phase is efficient, requiring only the gate values to be trainable-leaving all other model parameters frozen-thus allowing it to be completed within several hours on an 8 GPU node. (2) During deployment, these gate values are binarized to classify heads as either retrieval or streaming based on a threshold τ \tau τ . Retrieval heads,identified by a gate value above the threshold, use full attention, caching the KV pairs for all tokens. In contrast, streaming heads cache only the KV pairs of recent tokens and attention sinks.

图 2:DuoAttention 概述:(1) 在检索头识别阶段,我们为每个注意力头分配一个可训练的门值 α \alpha α,该值混合了全注意力和流式注意力的输出。训练目标是优化这些值以最小化与全注意力模型输出的偏差,同时应用正则化损失以鼓励较低的门值。此训练阶段是高效的,只需要门值是可训练的——所有其他模型参数保持冻结——因此允许它在 8 GPU 节点上在几个小时内完成。(2) 在部署期间,这些门值被二值化,根据阈值 τ \tau τ 将头分类为检索或流式。检索头,由高于阈值的门值识别,使用全注意力,缓存所有令牌的 KV 对。相比之下,流式头仅缓存最近令牌和注意力汇的 KV 对。

DuoAttention enables a Llama-3-8B model to handle up to 3.3 million contextual tokens measured on a single A100 GPU,achieving a 6.4 × {6.4} \times 6.4× capacity increase compared to standard full attention FP16 deployments. DuoAttention paves the way for deploying LLMs in applications requiring million-level context handling.

DuoAttention 使 Llama-3-8B 模型能够在单个 A100 GPU 上处理多达 330 万个上下文令牌,与标准全注意力 FP16 部署相比,实现了 6.4 × {6.4} \times 6.4× 容量增加。DuoAttention 为在需要百万级上下文处理的应用中部署 LLM 铺平了道路。

2 DUOATTENTION

2 DUOATTENTION

2.1 RETRIEVAL AND STREAMING HEADS

2.1 检索头和流式头

Retrieval Heads In Transformer-based LLMs, attention heads exhibit distinct and consistent patterns, reflecting their specialized functionalities (Clark et al., 2019, Xiao et al., 2023b, Wu et al., 2024). Figure 1 visualizes two types of attention heads in the Llama-2-7B-32K-Instruct model using the sentence “The best fruit is orange. What is the best fruit? Orange”. The left panel highlights an attention head that emphasizes relevant tokens during decoding; for instance, the first occurrence of “best fruit” is accentuated while decoding the second “best fruit,” and the initial “orange” is highlighted when inferring the second “orange.” These attention heads, which we term Retrieval Heads, are crucial for context processing as they capture contextually relevant tokens. Compressing the KV cache for retrieval heads would lead to the loss of vital contextual information, and thus they require full attention across all tokens.

基于Transformer的大型语言模型(LLMs)中的注意力头表现出不同的、一致的模式,反映了它们的专业功能(Clark et al., 2019, Xiao et al., 2023b, Wu et al., 2024)。图1使用句子“最好的水果是橙子。最好的水果是什么?橙子”可视化了Llama-2-7B-32K-Instruct模型中的两种注意力头。左侧面板突出显示了一个在解码过程中强调相关标记的注意力头;例如,在解码第二个“最好的水果”时,第一个“最好的水果”被强调,而在推断第二个“橙子”时,初始的“橙子”被突出显示。这些我们称之为检索头的注意力头对于上下文处理至关重要,因为它们捕捉了上下文相关的标记。压缩检索头的KV缓存会导致重要上下文信息的丢失,因此它们需要对所有标记进行全注意力处理。

Streaming Heads In contrast, the attention head depicted in the middle panel of Figure 1 primarily attends to recent tokens and attention sinks (Xiao et al. 2023b), without highlighting earlier relevant tokens in the context. We refer to these as Streaming Heads. Compressing the KV cache for Streaming Heads is feasible because dropping the unattended middle tokens does not significantly alter the attention output. Therefore, streaming heads can be optimized by retaining only the KV states of attention sinks and recent tokens, without compromising the model’s ability to manage long contexts.

流式头相比之下,图1中间面板中描述的注意力头主要关注最近的标记和注意力汇聚点(Xiao et al. 2023b),而不强调上下文中较早的相关标记。我们称这些为流式头。压缩流式头的KV缓存是可行的,因为丢弃未被关注的中间标记不会显著改变注意力输出。因此,流式头可以通过仅保留注意力汇聚点和最近标记的KV状态来优化,而不会损害模型管理长上下文的能力。

Impact of Token Pruning on Retrieval and Streaming Heads The right panel of Figure 1 shows a preliminary passkey retrieval experiment, showing that the model’s performance drops significantly when the middle tokens in the KV cache of retrieval heads are pruned, i.e., replaced with streaming attention. In contrast, removing the middle tokens for streaming heads has no significant impact on passkey retrieval accuracy. This observation indicates that we can enhance computational efficiency without sacrificing the model’s long-context capabilities: By dropping middle tokens for streaming heads while keeping full attention for retrieval heads, we reduce the memory demands of streaming heads to O ( 1 ) O\left( 1\right) O(1) ,thereby improving the efficiency of processing long contexts.

令牌剪枝对检索和流式处理头的影响 图1的右侧面板展示了一个初步的通行密钥检索实验,显示当检索头的KV缓存中的中间令牌被剪枝时,即被流式注意力替换时,模型的性能显著下降。相比之下,移除流式处理头的中间令牌对通行密钥检索准确性没有显著影响。这一观察表明,我们可以在不牺牲模型长上下文能力的情况下提高计算效率:通过为流式处理头丢弃中间令牌,同时为检索头保持全注意力,我们将流式处理头的内存需求减少到 O ( 1 ) O\left( 1\right) O(1),从而提高了处理长上下文的效率。

Figure 3: Example from the synthetic dataset used to identify retrieval heads. We embed ten 32-word passkeys within a long text and ask the model to recall these passkeys. Distillation loss is calculated solely on the passkeys.

图3:用于识别检索头的合成数据集中的示例。我们在长文本中嵌入了十个32词的通行密钥,并要求模型回忆这些通行密钥。蒸馏损失仅在通行密钥上计算。

Figure 4: Optimized gate values of four LLMs. Llama-2-7B uses MHA with 32 heads per layer, while Mistral and Llama-3 models use GQA with 8 heads per layer. Retrieval heads have higher scores. MHA models have a lower ratio of retrieval heads compared to GQA models.

图4:四个LLM的优化门值。Llama-2-7B使用每层32个头的MHA,而Mistral和Llama-3模型使用每层8个头的GQA。检索头的得分较高。与GQA模型相比,MHA模型的检索头比例较低。

2.2 Optimization-Based Identification of Retrieval Heads

2.2 基于优化的检索头识别

Definition of Retrieval Heads Section 2.1 qualitatively defines retrieval and streaming heads, but for precise identification, we need a concrete and quantitative definition. In this paper, we define “retrieval heads” as the attention heads that:

检索头的定义 第2.1节定性地定义了检索和流式处理头,但为了精确识别,我们需要一个具体且量化的定义。在本文中,我们将“检索头”定义为那些:

significantly alter model outputs when restricted to recent tokens and attention sinks.

当限制为最近令牌和注意力汇时,显著改变模型输出的注意力头。

We use this criterion to distinguish retrieval heads from streaming heads. Note that this definition differs from existing works (Ge et al., 2024; Wu et al., 2024; Tang et al., 2024a) that rely solely on attention scores to identify retrieval heads, which overlook 1) the end-to-end impact of compressing the KV cache for specific attention heads, 2) the role of value states, and 3) the variability of attention distributions across layers and heads. In contrast, our definition directly measures output deviation, allowing us to identify attention heads crucial for long-context processing, even when they are not apparent in attention scores. We support this argument with ablation studies presented in Section 3.5

我们使用这一标准来区分检索头和流式头。需要注意的是,这一定义与现有工作(Ge et al., 2024; Wu et al., 2024; Tang et al., 2024a)有所不同,后者仅依赖注意力分数来识别检索头,这忽略了1) 压缩KV缓存对特定注意力头的端到端影响,2) 值状态的作用,以及3) 注意力分布在不同层和头之间的可变性。相比之下,我们的定义直接测量输出偏差,使我们能够识别对长上下文处理至关重要的注意力头,即使在注意力分数中不明显时也能识别。我们在第3.5节中通过消融研究支持这一论点。

Optimization-based Identification We employ an optimization-based approach to identify retrieval heads, drawing inspiration from prior work in CNN filter pruning (Liu et al. 2017), as illustrated in Figure 2. First,we assign a gate value α i , j {\alpha }_{i,j} αi,j​ ,to each key-value (KV) head in the LLM. This value intuitively represents the importance of the j j j -th KV head in layer i i i for processing long-context information. Note that in models using GQA, one KV head can be associated with multiple attention heads, and our method accounts for the KV cache compression of an entire group of attention heads.

基于优化的识别方法 我们采用基于优化的方法来识别检索头,借鉴了CNN滤波器剪枝的先前工作(Liu et al. 2017),如图2所示。首先,我们为LLM中的每个键值(KV)头分配一个门值 α i , j {\alpha }_{i,j} αi,j​,该值直观地表示第 j j j层中的第 i i i个KV头在处理长上下文信息中的重要性。需要注意的是,在使用GQA的模型中,一个KV头可以与多个注意力头相关联,我们的方法考虑了整个注意力头组的KV缓存压缩。

Our optimization-based identification method directly assesses the impact of compressing the KV cache with only sink and recent tokens for each KV head. We begin by initializing the gate value α i , j ∈ [ 0 , 1 ] {\alpha }_{i,j} \in \left\lbrack {0,1}\right\rbrack αi,j​∈[0,1] for each head at 1,assuming that all heads initially serve as retrieval heads. These gate values are then optimized, with the LLM’s parameters remaining fixed, limiting the number of trainable parameters to N × H N \times H N×H and preventing the impact to the model’s original abilities.

我们的基于优化的识别方法直接评估仅使用sink和最近令牌压缩每个KV头的KV缓存的影响。我们首先将每个头的门值 α i , j ∈ [ 0 , 1 ] {\alpha }_{i,j} \in \left\lbrack {0,1}\right\rbrack αi,j​∈[0,1]初始化为1,假设所有头最初都作为检索头。然后优化这些门值,同时保持LLM的参数固定,将可训练参数的数量限制为 N × H N \times H N×H,并防止对模型原始能力的影响。

During the forward pass, we combine the outputs of full and streaming attention (which attends only to sink and recent tokens) for each KV head, using the gate value as the mixing weight:

在前向传递过程中,我们使用门值作为混合权重,将每个KV头的完整和流式注意力(仅关注接收和最近的标记)的输出结合起来:

where the attention calculations are defined as:

其中注意力计算定义为:

where M causal  {\mathbf{M}}_{\text{causal }} Mcausal ​ is the causal attention mask (a lower triangular matrix),and M streaming  {\mathbf{M}}_{\text{streaming }} Mstreaming ​ represents a Λ \Lambda Λ -like mask (Han et al. 2023,Xiao et al. 2023b) that attends only to recent and initial tokens.

其中 M causal  {\mathbf{M}}_{\text{causal }} Mcausal ​ 是因果注意力掩码(一个下三角矩阵),而 M streaming  {\mathbf{M}}_{\text{streaming }} Mstreaming ​ 表示一个 Λ \Lambda Λ -like 掩码(Han et al. 2023, Xiao et al. 2023b),它仅关注最近的和初始的标记。

Synthetic Dataset for Identifying Retrieval Heads However, relying solely on natural language modeling objectives is insufficient for identifying retrieval heads because the supervision signal in

用于识别检索头的合成数据集 然而,仅依赖自然语言建模目标不足以识别检索头,因为在

—— 更多内容请到Doc2X翻译查看——
—— For more content, please visit Doc2X for translations ——

DuoAttention: 高效的长上下文大语言模型推理方法 在人工智能的日新月异中,长上下文大语言模型(LLMs)如同一颗闪亮的明星,吸引着研究人员的目光。然而,部署这些模型并非易事,尤其在处理长上下文时,面临着计算和内存的巨大挑战。在这一背景下,“DuoAttention”的理念应运而生,旨在通过高效的长上下文推理方法,缓解这些问题。 阅读详情

相关推荐

YOLOv10改进 | 注意力篇 | YOLOv10引入DualAttention注意力

然后,通过另一个构建的像素路径,这种压缩的全局语义可以作为学习更精细的像素级细节的有用先验信息。这一路径负责将输入特征图总结为语义标记,然后这些语义标记作为增强的查询(semantic queries),被送入下一个Dual block的语义路径作为先验信息。到此本文的正式分享内容就结束了,在这里给大家推荐我的YOLOv10改进有效涨点专栏,后期我会根据各种最新的前沿顶会进行论文复现,也会对一些老的改进机制进行补充,如果大家觉得本文帮助到你了,订阅本专栏,关注后续更多的更新~

tsg6698的博客 371

DuoAttention:高效长上下文LLM推理框架

DuoAttention 是一个针对长上下文的大型语言模型(LLM)推理的高效框架。该框架通过区分处理长上下文的检索头和处理近期信息的流式头,大幅减少了LLM的解码和预填充内存及延迟,同时不牺牲模型处理长上下文的能力。 ## 2. 项目快速启动 ### 环境搭建 首先,创建一个名为 `duo` 的虚拟环境,并安装必要的依赖: ```bash conda create -yn duo pyt

gitblog_01052的博客 973

MIT Bevfusion环境安装 Docker方式,nuscenes数据处理

Bevfusion环境安装,数据生成报错解决module 'numpy' has no attribute 'long';most likely due to a circular import。bevfusion docker环境

Jack_Man_N的博客 1144

DuoAttention: 高效的长上下文LLM推理与检索和流式头

DUOATTENTION: EFFICIENT LONG-CONTEXT LLM INFERENCE WITH RETRIEVAL AND STREAMING HEADS DuoAttention: 高效的长上下文LLM推理与检索和流式头

Together_CZ的博客 1249

西门子smart能搜到cpu但连接当前IP地址失败

将ip地址改到同一网段 本机为192.168.1.119就是192.168.1.1网段,但plc在192.168.0.1网段,所以显示ip连接失败,改为同一网段192.168.1.xxx即可(为避免ip冲突尽量改到200往后最大255)如果显示这一步,那么你的物理连接是没有问题的,应当是plc和电脑并不在同一网段导致的问题。输入ipconfig 查看本机网段与plc是否为同一网段。此电脑就不在同一网段所有造成通讯失败,接下来解决这个问题。win+r打开运行,打开cmd。再次尝试,如果还是报错,那么。

weixin_62678581的博客 9867

保姆级教程:用Kvaser DB Editor从零创建你的第一个DBC文件(附CAN信号属性详解)

本文提供了一份详细的Kvaser DB Editor教程,指导用户从零开始创建DBC文件,并深入解析CAN信号属性的关键设置。通过步骤清晰的安装指南、信号属性配置详解和实用技巧,帮助工程师和学生快速掌握CAN总线通信的核心工具,提升工作效率。

weixin_27076351的博客 250

MIT韩松团队提出DuoAttention框架:单GPU实现330万Token上下文推理

©作者 |肖光烜单位 |麻省理工学院博士生研究方向 |深度学习加速TL;DR:DuoAttention 通过将大语言模型的注意力头分为检索头(Retrieval Heads,需要完整 KV 缓存)和流式头(Streaming Heads,只需固定量 KV 缓存),大幅提升了长上下文推理的效率,显著减少内存消耗、同时提高解码(Decoding)和预填充(Pre-filling)速度,同时在长短...

Paper weekly 923

雷达信号处理——恒虚警检测(CFAR)

上图展示的是同时在距离维和多普勒维进行恒虚警检测的原理,如果只是在距离维或多普勒维做CFAR,检测单元的左右两侧均有设定好数量的保护单元和参考单元。不同的CFAR检测器适用于不同的场景,CA-CFAR算法适用于均匀环境中的单目标或彼此相距较远的多目标环境,在邻近多目标环境会发生目标的遮蔽效应,在杂波边缘会发生杂波边缘效应。从图中也可以看到,根据阈值的选择不同,有不同的检测器,其中CA-CFAR是取的参考单元的均值,SO-CFAR取的是参考单元的最小值,而GO-CFAR是取的参考单元的最大值。

诚朴勇毅 3万+

TickDB REST API完整指南:6个端点实战获取实时报价、K线、订单簿深度与成交记录

TickDB 是一款 AI 原生的实时行情数据 API,一套 REST API 即可覆盖美股、港股、A股、外汇、指数、加密货币与大宗商品等 37,527+ 品种。本文带你用 6 个核心端点,几分钟内跑通实时报价(Ticker)、K 线(Kline)、订单簿深度(Depth)与成交记录(Trades)的获取流程,是新手接入金融行情数据的完整指南。 --- ## 一、为什么选择 TickDB 实时

gitblog_00224的博客 1056

华为od 面试八股文_Android安卓_05_含答案

华为od 面试八股文_Android安卓_05_含答案

MISAYAONE的博客 951

深入浅出:使用Python爬取链家二手房数据

在数据驱动的时代,房地产数据作为社会经济的重要指标,一直是数据分析师、投资者和研究人员关注的焦点。链家作为国内领先的房产中介平台,其二手房数据具有较高的真实性和时效性。然而,官方API的访问限制和数据获取的门槛,让很多初学者望而却步。本文将带你从零开始,使用Python编写一个符合robots.txt规范的链家二手房爬虫,获取标题、户型、价格等信息。文章不仅会提供完整代码,还会深入讲解反爬策略、请求头伪装、数据解析、异常处理等实战技术。目录前言:为什么要写这篇博客?一、爬虫前的准备:法律与道德。

2201_76125261的博客 90

长上下文泛化问题:算力、显存与无限注意力

本文针对 2026 年大语言模型面临的“长上下文泛化 (Long-Context Generalization)”核心挑战进行了全景式技术复盘。文章首先剖析了长文本处理中普遍存在的 "Lost in the Middle" 现象及其背后的注意力退化机制;继而系统阐述了从 **RoPE** 旋转位置编码到 **YaRN** 频率内插,再到 **LongRoPE** 非均匀进化搜索的数学修正演进路径。在架构革新层面,深入探讨了 **Mamba** 与 **Jamba** 等线性复杂度模型如何打破 $O(N^2)

qq_44768937的博客 1463

华为OD机试真题 Java&Go 实现【优雅子数组】【100%通过率】【2022.11 Q4 新题】

如果一个数组中出现次数最多的元素出现大于等于K次,被称为K -优雅数组,k也可以被称为优雅阈值。例如,数组1,2,3,1、2,3,1,它是一个3-优雅数组,因为元素1出现次数大于等于3次,数组1,2,3,1,2就不是一个3-优雅数组,因为其中出现次数最多的元素是1和2,只出现了2次。给定一个数组A和k,请求出A有多少子数组是k-优雅子数组。

MISAYAONE的博客 1万+

duo-attention:高效长上下文LLM推理

duo-attention 是一个开源项目,旨在为长上下文的大型语言模型(LLM)提供高效的推理解决方案。在部署长上下文LLM时,计算和内存挑战非常显著。现有的KV缓存剪枝方法要么损害LLM处理长上下文的能力,要么仅提供有限的效率改进。duo-attention 通过引入一种新的注意力机制,即 Retrieval Heads 和 Streaming Heads,来解决这一问题,从而在不牺牲长上下文

gitblog_00954的博客 1111

QCC304x系列开发教程(实战篇)之5.2 QCC3040之提示音

earbud_init.c中,初始化table中UiPrompts_Init /*! \brief Table of initialisation functions */ static const init_table_entry_t appInitTable[] = { ......... {UiPrompts_Init, 0, NULL}, ......... } ui_prompts.c中是提示音音处理的主要逻辑和配置源文件 函数static void uiPro...

心跳包的博客 2393

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

MIT韩松 | DuoAttention:单GPU实现330万Token上下文推理

编辑| 机器之心点击下方卡片,关注“自动驾驶之心”公众号戳我-> 领取自动驾驶近15个方向学习路线>>点击进入→自动驾驶之心『大语言模型』技术交流群本文只做学术分享,如有侵权,联系删文本文第一作者肖光烜是麻省理工学院电子工程与计算机科学系(MIT EECS)的三年级博士生,师从韩松教授,研究方向为深度学习加速,尤其是大型语言模型(LLM)的加速算法设计。他在清华大学计算机科学与...

CV_Autobot的博客 488

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【大模型推理加速】KV cache

KV cache 已存储换计算的优化,随着句子长度不断增加, KV 存储空间膨胀, 如典型的6B模型, 做8 batch, 8K 推理时, KV cache 大小约为29 GB.

王尚权 qq:2515162716 986

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

视频隐私脱敏工程化:CSRT/KCF追踪算法在本地推理中的实现与边界

在短视频内容合规审查中,动态遮挡(Dynamic Masking)是高频且高耗时的环节。传统手动关键帧打码不仅效率低下,且在目标快速移动或遮挡时极易出现遮罩漂移。本文从计算机视觉工程角度,解析基于CSRT与KCF算法的目标追踪方案在视频隐私处理中的技术路径、性能权衡及落地实践。

Mysssone的博客 238

我用 GPUWorks 在 M1 Pro 上搭了个发票识别原型

公司里经常要看电子发票。我想先做个小原型:把 PDF 发给一个自己部署的图文模型,让它提取发票类型、日期、交易双方和金额,再把结果交给同事核对。这个原型不用先接财务系统,先看看从部署到调用要走多少步,模型返回的内容能不能帮上忙。

鸽芷咕的博客 2万+

ChatGPT、Perplexity、Gemini 与 AI Overviews 引用对比:谁的引用更可信

AI 引擎的引用机制差异很大:ChatGPT 以行内引用加 Sources 列表呈现来源,Perplexity 几乎逐句可溯源,Gemini 在回答中内嵌相关来源链接,AI Overviews 则把多源聚合答案与来源列表一起放在搜索结果顶部。本文从引用透明度、来源偏好、可干预性 3 个维度对比这 4 个引擎,并给出 GEO 的 3 点启示,帮企业在多引擎环境下提升被引用的概率。

淡然一笑 的专栏 72

【从零理解 Spring AI · 12】MCP 的两种角色:做客户端还是做服务端

上一篇讲了 MCP 的概念:标准协议、三大角色、M+N 替代 M×N。

nice_lcj520的博客 508

AI Agent之核心框架

A: 显式 Thought 有三类价值:可解释性(便于人类审计)、可调试性(定位哪一步选错工具)、学习信号(可做监督微调或评估中间步骤质量)。ReAct 把推理与 可执行行动 绑定,每一步行动后都有 真实 Observation 反馈,从而用工具结果约束生成,降低闭卷幻觉。做完不等于结束——还要 评估做得好不好,把 教训 记下来,下次 带着教训重试,反思作为 记忆,影响后续尝试的策略。工作流程:Action → Evaluation → Reflection → Retry。A: 普通 CoT 只在。

剑雨巍巍的博客 207

AI 应用开发工程师技术实践:大模型接入、通用组件封装与性能优化

本文提出AI应用从PoC到生产的核心挑战:模型调用的持续可用、可控与可优化。通过构建统一LLM网关实现多供应商路由与故障转移,封装对话、工具调用与RAG组件以解耦业务逻辑,结合前缀缓存、批处理与流式背压治理等性能优化手段,形成“接通→接稳→接快”的工程路径,最终以工程化能力决定AI应用的生产可用性。

2301_79189962的博客 562

GPT-1 架构和计算细节

下面配合框图,对 GPT-1 的每个组件做公式展开 + 数值实例(配置:12 层 × 768,12 头,FFN 3072,词表 40478,共 117M 参数)。GPT-1 把翻译模型的编码器-解码器砍掉一半,只留解码器,然后以"预测下一个词"为目标在 BooksCorpus 上预训练。与 2017 原版的四个架构差异(框图中标 ★):h0=E[token]+Epos[position],E∈R40478×768, Epos∈R512×768h^0 = E[\text{token}] + E_{pos}[\

eloudy的专栏 289

终端 AI 审计与 “龙虾” 类智能体风险治理:从可见性到闭环溯源

生成式 AI 办公普及之后,数据泄露风险逐步下沉到终端。员工将合同、源代码、客户资料粘贴至公有大模型,以及龙虾(OpenClaw)这类本地开源 AI 智能体的滥用,成为企业安全新挑战。传统网络边界防护存在审计盲区,本文从风险链路拆解、核心能力模型、分级治理策略、落地踩坑点以及技术局限性多角度,分享终端侧 AI 安全治理的实践思路。

2401_86491085的博客 646

抽象主义人工智能和生命体AI的内在联系

彭军辉提出“抽象主义人工智能”作为第四大AI范式,批判符号主义、连接主义、行为主义剥离生命本体的误区,确立“智能是现象,生命是本体”的核心公理。主张原生智能必须依附具备存续诉求的生命本体,提出“生命体AI理论”,构建七层演化阶梯与自闭环工程框架,强调智能不可跳过底层存续机制直接模拟语言。该体系为生成式AI提供科学边界,推动从“仿真表象”向“原生智能”的范式跃迁,开辟硅基人工生命新赛道。

抽象主义人工智能和生命体AI 63
上一篇: VERIFIED: A Video Corpus Moment Retrieval Benchmark for Fine-Grained Video Understanding 翻译
下一篇: OKAMI: Teaching Humanoid Robots Manipulation Skills through Single Video Imitation 翻译
Doc2X
博客等级 码龄2年 341粉丝 30原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值