LLM Inference Optimization: Technology Selection, GPU Memory Planning, and Production Monitoring

    A practical guide to selecting LLM inference techniques, estimating GPU memory requirements, and setting production concurrency limits through load testing. It also explains how to route requests by difficulty and control reasoning-token budgets to keep latency and costs predictable.

    1. Understand What Each Technique Optimizes

    The cost of large language model inference comes primarily from three sources: attention computation, GPU memory consumed by the KV cache, and additional reasoning tokens generated to improve answer quality. The following techniques address one or more of these costs.

    Multi-Head Latent Attention (MLA)

    MLA compresses each token’s keys and values into a low-dimensional latent vector before caching them, substantially reducing KV cache size.

    The trade-off is additional up-projection computation during inference. The model must also use this architecture during training: an already-trained standard multi-head attention model is difficult to retrofit with MLA.

    Sparse Attention

    Sparse attention computes attention for only a subset of token pairs. Its theoretical complexity can drop from quadratic in sequence length to nearly linear.

    Actual latency, however, depends on three factors:

    • Whether the sparsity pattern uses blocks, sliding windows, or dynamic top-k selection.
    • Whether the kernels can efficiently access sparsely arranged memory.
    • How well the hardware handles irregular memory access.

    Dynamic selection requires additional indexing computation and can even be slower than dense attention for short sequences. Benchmark it on your target hardware and sequence lengths before choosing an approach.

    Hybrid Reasoning

    Hybrid reasoning lets a model spend less effort on simple questions and more effort on difficult ones. It requires two complementary controls:

    • Routing policy: Determines the reasoning level assigned to each request.
    • Reasoning budget: Caps the number of reasoning tokens each request can generate.

    Without a budget, excessively long reasoning can make latency and costs difficult to control.

    RLVR and GRPO

    Reinforcement Learning with Verifiable Rewards (RLVR) uses rules or programs to score answers.

    Group Relative Policy Optimization (GRPO) samples a group of responses to the same question and uses relative rewards within that group instead of a separately trained value network.

    These approaches can be efficient for verifiable tasks such as mathematics and coding, provided that the verifier covers a sufficiently broad range of problems and the model cannot easily exploit loopholes in it.

    2. Estimate GPU Memory Requirements

    Before deployment, estimate memory requirements by adding three components: model weights, the KV cache, and runtime overhead.

    Model Weight Memory

    1. Weight memory = Parameter count × Bytes per parameter

    FP16 and BF16 use 2 bytes per parameter, INT8 uses 1 byte, and INT4 uses approximately 0.5 bytes.

    For example, a 70B-parameter model loaded in FP16 requires approximately 140 GB for its weights.

    KV Cache Memory

    1. KV cache memory =
    2. 2 × Number of layers × Number of KV heads × head_dim
    3. × Bytes per element × Tokens per sequence × Concurrent sequences

    The factor of 2 accounts for storing both keys and values.

    For MLA, replace 2 × Number of KV heads × head_dim with the latent-vector dimension plus the decoupled RoPE dimension.

    Runtime Overhead

    Runtime overhead includes activations, CUDA graphs, and communication buffers. As a planning guideline, reserve approximately 10% of total GPU memory for overhead and keep at least 2–5 GB of headroom.

    The memory available for the KV cache is:

    1. Available KV cache memory =
    2. Total GPU memory × Utilization cap − Weight memory − Runtime overhead

    The theoretical concurrency limit is:

    1. Theoretical concurrency limit =
    2. Available KV cache memory ÷ KV cache memory per sequence

    Worked Example

    Consider eight GPUs with 80 GB of memory each:

    • Total GPU memory: 640 GB
    • Memory utilization cap: 0.9
    • Memory available under the cap: 576 GB
    • Model weight memory: 140 GB
    • Estimated runtime overhead: 40 GB

    The remaining memory available for the KV cache is:

    1. 576 − 140 − 40 = 396 GB

    Assume the model has 80 layers, 8 KV heads, a head_dim of 128, and an FP16 KV cache.

    The KV cache footprint per token is:

    1. 2 × 80 × 8 × 128 × 2 = 327,680 bytes

    That is approximately 0.33 MB per token. A sequence containing 4,096 tokens requires approximately 1.34 GB, giving a theoretical concurrency limit of:

    1. 396 ÷ 1.34 ≈ 295 concurrent sequences

    This is a theoretical ceiling, not a production target. Paged allocation can reduce fragmentation, but actual concurrency will be lower if the service reserves cache capacity based on the maximum output length.

    3. Use Load Testing to Set the Concurrency Limit

    1. Build a representative test set. Match the real distribution of input lengths, output lengths, and reasoning lengths. Do not test only fixed-length requests.
    2. Increase concurrency in stages. For example, test 8, 16, 32, and 64 concurrent requests. Run each stage for at least 10 minutes and record the results.
    3. Track serving metrics at every stage. Record P50 and P95 time to first token (TTFT), time per output token (TPOT), output throughput in tokens per second, timeout rate, and KV cache utilization.
    4. Choose the highest concurrency that meets your service-level objectives (SLOs). Then leave approximately 20% headroom when setting the production limit.
    5. Investigate sudden increases in timeouts. Check, in order, whether requests are queuing, whether the KV cache is nearly full, and whether preemption or recomputation has been triggered.

    Metric Definitions

    TTFT: The time from sending a request to receiving the first token.

    TPOT: The average interval between output tokens after the first token:

    1. TPOT = (End-to-end latency − TTFT) ÷ (Number of output tokens − 1)

    KV cache utilization: The fraction of cache blocks currently occupied:

    1. KV cache utilization = Occupied cache blocks ÷ Total cache blocks

    4. Route Hybrid Reasoning Requests and Set Token Budgets

    A typical routing policy uses three levels:

    • Simple questions: Answer directly.
    • Moderately difficult questions: Allocate a small reasoning budget.
    • Complex questions: Allocate a larger reasoning budget.

    Routing signals can include question length, task-type labels, historical failure rates, or the output of a lightweight classification model.

    Set an upper limit on the reasoning budget in tokens. The cost of an individual request can be estimated as:

    1. Request cost =
    2. (Input tokens ÷ 1,000,000 × Input price)
    3. + ((Reasoning tokens + Output tokens) ÷ 1,000,000 × Output price)

    Here, input and output prices are quoted per million tokens.

    For an illustrative example, assume an input price of CNY 1 per million tokens and an output price of CNY 4 per million tokens. A request contains:

    • 1,000 input tokens
    • 3,000 reasoning tokens
    • 500 output tokens

    Its estimated cost is:

    1. 1,000 ÷ 1,000,000 × 1
    2. + 3,500 ÷ 1,000,000 × 4
    3. = 0.001 + 0.014
    4. = CNY 0.015

    Routing controls how much reasoning a request receives; token budgets keep that reasoning within a defined cost and latency envelope.