The nanoGPT-style implementation of RWKV Language Model - an RNN with GPT-level LLM performance.
Python
201
8 commits
updated Nov 9, 2023

The nanoGPT-style implementation of RWKV Language Model - an RNN with GPT-level LLM performance.
This is a rewrite of RWKV-v4neo and HuggingFace Implementation that aims to create a clean code base of RWKV for head-to-head comparison with GPT-series, while keeping in line with the simplicity and practicality of nanoGPT. This single repository can be utilized for training both GPT and RWKV models.

RWKV is essentially an RNN with unrivaled advantage when doing inference. Here we benchmark the speed and space occupation of RWKV, along with its Transformer counterpart (code could be found here). We could easily find:



We organize this project as follows:
how to set up environment to kick off this project
some concepts you should be familiar with
a step-by-step tutorial of building RWKV in a jupyter notebook
reproduce RWKV and GPT under relatively equitable conditions.
using trained RWKV to do generation
things remaining to be done (Welcome PR)
some useful references about RWKV and Large Language Model
We would recommend using Conda to manage the environment.
conda create -n nanoRWKV python=3.8
conda activate nanoRWKV
pip install torch numpy transformers datasets tiktoken wandb tqdm ninja
## replace * with your driver version if loading kernel failed
# conda install cuda -c nvidia/label/cuda-11.*.0
Before kicking off this project, make sure you are familiar with the following concepts:
We would present a step-by-step tutorial of building RWKV in a jupyter notebook.
After all set up, let's build RWKV - first tokenize the dataset (OpenWebText):
python data/openwebtext/prepare.py
Then train RWKV(130M) with 8*V100 32GB on one node using PyTorch Distributed Data Parallel (DDP) :
torchrun --standalone --nproc_per_node=8 train.py config/train_rwkv.py
For comparision, we also train a GPT-2 model(124M) on the same device with:
torchrun --standalone --nproc_per_node=8 train.py config/train_gpt2.py
We got the results as follows (check this wandb project):
| model | params | train loss | val loss |
|---|---|---|---|
| GPT-2 | 124M | 2.82 | 2.86 |
| RWKV | 130M | 2.85 | 2.88 |
Existing OpenAI GPT-2 checkpoints and RWKV checkpoints allow us to get some baselines in place for openwebtext. We can get the numbers as follows:
python train.py config/eval_rwkv4_{169m|430m|1b5|3b|7b|14b}.py
python train.py config/eval_gpt2{|_medium|_large|_xl}.py
and observe the following losses on val set:
| model | RWKV | GPT-2 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| parameters | 169M | 430M | 1.5B | 3B | 7B | 14B | 124M | 350M | 774M | 1.5B |
| val loss | 3.11 | 2.79 | 2.54 | 2.42 | 2.32 | 2.23 | 3.11 | 2.82 | 2.66 | 2.56 |
Notice that both models are not trained in the openwebtext (RWKV in The Pile and OpenAI GPT-2 in private WebText), so they could be further improved due to dataset domain gap.
After training is done, we could use the following to ask LLM to do generation:
python sample.py \
--init_from=$model \
--start="What is the answer to life, the universe, and everything?" \
--num_samples=1 --max_new_tokens=100
The $model above could be either GPT-seris or RWKV-series:
#For GPT-series:
model=gpt2/gpt2-xl/gpt2-medium/gpt2-large
#For RWKV-series:
model_type=169m/430m/1b5/3b
model=RWKV/rwkv-4-{model_type}-pile
This is not a done project and there are a lot remaining:
Here are some useful references (offering my sincerest gratitude):
32 followers · starred Dec 2023
The nanoGPT-style implementation of RWKV Language Model - an RNN with GPT-level LLM performance.
Python
201
8 commits
updated Nov 9, 2023

The nanoGPT-style implementation of RWKV Language Model - an RNN with GPT-level LLM performance.
This is a rewrite of RWKV-v4neo and HuggingFace Implementation that aims to create a clean code base of RWKV for head-to-head comparison with GPT-series, while keeping in line with the simplicity and practicality of nanoGPT. This single repository can be utilized for training both GPT and RWKV models.

RWKV is essentially an RNN with unrivaled advantage when doing inference. Here we benchmark the speed and space occupation of RWKV, along with its Transformer counterpart (code could be found here). We could easily find:



We organize this project as follows:
how to set up environment to kick off this project
some concepts you should be familiar with
a step-by-step tutorial of building RWKV in a jupyter notebook
reproduce RWKV and GPT under relatively equitable conditions.
using trained RWKV to do generation
things remaining to be done (Welcome PR)
some useful references about RWKV and Large Language Model
We would recommend using Conda to manage the environment.
conda create -n nanoRWKV python=3.8
conda activate nanoRWKV
pip install torch numpy transformers datasets tiktoken wandb tqdm ninja
## replace * with your driver version if loading kernel failed
# conda install cuda -c nvidia/label/cuda-11.*.0
Before kicking off this project, make sure you are familiar with the following concepts:
We would present a step-by-step tutorial of building RWKV in a jupyter notebook.
After all set up, let's build RWKV - first tokenize the dataset (OpenWebText):
python data/openwebtext/prepare.py
Then train RWKV(130M) with 8*V100 32GB on one node using PyTorch Distributed Data Parallel (DDP) :
torchrun --standalone --nproc_per_node=8 train.py config/train_rwkv.py
For comparision, we also train a GPT-2 model(124M) on the same device with:
torchrun --standalone --nproc_per_node=8 train.py config/train_gpt2.py
We got the results as follows (check this wandb project):
| model | params | train loss | val loss |
|---|---|---|---|
| GPT-2 | 124M | 2.82 | 2.86 |
| RWKV | 130M | 2.85 | 2.88 |
Existing OpenAI GPT-2 checkpoints and RWKV checkpoints allow us to get some baselines in place for openwebtext. We can get the numbers as follows:
python train.py config/eval_rwkv4_{169m|430m|1b5|3b|7b|14b}.py
python train.py config/eval_gpt2{|_medium|_large|_xl}.py
and observe the following losses on val set:
| model | RWKV | GPT-2 | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| parameters | 169M | 430M | 1.5B | 3B | 7B | 14B | 124M | 350M | 774M | 1.5B |
| val loss | 3.11 | 2.79 | 2.54 | 2.42 | 2.32 | 2.23 | 3.11 | 2.82 | 2.66 | 2.56 |
Notice that both models are not trained in the openwebtext (RWKV in The Pile and OpenAI GPT-2 in private WebText), so they could be further improved due to dataset domain gap.
After training is done, we could use the following to ask LLM to do generation:
python sample.py \
--init_from=$model \
--start="What is the answer to life, the universe, and everything?" \
--num_samples=1 --max_new_tokens=100
The $model above could be either GPT-seris or RWKV-series:
#For GPT-series:
model=gpt2/gpt2-xl/gpt2-medium/gpt2-large
#For RWKV-series:
model_type=169m/430m/1b5/3b
model=RWKV/rwkv-4-{model_type}-pile
This is not a done project and there are a lot remaining:
Here are some useful references (offering my sincerest gratitude):
32 followers · starred Dec 2023