We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Tokenization & Embeddings step 5 of 15
Learned Absolute Position Embedding
A learned position embedding table stores one vector for each absolute position: row 0 represents position 0, row 1 represents position 1, and so on. A transformer can add these vectors to token embeddings to supply position information. During training, the table values can be updated by backpropagation.
This exercise implements only the lookup. The table is supplied; do not initialize or train it, or add the result to token embeddings.
Your task
Implement learned_position_embedding(positions, embedding_table).
positionshas shape(N, T): one position ID for each token in each batch row. The runtime delivers integer-valued floats, so cast them to an integer dtype before indexing. Every ID is in the range0 <= position < max_len.embedding_tablehas shape(max_len, d_model)and is shared by the whole batch.- Return shape
(N, T, d_model), preserving the table's floating-point dtype.
output[n, t, :] = embedding_table[positions[n, t], :]
Use the supplied IDs, even when they repeat, skip positions or appear in reverse
order. Do not replace them with 0, 1, ..., T-1. Repeated IDs select the same
vector, including when they occur in different batch rows.
For example, with positions=[[2, 0, 2]] and the table
[[1, 2], [3, 4], [5, 6]], return [[[5, 6], [1, 2], [5, 6]]].
What makes the positions absolute?
Each ID names a position directly, rather than a distance between two tokens.
The supplied table only has rows for positions 0 through max_len-1.
Looking up a longer sequence requires valid IDs within that range; this
function does not extend the table.
The illustration below starts on the Learned tab. Its values are synthetic, not trained weights or the table used by the tests.
Loading visualization…
Understand the ideas behind this exercise. The Problem tab contains the exact implementation requirements.
Give attention information about token order
Self-attention based only on token content has no absolute position input. Position information can be added to token vectors, added to attention scores, or introduced by transforming queries and keys.
Learned absolute embeddings provide a vector for each position. Sinusoidal encodings use sine and cosine pairs at different frequencies. When adding either to token embeddings, the feature widths must match. In a sinusoidal encoding, position determines the angle while the feature-pair index determines the frequency.
Describe the distance between query and key
Relative-position methods represent the relationship between two positions.
A learned score table, for example, can use their signed distance as an index.
For query position 3 and key position 1, key - query is -2, while
query - key is 2. Use the task's sign convention.
Clipping distances to a fixed range makes all more-distant pairs share an entry at the corresponding end of that range.
Rotate vectors or add a score bias
RoPE rotates pairs of query and key coordinates according to position. Rotations preserve vector lengths. Applying the same frequency scheme to queries and keys makes their dot product depend on the relative rotation between their positions. RoPE transforms the vectors rather than adding an embedding to them.
Some implementations pair adjacent features; others pair corresponding features from the two halves of a vector. Use the specified pairing, because it changes the result.
ALiBi adds a linear distance bias to attention scores, with a slope for each head. It affects preferences among keys without creating a position-embedding vector. Causal masking is still needed separately: a finite negative score bias does not give a future key exactly zero attention.
Check position zero, equal positions, and a small offset. With a cache during generation, use the new token's actual sequence position; restarting at zero on every step gives the wrong positional relationship to cached keys.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
PyTorch reference solution
Sign in to track your attempts toward unlocking the reference solution.