The Second Dimension of LLM Sparsity: Conditional Memory

    This article introduces conditional memory as a second, orthogonal dimension of sparsity in large language models, complementing conditional computation such as MoE. It explains why standard Transformers lack a native knowledge lookup primitive, defines conditional memory and its engineering constraints, and presents the U-shaped budget allocation rule between computation and memory. It also covers why gains extend to reasoning and long context, and provides a practical evaluation checklist.

    1. The Starting Point: Sparsity Only Solved Half the Problem

    Over the past few years, expanding the capacity of large language models has been almost synonymous with expanding conditional computation. Mixture of Experts (MoE) routes each input to a small number of experts, so the total parameter count can far exceed what a single forward pass actually activates. This trades fixed training and inference compute for larger model capacity. The approach has been highly successful in practice, but it covers only one dimension of sparsity: sparsely selecting how much computation to perform.

    The other half of the problem has long been ignored: how knowledge itself should be sparsely retrieved. A standard Transformer has no native knowledge lookup primitive. To answer questions like “what is the conventional collocation of a given term” or “what is the idiomatic way to use a given API,” the model can only “compute” the answer through multiple layers of matrix multiplication and attention. Simulating retrieval with computation essentially expands a table that should be an O(1) hit into a long chain of computation. The cost has three layers:

    • Wasted compute: local, high-frequency, static co-occurrence relations are recomputed repeatedly.
    • Capacity crowding: shallow layers are forced to reconstruct large amounts of memory, compressing the “effective depth” available to deep layers for abstraction and reasoning.
    • Interference accumulation: local noise propagates level by level once it enters the residual stream, and attention must spend extra capacity to resolve these local dependencies.

    So a natural question arises: can we introduce an orthogonal dimension of sparsity alongside conditional computation, called conditional memory, so that “what should be remembered is remembered, and what should be computed is computed”?

    2. Definition and Boundaries of Conditional Memory

    Conditional memory can be summarized as follows: using some features of the input as an index, it sparsely activates a small block of static parameters to perform a knowledge lookup directly, without going through the full computation graph. Its division of labor with conditional computation is clear:

    • Conditional computation (MoE): the input decides which compute units to invoke, producing results that depend on context.
    • Conditional memory (Engram-style modules): the input decides which memory slots to hit, producing static knowledge that is highly stable with respect to the training distribution.

    In engineering terms, this type of module modernizes the ideas behind modern N-gram embeddings: it maintains a learnable embedding entry for each n-gram key, and during the forward pass it uses hashing to map keys to slots, retrieving only the few matched vectors to participate in subsequent computation. Because lookup is independent of key length, the overall complexity is O(1) and does not grow linearly with sequence length. The illustrative code below only expresses the structure and is not a specific implementation:

    1. class ConditionalMemory(nn.Module):
    2. def __init__(self, num_slots, dim, ngram=2, topk=2):
    3. super().__init__()
    4. self.table = nn.Embedding(num_slots, dim)
    5. self.ngram, self.topk = ngram, topk
    6. def keys(self, token_ids): # build local keys
    7. k = self.ngram
    8. return [token_ids[:, i:i + k] for i in range(token_ids.size(1) - k + 1)]
    9. def forward(self, token_ids):
    10. hits = []
    11. for seg in self.keys(token_ids):
    12. h = hash_kernel(seg) % self.table.num_embeddings # stable hash
    13. idx = topk_select(h, self.topk) # sparse activation
    14. hits.append(self.table(idx))
    15. mem = reduce_sum(hits) / len(hits) # normalize to avoid magnitude drift
    16. return mem

    The engineering constraints must be emphasized, otherwise the benefits will be eaten up by implementation details: the hash must be stable and low-collision; when the key space is much larger than the number of slots, collisions must be handled (multiple hashes or checksum bits); lookup results must be normalized and injected into the residual stream in a gated manner to prevent numerical instability early in training; and key construction must be consistent with the tokenizer to avoid train/inference mismatch.

    3. How to Allocate the Sparsity Budget: The U-Shaped Scaling Rule

    With two dimensions that can be sparsified, the real question becomes budget allocation: given a total parameter count and total FLOPs, how much goes to computation and how much to memory? Related work formalizes this problem and observes a U-shaped rule: the horizontal axis is the memory fraction, the vertical axis is performance under the same budget, and the optimum is not at either end but somewhere in the middle. Both pure computation and pure memory are worse, and the optimal memory fraction shifts slowly to the right as the total budget grows.

    The practical implication is direct: in small models the memory module yields limited or even negative returns (the lookup overhead cannot be amortized), medium-scale models benefit most, and at very large scale the ratio must be recalibrated. Accordingly, scaling the memory module to about 27 billion parameters, under a strictly parameter-matched, FLOP-matched comparison, the overall performance is better than a pure MoE baseline. “Strict alignment” is key to such experiments: only when neither compute nor capacity is given an advantage is the conclusion credible.

    4. Why the Gains Spill Over to Reasoning and Long Context

    A counterintuitive but explainable phenomenon is that the memory module mainly improves knowledge-type metrics (e.g., MMLU +3.4, CMMLU +4.0), but the gains on reasoning, code, and math are larger (BBH +5.0, ARC-Challenge +3.7, HumanEval +3.0, MATH +2.4). The reasons come down to two mechanisms:

    1. Increased effective depth. Shallow layers no longer spend parameters and compute reconstructing static knowledge, so information arrives at deep layers “cleaner,” and deep layers can be used for composition and abstraction, which is equivalent to deepening the network’s effective depth.
    2. Freed attention capacity. Local dependencies are handed off to lookup, so attention no longer needs to handle neighborhood stitching and can focus on global context modeling, significantly improving long-context retrieval (multi-query “needle in a haystack” accuracy rises from 84.2 to 97.0).

    5. Deployment Evaluation Checklist

    • Whether the baseline is matched in parameters, FLOPs, data, and training steps;
    • Whether metrics cover all three categories—knowledge, reasoning, and long context—to avoid misjudging from a single-point improvement;
    • Whether hit rate and collision rate are observable, and whether monitoring alerts and logging services are configured;
    • Inference latency and memory: lookup is memory-access intensive, so bandwidth and cache locality must be evaluated;
    • Parameter hosting and versioning: large embedding tables are well suited to object storage with canary releases through a container platform;
    • Compatibility regression with existing conditional computation, positional encoding, and long-context schemes.

    6. Conclusion

    Conditional memory separates “knowledge lookup” from the computation graph and forms a complementary two-dimensional sparsity framework with conditional computation. It brings not only a lift in knowledge-type metrics, but also improved reasoning and long-context performance by freeing capacity in shallow layers and attention. For engineering teams, what truly needs to be mastered is not a particular module name, but the budget allocation method, the experimental design of strict controls, and observability support—this is also the foundation most worth building first when validating similar architectures on platforms such as Baidu AI Cloud.