Building a GPT Model from a Configuration Dictionary
BASE_CONFIG holds the settings shared by every GPT-2 size, including vocabulary size 50257, context length 1024, dropout rate 0.0, and query-key-value bias True.
Predict Before You Read the Script
Loading a pretrained GPT-2 model is not one action. It is a sequence of steps that depend on each other. First the settings have to be decided, because a model cannot be built without them. Then the pretrained weights have to be downloaded, because they cannot be loaded before they exist locally. Finally the weights have to be loaded into a model whose shape matches them. This article follows one short script that performs these steps in order. You will predict what its key variables hold, then trace where a result could go wrong.
The goal is not to memorize the script line by line. The goal is to look at a run, point to the step where the outcome stopped matching what you expected, and explain why.
What do you think happens?
The script defines a shared dictionary called BASE_CONFIG and a second dictionary of model sizes. Which dictionary does the model's shape come from when the model is built?
Reveal answer
BASE_CONFIG
The script builds the model from BASE_CONFIG, not from model_configs. That single fact determines the model's shape, so it is the first place to look when a model's size seems wrong. The weights come from a separate input, params, which is covered later in this article.
Following the Script From Top to Bottom
The script runs in a fixed order. It first imports three names: a download helper, the GPTModel class, and a weight-loading function. It then defines a base configuration dictionary, defines a second dictionary of model sizes, chooses a model, and merges the two. After that it downloads the weights, builds the model, loads the weights into it, and switches the model to evaluation mode.
The diagram below shows that sequence. Read it as a dependency chain rather than a list. Each step consumes something produced by the step before it. The configuration must exist before the model can be constructed, and the download must complete before the loading step can run. The loading step is marked because it is where a mismatch between the downloaded weights and the model's shape first appears.
Base Settings Shared by Every Size
BASE_CONFIG holds the settings that describe the model in general, and these are shared by every GPT-2 size. It contains four settings. The vocabulary size is 50257. The context length is 1024. The dropout rate is 0.0. The query-key-value bias is True. Together these describe the general shape of the model that the script builds.
BASE_CONFIG is the dictionary of GPT-2 settings that apply to every model size: vocabulary size 50257, context length 1024, dropout rate 0.0, and query-key-value bias True.
The size-specific settings are different. Each GPT-2 size has its own embedding dimension, number of layers, and number of attention heads. These are the values that change when you pick a different size. The base settings do not change between sizes, which is why they are kept in one shared dictionary instead of being repeated for each size.
Two of the four base settings are worth understanding in more detail, because they describe behavior inside the model rather than a simple count.
Merging a Size Into the Base With update
The script chooses one size and merges its settings into BASE_CONFIG with the dictionary update operation. The size-specific settings are looked up by the chosen key in model_configs. After the merge, BASE_CONFIG contains the four shared settings together with the size's embedding dimension, number of layers, and number of attention heads. The model is then built from this single combined configuration.
The diagram below shows the result of the merge. The four base settings come from BASE_CONFIG, and the three size-specific settings come from the chosen entry in model_configs. Under dictionary update, a key present in both places takes the value from the size entry, while a key present only in the base stays as it was.
Tracing the keys after one merge
BASE_CONFIG holds four keys: vocabulary size, context length, dropout rate, and query-key-value bias. The chosen size entry holds three keys: embedding dimension, number of layers, and number of attention heads. After the update, how many keys does the configuration hold, and which keys came from the base?
- 1Start with the base
BASE_CONFIG has four keys. None of them has been changed yet.
- 2Apply the size entry
The update adds the three size-specific keys. None of those names matches a base key, so no base value is replaced.
- 3Count the result
Four base keys plus three size keys gives seven keys in the merged configuration.
- 4Identify the origin
The four shared settings still come from BASE_CONFIG. The three size settings come from the chosen entry.
The merged configuration has seven keys. Four come from BASE_CONFIG and three come from the chosen size entry.
What the Download Step Produces
Calling the download helper does more than return numbers. Running it causes several files to be downloaded, and each file is listed with its size and transfer progress in the output. The files are not all the same kind of thing, so it helps to group them by role. The grouping below is a way to organize the files. The source lists the files but does not explain each one in detail, so treat the groups as an aid to reading, not a complete specification.
The checkpoint, index, data, and meta files together make up the model's saved checkpoint. The checkpoint data file is the largest of the downloaded files. The encoder.json and vocab.bpe files relate to the vocabulary, which connects text to the numbers a model works with. The hparams.json file holds hyperparameters. The diagram below shows how these groups relate to the download helper and to the two values it returns.
Loading Weights Into the Model
The download helper returns two values, called settings and params. The model is constructed from BASE_CONFIG. Then the function load_weights_into_gpt places the downloaded params into that model, and the loader is described as loading the weights into a GPTModel instance. The last step, model.eval(), prepares the loaded model for use.
Notice that settings is returned but is not passed into the model in the script. The model's shape comes from BASE_CONFIG, while the weights come from params. Keeping these two inputs separate in your head matters. A mismatch between the configuration and the weights does not show up when the configuration is written. It shows up at the loading step, when the weights are placed into the model.
Mistakes When Changing the Model Size
Changing the model size is the most common place where the configuration and the weights drift apart. The following mistakes are easy to make because each one looks reasonable on its own.
Common mistake Editing model_configs and expecting the model's shape to change directly
Why it failsThe model is built from BASE_CONFIG, so edits to
model_configsonly matter after they are merged into BASE_CONFIG by the update step.Do this insteadConfirm that the merge line runs with the intended key, then check the merged configuration before building the model.
Common mistake Assuming the settings value returned by the download helper is used to build the model
Why it failsThe script returns settings but does not pass it into the model. The model's shape comes from BASE_CONFIG.
Do this insteadTrace how the model is built from BASE_CONFIG, and treat params as the only input that feeds the weight-loading step.
Common mistake Treating a dropout rate of 0.0 as a setting that affects the loaded weights
Why it failsThe dropout rate is a configuration setting that controls behavior during the forward pass. It is not a value stored in the downloaded weights.
Do this insteadRead the dropout rate as a property of how the built model runs, separate from the weight values.
Common mistake Changing CHOOSE_MODEL without checking that the download step fetches files for the newly chosen size
Tracing a Divergence Through the Script
The method for debugging this script is the same for any variable. For each variable, write down what you expected it to hold. Then find the line where it was last assigned. The first line whose result differs from your expectation is where the divergence began. Downstream symptoms, such as a model with the wrong shape, often appear far from that line.
A run finishes without an error, but the model's shape is not what you expected. Using the script order, name the first variable you would check, the line that last assigned it, and the step that would show the mismatch.
Hints
- Start from the model's shape, which comes from BASE_CONFIG after the merge.
- Check whether the merge line ran with the key you intended.
- The shape mismatch between configuration and weights shows up at the loading step, so do not stop there when tracing the cause.
Key Takeaways
- BASE_CONFIG holds the four settings shared by every GPT-2 size: vocabulary size 50257, context length 1024, dropout rate 0.0, and query-key-value bias True.
- The chosen size's settings are merged into BASE_CONFIG with dictionary update, and the model is built from that single combined configuration.
- The steps run in order: download, configuration, then weight loading, and a mismatch between the configuration and the weights appears at the loading step.
- The download produces checkpoint files, vocabulary files, and a hyperparameter file, and the helper returns settings and params, with params feeding the weight-loading step.