# Oral 31861

- Kind: Oral
- Talk page and recording: https://iclr.cc/virtual/2025/oral/31861

> Transcript of the talk's recording (session_asr), made by speech recognition or taken from the host's captions. It may contain errors. The words are the speakers'; listen to the recording on the talk's page before quoting it.

Hello everyone, I'm from Peking University, and I'm excited to present our work FlexPiFel. And let's begin with the motivation. Today's large language models can handle very long contexts, up to 128k tokens or even over a million. And however, inference over such long context is computationally expensive, particularly during the pre-filling phase.
And the challenge lies in computing attention, uh which has quadratic complexity rela uh relative to sequence length. And that quickly becomes a bottleneck. However, it is an opportunity. Attention scores are usually sparse, and uh with most values near zero. By identifying critical curve key pairs and skipping the rest, we can reduce a lot of computation. And this is the core idea uh behind sparse attention and where our method comes in.
And to understand the attention sparsity, we visualize the attention maps and analyze their distributions. First, attention patterns vary across heads. Some attention heads show diverse and unstructured patterns, as shown in the top left picture, where attention focus on different positions depending on the query, and others show structure patterns. And like the top right example, where attention forms vertical or slash lines.
Uh I think our conceit then across all the queries. And second, not all inputs requires the same amount of uh uh computation. The hidden maps show how many query key pairs are needed to capture ninety five percent of
Ternion scores. The heat maps show how many per-key pairs are needed to capture 95% of the ternion scores for two distinct, distincting proofs across all layers and heads. And you can, we can find that different inputs and different attention heads require different amounts of attention computation. So we need a more adaptive and input-aware method for sparse attention. And this brings us to our method, FactorFew.
Uh a flexed diff is a flexible and context-aware sparse attention mechanism designed to reduce computation, mainly in the pre-filling phase. And it integrates seamlessly with existing LMs and don't require additional training. It dynamically adapts to each input and attention heads during inference. In fact, flexed diff accelerates attention computation without sacrificing performance, as we will demonstrate s uh on several Thanks benchmarks later. for watching!
Okay. And now let's take a look at how flexible it works. And for every inputs and for each attention head, uh we first determine the attention pattern type. And then based on the chosen pattern, we select a curvature pairs for attention computation. And finally we compute sparse attention over just those selected pairs. And let's dive into the details for each step. And the first step is sparse pattern determination.
And as noted earlier, uh attention heads show varied behaviours. Uh some heads can s consistently follow structure patterns, a stable across queries, while other others very significantly based on the query. And the first step is our sparse pattern determination.
And as noted earlier, attention has show varied behaviors. Some heads consistently follow structured patterns and they are stable across queries, while others varied based on the queries showing diverse patterns. So we classify attention heads into two types. The first is query-aware pattern, which means attention is concentrated on different positions depending on the query and no consistent structure can be found here.
And the second is structural sparse pattern, also called vertical slash pattern, where attention forms vertical and slash lines that stay consistent across queries. And the question is how to handle these two types of patterns. And for query-aware pattern, since we cannot compute a full attention map, we use a block-wise attention approximation to identify the important query key pairs. And ideally we can apply this strategy to all heads, but the approximation isn't always reliable.
A window approximation is unreliable we fall back to a predefined vertical slash pattern, uh which is common in modern L_M_S_ uh particularly those using a rotary positioning ta uh encoding. And how do we handle these two patterns? For rotary aware pattern, since we cannot compute a f uh sorry.
Okay, the next slide. And after we get how to handle the two penalty types, and now we need to determine whether the approximation is good enough. And we take a subset of queries, usually the last few query vectors and computes two attention distributions. The two attention is obtained by computing the actual attention scores and putting them to get a block-wise score distribution.
And for the estimated distribution, we first put the queries and keys with the kernel size equal to attention block size and then computing attention on the product query and key. And after we get two distributions, we chose Jensen-Shannon divergence to measure the similarity between the two distributions. If the divergence is below a threshold, we consider the approximation accurate and use query-aware pattern. Otherwise, we use the vertical slash pattern.
And the second step of Flexifu is cross-index selection. And once we have chosen a patent height, we need to select which query key pairs to keep. A simple approach would be to use a fixed computational budget such as the top-k tokens for each head. However, as we have shown before, sparsity varies for different heads and inputs. So instead we use a cumulative attention-based.
Let's it keeps uh on changing. budget determination. Uh specifically we want to find the smallest num smallest number of tokens of whose attentions for a sum to at least gamma like uh ninety or ninety five percent of the total scores. And this is achieved by sorting the attention scores and selecting tokens with the highest highest scores uh until the threshold is reached. And this approach ensures that uh heads with sharp distributions require fewer tokens and while those with better distributions with more.
And it automatically adjusts uh the uh computational budget per hand. And next I will explain how we apply this method to the two-patient types. And for heads with query-aware pattern, we compute a tangent for us using both queries and keys, and similar to the pattern determination step, but with a standard block size of sixty four or one hundred twenty eight, we obtain a relatively small attention map.
And then we can sort the block by estimated scores, and select the top ones until the cumulative score uh exceeds gamma. And for virtual selection pattern, we select a representative subset of queries, uh usually the last block, and compute attention. Yes.
Okay. Uh then we compute every scores along vertical and slash lines in the attention map. And we select the top lines that together cover gamma of the uh of the retention scores. And due to the structure nature of this pattern, uh we can extend the selected lines to the entire attention map and get all query keypairs to be computed. And the final step of fax perfu is sparse calculation.
Once we have the sparse index sets, uh we can use them to compute attention. Uh simply masking out uh all those uh entries in the dense matrix uh would not save computation. So we employ customised attention kernels. We use block-wise We use block-wise attention calculation.
And implement sparse attention based on flash attention. And here we only calculate the attention over selected KV blocks for each KV query block. And our implementation is built with Triton and achieves exacting acceleration. For example, if we mask out 95% of the query key pairs and we can achieve up to 20 times speed up.
I won't Combine let you go. with the pattern determination and index selection described earlier, and this gives us the fully dynamic dynamic and efficient sparse attention mechanism and we call it flexible field. And let's review some of the results. An on-router benchmark, a synthetic benchmark with controllable length and complexity, and flexible field consistently preserves model's performance across multiple context lengths.
And we can find that for 128K tokens, our method even slightly improves accuracy for some models while reducing attention computation. And on infinite batch, which includes real-world tasks like QA, summarization, and code debugging, we again see that flexible field maintains high performance across many tasks.
And for the latency of the factory field, our approach lets us trade off performance and latency by adjusting the cumulative attention threshold. For example, setting gamma to 0.95 gives us a 2.4 times speed up on attention while preserving performance. Lowering it to 0.9 or 0.85 can offer even faster inference and still keep high accuracy in many context lengths.
As done of the uh aviation studies, uh if we fix the computation budget, perhaps performance drops. Uh by comparing the orange and purple lines uh we see that our dynamic budget location method enhance both speed and accuracy. And we
And we can also evaluate the impact of sparse attention threshold and an appropo uh an appr appropriate value lets us make the most of recovery pattern and enhance the performance. However if it is too large, uh we may incorrectly apply the recovery pattern to has without reliable estimations, which actually actually hurts the performance.
Here are some of the visualizations. And the sparse masks used by FlexPIFU revealed that query-aware heads often focus on scattered blocks, but sometimes also resembling the vertical and slash lines. And we can also find that the sparsity ratio adapts based on task and context length. The longer inputs have a higher sparsity and more complex tasks have lower sparsity.
Finally, if FlexPifu is open source, you can scan the QR code to visit the GitHub repo. It's easy to integrate into your inference pipeline with just a few lines of code. We're going to try it out and share our feedback. That's the end of my talk. Thank you for listening and happy to take any questions. Great. Yeah. Thanks so much for the talk. And then, yeah, we have time for one quick question.
So I have a quick question. So I'm curious, when you choose one each other, so you mentioned there are two different attention patterns. So there's this vertical slash pattern as well as the other pattern. How do you choose which one to use? Because we don't explicitly identify the vertical slash pattern, we only identify whether a query-aware pattern should be used. The query-aware pattern can capture potentially more diverse attention blocks.
And we measured uh we measured uh uh difference between the two estimations and when the estimation is accurate, we use the curve of the pattern and it can capture more diverse blocks. And if the uh estimation is inaccurate, we use the as a fall-back. Uh this is uh important to ensure the performance.
