Large Language Models (LLMs) are typically viewed as complex, non-linear systems. However, research presented at ACL 2024 reveals a surprising simplicity: the transformations between layers in popular transformer decoders are nearly 99% linear. This "secret linearity" offers new insights into model internal dynamics and provides pathways for more efficient model compression and improved training techniques.
The 0.99 Discovery: High Linearity in LLMs
By employing Procrustes Similarity Analysis—a statistical method for comparing vector sets—researchers calculated a Linearity Score for various state-of-the-art models, including LLaMA-2, GPT-2, and Mistral. Across nearly every major decoder-only model, the transformation from one layer's output to the next scored approximately 0.99, indicating an almost perfect linear mapping.
This high linearity is primarily driven by the "residual stream"—the main data highway that bypasses individual transformer blocks. As shown in the comparison above, when the residual connections are removed, the linearity score drops significantly, revealing that individual blocks perform small, non-linear adjustments while the overall flow remains remarkably linear. Essentially, the model makes tiny, linear tweaks to the data as it flows down the highway.
Linearity Dynamics During Training
The degree of linearity is not static; it evolves as a model learns. During the pretraining phase, as models ingest vast amounts of web data, they naturally become less linear. This suggests that the model is developing more complex, non-linear features to represent intricate language patterns.
Interestingly, this trend often reverses during fine-tuning. When specialized for specific tasks, models tend to become more linear again, potentially simplifying their internal logic to better suit the target domain. The researchers tracked these dynamics across models like TinyLlama, Pythia, and BLOOM, finding consistent patterns of decreasing linearity during the initial stages of learning.
Exploiting Linearity for Better Models
Understanding this internal structure allows for practical improvements in model efficiency and performance through two main avenues:
Smarter Depth Pruning
Traditional pruning often involves simply dropping layers, which can lead to significant performance degradation. By identifying the "most linear" layers and replacing them with simple linear approximations followed by a distillation process, researchers were able to maintain much lower error rates (perplexity) on benchmarks like WikiText compared to traditional pruning methods.
Cosine-Similarity Regularization
The authors introduced a novel training loss term that forces embeddings from sequential layers to align more closely. Paradoxically, by increasing cosine similarity—which one might expect to simplify the model—they forced the model's non-linear components to work harder to compensate. This resulted in "more expressive" embeddings that performed better on downstream logic and reasoning tasks. For instance, a Mistral 650M model improved its average SuperGLUE score from 43.33 to 46.06, outperforming standard training regimes.
Conclusion
The discovery that transformers are "secretly linear" challenges the "black box" perception of LLMs. By recognizing the dominance of the residual stream and the evolution of linearity during training, we can develop more expressive models and more effective compression algorithms. While currently focused on decoders, future work may extend these findings to other architectures like encoders (BERT) or encoder-decoder models (T5), potentially revealing a universal property of the transformer architecture.
Summary updated