@@ -6,7 +6,7 @@ foundation models from Google.
66For additional information about Gemma, see
77[ ai.google.dev/gemma] ( https://ai.google.dev/gemma ) . Model weights, including
88gemma.cpp specific artifacts, are
9- [ available on kaggle] ( https://www.kaggle.com/models/google/gemma ) .
9+ [ available on kaggle] ( https://www.kaggle.com/models/google/gemma-2 ) .
1010
1111## Who is this project for?
1212
@@ -18,8 +18,8 @@ deployment-oriented C++ inference runtimes, which are not designed for
1818experimentation, and Python-centric ML research frameworks, which abstract away
1919low-level computation through compilation.
2020
21- gemma.cpp provides a minimalist implementation of Gemma-1, Gemma- 2, Gemma-3, and
22- PaliGemma models, focusing on simplicity and directness rather than full
21+ gemma.cpp provides a minimalist implementation of Gemma-2, Gemma-3, and
22+ PaliGemma-2 models, focusing on simplicity and directness rather than full
2323generality. This is inspired by vertically-integrated model implementations such
2424as [ ggml] ( https://github.com/ggerganov/ggml ) ,
2525[ llama.c] ( https://github.com/karpathy/llama2.c ) , and
@@ -53,7 +53,7 @@ Guidelines](https://opensource.google.com/conduct/).
5353
5454- LLM
5555
56- - CPU-only inference for: Gemma 1 -3, Griffin(SSM), PaliGemma 1- 2.
56+ - CPU-only inference for: Gemma 2 -3, Griffin(SSM), PaliGemma 2.
5757 - Sampling with TopK and temperature.
5858 - Backward pass (VJP) and Adam optimizer for Gemma research.
5959
@@ -106,57 +106,20 @@ winget install --id Microsoft.VisualStudio.2022.BuildTools --force --override "-
106106
107107Visit the
108108[ Kaggle page for Gemma-2] ( https://www.kaggle.com/models/google/gemma-2/gemmaCpp )
109- [ or Gemma-1] ( https://www.kaggle.com/models/google/gemma/frameworks/gemmaCpp ) ,
110109and select ` Model Variations |> Gemma C++ ` .
111110
112111On this tab, the ` Variation ` dropdown includes the options below. Note bfloat16
113112weights are higher fidelity, while 8-bit switched floating point weights enable
114113faster inference. In general, we recommend starting with the ` -sfp ` checkpoints.
115114
116- If you are unsure which model to start with, we recommend starting with the
117- smallest Gemma-2 model, i.e. ` 2.0-2b-it-sfp ` .
118-
119- Alternatively, visit the
120- [ gemma.cpp] ( https://huggingface.co/models?other=gemma.cpp ) models on the Hugging
121- Face Hub. First go the model repository of the model of interest (see
122- recommendations below). Then, click the ` Files and versions ` tab and download
123- the model and tokenizer files. For programmatic downloading, if you have
124- ` huggingface_hub ` installed, you can also download by running:
125-
126- ```
127- huggingface-cli login # Just the first time
128- huggingface-cli download google/gemma-2b-sfp-cpp --local-dir build/
129- ```
130-
131- Gemma-1 2B instruction-tuned (` it ` ) and pre-trained (` pt ` ) models:
132-
133- | Model name | Description |
134- | ----------- | ----------- |
135- | ` 2b-it ` | 2 billion parameter instruction-tuned model, bfloat16 |
136- | ` 2b-it-sfp ` | 2 billion parameter instruction-tuned model, 8-bit switched floating point |
137- | ` 2b-pt ` | 2 billion parameter pre-trained model, bfloat16 |
138- | ` 2b-pt-sfp ` | 2 billion parameter pre-trained model, 8-bit switched floating point |
139-
140- Gemma-1 7B instruction-tuned (` it ` ) and pre-trained (` pt ` ) models:
141-
142- | Model name | Description |
143- | ----------- | ----------- |
144- | ` 7b-it ` | 7 billion parameter instruction-tuned model, bfloat16 |
145- | ` 7b-it-sfp ` | 7 billion parameter instruction-tuned model, 8-bit switched floating point |
146- | ` 7b-pt ` | 7 billion parameter pre-trained model, bfloat16 |
147- | ` 7b-pt-sfp ` | 7 billion parameter pre-trained model, 8-bit switched floating point |
148-
149- > [ !NOTE]
150- > ** Important** : We strongly recommend starting off with the ` 2b-it-sfp ` model to
151- > get up and running.
115+ > [ !NOTE] ** Important** : We strongly recommend starting off with the
116+ > ` gemma2-2b-it-sfp ` model to get up and running.
152117
153118Gemma 2 models are named ` gemma2-2b-it ` for 2B and ` 9b-it ` or ` 27b-it ` . See the
154- ` kModelFlags ` definition in ` common .cc` .
119+ ` ModelPrefix ` function in ` configs .cc` .
155120
156121### Step 2: Extract Files
157122
158- If you downloaded the models from Hugging Face, skip to step 3.
159-
160123After filling out the consent form, the download should proceed to retrieve a
161124tar archive file ` archive.tar.gz ` . Extract files from ` archive.tar.gz ` (this can
162125take a few minutes):
@@ -194,10 +157,9 @@ cmake --build --preset make -j [number of parallel threads to use]
194157```
195158
196159Replace ` [number of parallel threads to use] ` with a number - the number of
197- cores available on your system is a reasonable heuristic. For example,
198- ` make -j4 gemma ` will build using 4 threads. If the ` nproc ` command is
199- available, you can use ` make -j$(nproc) gemma ` as a reasonable default
200- for the number of threads.
160+ cores available on your system is a reasonable heuristic. For example, `make -j4
161+ gemma` will build using 4 threads. If the ` nproc` command is available, you can
162+ use ` make -j$(nproc) gemma ` as a reasonable default for the number of threads.
201163
202164If you aren't sure of the right value for the ` -j ` flag, you can simply run
203165` make gemma ` instead and it should still build the ` ./gemma ` executable.
@@ -206,7 +168,8 @@ If you aren't sure of the right value for the `-j` flag, you can simply run
206168> On Windows Subsystem for Linux (WSL) users should set the number of
207169> parallel threads to 1. Using a larger number may result in errors.
208170
209- If the build is successful, you should now have a ` gemma ` executable in the ` build/ ` directory.
171+ If the build is successful, you should now have a ` gemma ` executable in the
172+ ` build/ ` directory.
210173
211174#### Windows
212175
@@ -218,15 +181,17 @@ cmake --preset windows
218181cmake --build --preset windows -j [number of parallel threads to use]
219182```
220183
221- If the build is successful, you should now have a ` gemma.exe ` executable in the ` build/ ` directory.
184+ If the build is successful, you should now have a ` gemma.exe ` executable in the
185+ ` build/ ` directory.
222186
223187#### Bazel
224188
225189``` sh
226190bazel build -c opt --cxxopt=-std=c++20 :gemma
227191```
228192
229- If the build is successful, you should now have a ` gemma ` executable in the ` bazel-bin/ ` directory.
193+ If the build is successful, you should now have a ` gemma ` executable in the
194+ ` bazel-bin/ ` directory.
230195
231196#### Make
232197
@@ -240,33 +205,21 @@ You can now run `gemma` from inside the `build/` directory.
240205
241206` gemma ` has the following required arguments:
242207
243- Argument | Description | Example value
244- --------------- | ---------------------------- | -----------------------
245- ` --model ` | The model type. | ` 2b-it ` ... (see below)
246- ` --weights ` | The compressed weights file. | ` 2b-it-sfp.sbs `
247- ` --weight_type ` | The compressed weight type. | ` sfp `
248- ` --tokenizer ` | The tokenizer file. | ` tokenizer.spm `
249-
250- ` gemma ` is invoked as:
251-
252- ``` sh
253- ./gemma \
254- --tokenizer [tokenizer file] \
255- --weights [compressed weights file] \
256- --weight_type [f32 or bf16 or sfp (default:sfp)] \
257- --model [2b-it or 2b-pt or 7b-it or 7b-pt or ...]
258- ```
208+ Argument | Description | Example value
209+ ------------- | ---------------------------- | ---------------
210+ ` --weights ` | The compressed weights file. | ` 2b-it-sfp.sbs `
211+ ` --tokenizer ` | The tokenizer file. | ` tokenizer.spm `
259212
260213Example invocation for the following configuration:
261214
262- - Compressed weights file ` 2b-it-sfp.sbs ` (2B instruction-tuned model, 8-bit
263- switched floating point).
264- - Tokenizer file ` tokenizer.spm ` .
215+ - weights file ` gemma2-2b-it-sfp.sbs ` (Gemma2 2B instruction-tuned model,
216+ 8-bit switched floating point).
217+ - Tokenizer file ` tokenizer.spm ` (can omit for single-format weights files
218+ created after 2025-05-06, or output by migrate_weights.cc).
265219
266220``` sh
267221./gemma \
268- --tokenizer tokenizer.spm \
269- --weights 2b-it-sfp.sbs --model 2b-it
222+ --tokenizer tokenizer.spm --weights gemma2-2b-it-sfp.sbs
270223```
271224
272225### RecurrentGemma
@@ -288,11 +241,9 @@ Step 1, and run the binary as follows:
288241
289242### PaliGemma Vision-Language Model
290243
291- This repository includes a version of the PaliGemma VLM
292- ([ paper] ( https://arxiv.org/abs/2407.07726 ) ,
293- [ code] ( https://github.com/google-research/big_vision/tree/main/big_vision/configs/proj/paligemma ) )
294- and its successor PaliGemma 2 ([ paper] ( https://arxiv.org/abs/2412.03555 ) ). We
295- provide a C++ implementation of the PaliGemma model family here.
244+ This repository includes a version of the PaliGemma 2 VLM
245+ ([ paper] ( https://arxiv.org/abs/2412.03555 ) ). We provide a C++ implementation of
246+ the PaliGemma 2 model here.
296247
297248To use the version of PaliGemma included in this repository, build the gemma
298249binary as noted above in Step 3. Download the compressed weights and tokenizer
@@ -303,8 +254,7 @@ and run the binary as follows:
303254``` sh
304255./gemma \
305256--tokenizer paligemma_tokenizer.model \
306- --model paligemma-224 \
307- --weights paligemma-3b-mix-224-sfp.sbs \
257+ --weights paligemma2-3b-mix-224-sfp.sbs \
308258--image_file paligemma/testdata/image.ppm
309259```
310260
@@ -346,26 +296,17 @@ from the multi-file format to the single-file format is available.
346296``` sh
347297io/migrate_weights \
348298 --tokenizer .../tokenizer.spm --weights .../gemma2-2b-it-sfp.sbs \
349- --model gemma2-2b-it -- output_weights .../gemma2-2b-it-sfp-single.sbs
299+ --output_weights .../gemma2-2b-it-sfp-single.sbs
350300```
351301
352- After migration, you can use the new weights file with gemma.cpp like this:
302+ After migration, you can omit the tokenizer argument like this:
353303
354304``` sh
355305./gemma --weights .../gemma2-2b-it-sfp-single.sbs
356306```
357307
358308### Troubleshooting and FAQs
359309
360- ** Running ` ./gemma ` fails with "Failed to read cache gating_ein_0 (error 294) ..."**
361-
362- The most common problem is that the ` --weight_type ` argument does not match that
363- of the model file. Revisit step #3 and check which weights you downloaded.
364-
365- Note that we have already moved weight type from a compile-time decision to a
366- runtime argument. In a subsequent step, we plan to bake this information into
367- the weights.
368-
369310** Problems building in Windows / Visual Studio**
370311
371312Currently if you're using Windows, we recommend building in WSL (Windows
@@ -376,8 +317,8 @@ configurations, see issues for active discussion.
376317
377318A common issue is that you are using a pre-trained model, which is not
378319instruction-tuned and thus does not respond to instructions. Make sure you are
379- using an instruction-tuned model (` 2b-it-sfp ` , ` 2b-it ` , ` 7b-it-sfp ` , ` 7b-it ` )
380- and not a pre-trained model (any model with a ` -pt ` suffix).
320+ using an instruction-tuned model (` gemma2- 2b-it-sfp` ) and not a pre-trained
321+ model (any model with a ` -pt ` suffix).
381322
382323** What sequence lengths are supported?**
383324
@@ -387,11 +328,10 @@ sequences will be slow due to the quadratic cost of attention.
387328
388329** How do I convert my fine-tune to a ` .sbs ` compressed model file?**
389330
390- For PaliGemma (1 and 2) checkpoints, you can use
391- python/convert_from_safetensors.py to convert from safetensors format (tested
392- with building via bazel). For an adapter model, you will likely need to call
393- merge_and_unload() to convert the adapter model to a single-file format before
394- converting it.
331+ For PaliGemma 2 checkpoints, you can use python/convert_from_safetensors.py to
332+ convert from safetensors format (tested with building via bazel). For an adapter
333+ model, you will likely need to call merge_and_unload() to convert the adapter
334+ model to a single-file format before converting it.
395335
396336Here is how to use it using a bazel build of the compression library assuming
397337locally installed (venv) torch, numpy, safetensors, absl-py, etc.:
@@ -405,22 +345,18 @@ ln -s $BAZEL_OUTPUT_DIR [...]/site-packages/compression
405345python3 python/convert_from_safetensors.py --load_path [...].safetensors.index.json
406346```
407347
408- See also compression/convert_weights.py for a slightly older option to convert a
409- pytorch checkpoint. (The code may need updates to work with Gemma-2 models.)
410-
411348** What are some easy ways to make the model run faster?**
412349
4133501 . Make sure you are using the 8-bit switched floating point ` -sfp ` models.
414351 These are half the size of bf16 and thus use less memory bandwidth and cache
415352 space.
416- 2 . If you're on a laptop, make sure power mode is set to maximize performance
353+ 2 . Due to auto-tuning, the second and especially third query will be faster.
354+ 3 . If you're on a laptop, make sure power mode is set to maximize performance
417355 and saving mode is ** off** . For most laptops, the power saving modes get
418356 activated automatically if the computer is not plugged in.
419- 3 . Close other unused cpu-intensive applications.
420- 4 . On macs, anecdotally we observe a "warm-up" ramp-up in speed as performance
357+ 4 . Close other unused cpu-intensive applications.
358+ 5 . On macs, anecdotally we observe a "warm-up" ramp-up in speed as performance
421359 cores get engaged.
422- 5 . Experiment with the ` --num_threads ` argument value. Depending on the device,
423- larger numbers don't always mean better performance.
424360
425361We're also working on algorithmic and optimization approaches for faster
426362inference, stay tuned.
@@ -452,10 +388,7 @@ $ ./gemma [...]
452388 __/ | | | | |
453389 | ___/ | _| | _|
454390
455- tokenizer : tokenizer.spm
456- weights : 2b-it-sfp.sbs
457- model : 2b-it
458- max_generated_tokens : 2048
391+ ...
459392
460393* Usage*
461394 Enter an instruction and press enter (%C reset conversation, %Q quits).
@@ -493,7 +426,7 @@ For using the `gemma` executable as a command line tool, it may be useful to
493426create an alias for gemma.cpp with arguments fully specified:
494427
495428` ` ` sh
496- alias gemma2b=" ~/gemma.cpp/build/gemma -- --tokenizer ~/gemma.cpp/build/tokenizer.spm --weights ~/gemma.cpp/build/gemma2-2b-it-sfp.sbs --model gemma2-2b-it -- verbosity 0"
429+ alias gemma2b=" ~/gemma.cpp/build/gemma -- --tokenizer ~/gemma.cpp/build/tokenizer.spm --weights ~/gemma.cpp/build/gemma2-2b-it-sfp.sbs --verbosity 0"
497430` ` `
498431
499432Replace the above paths with your own paths to the model and tokenizer paths
@@ -523,8 +456,8 @@ Let's break down the code:
523456### Incorporating gemma.cpp as a Library in your Project
524457
525458The easiest way to incorporate gemma.cpp in your own project is to pull in
526- gemma.cpp and dependencies using `FetchContent`. You can add the following to your
527- CMakeLists.txt:
459+ gemma.cpp and dependencies using `FetchContent`. You can add the following to
460+ your CMakeLists.txt:
528461
529462```
530463include(FetchContent)
@@ -593,9 +526,10 @@ submit a PR with a `README.md` edit.
593526
594527## Acknowledgements and Contacts
595528
596- gemma.cpp was started in fall 2023 by [Austin Huang](mailto:austinvhuang@google.com)
597- and [Jan Wassenberg](mailto:janwas@google.com), and subsequently released February 2024
598- thanks to contributions from Phil Culliton, Paul Chang, and Dan Zheng.
529+ gemma.cpp was started in fall 2023 by
530+ [Austin Huang](mailto:austinvhuang@google.com) and
531+ [Jan Wassenberg](mailto:janwas@google.com), and subsequently released February
532+ 2024 thanks to contributions from Phil Culliton, Paul Chang, and Dan Zheng.
599533
600534Griffin support was implemented in April 2024 thanks to contributions by Andrey
601535Mikhaylov, Eugene Kliuchnikov, Jan Wassenberg, Jyrki Alakuijala, Lode
0 commit comments