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-j / README.md

239 lines · 10957 bytes Raw Blame History Rendered
1# gpt-j
2
3Local GPT-J inference on your computer using C/C++
4
5No video card required. You just need to have 16 GB of RAM.
6
7## Motivation
8
9The GPT-J 6B model is the open-source alternative to OpenAI's GPT-3. It's basically a neural network that allows you to
10generate coherent, human-like text given a certain context (prompt).
11
12The GPT-J model is quite big - the compact version of the model uses 16-bit floating point representation of the weights
13and is still 12 GB big. This means that in order to run inference on your computer, you would need to have a video card
14with at least 12 GB of video RAM. Alternatively, you can try to run the python implementations on the CPU, but that
15would probably not be very efficient as they are primarily optimized for running on a GPU (or at least this is my guess -
16I don't have much experience with python).
17
18I wanted to try and run the model on my MacBook, so I decided to implement the model inference from scratch using my own
19custom build tensor library. The tensor library (called [ggml](https://github.com/ggerganov/ggml), written in C) is in
20early development stage, but it already allows me to run the GPT-J model.
21
22On my 32GB MacBook M1 Pro, I achieve an inference speed of about `125 ms/token` or about ~6 words per second (1 word
23typically consists of 1 or 2 tokens).
24
25Here is a sample run with prompt `int main(int argc, char ** argv) {`:
26
27```bash
28$ time ./bin/gpt-j -p "int main(int argc, char ** argv) {"
29
30gptj_model_load: loading model from 'models/gpt-j-6B/ggml-model.bin' - please wait ...
31gptj_model_load: n_vocab = 50400
32gptj_model_load: n_ctx = 2048
33gptj_model_load: n_embd = 4096
34gptj_model_load: n_head = 16
35gptj_model_load: n_layer = 28
36gptj_model_load: n_rot = 64
37gptj_model_load: f16 = 1
38gptj_model_load: ggml ctx size = 13334.86 MB
39gptj_model_load: memory_size = 1792.00 MB, n_mem = 57344
40gptj_model_load: ................................... done
41gptj_model_load: model size = 11542.79 MB / num tensors = 285
42main: number of tokens in prompt = 13
43
44int main(int argc, char ** argv) {
45 (void)argc;
46 (void)argv;
47
48 {
49 struct sockaddr_in addr;
50 int addrlen;
51 char * ip = "192.168.1.4";
52 int i;
53
54 if ( (addrlen = sizeof(addr)) == -1 )
55 return -1;
56
57 for (i = 0; i < 10; ++i) {
58 addr.sin_family = AF_INET;
59 addr.sin_addr.s_addr = inet_addr(ip);
60
61main: mem per token = 16430420 bytes
62main: load time = 6211.48 ms
63main: sample time = 13.74 ms
64main: predict time = 26420.34 ms / 124.62 ms per token
65main: total time = 33035.37 ms
66
67real 0m33.171s
68user 3m32.269s
69sys 0m3.686s
70
71$
72```
73
74It took ~6.2 seconds to load the model to memory. After that, it took ~26.4 seconds to generate 200 tokens of what
75looks like to be the beginning of a networking program in C. Pretty cool!
76
77Here is another run, just for fun:
78
79```bash
80time ./bin/gpt-j -n 500 -t 8 -p "Ask HN: Inherited the worst code and tech team I have ever seen. How to fix it?
81"
82
83gptj_model_load: loading model from 'models/gpt-j-6B/ggml-model.bin' - please wait ...
84gptj_model_load: n_vocab = 50400
85gptj_model_load: n_ctx = 2048
86gptj_model_load: n_embd = 4096
87gptj_model_load: n_head = 16
88gptj_model_load: n_layer = 28
89gptj_model_load: n_rot = 64
90gptj_model_load: f16 = 1
91gptj_model_load: ggml ctx size = 13334.86 MB
92gptj_model_load: memory_size = 1792.00 MB, n_mem = 57344
93gptj_model_load: ................................... done
94gptj_model_load: model size = 11542.79 MB / num tensors = 285
95main: number of tokens in prompt = 24
96
97Ask HN: Inherited the worst code and tech team I have ever seen. How to fix it?
98
99I've inherited a team with some very strange and un-documented practices, one of them is that they use an old custom
100application with a very slow tech stack written in Python that the team doesn't want to touch but also doesn't want to
101throw away as it has some "legacy" code in it.
102
103The problem is, the tech stack is very very slow.
104
105They have a single web server on a VM that is slow.
106The server is a little bit busy (not very busy though) and they have a lot of processes (30+ that are constantly being
107spawned by the application)
108They have an application that is single threaded and was written in Python and the team don't want to touch this, and
109the application is very slow.
110
111My task as a new member of the team is to fix this.
112
113I'm a senior dev on the team (3 years on the project) and have been told that I will take the lead on this task. I know
114next to nothing about Python. So here is what I have so far.
115
116What I have done is I've been trying to debug the processes with the "ps" command. This way I can see what is running
117and where. From what I see, the application spawns 10 processes a minute and some of them are used for nothing.
118
119I have also started to look for the code. The application source is not in GitHub or any other repository, it is only on
120our internal GitLab.
121
122What I've found so far:
123
124The application uses a custom SQLAlchemy implementation to interact with the data. I've looked at the source, it looks
125like an object cache or something like that. But from what I've seen, the cache gets full every 20 minutes and then gets
126cleared with a special command.
127
128Another strange thing is that the application creates a file for every entry in the database (even if the entry already
129exists). I've looked at the file to see if it contains something, but it seems to be a JSON file with lots of records.
130
131The other strange thing is that I can only find the database tables in the GitLab repository and not the code. So I
132can't really understand how the application is supposed to interact with the database.
133
134I also found a "log" directory, but the code is encrypted with AES. From what I've found, it is in
135
136main: mem per token = 16430420 bytes
137main: load time = 3900.10 ms
138main: sample time = 32.58 ms
139main: predict time = 68049.91 ms / 130.11 ms per token
140main: total time = 73020.05 ms
141
142real 1m13.156s
143user 9m1.328s
144sys. 0m7.103s
145```
146
147## Implementation details
148
149The high level implementation of the model is contained in the [main.cpp](main.cpp) file. The core computations are
150performed by the [ggml](https://github.com/ggerganov/ggml/blob/master/include/ggml.h) library.
151
152
153#### Matrix multiplication
154
155The most performance critical part of the implementation is of course the matrix multiplication routine. 99% of the time
156is spent here, so it was important to optimize this as much as possible.
157
158On Arm64, I utilize the 128-bit NEON intrinsics for 16-bit floating point operations:
159
160https://github.com/ggerganov/ggml/blob/fb558f78d905f85c54813602649ddd628ffe0f3a/src/ggml.c#L187-L243
161
162These instructions allow each core to operate simultaneously on 64 16-bit floats. I'm no expert in SIMD, but after quite
163some trials this was the most efficient code for dot product of a row and column that I could come up with. Combined
164with the parallel computation on 8 CPU threads, I believe I'm close to the maximum performance that one could possibly
165get on the M1 CPU. Still, I'm curious to know if there is a more efficient way to implement this.
166
167
168#### Attempt to use the M1 GPU
169
170One interesting property of the GPT-J transformer architecture is that it allows you to perform part of the inference in
171parallel - i.e. the Feed-forward network can be computed in parallel to the Self-attention layer:
172
173https://github.com/ggerganov/ggml/blob/fb558f78d905f85c54813602649ddd628ffe0f3a/examples/gpt-j/main.cpp#L507-L531
174
175So I thought why not try and bring in the M1 GPU to compute half of the neural network in parallel to the CPU and
176potentially gain some extra performance. Thanks to the M1's shared memory model, it was relatively easy to offload part
177of the computation to the GPU using Apple's [Metal Performance
178Shaders](https://developer.apple.com/documentation/metalperformanceshaders). The GPU shares the host memory, so there is
179no need to copy the data back and forth as you would normally do with Cuda or OpenCL. The weight matrices are directly
180available to be used by the GPU.
181
182However, to my surprise, using MPS together with the CPU did not lead to any performance improvement at all. My
183conclusion was that the 8-thread NEON CPU computation is already saturating the memory bandwidth of the M1 and since
184the CPU and the GPU on the MacBook are sharing that bandwidth, it does not help to offload the computation to the GPU.
185Another observation was that the MPS GPU matrix multiplication using 16-bit floats had the same performance as the
1868-thread NEON CPU implementation. Again, I explain this with a saturated memory channel. But of course, my explanation
187could be totally wrong and somehow the implementation wasn't utilizing the resources correctly.
188
189In the end, I decided to not use MPS or the GPU all together.
190
191### Zero memory allocations
192
193Another property of my implementation is that it does not perform any memory allocations once the model is loaded into
194memory. All required memory is allocated at the start of the program with a single `malloc` (technically 2 calls, but
195that is not important).
196
197## Usage
198
199If you want to give this a try and you are on Linux or Mac OS, simply follow these instructions:
200
201```bash
202# Download the ggml-compatible GPT-J 6B model (requires 12GB disk space)
203../examples/gpt-j/download-ggml-model.sh 6B
204
205# Run the inference (requires 16GB of CPU RAM)
206./bin/gpt-j -m models/gpt-j-6B/ggml-model.bin -p "This is an example"
207
208# Input prompt through pipe and run the inference.
209echo "This is an example" > prompt.txt
210cat prompt.txt | ./bin/gpt-j -m models/gpt-j-6B/ggml-model.bin
211```
212
213To run the `gpt-j` tool, you need the 12GB `ggml-model.bin` file which contains the GPT-J model in
214[ggml](https://github.com/ggerganov/ggml) compatible format. In the instructions above, the binary file
215is downloaded from my repository on Hugging Face using the [download-ggml-model.sh](download-ggml-model.sh) script.
216You can also, download the file manually from this link:
217
218https://huggingface.co/ggerganov/ggml/tree/main
219
220---
221
222Alternatively, if you don't want to download the 12GB ggml model file, you can perform the conversion yourself using
223python.
224
225First, you need to download the full GPT-J model from here: https://huggingface.co/EleutherAI/gpt-j-6B
226
227Note that the full model is quite big - about 72 GB. After you download it, you need to convert it to ggml format using
228the [convert-h5-to-ggml.py](convert-h5-to-ggml.py) script. This will generate the `ggml-model.bin` file, which you can
229then use with the `gpt-j` program.
230
231
232## GPT-2
233
234I also implemented a tool for CPU inference using the smaller GPT-2 models. They have worse quality compared to GPT-J,
235but are much faster to execute.
236
237For example, the Small GPT-2 model is only 240 MB big and the inference speed on my MacBook is about 200 tokens/sec.
238
239For more details, checkout the GPT-2 example here: [gpt-2](https://github.com/ggerganov/ggml/tree/master/examples/gpt-2)