Your understanding of the transformer encoder is quite good. Let's clarify some points and then explain the decoder.
In the encoder, the input embedding matrix x_i is typically of size n x d, where n is the sequence length (number of tokens in the sentence after tokenization) and d is the embedding dimension.
When x is multiplied by matrices Q (query), K (key), and V (value), the resulting matrices q, k, and v have the same shape as x, so they are also n x d. However, before attention heads are computed, these matrices are usually projected down to a smaller dimension d' = d / h where h is the number of attention heads. So if you have h attention heads, q, k, and v in each head will be of size n x d'.
The attention computation outputs a weighted sum of the values (from the v matrix) for each position in the sequence. These outputs are concatenated (bringing the dimension back to n x d) and then run through a linear transformation to ensure the output has the same dimension as the input embeddings. The residual connection and normalization then produce x_{i+1}, which is passed through the multilayer perceptron to obtain x_{i+2}.
Now, let's talk about the transformer decoder:
-
Similar to the encoder, the decoder takes a sequence, typically the target sequence during training or the generated sequence during inference, and converts it into an embedding matrix. Let's call this matrix y_i with size m x d, where m is the length of the target sequence.
-
The decoder also has self-attention layers, but with a modification: masking. Masking ensures that the position being predicted can only attend to earlier positions in the sequence. This is called causal or autoregressive masking, which prevents the model from seeing "future" tokens.
-
The query (q), key (k), and value (v) matrices in the decoder self-attention layers are derived from y_i and have the same shapes as in the encoder before they are projected into multiple heads (i.e., m x d).
-
After the self-attention heads in the decoder do their computation (with masking), the outputs are concatenated and linearly transformed, followed by a residual connection and normalization to produce y_{i+1}.
-
The next step is the cross-attention layer. The queries (q) come from the output of the decoder's masked self-attention layer (y_{i+1}), but the keys (k) and values (v) come from the output of the encoder (x_{i+2}). This allows the decoder to focus on different parts of the input sentence when producing each token in the output sequence.
-
After the cross-attention heads do their computation, the outputs are concatenated, linearly transformed, and pass through a residual connection and normalization, similar to the self-attention layers, to produce y_{i+2}.
-
y_{i+2} is finally passed through the decoder's multilayer perceptron, and after another residual connection and normalization, you get y_{i+3}.
-
The decoder stack typically consists of several layers of the above operations. The output of the last layer of the decoder is usually passed through a final linear layer and a softmax to produce the probabilities for each token in the vocabulary.
The entire process is designed to generate a sequence that corresponds to the input sequence semantically, with the encoder providing context and the decoder generating the output one token at a time, conditioned on the input and what has been generated so far.