<p>First, you are correct in your complexity calculations. So, what is the source of confusion?</p>
<p>When the original <a href="https://arxiv.org/pdf/1409.0473.pdf" rel="noreferrer">Attention paper</a> was first introduced, it didn't require to calculate <code>Q</code>, <code>V</code> and <code>K</code> matrices, as the values were taken directly from the hidden states of the RNNs, and thus the complexity of Attention layer <strong>is</strong> <code>O(n^2·d)</code>.</p>
<p>Now, to understand what <code>Table 1</code> contains please keep in mind how most people scan papers: they read title, abstract, then look at figures and tables. Only then if the results were interesting, they read the paper more thoroughly. So, the main idea of the <code>Attention is all you need</code> paper was to replace the RNN layers completely with attention mechanism in seq2seq setting because RNNs were really slow to train. If you look at the <code>Table 1</code> in this context, you see that it compares RNN, CNN and Attention and highlights the motivation for the paper: using Attention should have been beneficial over RNNs and CNNs. It should have been advantageous in 3 aspects: constant amount of calculation steps, constant amount of operations <strong>and</strong> lower computational complexity for usual Google setting, where <code>n ~= 100</code> and <code>d ~= 1000</code>. But as any idea, it hit the hard wall of reality. And in reality in order for that great idea to work, they had to add positional encoding, reformulate the Attention and add multiple heads to it. The result is the Transformer architecture which while has the computational complexity of <code>O(n^2·d + n·d^2)</code> still is much faster then RNN (in a sense of wall clock time), and produces better results.</p>
<p>So the answer for your question is that attention layer the authors refer to in <code>Table 1</code> is strictly the attention mechanism. It is not the complexity of the Transformer. They are very well aware about the complexity of their model (I quote):</p>
<blockquote>
<p>Separable convolutions [6], however, decrease the complexity
considerably, to <code>O(k·n·d + n·d^2)</code>. Even with <code>k = n</code>, however, the
complexity of a separable convolution is equal to the combination of a
self-attention layer and a point-wise feed-forward layer, the approach
we take in our model.</p>
</blockquote>
Computational Complexity of Self-Attention in the Transformer Model
<p>I recently went through the <a href="https://papers.nips.cc/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf" rel="noreferrer">Transformer</a> paper from Google Research describing how self-attention layers could completely replace traditional RNN-based sequence encoding layers for machine translation. In Table 1 of the paper, the authors compare the computational complexities of different sequence encoding layers, and state (later on) that self-attention layers are faster than RNN layers when the sequence length <code>n</code> is smaller than the dimensionality of the vector representations <code>d</code>.</p>
<p>However, the self-attention layer seems to have an inferior complexity than claimed if my understanding of the computations is correct. Let <code>X</code> be the input to a self-attention layer. Then, <code>X</code> will have shape <code>(n, d)</code> since there are <code>n</code> word-vectors (corresponding to rows) each of dimension <code>d</code>. Computing the output of self-attention requires the following steps (consider single-headed self-attention for simplicity):</p>
<ol>
<li>Linearly transforming the rows of <code>X</code> to compute the query <code>Q</code>, key <code>K</code>, and value <code>V</code> matrices, each of which has shape <code>(n, d)</code>. This is accomplished by post-multiplying <code>X</code> with 3 learned matrices of shape <code>(d, d)</code>, amounting to a computational complexity of <code>O(n d^2)</code>.</li>
<li>Computing the layer output, specified in Equation 1 of the paper as <code>SoftMax(Q Kt / sqrt(d)) V</code>, where the softmax is computed over each row. Computing <code>Q Kt</code> has complexity <code>O(n^2 d)</code>, and post-multiplying the resultant with <code>V</code> has complexity <code>O(n^2 d)</code> as well.</li>
</ol>
<p>Therefore, the total complexity of the layer is <code>O(n^2 d + n d^2)</code>, which is worse than that of a traditional RNN layer. I obtained the same result for multi-headed attention too, on considering the appropriate intermediate representation dimensionalities (<code>dk</code>, <code>dv</code>) and finally multiplying by the number of heads <code>h</code>.</p>
<p>Why have the authors ignored the cost of computing the Query, Key, and Value matrices while reporting total computational complexity?</p>
<p>I understand that the proposed layer is fully parallelizable across the <code>n</code> positions, but I believe that Table 1 does not take this into account anyway.</p>