Evaluating New LLMs: Inference Structure and Deployment Cost

    When evaluating a new large language model, benchmark scores and version numbers reveal only part of its capability. This article explains how the prefill and decode stages differ, how architecture pruning and cross-layer KV cache sharing affect computation, and how active parameters influence latency and memory. It also provides estimation formulas, a deployment checklist, and risk boundaries for judging whether a model's inference workload matches your business load.

    Evaluating New LLMs: Inference Structure and Deployment Cost

    When evaluating a new model, first understand what computations it performs during inference. Then determine whether those computations match your business workload. Benchmark scores and version numbers reflect only part of a model’s capability. What determines latency, memory footprint, and deployment cost is the actual computational structure of the inference stage. This article breaks down the prefill and decode stages, explains the impact of architecture pruning, cross-layer shared cache, and active parameters, and concludes with estimation formulas, a checklist, and risk boundaries.

    1. Prefill and Decode: The Asymmetry Between Two Stages

    Large language model text generation is usually divided into two stages.

    1. Prefill: The model reads the full input prompt at once, computes all input positions in parallel, and writes key-value (KV) caches for every attention layer. This stage also outputs the first generated token.
    2. Decode: The model generates one new token at a time in an autoregressive loop. At each step, it reads the entire KV cache from previous steps.

    The two stages place different demands on hardware. Prefill processes many tokens at once, performs a large amount of computation, and is usually compute-bound. Decode processes only one token per step but repeatedly reads weights and caches, so it is usually memory-bandwidth-bound. This difference is called prefill-decode asymmetry, or PD asymmetry. The optimization strategies for the two stages are therefore different.

    Comparison Prefill Decode
    Tokens per step Input length L_in, processed in parallel 1 token, generated step by step
    Main bottleneck Compute (compute-intensive) Memory bandwidth (reading weights and KV cache)
    Compute characteristics Approximately proportional to input length (attention term separate) Compute per output token is largely fixed
    Memory usage Generates KV cache for all input positions KV cache grows with generated length
    Key metric Time to first token (TTFT) Time per output token (TPOT) and throughput
    Common optimizations Chunked prefill, higher compute utilization, prefix cache reuse Batching, weight quantization, KV compression, speculative decoding

    2. Pruning Space in Standard Architectures

    Many people assume this asymmetry exists only in new architectures. In fact, a standard decoder-only Transformer has the same characteristic.

    Prefill must generate a KV cache for every layer, and the cache depends only on the input to that layer’s attention. For non-final positions, each layer’s attention and feed-forward network (MLP) still need to be computed because their outputs are inputs to the next layer and determine the KV for subsequent layers. The last layer is different. For non-final positions, the last layer only needs to generate its own K and V. Its attention output and MLP result are not used by later steps and can be skipped. The output layer (LM Head) also only needs to compute vocabulary probabilities for the last position.

    The benefit of this pruning in a standard architecture is limited because it only involves part of the last layer and the output layer. Its value is as a design principle: not all computations are necessary during inference. Designers can decide which computations to prune based on which results are actually used by later steps.

    3. Cross-Layer Shared Cache: Extending Pruning to Deeper Layers

    If multiple layers share the same KV cache, prefill only needs to compute up to the layer that produces the shared cache. Deeper layers no longer generate their own caches; they only use the hidden state of the last position to query the shared cache. The core idea of this approach is “generate the cache only once,” which extends pruning from the last layer to deeper parts of the model.

    The benefits are mainly twofold. First, prefill computation decreases, which may improve time to first token in long-prompt scenarios. Second, KV cache storage may decrease because multiple layers do not each need to store a copy.

    This approach also has clear boundaries.

    • Shared cache limits the independence of each layer’s representation. Queries in deeper layers can only use key-value pairs generated by shallower layers, which may affect expressiveness.
    • What is shared is the key-value result; deeper layers still use their own query projections. Whether to retain each layer’s KV projection weights depends on the specific design and should be checked against the model’s technical report.
    • This structure usually needs to be determined during training. When modifying an existing model, quality must be verified through training or ablation experiments, especially for long-context retrieval and reasoning tasks.

    The pseudocode below only illustrates the computation flow and does not represent any specific implementation.

    1. def prefill(tokens, layers, share_at):
    2. """Prefill: compute the shared KV cache and output the first-token prediction.
    3. tokens: input sequence of length n
    4. layers: all Transformer layers
    5. share_at: index of the layer that produces the shared KV (0-based)
    6. """
    7. hidden = embed(tokens) # shape [n, d]
    8. # Shallow layers (including share_at): all positions participate
    9. for idx in range(share_at + 1):
    10. hidden, kv = layers[idx].attention_and_ffn(hidden)
    11. if idx == share_at:
    12. shared_kv = kv
    13. # Deep layers: reuse shared_kv and track only the last position
    14. last = hidden[-1:] # shape [1, d]
    15. for layer in layers[share_at + 1:]:
    16. last = layer.attend_last_position(last, shared_kv)
    17. last = layer.ffn(last)
    18. return shared_kv, lm_head(last)

    When share_at is the last layer, the deep-layer loop is empty, last is taken directly from hidden[-1:], and the output branch remains defined.

    4. Total Parameters vs. Active Parameters: Reading the Spec Sheet

    Spec sheets commonly show two numbers. Total parameter count is the number of all weights in the model. Active parameter count is the number of weights actually involved in computation when processing one token. In a mixture-of-experts (MoE) structure, each token is routed to only some experts, so active parameters are much smaller than total parameters.

    Some designs distinguish between prefill and decode active parameters, and the subset of weights involved in computation can differ between the two stages. When evaluating these numbers, note three points.

    • Active parameters determine per-step computation, but they do not equal memory usage. Memory must still hold all weights, and all MoE experts need to reside in memory unless offloading is used.
    • Prefill and decode active amounts may differ and should be estimated separately. Do not simply average them.
    • Routing and cross-device communication add overhead. The same active parameter count does not necessarily mean the same actual speed.

    5. Estimation Method: Formulas, Examples, and Checklist

    The estimated computation here considers only the parameter portion of matrix multiplication. Notation:

    • N_pre: active parameters per token in the prefill stage
    • N_dec: active parameters per token in the decode stage
    • L_in: input length (number of tokens)
    • L_out: output length (number of tokens)

    The computation for an entire request is approximately:

    Work ≈ 2 × (N_pre × L_in + N_dec × L_out) + attention term

    The coefficient 2 represents one multiplication and one addition per parameter. The attention term depends on context length and cannot be ignored for long contexts; it is not expanded in this formula. Embedding lookup and normalization layer computation are also not included.

    Example (hypothetical values, not representing any real model): N_pre = 20B, N_dec = 15B, L_in = 4000, L_out = 500.

    • Prefill computation: 2 × 20×10⁹ × 4000 = 1.6×10¹⁴ FLOPs
    • Decode computation: 2 × 15×10⁹ × 500 = 1.5×10¹³ FLOPs
    • With effective compute of 300 TFLOPS, prefill takes about 0.5 seconds. If actual utilization is only half, it takes about 1 second.

    The two main memory estimation terms are as follows.

    • Weight memory ≈ total parameter count × bytes per parameter. For 100B total parameters in BF16 (2 bytes per parameter), this is about 200 GB.
    • KV cache ≈ 2 × cache layers × KV heads × head_dim × sequence length × batch size × bytes per element. With 60 layers, 8 KV heads, head_dim of 128, sequence length of 4500, batch size of 1, and BF16, each sequence uses about 1.1 GB. With cross-layer sharing and only 30 independent caches, this is about 0.55 GB.

    Decode speed has a rough upper bound. With batch size 1, each step needs to read about 15B × 2 bytes = 30 GB based on decode active weights. Assuming memory bandwidth of 2 TB/s, the lower bound per step is about 15 milliseconds, corresponding to a theoretical upper bound of about 67 tokens/s. In practice, KV cache reads, routing, and kernel launch overhead make it slower.

    The following checklist can be used for item-by-item verification.

    Check item Data needed Why it matters
    Input and output length distribution P50 and P95 lengths from business logs Determines the relative share of prefill and decode
    Prefill and decode active parameters Technical report or model configuration Plug into the formulas separately
    Weight memory Total parameter count and weight precision Determines whether one GPU can hold the model or how many GPUs are needed
    KV cache Cache layers, KV heads, head_dim, sequence length, batch size Determines the concurrency limit
    Memory bandwidth Hardware specifications Estimates the decode speed ceiling
    Communication overhead Cross-GPU traffic for tensor or expert parallelism MoE routing and multi-GPU deployment add latency
    Batch size and concurrency Target QPS and scheduling policy Affects throughput and tail latency
    Quantization method Precision of weights and KV cache Affects memory usage and output quality
    Deployment method Cloud GPU, on-premises data center, inference framework Determines available optimizations and actual performance

    6. Risks and Applicable Boundaries

    • The formulas provide order-of-magnitude estimates. They do not include framework scheduling, kernel efficiency, or communication waits. Before launch, stress-test with real workloads.
    • The quality impact of shared cache depends on the specific model. It cannot be assumed for all models.