Hybrid Linear Attention Cost Tradeoffs for Very Large Model Selection

    This article examines how long-context cost should shape attention-structure choices for very large models. It estimates weight, KV-cache, activation, and training memory at trillion-parameter scale; compares the compute and memory boundaries of full attention and linear attention; and explains why hybrid layouts usually retain a small share of full-attention layers. The key takeaway is that the full-attention ratio should be set through your own evaluation set rather than a universal rule.

    Conclusion: Determine Long-Context Cost First, Then Choose the Attention Structure

    Over the past few years, large models have mainly become stronger by increasing parameter count. Once parameter scale reaches the trillion level, memory, compute, and long-context overhead become concrete constraints. Scaling parameters alone is no longer enough; the underlying attention structure also needs reevaluation.

    The judgment in this article is as follows. As sequences grow longer, linear attention can reduce the growth of attention compute from quadratic to linear. However, replacing full attention entirely may degrade performance on tasks that require exact retrieval and complex reasoning. The size of the drop depends on the task, metrics, and evaluation set. A common approach is therefore a hybrid layout: most layers use linear attention, while a small number of full-attention layers are retained.

    There is no universal standard for the hybrid ratio. As an arithmetic example, if 12 of 48 layers are full-attention layers, the ratio is 25%, and the KV cache of those 12 layers would be only 25% of that in a pure full-attention model. This is only a calculation example, not the real configuration of any released model. The actual ratio must be determined with your own evaluation set.

    The terms “attention residual” and “hybrid linear attention” mentioned in this article are not standardized terms. Implementations differ considerably across teams. Before citing a specific approach, consult the corresponding original paper or technical report.

    1. Memory: The First Wall for Trillion-Parameter Models

    Memory usage is first determined by parameter precision. The following table estimates weight size for a 2.8 trillion-parameter model using decimal units. It counts only the weights themselves, excluding activations, KV cache, and communication buffers.

    Numerical precision Bytes per parameter Total weight memory
    FP32 4 11.2 TB
    BF16 / FP16 2 5.6 TB
    INT8 1 2.8 TB
    INT4 0.5 1.4 TB

    For BF16 inference, weights alone require 5.6 TB of memory. If each accelerator has 80 GB of memory, the weights alone require at least 70 cards (5600 GB ÷ 80 GB = 70). Actual deployment must also leave room for activations, KV cache, and framework overhead, so the required number of cards will be higher.

    At inference, two other major costs exist besides weights.

    • The KV cache formula is: KV bytes = 2 × layers × KV heads × head dimension × context length × batch size × bytes per element. For a hypothetical configuration with 80 layers, 8 KV heads, head dimension 128, BF16, 128K context, and batch size 1, the result is 2 × 80 × 8 × 128 × 131072 × 2 = 42,949,672,960 bytes, or about 42.9 GB.
    • Activation memory depends on batch size and sequence length. Most inference frameworks do not store all activations. Actual usage depends on the specific implementation and must be measured.

    Training memory overhead is larger. With common mixed precision and the Adam optimizer, each parameter typically consumes: BF16 weights 2 bytes, BF16 gradients 2 bytes, FP32 master weights 4 bytes, Adam first moment 4 bytes, and second moment 4 bytes, for 16 bytes total. The DeepSpeed ZeRO paper uses the same accounting. On this basis, the state for a 2.8 trillion-parameter model alone is about 44.8 TB, still excluding activations.

    2. Cost Boundary Between Full Attention and Linear Attention

    Let n be the sequence length and d be the dimension of a single attention head.

    Full attention has a compute cost of O(n²d) per layer per head. QK^T and the attention weights multiplied by V each account for roughly half the order. The KV cache grows linearly with n and uses O(nd) memory per head.

    Linear attention replaces softmax with a kernel function φ and rewrites attention in recurrent form: St = S{t-1} + φ(k_t) v_t^T, with output o_t = φ(q_t)^T S_t. The state S is a d×d matrix, the compute cost is O(nd²), and the state memory is O(d²), independent of n.

    The ratio between the two for the attention term is about n/d. When d = 128 and n = 131072, the ratio is about 1024. When n approaches d, the gap disappears. Therefore, linear attention provides a clear benefit only in long-context settings.

    This ratio applies only to the attention term. QKV projections, output projection, and feed-forward networks have the same compute cost in both structures and grow linearly with n. The speedup for the whole model is lower than the ratio for the attention term. The share of total compute accounted for by the attention term depends on context length and model width, and must be calculated for your own configuration.

    Linear attention also has a cost. Full attention retains the keys and values of every historical token and can perform exact lookup. Linear attention compresses history into a fixed-size state, which reduces exact lookup capability. This is why hybrid layouts retain some full-attention layers.

    3. Hybrid Layout: Which Full-Attention Layers to Keep

    The KV cache of a hybrid model can be approximated as: hybrid model KV cache ≈ f × pure full-attention model KV cache, where f is the proportion of full-attention layers. The state size of linear layers is independent of n, so it is not counted in memory that grows with length.