arXiv is now an independent nonprofit! Learn more
License: arXiv.org perpetual non-exclusive license
arXiv:2412.08585v3 [cs.LG] 17 Dec 2024

marginparsep has been altered.
topmargin has been altered.
marginparwidth has been altered.
marginparpush has been altered.

The page layout violates the ICML style.

Please do not change the page layout, or include packages like geometry, savetrees, or fullpage, which change it for you.

We’re not able to reliably undo arbitrary changes to the style. Please remove the offending package(s), or layout-changing commands and try again.

 

TurboAttention: Efficient attention approximation for High Throughputs LLMs

 

Hao Kang1 2  Srikant Bharadwaj1  James Hensman1  Tushar Krishna2  Victor Rühle1  Saravan Rajmohan1 

footnotetext: *Equal contribution 1Microsoft 2Georgia Institute of Technology. Correspondence to: Srikant Bharadwaj <srikant.bharadwaj@microsoft.com>.
Abstract

Large language model (LLM) inference demands significant amount of computation and memory, especially in the key attention mechanism. While techniques, such as quantization and acceleration algorithms, like FlashAttention, have improved efficiency of the overall inference, they address different aspects of the problem: quantization focuses on weight-activation operations, while FlashAttention improves execution but requires high-precision formats. Recent Key-value (KV) cache quantization reduces memory bandwidth but still needs floating-point dequantization for attention operation.

We present TurboAttention, a comprehensive approach to enable quantized execution of attention that simultaneously addresses both memory and computational efficiency. Our solution introduces two key innovations: FlashQ, a headwise attention quantization technique that enables both compression of KV cache and quantized execution of activation-activation multiplication, and Sparsity-based Softmax Approximation (SAS), which eliminates the need for dequantization to FP32 during exponentiation operation in attention. Experimental results demonstrate that TurboAttention achieves 1.2-1.8x speedup in attention, reduces the KV cache size by over 4.4x, and enables up to 2.37x maximum throughput over the FP16 baseline while outperforming state-of-the-art quantization and compression techniques across various datasets and models.

1 Introduction

Refer to caption
(a) Prompt:Output Tokens = 8:1
Refer to caption
(b) Timeshare in GPU attention kernel
Refer to caption
(c) End-to-end inference timeshare
Figure 1: Latency profile of Phi3-Medium on Nvidia A100 GPU. (a) KV cache compression techniques impose a dequantization overhead in attention kernel latency. (b) TurboAttention significantly improves attention kernel latency compared to FlashAttention(FP16) baseline while other work mainly focuses on reducing KV-cache memory footprint and bandwidth only.(c) TurboAttention reduces latency of Matmul+KV-cache load by enabling quantized integer inference, dequantization by applying block progressive quantization, and faster softmax by introducing sparse activated softmax.

Large language models (LLMs) (Touvron et al., 2023; Gunasekar et al., 2023; Brown et al., 2020) have excelled in tasks like natural language understanding (Joshi et al., 2017; Dodge et al., 2021) and generative text production (Hendrycks et al., 2021; Zhong et al., 2017). However, as model size increases, computational and memory demands scale correspondingly, particularly during inference. This necessitates efficient strategies to reduce memory utilization and computational complexity, thereby reducing inference latency and improving throughput — critical requirements for real-time applications that demand faster user experiences.

Quantization is among the most commonly used optimization techniques that simultaneously addresses both computational and memory constraints in LLM inference. By reducing the numerical precision of model parameters, KV cache states, and activation values, this technique enables low-precision forward pass computations while significantly reducing memory footprint. This unified approach to precision reduction offers a systematic method for optimizing inference efficiency when applied to the computational and memory intensive parts of the LLM inference.

The bottlenecks during LLM inference can be split into three major sections: the linear projection operations (QKV projection and FFN), the memory-intensive Key/Value (KV) cache operations, and the attention mechanism’s square computational and memory complexity with respect to context length. As both context lengths and model sizes scale up, the KV cache footprint expands substantially, while the attention computations become increasingly resource-intensive, leading to reduced throughput and elevated latencies, which ultimately results in degraded user experiences as well as increased costs. 1(a) shows the time spent in the attention operation compared to others during inference as prompt sizes increases. The attention mechanism contributes significantly to the overall generation time, contributing up to 80% of the overall latency at large context lengths (>80k).

Previous quantization works, such as Atom (Zhao et al., 2024), QuaRot(Ashkboos et al., 2024), and Qserve (Lin et al., 2024), have primarily applied quantization techniques to linear network components (such as QKV and FFN projections), converting floating-point parameters and activations to low-bit integers to enhance memory efficiency and computational performance. On the other hand, approaches such as KIVI (Zirui Liu et al., 2023), GEAR (Kang et al., 2024), and Qserve (Lin et al., 2024) focus on compressing the KV cache to 4-bit or even 2-bit formats but rely on time-intensive floating-point decompression before executing the attention mechanism. This incurs decompression overhead during the execution of attention leading to increased overall latencies, as shown in 1(b). In general, these approaches largely neglect the latency overhead of the attention operation, which is a key bottleneck in terms of latency overhead during inference.

On the other hand, attention execution acceleration methods, such as Flash Attention-1,2,3 (Dao et al., 2022; Dao, 2023; Shah et al., 2024), along with related optimizations (Dao et al., 2023; Hong et al., 2024), have improved efficiency of the attention operation and have become the commercial standard. However, they operate exclusively on high-precision formats (FP16/32) which leads to high attention latencies at longer context lengths, as shown in 1(c). This leaves considerable scope for further optimizations using quantization, which could yield substantial benefits in memory usage and computational speed.

In this paper, we present, TurboAttention, a novel unified technique for enabling quantized execution of attention along with a cooperative KV cache compression mechanism which reduces latency, memory footprint, incurs negligible accuracy loss. First, to accelerate the matrix multiplications, minimize decompression time, and ensure compatibility with FlashAttention, we introduce FlashQ (section 3). Second, for efficient GPU tensor core-friendly, softmax computation, we develop SAS (section 4). Third, to alleviate the memory bottleneck posed by the KV cache, we design a CUDA kernel-friendly headwise mixed precision quantization method(section 3). Together, FlashQ and SAS form our comprehensive solution, TurboAttention. To our knowledge, this is the first work to bridge the state-of-the-art attention acceleration method (FlashAttention) with a quantization algorithm, and the first to apply lossy techniques to accelerate the entire attention mechanism, encompassing both matrix multiplication and softmax operations. A comparison of our approach with concurrent works is provided in Table 1.

Our evaluations show that TurboAttention outperforms state-of-the-art FlashAttention-3 on delivering up to 1.8x improvement in latency as well as up to 2.37×2.37\times maximum throughput. Further, our method delivers near-lossless accuracy across various tasks including mathematical reasoning (GSM8k and AQuA), symbolic reasoning (BigBench-Hard) and models (Llama3, Qwen2 and Phi-3), further validating its effectiveness.

Table 1: TurboAttention enables quantized execution of attention operation as well as KV cache compression. Note that techniques such as ATOMZhao et al. (2024), QuaRotAshkboos et al. (2024) are orthogonal to TurboAttention and can easily be applied in conjunction.
Target Technique QKV Projection KV Cache Compression Attention Execution MLP Memory Overhead Inference Latency
ATOM Zhao et al. (2024) Quantized {\color[rgb]{0,0.5,0}\checkmark} - Quantized {\color[rgb]{0,0.5,0}\downarrow} {\color[rgb]{0,0.5,0}\downarrow}
QuaRot Ashkboos et al. (2024) Quantized {\color[rgb]{0,0.5,0}\checkmark} - Quantized {\color[rgb]{0,0.5,0}\downarrow} {\color[rgb]{0,0.5,0}\downarrow}
Linear Operation (QKV, MLP, etc.) Qserve Lin et al. (2024) Quantized {\color[rgb]{0,0.5,0}\checkmark} - Quantized {\color[rgb]{0,0.5,0}\downarrow\downarrow} {\color[rgb]{0,0.5,0}\downarrow\downarrow}
KIVI Zirui Liu et al. (2023) - {\color[rgb]{0,0.5,0}\checkmark} - - {\color[rgb]{0,0.5,0}\downarrow} {\color[rgb]{0,0.5,0}\downarrow}
GEAR Kang et al. (2024) - {\color[rgb]{0,0.5,0}\checkmark} - - {\color[rgb]{0,0.5,0}\downarrow} {\color[rgb]{0,0.5,0}\downarrow}
Attention Operation FlashAttention Dao et al. (2022) - - Flash - ×{\color[rgb]{1,0,0}\times} {\color[rgb]{0,0.5,0}\downarrow}
TurboAttention (This Work) - {\color[rgb]{0,0.5,0}\checkmark} Flash + Quantized - {\color[rgb]{0,0.5,0}\downarrow\downarrow} {\color[rgb]{0,0.5,0}\downarrow\downarrow}

2 Background and Motivation

In this section, we first introduce the key aspects of the attention mechanism and state-of-the-art quantization techniques before discussing the motivation and challenges in enabling a quantized execution of attention mechanism.

Refer to caption
Figure 2: High-Level comparison of TurboAttention compared to state-of-the-art KV-cache compression technique combined with FlashAttention. TurboAttention accelerates the attention mechanism by adapting (1) FlashQ which enables KV cache compression and accelerated Matmuls ((2) and (3)) and (4) SAS which enables techniques which allow faster execution of attention by utilizing the tensor cores of GPUs efficiently.

2.1 Attention Mechanism

Multi-head attention (MHA) is a core component of the transformer architecture, with each transformer model consisting of LL stacked layers. Each layer comprises two submodules: a multi-head attention mechanism (MHA) and a feed-forward network (FFN). Given input token embeddings 𝑿n×d\boldsymbol{X}\in\mathbb{R}^{n\times d}, the MHA computes attention in parallel across HH attention heads, defined as:

MHA(𝑿)\displaystyle\text{MHA}\left(\boldsymbol{X}\right) =Concat(𝑯(1),,𝑯(H))𝑾o,\displaystyle=\text{Concat}\left(\boldsymbol{H}^{(1)},...,\boldsymbol{H}^{(H)}\right)\boldsymbol{W}_{o}, (1)
𝑯(h)\displaystyle\boldsymbol{H}^{(h)} =Softmax(𝑸(h)𝑲(h)dH)𝑽(h)\displaystyle=\text{Softmax}\left(\frac{\boldsymbol{Q}^{(h)}\boldsymbol{K}^{(h)\top}}{\sqrt{d_{H}}}\right)\boldsymbol{V}^{(h)}

where 𝑸(h)=𝑿𝑾qh,𝑲(h)=𝑿𝑾kh,𝑽(h)=𝑿𝑾vh\boldsymbol{Q}^{(h)}=\boldsymbol{X}\boldsymbol{W}_{q_{h}},\boldsymbol{K}^{(h)}=\boldsymbol{X}\boldsymbol{W}_{k_{h}},\boldsymbol{V}^{(h)}=\boldsymbol{X}\boldsymbol{W}_{v_{h}} are the Query, Key, and Value matrices for head hh. The projection matrices 𝑾qh,𝑾kh,𝑾vhd×dH\boldsymbol{W}_{q_{h}},\boldsymbol{W}_{k_{h}},\boldsymbol{W}_{v_{h}}\in\mathbb{R}^{d\times d_{H}} map the input embedding 𝑿\boldsymbol{X} to the respective attention components, and dHd_{H} is typically set to d/Hd/H to balance the dimensionality across heads.

