Introduction
Generating multiple independent completions for a single prompt—such as an IDE providing several code snippets or a system generating many answers for reranking—is a common but demanding task in modern Large Language Model (LLM) deployment. This scenario is known as single-context batch sampling.
In this paper, the authors introduce Bifurcated Attention, a novel optimization for LLM inference that slashes latency in these "single-context, many-completion" scenarios. By mathematically splitting the attention mechanism to load the shared prompt's memory only once per batch, the method achieves up to a 4x speedup and enables massive batch sizes without any loss in model accuracy.
The Bottleneck: Redundant Memory I/O
The primary bottleneck in single-context batch sampling isn't raw computational power (FLOPs); it is Memory I/O.
During the "incremental decoding" phase, where the model generates tokens one by one, it must repeatedly load the Key-Value (KV) cache—the memory of all previous tokens—from slow global memory to the fast processor. In a standard batch of 128 completions, the model naively loads the exact same prompt KV cache 128 times at every single step. As context lengths grow, this redundant data movement becomes a massive latency wall, often leading to "Out of Memory" errors or sluggish performance.
The most impactful insight of this research is realizing that the prompt KV cache is a static, shared resource. In high-batch scenarios, the cost of loading this shared resource dominates the entire inference process.
The Solution: Context-Aware Bifurcated Attention
To address this, the authors introduce Context-Aware Bifurcated Attention. The core idea is a clever mathematical refactoring of the attention operation. Instead of treating the entire KV cache as one giant block, they "bifurcate" (split) the attention calculation into two distinct operations:
- Context Attention: Computes attention against the shared prompt (the "context"). Because this data is identical for every item in the batch, it is loaded into memory once and broadcast to all batch indices.
- Decoding Attention: Computes attention against the unique tokens generated for each specific completion. This part is unique to each batch item and is handled normally.
The results of these two operations are then mathematically merged. Crucially, this is an exact computation—it produces the exact same result as standard attention with the same number of FLOPs, but with significantly fewer memory reads. The method is highly flexible, working seamlessly with standard Multi-Head Attention (MHA), Multi-Query Attention (MQA), and Grouped-Query Attention (GQA). It is also compatible with torch.compile, allowing for kernel fusion and further hardware optimization.
Key Insights and MQA Scaling Laws
The paper also provides a detailed analysis of Multi-Query Attention (MQA). The authors found that while MQA is more memory-efficient, it is slightly less "expressive" than Multi-Head Attention. However, they demonstrate a consistent scaling law: you can match MHA performance by simply making the MQA model about 10% larger.
Furthermore, by reducing the "cost per sample," bifurcated attention effectively makes models smarter in practice. If a system can generate 128 samples in the time it used to take for 5, its "pass@top-k" accuracy skyrockets, even though the underlying model weights remain unchanged.
Results and Performance Gains
The researchers evaluated their method across a range of models (from 125M to 13B parameters) and tested on established open-source models like CodeGen-16B and StarCoder (15.5B). Performance was measured using code generation benchmarks like Multi-lingual HumanEval, MBPP, and MBXP.
Key results include:
- Massive Batch Scaling: For the CodeGen-16B model with a 2k context, bifurcated attention allowed the batch size to increase from 5 to 128.
- Latency Reduction: In an MHA setup with a batch size of 128 and 10k context, the method achieved a ~4x reduction in incremental decoding latency.
- Accuracy Gains: On the MBPP dataset, using bifurcated attention to enable larger sampling (pass@128) improved the score from 59.0% to 84.6% compared to a small batch size, all while maintaining acceptable real-time latency.
- Outperforming FlashAttention-2: While FlashAttention-2 is the industry standard for speed, it isn't designed for shared-prefix batching. The authors show that their bifurcated approach achieves lower latencies than FA2 in these specific parallel sampling scenarios.
Limitations and Future Work
The authors acknowledge that for very small batches or very short contexts, the overhead of splitting the attention into two GEMM (General Matrix Multiply) operations might actually be slightly slower than the naive approach. They suggest a "workload-based switch" to use standard attention for small tasks.
Future work includes combining this I/O reduction technique with 4-bit or 8-bit quantization, which would likely yield even more dramatic efficiency gains. Additionally, while the current implementation is optimized for PyTorch, the authors plan to integrate these kernels into lower-level libraries like NVIDIA’s FasterTransformer.
Summary updated