Concepts / Text Generation in GPT Models

Text Generation in GPT Models

A transformer block is a core structural component of GPT models that combines masked multi-head attention modules with fully connected feed forward networks using the GELU activation function.

  • Programming

A Repeated Unit Behind Every GPT

A GPT model is a large language model, and its internal design is built from a repeated unit called the transformer block. When people say a GPT model has many layers, they are pointing at this repetition. Before looking inside a single block, it helps to ask a concrete question: if a model is made of one repeated unit, what does that unit actually do to the data passing through it?

This article answers that question in stages. First you will look at what one transformer block contains. Then you will see how many of these blocks are stacked to form a GPT model, how text is produced from the model's output tensors, and why training is needed before the output becomes coherent.

What do you think happens?

Does a GPT model with a correct structure but no training produce coherent text?

  • Yes, the structure alone is enough

  • No, it produces incoherent text

Reveal answer
Answer:

No, it produces incoherent text

The architecture defines how data flows through the model, but without training a GPT model generates incoherent text. Training is what makes the generated text coherent.

Inside One Transformer Block

Definition

A transformer block is a core structural component of GPT models that combines masked multi-head attention modules with fully connected feed-forward networks using the GELU activation function.

The definition names two parts. Data passes through these parts in order within the block. Think of the block as a pipeline with two stations: the input tensor arrives at the attention station first, and its output then moves on to the feed-forward station, which produces the output tensor of the block.

Input tensorMasked multi-headattentionFeed-forward networkwith GELUOutput tensorentersthenproduces
In what order does the input tensor pass through the sub-modules inside one transformer block?

The diagram traces one block from input tensor to output tensor. The attention module is the first sub-module the data meets, and the feed-forward network that uses GELU is the second. Because the order is fixed, each block always transforms its input the same way: attention first, then the feed-forward step.

Stacking Blocks into a GPT Model

A GPT model is an LLM with many repeated transformer blocks. Each block has the same two-part structure described above, and the model is formed by repeating that structure many times. The source describes these blocks as having millions to billions of parameters, which means the parameters of a GPT model are spread across this repeated structure rather than living in one large monolithic unit.

Input tokenembeddingsTransformer block 1Transformer block 2Transformer block NOutput tensorsinputoutput becomes inputoutput becomes inputfinal representations
How does the output of each transformer block become the input of the next, and how does the stack turn input token embeddings into final representations?

The pipeline shows the overall flow. Input context enters the repeated transformer blocks, the blocks produce output tensors, and those tensors are decoded into text. The repeated blocks sit in the middle of the pipeline, so their outputs are what the decoding step works from. Depth in a GPT model therefore means more repetitions of the same unit, not a different design at each level.

GPT models also come in various sizes, measured by their number of parameters. The source gives four example sizes, and it notes that all of them can be implemented with the same GPTModel Python class. The size of a model is therefore set by configuration rather than by writing a different class for each size.

Model size (parameters)Implementation
124 millionGPTModel Python class
345 millionGPTModel Python class
762 millionGPTModel Python class
1,542 millionGPTModel Python class

Four GPT sizes named in the source, all implemented with one class

From Output Tensors to Words

Text generation is the step that turns numbers into language. The source describes the capability as decoding output tensors into human-readable text by sequentially predicting one token at a time from a given input context. Output tensors are numeric, so generation is what converts those numbers into words a person can read.

The key phrase is one token at a time. The model does not produce a whole sentence in a single step. It predicts one token from the input context, that token is decoded, and the process then repeats. The process is sequential, and each step depends on the context that the previous steps built up.

Input contextPredict next tokenAppend token tocontextDecoded textgiven to modeldecode and addextended context fed back ineach token
How does the model append one predicted token, then feed the extended context back in to predict the next token?
Worked example

Tracing Three Generation Steps (Illustrative)

A model is given the input context 'the cat'. Trace how it generates the next three tokens, one at a time. The words below are invented purely to illustrate the loop; they are not real model outputs.

  1. 1
    Step 1

    The model reads the input context 'the cat' and predicts one token from it. Suppose the decoded token is 'sat'.

  2. 2
    Step 2

    The predicted token is appended, so the extended context is 'the cat sat'. This longer context is fed back into the model, which predicts the next token, say 'on'.

  3. 3
    Step 3

    The context is now 'the cat sat on', and the model predicts another token, say 'the'.

  4. 4
    Step 4

    Each prediction depends on everything appended so far. The text is built one token at a time, never as a whole sentence in a single step.

Result

The loop extends the context token by token: 'the cat' becomes 'the cat sat on the' after three predictions.

Notice what the loop does with its own output. Each decoded token becomes part of the context for the next prediction. This is why the sequential nature matters: the model works forward one step at a time, and the text it produces is always the accumulated result of those steps.

Why Training Changes the Output

Having the right structure is not enough for useful output. The source states that without training, a GPT model generates incoherent text, which underscores the importance of model training for coherent text generation. The architecture defines how data flows through the blocks, and the stack of blocks produces output tensors, but training is what makes the generated text coherent.

Untrained GPTgenerates incoherent textTrained GPTgenerates coherent texttraining
How does the generated text differ between an untrained model and a trained model?

Mistakes Beginners Make

  • Common mistake

    Thinking a GPT model writes a whole sentence in one step

    Why it fails

    The source frames text generation as sequentially predicting one token at a time from an input context.

    Do this instead

    Think of generation as a loop in which each predicted token is appended to the context before the next prediction.

  • Common mistake

    Treating depth as a different design at each layer

    Why it fails

    A GPT model is formed by repeating the same two-part transformer block many times.

    Do this instead

    Read the model as one repeated unit stacked many times, with the parameters spread across that repetition.

  • Common mistake

    Assuming each model size needs its own class

    Why it fails

    The source notes that all four example sizes can be implemented with the same GPTModel Python class.

    Do this instead

    Size is set by configuration, not by a separate class per size.

  • Common mistake

    Expecting coherent text from an untrained model

    Why it fails

    Without training, a GPT model generates incoherent text.

    Do this instead

    Treat training as the step that makes generated text coherent, separate from the architecture.

Practice: Tracing the Generation Loop

MEDIUM

Using the loop from the text generation section, start with the input context 'a model' and describe what happens during the first two generation steps. For each step, say what the model predicts from, what is appended, and what context the next step receives. Then explain why the output of the second step depends on the output of the first.

Hints
  • Each step starts from the current context, not from the original input alone.
  • Appending a token changes the context that the next prediction uses.
  • The output of step two is built on top of the token added in step one.

Key Takeaways

  • A transformer block combines a masked multi-head attention module with a fully connected feed-forward network that uses the GELU activation function, with data passing through these parts in order.
  • A GPT model is an LLM built by repeating many transformer blocks, and its sizes of 124, 345, 762, and 1,542 million parameters are all implemented with the same GPTModel Python class.
  • Text generation decodes output tensors into human-readable text by sequentially predicting one token at a time from an input context, feeding each result back into the context.
  • Without training, a GPT model generates incoherent text, so the architecture alone does not provide coherent output; training is what makes the text coherent.

Related concepts

More concepts starting with T