Purpose
gemma.cpp is a lightweight, standalone C++ inference engine released by Google for running Gemma series foundation models. It is positioned as an experimental and research tool, with a core implementation of about 2K lines of code and auxiliary tools of about 4K lines of code, making it easy to embed into other projects and easy to modify. It is not an inference runtime intended for production deployment; Google recommends using the standard paths of Python frameworks such as JAX, Keras, PyTorch, and Transformers for production edge deployment.
Main capabilities
- CPU-only inference: supports Gemma 2, Gemma 3, and PaliGemma 2.
- Sampling: supports TopK and temperature parameters.
- Backpropagation (VJP) and the Adam optimizer, for Gemma research.
- Optimization: mixed-precision (fp8, bf16, fp32, fp64 bit) GEMM, designed for BF16 instructions and able to simulate when those instructions are unavailable; automatically runtime-tunes 7 parameters for each matrix shape; weight compression is integrated directly into GEMM, supports custom fp8 formats (2..3 bit mantissa) and tensor scaling, also supports bf16, f32, and non-uniform 4-bit (NUQ), and makes it easy to add new formats.
- Infrastructure: portable SIMD via the Highway library, with instruction set selected at runtime; supports CCX-aware, multi-way multi-socket thread pool tensor parallelism; disk I/O supports memory mapping or parallel reads (heuristic, user-overridable); custom formats support forward/backward-compatible metadata serialization; the tool for converting models from Safetensors is not yet open source.
- Portability: supports Linux, Windows/OS X, build systems are CMake/Bazel, adapts to any CPU.
- Frontend: C++ API supports streaming output for single-query and batch inference; basic interactive command-line application; basic Python bindings (pybind11).
Usage
System requirements: Clang C++ compiler (at least supporting C++17), and tar for extracting Kaggle archive files. Native Windows builds require Visual Studio 2012 Build Tools and optionally the Clang/LLVM C++ frontend (clang-cl), which can be installed via winget.
Step 1: From Kaggle or Hugging Face Hub, obtain the model weights and tokenizer. On Kaggle's Gemma-2 page, select Model Variations > Gemma C++. bfloat16 weights have higher fidelity, while 8-bit switched floating-point weights allow faster inference; it is usually recommended to start from the -sfp checkpoint, and it is strongly recommended to first get things working with the gemma2-2b-it-sfp model. Gemma 2 model naming: 2B is gemma2-2b-it, and there are also 9b-it or 27b-it.
Step 2: Extract the files. After filling out the consent form, the download will produce the tar archive archive.tar.gz; after extraction you will get the model weight file (such as 2b-it-sfp.sbs) and the tokenizer file (tokenizer.spm), and you can move them to a convenient directory (for example, this repository's build/ directory).
Step 3: Build. Using CMake: create a build directory in the top-level project directory and use cmake to generate the build files. If you have run cmake before and rerun it with different settings, you need to first delete all files under build/ with rm -rf build/*. After running cmake, enter the build/ directory and run make to build the ./gemma executable. Replace [number of parallel threads to use] with an appropriate number of threads, for example make -j4 gemma uses 4 threads; if the nproc command is available, you can use make -j$(nproc) gemma. If you are unsure of the -j value, you can directly run make gemma. WSL users should set the number of parallel threads to 1; larger values may cause errors. After a successful build, the build/ directory will contain the gemma executable (on Windows it is gemma.exe; under Bazel it is gemma in the bazel-bin/ directory). A Makefile alternative is also provided:https://github.com/jart/gemma3/blob/main/Makefile
Afterwards, gemma can be run inside the build/ directory. gemma has required parameters. Example configuration: weight file gemma2-2b-it-sfp.sbs (Gemma2 2B instruction-tuned model, 8-bit switched floating point), tokenizer file tokenizer.spm (can be omitted for single-format weight files created after 2025-05-06 or output by migrate_weights.cc).
PaliGemma vision-language model: this repository contains a C++ implementation of the PaliGemma 2 VLM. Build the gemma binary according to step 3, download the compressed weights and tokenizer from Kaggle, then run it. The image reading code is very basic and currently only supports binary PPM (P6); you can first convert with convert, for example: convert image.jpeg -resize 224x224^ image.ppm
Single-file format migration: weight files now have a new format, and a single file can directly contain the tokenizer (and model type). A tool is provided to migrate from the multi-file format to the single-file format. After migration, the tokenizer parameter can be omitted.
Use cases
gemma.cpp is aimed at experimental and research use cases, and is suitable for developers who want to embed inference with minimal dependencies, or conduct co-design research on high-level algorithms and low-level computation. It can also be integrated into projects as a library: pull gemma.cpp and its dependencies via CMake FetchContent; you can also build a shared library by changing the make invocation to build the libgemma target, producing libgemma.a on Unix platforms. It is already used by independent projects: gemma-cpp-python (Python bindings), lua-cgemma (Lua bindings), and a Godot engine demo project.
Limitations
gemma.cpp is aimed at experimental research and is not a runtime oriented toward production deployment; for production edge deployment, the standard path using Python frameworks is recommended. Currently only CPU inference is supported. Image reading only supports binary PPM (P6). The tool for converting models from Safetensors has not yet been open-sourced. CLI usage is experimental, and context length limits need to be considered. Windows/Visual Studio builds are currently recommended to be done in WSL. A common reason models do not respond to instructions and produce strange output is using a pretrained model that has not been instruction-tuned; you should use an instruction-tuned model (such as gemma2-2b-it-sfp) rather than a pretrained model with the -pt suffix. Regarding sequence length, refer to max_seq_len and InferenceArgs.seq_len in configs.cc; Gemma 3 models larger than 1B are usually 32K, and 128K is also feasible when there is enough memory, but long sequences will be slower due to the quadratic cost of attention. Acceleration methods: use 8-bit switched floating point -sfp models (half the size of bf16, using less memory bandwidth and cache); due to autotuning, the second and especially the third query will be faster; on laptops, make sure the power mode is set to highest performance and turn off power-saving mode (most laptops automatically enable power-saving mode when unplugged); close other unused CPU-intensive applications; on Mac, enabling performance cores will produce a warm-up-like speed improvement. Active development is on the dev branch; please submit PRs to the dev branch rather than main. The project follows Google's open source community guidelines and is not an officially supported Google product.