Introduction
gemma.cpp is Google's official lightweight C++ implementation for running Gemma language models. It prioritizes simplicity and minimal dependencies over maximum throughput, making it a research-friendly alternative to larger inference frameworks. The single-file design and CPU-focused approach enable experimentation on laptops without GPU requirements.
What gemma.cpp Does
- Runs Gemma 2B and 7B models in pure C++ with no external ML framework dependencies
- Provides interactive text generation with streaming token output
- Supports both instruction-tuned (IT) and base pretrained model variants
- Implements core transformer operations using the Highway library for SIMD vectorization
- Enables batch processing and embedding extraction for research workflows
Architecture Overview
The engine implements the Gemma transformer architecture (multi-query attention, GeGLU activations, RMSNorm) in C++ using Google's Highway library for portable SIMD operations. It loads model weights from a custom compressed format (.sbs), performs inference on CPU using matrix multiplication kernels optimized for the host architecture (SSE, AVX, NEON), and generates tokens autoregressively with configurable sampling parameters.
Self-Hosting & Configuration
- Build with CMake on Linux, macOS, or Windows with a C++17 compiler
- Download model weights from Kaggle in the supported .sbs format
- Configure generation parameters (temperature, top-k, top-p) via command-line flags
- Link as a library in custom C++ applications for embedded inference
- Optionally enable OpenMP for multi-threaded matrix operations
Key Features
- Minimal dependencies: only requires a C++17 compiler and CMake to build
- Portable across CPU architectures with SIMD optimization via Highway
- Small binary footprint suitable for embedded and edge deployment
- Research-friendly single-codebase design for understanding transformer internals
- Supports both Gemma 1 and Gemma 2 model architectures
Comparison with Similar Tools
- llama.cpp — broader model support with GGUF format; gemma.cpp is Gemma-specific and Google-maintained
- ONNX Runtime — general-purpose with GPU support; gemma.cpp is CPU-only and much lighter
- Hugging Face Transformers — Python with full ecosystem; gemma.cpp is pure C++ with no Python
- MediaPipe LLM — Google's mobile inference; gemma.cpp targets desktop and research use
- MLC-LLM — compiler-based with broad hardware; gemma.cpp is simpler with manual optimization
FAQ
Q: Why use gemma.cpp instead of llama.cpp for Gemma models? A: gemma.cpp is Google's reference implementation with guaranteed correctness for Gemma architectures. llama.cpp offers broader model support and GPU acceleration.
Q: Does it support GPU acceleration? A: Currently it is CPU-only, focusing on portability and simplicity. For GPU inference, use Gemma via Hugging Face or vLLM.
Q: What are the hardware requirements? A: The 2B model runs comfortably on a laptop with 8GB RAM. The 7B model needs approximately 16GB of available memory.
Q: Can I use it for fine-tuned Gemma models? A: It supports loading custom weights in the .sbs format. You would need to convert fine-tuned weights from other formats.