Trillion-Parameter MoE Multimodal Agents: Architecture and Engineering Practices
This article explores the architecture and engineering practices behind trillion-parameter Mixture of Experts (MoE) multimodal agents. It covers the transition from early multimodal stitching approaches to native multimodal training, the implementation of sparse MoE architectures to balance model capacity and computational cost, and optimization techniques for KV caching in long-context scenarios using Multi-head Latent Attention.
1. Technical Background of Native Multimodal Agents
Early multimodal systems mostly adopted a stitching approach. Vision models first converted images into text descriptions or feature vectors, which were then handed over to language models for processing. The problem with this approach is that information is lost at the stitching point. For example, the row and column relationships in tables, numerical trends in charts, and interface hierarchies in screenshots are difficult to preserve completely.
The native multimodal approach is to jointly train vision and text within the same set of parameters. Images are split into visual tokens, which enter the same Transformer alongside text tokens. The model can thus directly understand screenshots, scanned documents, and pages with mixed text and graphics, and associate visual information with textual information during the inference process.
For agents, this capability is even more significant. Agents need to read interface screenshots, identify anomalies in reports, understand flowcharts, and then decide which tool to call. If visual understanding and planning decisions are separated, information gaps may occur at each step. Therefore, multimodal capabilities are typically designed together with tool-calling capabilities, rather than being an add-on feature.
2. Sparse MoE Architecture: Trading Total Parameters for Capacity, Controlling Cost with Activated Parameters
Dense models call all parameters when processing each token, and the computational load is proportional to the parameter scale. The Mixture of Experts (MoE) architecture splits the feed-forward network into multiple experts, with a router selecting a small number of experts to participate in the computation for each token.
This design involves two parameter metrics:
- Total parameters: Determines the model’s knowledge capacity and the storage requirements for weights.
- Activated parameters: Determines the computational load for a single token, directly affecting inference latency and throughput.
The ratio between the two is called the activation ratio, which is the activated parameters divided by the total parameters. A lower activation ratio saves more compute per step, but routing design and load balancing become more critical. If most tokens are routed to a few experts, those experts will become overloaded while the rest remain idle for long periods, preventing the model capacity from being fully utilized.
The following code illustrates the basic computation process of top-k routing:
import numpy as npdef topk_route(hidden: np.ndarray, gate_weight: np.ndarray, k: int = 8):"""hidden: [tokens, dim]; gate_weight: [dim, experts]"""logits = hidden @ gate_weight # [tokens, experts]topk_idx = np.argsort(-logits, axis=-1)[:, :k] # Select k experts for each tokentopk_logits = np.take_along_axis(logits, topk_idx, axis=-1)weights = np.exp(topk_logits - topk_logits.max(-1, keepdims=True))weights /= weights.sum(-1, keepdims=True) # Weight the outputs of selected expertsreturn topk_idx, weights
In engineering implementation, auxiliary loss or capacity factor must also be added to prevent expert load imbalance. These details determine whether the MoE model can be trained stably and also affect the parallelism efficiency during inference.
3. Attention and KV Cache: The Main Cost of Long Context
During autoregressive generation, the model needs to save the Keys and Values of historical tokens, known as the KV cache. The cache size is proportional to the context length, number of layers, and number of attention heads. Therefore, long contexts are often limited by memory capacity first, rather than compute capacity.
The number of bytes occupied by each token can be estimated using the following formula:
Bytes per token = 2 × layers × KV heads × head dimension × bytes per element
Here, the coefficient 2 indicates that both K and V are saved. Let’s demonstrate the calculation with a set of hypothetical parameters: 61 layers, 64 attention heads, head dimension of 128, and 16-bit floating-point storage.
- Each token takes about 1,998,848 bytes, or about 2MB.
- 256K (262,144) tokens require about 524GB (decimal).
This scale indicates that standard multi-head attention is difficult to deploy directly in ultra-long context scenarios.
A common optimization is the approach of Multi-head Latent Attention, which compresses K and V into a low-dimensional latent vector. Only this vector is saved in the cache, and it is projected back during computation. Assuming a latent dimension of 512, plus 64 dimensions for positional encoding, the cache size becomes:
576 × 61 × 2 = 70,272 bytes, or about 70KB per token.
At a 256K context, this is about 18.4GB. Compared to the previous standard approach, the cache is reduced by about 28 times. Two points to note: first, this compression structure must be determined during the model training phase; existing standard attention models cannot directly apply it. Second, projecting back adds a small amount of computation, which needs to be weighed based on hardware conditions.
The following functions can be used to quickly estimate the cache requirements for different configurations:
def kv_cache_bytes(tokens: int, layers: int, kv_heads: int,head_dim: int, bytes_per_elem: int = 2) -> int:per_token = 2 * layers * kv_heads * head_dim * bytes_per_elemreturn per_token * tokensdef latent_cache_bytes(tokens: int, layers: int, latent_dim: int,bytes_per_elem: int = 2) -> int:return latent_dim
