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