Seq2Seq vs Transformer: How Attention Fixed the Bottleneck

Ushnah's profile picture
Ushnah AbbasiLast Updated on
11-12 Min Read TimeAdd as preferred on Google

Seq2seq models squeeze a whole input sentence into one fixed-length vector, which loses detail on long inputs. Transformers replace that bottleneck with attention over every input token, processed in parallel. Seq2seq is a task framing: an encoder reads an input sequence and a decoder writes an output sequence, originally with recurrent layers such as RNNs or LSTMs. The original transformer keeps that encoder-decoder layout and swaps the recurrent layers for self-attention.

 

Key Takeaways

  • Seq2seq is an encoder-decoder framing. In the RNN version, the decoder sees the input only through one fixed-length context vector, and that vector is the bottleneck.
  • Attention, first Bahdanau's additive form and then Luong's multiplicative form, lets the decoder weigh every encoder state at each output step.
  • The transformer keeps the encoder and decoder, drops recurrence and processes all tokens in parallel. Positional encoding supplies the word order.
  • The trade-off is cost. Self-attention compares every word with every other word, so its cost grows with the square of the input length.
  • BERT is encoder-only and GPT is decoder-only. T5 and BART keep both halves and are the transformer seq2seq models.

 

Seq2Seq vs Transformer Compared

The table below sets the four designs side by side. In the table, n is the sentence length and d is the size of each word vector; the RNN and transformer complexity figures come from Table 1 of Attention Is All You Need (2017).

AspectRNNSeq2seq (RNN encoder-decoder)Seq2seq with attentionTransformer
Core layersRecurrent layers (RNN, LSTM or GRU)A recurrent encoder and a recurrent decoderRecurrent encoder and decoder plus an attention layer between themSelf-attention and feedforward layers, with no recurrence
What each output step sees of the inputIts own hidden state, updated word by wordOne fixed-length context vector from the encoder's last stepA weighted sum of all encoder states, recomputed at every decoder stepAll encoder outputs, through attention in every decoder block
How word order is capturedReading orderReading orderReading orderPositional encoding added to each embedding
Sequential operations per layerO(n)O(n)O(n)O(1)
Longest path between two wordsO(n)O(n)O(n) inside the encoderO(1)
Computation per layerO(n·d²)O(n·d²)O(n·d²) plus attention over the encoder statesO(n²·d)
Typical tasksNext-word prediction, sentiment classificationTranslation, summarizationTranslation of longer sentencesBERT (encoder-only) for classification, GPT (decoder-only) for generation, T5 (encoder-decoder) for translation and summarization

The computation row shows the trade-off. Because self-attention compares every word with every other word, its cost grows with the square of the input length. The 2017 paper notes that self-attention layers are faster than recurrent layers when the sentence length is smaller than the vector size, which is why long documents stay expensive for transformers even though training runs in parallel.

 

Positional encoding follows from the same design choice. Without position vectors, self-attention has no sense of word order, and "the cat chases the mouse" would carry the same information for the model as "the mouse chases the cat".

 

The seq2seq task framing is still in use. T5 and BART map one sequence to another with transformer blocks where the recurrent layers used to be.

 

How NLP Moved from Rules to Transformers

NLP moved through three broad stages before the transformer: hand-written rules, statistical n-gram models that predict the next word from the previous few, and recurrent neural networks (RNNs and LSTMs) that carry context across a sentence but read it one token at a time. The seq2seq encoder-decoder described below is built from those recurrent layers. The 2017 paper Attention Is All You Need kept the encoder and the decoder, dropped recurrence and relied on attention alone. The authors report that the model was more parallelizable and needed significantly less time to train.

 

Sequence to Sequence Models

So, let's start with two questions: what is our input, and what output do we expect from it?

 

For NLP our input is mostly natural human language, text to be exact. 

 

We will be following this representation throughout this blog. 

 

 

Recurrent neural network unrolled over time, with each layer of neurons drawn as one box per time step

 

 

Here in the figure, input at each time (of sequence) is a vector, each layer has many neurons. We will represent the entire layer with many neurons as a box. 

 

We will be talking about sequence models here. A few types of sequence models are as follows:

1. One to one

This model takes one fixed-size input and returns one fixed-size output. An example is image classification.

