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
这是什么?minGPT 是 OpenAI GPT 的一个极简 PyTorch 重新实现,涵盖训练和推理。它的设计目标是代码小巧、易于阅读,而不是面向生产环境的优化。
这是一个完整的框架,还是一个小型参考库?它是一个只有三个文件的小型库:mingpt/model.py(Transformer 模型)、mingpt/bpe.py(分词器)和 mingpt/trainer.py(训练循环)。此外,projects 文件夹中还有演示脚本和 Jupyter 笔记本。
怎样把它添加到项目里?克隆仓库,并按照 'Library Installation' 一节的说明把 mingpt 安装到你的项目中,然后像 demo.ipynb 中演示的那样实例化 GPT 模型和 Trainer。
除了 Python,这里还需要别的东西吗?你需要 PyTorch 来构建和训练 Transformer 模型。值得注意的是,作者承认这个仓库目前甚至还没有提供 requirements.txt 文件。
这个项目还在积极开发吗?不了。截至 2023 年 1 月,minGPT 已处于半归档状态。由于它在 notebook、博客和课程中被广泛引用,作者不愿再做较大的改动;如果想了解更新的工作,他推荐其重写版本 nanoGPT。
nanoGPT 是什么?它和 minGPT 有什么关系?nanoGPT 是 karpathy 对 minGPT 的后继重写版本。在他决定超越纯粹的教学用途之后,它被构建出来:依然保持简洁,但能够复现中等规模的行业基准,并具有更好的运行效率。
minGPT 附带了哪些演示项目?projects/adder 从零开始训练一个 GPT 来做加法;projects/chargpt 在输入的文本文件上训练一个字符级语言模型;demo.ipynb 和 generate.ipynb 则在 Jupyter 笔记本中展示基本用法,包括加载预训练的 GPT-2。
minGPT 如何处理分词?mingpt/bpe.py 重新实现了 OpenAI 的 Byte Pair Encoder,与原始 GPT-2 代码的做法完全一致,将文本与整数序列相互转换。
我能用预训练的 GPT-2 生成文本,而不是从零开始训练吗?可以。generate.ipynb 展示了如何加载预训练的 GPT-2 模型,并根据给定的提示生成文本续写。
Creator
karpathy
Size
1.5 MB
Stars
25K
Updated
CommitJan 08
Age
6y
Commits · 90 Days