Robustness Verification for Transformers

Zhouxing Shi, Huan Zhang, Kai-Wei Chang, Minlie Huang, Cho-Jui Hsieh

Introduction

Deep neural networks have been successfully applied to many domains. However, these black-box models are generally difficult to analyze and their behavior is not guaranteed. Moreover, it has been shown that the predictions of deep networks become unreliable and unstable when tested in unseen situations, e.g., in the presence of small adversarial perturbations to the input (Szegedy et al., 2013; Goodfellow et al., 2014; Lin et al., 2019). Therefore, neural network verification has become an important tool for analyzing and understanding the behavior of neural networks, with applications in safety-critical applications (Katz et al., 2017; Julian et al., 2019; Lin et al., 2019), model explanation (Shih et al., 2018) and robustness analysis (Tjeng et al., 2019; Wang et al., 2018c; Gehr et al., 2018; Wong & Kolter, 2018; Singh et al., 2018; Weng et al., 2018; Zhang et al., 2018).

We resolve several particular challenges in verifying Transformers. First, Transformers with self-attention layers have a complicated architecture. Unlike simpler networks, they cannot be written as multiple layers of affine transformations or element-wise activation functions. Therefore, we need to propagate linear bounds differently for self-attention layers. Second, dot products, softmax, and weighted summation in self-attention layers involve multiplication or division of two variables both under perturbation, namely cross-nonlinearity, which is not present in feed-forward networks. Ko et al. (2019) proposed a gradient descent based approach to find linear bounds, however it is inefficient and poses a computational challenge for Transformer verification since self-attention is the core of Transformers. In contrast, we derive closed-form linear bounds that can be computed in O(1)O(1) complexity. Third, in the computation of self-attention, output neurons in each position depend on all input neurons from different positions (namely cross-position dependency), unlike the case in recurrent neural networks where outputs depend on only the hidden features from the previous position and the current input. Previous works (Zhang et al., 2018; Weng et al., 2018; Ko et al., 2019) have to track all such dependency and thus is costly in time and memory. To tackle this, we introduce an efficient bound propagating process in a forward manner specially for self-attention layers, enabling the tighter backward bounding process for other layers to utilize bounds computed by the forward process. In this way, we avoid cross-position dependency in the backward process which is relatively slower but produces tighter bounds. Combined with the forward process, the complexity of the backward process is reduced by O(n)O(n) for input length nn, while the computed bounds remain comparably tight. Our contributions are summarized below:

We propose an effective and efficient algorithm for verifying the robustness of Transformers with self-attention layers. To our best knowledge, this is the first method for verifying Transformers.

We resolve key challenges in verifying Transformers, including cross-nonlinearity and cross-position dependency. Our bounds are significantly tighter than those by adapting Interval Bound Propagation (IBP) (Mirman et al., 2018; Gowal et al., 2018).

We quantitatively and qualitatively show that the certified bounds computed by our algorithm consistently reflect the importance of input words in sentiment analysis, which justifies that these bounds are meaningful in practice and they shed light on interpreting Transformers.

Related Work

Several works focus on solving Eq. (1) exactly and optimally, using mixed integer linear programming (MILP) (Tjeng et al., 2019; Dutta et al., 2018), branch and bound (BaB) (Bunel et al., 2018), and satisfiability modulo theory (SMT) (Ehlers, 2017; Katz et al., 2017). Unfortunately, due to the nonconvexity of model ff, solving Eq. (1) is NP-hard even for a simple ReLU network (Katz et al., 2017). Therefore, we can only expect to compute a lower bound of Eq. (1) efficiently by using relaxations. Many algorithms can be seen as using convex relaxations for non-linear activation functions (Salman et al., 2019), including using duality (Wong & Kolter, 2018; Dvijotham et al., 2018), abstract domains (Gehr et al., 2018; Singh et al., 2018; Mirman et al., 2018; Singh et al., 2019), layer-by-layer reachability analysis (Wang et al., 2018b; Weng et al., 2018; Zhang et al., 2018; Gowal et al., 2018) and semi-definite relaxations (Raghunathan et al., 2018; Dvijotham et al., 2019). Additionally, robustness verification can rely on analysis on local Lipschitz constants (Hein & Andriushchenko, 2017; Zhang et al., 2019). However, existing methods are mostly limited to verifying networks with relatively simple architectures, such as feed-forward networks and RNNs (Wang et al., 2018a; Akintunde et al., 2019; Ko et al., 2019), while none of them are able to handle Transformers.

Transformers and Self-Attentive Models.

Transformers (Vaswani et al., 2017) based on self-attention mechanism, further with pre-training on large-scale corpora, such as BERT (Devlin et al., 2019), XLNet (Yang et al., 2019), RoBERTa (Liu et al., 2019), achieved state-of-the-art performance on many NLP tasks. Self-attentive models are also useful beyond NLP, including VisualBERT on vision and language applications (Li et al., 2019b; Su et al., 2019), image transformer for image generation (Parmar et al., 2018), acoustic models for speech recognition (Zhou et al., 2018), sequential recommendation (Kang & McAuley, 2018) and graph embedding (Li et al., 2019a).

The robustness of NLP models has been studied, especially many methods have been proposed to generate adversarial examples (Papernot et al., 2016; Jia & Liang, 2017; Zhao et al., 2017; Alzantot et al., 2018; Cheng et al., 2018; Ebrahimi et al., 2018; Shi et al., 2019). In particular, Hsieh et al. (2019) showed that Transformers are more robust than LSTMs. However, there is not much work on robustness verification for NLP models. Ko et al. (2019) verified RNN/LSTM. Jia et al. (2019); Huang et al. (2019) used Interval Bound Propagation (IBP) for certified robustness training of CNN and LSTM. In this paper, we propose the first verification method for Transformers.

Methodology

