minGPT README

The project's README from github.com/karpathy/minGPT. As one markdown file: README.md.

minGPT

mingpt

A PyTorch re-implementation of GPT, both training and inference. minGPT tries to be small, clean, interpretable and educational, as most of the currently available GPT model implementations can a bit sprawling. GPT is not a complicated model and this implementation is appropriately about 300 lines of code (see mingpt/model.py). All that's going on is that a sequence of indices feeds into a Transformer, and a probability distribution over the next index in the sequence comes out. The majority of the complexity is just being clever with batching (both across examples and over sequence length) for efficiency.

note (Jan 2023): though I may continue to accept and change some details, minGPT is in a semi-archived state. For more recent developments see my rewrite nanoGPT. Basically, minGPT became referenced across a wide variety of places (notebooks, blogs, courses, books, etc.) which made me less willing to make the bigger changes I wanted to make to move the code forward. I also wanted to change the direction a bit, from a sole focus on education to something that is still simple and hackable but has teeth (reproduces medium-sized industry benchmarks, accepts some tradeoffs to gain runtime efficiency, etc).

The minGPT library is three files: mingpt/model.py contains the actual Transformer model definition, mingpt/bpe.py contains a mildly refactored Byte Pair Encoder that translates between text and sequences of integers exactly like OpenAI did in GPT, mingpt/trainer.py is (GPT-independent) PyTorch boilerplate code that trains the model. Then there are a number of demos and projects that use the library in the projects folder:

Library Installation

If you want to import mingpt into your project:

git clone https://github.com/karpathy/minGPT.git
cd minGPT
pip install -e .

Usage

Here's how you'd instantiate a GPT-2 (124M param version):

from mingpt.model import GPT
model_config = GPT.get_default_config()
model_config.model_type = 'gpt2'
model_config.vocab_size = 50257 # openai's model vocabulary
model_config.block_size = 1024  # openai's model block_size (i.e. input context length)
model = GPT(model_config)

And here's how you'd train it:

# your subclass of torch.utils.data.Dataset that emits example
# torch LongTensor of lengths up to 1024, with integers from [0,50257)
train_dataset = YourDataset()

from mingpt.trainer import Trainer
train_config = Trainer.get_default_config()
train_config.learning_rate = 5e-4 # many possible options, see the file
train_config.max_iters = 1000
train_config.batch_size = 32
trainer = Trainer(train_config, model, train_dataset)
trainer.run()

See demo.ipynb for a more concrete example.

Unit tests

Coverage is not super amazing just yet but:

python -m unittest discover tests

todos

References

Code:

Papers + some implementation notes:

Improving Language Understanding by Generative Pre-Training (GPT-1)

Language Models are Unsupervised Multitask Learners (GPT-2)

Language Models are Few-Shot Learners (GPT-3)

Generative Pretraining from Pixels (Image GPT)

License

MIT

minGPT (karpathy/minGPT)

Searching Database…

25K ·GitHub
What is this?minGPT is a minimal PyTorch re-implementation of OpenAI's GPT, covering both training and inference, written to be small and easy to read rather than production-optimized.
Is this a full framework or a tiny reference library?It's a tiny library of three files: mingpt/model.py (the Transformer), mingpt/bpe.py (the tokenizer), and mingpt/trainer.py (the training loop), plus demo scripts/notebooks in the projects folder.
How do I add it to a project?Clone the repo and install mingpt into your project as described in the 'Library Installation' section, then instantiate a GPT model and Trainer as shown in demo.ipynb.
Do I need anything besides Python here?You need PyTorch to build and train the Transformer model; notably the author admits the repo doesn't even ship a requirements.txt file yet.
Is this still actively developed?No — as of January 2023 minGPT is semi-archived, since it became widely referenced in notebooks, blogs and courses, making the author reluctant to make bigger changes; for newer work he points to his rewrite nanoGPT.
What is nanoGPT and how does it relate to minGPT?nanoGPT is karpathy's successor rewrite of minGPT, built after he decided to move beyond pure education toward something that still stays simple but reproduces medium-sized industry benchmarks with better runtime efficiency.
What demo projects come with minGPT?projects/adder trains a GPT from scratch to add numbers, projects/chargpt trains a character-level language model on an input text file, and demo.ipynb/generate.ipynb show basic usage in notebooks, including loading a pretrained GPT-2.
How does minGPT handle tokenization?mingpt/bpe.py reimplements OpenAI's Byte Pair Encoder, converting text to and from sequences of integers exactly as the original GPT-2 code does.
Can I generate text with a pretrained GPT-2 instead of training from scratch?Yes, generate.ipynb shows how to load a pretrained GPT-2 model and generate text continuations from a given prompt.
Creator
karpathy
Size
1.5 MB
Stars
25K
Updated
CommitJan 08
Age
6y
Commits · 90 Days