Concepts / The GPT Model Architecture

The GPT Model Architecture

GPT_CONFIG_124M is a dictionary of settings that determines the shape of the GPT model, and the chapter changes only context_length, from 1,024 to 256 tokens.

  • Programming

Why the Model Needs a Configuration

Before any text can be generated, the model has to exist and be placed in a known state. This chapter begins with a listing that builds that starting point. Its key feature is that it does not define the model's internals. It passes one dictionary to the GPTModel class and receives an object back. The same class can produce a small or a large model depending only on the numbers stored in that dictionary.

The dictionary is called GPT_CONFIG_124M, and it is the single place where the model's shape is recorded. Each key names one setting, and each value fixes that setting for the model that gets built. As you read the dictionary, ask two questions: which lines decide how large the model is, and how much text it can look at in one pass. You will return to those lines when you trace the generation process.

Reading the Configuration Dictionary

The dictionary holds seven keys. Only one of them, context_length, is changed from the previous chapter. The other six keep the settings used in chapter 4, so the model is the same architecture with a shorter window of text. Among the settings this article names directly, n_layers controls how many transformer blocks are stacked, and context_length controls how many tokens the model can look at in one pass.

What do you think happens?

If you read the dictionary and then ask for its context_length value, what number do you expect?

  • 1,024

  • 256

  • 50,257

  • 12

Reveal answer
Answer:

256

The chapter shortens the context window from 1,024 tokens to 256 tokens. The other option, 50,257, is the vocabulary size, and 12 is the n_layers value for the 124-million-parameter model, not the context length.

python
Output (expected)
256
Notes on the code
  • The dictionary is the single place where the shape of the model is recorded.
  • Ordinary key-based access reads one setting back out of the dictionary.
  • n_layers is the key that sets how many times the transformer block is repeated.

Why Context Length Dropped to 256

The context length defines how much text the model sees in a single pass. The chapter moves it from 1,024 tokens to 256 tokens while keeping the rest of the architecture the same. The source's stated reason is practical: shortening the context reduces the computational demands of training. That reduction is what makes training feasible on a standard laptop computer.

The Three Stages of Text Generation

Text generation with a GPT model is a cycle that starts with text and ends with text. It moves through three stages. In the first stage, the tokenizer converts input text into a series of token IDs. In the second stage, the model receives those token IDs and produces logits. A logit vector represents the probability distribution over every token in the vocabulary. In the third stage, those logits are converted back into token IDs, and the tokenizer decodes those IDs into human-readable text.

Input texthuman-readableToken IDsnumeric formLogitsscores over vocabularyToken IDschosen from logitsGenerated textdecoded outputtokenizer encodesmodel processesconverted back to IDstokenizer decodes
The learner question: at each stage, what form does the data take, and where does text become token IDs and then return to text?

The stages are easy to confuse because two of them produce numbers. The model's output is not text, and it is not a token ID either. It is a vector of scores over the vocabulary. Only after that vector is turned back into a token ID can the tokenizer produce a word or a piece of a word.

Helper Functions at the Numeric Boundary

The chapter adopts the generate_text_simple function from chapter 4 and adds two helper functions, text_to_token_ids and token_ids_to_text. Together they form the boundary between the human side and the numeric side of the cycle. The helper names describe their direction. text_to_token_ids moves from text toward the numeric side. token_ids_to_text moves from numbers back toward text. Keeping that direction in mind makes it easier to decide which helper belongs at a given point in the cycle.

Before
text = "Every effort moves you"
# still human-readable text

Text, on the human side of the cycle

After
ids = text_to_token_ids(text, tokenizer)
# now a sequence of token IDs for the model

Token IDs, on the numeric side of the cycle

Locating an Unexpected Result

When a generated result looks wrong, the most useful question is which stage produced the unexpected value. Check the stages in order: the token IDs after encoding, the logits from the model, and then the text after decoding. Each stage boundary is a place where a problem can hide, so checking in order tells you where to start debugging.

  1. 1
    Inspect the token IDs produced by text_to_token_ids. If they are wrong, the problem is in encoding, before the model runs.
  2. 2
    If the token IDs are correct, inspect the logits from the model. If the scores are wrong here, the problem is in model processing.
  3. 3
    If the logits are sensible, inspect the text after token_ids_to_text. If only the text is wrong, the problem is in decoding.

Assembling the Architecture