We aim to verify the robustness of a Transformer whose input is a sequence of frames X=[x(1),x(2),⋯ ,x(n)]{\mathbf{X}}=[{\mathbf{x}}^{(1)},{\mathbf{x}}^{(2)},\cdots,{\mathbf{x}}^{(n)}]. We take binary text classification as a running example, where x(i){\mathbf{x}}^{(i)} is a word embedding and the model outputs a score yc(X)y_{c}({\mathbf{X}}) for each class cc (c∈{0,1}c\in\{0,1\}). Nevertheless, our method for verifying Transformers is general and can also be applied in other applications.

We compute bounds from the first sub-layer to the last sub-layer. For neurons in the ll-th layer, we aim to represent their bounds as linear functions of neurons in a previous layer, the l′l^{\prime}-th layer:

where 1/p+1/q=11/p+1/q=1 with p,q≥1p,q\geq 1. These steps resemble to CROWN (Zhang et al., 2018) which is proposed to verify feed-forward networks. We further support verifying self-attentive Transformers which are more complex than feed-forward networks. Moreover, unlike CROWN that conducts a fully backward process, we combine the backward process with a forward process (see Sec. 3.3) to reduce the computational complexity of verifying Transformers.

2 Linear Transformations and Unary Nonlinear Functions

Linear transformations and unary nonlinear functions are basic operations in neural networks. We show how bounds Eq. (2) at the l′l^{\prime}-th sub-layer are propagated to the (l′−1)(l^{\prime}-1)-th layer.

If the l′l^{\prime}-th sub-layer is connected with the (l′−1)(l^{\prime}-1)-th sub-layer with a linear transformation Φ(l′,k)(X)=W(l′)Φ(l′−1,k)(X)+b(l′)\Phi^{(l^{\prime},k)}({\mathbf{X}})={\mathbf{W}}^{(l^{\prime})}\Phi^{(l^{\prime}-1,k)}({\mathbf{X}})+{\mathbf{b}}^{(l^{\prime})} where W(l′),b(l′){\mathbf{W}}^{(l^{\prime})},{\mathbf{b}}^{(l^{\prime})} are parameters of the linear transformation, we propagate the bounds to the (l′−1)(l^{\prime}-1)-th layer by substituting Φ(l′,k)(X)\Phi^{(l^{\prime},k)}({\mathbf{X}}):

where “L/UL/U” means that the equations hold for both lower bounds and upper bounds respectively.

Unary Nonlinear Functions

If the l′l^{\prime}-th layer is obtained from the (l′−1)(l^{\prime}-1)-th layer with an unary nonlinear function Φj(l′,k)(X)=σ(l′)(Φj(l′−1,k)(X))\Phi^{(l^{\prime},k)}_{j}({\mathbf{X}})=\sigma^{(l^{\prime})}(\Phi^{(l^{\prime}-1,k)}_{j}({\mathbf{X}})), to propagate linear bounds over the nonlinear function, we first bound σ(l′)(Φj(l′−1,k)(X))\sigma^{(l^{\prime})}(\Phi^{(l^{\prime}-1,k)}_{j}({\mathbf{X}})) with two linear functions of Φj(l′−1,k)(X)\Phi^{(l^{\prime}-1,k)}_{j}({\mathbf{X}}):

where αj(l′,k),L/U,βj(l′,k),L/U\alpha_{j}^{(l^{\prime},k),L/U},\beta_{j}^{(l^{\prime},k),L/U} are parameters such that the inequation holds true for all Φj(l′−1,k)(X)\Phi^{(l^{\prime}-1,k)}_{j}({\mathbf{X}}) within its bounds computed previously. Such linear relaxations can be done for different functions, respectively. We provide detailed bounds for functions involved in Transformers in Appendix B.

where Λ:,j,+(l,i,l′,k),L/U{\bm{\Lambda}}^{(l,i,l^{\prime},k),L/U}_{:,j,+} and Λ:,j,−(l,i,l′,k),L/U{\bm{\Lambda}}^{(l,i,l^{\prime},k),L/U}_{:,j,-} mean to retain positive and negative elements in vector Λ:,j(l,i,l′,k),L/U{\bm{\Lambda}}^{(l,i,l^{\prime},k),L/U}_{:,j} respectively and set other elements to 0.

3 Self-Attention Mechanism

Self-attention layers are the most challenging parts for verifying Transformers. We assume that Φ(l−1,i)(X)\Phi^{(l-1,i)}({\mathbf{X}}) is the input to a self-attention layer. We describe our method for computing bounds for one attention head, and bounds for different heads of the multi-head attention in Transformers can be easily concatenated. Φ(l−1,i)(X)\Phi^{(l-1,i)}({\mathbf{X}}) is first linearly projected to queries q(l,i)(X){\mathbf{q}}^{(l,i)}({\mathbf{X}}), keys k(l,i)(X){\mathbf{k}}^{(l,i)}({\mathbf{X}}), and values v(l,i)(X){\mathbf{v}}^{(l,i)}({\mathbf{X}}) with different linear projections, and their bounds can be obtained as described in Sec. 3.2. We also keep their linear bounds that are linear functions of the perturbed embeddings. For convenience, let x(r)=x(r1)⊕x(r2)⊕⋯x(rt){\mathbf{x}}^{(r)}={\mathbf{x}}^{(r_{1})}\oplus{\mathbf{x}}^{(r_{2})}\oplus\cdots{\mathbf{x}}^{(r_{t})}, where ⊕\oplus indicates vector concatenation, and thereby we represent the linear bounds as linear functions of x(r){\mathbf{x}}^{(r)}:

where q/k/vq/k/v and q/k/v{\mathbf{q}}/{\mathbf{k}}/{\mathbf{v}} mean that the inequation holds true for queries, keys and values respectively. We then bound the output of the self-attention layer starting from q(l,i)(X){\mathbf{q}}^{(l,i)}({\mathbf{X}}), k(l,i)(X){\mathbf{k}}^{(l,i)}({\mathbf{X}}), v(l,i)(X){\mathbf{v}}^{(l,i)}({\mathbf{X}}).

