ggml
[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)
Brought by drakestapleton
ggml / examples / gpt-2 / README.md
| 1 | # gpt-2 |
| 2 | |
| 3 | This is a C++ example running GPT-2 inference using the [ggml](https://github.com/ggerganov/ggml) library. |
| 4 | |
| 5 | The program runs on the CPU - no video card is required. |
| 6 | |
| 7 | The [Cerebras-GPT](https://huggingface.co/cerebras) models are also supported. |
| 8 | |
| 9 | The example supports the following GPT-2 models: |
| 10 | |
| 11 | | Model | Description | Disk Size | |
| 12 | | --- | --- | --- | |
| 13 | | 117M | Small model | 240 MB | |
| 14 | | 345M | Medium model | 680 MB | |
| 15 | | 774M | Large model | 1.5 GB | |
| 16 | | 1558M | XL model | 3.0 GB | |
| 17 | |
| 18 | Sample performance on MacBook M1 Pro: |
| 19 | |
| 20 | | Model | Size | Time / Token | |
| 21 | | --- | --- | --- | |
| 22 | | GPT-2 | 117M | 5 ms | |
| 23 | | GPT-2 | 345M | 12 ms | |
| 24 | | GPT-2 | 774M | 23 ms | |
| 25 | | GPT-2 | 1558M | 42 ms | |
| 26 | |
| 27 | *TODO: add tables for Cerebras-GPT models* |
| 28 | |
| 29 | Sample output: |
| 30 | |
| 31 | ```bash |
| 32 | $ ./bin/gpt-2 -h |
| 33 | usage: ./bin/gpt-2 [options] |
| 34 | |
| 35 | options: |
| 36 | -h, --help show this help message and exit |
| 37 | -s SEED, --seed SEED RNG seed (default: -1) |
| 38 | -t N, --threads N number of threads to use during computation (default: 8) |
| 39 | -p PROMPT, --prompt PROMPT |
| 40 | prompt to start generation with (default: random) |
| 41 | -n N, --n_predict N number of tokens to predict (default: 200) |
| 42 | --top_k N top-k sampling (default: 40) |
| 43 | --top_p N top-p sampling (default: 0.9) |
| 44 | --temp N temperature (default: 1.0) |
| 45 | -b N, --batch_size N batch size for prompt processing (default: 8) |
| 46 | -m FNAME, --model FNAME |
| 47 | model path (default: models/gpt-2-117M/ggml-model.bin) |
| 48 | |
| 49 | $ ./bin/gpt-2 |
| 50 | gpt2_model_load: loading model from 'models/gpt-2-117M/ggml-model.bin' |
| 51 | gpt2_model_load: n_vocab = 50257 |
| 52 | gpt2_model_load: n_ctx = 1024 |
| 53 | gpt2_model_load: n_embd = 768 |
| 54 | gpt2_model_load: n_head = 12 |
| 55 | gpt2_model_load: n_layer = 12 |
| 56 | gpt2_model_load: f16 = 1 |
| 57 | gpt2_model_load: ggml ctx size = 311.12 MB |
| 58 | gpt2_model_load: memory size = 72.00 MB, n_mem = 12288 |
| 59 | gpt2_model_load: model size = 239.08 MB |
| 60 | main: number of tokens in prompt = 1 |
| 61 | |
| 62 | So this is going to be the end of the line for us. |
| 63 | |
| 64 | If the Dolphins continue to do their business, it's possible that the team could make a bid to bring in new defensive coordinator Scott Linehan. |
| 65 | |
| 66 | Linehan's job is a little daunting, but he's a great coach and an excellent coach. I don't believe we're going to make the playoffs. |
| 67 | |
| 68 | We're going to have to work hard to keep our heads down and get ready to go.<|endoftext|> |
| 69 | |
| 70 | main: mem per token = 2048612 bytes |
| 71 | main: load time = 106.32 ms |
| 72 | main: sample time = 7.10 ms |
| 73 | main: predict time = 506.40 ms / 5.06 ms per token |
| 74 | main: total time = 629.84 ms |
| 75 | ``` |
| 76 | |
| 77 | ## Downloading and converting the original models (GPT-2) |
| 78 | |
| 79 | You can download the original model files using the [download-model.sh](download-model.sh) Bash script. The models are |
| 80 | in Tensorflow format, so in order to use them with ggml, you need to convert them to appropriate format. This is done |
| 81 | via the [convert-ckpt-to-ggml.py](convert-ckpt-to-ggml.py) python script. |
| 82 | |
| 83 | Here is the entire process for the GPT-2 117M model (download from official site + conversion): |
| 84 | |
| 85 | ```bash |
| 86 | cd ggml/build |
| 87 | ../examples/gpt-2/download-model.sh 117M |
| 88 | |
| 89 | Downloading model 117M ... |
| 90 | models/gpt-2-117M/checkpoint 100%[=============================>] 77 --.-KB/s in 0s |
| 91 | models/gpt-2-117M/encoder.json 100%[=============================>] 1018K 1.20MB/s in 0.8s |
| 92 | models/gpt-2-117M/hparams.json 100%[=============================>] 90 --.-KB/s in 0s |
| 93 | models/gpt-2-117M/model.ckpt.data-00000-of-00001 100%[=============================>] 474.70M 1.21MB/s in 8m 39s |
| 94 | models/gpt-2-117M/model.ckpt.index 100%[=============================>] 5.09K --.-KB/s in 0s |
| 95 | models/gpt-2-117M/model.ckpt.meta 100%[=============================>] 460.11K 806KB/s in 0.6s |
| 96 | models/gpt-2-117M/vocab.bpe 100%[=============================>] 445.62K 799KB/s in 0.6s |
| 97 | Done! Model '117M' saved in 'models/gpt-2-117M/' |
| 98 | |
| 99 | Run the convert-ckpt-to-ggml.py script to convert the model to ggml format. |
| 100 | |
| 101 | python /Users/john/ggml/examples/gpt-2/convert-ckpt-to-ggml.py models/gpt-2-117M/ 1 |
| 102 | |
| 103 | ``` |
| 104 | |
| 105 | This conversion requires that you have python and Tensorflow installed on your computer. Still, if you want to avoid |
| 106 | this, you can download the already converted ggml models as described below. |
| 107 | |
| 108 | ## Downloading and converting the original models (Cerebras-GPT) |
| 109 | |
| 110 | Clone the respective repository from here: https://huggingface.co/cerebras |
| 111 | |
| 112 | Use the [convert-cerebras-to-ggml.py](convert-cerebras-to-ggml.py) script to convert the model to `ggml` format: |
| 113 | |
| 114 | ```bash |
| 115 | cd ggml/build |
| 116 | git clone https://huggingface.co/cerebras/Cerebras-GPT-111M models/ |
| 117 | python ../examples/gpt-2/convert-cerebras-to-ggml.py models/Cerebras-GPT-111M/ |
| 118 | |
| 119 | ``` |
| 120 | |
| 121 | ## Downloading the ggml model directly (GPT-2) |
| 122 | |
| 123 | For convenience, I will be hosting the converted ggml model files in order to make it easier to run the examples. This |
| 124 | way, you can directly download a single binary file and start using it. No python or Tensorflow is required. |
| 125 | |
| 126 | Here is how to get the 117M ggml model: |
| 127 | |
| 128 | ```bash |
| 129 | cd ggml/build |
| 130 | ../examples/gpt-2/download-ggml-model.sh 117M |
| 131 | |
| 132 | Downloading ggml model 117M ... |
| 133 | models/gpt-2-117M/ggml-model.bin 100%[===============================>] 239.58M 8.52MB/s in 28s |
| 134 | Done! Model '117M' saved in 'models/gpt-2-117M/ggml-model.bin' |
| 135 | You can now use it like this: |
| 136 | |
| 137 | $ ./bin/gpt-2 -m models/gpt-2-117M/ggml-model.bin -p "This is an example" |
| 138 | |
| 139 | ``` |
| 140 | |
| 141 | At some point, I might decide to stop hosting these models. So in that case, simply revert to the manual process above. |
| 142 | |
| 143 | ## Quantizing the models |
| 144 | |
| 145 | You can also try to quantize the `ggml` models via 4-bit integer quantization. |
| 146 | Keep in mind that for smaller models, this will render them completely useless. |
| 147 | You generally want to quantize larger models. |
| 148 | |
| 149 | ```bash |
| 150 | # quantize GPT-2 F16 to Q4_0 (faster but less precise) |
| 151 | ./bin/gpt-2-quantize models/gpt-2-1558M/ggml-model-f16.bin models/gpt-2-1558M/ggml-model-q4_0.bin 2 |
| 152 | ./bin/gpt-2 -m models/gpt-2-1558M/ggml-model-q4_0.bin -p "This is an example" |
| 153 | |
| 154 | # quantize Cerebras F16 to Q4_1 (slower but more precise) |
| 155 | ./bin/gpt-2-quantize models/Cerebras-GPT-6.7B/ggml-model-f16.bin models/Cerebras-GPT-6.7B/ggml-model-q4_1.bin 3 |
| 156 | ./bin/gpt-2 -m models/Cerebras-GPT-6.7B/ggml-model-q4_1.bin -p "This is an example" |
| 157 | |
| 158 | ``` |
| 159 | |
| 160 | ## Batched generation example |
| 161 | |
| 162 | You can try the batched generation from a given prompt using the gpt-2-batched binary. |
| 163 | |
| 164 | Sample output: |
| 165 | |
| 166 | ```bash |
| 167 | $ gpt-2-batched -np 5 -m models/gpt-2-117M/ggml-model.bin -p "Hello my name is" -n 50 |
| 168 | |
| 169 | main: seed = 1697037431 |
| 170 | gpt2_model_load: loading model from 'models/gpt-2-117M/ggml-model.bin' |
| 171 | gpt2_model_load: n_vocab = 50257 |
| 172 | gpt2_model_load: n_ctx = 1024 |
| 173 | gpt2_model_load: n_embd = 768 |
| 174 | gpt2_model_load: n_head = 12 |
| 175 | gpt2_model_load: n_layer = 12 |
| 176 | gpt2_model_load: ftype = 1 |
| 177 | gpt2_model_load: qntvr = 0 |
| 178 | gpt2_model_load: ggml tensor size = 320 bytes |
| 179 | gpt2_model_load: backend buffer size = 312.72 MB |
| 180 | ggml_init_cublas: found 1 CUDA devices: |
| 181 | Device 0: NVIDIA GeForce GTX 1660, compute capability 7.5 |
| 182 | gpt2_model_load: using CPU backend |
| 183 | gpt2_model_load: memory size = 72.00 MB, n_mem = 12288 |
| 184 | gpt2_model_load: model size = 239.08 MB |
| 185 | extract_tests_from_file : No test file found. |
| 186 | test_gpt_tokenizer : 0 tests failed out of 0 tests. |
| 187 | main: compute buffer size: 3.26 MB |
| 188 | |
| 189 | |
| 190 | main: generating 5 sequences ... |
| 191 | main: prompt: 'Hello my name is' |
| 192 | main: number of tokens in prompt = 4, first 8 tokens: 15496 616 1438 318 |
| 193 | |
| 194 | |
| 195 | sequence 0: |
| 196 | |
| 197 | Hello my name is John. You can call me any way you want, if you want, but for my very first date, I will be on the phone with you. We're both in our early 20s, but I feel like it's all |
| 198 | |
| 199 | sequence 1: |
| 200 | |
| 201 | Hello my name is Robert, and I want to say that we're proud to have your company here on the world's largest platform for sharing your stories with us. This is a huge opportunity for our community. We have hundreds of people on this team and |
| 202 | |
| 203 | sequence 2: |
| 204 | |
| 205 | Hello my name is Jack. I'm the one who created you. |
| 206 | |
| 207 | Jack is a boy with a big smile and a big heart. He is a handsome guy. He loves the outdoors and loves the people he meets. He wants to be a |
| 208 | |
| 209 | sequence 3: |
| 210 | |
| 211 | Hello my name is John. I am a Canadian citizen with a large number of family in Quebec and I am interested in studying. My aim is to take up a post in the Journal of the International Academy of Sciences of Canada which I am currently finishing. |
| 212 | |
| 213 | sequence 4: |
| 214 | |
| 215 | Hello my name is Dan. I am an entrepreneur. I am a great father. I am a great husband. I am a great husband. I am a great dad. And I am a great husband. |
| 216 | |
| 217 | I love my life. I love |
| 218 | |
| 219 | |
| 220 | |
| 221 | main: load time = 880.80 ms |
| 222 | main: sample time = 91.43 ms |
| 223 | main: predict time = 2518.29 ms |
| 224 | main: total time = 3544.32 ms |
| 225 | ``` |