Deep Dive into Large-Scale MoE Architecture: Long-Context Compressed Attention in Practice

    This article breaks down a typical trillion-parameter sparse language model architecture, focusing on how it handles million-token contexts. It covers the overall layer design, compressed sparse attention, recompressed attention, sparse expert routing, and multi-token prediction, along with the engineering trade-offs behind memory, compute, and throughput. Readers will find practical guidance on capacity planning, long-context evaluation, and deployment observability.

    1. Why Attention Needs Rebuilding at the Trillion-Parameter Scale

    When model parameters reach the trillion level and context windows expand to millions of tokens, the bottleneck shifts from “can we compute it?” to “can we store it and fetch it fast enough?” Attention complexity grows quadratically with sequence length, and during autoregressive decoding, every generated token requires repeatedly reading the historical key-value cache. Memory bandwidth often becomes the ceiling before compute does.

    As a result, the design focus of modern large-scale sparse models has moved away from simply stacking parameters toward three goals:

    • Using sparse activation to control the compute cost per token.
    • Using compression and retrieval to control the size of the context state.
    • Using more stable inter-layer connections to keep deep networks trainable.

    This article uses a typical large-scale sparse language model architecture as a sample. It breaks down five parts: the overall computation flow, compressed sparse attention, recompressed attention, mixture of experts, and multi-token prediction, and highlights the key engineering trade-offs.

    2. Overall Architecture: Parameter Scale, Sparse Activation, and Layer Orchestration

    A typical model of this kind is a pure language model with trillions of total parameters, while only tens of billions of parameters are activated per token. The sparse activation ratio is roughly thirty to one. This means memory usage for both training and inference is determined by total parameters, while compute consumption is determined by activated parameters. The two must be planned separately for capacity.

    Structurally, the model usually consists of around sixty repeated blocks, each containing one attention sub-layer and one expert sub-layer. The attention sub-layer is not a single mechanism; three types alternate:

    • Compressed sparse attention: processes local and compressed global information at a lower compression ratio, with retrieval-based filtering.
    • Recompressed attention: maintains ultra-long-range context summaries at a very high compression ratio.
    • Sliding window attention: interacts only with tokens inside the window, used in shallow or final layers.

    The expert sub-layer also comes in two types. Shallow layers use hash-routed expert layers, where routing is mapped directly from token identifiers, with a fixed communication pattern and no learned gating. The remaining layers use standard gated sparse expert layers, where a linear score plus normalization selects the top few experts. This combination of “deterministic routing in shallow layers plus learned routing in deep layers” provides more stable gradient paths early in training while preserving deep-layer capacity.

    Inter-layer connections use a residual variant with pre- and post-processing. The sub-layer output first goes through a pre-processing operator for normalization and projection, is then added to the main path, and finally scaled by a post-processing operator. Compared with a plain residual, the two extra operators buy more controllable activation magnitudes in deep training, at the cost of a small amount of extra read/write overhead per layer.

    At the end of the model, a multi-token prediction head is typically attached. It supervises tokens at several future positions in a single forward pass, increasing training signal density and providing a draft source for speculative decoding during inference.

    3. Compressed Sparse Attention: Designing a Dual-Channel Key-Value State

    The core of compressed sparse attention is splitting the key-value state into two channels: one for fine-grained local information and one for coarse-grained global information.

    The first path is the sliding window channel. Each token computes attention only with tokens inside the window. The key-value cache is fixed at the window size and is overwritten in a ring once full. During decoding, the input shape simplifies to a hidden vector for a single token, which is written into the window cache after a downsampling projection. One easily overlooked detail: positional encoding is applied only to the first few dimensions of the channel dimension; the remaining dimensions are not rotated but go through a quantization path. This keeps position-sensitive information concentrated in a low-dimensional subspace, so quantization error does not break relative position relationships.

    The second path is the compressed channel. Historical states are fed into a compressor in blocks, producing a fixed number of compressed key-value tokens each time, which are written into the compressed cache. An index module then scores the compressed tokens, and only the top-scoring few participate in attention. The overall logic can be summarized with pseudocode:

    1. # Decoding stage: q is the query of the current token
    2. k_win, v_win = kv_downsample(x) # shape [1, head_dim]
    3. k_win = rope(k_win, dims=rope_head_dim) # rotate only the first few dims
    4. k_win = quantize(k_win) # quantize the remaining dims
    5. window_cache.ring_write(k_win, v_win) # ring overwrite
    6. c_kv = compressor(history_blocks) # output T compressed key-values
    7. scores = index_module(q, c_kv) # lightweight scoring against query
    8. keep = topk(scores, k=top_k) # keep no more than top_k
    9. ctx = concat(window_cache.read(), c_kv[keep])
    10. out = softmax(q @ ctx.T) @ ctx.v

    Three engineering points matter here: the compressor block size determines the balance between temporal resolution and compute; index scoring should use a low-dimensional projection to reduce overhead; and the number of retained candidates should have an upper bound to avoid candidate set growth on long sequences.

    4. Recompressed Attention: The Summary Channel for Extremely Long Contexts

    Recompressed attention pushes the compression ratio to 128:1 or higher, with each compression unit summarizing hundreds of original tokens. It does not handle detail retrieval. Instead, it provides a coarse-grained signal of “what happened long ago,” preventing deep layers from losing the topic in extremely long contexts.

    Implementation points include: the compressor usually uses a pooling or convolution structure with positional information, avoiding summaries from different positions being averaged into meaningless vectors; the compressed cache and window cache are managed separately, with the former growing slowly and staying resident long-term, and the latter fixed in length and frequently overwritten; the two cache types must be unified to the same dimension and quantization format before concatenation, otherwise unnecessary conversion overhead is introduced.

    5. Sparse Expert Layers and Routing Strategy

    A standard sparse expert layer uses a gating network to select a few experts for each token, normalizes the weights, and computes a weighted sum. Load balancing must be handled in engineering. A common approach is an auxiliary loss that constrains the selection probability of each expert, combined with a capacity limit and overflow dropping strategy to prevent individual experts from becoming hotspots.

    The hash-routed layer uses the hash of the token identifier to directly determine the expert index. Routing requires no parameters and is deterministic. Its value lies in stable parallelism and predictable communication, making it suitable for shallow layers handling general feature transformations. The trade-off is that it cannot adapt based on semantics, so it should not be stacked too deeply.

    6. Multi-Token Prediction and Inference Acceleration

    The multi-token prediction head predicts several future positions during training, equivalent to adding denser supervision to the model. During inference, it can serve as a draft model, working with verification-based decoding to accept multiple tokens at once. The balance between draft length and acceptance rate matters: too long a draft lowers the acceptance rate and adds wasted computation.

    7. Deployment Evaluation Checklist

    • Memory budget: total parameters determine weight usage, activated parameters determine compute, and the cache is determined jointly by window length, compression ratio, and the filtering upper bound.
    • Long-text quality: validate detail retrieval and global topic retention separately, avoiding a single metric.
    • Throughput and latency: watch bandwidth usage during decoding, keep the compressed cache resident as much as possible, and reduce repeated data movement.
    • Observability: build monitoring and alerts for routing distribution, cache hit rate, and compressed summary quality.
    • Deployment shape: container platforms host inference services, object storage holds weights, message queues smooth peaks, and logging and monitoring systems track long-term stability.

    Overall, the takeaway from this architecture is treating context as a first-class citizen that can be compressed and retrieved, using hybrid attention and hybrid routing to make explicit trade-offs among capacity, compute, and memory, rather than relying on a single mechanism to solve everything.