Long-Context Inference Architecture: Linear Attention and Index Pool Design
This article breaks down the core design patterns behind long-context LLM inference, covering linear attention with fixed-size recurrent states, sparse indexing with an index pool, backbones without explicit positional encoding, and constrained hyper-connections. It explains the trade-offs among state capacity, retrieval precision, and training stability, along with practical guidance on layer ratios, caching, serving infrastructure, and evaluation.
1. Why Long-Context Architectures Need a Redesign
As applications move from short question answering to long-document understanding, repository-level code analysis, and multi-turn agent tasks, context length keeps growing. The traditional approach of full attention plus a complete key-value cache quickly becomes unsustainable in terms of GPU memory and memory bandwidth. Attention computation grows quadratically with sequence length, while the key-value cache expands linearly. Together, they sharply reduce how much a single device can handle.
The industry has therefore converged on two main directions: compressing attention computation and state, and compressing the candidate set used for indexing and retrieval. This article takes a neutral view of the general design patterns behind both capabilities.
2. Linear Attention: Replacing a Growing Cache with a Fixed State
The core idea of linear attention is to rewrite “weighting each historical token one by one” as “maintaining a fixed-size recurrent state.” Taking incremental linear attention with forget gating as an example, its recurrence can be abstracted as:
S_t = A_t ⊙ S_{t-1} + B_t ⊗ V_tO_t = Q_t · S_t
Here, S_t is the state matrix, A_t is the gating decay term, and B_t is derived from the key vectors. The engineering value is that each inference step updates only a fixed-size state, so memory does not grow linearly with context length and memory access during decoding stays roughly constant.
The cost is equally clear: a fixed state has limited capacity, so recall is weaker than full attention for tasks that require precisely retrieving a specific detail across a very long span. A common practice is therefore layered hybridization—most layers use linear attention for long-range information compression, while a few layers retain full or sparse attention for precise retrieval.
3. Sparse Indexing and the Index Pool: Shrinking the Candidate Set
The second capability revolves around “select first, then compute.” The general flow is: generate compressed representations for historical blocks and build an index pool that can be updated incrementally; when a query arrives, match it against the index by similarity and select only a small number of highly relevant blocks for subsequent attention computation. The logic looks like this:
# Pseudocode: block-level compressed index + Top-K selectionindex_pool.add(block_id, compress(block_kv)) # compress on writescores = index_pool.query(q) # score the querykeep = topk(scores, k) # keep only k blocksout = attention(q, kv[keep]) # sparse computation
There are three design considerations: compression granularity (per block or per slot), index capacity and eviction policy, and the K value for Top-K. Too small a K misses key evidence, while too large a K degrades back toward near-full computation. In practice, the index pool and the linear state are usually complementary: the state provides the “overview,” and the index pool provides the “location.”
4. Removing Positional Encoding from the Backbone and Constrained Hyper-Connections
Difficulty extrapolating to long context often stems from positional encoding. One approach is to omit explicit rotary positional encoding in the language backbone and let the content itself and the hierarchical structure carry order information, reducing distribution shift during extrapolation; the attention part then compensates for local ordering through relative bias and similar mechanisms.
Hyper-connection structures focus on reworking the residual path: a traditional residual is an identity direct connection, whereas hyper-connections introduce learnable cross-layer connection matrices. Applying constraints on a manifold (such as keeping them orthogonal or near-orthogonal) can mitigate signal amplification and gradient instability in deep networks, making deeper and wider stacks more stable during long-sequence training.
5. Implementation and Evaluation Recommendations
- Layer ratio: decide the proportion of linear layers versus precise-retrieval layers, and run ablations at moderate length first.
- Caching strategy: combine paged cache management with prefix reuse to reduce repeated computation.
- Serving infrastructure: host inference instances on a container platform, use a message queue to smooth request spikes, store long documents in object storage, and track time-to-first-token and throughput with logging and monitoring with alerting.
- Evaluation dimensions: beyond perplexity, focus on long-range precise retrieval, multi-hop reasoning, and the memory curve under very long inputs.
6. Conclusion
Linear attention, the index pool, backbones without explicit positional encoding, and constrained hyper-connections are all essentially trade-offs within the triangle of state capacity, retrieval precision, and training stability. No single approach dominates across the board. The sensible approach is hybrid orchestration based on the business context length distribution and recall requirements, continuously calibrated with observable metrics. On a platform with a complete cloud product portfolio such as Baidu AI Cloud, these architectures can be combined with elastic compute, storage, and observability to form an operable long-context inference solution.
