Year: 2020

Abstract:
Lipschitz constants of neural networks have been explored in various contexts in deep learning, such as provable adversarial robustness, estimating Wasserstein distance, stabilising training of GANs, and formulating invertible neural networks. Such works have focused on bounding the Lipschitz constant of fully connected or convolutional networks, composed of linear maps and pointwise non-linearities. In this paper, we investigate the Lipschitz constant of self-attention, a non-linear neural network module widely used in sequence modelling. We prove that the standard dot-product self-attention is not Lipschitz, and propose an alternative L2 self-attention that is Lipschitz. We derive an upper bound on the Lipschitz constant of L2 self-attention and provide empirical evidence for its asymptotic tightness. To demonstrate the practical relevance of our theoretical work, we formulate invertible self-attention and use it in a Transformer-based architecture for a character-level language modelling task.

heading: 1 INTRODUCTION
text: Lipschitz continuity is a strong form of continuity for functions. Loosely speaking, a function is Lipschitz continuous if changing its input by a certain amount cannot change its output by more than K times that amount. The constant K is a hard constraint on how rapidly the function’s output can vary, and the smallest such K is known as the function’s Lipschitz constant. For example, f1(x) = √ |x| and f2(x) = exp(x) for x ∈ R are not Lipschitz continuous, because their output can change arbitrarily fast as x approaches 0 and +∞ respectively. On the other hand, g1(x) = tanh(x) and g2(x) = αx are Lipschitz continuous, because their rate of change (derivative) is bounded.
In deep learning, we often use Lipschitz continuity as a constraint for neural networks, to control how much a network’s output can change relative to its input. Such Lipschitz constraints are useful in several contexts. For example, Lipschitz constraints can endow models with provable robustness against adversarial pertubations (Cisse et al., 2017; Tsuzuku et al., 2018; Anil et al., 2019), and guaranteed generalisation bounds (Sokolić et al., 2017). Moreover, the dual form of the Wasserstein distance is defined as a supremum over Lipschitz functions with a given Lipschitz constant, hence Lipschitz-constrained networks are used for estimating Wasserstein distances (Peyré & Cuturi, 2019). Further, Lipschitz-constrained networks can stabilise training for GANs, an example being spectral normalisation (Miyato et al., 2018). Finally, Lipschitz-constrained networks are also used to construct invertible models and normalising flows. For example, Lipschitz-constrained networks can be used as a building block for invertible residual networks and hence flow-based generative models (Behrmann et al., 2019; Chen et al., 2019). Additionally, Neural ODEs (Chen et al., 2018; Grathwohl et al., 2019) are typically defined using vector fields parameterized via Lipschitz networks, so that the flow generated by the vector field is guaranteed to exist for all times.
Nonetheless, designing Lipschitz-continuous neural networks and computing (or even upperbounding) their Lipschitz constant is a hard problem. Previous work mostly focused on fullyconnected and convolutional networks, not only because they are common in deep learning, but also because they are relatively simple to analyze, as compositions of linear maps and pointwise nonlinearities. Even in this case however, exact evaluation of the Lipschitz constant of fully-connected and convolutional networks is NP-hard (Virmaux & Scaman, 2018) and obtaining a tight upper bound remains a challenging task (Virmaux & Scaman, 2018; Fazlyab et al., 2019; Latorre et al., 2020).
Fully-connected and convolutional networks are not the only neural networks worthy of interest. Recently, self-attention (Vaswani et al., 2017) has become a popular alternative to recurrent neural
networks. Self-attention is a key component of the Transformer (Vaswani et al., 2017), that has found success as a building block in models of various data modalities, starting with natural-language processing (Vaswani et al., 2017; Devlin et al., 2019; Brown et al., 2020) and extending to computer vision (Zhang et al., 2019; Parmar et al., 2019), audio generation (Huang et al., 2019), and reinforcement learning (Parisotto et al., 2020). However, so far no previous work has analyzed the Lipschitz properties of self-attention, and thus it has been unclear whether self-attention is a viable option in applications that require Lipschitz constraints. In this work, we address this gap in the theory of self-attention by providing a thorough analysis of its Lipschitz properties. In particular, we make the following contributions:
• We prove that the widely used dot-product self-attention is not Lipschitz, and therefore not suitable to use in applications requiring Lipschitz constraints.
• We formulate L2 self-attention as an alternative, and show that it is Lipschitz. • We derive a theoretical upper bound on the Lipschitz constant of L2 self-attention, and provide
empirical evidence of the asymptotic tightness of the bound. • As a practical demonstration of the theory, we use this bound to formulate invertible self-attention,
and explore its use in a Transformer architecture for character-level language modelling.

