Manuscript Offprint

arXiv:2611.03526

Library of Layers (LoL): Adaptively Ordering Transformer Layers at Runtime

Abdellah Rahmani, Sean McLeish, Ang Li, Sanae Lotfi, Pascal Frossard, Micah Goldblum

ABSTRACT

Computer programmers chain together the same reusable computational primitives, for example, conditional statements or loops, but in different orders to implement different algorithms. In contrast, existing language model architectures always execute their layers in the same order. We introduce Library of Layers (LoL), an architecture that dynamically orders its layers during inference to solve the problem at hand. LoL has a single hyperparameter, the size of its global library of transformer blocks, that allows it to transition seamlessly between parameter reuse and sparsity, generalizing the parameter-efficiency and FLOPs-efficiency tradeoffs of dense, depth-recurrent, and mixture of experts architectures in one unified family. We show that LoL achieves competitive performance with all three above baselines when pretrained on text data. On synthetic sudoku and maze data, we observe that the model employs distinct layer orderings to implement different solution strategies, and we can intervene on routing to force a model to apply a specific solution strategy to a problem instance. In a multilingual natural language setting, we find that LoL chooses different layer order patterns based on language and script. Similarly, we train LoL on safety data and fit probes to its router outputs, finding that these probes distinguish safe from unsafe text with high AUROC. LoL provides a path toward a future in which models dynamically compose their own computational graph, enabling more efficient and controllable reasoning.

1 INTRODUCTION

Many distinct algorithms are built from the same set of computational primitives, composed in different orders to implement different behaviors. For example, a database query engine can combine operations such as filtering, joining, sorting, and aggregation in different sequences depending on the query being executed. Modular programming lets the same reusable components be dynamically composed for many purposes within a single codebase. Transformer language models lack this flexibility; their layers are executed in a fixed, predefined order for every input. This rigidity may make these models less efficient, since useful computational primitives may need to be represented repeatedly at different depths.

Prior work has focused on increasing the sparsity of models using mixture of experts (MoE) (Shazeer et al., 2017; Fedus et al., 2022) or reusing layers multiple times during the forward pass in looped models (Dehghani et al., 2018; Lan et al., 2019; Geiping et al., 2026). Other work has investigated skipping layers dynamically during the forward pass (Elhoushi et al., 2024; Raposo et al., 2024; Bae et al., 2025). However, all of these approaches still enforce the order in which the layers of the model must be executed.

In this paper, we introduce Library of Layers (LoL), an architecture in which a small router, trained end-to-end without explicit supervision, dynamically selects the next layer from a global library of transformer blocks. We note this architecture encompasses the two properties explored by prior work. A small library of layers forces layer reuse, whereas a large library of layers allows many

*Equal contribution. Correspondence to: abdellah.rahmani@epfl.ch, smcleish@umd.edu Code: github.com/arahmani1/library-of-layers

Figure

Figure 1: We introduce Library of Layers (LoL), a novel architecture where the next executed transformer layer is chosen at runtime. We use strided window attention within the routed core to allow information to flow through the sequence dimension while using the same attention and MLP parameters for all tokens within each window.

layers to remain inactive on a given forward pass. By changing only the library size, LoL therefore spans a continuum from highly reused, dense computation to highly sparse computation. We visualize our novel architecture in Figure 1.

Dynamic layer selection introduces a challenge. We want to allow different token positions the flexibility to select different layer orderings, but this means that a vector at one token position extracted by a selected attention layer might attend to vectors at previous token positions extracted by different selected attention layers. We observe that this mismatch makes training unstable and harms performance. At the same time, if we chose a single layer ordering for the entire context, we would risk either breaking causality or making layer decisions with too little information (only the prompt).

We therefore introduce a windowed attention masking strategy that simultaneously allows different windows to adaptively select layer orderings while also preventing the above mismatch problem and preserving causality. We partition the context into windows, route each window using only information available from earlier windows, and shift the window boundaries across routed depths so that information can propagate between neighboring windows, expanding the field of view with depth.

We use synthetic and natural-language experiments to study and interpret our models' learned layer orderings. On sudoku and maze tasks, LoL learns distinct layer-ordering patterns for different solution strategies; forcing a strategy's learned routing path at inference time causes the model to follow that strategy. This phenomenon is especially clear in mazes, where all algorithms we consider use the same output syntax and differ only in the order in which they explore cells. We find that fixing layer ordering controls the cell exploration order itself. In multilingual language modeling, different languages use distinct execution paths, with languages that share scripts exhibiting similar routing patterns. We also train LoL on a mix of safe and unsafe data, finding that a ridge regression trained on routing features distinguishes these categories on held-out data with high AUROC. On general web text, we vary the library size to induce parameter reuse regimes with a FLOPs-to-parameters ratio comparable to that of looped transformers and also to induce sparse regimes with a FLOPs-to-parameters ratio comparable to MoE models, finding that LoL remains competitive across this range. Finally, at 1B parameters on math data, LoL is competitive with conventional dense transformers in terms of loss and benchmark accuracy in a FLOPs-matched setting.

These results suggest that layer ordering can become a learned part of the computation itself rather than a fixed architectural choice. LoL unifies behaviors associated with seemingly distinct dense, depth-recurrent, and MoE models by varying a single knob, the size of the layer library, while exposing the model's internal execution path. Because routing patterns can be extracted and intervened on, LoL also provides a simple mechanism for studying and controlling latent computation.

Our contributions are summarized as follows:

  • We propose Library of Layers (LoL), a novel architecture in which a router selects blocks to apply from a global library of transformer layers, making layer selection, ordering, and the number of times each layer is used conditional on the input.

  • We use maze, sudoku, multilingual, and safety training data to empirically show the LoL router learns input-adaptive patterns from data. Moreover, these router patterns can then be used to control the model at test time by forcing the layer execution ordering.

  • We demonstrate that LoL models perform competitively across a spectrum of sparsity levels, from low-sparsity looped models that repeatedly reuse the same layers to high-sparsity mixture of experts models.

References (70)