llama-rb 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,389 @@
1
+ # llama.cpp
2
+
3
+ ![llama](https://user-images.githubusercontent.com/1991296/230134379-7181e485-c521-4d23-a0d6-f7b3b61ba524.png)
4
+
5
+ [![Actions Status](https://github.com/ggerganov/llama.cpp/workflows/CI/badge.svg)](https://github.com/ggerganov/llama.cpp/actions)
6
+ [![License: MIT](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT)
7
+
8
+ Inference of [LLaMA](https://arxiv.org/abs/2302.13971) model in pure C/C++
9
+
10
+ **Hot topics:**
11
+
12
+ - [Roadmap Apr 2023](https://github.com/ggerganov/llama.cpp/discussions/784)
13
+
14
+ ## Description
15
+
16
+ The main goal is to run the model using 4-bit quantization on a MacBook
17
+
18
+ - Plain C/C++ implementation without dependencies
19
+ - Apple silicon first-class citizen - optimized via ARM NEON and Accelerate framework
20
+ - AVX2 support for x86 architectures
21
+ - Mixed F16 / F32 precision
22
+ - 4-bit quantization support
23
+ - Runs on the CPU
24
+
25
+ This was [hacked in an evening](https://github.com/ggerganov/llama.cpp/issues/33#issuecomment-1465108022) - I have no idea if it works correctly.
26
+ Please do not make conclusions about the models based on the results from this implementation.
27
+ For all I know, it can be completely wrong. This project is for educational purposes.
28
+ New features will probably be added mostly through community contributions.
29
+
30
+ **Supported platforms:**
31
+
32
+ - [X] Mac OS
33
+ - [X] Linux
34
+ - [X] Windows (via CMake)
35
+ - [X] Docker
36
+
37
+ **Supported models:**
38
+
39
+ - [X] LLaMA 🦙
40
+ - [X] [Alpaca](https://github.com/ggerganov/llama.cpp#instruction-mode-with-alpaca)
41
+ - [X] [GPT4All](https://github.com/ggerganov/llama.cpp#using-gpt4all)
42
+ - [X] [Chinese LLaMA / Alpaca](https://github.com/ymcui/Chinese-LLaMA-Alpaca)
43
+ - [X] [Vigogne (French)](https://github.com/bofenghuang/vigogne)
44
+ - [X] [Vicuna](https://github.com/ggerganov/llama.cpp/discussions/643#discussioncomment-5533894)
45
+
46
+ **Bindings:**
47
+
48
+ - Python: [abetlen/llama-cpp-python](https://github.com/abetlen/llama-cpp-python)
49
+ - Go: [go-skynet/go-llama.cpp](https://github.com/go-skynet/go-llama.cpp)
50
+
51
+ **UI:**
52
+
53
+ - [nat/openplayground](https://github.com/nat/openplayground)
54
+ - [oobabooga/text-generation-webui](https://github.com/oobabooga/text-generation-webui)
55
+
56
+ ---
57
+
58
+ Here is a typical run using LLaMA-7B:
59
+
60
+ ```java
61
+ make -j && ./main -m ./models/7B/ggml-model-q4_0.bin -p "Building a website can be done in 10 simple steps:" -n 512
62
+ I llama.cpp build info:
63
+ I UNAME_S: Darwin
64
+ I UNAME_P: arm
65
+ I UNAME_M: arm64
66
+ I CFLAGS: -I. -O3 -DNDEBUG -std=c11 -fPIC -pthread -DGGML_USE_ACCELERATE
67
+ I CXXFLAGS: -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC -pthread
68
+ I LDFLAGS: -framework Accelerate
69
+ I CC: Apple clang version 14.0.0 (clang-1400.0.29.202)
70
+ I CXX: Apple clang version 14.0.0 (clang-1400.0.29.202)
71
+
72
+ make: Nothing to be done for `default'.
73
+ main: seed = 1678486056
74
+ llama_model_load: loading model from './models/7B/ggml-model-q4_0.bin' - please wait ...
75
+ llama_model_load: n_vocab = 32000
76
+ llama_model_load: n_ctx = 512
77
+ llama_model_load: n_embd = 4096
78
+ llama_model_load: n_mult = 256
79
+ llama_model_load: n_head = 32
80
+ llama_model_load: n_layer = 32
81
+ llama_model_load: n_rot = 128
82
+ llama_model_load: f16 = 2
83
+ llama_model_load: n_ff = 11008
84
+ llama_model_load: ggml ctx size = 4529.34 MB
85
+ llama_model_load: memory_size = 512.00 MB, n_mem = 16384
86
+ llama_model_load: .................................... done
87
+ llama_model_load: model size = 4017.27 MB / num tensors = 291
88
+
89
+ main: prompt: 'Building a website can be done in 10 simple steps:'
90
+ main: number of tokens in prompt = 15
91
+ 1 -> ''
92
+ 8893 -> 'Build'
93
+ 292 -> 'ing'
94
+ 263 -> ' a'
95
+ 4700 -> ' website'
96
+ 508 -> ' can'
97
+ 367 -> ' be'
98
+ 2309 -> ' done'
99
+ 297 -> ' in'
100
+ 29871 -> ' '
101
+ 29896 -> '1'
102
+ 29900 -> '0'
103
+ 2560 -> ' simple'
104
+ 6576 -> ' steps'
105
+ 29901 -> ':'
106
+
107
+ sampling parameters: temp = 0.800000, top_k = 40, top_p = 0.950000
108
+
109
+
110
+ Building a website can be done in 10 simple steps:
111
+ 1) Select a domain name and web hosting plan
112
+ 2) Complete a sitemap
113
+ 3) List your products
114
+ 4) Write product descriptions
115
+ 5) Create a user account
116
+ 6) Build the template
117
+ 7) Start building the website
118
+ 8) Advertise the website
119
+ 9) Provide email support
120
+ 10) Submit the website to search engines
121
+ A website is a collection of web pages that are formatted with HTML. HTML is the code that defines what the website looks like and how it behaves.
122
+ The HTML code is formatted into a template or a format. Once this is done, it is displayed on the user's browser.
123
+ The web pages are stored in a web server. The web server is also called a host. When the website is accessed, it is retrieved from the server and displayed on the user's computer.
124
+ A website is known as a website when it is hosted. This means that it is displayed on a host. The host is usually a web server.
125
+ A website can be displayed on different browsers. The browsers are basically the software that renders the website on the user's screen.
126
+ A website can also be viewed on different devices such as desktops, tablets and smartphones.
127
+ Hence, to have a website displayed on a browser, the website must be hosted.
128
+ A domain name is an address of a website. It is the name of the website.
129
+ The website is known as a website when it is hosted. This means that it is displayed on a host. The host is usually a web server.
130
+ A website can be displayed on different browsers. The browsers are basically the software that renders the website on the user’s screen.
131
+ A website can also be viewed on different devices such as desktops, tablets and smartphones. Hence, to have a website displayed on a browser, the website must be hosted.
132
+ A domain name is an address of a website. It is the name of the website.
133
+ A website is an address of a website. It is a collection of web pages that are formatted with HTML. HTML is the code that defines what the website looks like and how it behaves.
134
+ The HTML code is formatted into a template or a format. Once this is done, it is displayed on the user’s browser.
135
+ A website is known as a website when it is hosted
136
+
137
+ main: mem per token = 14434244 bytes
138
+ main: load time = 1332.48 ms
139
+ main: sample time = 1081.40 ms
140
+ main: predict time = 31378.77 ms / 61.41 ms per token
141
+ main: total time = 34036.74 ms
142
+ ```
143
+
144
+ And here is another demo of running both LLaMA-7B and [whisper.cpp](https://github.com/ggerganov/whisper.cpp) on a single M1 Pro MacBook:
145
+
146
+ https://user-images.githubusercontent.com/1991296/224442907-7693d4be-acaa-4e01-8b4f-add84093ffff.mp4
147
+
148
+ ## Usage
149
+
150
+ Here are the step for the LLaMA-7B model:
151
+
152
+ ```bash
153
+ # build this repo
154
+ git clone https://github.com/ggerganov/llama.cpp
155
+ cd llama.cpp
156
+ make
157
+
158
+ #For Windows and CMake, use the following command instead:
159
+ cd <path_to_llama_folder>
160
+ mkdir build
161
+ cd build
162
+ cmake ..
163
+ cmake --build . --config Release
164
+
165
+ # obtain the original LLaMA model weights and place them in ./models
166
+ ls ./models
167
+ 65B 30B 13B 7B tokenizer_checklist.chk tokenizer.model
168
+
169
+ # install Python dependencies
170
+ python3 -m pip install torch numpy sentencepiece
171
+
172
+ # convert the 7B model to ggml FP16 format
173
+ python3 convert-pth-to-ggml.py models/7B/ 1
174
+
175
+ # quantize the model to 4-bits (using method 2 = q4_0)
176
+ ./quantize ./models/7B/ggml-model-f16.bin ./models/7B/ggml-model-q4_0.bin 2
177
+
178
+ # run the inference
179
+ ./main -m ./models/7B/ggml-model-q4_0.bin -n 128
180
+ ```
181
+
182
+ Currently, it's best to use Python 3.9 or Python 3.10, as `sentencepiece` has not yet published a wheel for Python 3.11.
183
+
184
+ When running the larger models, make sure you have enough disk space to store all the intermediate files.
185
+
186
+ ### Memory/Disk Requirements
187
+
188
+ As the models are currently fully loaded into memory, you will need adequate disk space to save them
189
+ and sufficient RAM to load them. At the moment, memory and disk requirements are the same.
190
+
191
+ | model | original size | quantized size (4-bit) |
192
+ |-------|---------------|------------------------|
193
+ | 7B | 13 GB | 3.9 GB |
194
+ | 13B | 24 GB | 7.8 GB |
195
+ | 30B | 60 GB | 19.5 GB |
196
+ | 65B | 120 GB | 38.5 GB |
197
+
198
+ ### Interactive mode
199
+
200
+ If you want a more ChatGPT-like experience, you can run in interactive mode by passing `-i` as a parameter.
201
+ In this mode, you can always interrupt generation by pressing Ctrl+C and enter one or more lines of text which will be converted into tokens and appended to the current context. You can also specify a *reverse prompt* with the parameter `-r "reverse prompt string"`. This will result in user input being prompted whenever the exact tokens of the reverse prompt string are encountered in the generation. A typical use is to use a prompt which makes LLaMa emulate a chat between multiple users, say Alice and Bob, and pass `-r "Alice:"`.
202
+
203
+ Here is an example few-shot interaction, invoked with the command
204
+
205
+ ```bash
206
+ # default arguments using 7B model
207
+ ./examples/chat.sh
208
+
209
+ # advanced chat with 13B model
210
+ ./examples/chat-13B.sh
211
+
212
+ # custom arguments using 13B model
213
+ ./main -m ./models/13B/ggml-model-q4_0.bin -n 256 --repeat_penalty 1.0 --color -i -r "User:" -f prompts/chat-with-bob.txt
214
+ ```
215
+
216
+ Note the use of `--color` to distinguish between user input and generated text.
217
+
218
+ ![image](https://user-images.githubusercontent.com/1991296/224575029-2af3c7dc-5a65-4f64-a6bb-517a532aea38.png)
219
+
220
+ ### Instruction mode with Alpaca
221
+
222
+ 1. First, download the `ggml` Alpaca model into the `./models` folder
223
+ 2. Run the `main` tool like this:
224
+
225
+ ```
226
+ ./examples/alpaca.sh
227
+ ```
228
+
229
+ Sample run:
230
+
231
+ ```
232
+ == Running in interactive mode. ==
233
+ - Press Ctrl+C to interject at any time.
234
+ - Press Return to return control to LLaMa.
235
+ - If you want to submit another line, end your input in '\'.
236
+
237
+ Below is an instruction that describes a task. Write a response that appropriately completes the request.
238
+
239
+ > How many letters are there in the English alphabet?
240
+ There 26 letters in the English Alphabet
241
+ > What is the most common way of transportation in Amsterdam?
242
+ The majority (54%) are using public transit. This includes buses, trams and metros with over 100 lines throughout the city which make it very accessible for tourists to navigate around town as well as locals who commute by tram or metro on a daily basis
243
+ > List 5 words that start with "ca".
244
+ cadaver, cauliflower, cabbage (vegetable), catalpa (tree) and Cailleach.
245
+ >
246
+ ```
247
+
248
+ ### Using [GPT4All](https://github.com/nomic-ai/gpt4all)
249
+
250
+ - Obtain the `gpt4all-lora-quantized.bin` model
251
+ - It is distributed in the old `ggml` format which is now obsoleted
252
+ - You have to convert it to the new format using [./convert-gpt4all-to-ggml.py](./convert-gpt4all-to-ggml.py). You may also need to
253
+ convert the model from the old format to the new format with [./migrate-ggml-2023-03-30-pr613.py](./migrate-ggml-2023-03-30-pr613.py):
254
+
255
+ ```bash
256
+ python3 convert-gpt4all-to-ggml.py models/gpt4all-7B/gpt4all-lora-quantized.bin ./models/tokenizer.model
257
+ python3 migrate-ggml-2023-03-30-pr613.py models/gpt4all-7B/gpt4all-lora-quantized.bin models/gpt4all-7B/gpt4all-lora-quantized-new.bin
258
+ ```
259
+
260
+ - You can now use the newly generated `gpt4all-lora-quantized-new.bin` model in exactly the same way as all other models
261
+ - The original model is saved in the same folder with a suffix `.orig`
262
+
263
+ ### Obtaining and verifying the Facebook LLaMA original model and Stanford Alpaca model data
264
+
265
+ - **Under no circumstances share IPFS, magnet links, or any other links to model downloads anywhere in this respository, including in issues, discussions or pull requests. They will be immediately deleted.**
266
+ - The LLaMA models are officially distributed by Facebook and will **never** be provided through this repository.
267
+ - Refer to [Facebook's LLaMA repository](https://github.com/facebookresearch/llama/pull/73/files) if you need to request access to the model data.
268
+ - Please verify the sha256 checksums of all downloaded model files to confirm that you have the correct model data files before creating an issue relating to your model files.
269
+ - The following command will verify if you have all possible latest files in your self-installed `./models` subdirectory:
270
+
271
+ `sha256sum --ignore-missing -c SHA256SUMS` on Linux
272
+
273
+ or
274
+
275
+ `shasum -a 256 --ignore-missing -c SHA256SUMS` on macOS
276
+
277
+ - If your issue is with model generation quality then please at least scan the following links and papers to understand the limitations of LLaMA models. This is especially important when choosing an appropriate model size and appreciating both the significant and subtle differences between LLaMA models and ChatGPT:
278
+ - LLaMA:
279
+ - [Introducing LLaMA: A foundational, 65-billion-parameter large language model](https://ai.facebook.com/blog/large-language-model-llama-meta-ai/)
280
+ - [LLaMA: Open and Efficient Foundation Language Models](https://arxiv.org/abs/2302.13971)
281
+ - GPT-3
282
+ - [Language Models are Few-Shot Learners](https://arxiv.org/abs/2005.14165)
283
+ - GPT-3.5 / InstructGPT / ChatGPT:
284
+ - [Aligning language models to follow instructions](https://openai.com/research/instruction-following)
285
+ - [Training language models to follow instructions with human feedback](https://arxiv.org/abs/2203.02155)
286
+
287
+ ### Perplexity (Measuring model quality)
288
+
289
+ You can use the `perplexity` example to measure perplexity over the given prompt. For more background,
290
+ see https://huggingface.co/docs/transformers/perplexity. However, in general, lower perplexity is better for LLMs.
291
+
292
+ #### Latest measurements
293
+
294
+ The latest perplexity scores for the various model sizes and quantizations are being tracked in [discussion #406](https://github.com/ggerganov/llama.cpp/discussions/406). `llama.cpp` is measuring very well
295
+ compared to the baseline implementations. Quantization has a small negative impact to quality, but, as you can see, running
296
+ 13B at q4_0 beats the 7B f16 model by a significant amount.
297
+
298
+ All measurements are done against wikitext2 test dataset (https://paperswithcode.com/dataset/wikitext-2), with default options (512 length context).
299
+ Note that the changing the context length will have a significant impact on perplexity (longer context = better perplexity).
300
+ ```
301
+ Perplexity - model options
302
+ 5.5985 - 13B, q4_0
303
+ 5.9565 - 7B, f16
304
+ 6.3001 - 7B, q4_1
305
+ 6.5949 - 7B, q4_0
306
+ 6.5995 - 7B, q4_0, --memory_f16
307
+ ```
308
+
309
+ #### How to run
310
+
311
+ 1. Download/extract: https://s3.amazonaws.com/research.metamind.io/wikitext/wikitext-2-raw-v1.zip?ref=salesforce-research
312
+ 2. Run `./perplexity -m models/7B/ggml-model-q4_0.bin -f wiki.test.raw`
313
+ 3. Output:
314
+ ```
315
+ perplexity : calculating perplexity over 655 chunks
316
+ 24.43 seconds per pass - ETA 4.45 hours
317
+ [1]4.5970,[2]5.1807,[3]6.0382,...
318
+ ```
319
+ And after 4.45 hours, you will have the final perplexity.
320
+
321
+ ### Android
322
+
323
+ You can easily run `llama.cpp` on Android device with [termux](https://termux.dev/).
324
+ First, obtain the [Android NDK](https://developer.android.com/ndk) and then build with CMake:
325
+ ```
326
+ $ mkdir build-android
327
+ $ cd build-android
328
+ $ export NDK=<your_ndk_directory>
329
+ $ cmake -DCMAKE_TOOLCHAIN_FILE=$NDK/build/cmake/android.toolchain.cmake -DANDROID_ABI=arm64-v8a -DANDROID_PLATFORM=android-23 -DCMAKE_C_FLAGS=-march=armv8.4a+dotprod ..
330
+ $ make
331
+ ```
332
+ Install [termux](https://termux.dev/) on your device and run `termux-setup-storage` to get access to your SD card.
333
+ Finally, copy the `llama` binary and the model files to your device storage. Here is a demo of an interactive session running on Pixel 5 phone:
334
+
335
+ https://user-images.githubusercontent.com/271616/225014776-1d567049-ad71-4ef2-b050-55b0b3b9274c.mp4
336
+
337
+ ### Docker
338
+
339
+ #### Prerequisites
340
+ * Docker must be installed and running on your system.
341
+ * Create a folder to store big models & intermediate files (in ex. im using /llama/models)
342
+
343
+ #### Images
344
+ We have two Docker images available for this project:
345
+
346
+ 1. `ghcr.io/ggerganov/llama.cpp:full`: This image includes both the main executable file and the tools to convert LLaMA models into ggml and convert into 4-bit quantization.
347
+ 2. `ghcr.io/ggerganov/llama.cpp:light`: This image only includes the main executable file.
348
+
349
+ #### Usage
350
+
351
+ The easiest way to download the models, convert them to ggml and optimize them is with the --all-in-one command which includes the full docker image.
352
+
353
+ ```bash
354
+ docker run -v /llama/models:/models ghcr.io/ggerganov/llama.cpp:full --all-in-one "/models/" 7B
355
+ ```
356
+
357
+ On complete, you are ready to play!
358
+
359
+ ```bash
360
+ docker run -v /llama/models:/models ghcr.io/ggerganov/llama.cpp:full --run -m /models/7B/ggml-model-q4_0.bin -p "Building a website can be done in 10 simple steps:" -n 512
361
+ ```
362
+
363
+ or with light image:
364
+
365
+ ```bash
366
+ docker run -v /llama/models:/models ghcr.io/ggerganov/llama.cpp:light -m /models/7B/ggml-model-q4_0.bin -p "Building a website can be done in 10 simple steps:" -n 512
367
+ ```
368
+
369
+ ### Contributing
370
+
371
+ - Contributors can open PRs
372
+ - Collaborators can push to branches in the `llama.cpp` repo and merge PRs into the `master` branch
373
+ - Collaborators will be invited based on contributions
374
+ - Any help with managing issues and PRs is very appreciated!
375
+ - Make sure to read this: [Inference at the edge](https://github.com/ggerganov/llama.cpp/discussions/205)
376
+ - A bit of backstory for those who are interested: [Changelog podcast](https://changelog.com/podcast/532)
377
+
378
+ ### Coding guidelines
379
+
380
+ - Avoid adding third-party dependencies, extra files, extra headers, etc.
381
+ - Always consider cross-compatibility with other operating systems and architectures
382
+ - Avoid fancy looking modern STL constructs, use basic `for` loops, avoid templates, keep it simple
383
+ - There are no strict rules for the code style, but try to follow the patterns in the code (indentation, spaces, etc.). Vertical alignment makes things more readable and easier to batch edit
384
+ - Clean-up any trailing whitespaces, use 4 spaces indentation, brackets on same line, `void * ptr`, `int & a`
385
+ - See [good first issues](https://github.com/ggerganov/llama.cpp/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) for tasks suitable for first contributions
386
+
387
+ ### Docs
388
+
389
+ - [GGML tips & tricks](https://github.com/ggerganov/llama.cpp/wiki/GGML-Tips-&-Tricks)
@@ -39,6 +39,8 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
39
39
 
40
40
  bool invalid_param = false;
41
41
  std::string arg;
42
+ gpt_params default_params;
43
+
42
44
  for (int i = 1; i < argc; i++) {
43
45
  arg = argv[i];
44
46
 
@@ -66,6 +68,11 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
66
68
  break;
67
69
  }
68
70
  std::ifstream file(argv[i]);
71
+ if (!file) {
72
+ fprintf(stderr, "error: failed to open file '%s'\n", argv[i]);
73
+ invalid_param = true;
74
+ break;
75
+ }
69
76
  std::copy(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), back_inserter(params.prompt));
70
77
  if (params.prompt.back() == '\n') {
71
78
  params.prompt.pop_back();
@@ -168,7 +175,7 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
168
175
  }
169
176
  params.n_parts = std::stoi(argv[i]);
170
177
  } else if (arg == "-h" || arg == "--help") {
171
- gpt_print_usage(argc, argv, params);
178
+ gpt_print_usage(argc, argv, default_params);
172
179
  exit(0);
173
180
  } else if (arg == "--random-prompt") {
174
181
  params.random_prompt = true;
@@ -180,13 +187,13 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
180
187
  params.input_prefix = argv[i];
181
188
  } else {
182
189
  fprintf(stderr, "error: unknown argument: %s\n", arg.c_str());
183
- gpt_print_usage(argc, argv, params);
190
+ gpt_print_usage(argc, argv, default_params);
184
191
  exit(1);
185
192
  }
186
193
  }
187
194
  if (invalid_param) {
188
195
  fprintf(stderr, "error: invalid parameter for argument: %s\n", arg.c_str());
189
- gpt_print_usage(argc, argv, params);
196
+ gpt_print_usage(argc, argv, default_params);
190
197
  exit(1);
191
198
  }
192
199