heading: 2 LIPSCHITZ CONSTANT OF FULLY-CONNECTED/CONVOLUTIONAL LAYERS
text: We first define the notion of Lipschitz continuity, and proceed to define the Lipschitz constant. Definition 2.1. Given two metric spaces (X , dX ) and (Y, dY), a function f : X → Y is called Lipschitz continuous (or K-Lipschitz) if there exists a constant K ≥ 0 such that
dY(f(x), f(x ′)) ≤ KdX (x, x′) for all x, x′ ∈ X . (1)
The smallest such K is the Lipschitz constant of f , denoted Lip(f).
In this paper, we focus on the common case where X = Rn, Y = Rm, and dX , dY are induced by a p-norm ‖x‖p := ( ∑ i |xi|p)1/p. We will primarily consider the cases p = 2 and p = ∞, where ‖x‖∞ := maxi |xi|. To emphasise the dependence of the Lipschitz constant on the choice of p-norm, we will often denote it by Lipp(f). In this case, it follows directly from Definition 2.1 that the Lipschitz constant is given by
Lipp(f) = sup x6=x′∈Rn ‖f(x)− f(x′)‖p ‖x− x′‖p . (2)
Next, we outline some basic results that are useful for estimating Lipschitz constants, also covered in related works (Virmaux & Scaman, 2018; Behrmann et al., 2019). We describe how these results are used to provide bounds on the Lipschitz constant of fully-connected networks (FCN) and convolutional neural networks (CNN), using the fact that both are compositions of linear maps and pointwise non-linearities. To begin with, the following theorem suggests a way to bound Lipp(f) for a differentiable Lipschitz function f : Theorem 2.1 (Federer, 1969). Let f : Rn → Rm be differentiable and Lipschitz continuous under a choice of p-norm ‖ · ‖p. Let Jf (x) denote its total derivative (Jacobian) at x. Then Lipp(f) = supx∈Rn ‖Jf (x)‖p where ‖Jf (x)‖p is the induced operator norm on Jf (x).
Hence if f is a linear map represented by a matrix W then
Lipp(f) = ‖W‖p := sup ‖x‖p=1 ‖Wx‖p = { σmax(W ), if p = 2 maxi ∑ j |Wij | if p =∞
(3)
where ‖W‖p is the operator norm on matrices induced by the vector p-norm, and σmax(W ) is the largest singular value of W . Under this choice of norm, many common non-linearities (including relu, sigmoid, tanh, elu) are 1-Lipschitz. ‖W‖2 = σmax(W ) is usually estimated via power iteration; we provide details on how this is done in Appendix B.
Since we now know the Lipschitz constants of the components of both FCN and CNN, we can bound their Lipschitz constants by applying the following lemma: Lemma 2.1 (Federer, 1969). Let g, h be two composable Lipschitz functions. Then g ◦ h is also Lipschitz with Lip(g ◦ h) ≤ Lip(g) Lip(h).
Corollary 2.1. For a fully-connected network (FCN) or a convolutional neural network (CNN) f =WK ◦ ρK−1 ◦WK−1 ◦ . . . ◦ ρ1 ◦W1, we have Lipp(f) ≤ ∏ k ‖Wk‖p under a choice of p-norm with 1-Lipschitz non-linearities ρk. The above bound is not necessarily tight; there are various works that compute tighter bounds for FCN and CNN (e.g. Virmaux & Scaman, 2018; Fazlyab et al., 2019; Latorre et al., 2020).

heading: 3 LIPSCHITZ CONSTANT OF SELF-ATTENTION
text: 3.1 DOT-PRODUCT SELF-ATTENTION IS not LIPSCHITZ
Moving on, we investigate whether self-attention is Lipschitz. We first consider the widely used (scaled) dot-product multihead self-attention as formulated by Vaswani et al. (2017). Let x1, . . . , xN be a sequence of N elements, where xi ∈ RD for i = 1, . . . , N . We represent this sequence as a matrix X ∈ RN×D such that the ith row of X is the ith element of the sequence, i.e. Xi: = x>i . Dot-product multihead self-attention (DP-MHA) is a map from RN×D to RN×D consisting of H ‘heads’, where H is chosen to divide D. Each head is a map from RN×D to RN×D/H defined by
DP(X) := softmax ( XWQ(XWK)>/ √ D/H ) XWV , (4)
where WQ,WK ,WV ∈ RD×D/H are learnable parameters specific to each head. The input to the softmax is an N ×N matrix of dot products (hence dot-product self-attention), and the softmax is applied to each row of this matrix. Finally, the outputs of all heads are concatenated into an N ×D matrix and are right multiplied by WO ∈ RD×D, thus DP-MHA is defined by
MHA(X) := [ DP1(X), . . . ,DPH(X) ] WO. (5)
In what follows, we will prove that MHA as defined above is not Lipschitz, assuming that the MHA map is non-trivial, i.e. WQ,WK ,WV ,WO 6= 0. It is sufficient to show that a single head DP is not Lipschitz, since MHA is a linear combination of the outputs of each head. Let us write Equation (4) as DP(X) = PXWV , where P ∈ RN×N is the output of the softmax (we suppress the dependence of P on X to reduce clutter below). P is a stochastic matrix, i.e. its entries are non-negative and its rows sum to 1. Since the rows of X are the xi’s, a linear transformation of each xi by some matrix A is equivalent to right multiplication of X by A>. So right multiplication of X by WV is a linear map and thus Lipschitz. Therefore, we are interested in the mapping f(X) = PX; this is not a linear mapping because P itself is a non-linear function of X . In fact, we show that f is not Lipschitz, thus proving the first main result of the paper: Theorem 3.1. DP-MHA is not Lipschitz for any vector p-norm ‖ · ‖p with p ∈ [1,∞]. Summary of Proof. We use Theorem 2.1, noting that if the supremum of the norm of the Jacobian is infinite, then the mapping is not Lipschitz. In particular, we show that when xi = 0 for some i, some elements of the Jacobian of f grow proportionally to the sample variance of x 6=i, which is unbounded. Proof. We show the proof for the case D = 1 (i.e. X ∈ RN×1, a column vector) for readability. See Appendix C for the general case, which follows the same logic.
The mapping f can be written as f(X) = PX = softmax ( aXX> ) X =  f1(X) >
... fN (X) >  ∈ RN×1, (6) where a = WKWQ ∈ R (we assume a 6= 0 such that self-attention is non-trivial) and fi(X) =∑N
j=1 Pijxj with P > i: = softmax (axiX). Hence f can be interpreted as a map of each xi to a point
in the convex hull of x1, ..., xN . Since f is a map from RN×1 to RN×1, its Jacobian is
Jf = J11 . . . J1N... . . . ... JN1 . . . JNN  ∈ RN×N , (7) where Jij =
∂fi(X) ∂xj ∈ R. By taking partial derivatives we can show that Jij = aX>P (i) [ejiX + δijX] + PijI where eij ∈ RN×N is a binary matrix with zeros everywhere
except the (i, j)th entry, δij is the Kronecker delta, and P (i) := diag(Pi:)− P>i: Pi:. So for i = j:
Jii = aX >P (i)eiiX + aX >P (i)X + Pii (8)
Let us investigate the scalarX>P (i)X . We observe that it is in fact a variance of a discrete distribution. Specifically:
X>P (i)X = ∑ k Pikx 2 k − ( ∑ k Pikxk) 2 = Var(X), (9)
where X is a discrete distribution with support at the inputs {x1, . . . , xN} and probability mass function given by their softmax probabilities P(X = xj) = Pij . A consequence of this interpretation is that P (i) is positive semi-definite (PSD) since X>P (i)X = Var(X) ≥ 0, with equality if and only if the xj are all equal.
We use this observation to show that Jii is unbounded, and so ‖Jf‖p is unbounded, hence DP-MHA is not Lipschitz. Consider the case xi = 0. Then P>i: = softmax (XAxi) = 1 N 1, i.e. we have uniform attention regardless of x 6=i. The first term of Jii in Equation (8) disappears since eiiX = [0, . . . , xi, . . . , 0] = 0, and the last term becomes 1N I . Now consider the second term aX
>P (i)X = aVar(Xl). Note X is uniformly distributed, since P(X = xj) = Pij = 1/N . Hence the second term is equal to a times the sample variance of x1, . . . , xN , which can be arbitrarily large.
High-level intuition for proof. At xi = 0, fi(X) = 1N ∑ k xk, the mean of the inputs. The rate of change of fi is governed by how fast the softmax saturates when xi is perturbed, which is determined by how spread out the x 6=i are. The more spread out they are (the higher the sample variance), the greater the rate of saturation of the softmax, and the faster the rate of change of fi. Since the sample variance of x 6=i can be arbitrarily large, the rate of change of fi can also be arbitrarily large, i.e. the entries of the Jacobian (and hence its p-norm) can become arbitrarily large. In Appendix D, we show that adding bias terms to x>i W Q and x>j W K does not resolve the issue.
The implications of this result are the following. (1) There can be undesirable behaviour (e.g. training instabilities) for the Transformer when some inputs are close to zero. (2) Dot-product self-attention (and hence the standard Transformer) is not a suitable choice when we require a Lipschitz neural network, such as for formulating invertible residual networks (Behrmann et al., 2019). Therefore, to use self-attention and Transformers in such applications, a Lipschitz formulation of self-attention is required, together with an explicit (ideally tight) upper bound to its Lipschitz constant, to quantify how much the output can change with respect to changes in the input.
One method to make dot-product self-attention Lipschitz is by ensuring its inputs are bounded. Indeed, if the input space is compact, e.g. [0, 1]N×D, any continuously differentiable function is Lipschitz, including dot-product self-attention. However, as we further discuss in Section 6, such an approach has its own challenges, since it makes the Lipschitz constant depend on the input range. Instead, in the next section we formulate a version of self-attention that is provably Lipschitz on all of RN×D, allowing us to derive an upper bound that holds for any subset of RN×D.

heading: 3.2 L2 SELF-ATTENTION: A LIPSCHITZ FORMULATION OF SELF-ATTENTION
text: The pathology in dot-product self-attention arises because the softmax probabilities Pi: are constant with respect to x 6=i when xi = 0. This behaviour can be undesirable as we want Pij to vary according to xj , regardless of whether xi is zero or not. Hence we propose an alternative form of self-attention based on L2 distance:
Pij ∝ exp(Lij) := exp ( − ∥∥x>i WQ − x>j WK∥∥22 /√D/H) , (10)
with the normalisation constant ensuring that ∑ j Pij = 1. We will refer to it as L2 self-attention. It is reminiscent of the standard squared-exponential kernel, but with softmax normalisation that ensures that each row of the kernel matrix sums to 1. Normalisation is usually necessary to deal with inputs of varying length N (Wang et al., 2018), hence we keep the softmax for L2 self-attention. Similarly to dot-product self-attention, L2 self-attention can be computed efficiently with matrix operations; see Appendix E for details, with a comparison of wall-clock runtimes between different choices of attention.
We first state the mathematical formulation of L2 multihead self-attention (L2-MHA) before proving the main result — the upper bound of its Lipschitz constant with respect to ‖ · ‖p for p = 2,∞. The
full L2-MHA map F : RN×D → RN×D is defined as F (X) := [ f1(X)WV,1, . . . , fH(X)WV,H ] WO where fh(X) := PhXAh.
In the above, WV,h ∈ RD×D/H , WO ∈ RD×D, Ph is defined as in Equation (10) with WQ,h = WK,h ∈ RD×D/H , and Ah := WQ,hWQ,h > / √ D/H ∈ RD×D. There are two changes from the usual form of multihead self-attention:
(1) We require WQ,h =WK,h for each head fh(X) to be Lipschitz. In Lemma F.1 of Appendix F we show that L2-MHA is not Lipschitz for arbitraryWQ,h, WK,h, and that tyingWQ,h =WK,h is sufficient for L2-MHA to be Lipschitz, with intuition for why tying is sufficient.
(2) In each head of the self-attention fh(X), right multiplication by Ah has been included for the theorem below to hold (details are in the proof). In practice, there is little harm done by this extra linear transformation, since when the heads are combined together in F , each fh(X) is additionally transformed by WV,h, a free parameter.
The second main result of the paper is the following: Theorem 3.2. L2-MHA is Lipschitz, with the following bound on Lip∞(F ):
Lip∞(F ) ≤
( 4φ−1(N − 1) + 1√
D/H
) max h ‖WQ,h‖∞‖WQ,h > ‖∞max h ‖WV,h > ‖∞ ‖WO > ‖∞
and the following bound on Lip2(F ):
Lip2(F ) ≤ √ N√ D/H ( 4φ−1(N − 1) + 1 )(√∑ h ‖WQ,h‖22 ‖WV,h‖22 ) ‖WO‖2
where φ(x) := x exp(x + 1) is an invertible univariate function on x > 0, and N is the input sequence length.
Specifically, φ−1(N − 1) = W0(Ne ) where W0 is the Lambert W -function, which grows sublogarithmically as O(logN − log logN) (Corless et al., 1996). Hence the above bounds can be simplified to O(logN) for p =∞ and O( √ N logN) for p = 2.
Proof. See Appendix F, which uses the key observation that X>P (i)X is a covariance matrix (c.f. Equation (9)) to bound ‖JF ‖p, the norm of the Jacobian of F . Appendix G shows how the argument can be modified to prove the analogous result for the case with masking in the self-attention.
These bounds are complemented by the concurrent work of Vuckovic et al. (2020), which provides a O( √ D logN) bound on Lip1(F ) using measure-theoretic tools.

heading: 4 APPLICATION: INVERTIBLE SELF-ATTENTION

heading: 4.1 INVERTIBLE RESIDUAL NETWORK
text: Consider the residual function g(x) := x+f(x). Behrmann et al. (2019) give the following sufficient condition for its invertibility: if f is a contraction with respect to some metric, i.e. if Lip(f) < 1, and the metric space on which f is defined is complete, then g is invertible. (A Euclidean space with a metric induced by a p-norm ‖ · ‖p for p ∈ [1,∞] is always complete.) Specifically, the inverse g−1(y) is the unique fixed point of the recursion xi+1 := y − f(xi), since by the definition of the inverse we have y = g−1(y)+f(g−1(y)). Because f is a contraction, Banach’s Fixed Point Theorem guarantees that this fixed point exists and is unique for all y, and that the recursion converges for all initial values x0 (often set to y in practice) exponentially fast. Hence the inverse can be computed to arbitrary accuracy (up to numerical precision in practice) by the above fixed-point iteration.
Note that a composition of such invertible residual blocks is also invertible. Behrmann et al. (2019) use this observation to design invertible ResNets: they take f to be a CNN normalised by an upper bound on Lip(f) given by Corollary 2.1, making the resulting function contractive. For the 2-norm ‖ · ‖2, a hyperparameter c < 1 is chosen and each linear map (convolution) W in the CNN is multiplied by c/‖W‖2 if c < ‖W‖2 where ‖W‖2 is estimated by power iteration (c.f. Appendix B). This multiplicative factor determines the scale of the Lipschitz constant of the normalised function.
4.2 INVERTIBLE SELF-ATTENTION
The standard use case of self-attention is with a skip connection inside the Transformer. A Transformer block is composed of residual blocks of multihead self-attention (MHA) and fully-connected (FCN) layers (Figure 1). Hence similarly to invertible ResNets, we can normalise L2-MHA by the upper bounds given in Theorem 3.2 to obtain Contractive-L2-MHA f , with which we can obtain invertible self-attention g(x) = x+ f(x).
In the next section, we investigate the properties of invertible self-attention and how it compares with the standard dot-product self-attention; we replace DP-MHA in the Transformer with Contractive-L2-MHA, hence replacing the residual self-attention module with invertible self-attention. We are not interested in the modified Transformer per se, but rather in comparing the properties of invertible self-attention to standard self-attention — we only use the Transformer as a testbed for this purpose, since self-attention is commonly used in a Transformer. Given the theoretical focus of the paper, we believe that a more challenging application of invertible self-attention, such as normalising flow-based modelling, would be more suitable as a separate paper focused on that particular application. In Appendix H, we show that Dropout in the residual branch is also contractive.

heading: 5 EXPERIMENTAL RESULTS
text: 5.1 ASYMPTOTIC TIGHTNESS OF THE UPPER BOUND ON Lip∞(F )
A tight bound on the Lipschitz constant of selfattention is desirable for all listed applications in Section 1; it leads to tighter generalisation bounds, lighter constraints for provable robustness, and better expressiveness in residual flow models. Hence we investigate the tightness of our bound on the Lipschitz constant of L2-MHA. The Lipschitz constant is a supremum over the space of inputs X ∈ RN×D (c.f. Equation (2)) and approximating it requires solving an intractable optimisation problem. Hence it is infeasible to estimate accurately in general, especially when X is high-dimensional. However, we may compute a lower bound on the Lipschitz
constant by maximising the norm of the Jacobian ‖Jf (X)‖ with respect to X until convergence. This local optimum will form a lower bound by Theorem 2.1, and we can expect this lower bound to be fairly tight for the low-dimensional case, provided the optimisation is thorough.
We use this observation to provide empirical evidence for the asymptotic tightness of the upper bound on Lip∞(f) in Theorem 3.2. In Figure 2, we show the upper bound as well as the lower bound on Lip∞(f) obtained by optimising ‖Jf (X)‖∞ with respect to X for L2-MHA f with 50 different random initialisations of X , with H = D = 1 and N varying between 100 and 1000. See Appendix I for further details. Note that we use a log-scale for the x-axis, and recall that the upper bound is O(logN − log logN), dominated by the O(logN) term for large N . Hence the plot for the upper bound shows a linear trend. We also observe that the slope of the lower bound is very similar, providing empirical evidence that the O(logN − log logN) upper bound is asymptotically tight. There are at least two possible explanations for the gap between the upper and lower bounds. (1) The lower bound is only a local optimum — the true Lipschitz constant is a global optimum across inputs, which can be difficult to attain especially for high values of N . (2) The multiplicative constant of the upper bound may be loose. Assuming asymptotic tightness, it remains an open question whether the multiplicative constant can be tightened. We show the analogous plot for Lip2(F ) and discuss the results in Appendix K. Additionally in Appendix L, we show that optimising ‖Jf (X)‖∞ w.r.t. X for DP-MHA f causes the norm to diverge, providing empirical verification of Theorem 3.1, that DP-MHA is indeed not Lipschitz.

heading: 5.2 NUMERICAL INVERTIBILITY OF MHA RESIDUAL MAP
text: Recall from Section 4.1 that g(x) = x + f(x) is invertible if f is contractive. Hence if f is Contractive-L2-MHA, g is necessarily invertible. However, technically we do not disprove the invertibility of DP-MHA, since the converse does not hold in general i.e. if f is DP-MHA, which we have shown is not Lipschitz hence not contractive, it may still be the case that g is invertible. To verify that DP-MHA (with the skip connection) is not invertible in practice, we compare the numerical invertibility of the residual map g(x) = x + cf(x) between the cases where f is L2-MHA and DP-MHA in Figure 3. For each, we take MHA with 8 heads and randomly initialised weights, and quantify the
maximum reconstruction error across a batch of 128 inputs whose outputs are inverted via the fixed-point iteration described in Section 4.1. We use N = 64, D = 64, and c ∈ {0.5, 0.7, 0.9} (see Appendix J for analogous results for a wider range of N and D and for DP-MHA with trained weights). To highlight the difference between the two types of self-attention, recall in the proof of Theorem 3.1 (showing that DP-MHA is not Lipschitz) that when one of the inputs xi is 0, some terms of the Jacobian grow with the sample variance of x 6=i. Hence we check numerical invertibility at a set of N inputs where xi = 0 and x 6=i are chosen uniformly at random. In Figure 3, we see that DP-MHA is not invertible whereas L2-MHA is invertible for sufficiently small c. This shows how not having the theoretical guarantee of f being contractive can cost us invertibility in practice. We note that the figure shows local invertibility at the sampled inputs, as opposed to global invertibility across the whole input space, yet this clearly highlights the difference between the two choices of self-attention. Experiments with the globally invertible self-attention obtained by normalising with the Lipschitz upper bound are provided in the next section.

heading: 5.3 EXPRESSIVENESS OF L2-MHA AND INVERTIBLE SELF-ATTENTION
text: A natural question to ask is: how does the expressiveness of L2-MHA and Contractive-L2-MHA (that leads to invertible self-attention with the skip connection) compare with the original DP-MHA? We expect that the Lipschitz constraint will limit the expressiveness of the Transformer, and would like to find out by how much. We investigate this by comparing the performance of the original Transformer and the Transformer with invertible self-attention (c.f. Figure 1) at character-level language modelling on the Penn Treebank dataset (Marcus et al., 1993). We compare the test negative log-likelihood (NLL) of a baseline LSTM, the original Transformer (DP-MHA), and a series of models between the original Transformer and the Transformer with invertible self-attention (Contractive-L2-MHA), making one change at a time and tuning the hyperparameters on a validation set. For Contractive-L2-MHA, we normalise L2-MHA by the bound on Lip∞(F ) as it is tighter than the bound on Lip2(F ). See Appendix I for experimental details.
The results are shown in Figure 4. The first plot shows the best performing LSTM reaching a test NLL of around 1.0, and the second plot shows the best performing Transformer reaching a slightly improved performance for 3–5 layers of Transformer blocks. We observe instabilities in training for a higher number of layers, requiring careful tuning of the learning rate schedule for stability at the cost of performance, a commonly observed phenomenon in the literature of deep Transformer architectures (Bapna et al., 2018; Parisotto et al., 2020). The third plot shows results for the Transformer with DP-MHA replaced with L2-MHA but without tying WQ and WK , and we observe a very similar test performance. The fourth plot shows the change when we further tie the query and key weights (making WQ =WK); we see that there is a small degradation in performance. Here the number of trainable parameters has been reduced, but in Appendix M we show that matching parameter count does not help performance, suggesting that the reduction in performance when tying queries and keys is not solely due to having fewer parameters. We note that performance saturates at around 5 layers for each Transformer model so far. On the rightmost plot we show results when further dividing self-attention in each block by the upper bound on Lip∞(F ), to obtain invertible self-attention. This does give reduced performance for the same number of layers, but we can attain similar performance with more layers, no longer saturating at 5 layers.
Thus we conclude the following. (1) Replacing the dot-product with the L2 distance incurs hardly any loss in expressiveness. (2) Tying the query and key weights to obtain Lipschitz self-attention incurs a small loss in expressiveness. (3) Dividing by the upper bound on Lip∞(F ) to obtain invertible self-attention incurs a noticeable loss in expressiveness, but also has a stabilising effect on the optimisation of the Transformer, thus allowing one to compensate for the apparent loss in expressiveness by increasing the number of layers. We show further experimental results that compare the training stability of DP-MHA and (Contractive)-L2-MHA in Appendix N.

heading: 6 CONCLUSION AND DISCUSSION
text: We have shown that the widely used dot-product self-attention is not Lipschitz, and that the proposed L2 self-attention is Lipschitz, by deriving anO(logN−log logN) Lipschitz bound for p =∞ and an O( √ N(logN−log logN)) bound for p = 2, whereN is the input sequence length. We also provided empirical evidence of the asymptotic tightness of the bound for p = ∞. Finally we demonstrated that Lipschitz-constrained self-attention can be used to formulate invertible self-attention, which we experimentally evaluated on a character-level language modelling task.
Our approach to Lipschitz self-attention has been to replace the dot-product kernel with an L2 kernel. An alternative would be to constrain the inputs of self-attention to be bounded; if the input space is compact, e.g. [0, 1]N×D, any continuously differentiable function is Lipschitz, including dot-product self-attention. However, while being simple to implement, this solution has its own difficulties. First, it makes the Lipschitz constant depend on the range of the input, and thus obtaining a tight bound would require non-trivial mathematical work. We stress that a guarantee that the function is Lipschitz does not tell us anything about its Lipschitz constant; without a tight Lipschitz bound, the true Lipschitz constant can be very large, at which point it is unhelpful that the function is Lipschitz. Second, since self-attention is typically applied at multiple layers within a model (e.g. Transformer), the input to each self-attention will live in a different compact set that depends on the parameters of the previous layers, complicating the analysis for subsequent layers. A solution is to constrain the inputs of each layer to be in the same compact set, e.g. by passing them through a sigmoid non-linearity. This however can have undesirable side effects such as vanishing gradients when the sigmoids are saturated (Hochreiter, 1998). Despite these difficulties, this could be a worthwhile alternative route for obtaining Lipschitz self-attention to explore in the future.
Having a provably Lipschitz self-attention module at our disposal makes it possible to use Transformerbased architectures in applications requiring Lipschitz constraints, while enjoying theoretical guarantees. A natural application of Lipschitz self-attention is for residual flows (Behrmann et al., 2019), and for parameterising Neural ODEs (Chen et al., 2018) where a Lipschitz vector field guarantees the existence of a unique solution to the ODE for all times. These models can be used for density estimation and generative modelling of sets. Another interesting direction for future work would be to analyse different variants of self-attention based on kernels other than dot-product and L2, as Tsai et al. (2019) do from an experimental perspective, for which we believe the mathematical tools developed in this paper may aid the analysis.

heading: A CHAIN RULE FOR VECTOR VALUED FUNCTIONS
text: In this section, we list some useful identities for deriving the Jacobians of the expressions in the paper.
Suppose λ is a scalar, u, v, x are column vectors, and f(u) is a vector valued function. We use the standard convention that for a ∈ Rm, b ∈ Rn, we have ∂a∂b ∈ R
m×n. Then we have the following chain rule identities:
• ∂∂x [λu] = λ ∂u ∂x + u ∂λ ∂x
• ∂f(u)∂x = ∂f(u) ∂u ∂u ∂x
• ∂∂x [u >v] = u> ∂v∂x + v > ∂u ∂x
Note ∂λ∂x is a row vector, so u ∂λ ∂x is a matrix.

heading: B POWER ITERATION
text: Although ‖W‖∞ can be computed efficiently in O(nm) time for W ∈ Rm×n, naïvely computing ‖W‖2 = σmax(W ) := √ λmax(W>W ) requires O(n3) operations. (By λmax(A) we denote the greatest eigenvalue of a symmetric matrix A.) We can however obtain an underestimate σ̃(W ) via power iteration:
bk+1 = W>Wbk ‖W>Wbk‖2 , σ̃k(W ) =
√ b>kW >Wbk
b>k bk , (11)
with each iteration taking O(n2) time. Then using K n iterations gives us an underestimate σ̃K in O(Kn2) time. Since this is an underestimate, the resulting approximation to the Lipschitz constant of the linear map will not be an upper bound. However the number of power iterations is usually chosen so that σ̃ is accurate enough — K = 5 is shown to be sufficient in the context of fully connected networks or convolutions considered by Behrmann et al. (2019).
The iteration will converge if W>W has an eigenvalue that is strictly greater in magnitude than its other eigenvalues, and the starting vector b0 has a nonzero component in the direction of an eigenvector associated with the dominant eigenvalue. This happens with probability 1 if b0 is chosen at random, and the convergence is geometric with ratio |λ2/λmax| where λ2 is the eigenvalue with second largest magnitude (Mises & Pollaczek-Geiringer, 1929).

heading: C PROOF OF THEOREM 3.1 FOR GENERAL D
text: Theorem 3.1. DP-MHA is not Lipschitz for any vector p-norm ‖ · ‖p with p ∈ [1,∞].
Proof. The mapping f can be written as f(X) = PX = softmax ( XA>X> ) X =  f1(X) >
... fN (X) >  ∈ RN×D, (12) where A = WKWQ > / √ D/H ∈ RD×D and fi(X) = ∑N j=1 Pijxj with P > i: = softmax (XAxi). Hence f can be interpreted as a map of each xi to a point in the convex hull of x1, ..., xN . Since f is a map from RN×D to RN×D, its Jacobian is
Jf = J11 . . . J1N... . . . ... JN1 . . . JNN  ∈ RND×ND, (13) where Jij =
∂fi(X) ∂xj ∈ RD×D. By taking partial derivatives we can show that Jij = X>P (i) [ ejiXA > +XAδij ] + PijI where eij ∈ RN×N is a binary matrix with zeros everywhere
except the (i, j)th entry, δij is the Kronecker delta, and P (i) := diag(Pi:)− P>i: Pi:. So for i = j:
Jii = X >P (i)eiiXA > +X>P (i)XA+ PiiI = Pii (xi − ∑ k Pikxk)x > i A > +X>P (i)XA+ PiiI. (14)
For the last equality, note eiiX has all rows equal to zero except for the ith row given by x>i . We can then verify that X>P (i)eiiX simplifies to Pii(xi − ∑ k Pikxk)x > i .
For vector p-norms, ‖Jf‖p is bounded if and only if its entries are bounded, by definition of the operator norm. The entries ofX>P (i)XA are bounded for arbitraryA only if the entries ofX>P (i)X are bounded. So let us investigate the entries of this D ×D matrix. Writing out each term of the matrix, we observe that it is in fact a covariance matrix of a discrete distribution. Specifically:
[X>P (i)X]lm = ∑ k Pikxklxkm − ( ∑ k Pikxkl) ( ∑ k Pikxkm) = Cov(Xl,Xm), (15)
where X is a discrete distribution with support at the inputs {x1, . . . , xN} and probability mass function given by their softmax probabilities P(X = xj) = Pij . A consequence of this interpretation is that P (i) is positive semi-definite (PSD) since for D = 1, Equation (15) becomes X>P (i)X = Var(X) ≥ 0, with equality if and only if the xj are all equal. We use this observation to show that the terms of Jii are unbounded, and so DP-MHA is not Lipschitz. Consider the case xi = 0. Then P>i: = softmax (XAxi) = 1 N 1, i.e. we have uniform attention regardless of x 6=i. The first term of Jii in Equation (14) disappears since xi = 0, and the last term becomes 1N I . For the second term, the entries [X
>P (i)X]ll = Var(Xl) are unbounded since the latter is equal to the sample variance of x1l, . . . , xNl, which can be arbitrarily large.

heading: D BIAS TERM IN DP SELF-ATTENTION
text: A natural question to ask is whether we can add bias terms bQ to x>i W Q and bK to x>j W K to resolve the issue of attention weights Pi: becoming uniform when xi = 0. The answer is no in general. It can again be shown that Jii is unbounded when xi is chosen such that x>i W
Q + bQ = 0 (such a choice is possible assuming WQ is full rank, a dense set in RD×D/H ). Then P>i: = 1N 1 again, and the diagonal entries of X>P (i)X are unbounded.

heading: E EFFICIENT COMPUTATION OF L2 SELF-ATTENTION
text: Dot-product self-attention only requires a few matrix multiplications to compute the logits (i.e. the inputs to the softmax) between all pairs of inputs, without having to loop over pairs, hence it can be computed efficiently. Similarly, we can show that L2 self-attention can also be computed in an efficient manner. Using the identity ‖a− b‖22 = ‖a‖22 − 2a>b+ ‖b‖22 we can compute the logits of L2 attention between all pairs via matrix multiplications and computation of row-wise L2 norms, with negligible overhead compared to dot-product self-attention. Specifically, for L2 self-attention we can show that
P = softmax ( −‖XW
Q‖2row1> − 2XWQ(XWK)> + 1‖XWK‖2>row√ D/H
) , (16)
where ‖A‖2row applies the squared L2 norm to each row of A, so if A ∈ Rm×n then ‖A‖2row ∈ Rm. In Table 1 we show the wall-clock training times for the Transformer models with different attention functions and a varying number of layers. It is evident that the differences between the models are rather small.

heading: F PROOF OF THEOREM 3.2
text: Recall the formulation of L2-MHA:
F : RN×D → RN×D F (X) = [ f1(X)WV,1, . . . , fH(X)WV,H ] WO
fh(X) = PhXAh
Phij ∝ exp(Lij) := exp
( − ‖x>i WQ,h − x>j WK,h‖22√
D/H
) , ∑ j Phij = 1
where we have that WQ,h,WK,h,WV,h ∈ RD×D/H , WO ∈ RD×D, Ph ∈ RN×N and Ah := WQ,hWQ,h > / √ D/H ∈ RD×D, and the softmax is applied to each row of the input matrix. Recall Equation (16):
Ph = softmax ( −‖XW Q,h‖2row1> − 2XWQ,h(XWK,h)> + 1‖XWK,h‖2 >
row√ D/H
) .
F.1 L2 SELF-ATTENTION IS not LIPSCHITZ FOR GENERAL WQ,WK
Let us first look at the case of H = 1 and suppress the index h to reduce clutter. Consider the map f̃(X) := PX , so f(X) = f̃(X)A. We need f̃ to be Lipschitz for f and hence F to be Lipschitz. Note that P is defined as:
Pij ∝ exp(Lij) := exp ( − ‖x>i WQ − x>j WK‖22√
D/H ) and the normalisation constant satisfies ∑ j Pij = 1, for P ∈ RN×N , X ∈ RN×D.
For L2 self-attention, we may take partial derivatives and use the chain rule to show that the Jacobian of f̃ is:
Jf̃ =  J̃11 . . . J̃1N... . . . ... J̃N1 . . . J̃NN  ∈ RND×ND (17) with
J̃ij = X >P (i) ∂Li: ∂xj + PijI ∈ RD×D (18)
where ∂Li: ∂xj = 2√ D/H [( XWK − 1x>i WQ ) WQ > δij + ( ejiXW Q − ejjXWK ) WK > ]
(19)
and
P (i) := diag(Pi:)− P>i: Pi: =  Pi1(1− Pi1) −Pi1Pi2 . . . −Pi1PiN −Pi2Pi1 Pi2(1− Pi2) . . . −Pi2PiN
... ...
. . . ...
−PiNPi1 −PiNPi2 . . . PiN (1− PiN )
 ,
Pij = exp
( −‖x>i WQ − x>j WK‖22 )∑ k exp ( −‖x>i WQ − x>kWK‖22
) . Recall that eji ∈ RN×N is a binary matrix with zeros everywhere except the (j, i)th entry. Hence ejiX has all rows equal to zero except for the jth row given by x>i . We can then verify:
X>P (i)ejiX = Pij(xj − ∑ k Pikxk)x > i . (20)
Also note P (i) is symmetric, and each row/colum sums to 0, i.e. P (i)1 = 1>P (i) = 0. Hence we may simplify the Jacobian terms as follows: J̃ii = 2√ D/H [ X>P (i)(XWK − 1xTi WQ)WQ > +X>P (i)eiiX(W Q −WK)WK > ] + PiiI
= 2√ D/H
[ X>P (i)(XWK − 1xTi WQ)WQ > + Pii(xi − ∑ k Pikxk)x > i (W Q −WK)WK > ] + PiiI
= 2√ D/H
[ X>P (i)XWKWQ > + Pii(xi −
∑ k Pikxk)x > i (W Q −WK)WK >
] + PiiI,
(21)
and for i 6= j:
J̃ij = 2√ D/H X>P (i)(eijXW Q − ejjXWK)WK > + PijI
= 2√ D/H Pij(xj − ∑ k Pikxk)(x > i W Q − x>j WK)WK > + PijI. (22)
We are now ready to show that f̃ is not Lipschitz for general WQ,WK :
Lemma F.1. If WK ∈ RD×D/H is full rank (i.e. full column rank), and WK 6=WQ, then Jij has terms that are unbounded for i 6= j, hence f̃ is not Lipschitz.
Proof. Let us investigate the expression K̃ij := PijWK > (xj − ∑ k Pikxk)(x > i W
Q − x>j WK) ∈ RDH×DH for i 6= j, which is related to J̃ij as follows by Equation (22):
WK > J̃ij = ( 2√ D/H K̃ij + PijI ) WK > .
It suffices to show that K̃ij is unbounded to show that J̃ij is unbounded, since WK is full rank and Pij ∈ [0, 1].
Let y>j = x > i W Q − x>j WK . Then we have:
yj − ∑ k Pikyk =W Q>xi −WK > xj − ∑ k Pik(W Q>xi −WK > xk)
=WQ > xi −WK > xj − (WQ > xi − ∑ k PikW K>xk) = −WK > (xj −
∑ k Pikxk).
Hence K̃ij = −Pij(yj − ∑ k Pikyk)y > j . Note yi can take an arbitrary value in RD/H , since WK 6=WQ and WK is full-rank. For all j 6= i, let us choose xj such that yj = −yi. This is possible for any value of yi since WK is full-rank. Note yj = −yi and not yi. We then have that ‖yj‖22 is equal for all j, hence
Pij := exp(−‖yj‖22)∑ k exp(−‖yk‖22) = 1N for all j. Then for i 6= j, K̃ij simplifies to
K̃ij = − 1
N
( −yi − 1
N (N − 2)(−yi)
) (−yi)> = −
2N − 2 N2 yiy > i
whose entries are unbounded since yi can be any vector in RD/H (note we assume N ≥ 2 for self-attention to be well-defined, hence 2N − 2 6= 0).
The intuition for this result is as follows: a reason for DP-MHA not being Lipschitz is that for xi = 0„ the attention weights Pij become uniform regardless of the values of xj for j 6= i. A similar issue arises for L2-MHA with WQ 6= WK and full-rank WK , as shown above: given any xi, we can choose xj such that the Pij become uniform.
F.2 L2 SELF-ATTENTION IS LIPSCHITZ FOR WQ = WK
Hence we impose the restriction that WK =WQ. With this assumption we have Pij ∝ exp ( −‖(xi − xj)> √ A‖22 ) (23)
where A =WQWQ > / √ D/H ∈ RD×D and √ A is chosen such that A = √ A √ A >
, in particular√ A :=WQ/(D/H) 1 4 . The terms in the Jacobian of f̃ simplify to:
J̃ii = 2X >P (i)XA+ PiiI (note P (i)1 = 0), (24) J̃ij = 2Pij(xj − ∑ k Pikxk)(xi − xj)>A+ PijI for i 6= j. (25)
Let the Jacobian of f(X) be:
Jf = J11 . . . J1N... . . . ... JN1 . . . JNN  ∈ RND×ND. (26) Since f(X) = f̃(X)A, and by the chain rule ∂∂xj [f̃i(X)A] = A > ∂f̃i(X) ∂xj = A∂f̃i(X)∂xj (by symmetry of A), we have that Jij = AJ̃ij . Hence
Jii = 2AX >P (i)XA+ PiiA (note P (i)1 = 0), (27) Jij = 2PijA(xj − ∑ k Pikxk)(xi − xj)>A+ PijA for i 6= j. (28)
Noting Lipp(f) = supX ‖Jf (X)‖p, we would like to upper bound ‖Jf‖p.
F.2.1 UPPER BOUND ON Lip∞(F ) FOR L2-MHA
Consider the choice p = ∞, where ‖Jf‖∞ is the maximum absolute row sum of Jf . A key observation is that if we can bound the∞-norm of the Jacobian of fi, a single output of f (i.e. a single block row ‖[Ji1, ..., JiN ]‖∞ of Jf ), then this is also a bound on ‖Jf‖∞ due to permutation equivariance of self-attention; all block rows have the same maximal ‖ · ‖∞ when each is optimised over the input X . Using this, we can prove that ‖Jf‖∞ admits an upper bound that is O(logN − log logN). Below we state and prove lemmas that lead to the proof of this upper bound.
First we analyse the term √ A > X>P (i)X √ A, that appears in the first term of Jii. Note that for
Y := X √ A, so that the rows of Y are y>i := x > i √ A, we have
√ A > X>P (i)X √ A = Y >P (i)Y = Cov(Y) (29)
where P(Y = yj) = Pij = exp(−‖yj − yi‖22)/ ∑ k exp(−‖yk − yi‖22). The last equality uses the observation in Equation (9).
The central inequality used throughout the proof of the main theorem is the following:
Lemma F.2. Tr(Cov(Y)) = ∑ j Pij‖yj− ∑ k Pikyk‖22 ≤ ∑ j Pij‖yj−yi‖22 ≤ φ−1(N −1) where φ(c) = c exp(c+ 1) is a one-dimensional invertible function on R≥0.
Proof. The first equality holds since Tr(Cov(Y)) = ∑ j Cov(Y)jj = ∑ j Var(Yj) = ∑ j E[(Yj − E[Yj ])2]. The next inequality holds since Var(Yj) = Var(Yj) = E[Y 2 j ]− E[Yj ]2 ≤ E[Y 2
j ] where Y = Y− yi. The final inequality can be proved as follows. We would like to bound∑
j
Pij‖yj − yi‖22 = ∑ j ‖yj − yi‖22 exp(−‖yj − yi‖22)∑
k exp(−‖yk − yi‖22) =
∑ j z
2 j exp(−z2j )∑ k exp(−z2k)
(30)
where zj := ‖yj − yi‖2 (hence zi = 0). Define:
g(z) :=
∑ j z
2 j exp(−z2j )∑ k exp(−z2k) =
∑ j 6=i z 2 j exp(−z2j )
1 + ∑ k 6=i exp(−z2k) . (31)
First note that as zj →∞, exp(−z2j )→ 0 exponentially fast, causing the product z2j exp(−z2j )→ 0. Hence we expect the above quantity to be bounded and attain its maximum.
Let h(zj) := exp(−z2j ) for notational conciseness, and note h(zj) > 0. By taking partial derivatives with the chain rule, we have that for j 6= i
∂g(z)
∂zj =
2yjh(zj) ( ∑ k h(zk)) 2
[ (1− z2j ) ∑ k h(zk) + ∑ k h(zk)z 2 k ] . (32)
Hence the derivative is 0 if and only if zj = 0 or (1− z2j ) ∑ k h(zk) + ∑ k h(zk)z 2 k = 0, the latter
being equivalent to z2j = 1 + ∑ k h(zk)z 2 k∑
k h(zk) = 1 + g(z). Hence at the maximum, the non-zero values
among {zj}Nj=1 must be equal to one another. It is clear now that the maximum value c is attained when z2j = 1 + c for j 6= i (and recall zi = 0). So h(zj) = exp(−1 − c) for j 6= i. Substituting this into g(z), and rearranging, we obtain c exp(c + 1) = N − 1. Note φ(x) := x exp(x + 1) is increasing for x > 0 hence c = φ−1(N − 1).
Note φ(logN) = (logN) exp(logN+1) ≥ N logN ≥ N−1 forN ≥ 3. Since φ is increasing, we have φ−1(N−1) ≤ log(N) forN ≥ 3. In fact, it is known that φ−1(N−1) = O(logN−log logN) (Corless et al., 1996).
Note theA term in f(X) = f̃(X)A allows us to use the above inequality, since Y >P (i)Y = Cov(Y) now appears in the terms of Jf :
Jii = 2 √ A[Y >P (i)Y ] √ A > + PiiA, (33)
Jij , = 2 √ APij(yj − ∑ k Pikyk)(yi − yj)> √ A > + PijA for i 6= j. (34)
Using the inequalities ‖BC‖ ≤ ‖B‖‖C‖, ‖B+C‖ ≤ ‖B‖+‖C‖ and ‖[A1, . . . , AN ]‖ ≤ ∑ i ‖Ai‖, we have: ‖[Ji1, . . . , JiN ]‖∞ ≤‖Jii‖∞ +
∑ j 6=i ‖Jij‖∞
≤2‖ √ A‖∞‖Y >P (i)Y ‖∞‖ √ A > ‖∞ + Pii‖A‖∞
+ 2 ∑ j 6=i ‖ √ A‖∞‖Pij(yj − ∑ k Pikyk)(yi − yj)>‖∞‖ √ A > ‖∞ + Pij‖A‖∞
=2‖ √ A‖∞‖ √ A > ‖∞ ( ‖Y >P (i)Y ‖∞ + ∑ j 6=i ‖Pij(yj − ∑ k Pikyk)(yi − yj)>‖∞ ) + ‖A‖∞
=2 ‖WQ‖∞‖WQ >‖∞√ D/H
( ‖Y >P (i)Y ‖∞ + ∑ j ‖Pij(yj − ∑ k Pikyk)(yi − yj)>‖∞ ) + ‖WQWQ>‖∞√ D/H .
For the first equality, note that ∑ j Pij = 1. For the second equality, note that the summand for j = i is 0 because the term yi − yj = 0. Each of the terms in the brackets are bounded by the following lemmas: Lemma F.3. ‖Y >P (i)Y ‖∞ ≤ φ−1(N − 1) √ D/H (φ defined as in Lemma F.2).
Proof. Recall that Y >P (i)Y = Cov(Y). Let σ(Ym) denote the standard deviation of Ym. Then [Cov(Y)]lm ≤ σ(Yl)σ(Ym). Hence
‖Cov(Y)‖∞ = max l ∑ m |[Cov(Y)]lm| ≤ max l σ(Yl) ∑ m σ(Ym)
≤ √ D
H ∑ m σ2(Ym) = √ D H Tr(Cov(Y))
≤ √ D
H φ−1(N − 1),
since ∑ m σ(Ym) ≤ √ D H √∑ m σ
2(Ym) (by e.g. using the Cauchy–Schwartz inequality on [σ(Y1), . . . , σ(YD/H)] and 1) and maxl σ(Yl) ≤ √∑ m σ
2(Ym), and the last inequality is from Lemma F.2.
Lemma F.4. ∑ j ‖Pij(yj − ∑ k Pikyk)(yi − yj)>‖∞ ≤ φ−1(N − 1) √ D/H .
Proof. Note ‖ab>‖∞ = ‖a‖∞‖b‖1 for real vectors a, b. Hence∑ j ‖Pij(yj − ∑ k Pikyk)(yi − yj)>‖∞ = ∑ j Pij‖yj − ∑ k Pikyk‖∞‖yi − yj‖1
= a>b ≤ ‖a‖2‖b‖2, where aj = √ Pij‖yj − ∑ k Pikyk‖∞, bj = √ Pij‖yi − yj‖1.
Note aj ≤ cj := √ Pij‖yj − ∑ k Pikyk‖2 since ‖x‖∞ ≤ ‖x‖2 for vector x. Hence ‖a‖2 ≤ ‖c‖2.
Also bj ≤ √ D H dj := √ D H √ Pij‖yi − yj‖1 since ‖x‖1 ≤ √ D H ‖x‖2 (e.g. by the Cauchy–Schwartz
inequality on [|x1|, . . . , |xD/H |] and 1) for x ∈ RD/H . Hence ‖b‖2 ≤ √ D H ‖d‖2.
Note ‖c‖22 = ∑ j Pij‖yj− ∑ k Pikyk‖22 = Tr(Cov(Y)) ≤ φ−1(N−1) from Lemma F.2, and ‖d‖22 =∑
j Pij‖yi − yj‖22 ≤ φ−1(N − 1) also from Lemma F.2. Hence ‖a‖2‖b‖2 ≤ √
D H ‖c‖2‖d‖2 ≤√
D Hφ −1(N − 1).
Putting the above lemmas altogether, with the observation supX ‖Jf (X)‖∞ = supX ‖[Ji1(X), . . . , JiN (X)]‖∞ by permutation invariance of ‖Jf‖∞ (since f is permutation equivariant and ‖ · ‖∞ is the maximum absolute row sum), we have
‖Jf‖∞ ≤ 4‖WQ‖∞‖WQ > ‖∞φ−1(N − 1) + ‖WQWQ>‖∞√ D/H
≤ ‖WQ‖∞‖WQ > ‖∞ ( 4φ−1(N − 1) + 1√
D/H
) (35)
≤ ‖WQ‖∞‖WQ > ‖∞ ( 4 logN +
1√ D/H
) ,
where the last inequality holds for N ≥ 3. The full multihead attention map that combines the heads fh(X) is:
F : X 7→ [ f1(X)WV,1, . . . fH(X)WV,H ] WO = g(X)WVWO
where g : X 7→ [f1(X), . . . , fH(X)], WO ∈ RD×D and
WV = W V,1 . . . 0 ... . . . ...
0 . . . WV,H  ∈ RDH×D. Note the Jacobian Jg is a block matrix whose rows are Jfh , hence ‖Jg‖∞ = maxh ‖Jfh‖∞, and similarly ‖WV >‖∞ = maxh ‖WV,h >‖∞. Hence we have
Lip∞(F ) ≤ max h ‖Jfh‖∞max h ‖WV,h
> ‖∞‖WO > ‖∞.
Combining this with Inequality (35), we have:
Lip∞(F ) ≤
( 4φ−1(N − 1) + 1√
D/H
) max h ‖WQ,h‖∞‖WQ,h > ‖∞max h ‖WV,h > ‖∞ ‖WO > ‖∞.
F.2.2 UPPER BOUND ON Lip2(F ) FOR L2-MHA
For p = 2, we use the following lemma: Lemma F.5. Let A be a block matrix with block rows A1, . . . , AN . Then ‖A‖2 ≤ √∑
i ‖Ai‖22, and equality holds if and only if the first right singular vectors of the Ai align.
Proof.
‖A‖22 = ∥∥∥∥∥∥∥ A1... AN  ∥∥∥∥∥∥∥ 2
2
= sup ‖x‖2=1 ∥∥∥∥∥∥∥ A1... AN x ∥∥∥∥∥∥∥ 2
2
= sup ‖x‖2=1 ∑ i ‖Aix‖22 ≤ ∑ i sup ‖x‖2=1 ‖Aix‖22 = ∑ i ‖Ai‖22.
Note that equality holds if and only if the first right singular vectors of the Ai align.
Hence a bound on the spectral norm of each block row of Jf can give us an O( √ N) bound on ‖Jf‖2, which may be loose, and it remains an open question as to whether this bound can be tightened.
To bound the ‖ · ‖2 norm of each row of Jf , we use the following lemmas: Lemma F.6. ‖Y >P (i)Y ‖2 ≤ φ−1(N − 1)
Proof. ‖Y >P (i)Y ‖2 = ‖Cov(Y)‖2 = λmax(Cov(Y)) ≤ Tr(Cov(Y)) ≤ φ−1(N − 1), where the first equality holds by symmetry of Cov(Y) and the next holds by Cov(Y) being positive semidefinite, so all its eigenvalues are non-negative, and hence the maximal eigenvalue is bounded by the sum of the eigenvalues, equal to its trace. The final inequality is from Lemma F.2.
Lemma F.7. ∑ j ‖Pij(yj − ∑ k Pikyk)(yi − yj)>‖2 ≤ φ−1(N − 1)
Proof. Directly use Cauchy–Schwartz on c and d in the proof of Lemma F.4.
Again using the inequalities ‖BC‖ ≤ ‖B‖‖C‖, ‖B + C‖ ≤ ‖B‖ + ‖C‖ and ‖[A1, . . . , AN ]‖ ≤∑ i ‖Ai‖, with the additional equality ‖B>‖2 = ‖B‖2, we have the bound:
‖[Ji1, . . . , JiN ]‖2
≤ 2‖W Q‖2‖WQ >‖2√ D/H
( ‖Y >P (i)Y ‖2 + ∑ j ‖Pij(yj − ∑ k Pikyk)(yi − yj)>‖2 ) + ‖WQWQ>‖2√ D/H
≤ 4φ−1(N − 1)‖W Q‖22√ D/H + ‖WQWQ>‖2√ D/H ≤ ‖W Q‖22√ D/H ( 4φ−1(N − 1) + 1 ) .
Using Lemma F.5, we have that
‖Jf‖2 ≤ √ N‖WQ‖22√ D/H
( 4φ−1(N − 1) + 1 ) (36)
≤ √ N‖WQ‖22√ D/H (4 logN + 1).
To obtain the final result for the full multihead self-attention F , we need a final lemma: Lemma F.8. Let A be a block matrix with block columns A1, . . . , AN . Then ‖A‖2 ≤ √∑ i ‖Ai‖22.
Proof.
‖A‖2 = ‖[A1, . . . , AN ]‖2 = sup∑ i ‖xi‖22=1 ∥∥∥∥∥∥∥[A1, . . . , AN ] x1... xN  ∥∥∥∥∥∥∥ 2
2
= sup∑ i ‖xi‖22=1 ‖ ∑ i Aixi‖2
≤ sup∑ i ‖xi‖22=1 ∑ i ‖Aixi‖2 = sup ‖ei‖2=1, ∑ i λ 2 i=1 ∑ i λi‖Aiei‖2 = sup∑ i λ 2 i=1 ∑ i λi‖Ai‖2
≤ √∑
i
‖Ai‖22,
where we are using the substitution xi = λiei, and the last inequality holds by e.g. Cauchy–Schwartz inequality on [λ1, . . . , λN ] and [‖A1‖2, . . . , ‖AN‖2].
Recall that F : X 7→ [ f1(X)WV,1, . . . , fH(X)WV,H ] WO. Since ‖fh(X)WV,h‖2 ≤ ‖Jfh‖2‖WV,h‖2, by Lemma F.8 we have that∥∥[f1(X)WV,1, . . . , fH(X)WV,H ]∥∥ 2 ≤ √∑
h
‖Jfh‖22‖WV,h‖22
and hence
Lip2(F ) ≤ √∑ h ‖Jfh‖22‖WV,h‖22  ‖WO‖2. (37) Combining this with Inequality (36), we have:
Lip2(F ) ≤ √ N√ D/H ( 4φ−1(N − 1) + 1 )(√∑ h ‖WQ,h‖22 ‖WV,h‖22 ) ‖WO‖2.

heading: G THE CASE WITH MASKING
text: Since self-attention is often used with masking, a natural question is how masking affects the derived bounds. In self-attention (for any choice of attention function), masking is implemented as follows: given a set of mask indicesM⊂ {1, . . . , N}× {1, . . . , N}, the logits (i.e. the inputs to the softmax) are set to −∞ at the mask indices. That is,
Lij = { L̃ij if (i, j) /∈M −∞ if (i, j) ∈M
where L̃ij is the original logit (e.g. for L2 self-attention, L̃ij = −(xi − xj)>A(xi − xj)). Masking implies fi(X) is not a function of xj for (i, j) ∈M, hence Jij = 0 for (i, j) ∈M. Thus fi(X) is equal to the ith output for self-attention with inputs restricted to {xj : (i, j) /∈ M}, the unmasked inputs with respect to the ith output. Hence Jij will no longer contribute to the bound on ‖[Ji1, . . . , JiN ]‖, and hence the bound for the unmasked case will continue to hold as long as (i, i) ∈M i.e. xi attends to itself (this is necessary for the proof of Lemma F.2 to hold). The bound can in fact be tightened by replacing N with |{xj : (i, j) /∈ M}|, the number of unmasked inputs with respect to the ith output.

heading: H DROPOUT IS CONTRACTIVE
text: At test time, Dropout multiplies inputs by the dropout keep probability p < 1, so it is a contraction with Lipschitz constant p at evaluation time. At training time, Dropout amounts to setting some inputs to zero, while keeping other inputs constant. This can be expressed as right multiplication by a diagonal binary matrix M , and for such matrices we can verify ‖M‖p := sup‖x‖p=1 ‖Mx‖p ≤ 1.

heading: I EXPERIMENTAL DETAILS
text: For the experiment in Section 5.1, showing the asymptotic tightness of the upper bound on Lip∞(F ) where F is L2-MHA, we fix all free parameters of F (namely WQ,WV ) to be the identity, and only optimise the input X . We use 50 random initialisations of X for each N , where Xij ∼ U [−c, c] for c ∼ U [0, 10] (we observed that having c itself be random improves optimisation). We display the top 5 results for each value of N after optimising each random initialisation till convergence using Adam (Kingma & Ba, 2015) with a learning rate of 0.1.
For the experiments in Section 5.3, we comparing the performance of the original Transformer and the Transformer with Lipschitz/invertible self-attention at character-level language modelling on the Penn Treebank dataset (Marcus et al., 1993).1 Each training example is a sentence represented as a variable-length sequence of characters, and examples are batched according to length such that padding is minimised, with the maximum sequence length set to 288. All models are autoregressive, outputting the logits for the categorical likelihood predicting the next character, and are trained using maximum likelihood (cross-entropy loss) with a batch size of 64. The LSTM models have the dimensionality of the hidden state equal to the dimensionality D of the cell state (the usual default implementation). The Transformer models are trained with a varying number of blocks (number of layers) with H = 8 heads and D = 512, tuning hyperparameters for dropout rate in {0, 0.1, 0.2} and base learning rate γ ∈ {0.2, 0.4, 0.6, 0.8, 1.0, 1.5, 2.0} with number of warmup iterations w ∈ {1000, 2000, 4000, 8000} for the standard custom learning rate schedule in Vaswani et al. (2017):
t = γ√ D min(t−1/2, tw−3/2),
where t is the learning rate at training iteration t. Hence the learning rate linearly increases from 0 to (Dw)−1/2 over w iterations, then decays proportionally to t−1/2. We use Glorot Uniform
initialisation (Glorot & Bengio, 2010) for all weights (U [ − √
1 din+dout
, √
1 din+dout
] ), except for
weights in L2-MHA that are initialised fromU [ − s√
D , s√ D
] , and s is a hyperparameter. ForD = 512,
we used s = 124 . All experiments were done in Tensorflow 1.14 (Abadi et al., 2016). The code will be released upon de-anonymisation.
In Table 2 we show the best Test NLL across training of Transformer models in Figure 4.

heading: J NUMERICAL INVERTIBILITY OF MHA RESIDUAL MAP
text: Following Section 5.2, Figure 5 confirms that numerical invertibility does not hold for trained weights for dot-product multihead self-attention (DP-MHA) (obtained from one-layer Transformer (DP)
1We use the standard training-validation-test split, and the dataset can be found at e.g. https://github. com/harvardnlp/TextFlow/tree/master/data/ptb.
model used for Figure 4), similar to the randomly initialised weight case. Figure 6 shows additional results for different values of N and D.
K BEHAVIOUR OF LOWER BOUND ON Lip2(F )
In Figure 7, we show the lower bound on Lip2(F ) obtained by optimising ‖JF (X)‖2 using the same optimisation procedure as for Figure 2 of Section 5.1. Here the optimisation is more difficult, evident in the variance of the top 5 values, and the trend is less clear, but it appears that Lip2(f) grows at a rate of O(logN). The message is less clear here, and there are at least two possibilities:
(1) The optimisation is difficult even for small values of N , hence Figure 7 shows a loose lower bound. (2) If the lower bound is tight, this suggests that the O( √ N logN) bound in Theorem 3.2 is not
asymptotically tight, and could be improved to O(logN) (or O(logN − log logN) as for p =∞).

heading: L OPTIMISING THE NORM OF THE JACOBIAN OF DP-MHA
text: In Figure 8, we show how the norm of the Jacobian ‖Jf (X)‖∞ for DP-MHA f keeps increasing when being optimised with respect to X . This is a useful sanity check validating our theoretical result of Theorem 3.1, that DP-MHA is not Lipshchitz. The oscillations are likely due to momentum term of Adam optimizer that was used to optimise the norm.

heading: M EXPERIMENT TYING KEYS AND QUERIES OF L2-MHA BUT PRESERVING PARAMETER COUNT
text: In Figure 4 of Section 5.3, we have shown that there is a clear reduction in performance when tying the keys and queries. To test whether this can be attributed to the reduction in parameter count, we tried doubling the number of columns ofWQ when the keys and queries are shared (i.e. fromD/H to 2D/H) so that the shared model has the same number of parameters as the unshared model. In Figure 9, the third column shows results for shared L2-MHA, but with the same number of parameters as the unshared L2-MHA i.e. without tying the keys and queries. The performance is similar to the second column (tying with a reduced number of parameters), suggesting that there is an inherent limitation in expressiveness to tying the keys and queries, and that the reduction in number of parameters is an insufficient explanation this phenomenon.

heading: N STABILITY EXPERIMENTS
text: In Figure 10 below, we compare the output variance of trained L2-MHA against trained DP-MHA, with weights from the one-layer Transformer (L2), WQ =WK model and (DP) model used for Figure 4 respectively. We take the same distribution of inputs as used for the numerical invertibility experiment in Section 5.2, and show the histogram of inputs and outputs after flattening the input/output tensors. We see that the range of outputs remains similar to the range of inputs for Lipschitz L2-MHA, whereas for DP-MHA the outputs have a much wider range, because the Jacobian norm is large for DP-MHA at these inputs.
In practice, this leads to instabilities while training for DP-MHA, hence requiring careful tuning of the learning rate schedule for training deeper Transformer models: linear warmup and square root decay, as detailed in Appendix I. In Figure 11, we show how training instabilities arise for DP-MHA with deeper Transformer models if we use a fixed learning rate. We compare the training curves of DP-MHA, L2-MHA (WQ = WK) and Contractive-L2-MHA for a varying number of layers for the first 20 epochs of training with a fixed learning rate. We see that DP-MHA fails to train beyond 10 layers, whereas both L2-MHA (WQ = WK) (i.e. Lipschitz L2-MHA but not contractive) and Contractive-L2-MHA shows stable training for up to 18 layers. This was the deepest model we could fit on a single GPU, and we expect to be able to train even deeper models with L2-MHA (WQ =WK) and Contractive-L2-MHA. In Table 2 we show the best Test NLL across training of Transformer models with fixed learning rate. Note that for DP-MHA training becomes unstable beyond 10 layers, so we are only able to provide results up to 10 layers. The generalisation performance of the best model for each setting is similar.

Review summaries:
This paper studies the Lipschitz continuity properties of self-attention. It is proved that the widely-used dot-product self-attention is not Lipschitz continuous. A novel L2 self-attention is proposed and proven to be Lipschitz continuous. Experiments show that the upper bound of Lipschitz constant for L2 self-attention is asymptotically tight. Invertibility of MHA residual map is investigated to prove the Lipschitz continuity of L2 self-attention. Finally, experiments on Transformers with L2 self-attention are studied.