Open-Source LLM Architecture: Dense, MoE, and Memory Optimization
This article explains why mature open-source LLM projects often release model families rather than a single model. It compares compact edge models, medium-sized Mixture-of-Experts (MoE) models, and large Dense models, while focusing on memory and bandwidth constraints, KV cache growth, GQA/MQA, KV cache quantization, cross-layer KV sharing, MoE versus Dense trade-offs, and a practical path from model selection to cloud deployment.
Introduction: Why open-source models are released as families
In recent years, open-weight models have gradually become a mainstream choice for private deployment and secondary development in enterprises. Compared with models that offer only a single specification, mature open-source projects often release an entire “family” at once, covering different compute scenarios ranging from edge devices to data centers.
This approach is driven by engineering considerations. Different deployment environments place very different demands on latency, memory, memory bandwidth, and model quality. Using one parameter configuration to fit every scenario is neither realistic nor economical.
A typical open-source model family can usually be divided into three main lines:
- Ultra-compact models for mobile, embedded, and IoT devices;
- Medium-sized models that adopt a Mixture-of-Experts (MoE) architecture for efficient local inference;
- Large models that adopt a Dense architecture and focus on higher model quality and easier post-training.
Understanding the positioning and trade-offs of these three architecture types is the first step in model selection.
1. Design logic behind a layered product line
Making models available as a family instead of a single offering addresses the tension between compute cost and model quality.
Edge/embedded compact models target environments such as mobile phones, vehicles, and IoT devices. These environments are often limited by memory, lack active cooling, and require low power consumption. These models are highly sensitive to parameter count and often need to compress capabilities into hundreds of millions of parameters, while maintaining basic understanding and generation quality with minimal memory usage.
Medium-sized MoE models target scenarios where efficient local inference can be achieved with a single GPU or a small number of GPUs. They attempt to decouple parameter count from activated computation, balancing “large capacity” with “fast inference.”
Large Dense models target scenarios with higher requirements for generation quality, complex reasoning, and post-training friendliness. These models are often instruction-tuned and aligned by specialized teams.
This layered structure allows users to match models to their hardware conditions rather than forcing small hardware to run overly large models or wasting high-end hardware on overly small models.
2. Core challenges of compact edge models: memory and bandwidth
When deploying large models on constrained devices, the real bottleneck is often not compute power, but memory usage and memory bandwidth. In particular, KV cache is a major source of memory growth in long-context scenarios.
During autoregressive generation, the model needs to cache the Key and Value tensors for each layer and each generated token to avoid repeated computation. As the context length increases, the KV cache grows linearly and can easily exceed the size of the model weights themselves.
To mitigate this issue, several general optimization techniques have emerged in the industry:
- Grouped-Query Attention (GQA) / Multi-Query Attention (MQA): Multiple Query heads share the same group of Key/Value heads, directly reducing KV cache size.
- KV cache quantization: Key/Value tensors are stored in lower precision, trading memory for accuracy.
- Cross-layer KV cache sharing: Later layers reuse Key/Value states already computed by earlier layers, reducing repeated storage and computation. This idea is not unique to a single model, but is one of the general directions explored when simplifying attention structures.
The following simplified example illustrates the idea of cross-layer KV sharing:
# Simplified illustration: cross-layer KV sharingclass SharedKVAttention:def __init__(self, num_layers, share_from_layer=1):self.shared_kv = {} # Only some layers compute and store KVself.share_from_layer = share_from_layerdef forward(self, x, layer_idx):if layer_idx < self.share_from_layer:# The first few layers compute and cache KV normallyk, v = self.compute_kv(x)self.shared_kv[layer_idx] = (k, v)else:# Later layers reuse KV from the previous layer, avoiding repeated computation and storagek, v = self.shared_kv[layer_idx - 1]return self.attention(x, k, v)
It is important to note that cross-layer sharing can reduce expressive capability to some extent. Therefore, it is more suitable for compact models that are extremely sensitive to memory usage, rather than large models that prioritize maximum quality.
3. Trade-offs between MoE and Dense
The core idea of the Mixture-of-Experts (MoE) architecture is “sparse activation”: the model has a large total number of parameters, but each token activates only a subset of expert networks.
Its advantages include:
- More parameters can be accommodated under similar compute budgets, resulting in higher theoretical capacity;
- During inference, the number of activated parameters is much smaller than the total parameter count, giving it a natural speed advantage.
However, MoE also has clear costs. Memory must still hold all expert weights, routing mechanisms increase engineering complexity, and post-training, including fine-tuning and alignment, is more difficult. Issues such as uneven expert distribution and unstable routing can make tuning more challenging.
The Dense architecture sends every token through all parameters. Its drawback is that compute and parameter count are tightly coupled. However, it also offers clear benefits: simpler structure, mature and stable training and post-training workflows, and more predictable behavior.
For this reason, scenarios that prioritize high quality and easy customization often still choose large Dense models as the base model.
A practical decision principle is:
Choose MoE when local inference efficiency is a priority and tuning complexity is acceptable; choose Dense when generation quality and post-training friendliness are priorities and sufficient compute resources are available.
4. From model selection to cloud deployment: a practical path
Once the architecture is selected, deployment is equally important. The following is a general practical path:
Capability alignment and evaluation: Compare candidate models using evaluation sets that are close to the business scenario, instead of judging models only by parameter count.
Inference optimization: Enable common inference optimizations such as KV cache quantization and continuous batching to improve throughput.
Elastic hosting: Deploy models on container platforms or model hosting services, and use auto-scaling to handle traffic fluctuations.
Observability: Integrate logging and monitoring/alerting services, and continuously observe key metrics such as time to first token, generation throughput, and memory usage.
Data and knowledge augmentation: For private-domain knowledge scenarios, Retrieval-Augmented Generation (RAG) and vector retrieval capabilities can be used to improve the model’s upper bound.
On Baidu AI Cloud, these steps can typically be implemented using common product categories such as container platforms, object storage, model hosting, logging services, and monitoring/alerting. This allows developers to focus on model selection and performance tuning instead of building infrastructure from scratch.
Summary
The family-style design of open-source large models is essentially about balancing compute constraints and quality requirements. Compact edge models reduce memory usage through mechanisms such as GQA, KV cache quantization, and cross-layer sharing. MoE improves efficiency and capacity through sparse activation. Dense models trade simpler computation for quality and post-training friendliness.
By understanding the applicability and engineering cost of each architecture, and combining them with reasonable inference optimization and cloud deployment practices, developers can build reliable and scalable LLM applications under limited resources.
