Abstract
Despite the ubiquity of Transformer-based models like BERT, the functional necessity of their specific architectural components remains partially obscured. This paper investigates the internal dynamics of BERT, focusing on the non-linearity of its layers and the surprising "commutativity" of its architecture. Through a novel diagnostic framework using linear approximators and normalized linearity scores, we demonstrate that BERT layers are highly tolerant to shuffling and swapping. This robustness is primarily attributed to skip connections, which facilitate a "fuzzy" hierarchy of feature extraction rather than a rigid, sequential pipeline. Furthermore, we analyze the efficiency of Feed-Forward Networks (FFNs), revealing that while they consume the majority of the model's parameters, their contribution to non-linear transformation is concentrated in the middle layers.
Introduction
The success of BERT has sparked significant interest in "BERTology," yet two fundamental mysteries persist. First, the role of Feed-Forward Networks (FFNs) is often overshadowed by the attention mechanism, despite FFNs accounting for approximately two-thirds of the model's total parameters. Second, the success of weight-sharing models like ALBERT suggests a high degree of redundancy or similarity between layers. If layers are performing similar operations, to what extent is the specific order of operations critical to performance? This study seeks to quantify the non-linearity of BERT's components and explain the structural tolerance that allows for layer commutativity.
Methodology
To analyze the internal transformations within BERT, we introduced a diagnostic framework centered on "linearity."
Linear Approximators
We measured the complexity of model components by training simple linear layers to mimic the outputs of complex BERT blocks (FFNs and Attention). If a linear layer can accurately predict the output of a component, that component is functionally linear.
Normalized Linearity Score ($\tilde{\gamma}$)
High-dimensional embeddings in Transformers often exhibit anisotropy, clustering in a narrow "cone." To prevent this geometric bias from inflating linearity scores, we developed a normalized metric. This ensures that our measurements reflect the actual transformation performed by the layer rather than the inherent distribution of the input data.
Architectural Perturbations
We subjected BERT to various "stress tests," including:
- Linearization: Progressively replacing non-linear MLPs with linear approximations.
- Layer Swapping: Exchanging the positions of adjacent or distant layers.
- Shuffling: Randomizing the order of blocks to observe the impact on downstream task performance (primarily using the MNLI dataset).
Key Insights
The U-Shaped Linearity Curve
Our analysis revealed that non-linearity is not distributed uniformly across the model. Instead, it follows a U-shaped curve. The initial and final layers of BERT are surprisingly linear, acting as input/output adapters. The most complex, non-linear transformations occur in the middle layers, where the model performs the bulk of its semantic processing.
The Role of Skip Connections
Why is BERT so tolerant to layer swapping? By testing simplified CNN architectures, we identified skip connections (residual connections) as the primary driver of commutativity. These connections act as a "highway," encouraging each layer to learn incremental, residual transformations. Because each layer only slightly modifies the representation passing through the skip connection, the exact sequence of these modifications becomes less critical, leading to a "fuzzy" hierarchy.
FFN vs. Attention Efficiency
While FFNs dominate the parameter count, they appear less efficient than the attention mechanism. Replacing FFNs with linear approximations resulted in significantly less performance degradation than removing attention blocks. However, FFNs cannot be discarded entirely; they provide a necessary non-linear bottleneck that attention layers alone cannot replicate, even when the number of attention layers is increased to match the parameter count.
Results
- Minimal Degradation from Swapping: Swapping adjacent layers in a fine-tuned BERT-base model resulted in an average performance drop of only 2%. This confirms that the model does not rely on a strict, sequential assembly line of features.
- Shuffling Tolerance: BERT demonstrated remarkable resilience to more aggressive shuffling. Even when the majority of layers were randomized, the model retained significant accuracy. Furthermore, baseline performance could often be recovered with fewer than 2,000 additional fine-tuning updates.
- Parameter Impact: Removing FFNs (66% of parameters) dropped GLUE scores from 74.5 to 69.6. While substantial, this drop is relatively small considering the massive reduction in model size, highlighting the potential for more parameter-efficient FFN designs.
Conclusion
BERT's architecture is characterized by a high degree of functional similarity between layers, enabled by skip connections and a U-shaped distribution of non-linearity. This "fuzzy" hierarchy explains why the model is so robust to structural perturbations like layer shuffling. Our findings suggest that current Transformer designs may be over-parameterized in their FFN components, opening the door for future research into more efficient, non-linear structures that maintain performance while significantly reducing memory and computational footprints.
Summary updated