Big O of Transformers

username: maxrivera
cooked: <p>What is the Big O runtime of a transformer model during inference? How about during training? Seems to me that it is still O(n), since it can only generate each word of the output one at a time.</p>

username: JuanFMontesinos
cooked: <p>Hi,<br>
it depends on whther you use autoregressive predictions or not.<br>
If that’s the case, it’s O(n2), otherwise it’s O(n). There are some other types of transformer with different times.</p>

username: maxrivera
cooked: <p>Transformers are supposed to do in parallel at least part of what RNN seq2seq models can’t. So what would you say the Big O is for an RNN seq2seq?</p>

username: maxrivera
cooked: <p>By the way, I thought an autoregressive transformer would be O(n), where n = length of the target sequence. Could you explain why it would be O(n2)?</p>

username: JuanFMontesinos
cooked: <p>Transformers are fully parallelized as they run matrix multiplications + linear layers. As linear layers process each input independently to the others, transformers reach a high parallelization. On contrary, RNNs requires the output of the previous iteration which leads to a bottleneck.</p>
<p>The transformer itself (but the newer/optimized versions in research) are O(n) where n is the lenght of the sequence. It’s as simple as for each element in the input we need a single run to obtain the corresponding output.<br>
The autoregressive versions run iteratively. For example, in translation (which is a seq2seq task where the length of the output is unknown).<br>
You process the context (known) and obtain features with an encoder.<br>
Then you need to obtain the translated version.</p>
<p>Algorithm<br>
Run the transformer once with the start token. Obtains the first prediction.<br>
Feed the start token + first pred–&gt; 2nd pred<br>
feed start, 1st,2nd and so on</p>
<p>In short, each time you run the transformer you are predictin the next (and only one) element of the sequence. Therefore, if your tgt sentence is of lenght n, you have to run the decoder  n + n-1 + n-2 —&gt; ~ O(n^2)</p>

username: maxrivera
cooked: <p>Thanks for the reply! What I’m confused about is - if the transformer only has an encoder, then everything is done in parallel, so no matter the size of the input, the runtime is constant? That sounds like O(1) to me</p>

username: JuanFMontesinos
cooked: <p>Soo I guess it depends on the base unit we consider here <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<p>I’m talking taking as unit the transformer processing a single element of the sequence.<br>
In that case the “encoder” is O(n) and an autoregressive would be O(n^2).</p>
<p>If you consider the whole transformer as unit (no matter the sequence length)<br>
Then the encoder is O(1) and the autoregressive is O(n)</p>
<p>Note that in practice, the sequence length may be a bottleneck. If your gpu has enough capability to run the whole seq at once no matter the seq length, we are in the second case. If your gpu is not powerful enough, it would make more sense to choose the former perspective.</p>
<p>EDIT: Another way of seeing it is <a href="https://www.reddit.com/r/LanguageTechnology/comments/9gulm9/complexity_of_transformer_attention_network/" rel="noopener nofollow ugc">https://www.reddit.com/r/LanguageTechnology/comments/9gulm9/complexity_of_transformer_attention_network/</a><br>
Taking into account the multi-head self attention complexity.</p>

username: maxrivera
cooked: <p>Thanks for the detailed answer! I never thought about the GPU capability influencing the runtime or complexity, but it does make sense</p>

username: JuanFMontesinos
cooked: <p>Well, being strictly precise, it depends on the sequence length as stated above.<br>
Taking into account that, “all the ops” run in parallel in the GPU, i’d say the limitation depends on the capability. Note that it’s not my area of expertise. Don’t take my words as carved on the stone!</p>