Manuscript Offprint

arXiv:2610.31818

Nested Sparsity for Scalable Pretraining and Continual Learning

Tianzhe Chu, Druv Pai, Yi Ma

Abstract

Mixture-of-experts (MoE) transformers, the standard backbone for modern large-scale deep learning, employ conditional computation: only a sparse subset of model weights is used in each forward pass. In MoE transformers, this sparsity arises in the MoE operator, which generalizes the multi-layer perceptron in the classical transformer. In this work, we explore an alternative mechanism for conditional computation that analogously generalizes the attention operator. The sparsity induced by this mechanism naturally nests (i.e., hierarchically composes) with the sparsity induced by the MoE operator. We show that a transformer employing this nested sparsity mechanism, which we call NESTOR, performs better than a conventional MoE transformer at equal compute and equal sparsity in language model pretraining. We also show that NESTOR is well-suited for continual pretraining: it can be adapted to longer contexts more effectively than conventional MoEs, as well as adapt more efficiently to new domains while forgetting less on old domains. Furthermore, we demonstrate that these benefits persist as model size, dataset size, and context length grow. Our results demonstrate the potential of alternative conditional computation mechanisms for deep neural network architectures. Code and checkpoints are available on GitHub.

1 Introduction

Conditional computation in LLMs. Large language models (LLMs) based on variants of the transformer architecture (Vaswani et al., 2017) improve predictably as their parameters, training data, and training compute grow (Kaplan et al., 2020; Brown et al., 2020; Hoffmann et al., 2022). A central goal of architecture design is to reach a lower loss with the same training compute. A particularly successful approach is conditional computation (Bengio et al., 2013; Shazeer et al., 2017), in which each input uses only a small (input-dependent) “active” subset of the model weights and correspondingly less compute for training or inference. In this setting, the number of total parameters grows much faster than the active parameters and compute used on each token.

Mixtures of experts (MoEs). In modern LLMs, this form of conditional computation is implemented almost exclusively via the MoE operator (Jacobs et al., 1991; Shazeer et al., 2017; Lepikhin et al., 2021; Fedus et al., 2022), which replaces the multi-layer perceptron (MLP) in each transformer layer by many expert MLPs and routes each token to a few of them. MoE transformers need substantially less compute than

∗^{*}Equal contribution; ordered alphabetically.

Figure

(a) The nested sparsity mechanism in NESTOR.

(b) The effective attention map computed by each column.

Figure 1: Nested sparsity in NESTOR. (a) The NESTOR architecture. In each block, the column router sends the token xjx_j to Kc=2K_c = 2 of the C=4C = 4 columns. Each column has MM layers, each with its own attention and its own E=4E = 4 experts, of which a per-layer expert router selects Ke=2K_e = 2. The token uses only the colored parameters. (b) The causal attention map computed by each column for S=8S = 8 tokens, shown in terms of the whole sequence: column cc computes attention only among the tokens Uc\mathcal{U}_c routed to it.

dense transformers to reach the same loss (Fedus et al., 2022; Clark et al., 2022), and this advantage is retained at scale when the experts are configured appropriately (Ludziejewski et al., 2024; Abnar et al., 2025). As a result, many modern LLMs are MoE transformers (Jiang et al., 2024; DeepSeek-AI, 2024; Yang et al., 2025; Kimi Team, 2025a; OpenAI, 2025). In all of these models, however, this conditional computation is confined to the MLP weights: every token is processed by the same attention parameters in every layer. The attention operator accounts for a considerable fraction of the (active) parameters used by each token, and at long context lengths it accounts for most of the computation. One may then ask whether conditional computation can be applied to the attention operator as well, and how it should interact with the MoE.

Sparsity in the attention operator. Attention operators traditionally implement two forms of conditional computation. The more popular by far is the so-called “sparse attention” methods, which reduce the cost of attention by restricting the set of tokens to which each token attends, via fixed local patterns (Child et al., 2019; Beltagy et al., 2020), hashing or clustering (Kitaev et al., 2020; Roy et al., 2021), or a top-K selection for each query (Yuan et al., 2025; Lu et al., 2025; DeepSeek-AI, 2025). Relatedly, many LLMs interleave a few global attention layers with cheaper local or linear attention layers (Brown et al., 2020; Gemma Team, 2025; Kimi Team, 2025b). These methods sparsify the attention map, with the purpose of approximating full attention at a lower cost, while all tokens still share the same attention parameters. Other methods restrict the set of parameters used by each token, such as mixtures of attention heads (Zhang et al., 2022; Csordás et al., 2024), which route each token to a subset of the attention parameters, while every token still attends to the whole context. Other methods make both the map and the parameters sparse (Piekos et al., 2025), or pair mixtures of attention heads with an MoE (Shen et al., 2023; Csordás et al., 2024). In each of them, the attention is routed separately from the MLP and anew in every layer.

Continual pretraining. One of the circumstances in which such attention sparsity is most effective at reducing both training and inference costs is with long context documents. Pretrained models are routinely continually (pre-)trained in a long-context extension stage (Xiong et al., 2024; Fu et al., 2024; Grattafiori et al., 2024), and simultaneously further on new domains, such as code and mathematics (Gururangan et al., 2020; Rozière et al., 2023; Azerbaijan et al., 2024; Shao et al., 2024). Since neural networks trained on a new distribution tend to perform worse on the old one (McCloskey et al., 1989; French, 1999; Kirkpatrick et al., 2017), and LLMs are no exception (Luo et al., 2025), a central challenge is to maintain the performance on old data while capably handling the new data.

Our contributions. We propose NESTOR (Figure 1), a transformer architecture with nested sparsity: we generalize the attention operator in the same way that the MoE operator generalizes the MLP, and hierarchically composes the two resultant operators' sparsity. This yields a natural architecture that generalizes MoE transformers and has three coupled but distinct forms of sparsity: attention weight sparsity, attention map sparsity, and expert sparsity. We study it empirically and theoretically in language model pretraining and continual pretraining, showing that NESTOR architectures utilizing all sparsity axes in tandem outperform strong MoE and dense Transformer baselines at scales from 100M to 1B active parameters, at the limit of our compute resources. Namely, our contributions are:

• Compute-efficient pretraining (Section 3). We perform a scaling law study, finding that NESTOR is 1.13−1.29×1.13-1.29\times as compute-efficient as the MoE transformer, and 3.1−4.1×3.1-4.1\times as compute-efficient as a dense transformer; neither factor shrinks as the scale grows.

• Effective continual pretraining (Section 4). When adapted to various specialized domains, we show that NESTOR gains about as much as the MoE transformer on every new domain while forgetting less of the pretraining distribution, with fewer training FLOPs per token in the continual pretraining data. When extended from a context length of 4k to lengths up to 64k, NESTOR preserves its advantage over the MoE transformer when each model receives an equal number of continual pretraining tokens, and its advantage over the MoE transformer grows at longer context lengths when each model uses equal continual pretraining FLOPs.

• Theoretical analysis (Section 5). We give a theoretical account of these observations. Namely, we give a statistical model of data with hierarchical latent structure, in which a stylized version of NESTOR outperforms a stylized version of an MoE transformer. We show that this analytically tractable model reproduces the pretraining compute efficiency, its growth with the context length, and the reduced forgetting that we observe empirically.

Overall, our results suggest that conditional computation need not be confined to the MLP, and that adding and composing new sparsity mechanisms is a promising direction for architecture design. We postpone a detailed discussion of related work to Appendix A.

References (83)