Transformer Meets Twicing: Harnessing Unattended Residual Information

Laziz Abdullaev, Tan Minh Nguyen

Abstract

Deep Transformer architectures frequently encounter "over-smoothing," a state where token representations converge and lose expressive power in deeper layers. This paper introduces Twicing Attention, a mechanism derived from classical statistical regression that re-smooths attention residuals to maintain feature diversity. Twicing Attention serves as a drop-in replacement for standard self-attention, offering enhanced resistance to representation collapse and improved robustness against adversarial attacks and data corruption without requiring additional parameters.

Introduction: The Challenge of Representation Collapse

As Transformers scale in depth, they often face representation collapse. Because self-attention functions as a low-pass smoothing filter, repeated applications across layers tend to homogenize features. Eventually, tokens become nearly identical, limiting the model's capacity to capture complex patterns. While various architectural modifications have been proposed to mitigate this, many introduce significant computational overhead. The authors of this paper sought a mathematically grounded way to "slow down" this decay of information without breaking the efficiency of the Transformer architecture.

Methodology: Twicing the Residuals

The authors bridge the gap between deep learning and nonparametric statistics by showing that self-attention is mathematically equivalent to a Non-Local Means (NLM) smoothing filter. To combat aggressive smoothing, they borrow a technique called "Twicing" (originally proposed by John Tukey in 1977). In statistics, twicing involves smoothing data, calculating the residual, smoothing that residual, and adding it back to the initial result.

Mathematically, if $A$ represents the attention matrix, standard attention is $U = AV$. Twicing Attention modifies this operator to $(2A - A^2)$, resulting in: $$U = AV + A(V - AV)$$

This formulation is computationally efficient. By rewriting the expression, the authors implement Twicing Attention using two sequential standard attention passes. This maintains $O(N^2 D)$ complexity and requires zero additional parameters.

Preserving Feature Diversity

A key metric for over-smoothing is token cosine similarity. High similarity across tokens indicates a loss of unique information and a collapse of the representation space.

Token cosine similarity across layers for DeiT and DeiT-Twicing. Lower similarity in Twicing indicates preserved feature diversity.

As shown in the figure above, DeiT-Twicing consistently maintains lower token cosine similarity compared to the standard DeiT baseline, particularly in the deeper layers. This slower decay of information—proven theoretically to be $O(n^{-1/2})$ versus the standard $O(n^{-1})$—allows the model to remain expressive even at great depths.

Visualizing Attention Quality

The impact of Twicing is also visible qualitatively through attention heatmaps. Standard attention maps in deep layers often "collapse" onto small, non-descriptive points or irrelevant background noise.

Qualitative comparison of attention heatmaps between standard DeiT and DeiT-Twicing.

In the heatmaps above, DeiT-Twicing demonstrates a more comprehensive and diverse focus on meaningful object parts, such as the wings of a bird or the head of a parrot, whereas the baseline model's attention is more fragmented. This confirms that Twicing preserves the model's ability to attend to diverse features across the input.

Experimental Results and Robustness

The researchers evaluated Twicing Attention across multiple modalities, including ImageNet-1K (classification), ADE20K (segmentation), and WikiText-103 (language modeling).

  • Performance Boost: DeiT-Twicing improved Top-1 accuracy on ImageNet-1K to 72.60% (from a 72.00% baseline).
  • Superior Robustness: By reducing bias in the attention kernel, the model becomes naturally more resilient. It outperformed standard models on the ImageNet-C benchmark across all 15 corruption types and maintained higher accuracy under PGD adversarial attacks.
  • Language Modeling: On WikiText-103, the Twicing-enabled model achieved a lower test perplexity of 36.69 compared to the baseline's 37.51.

Conclusion and Future Work

Twicing Attention provides a simple yet effective way to combat over-smoothing in Transformers. By leveraging unattended residual information, it enhances both performance and robustness with minimal latency impact. While the current implementation adds a small amount of compute time due to the second attention pass, the authors note that applying Twicing only to the final layers provides most of the benefits with almost no speed penalty. Future work may explore scaling this technique to massive Large Language Models (LLMs) to prevent repetitive generation in long-context scenarios.

Summary updated

BibTeX
@inproceedings{abdullaev2025transformer,
  author = {Laziz Abdullaev and Tan Minh Nguyen},
  title = {{Transformer Meets Twicing: Harnessing Unattended Residual Information}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=16kG5aNleS},
}
Figure