Skip to content
 
 

Latest commit

 

History

43 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tinygpt2

A reference implementation of GPT-2 in pure Python. Download the weights from HuggingFace and performs inference just using NumPy. Really only useful for as a teaching tool, don't actually use this for anything.

Also need to download the encoder and vocab files vocab.bpe and encoder.json

Running

# Install dependencies
poetry install

# Run inference examples
poetry run python example.py

Structure

The core logic is split into several files:

  • gpt2_tensors.py - Loads the tensors into layer forms that can be used for inference
  • gpt2_ops.py - Implements the GPT-2 primitives (gelu, layernorm, softmax, etc.)
  • gpt2_run.py - Loads the weights and runs inference

In addition, the following files are included:

In the model directory, you will find the following files:

  • config.json - The GPT-2 model configuration
  • model.safetensors - The model weights
  • vocab.bpe - The BPE vocab from the original GPT-2 repository
  • encoder.json - The encoder json from the original GPT-2 repository

Visualization

The attention patterns of the model can be visualized using the gpt2_viz.py script. This will generate a heatmap of the attention patterns for a given input sequence.

License

MIT License

About

A reference implementation of GPT-2 in Python, for teaching ML compilers

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages