TinyGPT icon indicating copy to clipboard operation
TinyGPT copied to clipboard

Tiny C++11 GPT-2 inference implementation from scratch

TinyGPT

Tiny C++11 GPT-2 inference implementation from scratch, which is mainly based on the project picoGPT.

Accompanying blog post: Write a GPT from scratch (TinyGPT)

Core class

  • Tensor: Tensor class similar to the numpy interface.
  • Model: GPT-2 model implementation with reference to gpt2_pico.py.
  • Tokenizer: BPE tokenizer with exactly the same logic as GPT-2 encoder.py.

Build and Run

1. Get the code

git clone --recurse-submodules https://github.com/keith2018/TinyGPT.git

2. Install Intel MKL(Math Kernel Library)

Official website: Intel®-Optimized Math Library for Numerical Computing on CPUs & GPUs

3. Download GPT-2 model file

python3 tools/download_gpt2_model.py

if success, you'll see the file model_file.data in directory assets/gpt2

4. Build and Run

mkdir build
cmake -B ./build -DCMAKE_BUILD_TYPE=Release
cmake --build ./build --config Release

This will generate the executable file and copy assets to directory app/bin, then you can run the demo:

cd app/bin
./TinyGPT_demo
[DEBUG] TIMER TinyGPT::Model::loadModelGPT2: cost: 800 ms
[DEBUG] TIMER TinyGPT::Encoder::getEncoder: cost: 191 ms
INPUT:Alan Turing theorized that computers would one day become
GPT:the most powerful machines on the planet.
INPUT:exit

Dependencies

License

This code is licensed under the MIT License (see LICENSE).