2. One to many

This model uses only one input but outputs a sequence. Examples can be image captioning, which takes a fixed-size image as input and gives a sequence of words as output.

3. Many to one

This model takes a sequence as input and outputs one word. An example can be sentiment analysis, where a sentence or sequence of words can be classified as either positive, negative, or neutral. 

4. Many to many

The model uses a sequence as input and output a sequence as well. Examples can be machine translation, a sequence of words in French, and an output of a sequence but in English.

 

 

Sequence model types: one to one, one to many, many to one and two many-to-many layouts

 

 

Many NLP tasks can be categorized as seq2seq tasks like summarization, dialogue generation, code generation, language translation, etc. We will take the example of language translation to understand further. This task requires converting a sequence in the source language into the target language by keeping the semantic information intact, as in the example below.

 

 

Translation as a seq2seq task: the English sentence I play the flute mapped to its Urdu translation

 

 

Now let’s dive into a basic sequence-to-sequence model, which formed the base of the transformers.

 

 

Seq2seq encoder-decoder translating the German Ich gehe zum Hauptbahnhof into I am going to the central train station

 

 

In this figure, we can see that a sequence-to-sequence model is composed of two parts. The encoder and decoder. The input sentence is first tokenized and then converted into embeddings, which then go to the encoder, that is in the last hidden layer of the encoder. The whole crux of the input is encoded. This encoded information is given to the decoder, which is responsible for generating the next word (and hence an output sequence). 

 

As seen in the figure, we also give the generated output to the next layer as input. During training, we also give the ground truth at each timestamp to guide the model towards correct generation. This is called teacher forcing. At each timestamp, a word is predicted. 

 

Argmax is used to predict the most probable word at each timestamp given the previously generated output. This is called the greedy approach. A better approach is to apply beam search. At each timestamp, a track of the most likely words is kept, and the one with the highest score for the generated sequence is kept. It determines the size of the search and is a hyperparameter. 

 

Shortcomings of Seq2Seq Models

The issue with this seq2seq model is that all the information from the start of the sentence till the end is encoded in one layer, thus it is overloaded. This becomes a bottleneck. Apart from that each hidden layer is dependent on the previous one. To cater to these problems, and make the model more parallel rather than sequential, attention was introduced which is the main focus of transformer models. 

 

 

Seq2seq bottleneck: the whole input sentence is encoded in one vector, so information from the start of the sentence may be lost

 

 

All about Attention

To understand it better, let's look at this figure below. 

 

 

Attention in a seq2seq decoder: attention scores over the encoder states, the softmax attention distribution and the attention output

 

 

The figure shows the main idea of attention. Information bottlenecks in seq2seq models can be removed using attention. At each decoder step we use the dot product of decoder activation and encoder activations to get attention scores. Then softmax of attention scores is taken to turn these values into a probability distribution. This attention distribution is used to calculate the weighted sum of encoder activations. Attention output is used to influence the generation of words at each time step. The attention output is concatenated with decoder activations to calculate the output. 

 

This mechanism has two classic forms. Bahdanau, Cho and Bengio (2014) introduced attention for translation after arguing that the single fixed-length vector was a bottleneck of the basic encoder-decoder. Their scores come from a small feedforward network, a form known as additive attention. Luong, Pham and Manning (2015) used simpler multiplicative scores, such as the dot product described above. They also compared global attention over every source word with local attention over a small window. The transformer's scaled dot-product attention builds on the multiplicative form.

 

Attention actually allows the decoder to focus on relevant parts in the source input sentence. It helps in resolving the bottleneck issue by removing the reliance on a single vector to capture the whole input sequence. Now the decoder has attention for guidance. It paves the way for a direct connection between the decoder and encoder which helps especially for longer sentences. It also allows us to have interpretability power, by analyzing the attention output we can understand what the decoder was focusing on while predicting a certain target output.  

 

Transformer Model

Transformer architecture: encoder and decoder stacks with multi-head attention, feed forward, add and norm layers and positional encoding

The transformer keeps the seq2seq encoder-decoder layout shown above. What changed is inside each block.

 

