Megatron is a large, powerful transformer developed by the Applied Deep Learning Research team at NVIDIA. This particular Megatron model was trained from a generative, left-to-right transformer in the style of GPT-2. This model was trained on text sourced from Wikipedia, RealNews, OpenWebText, and CC-Stories. It contains 345 million parameters.

Find more information at

How to run Megatron GPT2 using Transformers


In that guide, we run all the commands from a folder called $MYDIR and defined as (in bash):

export MYDIR=$HOME

Feel free to change the location at your convenience.

To run some of the commands below, you'll have to clone Transformers.

git clone $MYDIR/transformers

Get the checkpoints from the NVIDIA GPU Cloud

You must create a directory called nvidia/megatron-gpt2-345m:

mkdir -p $MYDIR/nvidia/megatron-gpt2-345m

You can download the checkpoints from the NVIDIA GPU Cloud (NGC). For that you have to sign up for and setup the NVIDIA GPU Cloud (NGC) Registry CLI. Further documentation for downloading models can be found in the NGC documentation.

Alternatively, you can directly download the checkpoints using:

wget --content-disposition -O $MYDIR/nvidia/megatron-gpt2-345m/

Converting the checkpoint

In order to be loaded into Transformers, the checkpoint has to be converted. You should run the following command for that purpose. That command will create config.json and pytorch_model.bin in $MYDIR/nvidia/megatron-gpt2-345m. You can move those files to different directories if needed.

python3 $MYDIR/transformers/src/transformers/models/megatron_gpt2/ $MYDIR/nvidia/megatron-gpt2-345m/

Text generation

The following code shows how to use the Megatron GPT2 checkpoint and the Transformers API to generate text.

import os
import torch

from transformers import GPT2Tokenizer, GPT2LMHeadModel

# The tokenizer. Megatron was trained with standard tokenizer(s).
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
# The path to the config/checkpoint (see the conversion step above).
directory = os.path.join(os.environ['MYDIR'], 'nvidia/megatron-gpt2-345m')
# Load the model from $MYDIR/nvidia/megatron-gpt2-345m.
model = GPT2LMHeadModel.from_pretrained(directory)

# Copy to the device and use FP16.
assert torch.cuda.is_available()
device = torch.device("cuda")

# Generate the sentence.
output = model.generate(input_ids=None, max_length=32, num_return_sequences=1)

# Output the text.
for sentence in output:
    sentence = sentence.tolist()
    text = tokenizer.decode(sentence, clean_up_tokenization_spaces=True)

To use this as a normal HuggingFace model

If you want to use this model with HF Trainer, here is a quick way to do that:

  1. Download nvidia checkpoint:

    wget --content-disposition -O
  2. Convert:

    python src/transformers/models/megatron_gpt2/
  3. Fetch missing files

    git clone
  4. Move the converted files into the cloned model dir

    mv config.json pytorch_model.bin megatron-gpt2-345m/
  5. The megatron-gpt2-345m dir should now have all the files which can be passed to HF Trainer as --model_name_or_path megatron-gpt2-345m

Original code

The original Megatron code can be found here:

Downloads last month
Hosted inference API

Unable to determine this model’s pipeline type. Check the docs .