We bound multiplications and divisions in the self-attention mechanism with linear functions. We aim to bound bivariate function z=xyz=xy or z=xy(y>0)z=\frac{x}{y}(y>0) with two linear functions zL=αLx+βLy+γLz^{L}=\alpha^{L}x+\beta^{L}y+\gamma^{L} and zU=αUx+βUy+γUz^{U}=\alpha^{U}x+\beta^{U}y+\gamma^{U}, where x∈[lx,ux],y∈[ly,uy]x\in[l_{x},u_{x}],y\in[l_{y},u_{y}] are bounds of x,yx,y obtained previously. For z=xyz=xy, we derive optimal parameters: αL=ly\alpha^{L}=l_{y}, αU=uy\alpha^{U}=u_{y}, βL=βU=lx\beta^{L}=\beta^{U}=l_{x}, γL=−lxly\gamma^{L}=-l_{x}l_{y}, γU=−lxuy\gamma^{U}=-l_{x}u_{y}. We provide a proof in Appendix C. However, directly bounding z=xyz=\frac{x}{y} is tricky; fortunately, we can bound it indirectly by first bounding a unary function y‾=1y\overline{y}=\frac{1}{y} and then bounding the multiplication z=xy‾z=x\overline{y}.

A Forward Process

For the self-attention mechanism, instead of using the backward process like CROWN (Zhang et al., 2018), we compute bounds with a forward process which we will show later that it can reduce the computational complexity. Attention scores are computed from q(l,i)(X){\mathbf{q}}^{(l,i)}({\mathbf{X}}) and k(l,i)(X){\mathbf{k}}^{(l,i)}({\mathbf{X}}): Si,j(l)=(q(l,i)(X))Tk(l,j)(X)=∑k=1dqkqk(l,i)(X)kk(l,j)(X),{\mathbf{S}}_{i,j}^{(l)}=({\mathbf{q}}^{(l,i)}({\mathbf{X}}))^{T}{\mathbf{k}}^{(l,j)}({\mathbf{X}})=\sum_{k=1}^{d_{qk}}{\mathbf{q}}^{(l,i)}_{k}({\mathbf{X}}){\mathbf{k}}^{(l,j)}_{k}({\mathbf{X}}), where dqkd_{qk} is the dimension of q(l,i)(X){\mathbf{q}}^{(l,i)}({\mathbf{X}}) and k(l,j)(X){\mathbf{k}}^{(l,j)}({\mathbf{X}}). For each multiplication qk(l,i)(X)kk(l,j)(X){\mathbf{q}}^{(l,i)}_{k}({\mathbf{X}}){\mathbf{k}}^{(l,j)}_{k}({\mathbf{X}}), it is bounded by:

We then obtain the bounds of Si,j(l){\mathbf{S}}_{i,j}^{(l)}:

Recall that x(r){\mathbf{x}}^{(r)} is a concatenation of x(r1),x(r2),⋯ ,x(rt){\mathbf{x}}^{(r_{1})},{\mathbf{x}}^{(r_{2})},\cdots,{\mathbf{x}}^{(r_{t})}. We can split Ωj,:(l′,i),Φ,L/U{\bm{\Omega}}^{(l^{\prime},i),\Phi,L/U}_{j,:} into tt vectors with equal dimensions, Ωj,:(l′,i,1),Φ,L/U,Ωj,:(l′,i,2),Φ,L/U,⋯ ,Ωj,:(l′,i,t),Φ,L/U{\bm{\Omega}}^{(l^{\prime},i,1),\Phi,L/U}_{j,:},{\bm{\Omega}}^{(l^{\prime},i,2),\Phi,L/U}_{j,:},\cdots,{\bm{\Omega}}^{(l^{\prime},i,t),\Phi,L/U}_{j,:}, such that Eq. (5) becomes

Backward Process to Self-Attention Layers

When computing bounds for a later sub-layer, the ll-th sub-layer, using the backward process, we directly propagate the bounds at the the closest previous self-attention layer assumed to be the l′l^{\prime}-th layer, to the input layer, and we skip other previous sub-layers. The bounds propagated to the l′l^{\prime}-th layer are as Eq. (2). We substitute Φ(l′,k)(X)\Phi^{(l^{\prime},k)}({\mathbf{X}}) with linear bounds in Eq. (6):

We take global bounds as Eq. (3) and Eq. (4) to obtain the bounds of the ll-th layer.

Advantageous of Combining the Backward Process with a Forward Process

Introducing a forward process can significantly reduce the complexity of verifying Transformers. With the backward process only, we need to compute Λ(l,i,l′,k){\bm{\Lambda}}^{(l,i,l^{\prime},k)} and Δ(l,i,l′){\bm{\Delta}}^{(l,i,l^{\prime})} (l′≤l)(l^{\prime}\leq l), where the major cost is on Λ(l,i,l′,k){\bm{\Lambda}}^{(l,i,l^{\prime},k)} and there are O(m2n2)O(m^{2}n^{2}) such matrices to compute. The O(n2)O(n^{2}) factor is from the dependency between all pairs of positions in the input and output respectively, which makes the algorithm inefficient especially when the input sequence is long. In contrast, the forward process represents the bounds as linear functions of the perturbed positions only instead of all positions by computing Ω(l,i){\bm{\Omega}}^{(l,i)} and Θ(l,i){\bm{\Theta}}^{(l,i)}. Imperceptible adversarial examples may not have many perturbed positions (Gao et al., 2018; Ko et al., 2019), and thus we may assume that the number of perturbed positions, tt, is small. The major cost is on Ω(l,i){\bm{\Omega}}^{(l,i)} while there are only O(mn)O(mn) such matrices and the sizes of Λ(l,i,l′,k){\bm{\Lambda}}^{(l,i,l^{\prime},k)} and Ω(l,i){\bm{\Omega}}^{(l,i)} are relatively comparable for a small tt. We combine the backward process and the forward process. The number of matrices Ω{\bm{\Omega}} in the forward process is O(mn)O(mn), and for the backward process, since we do not propagate bounds over self-attention layers and there is no cross-position dependency in other sub-layers, we only compute Λ(l,i,l′,k){\bm{\Lambda}}^{(l,i,l^{\prime},k)} such that i=ki=k, and thus the number of matrices Λ{\bm{\Lambda}} is reduced to O(m2n)O(m^{2}n). Therefore, the total number of matrices Λ{\bm{\Lambda}} and Ω{\bm{\Omega}} we compute is O(m2n)O(m^{2}n) and is O(n)O(n) times smaller than O(m2n2)O(m^{2}n^{2}) when only the backward process is used. Moreover, the backward process makes bounds tighter compared to solely the forward one, as we explain in Appendix D.

