On the Expressivity Role of LayerNorm in Transformers' Attention

Shaked Brody, Uri Alon, Eran Yahav
5/4/2023
cs.LG

Official code: tech-srl/layer_norm_expressivity_role (60 stars, last commit 2024-09-27).

Abstract

Layer Normalization (LayerNorm) is an inherent component in all Transformer-based models. In this paper, we show that LayerNorm is crucial to the expressivity of the multi-head attention layer that follows it. This is in contrast to the common belief that LayerNorm's only role is to normalize the activations during the forward pass, and their gradients during the backward pass. We consider a geometric interpretation of LayerNorm and show that it consists of two components: (a) projection of the input vectors to a $d-1$ space that is orthogonal to the $\left[1,1,...,1\right]$ vector, and (b) scaling of all vectors to the same norm of $\sqrt{d}$. We show that each of these components is important for the attention layer that follows it in Transformers: (a) projection allows the attention mechanism to create an attention query that attends to all keys equally, offloading the need to learn this operation by the attention; and (b) scaling allows each key to potentially receive the highest attention, and prevents keys from being "un-select-able". We show empirically that Transformers do indeed benefit from these properties of LayeNorm in general language modeling and even in computing simple functions such as "majority". Our code is available at https://github.com/tech-srl/layer_norm_expressivity_role .

View on arXivView PDF

Code Implementations(1)

Code for the paper "On the Expressivity Role of LayerNorm in Transformers' Attention" (Findings of ACL'2023)

603Shell, PythonMay 3, 20232 years ago
attentionlayer-normalizationlayernormtransformers

Similar Papers

Cite this paper

@article{brody2023expressivity,
  title  = {On the Expressivity Role of LayerNorm in Transformers' Attention},
  author = {Shaked Brody and Uri Alon and Eran Yahav},
  year   = {2023},
  eprint = {2305.02582},
  archivePrefix = {arXiv},
  url    = {http://arxiv.org/abs/2305.02582v2}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/2305.02582.svg)](https://codeofpaper.com/paper/2305.02582)

Discussion