Earlier, a stand-in version of a GPT model showed how data enters and leaves the model, with its inner parts left as placeholders. This section replaces those placeholders with the real building blocks, the TransformerBlock class and the LayerNorm class. Once they are in place, the model can be assembled into a fully working version of the 124-million-parameter GPT-2 model. Read the architecture from input to output and it becomes a pipeline. Tokenized text is first turned into token embeddings. Positional embeddings are then added, so the model knows where each token sits in the sequence. The combined tensor enters the stack of transformer blocks. After the last block, a final layer normalization step adjusts the result, and a linear output layer produces the scores used to predict the next token.

GPT modelfull architectureToken and positionalembeddingsprepare the inputRepeated transformerblocksstacked, same designFinal layernormalizationprepares the outputLinear output layerproduces scorescontainscontainscontainscontains
The learner question: what contains what, and in what order do the repeated transformer blocks, the normalization step, and the output layer sit within the full model?

Setting the Number of Blocks

The number of repetitions is not written into the block itself. It is controlled by a configuration entry called n_layers, which lives in the GPT_CONFIG_124M dictionary. For the 124-million-parameter GPT-2 model, that entry is set so the transformer block is repeated 12 times. The largest GPT-2 model uses 48. Changing the configuration value changes the depth of the model without changing the code of the block. Only the middle section of the pipeline is repeated, which is why the number of blocks is the main lever for changing the model's depth.

GPT-2 124Mn_layers = 12Largest GPT-2n_layers = 48same block code, different n_layers
The learner question: how does the block count change between GPT-2 sizes, and what stays the same?

From Hidden Vectors to Vocabulary Scores

The final answer the model produces for each position is a set of values that spans every token the model knows. To see how that set is formed, follow the data from the last transformer block. Its output is first adjusted by the final layer normalization step, which prepares the representation. The linear output layer then maps that representation into a space with one dimension per vocabulary entry, 50,257 in the 124-million-parameter case. Each dimension is a score for one token. The next token is chosen from that set of scores.

Final transformerblock outputhidden-state tensorFinal layernormalizationprepares representationLinear output layermaps to vocabulary spaceScores over 50,257tokensone score per tokenNext token chosenselected from the scoresfeedsfeedsproduceschoose from
The learner question: how does a hidden-state vector move through layer normalization and the linear output layer to become one score per vocabulary entry?

Mistakes When Reading the Architecture

  • Common mistake

    Treating the model's output as text or as a token ID

    Why it fails

    The model's output is a vector of scores over the vocabulary. Only after that vector is turned back into a token ID can the tokenizer produce a word or part of a word.

    Do this instead

    Label the output of the model as logits and keep it separate from the token IDs and text on either side of it.

  • Common mistake

    Changing the transformer block's code to make the model deeper

    Why it fails

    The repetition count is set by the n_layers entry in the configuration dictionary. Changing that value changes the depth without changing the block's code.

    Do this instead

    Change n_layers in GPT_CONFIG_124M to change the number of stacked blocks.

  • Common mistake

    Assuming the context length change alters the architecture

    Why it fails

    Only context_length changes from the previous chapter. The model is the same architecture with a shorter window of text.

    Do this instead

    Read the reduction from 1,024 to 256 tokens as a change in how much text is seen per pass, which lowers training demands.

  • Common mistake

    Mixing up text_to_token_ids and token_ids_to_text, which move in opposite directions across the numeric boundary

Tracing the Pipeline in Practice

EASY

A generated sentence comes out garbled, but the token IDs printed right after encoding look correct. The logits look sensible too. At which stage should you look next, and why?

Hints
  • Check the stages in order: token IDs after encoding, logits from the model, then text after decoding.
  • Since the token IDs and logits are both fine, the boundary still to check is the last one.
MEDIUM

Explain, in your own words, how the final transformer block's output becomes one score for each of the 50,257 vocabulary entries.

Hints
  • Name the two components that come after the last block, in order.
  • Think about what the linear output layer maps the representation into.

Key Takeaways

  • GPT_CONFIG_124M is the single place where the model's shape is recorded, and the chapter changes only context_length, from 1,024 to 256 tokens, to reduce training demands.
  • Text generation runs in three stages: the tokenizer encodes text into token IDs, the model turns token IDs into logits, and the logits are converted back into token IDs and decoded into text.
  • The GPT architecture is a stack of repeated transformer blocks, followed by a final layer normalization step and a linear output layer that maps to the 50,257-token vocabulary.
  • The n_layers entry sets how many times the transformer block repeats, with 12 for GPT-2 124M and 48 for the largest GPT-2 model, without changing the block's code.

Related concepts

More concepts starting with T