interplane.Get Omega
Read in your language

ggml

[Ported from github.com/ggml-org/ggml] docs : sync ggml_type enum in gguf.md (#1685)

Brought by drakestapleton

branch master Branches Tags History

ggml / examples / gpt-2 / README.md

225 lines · 8795 bytes Raw Blame History Rendered
1# gpt-2
2
3This is a C++ example running GPT-2 inference using the [ggml](https://github.com/ggerganov/ggml) library.
4
5The program runs on the CPU - no video card is required.
6
7The [Cerebras-GPT](https://huggingface.co/cerebras) models are also supported.
8
9The 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
18Sample 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
29Sample output:
30
31```bash
32$ ./bin/gpt-2 -h
33usage: ./bin/gpt-2 [options]
34
35options:
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
50gpt2_model_load: loading model from 'models/gpt-2-117M/ggml-model.bin'
51gpt2_model_load: n_vocab = 50257
52gpt2_model_load: n_ctx = 1024
53gpt2_model_load: n_embd = 768
54gpt2_model_load: n_head = 12
55gpt2_model_load: n_layer = 12
56gpt2_model_load: f16 = 1
57gpt2_model_load: ggml ctx size = 311.12 MB
58gpt2_model_load: memory size = 72.00 MB, n_mem = 12288
59gpt2_model_load: model size = 239.08 MB
60main: number of tokens in prompt = 1
61
62So this is going to be the end of the line for us.
63
64If 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
66Linehan'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
68We're going to have to work hard to keep our heads down and get ready to go.<|endoftext|>
69
70main: mem per token = 2048612 bytes
71main: load time = 106.32 ms
72main: sample time = 7.10 ms
73main: predict time = 506.40 ms / 5.06 ms per token
74main: total time = 629.84 ms
75```
76
77## Downloading and converting the original models (GPT-2)
78
79You can download the original model files using the [download-model.sh](download-model.sh) Bash script. The models are
80in Tensorflow format, so in order to use them with ggml, you need to convert them to appropriate format. This is done
81via the [convert-ckpt-to-ggml.py](convert-ckpt-to-ggml.py) python script.
82
83Here is the entire process for the GPT-2 117M model (download from official site + conversion):
84
85```bash
86cd ggml/build
87../examples/gpt-2/download-model.sh 117M
88
89Downloading model 117M ...
90models/gpt-2-117M/checkpoint 100%[=============================>] 77 --.-KB/s in 0s
91models/gpt-2-117M/encoder.json 100%[=============================>] 1018K 1.20MB/s in 0.8s
92models/gpt-2-117M/hparams.json 100%[=============================>] 90 --.-KB/s in 0s
93models/gpt-2-117M/model.ckpt.data-00000-of-00001 100%[=============================>] 474.70M 1.21MB/s in 8m 39s
94models/gpt-2-117M/model.ckpt.index 100%[=============================>] 5.09K --.-KB/s in 0s
95models/gpt-2-117M/model.ckpt.meta 100%[=============================>] 460.11K 806KB/s in 0.6s
96models/gpt-2-117M/vocab.bpe 100%[=============================>] 445.62K 799KB/s in 0.6s
97Done! Model '117M' saved in 'models/gpt-2-117M/'
98
99Run 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
105This conversion requires that you have python and Tensorflow installed on your computer. Still, if you want to avoid
106this, you can download the already converted ggml models as described below.
107
108## Downloading and converting the original models (Cerebras-GPT)
109
110Clone the respective repository from here: https://huggingface.co/cerebras
111
112Use the [convert-cerebras-to-ggml.py](convert-cerebras-to-ggml.py) script to convert the model to `ggml` format:
113
114```bash
115cd ggml/build
116git clone https://huggingface.co/cerebras/Cerebras-GPT-111M models/
117python ../examples/gpt-2/convert-cerebras-to-ggml.py models/Cerebras-GPT-111M/
118
119```
120
121## Downloading the ggml model directly (GPT-2)
122
123For convenience, I will be hosting the converted ggml model files in order to make it easier to run the examples. This
124way, you can directly download a single binary file and start using it. No python or Tensorflow is required.
125
126Here is how to get the 117M ggml model:
127
128```bash
129cd ggml/build
130../examples/gpt-2/download-ggml-model.sh 117M
131
132Downloading ggml model 117M ...
133models/gpt-2-117M/ggml-model.bin 100%[===============================>] 239.58M 8.52MB/s in 28s
134Done! Model '117M' saved in 'models/gpt-2-117M/ggml-model.bin'
135You 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
141At 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
145You can also try to quantize the `ggml` models via 4-bit integer quantization.
146Keep in mind that for smaller models, this will render them completely useless.
147You 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
162You can try the batched generation from a given prompt using the gpt-2-batched binary.
163
164Sample 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
169main: seed = 1697037431
170gpt2_model_load: loading model from 'models/gpt-2-117M/ggml-model.bin'
171gpt2_model_load: n_vocab = 50257
172gpt2_model_load: n_ctx = 1024
173gpt2_model_load: n_embd = 768
174gpt2_model_load: n_head = 12
175gpt2_model_load: n_layer = 12
176gpt2_model_load: ftype = 1
177gpt2_model_load: qntvr = 0
178gpt2_model_load: ggml tensor size = 320 bytes
179gpt2_model_load: backend buffer size = 312.72 MB
180ggml_init_cublas: found 1 CUDA devices:
181 Device 0: NVIDIA GeForce GTX 1660, compute capability 7.5
182gpt2_model_load: using CPU backend
183gpt2_model_load: memory size = 72.00 MB, n_mem = 12288
184gpt2_model_load: model size = 239.08 MB
185extract_tests_from_file : No test file found.
186test_gpt_tokenizer : 0 tests failed out of 0 tests.
187main: compute buffer size: 3.26 MB
188
189
190main: generating 5 sequences ...
191main: prompt: 'Hello my name is'
192main: number of tokens in prompt = 4, first 8 tokens: 15496 616 1438 318
193
194
195sequence 0:
196
197Hello 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
199sequence 1:
200
201Hello 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
203sequence 2:
204
205Hello my name is Jack. I'm the one who created you.
206
207Jack 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
209sequence 3:
210
211Hello 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
213sequence 4:
214
215Hello 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
217I love my life. I love
218
219
220
221main: load time = 880.80 ms
222main: sample time = 91.43 ms
223main: predict time = 2518.29 ms
224main: total time = 3544.32 ms
225```