# openai/gpt-2 architecture

> Architecture diagram of the GitHub repository openai/gpt-2, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/openai/gpt-2
- Repository: https://github.com/openai/gpt-2

## Overview

This repository provides GPT-2 inference and text generation, not training. Users either enter prompts interactively or request unconditional samples; both paths load tokenizer/model metadata and checkpoints, encode token inputs, run autoregressive sampling through the Transformer, then decode and print generated text. The graph preserves these entry points and the shared model, tokenizer, and sampling layers. Source excerpts show their direct calls; model artifact downloading is documented by its standalone script, but no runtime connection from that script to generation is shown.

Main entry points: [`src/interactive_conditional_samples.py`](https://github.com/openai/gpt-2/blob/master/src/interactive_conditional_samples.py) (Prompt generation), [`src/generate_unconditional_samples.py`](https://github.com/openai/gpt-2/blob/master/src/generate_unconditional_samples.py) (Unconditional generation), [`src/sample.py`](https://github.com/openai/gpt-2/blob/master/src/sample.py) (Autoregressive sampler), [`src/encoder.py`](https://github.com/openai/gpt-2/blob/master/src/encoder.py) (BPE tokenizer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Generation interfaces"]
  node_interactive["Prompt generation"]
  node_unconditional["Unconditional generation"]
end

subgraph group_inference["Inference runtime"]
  node_tokenizer["BPE tokenizer<br/>[encoder.py]"]
  node_sampler["Autoregressive sampler<br/>[sample.py]"]
  node_transformer["GPT-2 Transformer<br/>[model.py]"]
end

subgraph group_artifacts["Model artifacts"]
  node_model_files[("Model files")]
  node_downloader["Artifact downloader<br/>[download_model.py]"]
end

node_user(("Researcher"))

node_user -->|"enters prompt"| node_interactive
node_user -->|"requests samples"| node_unconditional
node_interactive -->|"loads and encodes"| node_tokenizer
node_interactive -->|"requests generation"| node_sampler
node_interactive -->|"loads metadata and checkpoint"| node_model_files
node_sampler -->|"runs model"| node_transformer
node_unconditional -->|"loads and decodes"| node_tokenizer
node_unconditional -->|"requests generation"| node_sampler
node_unconditional -->|"loads metadata and checkpoint"| node_model_files
node_downloader -->|"downloads artifacts"| node_model_files

click node_interactive "https://github.com/openai/gpt-2/blob/master/src/interactive_conditional_samples.py"
click node_unconditional "https://github.com/openai/gpt-2/blob/master/src/generate_unconditional_samples.py"
click node_tokenizer "https://github.com/openai/gpt-2/blob/master/src/encoder.py"
click node_sampler "https://github.com/openai/gpt-2/blob/master/src/sample.py"
click node_transformer "https://github.com/openai/gpt-2/blob/master/src/model.py"
click node_downloader "https://github.com/openai/gpt-2/blob/master/download_model.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_interactive,node_unconditional toneBlue
class node_tokenizer,node_sampler,node_transformer toneAmber
class node_model_files,node_downloader toneMint
class node_user toneIndigo
```

## Components

### Generation interfaces

- [Prompt generation](https://github.com/openai/gpt-2/blob/master/src/interactive_conditional_samples.py): `src/interactive_conditional_samples.py`
- [Unconditional generation](https://github.com/openai/gpt-2/blob/master/src/generate_unconditional_samples.py): `src/generate_unconditional_samples.py`

### Inference runtime

- [BPE tokenizer](https://github.com/openai/gpt-2/blob/master/src/encoder.py): `src/encoder.py`
- [Autoregressive sampler](https://github.com/openai/gpt-2/blob/master/src/sample.py): `src/sample.py`
- [GPT-2 Transformer](https://github.com/openai/gpt-2/blob/master/src/model.py): `src/model.py`

### Model artifacts

- **Model files**
- [Artifact downloader](https://github.com/openai/gpt-2/blob/master/download_model.py): `download_model.py`

### Other components

- **Researcher**

## Connections

- Researcher → Prompt generation: enters prompt (evidence: `src/interactive_conditional_samples.py`)
- Researcher → Unconditional generation: requests samples (evidence: `src/generate_unconditional_samples.py`)
- Prompt generation → BPE tokenizer: loads and encodes (evidence: `src/interactive_conditional_samples.py`)
- Prompt generation → Autoregressive sampler: requests generation (evidence: `src/interactive_conditional_samples.py`)
- Prompt generation → Model files: loads metadata and checkpoint (evidence: `src/interactive_conditional_samples.py`)
- Autoregressive sampler → GPT-2 Transformer: runs model (evidence: `src/sample.py`)
- Unconditional generation → BPE tokenizer: loads and decodes (evidence: `src/generate_unconditional_samples.py`)
- Unconditional generation → Autoregressive sampler: requests generation (evidence: `src/generate_unconditional_samples.py`)
- Unconditional generation → Model files: loads metadata and checkpoint (evidence: `src/generate_unconditional_samples.py`)
- Artifact downloader → Model files: downloads artifacts (evidence: `download_model.py`)