Prefill and decoding. During generation, let the model generate ngn_{g} tokens. In the initial step, the input tokens 𝑿0n×d\boldsymbol{X}_{0}\in\mathbb{R}^{n\times d} are prefilled, and the Keys and Values (𝑲,𝑽\boldsymbol{K},\boldsymbol{V}) for each head and each layer are cached for future use. This prefill stage results in the KV caches: 𝑲0=Concat(𝑲(1),,𝑲(H))\boldsymbol{K}_{0}=\textrm{Concat}(\boldsymbol{K}^{(1)},\dots,\boldsymbol{K}^{(H)}) and 𝑽0=Concat(𝑽(1),,𝑽(H))\boldsymbol{V}_{0}=\textrm{Concat}(\boldsymbol{V}^{(1)},\dots,\boldsymbol{V}^{(H)}), where 𝑲0,𝑽0n×d\boldsymbol{K}_{0},\boldsymbol{V}_{0}\in\mathbb{R}^{n\times d}.

At each step tt (1tng1\leq t\leq n_{g}) of autoregressive decoding, the model generates a new token 𝒙t\boldsymbol{x}_{t}, conditioned on the input and previously generated tokens. For the subsequent steps, multi-head attention (MHA) only computes the Query/Key/Value vectors (𝒒t,𝒌t,𝒗td\boldsymbol{q}_{t},\boldsymbol{k}_{t},\boldsymbol{v}_{t}\in\mathbb{R}^{d}) for the newly generated token 𝒙t\boldsymbol{x}_{t}. These vectors are then appended to the KV cache: 𝑲t=𝑲t1|𝒌t\boldsymbol{K}_{t}=\boldsymbol{K}_{t-1}|\boldsymbol{k}_{t} and 𝑽t=𝑽t1|𝒗t\boldsymbol{V}_{t}=\boldsymbol{V}_{t-1}|\boldsymbol{v}_{t}. The attention mechanism is then performed between 𝒒t\boldsymbol{q}_{t} and the updated KV cache, i.e., 𝑲t\boldsymbol{K}_{t} and 𝑽t\boldsymbol{V}_{t}.

2.2 Attention Acceleration Techniques

The query matrix QRNq×dQ\in R^{N_{q}\times d} and key and value matrices KK, VV RNk×d\in R^{N_{k}\times d} are inputs to the following equation which is computed independently for each head and batch instance. The output matrix HRNq×dH\in R^{N_{q}\times d} is obtained in essentially three steps as shown in Equation 2.

S=QK, P=softmax(Sd), H=PV.S=QK^{\top},\quad\text{ }P=\operatorname{softmax}\left(\frac{S}{\sqrt{d}}\right),\quad\text{ }H=PV\,. (2)

These intermediate activations SS and PP are large and thus place a significant demand on memory bandwidth. FlashAttention (Dao et al., 2022) addresses this by chunking the Query, Key, and Value matrices along the token dimension and tiling the attention mechanism to accelerate the process as described below.

Flash Attention. FlashAttention integrates the online softmax algorithm Milakov & Gimelshein (2018) to fuse the three operations illustrated in Equation 2. It requires only a single pass over an entire row of tokens to compute attention. This method partitions the attention output matrix HH into independent output tiles, with each tile’s computation being independent of the others. This approach eliminates the need for intermediate global memory reads and writes of SS and PP tensors. FlashAttention employs a tiling strategy, where small chunks of queryquery, keykey, and valuevalue are transferred to the compute cores to first perform “partial attention” for each pair of chunks. This is followed by normalization at the end with the help of additional values (sum and max).

Additionally, optimization techniques such as Ring Attention Liu et al. (2023a), Striped Attention Brandon et al. (2023), and Lean Attention Sanovar et al. (2024) aim to balance computation across GPU resources while using FlashAttention’s core algorithm. These techniques significantly reduce the memory bandwidth overhead during inference, especially for long-context generations. However, the tiled dataflow of FlashAttention’s core algorithm, as shown in Figure 2, involving three activation tensors, makes it unsuitable for direct application of state-of-the-art quantization techniques. The quantization techniques do not address the tiled nature of queryquery, keykey, and valuevalue tensors, thus, techniques which utilize token-wise or channel-wise quantization scale and min cannot be implemented directly without loss of efficiency. On the other hand, these quantization techniques can be applied ot vanilla attention algorithm, but they have the overhead of storing the large intermediate tensors resulting in high memory bandwidth demand.

Further, to avoid overflow, the exponentiation (in partial-attention calculation) in FlashAttention is performed in FP32 data format (exponentiation in Figure 2(a)), while the matrix multiplications are performed in FP16 using GPU tensor cores. Current generation GPUs are inefficient in handling FP32 and do not make use of tensor cores, resulting in higher latency and inefficiency in the attention operation (FP32 CUDA cores deliver only \sim3% of the performance of FP16 Tensor Cores). The usage of FP16 and FP32 also adds pressure to the scratchpad and register file on the compute units of GPU.

2.3 Progressive Quantization

Progressive quantization (PQ) uses a combination of INT8 and INT4 representations progressively, combining the computational efficiency of symmetric quantization with the ability of asymmetric quantization to represent values accurately at INT4. An overview of PQ follows.

Quantization maps high-precision floating-point values (xx) into discrete levels (Q(x)Q(x)), expressed as:

Q(x)=xzs,x^=Q(x)s+z,Q(x)=\left\lceil\frac{x-z}{s}\right\rfloor,\quad\hat{x}=Q(x)\cdot s+z\,, (3)

where ss is a scaling factor and zz is a zero-point. These depend on the quantization type, symmetric (sym.) or asymmetric (asym.):