Experiments

To demonstrate the effectiveness of our algorithm, we compute certified bounds for several sentiment classification models and perform an ablation study to show the advantage of combining the backward and forward processes. We also demonstrate the meaningfulness of our certified bounds with an application on identifying important words.

We use two datasets: Yelp (Zhang et al., 2015) and SST (Socher et al., 2013). Yelp consists of 560,000/38,000 examples in the training/test set and SST consists of 67,349/872/1,821 examples in the training/development/test set. Each example is a sentence or a sentence segment (for the training data of SST only) labeled with a binary sentiment polarity.

We verify the robustness of Transformers trained from scratch. For the main experiments, we consider NN-layer models (N≤3N\leq 3), with 4 attention heads, hidden sizes of 256 and 512 for self-attention and feed-forward layers respectively, and we use ReLU activations for feed-forward layers. We remove the variance related terms in layer normalization, making Transformers verification bounds tighter while the clean accuracies remain comparable (see Appendix E for discussions). Although our method can be in principal applied to Transformers with any number of layers, we do not use large-scale pre-trained models such as BERT because they are too challenging to be tightly verified with the current technologies.

2 Certified Bounds

3 Effectiveness of Combining the Backward Process with a Forward Process

In the following, we show the effectiveness of combining the backward process with a forward process. We compare our proposed method (Backward & Forward) with two variations: 1) Fully-Forward propagates bounds in a forward manner for all sub-layers besides self-attention layers; 2) Fully-Backward computes bounds for all sub-layers including self-attention layers using the backward bound propagation and without the forward process. We compare the tightness of bounds and computation time of the three methods. We use smaller models with the hidden sizes reduced by 75%, and we use 1-position perturbation only, to accommodate Fully-Backward with large computational cost. Experiments are conducted on an NVIDIA TITAN X GPU. Table 3 presents the results. Bounds by Fully-Forward are significantly looser while those by Fully-Backward and Backward & Forward are comparable. Meanwhile, the computation time of Backward & Forward is significantly shorter than that of Fully-Backward. This demonstrates that our method of combining the backward and forward processes can compute comparably tight bounds much more efficiently.

4 Identifying Words Important to Prediction

SST contains sentiment labels for all phrases on parse trees, where the labels range from very negative (0) to very positive (4), and 2 for neutral. For each word, assuming its label is xx, we take ∣x−2∣|x-2|, i.e., the distance to the neutral label, as the importance score, since less neutral words tend to be more important for the sentiment polarity of the sentence. We evaluate on 100 random test input sentences and compute the average importance scores of the most or least important words identified from the examples. In Table 4, compared to the baselines (“Upper” and “Grad”), the average importance score of the most important words identified by our lower bounds are the largest, while the least important words identified by our method have the smallest average score. This demonstrates that our method identifies the most and least important words more accurately compared to baseline methods.

Qualitative Analysis on Yelp

We further analyze the results on a larger dataset, Yelp. Since Yelp does not provide per-word sentiment labels, importance scores cannot be computed as on SST. Thus, we demonstrate a qualitative analysis. We use 10 random test examples and collect the words identified as the most and least important word in each example. In Table 4, most words identified as the most important by certified lower bounds are exactly the words reflecting sentiment polarities (boldfaced words), while those identified as the least important words are mostly stopwords. Baseline methods mistakenly identify more words containing no sentiment polarity as the most important. This again demonstrates that our certified lower bounds identify word importance better than baselines and our bounds provide meaningful interpretations in practice. While gradients evaluate the sensitivity of each input word, this evaluation only holds true within a very small neighborhood (where the classifier can be approximated by a first-order Taylor expansion) around the input sentence. Our certified method gives valid lower bounds that hold true within a large neighborhood specified by a perturbation set SS, and thus it provides more accurate results.

Conclusion

We propose the first robustness verification method for Transformers, and tackle key challenges in verifying Transformers, including cross-nonlinearity and cross-position dependency. Our method computes certified lower bounds that are significantly tighter than those by IBP. Quantitative and qualitative analyses further show that our bounds are meaningful and can reflect the importance of different words in sentiment analysis.

Acknowledgement

This work is jointly supported by Tsinghua Scholarship for Undergraduate Overseas Studies, NSF IIS1719097 and IIS1927554, and NSFC key project with No. 61936010 and regular project with No. 61876096.

References

Appendix A Illustration of Different Bounding Processes

Figure 1 illustrates a comparison of the Fully-Forward, Fully-Backward and Backward & Forward processes, for a 2-layer Transformer as an example. For Fully-Forward, there are only forward processes connecting adjacent layers and blocks. For Fully-Backward, there are only backward processes, and each layer needs a backward bound propagation to all the previous layers. For our Backward & Forward algorithm, we use backward processes for the feed-forward parts and forward processes for self-attention layers, and for layers after self-attention layers, they no longer need backward bound propagation to layers prior to self-attention layers. In this way, we resolve the cross-position dependency in verifying Transformers while still keeping bounds comparably tight as those by using fully backward processes. Empirical comparison of the three frameworks are presented in Sec. 4.3.

