Principle:LaurentMazare Tch rs GPT Text Generation
| Knowledge Sources | |
|---|---|
| Domains | Natural Language Processing, Generative Modeling, Transformer Architecture |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
GPT-style text generation employs an autoregressive transformer with causal self-attention to generate coherent text sequences one token at a time.
Description
The Generative Pre-trained Transformer (GPT) architecture is a decoder-only transformer designed for autoregressive language modeling. Its key components are:
- Token and positional embeddings: Each input token is mapped to a dense vector via a learned embedding table. A separate positional embedding encodes the absolute position of each token in the sequence, providing the model with ordering information that the self-attention mechanism alone cannot capture.
- Causal (masked) self-attention: The core innovation is the use of a causal mask that prevents each position from attending to subsequent positions. This ensures that the prediction for position depends only on positions through , preserving the autoregressive property. Multi-head attention allows the model to jointly attend to information from different representation subspaces.
- Layer normalization and residual connections: Each transformer block applies layer normalization (typically pre-norm, applied before attention and feed-forward sublayers) and residual connections (adding the sublayer input to its output). These stabilize training of deep networks by controlling gradient magnitude and enabling information flow across layers.
- Feed-forward network: Each transformer block contains a position-wise feed-forward network, typically with a hidden dimension 4x the model dimension and a nonlinear activation (GELU).
- Autoregressive generation: Text is generated token by token. At each step, the model processes all previously generated tokens and produces a probability distribution over the vocabulary for the next token. The token is sampled from this distribution (with optional temperature, top-k, or nucleus sampling) and appended to the sequence.
Usage
GPT-style generation is used in text completion, dialogue systems, creative writing, code generation, and any task requiring fluent natural language generation. The architecture is also the basis for few-shot learning via prompting.
Theoretical Basis
Autoregressive Language Model Objective:
Scaled Dot-Product Attention:
where are query, key, and value matrices, is the key dimension, and is the causal mask:
Multi-Head Attention:
Transformer Block (Pre-Norm):
x := x + MultiHeadAttention(LayerNorm(x)) x := x + FeedForward(LayerNorm(x))
Positional Embedding:
Learned positional embeddings are added to token embeddings:
Generation with Temperature:
where is the logit for token and is the temperature parameter.