s={xmaxxmin2bit1,sym.max|x|2bit12,asym.,z={0,sym.xmin,asym.s=\begin{cases}\displaystyle\frac{x_{\operatorname{max}}-x_{\operatorname{min}}}{2^{\text{bit}}-1},&\text{sym.}\\ \displaystyle\frac{\max\left|x\right|}{2^{\text{bit}-1}-2},&\text{asym.}\end{cases},\quad z=\begin{cases}\displaystyle 0,&\text{sym.}\\ \displaystyle x_{\text{min}},&\text{asym.}\end{cases} (4)

For two quantized matrices A^\hat{A} and B^\hat{B}, the low-bit integer matrix multiplication is:

Oij\displaystyle O_{ij} kAik^Bkj^=sasbkQ(Aik)Q(Bkj)\displaystyle\approx\sum_{k}\hat{A_{ik}}\hat{B_{kj}}=s_{a}s_{b}\sum_{k}Q(A_{ik})Q(B_{kj}) (5)
+sazbkQ(Aik)+sbzakQ(Bkj)+zazb.\displaystyle+s_{a}z_{b}\sum_{k}Q(A_{ik})+s_{b}z_{a}\sum_{k}Q(B_{kj})+z_{a}z_{b}\,.

We see that asymmetric quantization introduces computation (the last three terms) that are not necessary in symmetric computation (where z=0z=0). In general, asymetric quantization leads to lower error rates but results in higher computational overhead.

Progressive quantization proposes two stage of quantization to solve this problem. First, symmetric quantization is used to build INT8 representations for computational efficiency, avoiding the extra overhead of asymmetric quantization (the three non-zero terms above). Then, for better memory efficiency, 8-bit integers are further compressed to INT4 using asymmetric quantization.

The integer inference formula for PQ is:

Oij=sasbkQ^(Aik)Q^(Bkj),O_{ij}=s_{a}s_{b}\sum_{k}\hat{Q}({A_{ik}})\hat{Q}({B_{kj}})\,, (6)

where Q^(A)\hat{Q}(A) and Q^(B)\hat{Q}(B) denote:

Q^(A)\displaystyle\hat{Q}(A) =Q(Q(x))saint+zaint,\displaystyle=Q(Q(x))\cdot s_{a}^{\operatorname{int}}+z_{a}^{\operatorname{int}}\,, (7)
Q^(B)\displaystyle\hat{Q}(B) =Q(Q(x))sbint+zbint.\displaystyle=Q(Q(x))\cdot s_{b}^{\operatorname{int}}+z_{b}^{\operatorname{int}}\,. (8)

Here, sas_{a} and sbs_{b} are the (FP16) scales from the first INT8 quantization step. Equations 7 and 8 represent dequantization from asymmetric INT4 quantization into INT8. The scales sints^{\operatorname{int}} and zero points zintz^{\operatorname{int}} are stored in INT8, but the majority of the data Q(Q(x))Q(Q(x)) are stored in INT4.

This approach, proposed in Qserve (Lin et al., 2024) for weight quantization, is more hardware-friendly compared to decompression or other low-bit integer inference methods, such as those in Atom (Zhao et al., 2024), LLM.int8 (Dettmers et al., 2022), or TensorRT-LLM . In our work, we extend the design of PQ to represent KV cache, with the objective of making the dequantization into the attention computations faster while keeping it compatible to attention acceleration mechanisms (such as FlashAttention).

Refer to caption

Figure 3: Dataflow of TurboAttention in pre-fill and decode. At pre-fill (left) we first compress QKV block-wise into INT8(Step1), and compute the attention matrix (on-line) using SAS (see section 4, Step2). Next, we compress the INT8 KV blocks into asymmetric INT4/INT2, channel-wise, in integer arithmetic: these are stored in the cache(Step3). At decoding (right), we first compress generated qkv to INT8(Step1) and decompress the KV cache to INT8 for integer inference(Step2). Again, we use SAS to compute attention(Step3).

2.4 Challenges in Efficient Attention Mechanism

As we saw in 1(a), the attention operation can constitute up to 80% of the overall inference execution time when combined with state-of-the-art quantization techniques for other parts of transformer. This is majorly due to the high-precision execution of attention operation in state-of-the-art techniques, such as FlashAttention. As we saw in subsection 2.2, the usage of FP16 and FP32 not only adds pressure to the scratchpad and register file on the compute units of GPU, but slows down the matrix multiplications and softmax operation inside the attention operation because of the higher precision. For example, the peak performance of FP32 CUDA cores used in FlashAttention’s exponentiation operation is only \sim3% of the FP16 tensor performance delivered by Nvidia’s A100-SXM. Thus, there is a need to enable quantized execution of the attention operation to leverage the faster low-precision tensor cores in modern GPUs. However, a naive quantization of the attention operation could lead to a loss of the model’s generative performance (accuracy).

On the other hand, compression techniques such as KIVI (Zirui Liu et al., 2023), GEAR (Kang et al., 2024), and Qserve (Lin et al., 2024) have proposed techniques to compress the KV cache to 4-bit or even 2-bit formats. However, these techniques aim to reduce the memory footprint of KV cache, but rely on time-intensive decompression to FP16 before executing the attention mechanism using acceleration mechanisms such as FlashAttention.

Additionally, these methods frequently employ dynamic reordering of outlier channels or smooth attention Lin et al. (2024) techniques to adjust key-query interactions, aiming to mitigate outliers Dettmers et al. (2022). However, these strategies introduce additional latency, hindering the adoption of integer inference within the attention mechanism. Specifically, the smoothing factor(Xiao et al., 2024b) that transfers outliers from key to query is not applicable in cases where both tensors are quantized activations. Similarly, dynamic reordering requires synchronized reordering of query, key, and value components, reducing its overall effectiveness. Thus, there is a huge need to develop a quantization technique that will enable a quantized execution of the attention operation in a FlashAttention compatible manner and reduce the memory footprint of KV cache while not losing the accuracy of LLMs.

TurboAttention thus presents a novel unified technique for enabling quantized execution of attention along with a cooperative KV cache compression mechanism which reduces latency, memory footprint, incurs negligible accuracy loss. We present the in two parts; FlashQ (section 3), which enables quantization of Key, Value, and Query in a FlashAttention compatible manner and SAS, which enables efficient GPU tensor core-friendly, softmax computation (section 4).

3 FlashQ: Headwise mix-precision progressive quantization

FlashQ involves three key components that enable and significantly accelerate the quantized attention mechanism:

  1. 1.

    Blockwise Progressive Quantization. FlashAttention-compatible quantization of Key and Value for quantized execution of MatMuls.

  2. 2.

    Head-wise Mixed Precision. Compress different heads to different formats (2-bit and 4-bit) for maximum compression.

  3. 3.

    Enhanced KV cache Buffer. For dynamic management of the quantized KV cache during the decode phase, eliminating the need for recompression of KV cache.

Figure 3illustrates the overall flow of FlashQ along with SAS (discussed later in section 4), integrated with the flash attention mechanism.

3.1 Blockwise Progressive Quantization (BPQ)

As we saw in subsection 2.1, multi-head attention generates large intermediate activations, which place a significant demand on memory bandwidth. FlashAttention (Dao et al., 2022) addresses this by chunking the Query, Key, and Value matrices along the token dimension and tiling the attention mechanism to accelerate the process. FlashAttention divides the hh-th head’s query, key, and value matrices (𝑸(h)\boldsymbol{Q}^{(h)}, 𝑲(h)\boldsymbol{K}^{(h)}, and 𝑽(h)\boldsymbol{V}^{(h)}) into sub-blocks of size TcT_{c} and TrT_{r}, denoted as 𝑸c(h)\boldsymbol{Q}^{(h)}_{c}, 𝑲r(h)\boldsymbol{K}^{(h)}_{r}, and 𝑽r(h)\boldsymbol{V}^{(h)}_{r}, for efficient tiled computation.

To enable a FlashAttention-compatible quantized execution of attention we propose Blockwise Progressive Quantization (BPQ). In BPQ, each of the sub-block is first compressed using 8-bit symmetric quantization. Unlike Qserve, which applies per-channel PQ to weights, we apply BPQ at a sub-block granularity via the function:

Xq1=Quantsym8(X)X^{q1}=Quant^{8}_{sym}(X) (9)

where 𝐗𝑸c(h),𝑲r(h),𝑽r(h)\mathbf{X}\in{\boldsymbol{Q}^{(h)}_{c},\boldsymbol{K}^{(h)}_{r},\boldsymbol{V}^{(h)}_{r}} represents a sub-block, and Quantsym8\text{Quant}_{\text{sym}}^{8} denotes symmetric 8-bit quantization. Given the critical memory bottleneck posed by the KV cache, as described in subsection 2.4, we apply progressive quantization after computation to further compress the Key and Value tensors for storage.

Further, building on the techniques from KIVI (Liu et al., 2024) and informed by our own analysis (Figure 4), we further reduce quantization errors by compressing the Key and Value 8-bit tensors in a channel-wise manner using asymmetric quantization:

Kgq2=Quantasym4 / 2(Kgq1),Vgq2=Quantasym4 / 2(Vgq1)K^{q2}_{g}=Quant^{\text{4 / 2}}_{asym}(K^{q1}_{g}),V^{q2}_{g}=Quant^{\text{4 / 2}}_{asym}(V^{q1}_{g}) (10)

where K^gq1\hat{K}^{q1}_{g} and V^gq1\hat{V}^{q1}_{g} are groups in each channel of the key and value 8-bit tensors. This minimizes the errors caused by outliers in certain channels while keeping the tiled nature needed to make it compatible with FlashAttention’s core algorithm. Since a new query vector is generated at each decoding step, compressing it further beyond the initial 8-bit quantization is unnecessary.

3.2 Head-wise Mixed Precision

To further enhance progressive quantization and achieve substantial memory savings in the KV cache, we explore reducing the bit-width to 2-bit. Although 4-bit KV cache quantization has shown near-lossless performance across various models, as demonstrated by Atom (Zhao et al., 2024), QuaRot (Ashkboos et al., 2024), and Qserve (Lin et al., 2024), uniformly applying 2-bit compression to all attention heads often leads to significant model performance degradation. Approaches like smooth factors (Lin et al., 2024; Xiao et al., 2024a) and offline reordering (e.g., Qserve and Atom) can improve compression accuracy but introduce additional latency overhead, making them challenging to integrate with dynamically expanding KV caches. While token-wise mixed precision could further enhance accuracy, it incurs dynamic execution overhead in CUDA kernels and is not FlashAttention-compatible, thus severely reducing hardware efficiency.

Refer to caption
Figure 4: Query, key and value channels min-max distribution of Phi3-mini and LLaMA3-8B Models. We observe that certain heads in query and key have a number of large-magnitude channels. For value, there is no obvious outlier pattern.

Inspired by recent work on head pruning in multi-head attention (Ge et al., 2024; Liu et al., 2023b; Rajput et al., 2024), we propose a headwise mixed-precision approach. This approach combines 2-bit and 4-bit compression, and thus achieves a favorable trade-off between accuracy and memory savings. It applies 2-bit and 4-bit BPQ for different heads based on their priority. The priority is calculated by:

priority(h)=gap(h)×std(h)\text{priority}^{(h)}=\text{gap}^{(h)}\times\text{std}^{(h)} (11)

Here, gap(h)\text{gap}^{(h)} represents the difference between the maximum and minimum values across all channels for head hh, which captures the range of values in the attention mechanism. A larger gap indicates that compressing this head may introduce a larger quantization error, thus making it more important to preserve precision. std(h)\text{std}^{(h)} is the standard deviation of the channel-wise gaps within each head. This measures the variability of the gaps, where higher variability (i.e., a larger standard deviation) implies that the head’s distribution is more uneven. Heads with a higher std(h)\text{std}^{(h)} are more sensitive to quantization, and thus require higher bit precision to minimize performance degradation.

Using this metric, we rank all heads in terms of their priority scores. Instead of using a fixed threshold, we compress the lowest-priority nhn_{h} heads in each layer to 2-bit, while the remaining heads are quantized to 4-bit. The quantization strategy is thus defined as:

Quantization strategy={2-bit,OPENif rank(priority(h))nh4-bit,OPENif rank(priority(h))>nh\text{Quantization strategy}=\begin{cases}\text{2-bit,}&\text{if rank(priority}^{(h)})\leq n_{h}\\ \text{4-bit,}&\text{if rank(priority}^{(h)})>n_{h}\\ \end{cases} (12)

Here, nhn_{h} is the number of heads to be compressed to 2-bit, determined per layer. By ranking the heads based on their priority and applying lower-bit quantization to the least critical heads (i.e., those with the smallest priority), we achieve a balanced trade-off between memory savings and model performance. Figure 3 illustrates the head-wise quantization dataflow of FlashQ, where the second head is block-wise progressively quantized to INT2. As we will see later, this technique presents a robust way to compress the KV-cache and can be employed in a majority of models easily.

3.3 Enhanced KV cache Buffer

FlashQ introduces an efficient decoding buffer to accelerate inference during long-context generation. To support integer inference in the attention mechanism, newly generated tokens are temporarily stored in a buffer \mathcal{B} of size nbn_{b} (e.g., nb=64n_{b}=64) and quantized using 8-bit symmetric quantization (Equation 9). This avoids the need for compression at every decoding iteration. Once the buffer reaches its capacity, FlashQ applies progressive quantization (Equation 10) to further compress the keys and values from 8 bits to lower bit-widths for improved memory efficiency.

To ensure stability, we employ a universal scale for 8-bit quantization and clamp outliers that exceed this range, preventing the need to re-compress previously stored tokens in the buffer when new tokens with larger values are added. This approach contrasts with previous methods like KIVI (Liu et al., 2024) and GEAR (Kang et al., 2024), which keep buffers in full precision, introducing significant memory overhead and preventing the use of integer inference in the attention mechanism. We summarize the detailed algorithm of TurboAttention in Appendix A.

4 SAS: Sparse Activated Softmax

Softmax is a key bottleneck in the attention mechanism, particularly in flash attention workflows, where tiling operations introduce additional overhead. From our experiments, we observe that softmax computation costs over 30% of the attention execution time both in prefill and decoding stages. The primary performance issue stems from frequent data type conversions between FP16 and FP32 for performing the exponentiation operation. Current GPUs are limited to single-precision floating-point (FP32) operations for exponentiation, which exacerbates this bottleneck. Previous methods, such as (Vasyltsov & Chang, 2021) and (Shim et al., 2017), either introduce significant errors that degrade performance or are unsuitable for modern large language models.

To mitigate these issues, we propose a softmax approximation technique based on polynomial approximation(POLY) and lookup tables(LUT), called Sparse Activated Softmax (SAS). This method divides the exponential computation into two parts: the integer and decimal components. For example, an exponent computation can be separated into integer xintx_{int} and decimal xdecx_{dec} parts which lie in [0,1) (Detailed algorithm in Appendix B). For the integer part, we use a lookup table, which remains compact because large values in the exponential function decay quickly, allowing us to skip larger integers. For the decimal part, we employ a simple polynomial approximation.

ex=exint×exdecLUT(xint)×POLY(xdec)e^{-x}=e^{-x_{int}}\times e^{-x_{dec}}\approx LUT(-x_{int})\times POLY(-x_{dec}) (13)

As such, the approximation algorithm of SAS goes:

SAS(x)={0,x<nrLUT(xint)×POLY(xdec),xnrSAS(x)=\begin{cases}\displaystyle 0,&\text{$x<n_{r}$}\\ \displaystyle LUT(x_{int})\times POLY(x_{dec}),&\text{$x\geq n_{r}$}\end{cases} (14)

Refer to caption

Figure 5: Polynomial-fit for the decimal part of value in exponentiation operation.

To optimize the computation of the exponential function exe^{-x}, we limit the approximation range to [0,1][0,1] and reduce the polynomial degree to less than 3 to enhance computational efficiency. Using the least squares method to determine the coefficients, our 3-degree polynomial approximation of exe^{-x} within this range is as follows:

POLY(x)=0.1025x3+0.4626x20.9922x+0.9996\displaystyle POLY(x)=-0.1025\,x^{3}+0.4626\,x^{2}-0.9922\,x+0.9996 (15)

This polynomial captures the essential behavior of exe^{-x} over [0,1][0,1] with minimal computational overhead, as shown in Figure 5. This hybrid approach reduces computational and data type conversion overhead while preserving accuracy, effectively alleviating the exponentiation bottleneck in flash attention algorithm. The overall exponentiation operation is accelerated, as the polynomial operation can be done solely in GPU Tensor Cores in FP16 dataformat.

Further, from our observations, the large negative values of the attention scores (resulting from the query-key multiplication) become extremely small after softmax due to the nature of the exponential function. As such, we apply a sparsification strategy, retaining only the larger attention scores within a certain range nrn_{r}(e.g., 0 to -5) and setting the rest to zero after the sparse activated softmax computation to keep the look-up table small enough. This further reduces the computational cost, making SAS a practical and efficient solution for large-scale language models.

5 Evaluations

Model LLaMA3-8B-inst Qwen2-7B-inst Phi3-3.8B-inst All
Method Bit GSM8k AQuA BBH GSM8k AQuA BBH GSM8k AQuA BBH Ave.
bb Acc Acc Acc Acc Acc Acc Acc Acc Acc Acc
FP16 16 78.24 50.79 58.71 71.87 45.67 50.71 84.53 58.66 57.83 61.89
KIVIg=64,nb=64g=64,n_{b}=64 4 61.18 46.46 53.20 52.16 45.28 45.33 57.09 54.72 51.24 51.85
GEAR-Lr=4(KCVT){\textrm{GEAR-L}}_{r=4}^{(\textrm{KCVT})} 4 64.94 47.09 57.14 54.46 46.06 46.41 79.86 54.36 53.40 55.97
TurboAttentionnb=64n_{b}=64 4 78.31 48.03 58.63 66.19 46.06 46.86 84.00 55.91 58.43 60.27
KIVIg=64,nb=64g=64,n_{b}=64 3 59.43 44.88 51.40 52.38 41.73 46.47 55.57 47.24 50.98 50.01
GEAR-Lr=4(KIVI){\textrm{GEAR-L}}_{r=4}^{(\textrm{KIVI})} 3 62.06 46.12 52.72 53.15 42.03 46.65 56.18 49.60 51.39 51.10
TurboAttentionnb=64(mixed){\textrm{\bf TurboAttention}}_{n_{b}=64}^{(\textrm{mixed})} 2/4 77.53 47.94 54.36 66.56 41.91 45.13 63.53 31.49 51.32 53.31
Table 2: Results on CoT reasoning tasks, which are hard generative tasks. Here, Bit represents the average compressed bit of KV cache for different methods. The best results are shown in bold. Mixed precision results have same KV cache size with 3-bit simulated benchmark.

5.1 Experiment Setup

Models and dataset We conducted an extensive evaluation of TurboAttention for generative inference using a variety of open-source pre-trained and instruction-tuned models, including LLaMA3-8B-inst (Dubey et al., 2024), Phi3-mini-inst (Abdin et al., 2024), and Qwen2.5-7B (Yang et al., 2024), on multiple generative tasks. These tasks included mathematical reasoning datasets such as GSM8k (Cobbe et al., 2021) and AQuA (Ling et al., 2017), symbolic reasoning tasks from BigBench Hard (BBH) (Suzgun et al., 2022). Given the complexity of these tasks, we use the chain-of-thought prompts created by (Fu et al., 2023) to improve reasoning, which contains 8-shot demonstrations of multi-step reasoning. With the CoT demonstrations, we have the average prefill length of GSM8k, AQuA, and BBH as 900, 1304, 1021 respectively. We then prompt the model to generate 256 tokens and extract answers from them.

5.2 TurboAttention Implementation

To minimize overheads, we implemented and optimized TurboAttention with OpenAI Triton(Tillet et al., 2019) kernel support as follows. First, to address the memory bottlenecks caused by frequent quantization and dequantization, we fused the QKV projection with quantization (Equation 9). Additionally, efficient dequantization from progressive quantization was integrated directly into the attention mechanism to reduce decoding latency (Equation 10).

Second, we implement a KV cache buffer (subsection 3.3 for the newly generated Key and Value vectors during decode. These Key/Value vectors are further compressed to INT4 and INT2 (depending on the head) every nbn_{b} steps. To avoid recompression when newly generated KV tokens exceed the previous cache value range maximum, we clamp outliers to ensure only newly generated tokens are compressed to INT8. This further improves decoding efficiency by reducing unnecessary recompression.

Third, we leveraged the Triton framework to implement INT8 matrix multiplications for the attention mechanism, thereby reducing both memory and computation bottlenecks. Finally, we implemented an efficient polynomial approximation and lookup table for SAS, significantly enhancing the overall performance of the softmax operation. For TurboAttention, we fix block size BcB_{c}, BrB_{r}, and nbn_{b} to 64, SAS threshold nrn_{r} to -6, and set half of the heads’ KV cache to 2-bit quantization for head-wise mixed precision. Further optimization can be achieved by implementing CUDA kernels, as they can deliver higher efficiency and substantial speedup.

Framework. For our experiments, we applied TurboAttention and the baseline methods to models available on Huggingface Wolf et al. (2019), with our inference framework implemented in PyTorch Paszke et al. (2019). Since our focus is on attention acceleration, all other parts of the model are maintained in FP16 unless otherwise stated.

Precision. In our experiments, we explore ultra-low precision quantization techniques, reporting performance results for 4-bit, 3-bit, and mixed headwise 2/4-bit quantization of the KV cache. Our findings indicate that TurboAttention achieves near-lossless performance with 4-bit KV cache compression across various tasks and models. However, consistent with findings in other KV cache compression methods, compressing to 2-bit can led to accuracy degradation. To balance accuracy and memory savings, we applied 2-bit precision to 50%50\% of model heads, achieving a practical trade-off. Consequently, we benchmark TurboAttention with mixed precision quantization against other 3-bit baseline methods for a comprehensive comparison in 2.

Additional ablation studies examining the impact of the number of 2-bit heads and block size variations are provided in subsection 5.6, while the results for pure 2-bit KV cache quantization are included in the Appendix.

5.3 Baseline Techniques

We compare TurboAttention against the following state-of-the-art techniques:

\bullet FP16 A dense model where both activations and weights are represented as 16-bit floating-point numbers. This serves as our base dense model without any attention optimizations.

\bullet FlashAttention An attention algorithm applying FlashAttention (Shah et al., 2024) with FP16 computation (and FP32 for exponentiation). This method is "exact" and does not alter accuracy but improves system efficiency by optimizing the attention mechanism.

\bullet KIVI (Liu et al., 2024) A near-lossless KV cache quantization method that compresses the key cache per-channel and the value cache per-token using fine-grained grouping. It stores residual tokens of length nbn_{b} in full precision, and achieving 4-bit KV cache compression at its best accuracy mode.

\bullet GEAR(Kang et al., 2024) A concurrent KV cache quantization method that achieves 2-bit near-lossless compression by leveraging low-rank approximations to reduce quantization errors. Residual tokens of length nbn_{b} are stored in full precision for error compensation. Here we use the efficiency version GEAR-L, that deploy quantiations and low rank approximation together to compress KV cache.

It is important to note that TurboAttention is not just a KV cache compression algorithm but rather a unified approximation algorithm for the attention mechanism. However, we compare it with KV cache compression algorithms to highlight its ability to preserve both performance and efficiency in large language models (LLMs).

5.4 Evaluation Results

Generative performance on reasoning tasks. Table 2 demonstrate that TurboAttention achieves comparable or superior performance to recent KV cache compression algorithms across different models and the challenging reasoning tasks, in both 4-bit and lower-bit compression settings. Despite further compressing the query and approximating the softmax operation, TurboAttention maintains near lossless accuracy, achieving an average accuracy of 60.27% across three models and datasets, which is closely aligned with the FP16 baseline average accuracy of 61.89%. Notably, TurboAttention also delivers outstanding 3-bit performance, surpassing baseline methods while providing reduced KV cache size, and enhanced inference efficiency.

Refer to caption

Figure 6: Speedup of the attention mechanism of Phi3-medium on 1xA100-80GB-SXM GPU. TurboAttention achieves up to 1.8×\times latency improvement for prefill under various scenarios. For decoding, TurboAttention delivers up to 1.7×\times improvement over FlashAttention in the FP16 data format. Other methods, such as KIVI, may exhibit higher latency than the FP16 baseline due to the time-consuming dequantization process required before computation. OOM denotes out of memory for Phi3-medium model.
Refer to caption
(a) Delivered throughput
Refer to caption
(b) Priority technique vs. Acc.
Figure 7: (a) Throughput comparison running Phi3-Medium on a 1xA100-SXM-80GB GPU. (b) Comparison of different priority methods with different number of 2-bit heads in LLaMA3-8B-inst on AQua dataset

5.5 Efficiency Comparison

In this section, we evaluate latency(wall clock time) and maximum throughputs of TurboAttention on a single A100 GPU(80GB). We measure the attention mechanism of the model under varying batch sizes with a fixed context length of 1k, as well as under different context lengths (ranging from 4k to 32k) with a fixed batch size of 4. We separately assess the prefill and decoding phases and report the speedup ratio compared to the FlashAttention FP16/32 baseline. Figure 6 shows the latency comparison across various input settings and methods, all using the same attention mechanism configuration.

Our results indicate that, across batch sizes from 1 to 64 and context lengths from 4k to 32k, TurboAttention consistently outperforms both FlashAttention FP16 and other KV cache compression techniques combined with Flash Attention baselines. Specifically, TurboAttention achieves a speedup of up to 1.8× for prefill and up to 1.7× for decoding, highlighting significant improvements in softmax, matrix multiplication, and dequantization efficiency. Additionally, by compressing the KV cache to int-4/2, TurboAttention enables long-context generation up to 32k with a batch size of 4, whereas the FP16 baseline encounters out-of-memory (OOM) issues beyond a 4k context length. We also observe that due to the dequantization overhead in other KV cache compression methods, such as KIVI, these methods exhibit worse latency relative to the FP16 baseline.

7(a)shows the throughputs comparison across various batch sizes with fixed context length 1k and generation length 125 tokens. It demonstrats that, compared to the Flash-FP16 baseline, TurboAttention significantly improves maximum throughputs by up to 2.37×2.37\times. Meanwhile, TurboAttention achieves better throughputs compared to KIVI or GEAR due to our efficient progressive dequantization, compressed KV cache, and SAS which improves the latency.

5.6 Ablation Study

Ablation study on head wise selection methodology In this study, we aim to validate the effectiveness of our priority-based head selection strategy as detailed in subsection 3.2. We compare our approach against several baseline methods that use simpler, less adaptive metrics to drive head selection. As explored in subsection 3.2 and illustrated in Figure 4, channelwise outliers critically impact quantization error. To address this, we examine head selection strategies that incorporate channelwise value ranges as the basis for selection.

Baseline methods include: Entropy: Selecting 2-bit heads based on the entropy of each head. Min-Max: Using the range between minimum and maximum values within heads. Variation: Evaluating variation in channelwise value ranges. The benchmarks are run on the LLaMA-3-8B-inst model with 8-shot COT prompts on the AQua dataset. Given LLaMA-3-8B-inst’s 8-head configuration per key/value cache, we perform head selection across a range of 0 to 16 heads. Results, as shown in 7(b), indicate that our prioritized strategy significantly surpasses other selection approaches in minimizing quantization error, supporting its efficiency and robustness in managing outliers.

Ablation study on block size We evaluate the sensitivity of TurboAttention to varying block sizes (BcB_{c} and BrB_{r}) for the query, key, and value matrices. These block sizes are closely related to the device’s SRAM capacity and have a substantial impact on system efficiency. Consequently, assessing TurboAttention’s performance across different block sizes is essential to confirm its robustness and adaptability across configurations. As shown in Table 3 , we tested Phi3-mini with various block sizes on the GSM8K-COT dataset using an 8-shot setup. The results demonstrate that TurboAttention maintains robustness across different block sizes, underscoring its ability to integrate seamlessly with Flash Attention across various systems while preserving model accuracy.

Table 3: TurboAttention on Phi3-mini with different block size of query, key, and value.
Block size(BrB_{r},BcB_{c}) Dataset Acc
(32,32) GSM8K 78.01
(32,64) GSM8K 78.01
(64,32) GSM8K 78.31
(64,64) GSM8K 78.31
(64,128) GSM8K 78.16
(128,64) GSM8K 78.01
(128,128) GSM8K 77.83

6 Conclusions

This paper introduces TurboAttention, a highly efficient compression algorithm for attention mechanisms that integrates seamlessly with attention acceleration methods like FlashAttention, achieving near loss-less performance. TurboAttention demonstrates state-of-the-art results in handling complex generative tasks, including mathematical and reasoning challenges, using 4-bit and 2-bit KV cache compression. Additionally, it provides significant performance gains, with up to 1.8x latency speedup and a 2.37x increase in maximum throughput over the FP16 baseline, marking a substantial advancement in efficient attention mechanism execution.

References

  • Abdin et al. (2024) Abdin, M., Aneja, J., Awadalla, H., Awadallah, A., Awan, A. A., Bach, N., Bahree, A., Bakhtiari, A., Bao, J., Behl, H., Benhaim, A., Bilenko, M., Bjorck, J., Bubeck, S., Cai, M., Cai, Q., Chaudhary, V., Chen, D., Chen, D., Chen, W., Chen, Y.-C., Chen, Y.-L., Cheng, H., Chopra, P., Dai, X., Dixon, M., Eldan, R., Fragoso, V., Gao, J., Gao, M., Gao, M., Garg, A., Giorno, A. D., Goswami, A., Gunasekar, S., Haider, E., Hao, J., Hewett, R. J., Hu, W., Huynh, J., Iter, D., Jacobs, S. A., Javaheripi, M., Jin, X., Karampatziakis, N., Kauffmann, P., Khademi, M., Kim, D., Kim, Y. J., Kurilenko, L., Lee, J. R., Lee, Y. T., Li, Y., Li, Y., Liang, C., Liden, L., Lin, X., Lin, Z., Liu, C., Liu, L., Liu, M., Liu, W., Liu, X., Luo, C., Madan, P., Mahmoudzadeh, A., Majercak, D., Mazzola, M., Mendes, C. C. T., Mitra, A., Modi, H., Nguyen, A., Norick, B., Patra, B., Perez-Becker, D., Portet, T., Pryzant, R., Qin, H., Radmilac, M., Ren, L., de Rosa, G., Rosset, C., Roy, S., Ruwase, O., Saarikivi, O., Saied, A., Salim, A., Santacroce, M., Shah, S., Shang, N., Sharma, H., Shen, Y., Shukla, S., Song, X., Tanaka, M., Tupini, A., Vaddamanu, P., Wang, C., Wang, G., Wang, L., Wang, S., Wang, X., Wang, Y., Ward, R., Wen, W., Witte, P., Wu, H., Wu, X., Wyatt, M., Xiao, B., Xu, C., Xu, J., Xu, W., Xue, J., Yadav, S., Yang, F., Yang, J., Yang, Y., Yang, Z., Yu, D., Yuan, L., Zhang, C., Zhang, C., Zhang, J., Zhang, L. L., Zhang, Y., Zhang, Y., Zhang, Y., and Zhou, X. Phi-3 technical report: A highly capable language model locally on your phone, 2024. URL https://arxiv.org/abs/2404.14219.
  • Ashkboos et al. (2024) Ashkboos, S., Mohtashami, A., Croci, M. L., Li, B., Jaggi, M., Alistarh, D., Hoefler, T., and Hensman, J. Quarot: Outlier-free 4-bit inference in rotated llms, 2024. URL https://arxiv.org/abs/2404.00456.
  • Brandon et al. (2023) Brandon, W., Nrusimha, A., Qian, K., Ankner, Z., Jin, T., Song, Z., and Ragan-Kelley, J. Striped attention: Faster ring attention for causal transformers. arXiv preprint arXiv:2311.09431, 2023.
  • Brown et al. (2020) Brown, T. B., Mann, B., Ryder, N., Subbiah, M., Kaplan, J., Dhariwal, P., Neelakantan, A., Shyam, P., Sastry, G., Askell, A., Agarwal, S., Herbert-Voss, A., Krueger, G., Henighan, T., Child, R., Ramesh, A., Ziegler, D. M., Wu, J., Winter, C., Hesse, C., Chen, M., Sigler, E., Litwin, M., Gray, S., Chess, B., Clark, J., Berner, C., McCandlish, S., Radford, A., Sutskever, I., and Amodei, D. Language models are few-shot learners, 2020. URL https://arxiv.org/abs/2005.14165.
  • Cobbe et al. (2021) Cobbe, K., Kosaraju, V., Bavarian, M., Chen, M., Jun, H., Kaiser, L., Plappert, M., Tworek, J., Hilton, J., Nakano, R., Hesse, C., and Schulman, J. Training verifiers to solve math word problems, 2021.
  • Dao (2023) Dao, T. Flashattention-2: Faster attention with better parallelism and work partitioning, 2023. URL https://arxiv.org/abs/2307.08691.
  • Dao et al. (2022) Dao, T., Fu, D. Y., Ermon, S., Rudra, A., and Ré, C. Flashattention: Fast and memory-efficient exact attention with io-awareness, 2022. URL https://arxiv.org/abs/2205.14135.
  • Dao et al. (2023) Dao, T., Haziza, D., Massa, F., and Sizov, G. Flashdecoding: Stanford CRFM — crfm.stanford.edu. https://crfm.stanford.edu/2023/10/12/flashdecoding.html, 2023. [Accessed 22-04-2024].
  • Dettmers et al. (2022) Dettmers, T., Lewis, M., Belkada, Y., and Zettlemoyer, L. Llm.int8(): 8-bit matrix multiplication for transformers at scale, 2022. URL https://arxiv.org/abs/2208.07339.
  • Dodge et al. (2021) Dodge, J., Sap, M., Marasović, A., Agnew, W., Ilharco, G., Groeneveld, D., Mitchell, M., and Gardner, M. Documenting large webtext corpora: A case study on the colossal clean crawled corpus, 2021. URL https://arxiv.org/abs/2104.08758.
  • Dubey et al. (2024) Dubey, A., Jauhri, A., Pandey, A., Kadian, A., Al-Dahle, A., Letman, A., Mathur, A., Schelten, A., Yang, A., Fan, A., Goyal, A., Hartshorn, A., Yang, A., Mitra, A., Sravankumar, A., Korenev, A., Hinsvark, A., Rao, A., Zhang, A., Rodriguez, A., Gregerson, A., Spataru, A., Roziere, B., Biron, B., Tang, B., Chern, B., Caucheteux, C., Nayak, C., Bi, C., Marra, C., McConnell, C., Keller, C., Touret, C., Wu, C., Wong, C., Ferrer, C. C., Nikolaidis, C., Allonsius, D., Song, D., Pintz, D., Livshits, D., Esiobu, D., Choudhary, D., Mahajan, D., Garcia-Olano, D., Perino, D., Hupkes, D., Lakomkin, E., AlBadawy, E., Lobanova, E., Dinan, E., Smith, E. M., Radenovic, F., Zhang, F., Synnaeve, G., Lee, G., Anderson, G. L., Nail, G., Mialon, G., Pang, G., Cucurell, G., Nguyen, H., Korevaar, H., Xu, H., Touvron, H., Zarov, I., Ibarra, I. A., Kloumann, I., Misra, I., Evtimov, I., Copet, J., Lee, J., Geffert, J., Vranes, J., Park, J., Mahadeokar, J., Shah, J., van der Linde, J., Billock, J., Hong, J., Lee, J., Fu, J., Chi, J., Huang, J., Liu, J., Wang, J., Yu, J., Bitton, J., Spisak, J., Park, J., Rocca, J., Johnstun, J., Saxe, J., Jia, J., Alwala, K. V., Upasani, K., Plawiak, K., Li, K., Heafield, K., Stone, K., El-Arini, K., Iyer, K., Malik, K., Chiu, K., Bhalla, K., Rantala-Yeary, L., van der Maaten, L., Chen, L., Tan, L., Jenkins, L., Martin, L., Madaan, L., Malo, L., Blecher, L., Landzaat, L., de Oliveira, L., Muzzi, M., Pasupuleti, M., Singh, M., Paluri, M., Kardas, M., Oldham, M., Rita, M., Pavlova, M., Kambadur, M., Lewis, M., Si, M., Singh, M. K., Hassan, M., Goyal, N., Torabi, N., Bashlykov, N., Bogoychev, N., Chatterji, N., Duchenne, O., Çelebi, O., Alrassy, P., Zhang, P., Li, P., Vasic, P., Weng, P., Bhargava, P., Dubal, P., Krishnan, P., Koura, P. S., Xu, P., He, Q., Dong, Q., Srinivasan, R., Ganapathy, R., Calderer, R., Cabral, R. S., Stojnic, R., Raileanu, R., Girdhar, R., Patel, R., Sauvestre, R., Polidoro, R., Sumbaly, R., Taylor, R., Silva, R., Hou, R., Wang, R., Hosseini, S., Chennabasappa, S., Singh, S., Bell, S., Kim, S. S., Edunov, S., Nie, S., Narang, S., Raparthy, S., Shen, S., Wan, S., Bhosale, S., Zhang, S., Vandenhende, S., Batra, S., Whitman, S., Sootla, S., Collot, S., Gururangan, S., Borodinsky, S., Herman, T., Fowler, T., Sheasha, T., Georgiou, T., Scialom, T., Speckbacher, T., Mihaylov, T., Xiao, T., Karn, U., Goswami, V., Gupta, V., Ramanathan, V., Kerkez, V., Gonguet, V., Do, V., Vogeti, V., Petrovic, V., Chu, W., Xiong, W., Fu, W., Meers, W., Martinet, X., Wang, X., Tan, X. E., Xie, X., Jia, X., Wang, X., Goldschlag, Y., Gaur, Y., Babaei, Y., Wen, Y., Song, Y., Zhang, Y., Li, Y., Mao, Y., Coudert, Z. D., Yan, Z., Chen, Z., Papakipos, Z., Singh, A., Grattafiori, A., Jain, A., Kelsey, A., Shajnfeld, A., Gangidi, A., Victoria, A., Goldstand, A., Menon, A., Sharma, A., Boesenberg, A., Vaughan, A., Baevski, A., Feinstein, A., Kallet, A., Sangani, A., Yunus, A., Lupu, A., Alvarado, A., Caples, A., Gu, A., Ho, A., Poulton, A., Ryan, A., Ramchandani, A., Franco, A., Saraf, A., Chowdhury, A., Gabriel, A., Bharambe, A., Eisenman, A., Yazdan, A., James, B., Maurer, B., Leonhardi, B., Huang, B., Loyd, B., Paola, B. D., Paranjape, B., Liu, B., Wu, B., Ni, B., Hancock, B., Wasti, B., Spence, B., Stojkovic, B., Gamido, B., Montalvo, B., Parker, C., Burton, C., Mejia, C., Wang, C., Kim, C., Zhou, C., Hu, C., Chu, C.-H., Cai, C., Tindal, C., Feichtenhofer, C., Civin, D., Beaty, D., Kreymer, D., Li, D., Wyatt, D., Adkins, D., Xu, D., Testuggine, D., David, D., Parikh, D., Liskovich, D., Foss, D., Wang, D., Le, D., Holland, D., Dowling, E., Jamil, E., Montgomery, E., Presani, E., Hahn, E., Wood, E., Brinkman, E., Arcaute, E., Dunbar, E., Smothers, E., Sun, F., Kreuk, F., Tian, F., Ozgenel, F., Caggioni, F., Guzmán, F., Kanayet, F., Seide, F., Florez, G. M., Schwarz, G., Badeer, G., Swee, G., Halpern, G., Thattai, G., Herman, G., Sizov, G., Guangyi, Zhang, Lakshminarayanan, G., Shojanazeri, H., Zou, H., Wang, H., Zha, H., Habeeb, H., Rudolph, H., Suk, H., Aspegren, H., Goldman, H., Damlaj, I., Molybog, I., Tufanov, I., Veliche, I.-E., Gat, I., Weissman, J., Geboski, J., Kohli, J., Asher, J., Gaya, J.-B., Marcus, J., Tang, J., Chan, J., Zhen, J., Reizenstein, J., Teboul, J., Zhong, J., Jin, J., Yang, J., Cummings, J., Carvill, J., Shepard, J., McPhie, J., Torres, J., Ginsburg, J., Wang, J., Wu, K., U, K. H., Saxena, K., Prasad, K., Khandelwal, K., Zand, K., Matosich, K., Veeraraghavan, K., Michelena, K., Li, K., Huang, K., Chawla, K., Lakhotia, K., Huang, K., Chen, L., Garg, L., A, L., Silva, L., Bell, L., Zhang, L., Guo, L., Yu, L., Moshkovich, L., Wehrstedt, L., Khabsa, M., Avalani, M., Bhatt, M., Tsimpoukelli, M., Mankus, M., Hasson, M., Lennie, M., Reso, M., Groshev, M., Naumov, M., Lathi, M., Keneally, M., Seltzer, M. L., Valko, M., Restrepo, M., Patel, M., Vyatskov, M., Samvelyan, M., Clark, M., Macey, M., Wang, M., Hermoso, M. J., Metanat, M., Rastegari, M., Bansal, M., Santhanam, N., Parks, N., White, N., Bawa, N., Singhal, N., Egebo, N., Usunier, N., Laptev, N. P., Dong, N., Zhang, N., Cheng, N., Chernoguz, O., Hart, O., Salpekar, O., Kalinli, O., Kent, P., Parekh, P., Saab, P., Balaji, P., Rittner, P., Bontrager, P., Roux, P., Dollar, P., Zvyagina, P., Ratanchandani, P., Yuvraj, P., Liang, Q., Alao, R., Rodriguez, R., Ayub, R., Murthy, R., Nayani, R., Mitra, R., Li, R., Hogan, R., Battey, R., Wang, R., Maheswari, R., Howes, R., Rinott, R., Bondu, S. J., Datta, S., Chugh, S., Hunt, S., Dhillon, S., Sidorov, S., Pan, S., Verma, S., Yamamoto, S., Ramaswamy, S., Lindsay, S., Lindsay, S., Feng, S., Lin, S., Zha, S. C., Shankar, S., Zhang, S., Zhang, S., Wang, S., Agarwal, S., Sajuyigbe, S., Chintala, S., Max, S., Chen, S., Kehoe, S., Satterfield, S., Govindaprasad, S., Gupta, S., Cho, S., Virk, S., Subramanian, S., Choudhury, S., Goldman, S., Remez, T., Glaser, T., Best, T., Kohler, T., Robinson, T., Li, T., Zhang, T., Matthews, T., Chou, T., Shaked, T., Vontimitta, V., Ajayi, V., Montanez, V., Mohan, V., Kumar, V. S., Mangla, V., Albiero, V., Ionescu, V., Poenaru, V., Mihailescu, V. T., Ivanov, V., Li, W., Wang, W., Jiang, W., Bouaziz, W., Constable, W., Tang, X., Wang, X., Wu, X., Wang, X., Xia, X., Wu, X., Gao, X., Chen, Y., Hu, Y., Jia, Y., Qi, Y., Li, Y., Zhang, Y., Zhang, Y., Adi, Y., Nam, Y., Yu, Wang, Hao, Y., Qian, Y., He, Y., Rait, Z., DeVito, Z., Rosnbrick, Z., Wen, Z., Yang, Z., and Zhao, Z. The llama 3 herd of models, 2024. URL https://arxiv.org/abs/2407.21783.
  • Fu et al. (2023) Fu, Y., Ou, L., Chen, M., Wan, Y., Peng, H., and Khot, T. Chain-of-thought hub: A continuous effort to measure large language models’ reasoning performance, 2023.
  • Ge et al. (2024) Ge, S., Zhang, Y., Liu, L., Zhang, M., Han, J., and Gao, J. Model tells you what to discard: Adaptive kv cache compression for llms, 2024. URL https://arxiv.org/abs/2310.01801.
  • Gunasekar et al. (2023) Gunasekar, S., Zhang, Y., Aneja, J., Mendes, C. C. T., Giorno, A. D., Gopi, S., Javaheripi, M., Kauffmann, P., de Rosa, G., Saarikivi, O., Salim, A., Shah, S., Behl, H. S., Wang, X., Bubeck, S., Eldan, R., Kalai, A. T., Lee, Y. T., and Li, Y. Textbooks are all you need, 2023. URL https://arxiv.org/abs/2306.11644.
  • Hendrycks et al. (2021) Hendrycks, D., Burns, C., Kadavath, S., Arora, A., Basart, S., Tang, E., Song, D., and Steinhardt, J. Measuring mathematical problem solving with the math dataset, 2021. URL https://arxiv.org/abs/2103.03874.
  • Hong et al. (2024) Hong, K., Dai, G., Xu, J., Mao, Q., Li, X., Liu, J., Dong, Y., Wang, Y., et al. Flashdecoding++: Faster large language model inference with asynchronization, flat gemm optimization, and heuristics. Proceedings of Machine Learning and Systems, 6:148–161, 2024.
  • Joshi et al. (2017) Joshi, M., Choi, E., Weld, D. S., and Zettlemoyer, L. Triviaqa: A large scale distantly supervised challenge dataset for reading comprehension, 2017. URL https://arxiv.org/abs/1705.03551.
  • Kang et al. (2024) Kang, H., Zhang, Q., Kundu, S., Jeong, G., Liu, Z., Krishna, T., and Zhao, T. Gear: An efficient kv cache compression recipe for near-lossless generative inference of llm, 2024.
  • Langley (2000) Langley, P. Crafting papers on machine learning. In Langley, P. (ed.), Proceedings of the 17th International Conference on Machine Learning (ICML 2000), pp. 1207–1216, Stanford, CA, 2000. Morgan Kaufmann.
  • Lin et al. (2024) Lin, Y., Tang, H., Yang, S., Zhang, Z., Xiao, G., Gan, C., and Han, S. Qserve: W4a8kv4 quantization and system co-design for efficient llm serving, 2024. URL https://arxiv.org/abs/2405.04532.
  • Ling et al. (2017) Ling, W., Yogatama, D., Dyer, C., and Blunsom, P. Program induction by rationale generation: Learning to solve and explain algebraic word problems. ACL, 2017.
  • Liu et al. (2023a) Liu, H., Zaharia, M., and Abbeel, P. Ring attention with blockwise transformers for near-infinite context. arXiv preprint arXiv:2310.01889, 2023a.
  • Liu et al. (2023b) Liu, Z., Wang, J., Dao, T., Zhou, T., Yuan, B., Song, Z., Shrivastava, A., Zhang, C., Tian, Y., Re, C., and Chen, B. Deja vu: Contextual sparsity for efficient llms at inference time, 2023b. URL https://arxiv.org/abs/2310.17157.
  • Liu et al. (2024) Liu, Z., Yuan, J., Jin, H., Zhong, S., Xu, Z., Braverman, V., Chen, B., and Hu, X. Kivi: A tuning-free asymmetric 2bit quantization for kv cache. arXiv preprint arXiv:2402.02750, 2024.
  • Milakov & Gimelshein (2018) Milakov, M. and Gimelshein, N. Online normalizer calculation for softmax. arXiv preprint arXiv:1805.02867, 2018.
  • Paszke et al. (2019) Paszke, A., Gross, S., Massa, F., Lerer, A., Bradbury, J., Chanan, G., Killeen, T., Lin, Z., Gimelshein, N., Antiga, L., Desmaison, A., Köpf, A., Yang, E., DeVito, Z., Raison, M., Tejani, A., Chilamkurthy, S., Steiner, B., Fang, L., Bai, J., and Chintala, S. Pytorch: An imperative style, high-performance deep learning library. In Wallach, H. M., Larochelle, H., Beygelzimer, A., d’Alché-Buc, F., Fox, E. B., and Garnett, R. (eds.), Advances in Neural Information Processing Systems 32: Annual Conference on Neural Information Processing Systems 2019, NeurIPS 2019, December 8-14, 2019, Vancouver, BC, Canada, pp. 8024–8035, 2019.
  • Rajput et al. (2024) Rajput, S., Sheng, Y., Owen, S., and Chiley, V. Inference-friendly models with mixattention, 2024. URL https://arxiv.org/abs/2409.15012.
  • Sanovar et al. (2024) Sanovar, R., Bharadwaj, S., Amant, R. S., Rühle, V., and Rajmohan, S. Lean attention: Hardware-aware scalable attention mechanism for the decode-phase of transformers, 2024. URL https://arxiv.org/abs/2405.10480.
  • Shah et al. (2024) Shah, J., Bikshandi, G., Zhang, Y., Thakkar, V., Ramani, P., and Dao, T. Flashattention-3: Fast and accurate attention with asynchrony and low-precision, 2024. URL https://arxiv.org/abs/2407.08608.
  • Shim et al. (2017) Shim, K., Lee, M., Choi, I., Boo, Y., and Sung, W. Svd-softmax: Fast softmax approximation on large vocabulary neural networks. In Guyon, I., Luxburg, U. V., Bengio, S., Wallach, H., Fergus, R., Vishwanathan, S., and Garnett, R. (eds.), Advances in Neural Information Processing Systems, volume 30. Curran Associates, Inc., 2017. URL https://proceedings.neurips.cc/paper_files/paper/2017/file/4e2a6330465c8ffcaa696a5a16639176-Paper.pdf.
  • Suzgun et al. (2022) Suzgun, M., Scales, N., Schärli, N., Gehrmann, S., Tay, Y., Chung, H. W., Chowdhery, A., Le, Q. V., Chi, E. H., Zhou, D., and Wei, J. Challenging big-bench tasks and whether chain-of-thought can solve them, 2022.
  • Tillet et al. (2019) Tillet, P., Kung, H.-T., and Cox, D. D. Triton: an intermediate language and compiler for tiled neural network computations. Proceedings of the 3rd ACM SIGPLAN International Workshop on Machine Learning and Programming Languages, 2019. URL https://api.semanticscholar.org/CorpusID:184488182.
  • Touvron et al. (2023) Touvron, H., Lavril, T., Izacard, G., Martinet, X., Lachaux, M.-A., Lacroix, T., Rozière, B., Goyal, N., Hambro, E., Azhar, F., Rodriguez, A., Joulin, A., Grave, E., and Lample, G. Llama: Open and efficient foundation language models, 2023. URL https://arxiv.org/abs/2302.13971.
  • Vasyltsov & Chang (2021) Vasyltsov, I. and Chang, W. Efficient softmax approximation for deep neural networks with attention mechanism, 2021. URL https://arxiv.org/abs/2111.10770.
  • Wolf et al. (2019) Wolf, T., Debut, L., Sanh, V., Chaumond, J., Delangue, C., Moi, A., Cistac, P., Rault, T., Louf, R., Funtowicz, M., et al. Huggingface’s transformers: State-of-the-art natural language processing. arXiv preprint arXiv:1910.03771, 2019.
  • Xiao et al. (2024a) Xiao, G., Lin, J., Seznec, M., Wu, H., Demouth, J., and Han, S. Smoothquant: Accurate and efficient post-training quantization for large language models, 2024a. URL https://arxiv.org/abs/2211.10438.
  • Xiao et al. (2024b) Xiao, G., Lin, J., Seznec, M., Wu, H., Demouth, J., and Han, S. Smoothquant: Accurate and efficient post-training quantization for large language models, 2024b. URL https://arxiv.org/abs/2211.10438.
  • Yang et al. (2024) Yang, A., Yang, B., Hui, B., Zheng, B., Yu, B., Zhou, C., Li, C., Li, C., Liu, D., Huang, F., Dong, G., Wei, H., Lin, H., Tang, J., Wang, J., Yang, J., Tu, J., Zhang, J., Ma, J., Yang, J., Xu, J., Zhou, J., Bai, J., He, J., Lin, J., Dang, K., Lu, K., Chen, K., Yang, K., Li, M., Xue, M., Ni, N., Zhang, P., Wang, P., Peng, R., Men, R., Gao, R., Lin, R., Wang, S., Bai, S., Tan, S., Zhu, T., Li, T., Liu, T., Ge, W., Deng, X., Zhou, X., Ren, X., Zhang, X., Wei, X., Ren, X., Liu, X., Fan, Y., Yao, Y., Zhang, Y., Wan, Y., Chu, Y., Liu, Y., Cui, Z., Zhang, Z., Guo, Z., and Fan, Z. Qwen2 technical report, 2024. URL https://arxiv.org/abs/2407.10671.
  • Zhao et al. (2024) Zhao, Y., Lin, C.-Y., Zhu, K., Ye, Z., Chen, L., Zheng, S., Ceze, L., Krishnamurthy, A., Chen, T., and Kasikci, B. Atom: Low-bit quantization for efficient and accurate llm serving, 2024. URL https://arxiv.org/abs/2310.19102.
  • Zhong et al. (2017) Zhong, V., Xiong, C., and Socher, R. Seq2sql: Generating structured queries from natural language using reinforcement learning. CoRR, abs/1709.00103, 2017.
  • Zirui Liu et al. (2023) Zirui Liu, Jiayi Yuan, Hongye Jin, Shaochen Zhong, Zhaozhuo Xu, Braverman, V., Beidi Chen, and Hu, X. Kivi : Plug-and-play 2bit kv cache quantization with streaming asymmetric quantization. 2023. doi: 10.13140/RG.2.2.28167.37282. URL https://rgdoi.net/10.13140/RG.2.2.28167.37282.

Appendix A Detailed Algorithm of TurboAttention

Algorithm 1 TurboAttention Prefill
0:  Matrices 𝐐,𝐊,𝐕N×d\mathbf{Q},\mathbf{K},\mathbf{V}\in\mathbb{R}^{N\times d} in HBM, compressed KV cache 𝐊q2,𝐕q2\mathbf{K}^{q2},\mathbf{V}^{q2},integer scale of KV cache s𝐊q2ints_{\mathbf{K}^{q2}}^{int}, floating scale sKs_{K}, sVs_{V}, s𝐕q2ints_{\mathbf{V}^{q2}}^{int},zero point of KV cache z𝐊q2intz_{\mathbf{K}^{q2}}^{int}, z𝐕q2intz_{\mathbf{V}^{q2}}^{int}, block sizes BcB_{c}, BrB_{r}, Softmax approximate algorithm SAS.
  Divide 𝐐\mathbf{Q} into Tr=NBrT_{r}=\left\lceil\frac{N}{B_{r}}\right\rceil blocks 𝐐1,,𝐐Tr\mathbf{Q}_{1},\dots,\mathbf{Q}_{T_{r}} of size Br×dB_{r}\times d each, and divide 𝐊,𝐕\mathbf{K},\mathbf{V} in to Tc=NBcT_{c}=\left\lceil\frac{N}{B_{c}}\right\rceil blocks 𝐊1,,𝐊Tc\mathbf{K}_{1},\dots,\mathbf{K}_{T_{c}} and 𝐕1,,𝐕Tc\mathbf{V}_{1},\dots,\mathbf{V}_{T_{c}}, of size Bc×dB_{c}\times d each.
 Divide the output 𝐎N×d\mathbf{O}\in\mathbb{R}^{N\times d} into TrT_{r} blocks 𝐎i,,𝐎Tr\mathbf{O}_{i},\dots,\mathbf{O}_{T_{r}} of size Br×dB_{r}\times d each, and divide the logsumexp LL into TrT_{r} blocks Li,,LTrL_{i},\dots,L_{T_{r}} of size BrB_{r} each.
for 1iTr1\leq i\leq T_{r} do
   Load 𝐐i\mathbf{Q}_{i} from HBM to on-chip SRAM.
   On chip, initialize 𝐎i(0)=(0)Br×dBr×d,i(0)=(0)BrBr,mi(0)=()BrBr\mathbf{O}_{i}^{(0)}=(0)_{B_{r}\times d}\in\mathbb{R}^{B_{r}\times d},\ell_{i}^{(0)}=(0)_{B_{r}}\in\mathbb{R}^{B_{r}},m_{i}^{(0)}=(-\infty)_{B_{r}}\in\mathbb{R}^{B_{r}}.
  for 1jTc1\leq j\leq T_{c} do
    Load 𝐊j,𝐕j\mathbf{K}_{j},\mathbf{V}_{j} from HBM to on-chip SRAM.
   On chip, compute s𝐐i=max(abs(𝐐i))119,𝐐iq1=𝐐is𝐐is_{\mathbf{Q}_{i}}=\frac{max(abs(\mathbf{Q}_{i}))}{119},\mathbf{Q}_{i}^{q1}=\left\lceil\frac{\mathbf{Q}_{i}}{s_{\mathbf{Q}_{i}}}\right\rfloor,s𝐊j=max(abs(𝐊j))119,𝐊jq1=𝐊js𝐊j,s_{\mathbf{K}_{j}}=\frac{max(abs(\mathbf{K}_{j}))}{119},\mathbf{K}_{j}^{q1}=\left\lceil\frac{\mathbf{K}_{j}}{s_{\mathbf{K}_{j}}}\right\rfloor,s𝐕j=max(abs(𝐕j))119,𝐕jq1=𝐕js𝐕js_{\mathbf{V}_{j}}=\frac{max(abs(\mathbf{V}_{j}))}{119},\mathbf{V}_{j}^{q1}=\left\lceil\frac{\mathbf{V}_{j}}{s_{\mathbf{V}_{j}}}\right\rfloor.
    On chip, compute 𝐒i(j)=s𝐐is𝐊j𝐐iq1𝐊jq1Br×Bc\mathbf{S}_{i}^{(j)}=s_{\mathbf{Q}_{i}}\cdot s_{\mathbf{K}_{j}}\cdot\mathbf{Q}_{i}^{q1}\mathbf{K}_{j}^{q1}\in\mathbb{R}^{B_{r}\times B_{c}}.
    On chip, compute mi(j)=max(mi(j1),rowmax(𝐒i(j)))Brm_{i}^{(j)}=\mathrm{max}(m_{i}^{(j-1)},\mathrm{rowmax}(\mathbf{S}_{i}^{(j)}))\in\mathbb{R}^{B_{r}}, 𝐏~i(j)=SAS(𝐒i(j)mi(j))Br×Bc\tilde{\mathbf{P}}_{i}^{(j)}=SAS(\mathbf{S}_{i}^{(j)}-m_{i}^{(j)})\in\mathbb{R}^{B_{r}\times B_{c}} (pointwise), i(j)=SAS(mij1mi(j))i(j1)+rowsum(𝐏~i(j))Br\ell_{i}^{(j)}=SAS(m_{i}^{j-1}-m_{i}^{(j)})\ell_{i}^{(j-1)}+\mathrm{rowsum}(\tilde{\mathbf{P}}_{i}^{(j)})\in\mathbb{R}^{B_{r}}.
   On chip, compute s𝐏~i(j)=max(abs(𝐏~i(j))CLOSE119,Q(𝐏~i(j))=𝐏~i(j)s𝐏~i(j)s_{\tilde{\mathbf{P}}_{i}^{(j)}}=\frac{max(abs(\tilde{\mathbf{P}}_{i}^{(j)})}{119},Q(\tilde{\mathbf{P}}_{i}^{(j)})=\left\lceil\frac{\tilde{\mathbf{P}}_{i}^{(j)}}{s_{\tilde{\mathbf{P}}_{i}^{(j)}}}\right\rfloor,
    On chip, compute 𝐎i(j)=diag(SAS(mi(j1)mi(j)))1𝐎i(j1)+s𝐏~i(j)s𝐕jQ(𝐏~i(j))𝐕jq1\mathbf{O}_{i}^{(j)}=\mathrm{diag}(SAS({m_{i}^{(j-1)}-m_{i}^{(j)}}))^{-1}\mathbf{O}_{i}^{(j-1)}+s_{\tilde{\mathbf{P}}_{i}^{(j)}}\cdot s_{\mathbf{V}_{j}}\cdot Q(\tilde{\mathbf{P}}_{i}^{(j)})\mathbf{V}_{j}^{q1}.
   On chip, compute s𝐊jq2int=max(𝐊jq2)min(𝐊jq2)2bit1s_{\mathbf{K}_{j}^{q2}}^{int}=\left\lceil\frac{max(\mathbf{K}_{j}^{q2})-min(\mathbf{K}_{j}^{q2})}{2^{bit}-1}\right\rfloor, z𝐊jq2int=min(𝐊jq2)s𝐊jq2intz_{\mathbf{K}_{j}^{q2}}^{int}=\left\lfloor\frac{min(\mathbf{K}_{j}^{q2})}{s_{\mathbf{K}_{j}^{q2}}^{int}}\right\rfloor, 𝐊jq2=𝐊jq1s𝐊jq2intz𝐊jq2int\mathbf{K}_{j}^{q2}=\left\lceil\frac{\mathbf{K}_{j}^{q1}}{s_{\mathbf{K}_{j}^{q2}}^{int}}-z_{\mathbf{K}_{j}^{q2}}^{int}\right\rfloor(channelwise)
   On chip, compute s𝐕jq2int=max(𝐕jq2)min(𝐕jq2)2bit1s_{\mathbf{V}_{j}^{q2}}^{int}=\left\lceil\frac{max(\mathbf{V}_{j}^{q2})-min(\mathbf{V}_{j}^{q2})}{2^{bit}-1}\right\rfloor, z𝐕jq2int=min(𝐕jq2)s𝐕jq2intz_{\mathbf{V}_{j}^{q2}}^{int}=\left\lfloor\frac{min(\mathbf{V}_{j}^{q2})}{s_{\mathbf{V}_{j}^{q2}}^{int}}\right\rfloor, 𝐕jq2=𝐕jq1s𝐕jq2intz𝐕jq2int\mathbf{V}_{j}^{q2}=\left\lceil\frac{\mathbf{V}_{j}^{q1}}{s_{\mathbf{V}_{j}^{q2}}^{int}}-z_{\mathbf{V}_{j}^{q2}}^{int}\right\rfloor(channelwise)
   Write 𝐊jq2\mathbf{K}_{j}^{q2} to HBM as the jj-th block of 𝐊q2\mathbf{K}^{q2}.
   Write 𝐕jq2\mathbf{V}_{j}^{q2} to HBM as the jj-th block of 𝐕q2\mathbf{V}^{q2}.
   Write s𝐊jq2ints_{\mathbf{K}_{j}^{q2}}^{int},s𝐕jq2ints_{\mathbf{V}_{j}^{q2}}^{int} to HBM as the jj-th block of s𝐊q2ints_{\mathbf{K}^{q2}}^{int}, s𝐕q2ints_{\mathbf{V}^{q2}}^{int}.
   Write s𝐊js_{\mathbf{K}_{j}},s𝐕js_{\mathbf{V}_{j}} to HBM as the jj-th block of s𝐊s_{\mathbf{K}}, s𝐕s_{\mathbf{V}}.
   Write z𝐊jq2intz_{\mathbf{K}_{j}^{q2}}^{int},z𝐕jq2intz_{\mathbf{V}_{j}^{q2}}^{int} to HBM as the jj-th block of z𝐊q2intz_{\mathbf{K}^{q2}}^{int}, z𝐕q2intz_{\mathbf{V}^{q2}}^{int}.
  end for
  On chip, compute 𝐎i=diag(i(Tc))1𝐎i(Tc)\mathbf{O}_{i}=\mathrm{diag}(\ell_{i}^{(T_{c})})^{-1}\mathbf{O}_{i}^{(T_{c})}.
  On chip, compute Li=mi(Tc)+log(i(Tc))L_{i}=m_{i}^{(T_{c})}+\log(\ell_{i}^{(T_{c})}).
  Write 𝐎i\mathbf{O}_{i} to HBM as the ii-th block of 𝐎\mathbf{O}.
  Write LiL_{i} to HBM as the ii-th block of LL.
end for
 Return the output 𝐎\mathbf{O} and the logsumexp LL.
Algorithm 2 TurboAttention Decode
0:  Matrices 𝐐,1×d\mathbf{Q},\in\mathbb{R}^{1\times d} in HBM, compressed KV cache 𝐊q2,𝐕q2N×d\mathbf{K}^{q2},\mathbf{V}^{q2}\in\mathbb{R}^{N\times d}, integer scale of KV cache s𝐊q2ints_{\mathbf{K}^{q2}}^{int},s𝐕q2ints_{\mathbf{V}^{q2}}^{int}, floating scale sKs_{K}, sVs_{V}, zero point of KV cache z𝐊q2intz_{\mathbf{K}^{q2}}^{int}, z𝐕q2intz_{\mathbf{V}^{q2}}^{int}, block size BcB_{c}, Softmax approximate algorithm SAS.
  Divide 𝐊q2,𝐕q2\mathbf{K}^{q2},\mathbf{V}^{q2} in to Tc=NBcT_{c}=\left\lceil\frac{N}{B_{c}}\right\rceil blocks 𝐊1q2,,𝐊Tcq2\mathbf{K}_{1}^{q2},\dots,\mathbf{K}_{T_{c}}^{q2} and 𝐕1q2,,𝐕Tcq2\mathbf{V}_{1}^{q2},\dots,\mathbf{V}_{T_{c}}^{q2}, of size Bc×dB_{c}\times d each.
 Divide s𝐊q2ints_{\mathbf{K}^{q2}}^{int},s𝐕q2ints_{\mathbf{V}^{q2}}^{int},z𝐕q2intz_{\mathbf{V}^{q2}}^{int}, block size BcB_{c}into Tc=NBcT_{c}=\left\lceil\frac{N}{B_{c}}\right\rceil blocks accordingly .
 Divide the output 𝐎1×d\mathbf{O}\in\mathbb{R}^{1\times d} into TcT_{c} blocks 𝐎i,,𝐎Tc\mathbf{O}_{i},\dots,\mathbf{O}_{T_{c}} of size 1×dTc1\times\frac{d}{T_{c}} each, and divide the logsumexp LL into TrT_{r} blocks Li,,LTcL_{i},\dots,L_{T_{c}} of size 11 each.
  Load 𝐐\mathbf{Q} from HBM to on-chip SRAM.
  On chip, initialize 𝐎j=(0)1×dTc1×d,i(0)=(0)Bc𝟙Bc,mi(0)=()BcBc\mathbf{O}_{j}=(0)_{1\times\frac{d}{T_{c}}}\in\mathbb{R}^{1\times d},\ell_{i}^{(0)}=(0)_{B_{c}}\in\mathbb{1}^{B_{c}},m_{i}^{(0)}=(-\infty)_{B_{c}}\in\mathbb{R}^{B_{c}}.
for 1jTc1\leq j\leq T_{c} do
   Load 𝐊jq2,𝐕jq2\mathbf{K}_{j}^{q2},\mathbf{V}_{j}^{q2} from HBM to on-chip SRAM.
  On chip, compute s𝐐=max(abs(𝐐))119,𝐐q1=𝐐s𝐐s_{\mathbf{Q}}=\frac{max(abs(\mathbf{Q}))}{119},\mathbf{Q}^{q1}=\left\lceil\frac{\mathbf{Q}}{s_{\mathbf{Q}}}\right\rfloor 𝐊jq1=𝐊jq2s𝐊jq2int+z𝐊jq2\mathbf{K}_{j}^{q1}=\mathbf{K}_{j}^{q2}\cdot s_{\mathbf{K}^{q2}_{j}}^{int}+z_{\mathbf{K}^{q2}_{j}}, 𝐕jq1=𝐕jq2s𝐕jq2int+z𝐕jq2\mathbf{V}_{j}^{q1}=\mathbf{V}_{j}^{q2}\cdot s_{\mathbf{V}^{q2}_{j}}^{int}+z_{\mathbf{V}^{q2}_{j}}
   On chip, compute 𝐒j=s𝐐s𝐊j𝐐q1𝐊jq11×Bc\mathbf{S}_{j}=s_{\mathbf{Q}}\cdot s_{\mathbf{K}_{j}}\cdot\mathbf{Q}^{q1}\mathbf{K}_{j}^{q1}\in\mathbb{R}^{1\times B_{c}}.
   On chip, compute mj=max(m(j1),rowmax(𝐒j))1m_{j}=\mathrm{max}(m_{(j-1)},\mathrm{rowmax}(\mathbf{S}_{j}))\in\mathbb{R}^{1}, 𝐏~(j)=SAS(𝐒jmj)1×Bc\tilde{\mathbf{P}}^{(j)}=SAS(\mathbf{S}_{j}-m_{j})\in\mathbb{R}^{1\times B_{c}} (pointwise), (j)=SAS(mj1m(j))(j1)+rowsum(𝐏~(j))1\ell^{(j)}=SAS(m^{j-1}-m^{(j)})\ell^{(j-1)}+\mathrm{rowsum}(\tilde{\mathbf{P}}^{(j)})\in\mathbb{R}^{1}.
  On chip, compute s𝐏~(j)=max(abs(𝐏~(j))CLOSE119,Q(𝐏~(j))=𝐏~(j)s𝐏~(j)s_{\tilde{\mathbf{P}}^{(j)}}=\frac{max(abs(\tilde{\mathbf{P}}^{(j)})}{119},Q(\tilde{\mathbf{P}}^{(j)})=\left\lceil\frac{\tilde{\mathbf{P}}^{(j)}}{s_{\tilde{\mathbf{P}}^{(j)}}}\right\rfloor,
   On chip, compute 𝐎(j)=diag(SAS(m(j1)m(j)))1𝐎(j1)+s𝐏~(j)s𝐕jQ(𝐏~(j))𝐕jq1\mathbf{O}^{(j)}=\mathrm{diag}(SAS({m^{(j-1)}-m^{(j)}}))^{-1}\mathbf{O}^{(j-1)}+s_{\tilde{\mathbf{P}}^{(j)}}\cdot s_{\mathbf{V}_{j}}\cdot Q(\tilde{\mathbf{P}}^{(j)})\mathbf{V}_{j}^{q1}.
end for
 On chip, compute 𝐎=diag((Tc))1𝐎(Tc)\mathbf{O}=\mathrm{diag}(\ell^{(T_{c})})^{-1}\mathbf{O}^{(T_{c})}.
 On chip, compute L=m(Tc)+log((Tc))L=m^{(T_{c})}+\log(\ell^{(T_{c})}).
 Write 𝐎\mathbf{O} to HBM.
 Return the output 𝐎\mathbf{O} and the logsumexp LL.

Appendix B Detailed Algorithm of SAS

Algorithm 3 Sparsity-based Softmax Approximation
0:  Input matrix XN×dX\in\mathbb{R}^{N\times d}, integer threshold nrn_{r}, look-up table TT with T[nr+1]T[n_{r}+1] set to 00, polynomial approximator function POLYPOLY.
Step 1: Normalize input values
xmax=rowmax(X)x_{max}=\text{rowmax}(X) // Max value per row
X=XxmaxX=X-x_{max}
Step 2: Apply sparsity threshold
X[X<nr]=nr+1X[X<n_{r}]=n_{r}+1
Step 3: Separate integer and decimal parts
X=Xint+XdecimalX=X_{int}+X_{decimal}
Step 4: Approximate exponential values
Xlut=T[Xint]X_{lut}=T[X_{int}] // Lookup for integer part
Xapprox=POLY(Xdecimal)X_{approx}=POLY(X_{decimal}) // Polynomial approx. for decimal part
X=Xlut×XapproxX=X_{lut}\times X_{approx}
Step 5: Normalize with row-wise sum
Xsum=rowsum(X)X_{sum}=\text{rowsum}(X)
X=XXsumX=\frac{X}{X_{sum}}
 Return XX

Appendix C Detailed illustration of FlashQ and TurboAttention

In this section, we separately evaluate the accuracy degradation caused by FlashQ and SAS. Results are shown in Table 4. It demonstrated that both techniques are near loss-less.

Table 4: Separate discussion about accuracy degradation of FlashQ and SAS
Model Dataset Method Acc
LLaMA3-8B-inst AQuA FP16 50.79
LLaMA3-8B-inst AQuA FlashQ-4bit 49.60
LLaMA3-8B-inst AQuA SAS 50.12
LLaMA3-8B-inst AQuA FlashQ-4bit + SAS 48.03

Appendix D Distribution difference of models

Different classes of models exhibit distinct query, key, and value (QKV) distributions, which can significantly influence the effectiveness of compression algorithms. In Figure 4, we observe that the value cache of the Phi-3 model demonstrates a more pronounced outlier distribution compared to LLaMA and other models. Furthermore, among LLaMA-3, Qwen-2, and Phi-3, the Phi-3 value cache exhibits a stronger outlier distribution along the channel dimension than the token dimension.

Previous approaches, such as GEAR and KIVI, often suffer from performance degradation across these models due to their suboptimal value cache quantization strategies, which primarily rely on groupwise or tokenwise quantization. These methods fail to adequately account for the unique distributional characteristics of certain models, such as Phi-3.

Refer to caption

Figure 8: LLaMA-3-8B-inst Value Channel/Tokenwise Min-Max Gap Distribution. This illustrates the distribution of value cache min-max gaps across tokenwise and channelwise dimensions.

Refer to caption

Figure 9: Phi-3-mini-inst Value Channel/Tokenwise Min-Max Gap Distribution. This figure highlights the more significant outlier distribution in the Phi-3 value cache, especially along the channel dimension.

Refer to caption

Figure 10: Quantization error of two method

As shown in Figure 10 and Figure 10, both LLaMA-3-8B-inst and Phi-3-mini-inst exhibit larger outlier distributions along the channel dimension, with Phi-3 being particularly extreme. These findings suggest that tokenwise compression is likely to perform worse than channelwise compression for models like Phi-3, where channel outliers dominate. Figure 10 shows a more direct result that channelwise group quantization has less quantization error compared with tokenwise group quantization.

Appendix E Integration of with other compression algorithms

We further test TurboAttention with weight and activation quantization algorithms. Combining with these algorithms will exploit further memory and computation savings. Results are shown in Table 5.

Table 5: Separate discussion about accuracy degradation of FlashQ and SAS
Model Dataset Method Acc
LLaMA3-8B-inst GSM8K FP16 78.24
LLaMA3-8B-inst GSM8K LLM.int8() 77.94
LLaMA3-8B-inst GSM8K LLM.int8() + TurboAttention 77.48
LLaMA2-2-7B PQ FP16 79.05
LLaMA2-2-7B PQ Qserve 78.07
LLaMA2-2-7B PQ Qserve + TurboAttention 77.64

19