picoGPT is an unnecessarily tiny and minimal implementation of GPT-2 in plain NumPy.
Go to file
jaymody d4e955d0ca I can't spell. 2023-02-09 18:53:26 -05:00
.gitignore Initial commit. 2023-01-21 22:12:22 +01:00
README.md Refactor. 2023-01-31 01:27:09 +01:00
encoder.py Initial commit. 2023-01-21 22:12:22 +01:00
gpt2.py I can't spell. 2023-02-09 18:53:26 -05:00
gpt2_pico.py I can't spell. 2023-02-09 18:53:26 -05:00
requirements.txt Refactor. 2023-01-31 01:27:09 +01:00
utils.py Refactor. 2023-01-31 01:27:09 +01:00

README.md

PicoGPT

You've seen openai/gpt-2.

You've seen karpathy/minGPT.

You've even seen karpathy/nanoGPT!

But have you seen picoGPT??!?

picoGPT is an unnecessarily tiny and minimal implementation of GPT-2 in plain NumPy. The entire forward pass code is 40 lines of code.

So is picoGPT:

  • Fast? Nah, picoGPT is megaSLOW 🐌
  • Training code? Error, 404 not found
  • Batch inference? picoGPT is civilized, one at a time only, single file line
  • Support top-p? top-k? temperature? categorical sampling?! greedy?
  • Readable? 🤔 Well actually, it's not too horrible on the eyes!
  • Smol??? YESS!!! TEENIE TINY in fact 🤏

Dependencies

pip install -r requirements.txt

If you're using an M1 Macbook, you'll need to replace tensorflow with tensorflow-macos.

Tested on Python 3.9.10.

Usage

python gpt2.py "Alan Turing theorized that computers would one day become"

Which generates

 the most powerful machines on the planet.

The computer is a machine that can perform complex calculations, and it can perform these calculations in a way that is very similar to the human brain.

You can also control the number of tokens to generate, the model size (one of ["124M", "355M", "774M", "1558M"]), and the directory to save the models:

python gpt2.py \
    "Alan Turing theorized that computers would one day become" \
    --n_tokens_to_generate 40 \
    --model_size "124M" \
    --models_dir "models"

gpt2_pico.py is the same as gpt2.py, but has even fewer lines of code (removed comments, extra whitespace, and combined certain operations into a single line). Why? Because why not.