At what point in the forward pass are positional embeddings combined with token representations?
Last updated
It depends on the scheme. Absolute encodings are added once, right after the token embedding lookup and before the first Transformer block. Relative schemes inject position inside every attention layer instead.
Absolute (sinusoidal or learned):
Look up token embeddings.
In the original Transformer, scale them by
sqrt(d_model).Add the position embeddings element-wise (summed, not concatenated, so the dimension stays
d_model).Apply dropout, then feed the first block. BERT also adds segment embeddings and applies LayerNorm here.
After that, position lives only implicitly in the residual stream: Deeper layers must preserve it themselves.
RoPE:
Applied in every attention layer, after the
W_qandW_kprojections and beforeQK^T.Nothing is added at the input.
ALiBi and T5 bias:
Added to the attention logits after
QK^T / sqrt(d)and before softmax.Applied in every layer for ALiBi. T5 computes its bias in the first layer of each stack and shares it across all layers.