MoE and MLA Explained: How They Reduce GPU Memory and Compute Costs

    Mixture of Experts (MoE) and Multi-Head Latent Attention (MLA) address different bottlenecks in large language models. Using DeepSeek-V2 as a reference, this article explains how MLA compresses the KV cache, how fine-grained MoE reduces per-token computation, and how to estimate memory requirements, validate long-context performance, and plan a multi-GPU deployment.

    As large language models grow, so do their training and inference costs. GPU memory and compute capacity are two major constraints. Mixture of Experts (MoE) and Multi-Head Latent Attention (MLA) address these constraints in different ways.

    Using the open-source MoE model described in the DeepSeek-V2 technical report as a reference, this article explains what each technique optimizes and provides practical calculations for model selection and deployment.

    Reported model specifications and benchmark results are from the DeepSeek-V2 technical report, arXiv:2405.04434. The memory calculations below are illustrative estimates. Other models and deployments require their own measurements.

    1. Key Takeaways

    1. MLA primarily reduces the inference KV cache. It caches compressed latent representations rather than full keys and values.
    2. Fine-grained MoE activates only a small subset of experts for each token. This allows a model to have many total parameters while using substantially fewer parameters per computation.
    3. MLA and MoE target different costs. MLA reduces KV-cache memory requirements; MoE reduces per-token computation. MoE does not, by itself, reduce the memory required to store model weights.

    2. How MLA Reduces GPU Memory Requirements

    Standard Multi-Head Attention (MHA) stores a key and a value for every attention head, token, and layer. As the sequence grows, this KV cache consumes more GPU memory.

    MLA uses low-rank compression to jointly represent keys and values as a lower-dimensional latent vector. During inference, the model caches this compressed representation. Up-projection matrices map it back to the key and value representations needed for attention.

    There is an important detail: the keys associated with Rotary Position Embeddings (RoPE) must also be retained. The report calls these decoupled RoPE keys. They are part of the cache, so the memory estimate must include both the compressed latent vector and the positional keys.

    An Illustrative KV-Cache Calculation

    Assume a model has:

    • 60 layers
    • A compressed latent dimension of 512
    • A decoupled RoPE key dimension of 64
    • bf16 storage, using 2 bytes per value

    The approximate KV-cache requirement per token is:

    1. KV cache per token
    2. = (latent dimension + RoPE key dimension) × bytes per value × layers
    3. = (512 + 64) × 2 × 60
    4. = 69,120 bytes
    5. ≈ 67.5 KiB

    A sequence of roughly 128,000 tokens would therefore require approximately 8.8 GB of KV-cache memory.

    For comparison, standard MHA with the same 60 layers, 128 attention heads, and 128 dimensions per head would require:

    1. KV cache per token
    2. = 2 × attention heads × head dimension × bytes per value × layers
    3. = 2 × 128 × 128 × 2 × 60
    4. = 3,932,160 bytes
    5. ≈ 3.9 MB

    At the same sequence length, that is approximately 500 GB of KV-cache memory. This illustrative comparison shows a reduction by a factor of several dozen—the main value of MLA.

    KV-cache memory grows approximately linearly with sequence length. Doubling the sequence length roughly doubles the cache requirement, making this relationship central to long-context memory planning.

    3. How Fine-Grained MoE Reduces Computation

    Traditional MoE designs typically use a smaller number of larger experts. Fine-grained MoE divides the expert capacity into more numerous, smaller experts, while retaining shared experts for common knowledge. A router selects a limited set of experts to process each token.

    According to the DeepSeek-V2 report, the model has approximately:

    • 236 billion total parameters
    • 21 billion activated parameters per token
    • An activation ratio of 8.9%

    Total parameter count reflects the model’s overall capacity, while activated parameter count is a key factor in per-token computation.

    However, sparse activation does not eliminate the need to store the other experts. In the GPU-resident deployment considered here, all expert weights still need to fit in GPU memory.

    At bf16 precision, the raw weight storage is approximately:

    1. Weight memory
    2. = total parameters × bytes per parameter
    3. = 236 billion × 2
    4. = 472 GB

    Before considering compute efficiency, confirm that the multi-GPU configuration can accommodate the full model weights.

    4. What Long-Context Support Requires

    The report describes a shorter context window during pretraining. Support for 128K context was introduced through additional context-extension training and positional encoding extension.

    A stated 128K context window is therefore not a substitute for application testing. Long-context capability should be validated against the tasks the model will actually perform.

    Before deployment, test at three input lengths:

    • 4K tokens
    • 32K tokens
    • 128K tokens

    For each length, record:

    • Time to first token
    • Generation throughput in tokens per second
    • Peak GPU memory usage

    If the application focuses on document question answering or code repository understanding, use representative documents and repositories rather than relying only on general benchmarks.

    5. Training Data and Reported Efficiency

    The report describes approximately 8.1 trillion pretraining tokens, drawn from sources including web pages, books, code, and academic papers.

    The supervised fine-tuning stage used approximately 1.5 million SFT examples, including instruction-following, mathematical reasoning, and code-generation samples.

    Compared with the preceding dense model, DeepSeek 67B, the report states that DeepSeek-V2 achieved:

    • 42.5% lower training costs
    • Maximum generation throughput of up to 5.76×

    These are the report’s results, subject to its cluster, configuration, and measurement methodology. Results on other hardware or inference frameworks may differ substantially.

    6. Deployment Checklist

    1. Estimate Total GPU Memory

    Start with:

    1. Required GPU memory
    2. ≈ model weights
    3. + concurrent sequences × KV cache per sequence
    4. + activations and communication buffers

    For quantized weights, calculate weight storage using the actual bit width rather than assuming bf16.

    2. Size Batches and Concurrency Around Context Length

    First calculate the cache required for one sequence at the target maximum length. Then work backward to determine how many concurrent sequences the deployment can support.

    Using the illustrative MLA estimate above, eight concurrent sequences of roughly 128K tokens require:

    1. KV-cache memory
    2. ≈ 8 × 8.8 GB
    3. ≈ 70 GB

    That figure covers only the KV cache. Model weights and other runtime memory still need to be included.

    3. Plan the Communication Topology

    Expert parallelism introduces All-to-All communication. Where possible, place experts in the same group within a node that has high-bandwidth interconnects.

    Limiting the number of devices a token can access can also reduce cross-node traffic.

    4. Monitor Memory and Serving Performance

    At a minimum, monitor:

    • GPU memory utilization
    • KV-cache utilization
    • Time to first token
    • Generation throughput in tokens per second
    • Request queue length

    For example, sustained GPU memory utilization above 90% could trigger capacity expansion or request throttling.

    5. Keep Load-Test Records

    Save latency measurements and peak memory usage for every tested input length. These records provide a practical basis for future capacity planning and model selection.

    7. When to Choose MoE with MLA

    MoE combined with MLA is well suited to workloads that require long contexts and high throughput, especially when a multi-GPU cluster is already available.

    For applications that must run on one GPU or primarily handle short requests, a smaller dense model often has lower deployment and debugging overhead.

    The DeepSeek-V2 report includes evaluations on Chinese and English benchmarks, but individual scores depend on the datasets and evaluation settings. Assess multilingual performance using your own application data rather than relying solely on public leaderboards.

    For workloads that require specialized domain knowledge, Retrieval-Augmented Generation (RAG) can supply external information. Retrieval quality directly affects the final answer and should be evaluated separately as well as together with the model.

    The central deployment distinction is straightforward: MLA reduces the memory cost of maintaining context, while MoE reduces the computation performed for each token. Realizing both benefits requires enough memory for the model weights, an appropriate communication topology, and workload-specific testing.