Open-Source LLM Inference Deployment: Engineering Attention States

    This article examines the engineering challenges of managing attention states when deploying large open-source models for inference. It breaks the state space into coordinated pools (window, compressed, index, and residual states), and covers prefix caching with multi-coordinate mapping, incremental compression, sparse block selection, hierarchical offloading, speculative decoding with rollback, parallelism strategies, and prefill/decode disaggregation.

    1. Where the Bottleneck Lies: Attention States, Not Parameter Count

    When people discuss very large open-source models, the first things they focus on are parameter counts and the number of experts. But in inference deployment, the harder problem is usually the state maintained by the attention layers. When every layer retains sliding-window attention while different layers switch between attention mechanisms with different compression ratios, the traditional assumption of “one raw KV cache per layer” no longer holds.

    The inference engine must answer three questions. First, where is the attention state of each token stored. Second, which portion of that state each layer reads. Third, how these states stay consistent across prefix reuse, recomputation, migration, and offloading. Together, the answers determine memory footprint, cache hit rate, scheduling granularity, and failure recovery behavior.

    2. Decomposing the State Space

    Reasoning about a single request’s attention state by responsibility is easier than describing it as a monolith. A common decomposition looks like this:

    State category Role Lifecycle characteristics
    Global token coordinates Provide a unified position numbering across all pools Lives as long as the request
    Sliding-window pool Holds raw KV for the most recent window Rolls and overwrites as decoding proceeds; bounded length
    Compression pools (multiple ratios) Hold the compressed representation of each block Grow with the sequence, appended block by block
    Index pool Holds lightweight keys for sparse selection Grows in sync with the compression pools
    Compression intermediate-state pool Holds residual information not yet forming a full block Resident during the request, released once a block completes

    Unified coordinates are the foundation of the whole design. The same token occupies different physical positions in different pools, and once a mapping goes wrong, the computation silently reads the wrong data. Such errors usually throw no exception; they only manifest as degraded output quality, which is expensive to debug. Coordinate mapping should therefore be implemented as an independently testable module.

    3. Engineering the Key Mechanisms

    3.1 Prefix Caching and Multi-Coordinate Mapping

    Prefix caching typically organizes token sequences in a radix tree, so multiple requests sharing the same prefix can directly reuse the computed state. Under a multi-pool structure, sharing must align with compressed-block boundaries. A compressed representation is only produced once a block is completely filled, so the incomplete block at the end of a prefix cannot be shared directly; its residual state must be copied or recomputed along with the rest.

    The following illustrative code uses class and method names purely to convey the idea; they do not correspond to any actual interface:

    1. # Illustration: maintain coordinate mapping across pools per request
    2. class KVSlotMapper:
    3. def __init__(self, window_size, compress_ratios):
    4. self.window_size = window_size
    5. self.compress_ratios = compress_ratios
    6. def map_position(self, token_pos):
    7. slots = {"window": token_pos % self.window_size}
    8. for r in self.compress_ratios:
    9. # Compression pools are addressed by block index; every r tokens form one block
    10. slots[f"compress_{r}"] = token_pos // r
    11. return slots

    3.2 Incremental Compression

    During decoding, the whole sequence cannot be recompressed at every step, otherwise the compute cost grows linearly with length. The common approach: each generated token is appended to the residual state; once the residual state fills a block, the compressed representation of that block is computed, written to the corresponding compression pool, and the residual state is cleared. This keeps the per-step overhead constant.

    1. # Illustration: single-step update of incremental compression
    2. def decode_step(state, new_kv, ratio, pool):
    3. state.residual.append(new_kv)
    4. if len(state.residual) == ratio:
    5. pool.append(compress_block(state.residual))
    6. state.residual.clear()

    3.3 Sparse Selection

    At decode time, sparse selection picks the blocks from the compression pool most relevant to the current query, then merges their compressed representations with the result of window attention. The flow has three steps: compute relevance scores using the index pool, take the Top-K blocks, and gather the corresponding compressed KV for the attention computation.

    The cost of selection depends on the number of compressed blocks, not the number of raw tokens. The larger the compression ratio, the fewer candidate blocks and the faster the selection. The Top-K operator sits on the critical path and usually needs a fused implementation to avoid repeated memory reads and writes.

    3.4 Hierarchical Offloading

    In long-context scenarios, the total volume of the compression pools remains significant. A common scheme is hierarchical placement: active data stays in GPU memory, less-used data goes to host memory, and is fetched back when needed. The window pool and index pool are small and frequently accessed, so they fit for permanent GPU residency; the much larger compression pools are the natural offloading targets.

    The cost of offloading is fetch-back latency. In practice, the blocks likely needed in the next step are predicted from the previous step’s selection results, and transfers are initiated early so they overlap with computation. Transfer bandwidth is limited, so prefetch hit rate directly affects tail latency.

    3.5 Multi-Token Prediction and Speculative Decoding

    Multi-token prediction uses an additional draft head to propose several candidate tokens at once, which the main model then verifies in a single forward pass, accepting the longest correct prefix. This increases the number of tokens produced per unit of time.

    The difficulty lies in rollback. Rejected candidate tokens may already have been written into the window pool, and may even have completed a compressed block and produced a compressed representation. Rollback must restore the window pool, compression pools, and residual state to the accepted position simultaneously, or subsequent decoding will read corrupted state. In engineering practice, a lightweight snapshot is kept for each speculative step, or write records are designed to be truncatable by position.

    3.6 Parallelism Strategies

    Attention computation can be data-parallel across requests, and projection layers can use tensor parallelism. The prefill stage of ultra-long contexts can be further partitioned along the sequence dimension, i.e., context parallelism. The MoE part uses expert parallelism, with fused dispatch and combine operators so that communication overlaps computation as much as possible.

    Strategy selection depends on the workload. Short requests and high-throughput scenarios rely more on data parallelism; long-context, low-latency scenarios require context parallelism and communication optimization.

    3.7 Prefill/Decode Disaggregation

    Prefill nodes compute the full state of the input, while decode nodes generate tokens one by one. When the two node types are deployed separately, state must be migrated from the prefill node to the decode node. The migrated content includes all pools