Unit 4 of 7 · about 3 hours · derive, implement

Positional information and position offsets

Self-attention sees sets, not sequences. Positional information is the only place order enters, so every offset bug becomes a meaning bug.

You'll be able to

  • Prove permutation equivariance of unpositioned self-attention and explain why order needs extra input.
  • Implement sinusoidal absolute encodings and predict any entry from the closed form.
  • Derive the relative-offset rotation and use it to explain shift generalization.

Covers

Positional information and position offsets
1

Read

In this order, only the parts listed

0 of 3 done

  1. Positional encodingUnderstanding Deep Learning - Transformers

    Section 12.3.1 (pp. 213 to 214): permutation equivariance, absolute encoding matrix, and relative per-offset parameters, with Figure 12.5

  2. Positional encoding implementation and theoryD2L - attention and transformer implementation route

    Positional Encoding: the sine-cosine formula, the PositionalEncoding class with max_len 1000 and dropout after the add, the encoding_dim 32 with num_steps 60 run plotting columns 6:10, the binary-frequency intuition, and the relative-offset rotation derivation

  3. Order-free self-attentionD2L - attention and transformer implementation route

    The paragraphs before Positional Encoding on parallel computation discarding sequential order

2

Focus on

  • Without positions the block is permutation-equivariant: equals . UDL Problem 12.3 asks for this proof; it is the reason order must be injected as input.
  • Absolute encodings add a unique column per position. The D2L TransformerEncoder scales embeddings by before the add because the encodings stay in .
  • Sinusoid pairs rotate with offset: position plus is a fixed rotation of position , depending on and frequency only. Shifting text shifts encodings linearly.
  • Relative schemes index by the offset between key and query positions, not by absolute index. They survive absolute shifts that absolute tables must relearn.
Common mistakes · 3
  • Not stating where positions enter. UDL lists three placements: once at the input, at every layer, or only in the query and key computation. The last leaves values free of position, which changes what later layers can read; write down which one your block uses.
  • Exceeding D2L max_len 1000. The stored table is fixed width, so longer inputs fail on the add; rebuild the table instead of truncating silently.
  • Comparing encodings or outputs in train mode. D2L applies dropout to the sum of embeddings and encodings, so train-mode runs drop entries at random; compare in eval mode or with dropout 0, as the D2L plot does.
3

Try it yourself

Closed-book, before checking anything

Closed-book, write and , sketch the equals proof, and verify the rotation numerically for one triple.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.State permutation equivariance for self-attention and explain why it forces positional input.Answer

    equals for any permutation matrix : reordering inputs reorders outputs identically. The block therefore carries no order information, so positions must enter as extra input.

  2. 2.Write the D2L sinusoidal encoding entries.Answer

    equals and equals , with the position and the width.

  3. 3.What does the rotation from position to position plus depend on?Answer

    Only the offset and the pair frequency . It never depends on the absolute position , which is why a fixed shift is a fixed linear map.

  4. 4.A 1500-token input meets D2L default PositionalEncoding. What happens?Answer

    The stored table is 1000 wide, so the slice caps at 1000 and the add fails on shape mismatch. Rebuild the encoding with a larger max_len.

5

Practice

  • TrenTorch: Sinusoidal positional encoding

    Implement sinusoidal_positional_encoding(seq_len, d_model) with sin on even columns and cos on odd columns

    Row 0 must read [0, 1, 0, 1, ...]. After the tests pass, assert the rotation identity numerically for one triple in your own repository.

  • UDL Problem 12.3: permutation equivariance

    Problems (p. 239): show equals for equation 12.8

    Pen-and-paper proof through the value, query and key maps and the column-wise softmax.

You're done when

Your encoding table matches the closed form entry by entry, the rotation check passes, and you can say which tensors carry position into the first attention call.

Only when you can do this without notes.

Leads to

Notes

Resources · 2 for this unit, 3 more in the section
  • Understanding Deep Learning - TransformersSimon J. D. Prince · primary

    Use Chapter 12.1-12.11 and the matching notebooks for dot-product self-attention, extension to transformers, transformer layers and vision transformers.

  • D2L - attention and transformer implementation routeD2L authors · required

    Use queries-keys-values, attention-scoring-functions, multihead-attention, self-attention-and-positional-encoding, transformer and vision-transformer. Implement masked single-head attention, multi-head attention and one pre-normalized block before comparing with PyTorch.

Also in this section

  • PyTorch - scaled dot product attentionPyTorch · reference

    Use the official function documentation as a differential oracle for shapes, masks, dropout and backend behavior after the manual version passes intervention tests.

  • Deep-ML candidate - attention problemsDeep-ML · optional

    Use 490 Scaled Dot-Product Attention as the fifth platform admission sample. If the judge catches a seeded masking error, use at most one of 490, 491, 53, 904 or 905 as a changed-prompt drill.

  • TrenTorch - attention and block questionsTrenTorch · support

    Solve the multi-head split, sinusoidal positional encoding, causal mask and pre-norm versus post-norm questions the units name, then carry the same checks into your own block.