Appendix B Linear Bounds of Unary Nonlinear Functions

We show in Sec. 3.2 that linear bounds can be propagated over unary nonlinear functions as long as the unary nonlinear functions can be bounded with linear functions. Such bounds are determined for each neuron respectively, according to the bounds of the input for the function. Specifically, for a unary nonlinear function σ(x)\sigma(x), with the bounds of xx obtained previously as x∈[l,u]x\in[l,u], we aim to derive a linear lower bound αLx+βL\alpha^{L}x+\beta^{L} and a linear upper bound αUx+βU\alpha^{U}x+\beta^{U}, such that

where parameters αL,βL,αU,βU\alpha^{L},\beta^{L},\alpha^{U},\beta^{U} are dependent on l,ul,u and designed for different functions σ(x)\sigma(x) respectively. We introduce how the parameters are determined for different unary nonlinear functions involved in Transformers such that the linear bounds are valid and as tight as possible. Bounds of ReLU and tanh has been discussed by Zhang et al. (2018), and we further derive bounds of exe^{x}, 1x\frac{1}{x}, x2x^{2}, x\sqrt{x}. x2x^{2} and x\sqrt{x} are only used when the layer normalization is not modified for experiments to study the impact of our modification. For the following description, we define the endpoints of the function to be bounded within range (l,r)(l,r) as (l,σ(l))(l,\sigma(l)) and (u,σ(u))(u,\sigma(u)). We describe how the lines corresponding to the linear bounds of different functions can be determined, and thereby parameters αL,βL,αU,βU\alpha^{L},\beta^{L},\alpha^{U},\beta^{U} can be determined accordingly.

For ReLU activation, σ(x)=max⁡(x,0)\sigma(x)=\max(x,0). ReLU is inherently linear on segments (−∞,0](-\infty,0] and [0,∞)[0,\infty) respectively, so we make the linear bounds exactly σ(x)\sigma(x) for u≤0u\leq 0 or l≥0l\geq 0; and for l<0<ul<0<u, we take the line passing the two endpoints as the upper bound; and we take σL(x)=0\sigma^{L}(x)=0 when u<∣l∣u<|l| and σL(x)=x\sigma^{L}(x)=x when u≥∣l∣u\geq|l| as the lower bound, to minimize the gap between the lower bound and the original function.

Tanh

