DenseFormer: Enhancing Information Flow in Transformers via Depth Weighted Averaging

Matteo Pagliardini, Amirkeivan Mohtashami, François Fleuret, Martin Jaggi · DOI

Introduction

Transformer models have become the backbone of modern natural language processing, but scaling them to hundreds of billions of parameters often leads to diminishing returns. Standard Transformers rely on skip connections between adjacent blocks, which can create an informational bottleneck. Deeper layers must simultaneously process new information and maintain representations from much earlier layers to predict the next token. DenseFormer addresses this by introducing a Depth-Weighted-Average (DWA) module, inspired by the dense connectivity of DenseNets in computer vision, to allow direct access to all previous layer representations.

Architecture: Depth-Weighted Averaging

The core innovation of DenseFormer is the DWA module placed after each Transformer block. Instead of a block receiving only the output of its immediate predecessor, the DWA module computes a learned, weighted average of the current block's output, all previous blocks' outputs, and the initial token embeddings.

DenseFormer Architecture and DWA weights with dilation.

Because the DWA module only learns a single scalar weight for each past layer, the parameter overhead is negligible. To manage computational time overhead, the authors introduce two sparsification hyperparameters:

  • Dilation ($k$): The DWA module averages the output of every $k$-th past block.
  • Periodicity ($p$): The DWA module is inserted only after every $p$-th Transformer block. A model utilizing these optimizations is referred to as a $k \times p$-DenseFormer.

Key Insights

Dense connectivity allows later layers to access early features directly, freeing up intermediate capacity that would otherwise be used to shuttle information forward. Analysis of learned DWA weights reveals stable routing patterns: models consistently prioritize the immediate previous block and the initial embeddings. Interestingly, early embeddings are weighted positively in early layers but negatively in later layers, suggesting a functional shift from processing the current token to predicting the next one. Pruning experiments confirm that even small weights connecting distant blocks are critical for maintaining low perplexity.

Results and Efficiency

Evaluations on the OpenWebText2 and PG-19 datasets demonstrate that DenseFormer significantly outperforms standard Transformers in both efficiency and performance. A 48-block DenseFormer can match the perplexity of a much deeper 72-block standard Transformer.

Perplexity vs. Batches Per Second for various models.

As shown in the figure, the $4 \times 5$-DenseFormer variant provides an optimal Pareto trade-off. It achieves better perplexity than the baseline while being approximately 1.4x faster at inference and requiring 45% less memory. Furthermore, given a fixed training time budget, DenseFormers reach lower perplexity than standard architectures, proving their superior data and compute efficiency.

Conclusion

DenseFormer provides a simple yet effective architectural modification to improve information flow in deep Transformers. By enabling dense, weighted connections across layers with minimal parameter overhead, it achieves superior performance and efficiency compared to standard deep models. Future work will focus on further optimizing hardware-efficient sparsity patterns and scaling the DWA operation to massive distributed training environments.

Summary updated

BibTeX
@inproceedings{pagliardini2024denseformer,
  author = {Matteo Pagliardini and Amirkeivan Mohtashami and François Fleuret and Martin Jaggi},
  title = {{DenseFormer: Enhancing Information Flow in Transformers via Depth Weighted Averaging}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=kMnoh7CXrq},
}
Figure