Depth Encodings Enable Deeper, More Stable and Efficient Transformers
TL;DRTransformer norm weights carry an emergent depth encoding: in 16 pre-trained LLMs, a layer’s depth can be read from its norm weights γ alone. LayerRoPE makes this encoding explicit, with a depth-conditioned magnitude and RoPE-style rotation of one shared gain. At Pre-Norm’s cost, it outperforms mHC and Attention Residuals on DepthBench, and reaches Pre-Norm’s 1.3B loss with 3.4× less compute. Surprisingly, it does this by amplifying residual variance (and regulating block I/O), rather than damping it.
A Transformer has one learned per-layer gain that acts directly on what each block reads from, or writes to, the residual stream: the normalization weight γℓ ∈ ℝᵈ. It scales the reads in Pre-Norm models, the writes in Post-Norm models, and both in Peri-Norm models. Every γℓ starts identical and is untied across layers, so any dependence on depth it shows is learned.
Across 16 open-weight LLMs from 9 families (dense, mixture-of-experts, parallel-branch and hybrid linear-attention; Pre-, Peri- and Post-Norm), the norm weights shift with depth in both magnitude and direction, systematically enough that a layer’s depth can be read from γℓ alone:
Magnitude is depth-conditioned. The mean and upper percentiles of γ shift systematically with depth: rising in 10 of the 16 models, and U-shaped in the other 6.
Direction is depth-conditioned. We observe that the normalized gain \gamma_\ell / \lVert \gamma_\ell \rVert also shifts steadily with depth. MDS projections, shown below, order the layers along a coherent arc: a clear directional shift with depth. This is not a rigid rotation, however. See1
Depth, decoded. We test this directly: fit how γℓ varies with depth on all other layers, decode the held-out layer’s depth, and compare against shuffled depths. Direction alone recovers depth to within a median of 1.1 layers, against 11.1 when shuffled, and beats every shuffle in every model (Figure 2).
LayerRoPE makes this depth encoding explicit. Instead of storing L independent vectors γℓ, it generates every layer’s gain from one shared γ ∈ ℝᵈ per site, through a depth-conditioned magnitude and a rotation spread across RoPE’s frequency spectrum:
LayerRoPE can thus be simply interpreted as a depth-conditioned scaling and rotation of γ in the complex plane. Reading the real gain as d/2 complex pairs \tilde\gamma_j = \gamma_{2j} + i\,\gamma_{2j+1}, the magnitude scales all pairs equally through a log-linear schedule (the shared trajectory found above), while the rotation spreads a per-layer base angle across pairs with RoPE’s fixed spectrum, b₀ being the base frequency.
LayerRoPE sits at four sites, the read and the write of every attention and MLP block (Eq. 2), so it regulates how each block reads from and writes to the residual stream, rather than the stream itself. Four scalars per site replace O(Ld) per-layer gain parameters with O(d).
import torch, torch.nn as nn class LayerRoPE(nn.Module): """Depth-conditioned gains for one site (e.g. the attention read). One shared γ and four scalars replace L per-layer gain vectors.""" def __init__(self, d, n_layers, base=100.0, beta_init=-0.5): super().__init__() self.gamma = nn.Parameter(torch.ones(d)) # shared γ self.a_mag = nn.Parameter(torch.zeros(())) self.b_mag = nn.Parameter(torch.tensor(beta_init)) self.a_rot = nn.Parameter(torch.zeros(())) self.b_rot = nn.Parameter(torch.tensor(beta_init)) j = torch.arange(d // 2) self.register_buffer("freq", base ** (-2 * j / d)) # RoPE spectrum self.register_buffer("log_l", torch.log(torch.arange(1.0, n_layers + 1))) def forward(self): # input-independent: once per pass r = self.a_mag + self.b_mag * self.log_l theta = torch.exp(self.a_rot + self.b_rot * self.log_l) ang = theta[:, None] * self.freq c = torch.polar(torch.exp(r)[:, None].expand_as(ang), ang) g = torch.view_as_complex(self.gamma.view(-1, 2)) return torch.view_as_real(g * c).flatten(1) # One LayerRoPE module per site sites = [LayerRoPE(d, n_layers) for _ in range(4)] attn_read, attn_write, mlp_read, mlp_write = (site() for site in sites) # normalize(h) = h / RMS(h), i.e. RMSNorm without its own learned weight: for layer, block in enumerate(blocks): h = h + attn_write[layer] * block.attn(normalize(h) * attn_read[layer]) h = h + mlp_write[layer] * block.mlp(normalize(h) * mlp_read[layer])
We train LLaMA-style models from 58M to 1.3B parameters on C4 at 4× the Chinchilla budget, up to 107B tokens. Every method, from Pre-, Post- and Peri-Norm to Layer-Norm Scaling (LNS) and LayerRoPE, gets its own learning rate, swept until the optimum is bracketed and extrapolated to 1.3B; LayerRoPE’s own hyperparameters stay fixed across scales.
LayerRoPE attains the lowest loss at every scale with the steepest compute exponent.
DepthBench fixes the parameter count at 400M and trades width for depth, from 16 layers of width 1216 to 32 layers of width 896. It compares ten methods under one recipe, each at its tuned learning rate: five that keep a single residual stream, and five that widen it into several streams or let each layer read earlier ones (Hyper-Connections, mHC, Attention Residuals, Block Attention Residuals and MoDA). We tune LayerRoPE once, at one shape, and apply it unchanged to two backbones, sweeping only its learning rate, as DepthBench does for every baseline.
On a plain Pre-Norm backbone, LayerRoPE has the lowest loss of all single-stream methods at every shape, and outperforms mHC and Attention Residuals on average while adding none of their costs. Added to Block Attention Residuals, it gives the lowest loss of all twelve methods at every shape, advancing the loss–cost Pareto frontier.
To isolate the curse of depth, we fix a narrow backbone (width 128) and vary only the number of layers, from 48 to 512 (18M–111M parameters), adding DeepNet, designed for very deep Transformers, as a baseline. Every other baseline degrades or fails with depth: Post-Norm diverges from 192 layers, Peri-Norm and LNS degrade beyond 96, and Pre-Norm stops improving after 256 and regresses at 512. DeepNet improves with depth, but remains well above LayerRoPE throughout.
LayerRoPE reaches the lowest loss at every depth, and an exhaustive per-depth learning-rate sweep confirms it, with every method at its own best rate.
Sweeping the peak learning rate over two to three orders of magnitude, with and without LayerRoPE on both backbones, changes the loss curve in three ways:
We also compare against methods that rescale the residual branches: Scaled Initialization and DS-Init, which shrink them at initialization, and LayerScale and SkipInit (ReZero’s scalar gate), which scale them by learned gains. Each is trained under the ladder protocol, its learning rate swept until the optimum is bracketed. LayerRoPE outperforms all of them at both scales; at 134M, LayerScale’s and SkipInit’s learned gains do not improve on Pre-Norm. Each diverges one learning-rate step above its optimum, while LayerRoPE trains stably at 4–8× higher rates.
With no tuning of their recipes, LayerRoPE carries over to two architectures far from the language-model ladder: a looped latent language model and Vision Transformers.
Parcae applies a recurrent block of layers T times to a latent state between an input and an output block. We train it at 140M, 370M and 770M parameters with its official recipe, beside a non-looped GPT of the same size, and apply LayerRoPE at every layer (each layer’s depth index is its position in the network, independent of the loop iteration), with magnitude slopes initialised at zero. Parcae + LayerRoPE is the best model on nearly every metric: lower validation perplexity, markedly lower Lambada perplexity at 140M and 370M, and higher average downstream accuracy.
140M11.2B tokens | 370M29.6B tokens | 770M61.6B tokens | |||||||
|---|---|---|---|---|---|---|---|---|---|
| Model | Val ppl ↓ | LMB ppl ↓ | Avg ↑ | Val ppl ↓ | LMB ppl ↓ | Avg ↑ | Val ppl ↓ | LMB ppl ↓ | Avg ↑ |
| GPT | 20.28 | 105.6 | 29.9 | 15.01 | 37.2 | 40.6 | 12.80 | 20.8 | 47.0 |
| Parcae | 18.66 | 81.2 | 33.0 | 14.22 | 32.5 | 43.4 | 12.49 | 18.6 | 50.1 |
| + LayerRoPE | 18.36 | 67.3 | 34.9 | 14.05 | 28.0 | 44.0 | 12.09 | 18.4 | 50.6 |
We train ViT-T and ViT-S on ImageNet-1k for 300 epochs with the DeiT recipe and apply LayerRoPE naively at every block, conditioning the gains γ of the pre-norm LayerNorms while keeping their centring and bias, with the magnitude slope initialised at zero. LayerRoPE improves both ViTs on every metric.
Where does LayerRoPE’s improvement come from? Freeze its depth slopes, so that every layer applies the same gain, and it loses 93% of its improvement over Pre-Norm and 99% over Peri-Norm. Gating each block without depth conditioning recovers at most an eighth. Learned scalars without it recover at most a third, even when tuned over 16 initializations and free at every layer, where they could express any schedule LayerRoPE learns.
The encoding is needed on both sides of each block: depth-encoding only the reads or only the writes keeps most of the gain but not all, and only both together learn to damp reads and amplify writes. Magnitude and rotation are complementary, and one shared γ performs as well as separate per-layer gains.
What LayerRoPE contributes is not its gains, nor their freedom across layers, but tying them to depth.
Prior remedies such as Layer-Norm Scaling and Peri-Norm damp the residual stream directly. LayerRoPE’s learned schedule does the opposite.
LayerRoPE does not shrink the residual pipe; it widens it, while regulating the blocks that read from and write to it.
How? LayerRoPE lets the residual stream grow in variance and norm, and instead regulates it at the inputs and outputs of each computational block.
Measuring just the learned magnitude gains, we see:
LayerRoPE learns to attenuate what each block reads from the residual stream.
LayerRoPE learns to amplify each block's write backs into the residual stream.
LayerRoPE’s behaviour, of residual-stream amplification with regulation of each block’s I/O, is therefore a learned property.
The two gains are not reciprocal: the damped reads contain the growth of the writes, cutting the net read × write amplification by up to 7×. Building this opposition in at initialization trains worse than letting LayerRoPE learn it: LayerRoPE starts with both sides damped and learns to amplify its writes.
Regulate the blocks, not the stream. Depth stability, our results suggest, depends less on how large the residual stream grows than on whether each block can scale what it reads and writes to its depth.
LayerRoPE’s explicit depth encoding provides exactly this regulation, while widening the residual stream.
This work was partly supported by NSF EFRI Award 2317706 and NSF CAREER Awards 2047556/2326491. The views and conclusions contained herein are those of the authors and should not be interpreted as representing any sponsor’s official policies or endorsements. We gratefully acknowledge use of the research computing resources of the Empire AI Consortium, Inc, with support from Empire State Development of the State of New York, the Simons Foundation, and the Secunda Family Foundation. This work was also supported in part by compute credits provided by Modal Labs through the Modal for Academics program.