Skip to content
Code for the paper "Language Models are Unsupervised Multitask Learners"
Python Jupyter Notebook Shell
Branch: tpu
Clone or download
Pull request Compare This branch is 179 commits ahead, 4 commits behind nshepperd:finetuning.
Latest commit be7aad5 Jan 13, 2020
Type Name Latest commit message Commit time
Failed to load latest commit information.
etc Add watchdog system Nov 15, 2019
src src/ Initial version for converting hdf5 snapsho… Jan 13, 2020
.gitattributes add .gitattributes file to ensure files copied to docker container ha… Feb 21, 2019
.gitignore Add watchdog system Nov 15, 2019 Update Mar 18, 2019 updates for 345M model May 3, 2019
Dockerfile.cpu updates for 345M model May 3, 2019
Dockerfile.gpu updates for 345M model May 3, 2019
LICENSE add license Feb 20, 2019 Merge remote-tracking branch 'origin/master' into finetuning May 14, 2019
Training_GPT_2_Using_TPUs.ipynb Adding fire dependency Nov 10, 2019 Don't print an extra newline in Dec 23, 2019 fix for windows (thanks to chrothenbach) Mar 7, 2019 Fix Mar 21, 2019 Add; now streams each line Jan 12, 2020
requirements.txt Add tokenizers==0.0.9 to requirements.txt Dec 24, 2019 Add support for expanding parameters from snapshots Jan 11, 2020 Add learning rate cosine annealing with warmup Nov 12, 2019 Add; now streams each line Jan 12, 2020 Add; now streams each line Jan 12, 2020 Combine input text files with <|endoftext|> delimiter to ensure there… Mar 19, 2019 Add more logging when computing validation losses Jan 12, 2020 Keep the last 100,000 heartbeats Nov 15, 2019 Minor watchdog fix Nov 15, 2019


Code from the paper "Language Models are Unsupervised Multitask Learners".

We have currently released small (117M parameter) and medium (345M parameter) versions of GPT-2. While we have not released the larger models, we have released a dataset for researchers to study their behaviors.

See more details in our blog post.


This repository is meant to be a starting point for researchers and engineers to experiment with GPT-2.

Some caveats

  • GPT-2 models' robustness and worst case behaviors are not well-understood. As with any machine-learned model, carefully evaluate GPT-2 for your use case, especially if used without fine-tuning or in safety-critical applications where reliability is important.
  • The dataset our GPT-2 models were trained on contains many texts with biases and factual inaccuracies, and thus GPT-2 models are likely to be biased and inaccurate as well.
  • To avoid having samples mistaken as human-written, we recommend clearly labeling samples as synthetic before wide dissemination. Our models are often incoherent or inaccurate in subtle ways, which takes more than a quick read for a human to notice.

Work with us

Please let us know if you’re doing interesting research with or working on applications of GPT-2! We’re especially interested in hearing from and potentially working with those who are studying

  • Potential malicious use cases and defenses against them (e.g. the detectability of synthetic text)
  • The extent of problematic content (e.g. bias) being baked into the models and effective mitigations





Fine tuning on custom datasets

To retrain GPT-2 117M model on a custom text dataset:

PYTHONPATH=src ./ --dataset <file|directory|glob>

If you want to precompute the dataset's encoding for multiple runs, you can instead use:

PYTHONPATH=src ./ <file|directory|glob> /path/to/encoded.npz
PYTHONPATH=src ./ --dataset /path/to/encoded.npz

Gradient Checkpointing is included to reduce the memory requirements of the model, and can be enabled by --memory_saving_gradients. The checkpoints are currently chosen manually (poorly) by just adding layer 10 to the 'checkpoints' collection in --memory_saving_gradients is enabled by default for training the 345M model.

Validation loss

Set --val_every to a number of steps N > 0, and "validation" loss against a fixed sample of the dataset will be calculated every N steps to get a better sense of training progress. N around 200 suggested. You can set --val_dataset to choose a separate validation dataset, otherwise it defaults to a sample from the train dataset (so not a real cross-validation loss!).


You can use SGD instead of Adam with --optimizer sgd. This also helps conserve memory when training the 345M model. Note: the learning rate needs to be adjusted for SGD, due to not having Adam's gradient normalization (0.0006 seems to be a good number from some experiments).

Multi gpu (out of date)

To do distributed on multiple GPUs or machines using Horovod:

mpirun -np 4 \
    -H localhost:4 \
    -bind-to none -map-by slot \
    -x PYTHONPATH=src \
    -mca pml ob1 -mca btl ^openib \
    /home/jovyan/gpt-2/ --dataset encoded.npz

GPT-2 samples

WARNING: Samples are unfiltered and may contain offensive content.

While we have not yet released GPT-2 itself, you can see some samples from it in the gpt-2-samples folder. We show unconditional samples with default settings (temperature 1 and no truncation), with temperature 0.7, and with truncation with top_k 40. We show conditional samples, with contexts drawn from WebText's test set, with default settings (temperature 1 and no truncation), with temperature 0.7, and with truncation with top_k 40.


Please use the following bibtex entry:

  title={Language Models are Unsupervised Multitask Learners},
  author={Radford, Alec and Wu, Jeff and Child, Rewon and Luan, David and Amodei, Dario and Sutskever, Ilya},

Future work

We may release code for evaluating the models on various benchmarks.

We are still considering release of the larger models.



You can’t perform that action at this time.