No history yet

LLM Architecture

The Transformer Architecture

At the heart of nearly every modern Large Language Model (LLM) is an architecture called the Transformer. Think of it as the engine's blueprint. Introduced in 2017, the Transformer revolutionized how machines process language by allowing them to handle entire sequences of text at once, rather than one word at a time. This parallel processing is a key reason LLMs can be trained on such massive datasets.

Lesson image

The core innovation of the Transformer is its use of attention mechanisms. Before we get to that, we need to understand how the model first sees and processes the text we give it.

From Words to Numbers

Computers don't understand words; they understand numbers. The first step in processing language is to break text down into smaller pieces and convert them into a numerical format.

Tokenization

noun

The process of breaking down a piece of text into smaller units called tokens. Tokens can be words, parts of words (subwords), or individual characters.

For example, the sentence "LLMs are powerful" might be tokenized into ["LLMs", "are", "powerful"]. A more complex word like "unbelievably" could be broken into subwords like ["un", "believe", "ably"]. This allows the model to recognize parts of words and understand novel words it hasn't seen before.

After tokenization, each token is mapped to a unique integer. But a single number can't capture the rich meaning of a word. That's where embedding layers come in. An embedding layer converts each integer token into a high-dimensional vector, a list of numbers that represents the token's meaning in relation to other tokens.

Words with similar meanings are placed closer together in this "embedding space." This allows the model to capture semantic relationships. For example, the vector difference between "king" and "queen" might be very similar to the vector difference between "man" and "woman".

Understanding Context with Attention

Once we have our numerical vectors, we need a way to understand how they relate to each other in a sentence. The word "bank" means something very different in "river bank" versus "investment bank." This is where the attention mechanism comes in.

Attention

noun

A mechanism that allows a model to weigh the importance of different tokens in an input sequence when processing a specific token. It helps the model focus on relevant context.

Attention works by calculating a set of "attention scores" for every token in the sequence relative to every other token. A high score means two tokens are highly relevant to each other. For each token, the model creates three different vectors: a Query (Q), a Key (K), and a Value (V).

  1. Query: Represents the current token's question, asking, "Who is relevant to me?"
  2. Key: Represents another token's label or identity, saying, "This is what I am."
  3. Value: Represents the actual meaning or content of that other token.

The model compares the Query of the current token with the Key of every other token. A strong match results in a high attention score. This score then determines how much of each token's Value gets passed along to the next layer. In essence, the model learns which words to "pay attention" to when interpreting another word.

Lesson image

This process happens in parallel across multiple "attention heads," allowing the model to focus on different types of relationships simultaneously. For instance, one head might focus on grammatical relationships while another focuses on semantic ones.

Remembering Word Order

A key challenge with the attention mechanism is that it doesn't inherently understand the order of words. It sees the input as a bag of tokens. To the model, "The cat chased the dog" and "The dog chased the cat" would look identical. This is a problem because word order is critical to meaning.

The solution is positional encoding. Before the embeddings are fed into the Transformer's main layers, a vector representing the position of each token is added to its embedding vector. This gives the model a signal about the sequence's order.

PE(pos,2i)=sin(pos/100002i/dmodel)PE(pos,2i+1)=cos(pos/100002i/dmodel)\begin{aligned} PE_{(pos, 2i)} &= \sin(pos / 10000^{2i/d_{\text{model}}}) \\ PE_{(pos, 2i+1)} &= \cos(pos / 10000^{2i/d_{\text{model}}}) \end{aligned}

By combining token embeddings, positional encodings, and attention mechanisms, the Transformer architecture can build a rich, context-aware understanding of language. This powerful foundation is what enables LLMs to generate coherent text, answer questions, and perform a wide range of complex language tasks.

Quiz Questions 1/6

What was the primary architectural innovation of the Transformer model that allowed it to process text more efficiently than previous models?

Quiz Questions 2/6

In a Transformer, what is the role of the 'Key' (K) vector in the attention mechanism?