https://github.com/ggerganov/llama.cpp Skip to content Toggle navigation Sign up * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code + Explore + All features + Documentation + GitHub Skills + Blog * Solutions + For + Enterprise + Teams + Startups + Education + By Solution + CI/CD & Automation + DevOps + DevSecOps + Case Studies + Customer Stories + Resources * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles + Repositories + Topics + Trending + Collections * Pricing [ ] * # In this repository All GitHub | Jump to | * No suggested jump to results * # In this repository All GitHub | Jump to | * # In this user All GitHub | Jump to | * # In this repository All GitHub | Jump to | Sign in Sign up {{ message }} ggerganov / llama.cpp Public * Notifications * Fork 151 * Star 3k Port of Facebook's LLaMA model in C/C++ License MIT license 3k stars 151 forks Star Notifications * Code * Issues 26 * Pull requests 6 * Actions * Projects 0 * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Security * Insights ggerganov/llama.cpp This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. master Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags Name already in use A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch? Cancel Create 1 branch 0 tags Code * Local * Codespaces * Clone HTTPS GitHub CLI [https://github.com/g] Use Git or checkout with SVN using the web URL. [gh repo clone ggerga] Work fast with our official CLI. Learn more. * Open with GitHub Desktop * Download ZIP Sign In Required Please sign in to use Codespaces. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching Xcode If nothing happens, download Xcode and try again. Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @ggerganov ggerganov 10% performance boost on ARM ... 113a9e8 Mar 12, 2023 10% performance boost on ARM 113a9e8 Git stats * 43 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time .github/workflows Add CI (#60) March 12, 2023 22:08 models Final touches March 10, 2023 21:50 .gitignore .gitignore models/ March 11, 2023 12:27 LICENSE Add LICENSE (#21) March 12, 2023 08:36 Makefile Update Makefile var + add comment March 11, 2023 12:27 README.md Update README.md March 12, 2023 23:39 convert-pth-to-ggml.py Revert "weights_only" arg - this causing more trouble than help March 12, 2023 20:59 ggml.c 10% performance boost on ARM March 13, 2023 00:56 ggml.h Initial release March 10, 2023 20:56 main.cpp Fix color getting reset before prompt output done (#65) March 13, 2023 00:07 quantize.cpp Windows fixes (#31) March 12, 2023 22:15 utils.cpp Add interactive mode (#61) March 12, 2023 23:13 utils.h Add interactive mode (#61) March 12, 2023 23:13 View code llama.cpp Description Usage Interactive mode Limitations README.md llama.cpp Actions Status License: MIT Inference of Facebook's LLaMA model in pure C/C++ Hot topics * Running on Windows: #22 * Fix Tokenizer / Unicode support: #11 Description The main goal is to run the model using 4-bit quantization on a MacBook * Plain C/C++ implementation without dependencies * Apple silicon first-class citizen - optimized via Arm Neon and Accelerate framework * AVX2 support for x86 architectures * Mixed F16 / F32 precision * 4-bit quantization support * Runs on the CPU This was hacked in an evening - I have no idea if it works correctly. Please do not make conclusions about the models based on the results from this implementation. For all I know, it can be completely wrong. This project is for educational purposes and is not going to be maintained properly. New features will probably be added mostly through community contributions, if any. Supported platforms: * [*] Mac OS * [*] Linux * [ ] Windows (soon) --------------------------------------------------------------------- Here is a typical run using LLaMA-7B: make -j && ./main -m ./models/7B/ggml-model-q4_0.bin -p "Building a website can be done in 10 simple steps:" -t 8 -n 512 I llama.cpp build info: I UNAME_S: Darwin I UNAME_P: arm I UNAME_M: arm64 I CFLAGS: -I. -O3 -DNDEBUG -std=c11 -fPIC -pthread -DGGML_USE_ACCELERATE I CXXFLAGS: -I. -I./examples -O3 -DNDEBUG -std=c++11 -fPIC -pthread I LDFLAGS: -framework Accelerate I CC: Apple clang version 14.0.0 (clang-1400.0.29.202) I CXX: Apple clang version 14.0.0 (clang-1400.0.29.202) make: Nothing to be done for `default'. main: seed = 1678486056 llama_model_load: loading model from './models/7B/ggml-model-q4_0.bin' - please wait ... llama_model_load: n_vocab = 32000 llama_model_load: n_ctx = 512 llama_model_load: n_embd = 4096 llama_model_load: n_mult = 256 llama_model_load: n_head = 32 llama_model_load: n_layer = 32 llama_model_load: n_rot = 128 llama_model_load: f16 = 2 llama_model_load: n_ff = 11008 llama_model_load: ggml ctx size = 4529.34 MB llama_model_load: memory_size = 512.00 MB, n_mem = 16384 llama_model_load: .................................... done llama_model_load: model size = 4017.27 MB / num tensors = 291 main: prompt: 'Building a website can be done in 10 simple steps:' main: number of tokens in prompt = 15 1 -> '' 8893 -> 'Build' 292 -> 'ing' 263 -> ' a' 4700 -> ' website' 508 -> ' can' 367 -> ' be' 2309 -> ' done' 297 -> ' in' 29871 -> ' ' 29896 -> '1' 29900 -> '0' 2560 -> ' simple' 6576 -> ' steps' 29901 -> ':' sampling parameters: temp = 0.800000, top_k = 40, top_p = 0.950000 Building a website can be done in 10 simple steps: 1) Select a domain name and web hosting plan 2) Complete a sitemap 3) List your products 4) Write product descriptions 5) Create a user account 6) Build the template 7) Start building the website 8) Advertise the website 9) Provide email support 10) Submit the website to search engines 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. The HTML code is formatted into a template or a format. Once this is done, it is displayed on the user's browser. 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. 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. A website can be displayed on different browsers. The browsers are basically the software that renders the website on the user's screen. 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. A domain name is an address of a website. It is the name of the website. 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. A website can be displayed on different browsers. The browsers are basically the software that renders the website on the user's screen. 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. A domain name is an address of a website. It is the name of the website. 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. The HTML code is formatted into a template or a format. Once this is done, it is displayed on the user's browser. A website is known as a website when it is hosted main: mem per token = 14434244 bytes main: load time = 1332.48 ms main: sample time = 1081.40 ms main: predict time = 31378.77 ms / 61.41 ms per token main: total time = 34036.74 ms And here is another demo of running both LLaMA-7B and whisper.cpp on a single M1 Pro MacBook: Usage Here are the step for the LLaMA-7B model: # build this repo git clone https://github.com/ggerganov/llama.cpp cd llama.cpp make # obtain the original LLaMA model weights and place them in ./models ls ./models 65B 30B 13B 7B tokenizer_checklist.chk tokenizer.model # install Python dependencies python3 -m pip install torch numpy sentencepiece # convert the 7B model to ggml FP16 format python3 convert-pth-to-ggml.py models/7B/ 1 # quantize the model to 4-bits ./quantize ./models/7B/ggml-model-f16.bin ./models/7B/ggml-model-q4_0.bin 2 # run the inference ./main -m ./models/7B/ggml-model-q4_0.bin -t 8 -n 128 For the bigger models, there are a few extra quantization steps. For example, for LLaMA-13B, converting to FP16 format will create 2 ggml files, instead of one: ggml-model-f16.bin ggml-model-f16.bin.1 You need to quantize each of them separately like this: ./quantize ./models/13B/ggml-model-f16.bin ./models/13B/ggml-model-q4_0.bin 2 ./quantize ./models/13B/ggml-model-f16.bin.1 ./models/13B/ggml-model-q4_0.bin.1 2 Everything else is the same. Simply run: ./main -m ./models/13B/ggml-model-q4_0.bin -t 8 -n 128 The number of files generated for each model is as follows: 7B -> 1 file 13B -> 2 files 30B -> 4 files 65B -> 8 files When running the larger models, make sure you have enough disk space to store all the intermediate files. Interactive mode If you want a more ChatGPT-like experience, you can run in interactive mode by passing -i as a parameter. 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:". Here is an example few-shot interaction, invoked with the command ./main -m ./models/13B/ggml-model-q4_0.bin -t 8 -n 256 --repeat_penalty 1.0 --color -i -r "User:" \ -p \ "Transcript of a dialog, where the User interacts with an Assistant named Bob. Bob is helpful, kind, honest, good at writing, and never fails to answer the User's requests immediately and with precision. User: Hello, Bob. Bob: Hello. How may I help you today? User: Please tell me the largest city in Europe. Bob: Sure. The largest city in Europe is Moscow, the capital of Russia. User:" Note the use of --color to distinguish between user input and generated text. image Limitations * Not sure if my tokenizer is correct. There are a few places where we might have a mistake: + llama.cpp/convert-pth-to-ggml.py Lines 79 to 87 in 26c0846 # Is this correct?? for i in range(32000): # TODO: this is probably wrong - not sure how this tokenizer works text = tokenizer.decode([29889, i]).encode('utf-8') # remove the first byte (it's always '.') text = text[1:] fout.write(struct.pack("i", len(text))) fout.write(text) + llama.cpp/utils.h Lines 65 to 69 in 26c0846 // TODO: this is probably wrong, but I cannot figure out how this tokenizer works .. // ref: https://github.com/google/sentencepiece std::vector llama_tokenize(const gpt_vocab & vocab, const std::string & text, bool bos); In general, it seems to work, but I think it fails for unicode character support. Hopefully, someone can help with that * I don't know yet how much the quantization affects the quality of the generated text * Probably the token sampling can be improved * The Accelerate framework is actually currently unused since I found that for tensor shapes typical for the Decoder, there is no benefit compared to the ARM_NEON intrinsics implementation. Of course, it's possible that I simlpy don't know how to utilize it properly. But in any case, you can even disable it with LLAMA_NO_ACCELERATE=1 make and the performance will be the same, since no BLAS calls are invoked by the current implementation About Port of Facebook's LLaMA model in C/C++ Resources Readme License MIT license Stars 3k stars Watchers 43 watching Forks 151 forks Releases No releases published Packages 0 No packages published Contributors 13 * @ggerganov * @blackhole89 * @beiller * @simonw * @prusnak * @marckohlbrugge * @bengarney * @wizzard0 * @jooray * @jcelerier * @etra0 + 2 contributors Languages * C 81.5% * C++ 16.2% * Makefile 1.2% * Python 1.1% Footer (c) 2023 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time. You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.