Self-attention replaces recurrence. Each encoder layer compares every word with every other word in the sentence at once, so no step waits for the one before it and training runs in parallel. The encoder reads the whole input in both directions and outputs contextual embeddings: one vector per word that reflects the words around it.

 

The decoder no longer depends on a single context vector. Each decoder block attends to all encoder outputs. The encoder supplies the key (K) and value (V) matrices, and the decoder supplies the query (Q). Masked self-attention stops the decoder from seeing words it has not generated yet.

 

Several attention heads run side by side, each tracking a different relation between words. The original transformer paper used 8 attention heads. A residual connection around each sub-layer is followed by layer normalization. Positional encoding, computed with sine and cosine functions, adds the word order that recurrence used to supply.

 

For a block-by-block walkthrough, see our guide to transformer architecture components.

 

Impact on NLP

The transformer model was a breakthrough in NLP. Encoders can be used as standalone models as they are very good at learning meaningful representations. As they are capable of understanding the text, they are used for tasks like sentiment analysis, named entity recognition, sentence classification, providing embeddings, etc. Common examples are BERT and its variants like RoBERTa and ALBERT. 

 

Similarly, decoders can also be used as standalone models. They are great at natural language generation tasks. Common examples are GPT, GPT-2, etc. Common encoder-decoder models are T5, BART, M2M100, Pegasus, etc. 

 

Transformers have changed natural language processing by making models faster to train and easier to scale. They were introduced in the research paper Attention Is All You Need (2017). They replaced recurrent models (LSTMs, GRUs) with self-attention mechanisms, resulting in strong performance in tasks such as translation, summarization, and question-answering. Unlike standard word vectors, several models like BERT, GPT, and T5 use contextual embeddings, and that enables a subtle grasp of language. A key improvement within them is their capacity to process full sequences in parallel.

 

Large language model families such as GPT, Claude and Gemini now power many AI-driven applications, such as chatbots, code generation, and virtual assistants. Also, transformers have made models such as T5, BART, and Pegasus better at creating abstractive summaries.

 

Building translation, summarization or document search on transformer models? See how our natural language processing services team approaches those projects.

 

Frequently Asked Questions About Seq2Seq and Transformers

Is a transformer a seq2seq model?

The original transformer is a seq2seq model. It was built for translation, with an encoder that reads the source sentence and a decoder that writes the target sentence, the same layout as an RNN encoder-decoder. What changed is inside the blocks: self-attention and feedforward layers replace recurrence. Encoder-only and decoder-only transformers are not seq2seq models.

What is the bottleneck in seq2seq models?

In an RNN seq2seq model, the encoder squeezes the whole input sentence into one fixed-length vector, and the decoder sees only that vector. Long sentences lose information from their start. Bahdanau, Cho and Bengio pointed to this fixed-length vector as a bottleneck in 2014 and proposed attention, which lets the decoder look back at every encoder state.

What is the difference between Bahdanau and Luong attention?

Bahdanau attention (2014) scores each encoder state with a small feedforward network, a form known as additive attention. Luong attention (2015) uses simpler multiplicative scores, such as a dot product between decoder and encoder states, and compares global attention over all source words with local attention over a window. Transformers use scaled dot-product attention.

Are BERT and GPT seq2seq models?

No. BERT uses only the encoder, so it reads text and produces representations for tasks such as classification. GPT models use only the decoder and generate text one token at a time, the design behind many large language models. Models that keep both halves, such as T5 and BART, are the transformer seq2seq models used for translation and summarization.

Why do transformers need positional encoding?

Self-attention compares every word with every other word at once, so on its own it has no sense of word order. Positional encoding adds a vector to each word embedding that marks its position. The original paper computes it with sine and cosine functions. Without it, 'the cat chases the mouse' and 'the mouse chases the cat' look the same.

...Loading Related Blogs

Explore More

From Introduction to Proposal in Days

Discovery Call
Our sales team reviews your message and asks for a discovery call to gather more information.
Expert Input
Our veterans go through your requirements to provide their take, backed by decades of experience.
Proposal
We provide a proposal specific to what you're building, for you to review at your own pace.

Trusted by top platforms for our transformative solutions and exceptional results:

How Can We Help You Build?

We recommend using your work email.
We'll send a mutual NDA before the discovery call if requested. Zero obligation.