For tanh⁡\tanh activation, σ(x)=1−e−2x1+e−2x\sigma(x)=\frac{1-e^{-2x}}{1+e^{-2x}}. tanh⁡\tanh is concave for l≥0l\geq 0, and thus we take the line passing the two endpoints as the lower bound and take a tangent line passing ((l+u)/2,σ((l+u)/2)((l+u)/2,\sigma((l+u)/2) as the upper bound. For u≤0u\leq 0, tanh⁡\tanh is convex, and thus we take the line passing the two endpoints as the upper bound and take a tangent line passing ((l+u)/2,σ((l+u)/2)((l+u)/2,\sigma((l+u)/2) as the lower bound. For l<0<ul<0<u, we take a tangent line passing the right endpoint and (dL,σ(dL))(dL≤0)(d^{L},\sigma(d^{L}))(d^{L}\leq 0) as the lower bound, and take a tangent line passing the left endpoint and (dU,σ(dU))(dU≥0)(d^{U},\sigma(d^{U}))(d^{U}\geq 0) as the upper bound. dLd^{L} and dUd^{U} can be found with a binary search.

Exp

σ(x)=exp(x)=ex\sigma(x)=exp(x)=e^{x} is convex, and thus we take the line passing the two endpoints as the upper bound and take a tangent line passing (d,σ(d))(d,\sigma(d)) as the lower bound. Preferably, we take d=(l+u)/2d=(l+u)/2. However, exe^{x} is always positive and used in the softmax for computing normalized attention probabilities in self-attention layers, i.e., exp(Si,j(l))exp({\mathbf{S}}^{(l)}_{i,j}) and ∑k=1nexp(Si,k(l))\sum_{k=1}^{n}exp({\mathbf{S}}^{(l)}_{i,k}). ∑k=1nexp(Si,k(l))\sum_{k=1}^{n}exp({\mathbf{S}}^{(l)}_{i,k}) appears in the denominator of the softmax, and to make reciprocal function 1x\frac{1}{x} finitely bounded, the range of xx should not pass 0. Therefore, we impose a constraint to force the lower bound function to be always positive, i.e., σL(l)>0\sigma^{L}(l)>0, since σL(l)\sigma^{L}(l) is monotonously increasing. σdL(x)=ed(x−d)+ed\sigma^{L}_{d}(x)=e^{d}(x-d)+e^{d} is the tangent line passing (d,σ(d))(d,\sigma(d)). So the constraint σdL(l)>0\sigma^{L}_{d}(l)>0 yields d<l+1d<l+1. Hence we take d=min⁡((l+u)/2,l+1−Δd)d=\min((l+u)/2,l+1-\Delta_{d}) where Δd\Delta_{d} is a small real value to ensure that d<l+1d<l+1 such as Δd=10−2\Delta_{d}=10^{-2}.

Reciprocal

For the reciprocal function, σ(x)=1x\sigma(x)=\frac{1}{x}. It is used in the softmax and layer normalization and its input is limited to have l>0l>0 by the lower bounds of exp(x)exp(x), and x\sqrt{x}. With l>0l>0, σ(x)\sigma(x) is convex. Therefore, we take the line passing the two endpoints as the upper bound. And we take the tangent line passing ((l+u)/2,σ((l+u)/2))((l+u)/2,\sigma((l+u)/2)) as the lower bound.

Square

For the square function, σ(x)=x2\sigma(x)=x^{2}. It is convex and we take the line passing the two endpoints as the upper bound. And we take a tangent line passing (d,σ(d))(d∈[l,u])(d,\sigma(d))(d\in[l,u]) as the lower bound. We still prefer to take d=(l+u)/2.d=(l+u)/2. x2x^{2} appears in the variance term of layer normalization and is later passed to a square root function to compute a standard derivation. To make the input to the square root function valid, i.e., non-negative, we impose a constraint σL(x)≥0(∀x∈[l,u])\sigma^{L}(x)\geq 0(\forall x\in[l,u]). σdL(x)=2d(x−d)+d2\sigma_{d}^{L}(x)=2d(x-d)+d^{2} is the tangent line passing (d,σ(d))(d,\sigma(d)). For u≤0u\leq 0, x2x^{2} is monotonously decreasing, the constraint we impose is equivalent to σL(u)=2du−d2≥0\sigma^{L}(u)=2du-d^{2}\geq 0, and with d≤0d\leq 0, we have d≥2ud\geq 2u. So we take d=max⁡((l+u)/2,2u)d=\max((l+u)/2,2u). For l≥0l\geq 0, x2x^{2} is monotonously increasing, and thus the constraint we impose is equivalent to σL(l)=2dl−d2≥0\sigma^{L}(l)=2dl-d^{2}\geq 0, and with d≥0d\geq 0, we have d≤2ld\leq 2l. So we take d=max⁡((l+u)/2,2l)d=\max((l+u)/2,2l). And for l<0<ul<0<u, since σdL(0)=−d2\sigma_{d}^{L}(0)=-d^{2} is negative for d≠0d\neq 0 while d=0d=0 yields a valid lower bound, we take d=0d=0.

Square root

For the square root function, σ(x)=x\sigma(x)=\sqrt{x}. It is used the to compute a standard derivation in layer normalization and its input is limited to be non-negative by the lower bounds of x2x^{2}, and thus l≥0l\geq 0. σ(x)\sigma(x) is concave, and thus we take the line passing the two endpoints as the lower bound and take the tangent line passing ((l+u)/2,σ((l+u)/2))((l+u)/2,\sigma((l+u)/2)) as the upper bound.

Appendix C Linear Bounds of Multiplications and Divisions

We provide a mathematical proof of optimal parameters for linear bounds of multiplications used in Sec. 3.3. We also show that linear bounds of division can be indirectly obtained from bounds of multiplications and the reciprocal function.

For each multiplication, we aim to bound z=xyz=xy with two linear bounding planes zL=αLx+βLy+γLz^{L}=\alpha^{L}x+\beta^{L}y+\gamma^{L} and zU=αUx+βUy+γUz^{U}=\alpha^{U}x+\beta^{U}y+\gamma^{U}, where xx and yy are both variables and x∈[lx,ux],y∈[ly,uy]x\in[l_{x},u_{x}],y\in[l_{y},u_{y}] are concrete bounds of x,yx,y obtained from previous layers, such that:

Our goal is to determine optimal parameters of bounding planes, i.e., αL,βL,γL\alpha^{L},\beta^{L},\gamma^{L}, αU,βU,γU\alpha^{U},\beta^{U},\gamma^{U}, such that the bounds are as tight as possible.

We define a difference function FL(x,y)F^{L}(x,y) which is the difference between the original function z=xyz=xy and the lower bound zL=αLx+βLy+γLz^{L}=\alpha^{L}x+\beta^{L}y+\gamma^{L}:

To make the bound as tight as possible, we aim to minimize the integral of the difference function FL(x,y)F^{L}(x,y) on our concerned area (x,y)∈[lx,ux]×[ly,uy](x,y)\in[l_{x},u_{x}]\times[l_{y},u_{y}], which is equivalent to maximizing

while FL(x,y)≥0 (∀(x,y)∈[lx,ux]×[ly,uy])F^{L}(x,y)\geq 0\ (\forall(x,y)\in[l_{x},u_{x}]\times[l_{y},u_{y}]). For an optimal bounding plane, there must exist a point (x0,y0)∈[lx,ux]×[ly,uy](x_{0},y_{0})\in[l_{x},u_{x}]\times[l_{y},u_{y}] such that FL(x0,y0)=0F^{L}(x_{0},y_{0})=0 (otherwise we can validly increase γL\gamma^{L} to make VLV^{L} larger). To ensure that FL(x,y)≥0F^{L}(x,y)\geq 0 within the concerned area, we need to ensure that the minimum value of FL(x,y)F^{L}(x,y) is non-negative. We show that we only need to check cases when (x,y)(x,y) is any of (lx,ly),(lx,uy),(ux,ly),(ux,uy)(l_{x},l_{y}),(l_{x},u_{y}),(u_{x},l_{y}),(u_{x},u_{y}), i.e., points at the corner of the considered area. The partial derivatives of FLF^{L} are:

If there is (x1,y1)∈(lx,ux)×(ly,uy)(x_{1},y_{1})\in(l_{x},u_{x})\times(l_{y},u_{y}) such that FL(x1,y1)≤F(x,y) (∀(x,y)∈[lx,ux]×[ly,uy])F^{L}(x_{1},y_{1})\leq F(x,y)\ (\forall(x,y)\in[l_{x},u_{x}]\times[l_{y},u_{y}]), ∂FL∂x(x1,y1)=∂FL∂y(x1,y1)=0\frac{\partial F^{L}}{\partial x}(x_{1},y_{1})=\frac{\partial F^{L}}{\partial y}(x_{1},y_{1})=0 should hold true. Thereby ∂FL∂x(x,y),∂FL∂y(x,y)<0 (∀(x,y)∈[lx,x1)×[ly,y1))\frac{\partial F^{L}}{\partial x}(x,y),\frac{\partial F^{L}}{\partial y}(x,y)<0\ (\forall(x,y)\in[l_{x},x_{1})\times[l_{y},y_{1})), and thus FL(lx,ly)<FL(x1,y1)F^{L}(l_{x},l_{y})<F^{L}(x_{1},y_{1}) and (x1,y1)(x_{1},y_{1}) cannot be the point with the minimum value of FL(x,y)F^{L}(x,y). On the other hand, if there is (x1,y1)(x1=lx,y1∈(ly,uy))(x_{1},y_{1})(x_{1}=l_{x},y_{1}\in(l_{y},u_{y})), i.e., on one border of the concerned area but not on any corner, ∂FL∂y(x1,y1)=0\frac{\partial F^{L}}{\partial y}(x_{1},y_{1})=0 should hold true. Thereby, ∂FL∂y(x,y)=∂FL∂y(x1,y)=0 (∀(x,y),x=x1=lx)\frac{\partial F^{L}}{\partial y}(x,y)=\frac{\partial F^{L}}{\partial y}(x_{1},y)=0\ (\forall(x,y),x=x_{1}=l_{x}), and FL(x1,y1)=FL(x1,ly)=FL(lx,ly)F^{L}(x_{1},y_{1})=F^{L}(x_{1},l_{y})=F^{L}(l_{x},l_{y}). This property holds true for the other three borders of the concerned area. Therefore, other points within the concerned area cannot have smaller function value FL(x,y)F^{L}(x,y), so we only need to check the corners, and the constraints on FL(x,y)F^{L}(x,y) become

We substitute γL\gamma^{L} in Eq. (7) with Eq. (8), yielding

where V0=(ux−lx)(uy−ly)2V_{0}=\frac{(u_{x}-l_{x})(u_{y}-l_{y})}{2}.

We have shown that the minimum function value FL(x,y)F^{L}(x,y) within the concerned area cannot appear in (lx,ux)×(ly,uy)(l_{x},u_{x})\times(l_{y},u_{y}), i.e., it can only appear at the border. When (x0,y0)(x_{0},y_{0}) is a point with a minimum function value FL(x0,y0)=0F^{L}(x_{0},y_{0})=0, (x0,y0)(x_{0},y_{0}) can also only be chosen from the border of the concerned area. At least one of x0=lxx_{0}=l_{x} and x0=uxx_{0}=u_{x} holds true.

To maximize V1LV^{L}_{1}, since now only αL\alpha^{L} is unknown in V1LV^{L}_{1} and the coefficient of αL\alpha^{L} is V0(ux−lx)≥0V_{0}(u_{x}-l_{x})\geq 0, we take αL=ly\alpha^{L}=l_{y}, and then

For the other case if we take x0=uxx_{0}=u_{x}:

We take αL=uy\alpha^{L}=u_{y} similarly as in the case when x0=lxx_{0}=l_{x}, and then

We notice that V1L=V2LV^{L}_{1}=V^{L}_{2}, so we can simply adopt the first one. We also notice that V1L,V2LV^{L}_{1},V^{L}_{2} are independent of y0y_{0}, so we may take any y0y_{0} within [ly,uy][l_{y},u_{y}] such as y0=lyy_{0}=l_{y}. Thereby, we obtain the a group of optimal parameters of the lower bounding plane:

C.2 Upper Bound of Multiplications

We derive the upper bound similarly. We aim to minimize

where V0=(ux−lx)(uy−ly)2V_{0}=\frac{(u_{x}-l_{x})(u_{y}-l_{y})}{2}.

To minimize V1UV_{1}^{U}, we take αU=uy\alpha^{U}=u_{y}, and then

For the other case if we take x0=uxx_{0}=u_{x}:

To minimize V2UV^{U}_{2}, we take αU=ly\alpha^{U}=l_{y}, and then

Since V1U=V2UV^{U}_{1}=V^{U}_{2}, we simply adopt the first case. And V1U,V2UV^{U}_{1},V^{U}_{2} are independent of y0y_{0}, so we may take any y0y_{0} within [ly,uy][l_{y},u_{y}] such as y0=lyy_{0}=l_{y}. Thereby, we obtain a group of optimal parameters of the upper bounding plane:

C.3 Linear Bounds of Divisions

We have shown that closed-form linear bounds of multiplications can be derived. However, we find that directly bounding z=xyz=\frac{x}{y} is relatively more difficult. If we try to derive a lower bound zL=αLx+βLy+γLz^{L}=\alpha^{L}x+\beta^{L}y+\gamma^{L} for z=xyz=\frac{x}{y} as shown in Appendix C.1, the difference function is

It is possible that a minimum function value of FL(x,y)F^{L}(x,y) for (x,y)(x,y) within the concerned area appears at a point other than the corners. For example, for lx ⁣= ⁣0.05,ux ⁣= ⁣0.15,ly ⁣= ⁣0.05,uy ⁣= ⁣0.15l_{x}\!=\!0.05,u_{x}\!=\!0.15,l_{y}\!=\!0.05,u_{y}\!=\!0.15, α ⁣= ⁣10,β ⁣= ⁣−20,γ ⁣= ⁣2\alpha\!=\!10,\beta\!=\!-20,\gamma\!=\!2, the minimum function value of FL(x,y)F^{L}(x,y) for (x,y)∈[0.05,0.15]×[0.05,0.15](x,y)\in[0.05,0.15]\times[0.05,0.15] appears at (0.1,0.1)(0.1,0.1) which is not a corner of [0.05,0.15]×[0.05,0.15][0.05,0.15]\times[0.05,0.15]. This makes it more difficult to derive closed-form parameters such that the constraints on FL(x,y)F^{L}(x,y) are satisfied. Fortunately, we can bound z=xyz=\frac{x}{y} indirectly by utilizing the bounds of multiplications and reciprocal functions. We bound z=xyz=\frac{x}{y} by first bounding a unary function y‾=1y\overline{y}=\frac{1}{y} and then bounding the multiplication z=xy‾z=x\overline{y}.

Appendix D Tightness of Bounds by the Backward Process and Forward Process

We have discussed that combining the backward process with a forward process can reduce computational complexity, compared to the method with the backward process only. But we only use the forward process for self-attention layers and do not fully use the forward process for all sub-layers, because bounds by the forward process can be looser than those by the backward process. We compare the tightness of bounds by the forward process and the backward process respectively. To illustrate the difference, for simplicity, we consider a mm-layer feed-forward network Φ(0)=x, y(l)=W(l)Φ(l−1)(x)+b(l),Φ(l)(x)=σ(y(l)(x))(0<l≤m)\Phi^{(0)}={\mathbf{x}},\ {\mathbf{y}}^{(l)}={\mathbf{W}}^{(l)}\Phi^{(l-1)}({\mathbf{x}})+{\mathbf{b}}^{(l)},\Phi^{(l)}({\mathbf{x}})=\sigma({\mathbf{y}}^{(l)}({\mathbf{x}}))(0<l\leq m), where x{\mathbf{x}} is the input vector, W(l){\mathbf{W}}^{(l)} and b(l){\mathbf{b}}^{(l)} are the weight matrix and the bias vector for the ll-th layer respectively, y(l)(x){\mathbf{y}}^{(l)}({\mathbf{x}}) is the pre-activation vector of the ll-th layer, Φ(l)(x)\Phi^{(l)}({\mathbf{x}}) is the vector of neurons in the ll-th layer, and σ(⋅)\sigma(\cdot) is an activation function. Before taking global bounds, both the backward process and the forward process bound Φj(l)(x)\Phi^{(l)}_{j}({\mathbf{x}}) with linear functions of x{\mathbf{x}}. When taking global bounds as Eq. (3) and Eq. (4), only the norm of weight matrix is directly related to the ϵ\epsilon in binary search for certified lower bounds. Therefore, we try to measure the tightness of the computed bounds using the difference between weight matrices for lower bounds and upper bounds respectively. We show how it is computed for the forward process and the backward process respectively.

For the forward process, we bound each neuron Φj(l)(x)\Phi^{(l)}_{j}({\mathbf{x}}) with linear functions:

To measure the tightness of the bounds, we are interested in Ω(l),L{\bm{\Omega}}^{(l),L}, Ω(l),U{\bm{\Omega}}^{(l),U}, and also Ω(l),U−Ω(l),L{\bm{\Omega}}^{(l),U}-{\bm{\Omega}}^{(l),L}. Initially,

We can forward propagate the bounds of Φ(l−1)(x)\Phi^{(l-1)}({\mathbf{x}}) to y(l)(x){\mathbf{y}}^{(l)}({\mathbf{x}}):

With the global bounds of y(l)(x){\mathbf{y}}^{(l)}({\mathbf{x}}) that can be obtained with Eq. (3) and Eq. (4), we bound the activation function:

And then bounds can be propagated from Φ(l−1)(x)\Phi^{(l-1)}({\mathbf{x}}) to Φ(l)(x)\Phi^{(l)}({\mathbf{x}}):

illustrates how the tightness of the bounds is changed from earlier layers to later layers.

D.2 The Backward Process and Discussions

For the backward process, we bound the neurons in the ll-th layer with linear functions of neurons in a previous layer, the l′l^{\prime}-th layer:

We have shown in Sec. 3.1 how such bounds can be propagated to l′=0l^{\prime}=0, for the case when the input is sequential. For the nonsequential case we consider here, it can be regarded as a special case when the input length is 1. So we can adopt the method in Sec. 3.1 to propagate bounds for the feed-forward network we consider here. We are interested in Λ(l,l′),L{\bm{\Lambda}}^{(l,l^{\prime}),L}, Λ(l,l′),U{\bm{\Lambda}}^{(l,l^{\prime}),U} and also Λ(l,l′),U−Λ(l,l′),L{\bm{\Lambda}}^{(l,l^{\prime}),U}-{\bm{\Lambda}}^{(l,l^{\prime}),L}. Weight matrices of linear bounds before taking global bounds are Λ(l,0),L{\bm{\Lambda}}^{(l,0),L} and Λ(l,0),U{\bm{\Lambda}}^{(l,0),U} which are obtained by propagating the bounds starting from Λ(l,l),L=Λ(l,l),U=I{\bm{\Lambda}}^{(l,l),L}={\bm{\Lambda}}^{(l,l),U}={\mathbf{I}}. According to bound propagation described in Sec. 3.2,

illustrates how the tightness bounds can be measured during the backward bound propagation until l′=0l^{\prime}=0.

There is a W(l′){\mathbf{W}}^{(l^{\prime})} in Eq. (10) instead of ∣W(l′)∣|{\mathbf{W}}^{(l^{\prime})}| in Eq. (9). The norm of (Ωj,:(l),U−Ωj,:(l),L)({\bm{\Omega}}^{(l),U}_{j,:}-{\bm{\Omega}}^{(l),L}_{j,:}) in Eq. (9) can quickly grow large as ll increases during the forward propagation when ∥Wj(l)∥\|{\mathbf{W}}^{(l)}_{j}\| is greater than 1, while this generally holds true for neural networks to have ∥Wj(l)∥\|{\mathbf{W}}^{(l)}_{j}\| greater than 1 in feed-forward layers. While in Eq. (10), Wj(l′){\mathbf{W}}^{(l^{\prime})}_{j} can have both positive and negative elements and tends to allow cancellations for different Wj,i(l′){\mathbf{W}}^{(l^{\prime})}_{j,i}, and thus the norm of (Λ:,j(l,l′−1),U−Λ:,j(l,l′−1),L)({\bm{\Lambda}}^{(l,l^{\prime}-1),U}_{:,j}-{\bm{\Lambda}}^{(l,l^{\prime}-1),L}_{:,j}) tends to be smaller. Therefore, the bounds computed by the backward process tend to be tighter than those by the forward framework, which is consistent with our experiment results in Table 3.

Appendix E Impact of Modifying the Layer Normalization