Ultra-Large Hybrid-Attention Models: Key Architecture Choices for Reducing Long-Context Costs
This article explains how sparse Mixture-of-Experts (MoE) activation separates total parameter count from per-token compute, how to set expert routing capacity and load balancing loss, and how hybrid attention allocates compute across context segments by relevance. It also covers how to measure efficiency gains and estimate KV cache memory for long-context workloads.
Introduction: Why Long Context Is Hard
The compute of standard self-attention grows quadratically with sequence length n, and KV cache memory grows linearly with n. At one million tokens, simply increasing parameter count does not solve either problem. This article covers four questions: how to decouple parameters, how to compress attention, how to adapt to domestic (China-made) hardware, and how to calculate API costs. Each section provides formulas that can be applied directly.
1. Decoupling Total Parameters from Compute with Sparse Activation
An MoE model spreads its total parameters across many expert networks, and each token passes through only a few of them. Take a model with about 1.6 trillion total parameters and about 49B activated per forward pass as an example. Its activation ratio is 49 / 1600 ≈ 3%. The forward compute per token is therefore close to that of a dense model with the same activated size, while the model’s knowledge capacity is determined mainly by its total parameters.
This figure describes only the parameter structure. It does not represent any measured inference latency. Actual speed also depends on expert parallelism, communication overhead, and batch size.
1.1 Setting Routing Capacity
The processing capacity of each expert is usually calculated as:
capacity = ceil(capacity_factor × tokens × top_k / num_experts)
For example, suppose a batch contains 4,096 tokens, there are 64 experts, top_k is 2, and capacity_factor is 1.25. Each expert then processes at most ceil(1.25 × 4096 × 2 / 64) = 160 tokens. Tokens that exceed an expert’s capacity are usually skipped and pass only through the residual connection. A high drop rate hurts model quality. A larger capacity_factor reduces drops, but it increases expert compute and memory usage, so the two must be balanced.
1.2 Load Balancing Loss
A commonly used auxiliary loss takes the following form:
L_aux = α × N × Σ(f_i × P_i)
Here, N is the number of experts, f_i is the fraction of tokens assigned to expert i, and P_i is the average probability that the router assigns to expert i. The common value of α is around 0.01, and the exact value must be tuned on a validation set. An α that is too large suppresses expert specialization. An α that is too small can leave some experts idle for long periods.
2. Hybrid Attention: Allocating Compute by Context Segment
2.1 Two Compression Strategies
Hybrid attention applies compute of different intensity to context segments based on their distance and relevance.
Compressed sparse attention performs structured compression on historical context and keeps only summary representations. Fewer tokens take part in attention computation, and long-range dependencies can still be modeled.
Heavily compressed attention targets segments that are farther away and carry less information. It uses a higher compression ratio and suits background information.
The choice between the two strategies depends on relevance judgment. A coarser judgment carries a higher risk of misclassification, so thresholds must be validated with regression tests on the target task.
2.2 Conceptual Code Example
The pseudocode below illustrates the hierarchical logic only. It cannot be run directly.
# Conceptual example; undefined functions and thresholds must be implemented separatelydef hybrid_attention(query, context_segments):results = []for segment in context_segments:relevance = estimate_relevance(query, segment) # Scoring function; output range must be defined in advanceif relevance > HIGH_THRESHOLD:attn_out = standard_attention(query, segment)elif relevance > LOW_THRESHOLD:attn_out = compressed_sparse_attention(query, segment)else:attn_out = heavy_compression_attention(query, segment)results.append(attn_out)return aggregate(results) # Aggregation method must be defined in advance
You must implement the following parts yourself: the scoring method for estimate_relevance, the basis for choosing the two thresholds, the aggregation method for aggregate (a weighted sum or a normalized merge), and the caching strategy for compressed representations. Total overhead includes both the scoring cost and the compute cost of each attention type. Measure the two separately.
2.3 Measuring Efficiency Gains
Rather than citing a single multiplier, fix the test conditions and measure the results yourself. A useful measurement record includes the following:
- The baseline model and version;
- Context length, for example 128K, 512K, or 1M;
- Hardware model and quantity;
- Precision, for example bf16, fp8, or int8;
- Batch size and concurrency;
- How FLOPs and KV cache are measured.
You can estimate KV cache memory with the following formula:
KV memory = 2 × number of layers × number of KV heads × head dimension × sequence length × batch ×
