Mask Attention Networks: Rethinking and Strengthen Transformer

Zhihao Fan, Yeyun Gong, Dayiheng Liu, Zhongyu Wei, Siyuan Wang, Jian Jiao, Nan Duan, Ruofei Zhang, Xuanjing Huang

Abstract

Transformer is an attention-based neural network, which consists of two sublayers, namely, Self-Attention Network (SAN) and Feed-Forward Network (FFN). Existing research explores to enhance the two sublayers separately to improve the capability of Transformer for text representation. In this paper, we present a novel understanding of SAN and FFN as Mask Attention Networks (MANs) and show that they are two special cases of MANs with static mask matrices. However, their static mask matrices limit the capability for localness modeling in text representation learning. We therefore introduce a new layer named dynamic mask attention network (DMAN) with a learnable mask matrix which is able to model localness adaptively. To incorporate advantages of DMAN, SAN, and FFN, we propose a sequential layered structure to combine the three types of layers. Extensive experiments on various tasks, including neural machine translation and text summarization demonstrate that our model outperforms the original Transformer.

Introduction

The Transformer architecture has become the backbone of modern Natural Language Processing (NLP). However, it often struggles to balance global context with local word relationships. Specifically, Self-Attention Networks (SAN) can be "noisy," where irrelevant tokens receive high attention scores due to shared neighbors. Conversely, Feed-Forward Networks (FFN) are "isolated," processing tokens individually without considering their context. While previous work has attempted to introduce "localness" via static attention windows, these fixed boundaries often fail to adapt to the nuances of natural language.

Mask Attention Networks (MANs)

This paper introduces a unifying framework: Mask Attention Networks (MANs). In this framework, the standard attention mechanism is augmented by an element-wise multiplication with a mask matrix $M$. This perspective reveals that SAN and FFN are simply special cases of MANs with static masks:

  • SAN uses an all-ones mask, allowing global access to all tokens.
  • FFN uses an identity mask, restricting attention to the token itself (self-evolution).
Visualization of mask matrices for SAN, DMAN, and FFN.

To overcome the limitations of static masks, we propose the Dynamic Mask Attention Network (DMAN). Unlike static masks, DMAN employs a learnable, "soft" mask function based on a sigmoid activation. This mask is context-aware, adapting based on the query token, relative distances between tokens, and the specific attention head and layer. By modeling localness dynamically, the model acts as a noise filter, ensuring that the global attention layer (SAN) doesn't get distracted by irrelevant neighbors.

Proposed Architecture

We propose a new sequential stacking order for the Transformer block: DMAN → SAN → FFN. This design allows the model to first capture local structures through DMAN, then integrate global context via SAN, and finally perform self-evolution with FFN. The authors found that modeling localness before globalness is more effective than the reverse, suggesting that building a solid local representation provides a better foundation for understanding long-distance dependencies.

Experiments and Results

The MAN architecture was evaluated on Machine Translation (IWSLT14 De-En, WMT14 En-De) and Abstractive Summarization (CNN/Daily Mail, Gigaword).

Performance comparison on Machine Translation tasks.

As shown in the results table, the proposed model consistently outperforms the baseline Transformer and several other specialized variants. On the WMT14 En-De (Big) task, our model achieved 30.4 BLEU, a 2.0 point improvement over the original Transformer with only a minimal increase in parameters (215M vs 213M). Similar gains were observed in summarization tasks, where ROUGE-1 and ROUGE-2 scores improved by 1.4 and 2.2 points, respectively, on the CNN/Daily Mail dataset. Ablation studies further confirmed that the dynamic nature of the mask in DMAN is crucial; replacing it with a static local mask resulted in lower performance.

Conclusion

By rethinking the Transformer sublayers as Mask Attention Networks, we provide a unified view that facilitates architectural improvements. The introduction of DMAN enables adaptive localness modeling, which, when combined sequentially with SAN and FFN, significantly enhances text representation. While adding a third sub-layer increases computational depth, the performance gains suggest it is a worthwhile trade-off. Future work may explore using these mask matrices to inject external linguistic knowledge, such as dependency trees, directly into the attention mechanism.

Summary updated

BibTeX
@inproceedings{fan2021mask,
  author = {Zhihao Fan and Yeyun Gong and Dayiheng Liu and Zhongyu Wei and Siyuan Wang and Jian Jiao and Nan Duan and Ruofei Zhang and Xuanjing Huang},
  title = {{Mask Attention Networks: Rethinking and Strengthen Transformer}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2021},
  doi = {10.18653/v1/2021.naacl-main.135},
}
Figure