liveBook · Manning

Order property

As explained in Build a Large Language Model (From Scratch)

The location of a token or element within a sequence, used here as the basis for relating elements in self-attention.

Positional information identifies where each token occurs in a sequence. Because self-attention and token embeddings are position-independent, adding it helps the model represent order and token relationships. section 2.8

Use two broad positional-embedding strategies:

  • Absolute positional embeddings assign a distinct learned vector to each sequence position and add it to the token embedding. section 2.8
  • Relative positional embeddings represent distances between tokens, supporting generalization to sequence lengths not seen during training. section 2.8

Combining token and positional embeddings

For inputs shaped [8, 4], a token embedding layer produces [8, 4, 256]. A positional embedding layer produces [4, 256]; broadcasting and addition yield [8, 4, 256] input embeddings for the LLM layers. section 2.8

Figure 2.18 Positional embeddings are added to the token embedding vector to create the input embeddings for an LLM. The positional vectors have the same dimension as the original token embeddings. The token embeddings are shown with value 1 for simplicity.
figure

Visualizes positional vectors matching token-embedding dimensions and being added to form input embeddings. section 2.8

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Explains absolute and relative strategies and demonstrates their tensor-shape implementation, without deriving attention internals.
Examples
Demonstrates PyTorch embeddings on batches of eight four-token sequences with 256-dimensional token and position vectors.
What it assumes
Assumes tokenization and token IDs, while demonstrating token-embedding lookup before adding positional information.
What you can do after
Apply positional embeddings in PyTorch by creating position indices, looking up vectors, and adding them to token embeddings.
How it connects
Connects token embeddings, self-attention, absolute positional embeddings, relative positional embeddings, and input embeddings.

Also explained in

Compare how 8 books explain Order property →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.