Model Overview
Model Features
Model Capabilities
Use Cases
🚀 Gemma - Ungated Version
Gemma is a family of lightweight, state - of - the - art open models from Google. It can handle various text generation tasks and is suitable for deployment in resource - limited environments, democratizing access to advanced AI models.
🚀 Quick Start
To quickly start using the Gemma model, first ensure you pip install -U transformers
. Then, you can refer to the following code snippets according to your use case.
✨ Features
- Lightweight and Versatile: Well - suited for a variety of text generation tasks, including question answering, summarization, and reasoning.
- Resource - Friendly: Can be deployed in environments with limited resources such as laptops, desktops, or personal cloud infrastructure.
- Improved Performance: The Gemma 1.1 version was trained using a novel RLHF method, leading to substantial gains in quality, coding capabilities, factuality, instruction following, and multi - turn conversation quality.
📦 Installation
Make sure to install the necessary libraries before running the model:
pip install -U transformers
If you want to run the model on a GPU or use quantization, additional libraries may be required:
pip install accelerate
pip install bitsandbytes
pip install flash-attn
💻 Usage Examples
Basic Usage
Running the model on a CPU
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
tokenizer = AutoTokenizer.from_pretrained("google/gemma-1.1-7b-it")
model = AutoModelForCausalLM.from_pretrained(
"google/gemma-1.1-7b-it",
torch_dtype=torch.bfloat16
)
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt")
outputs = model.generate(**input_ids, max_new_tokens=50)
print(tokenizer.decode(outputs[0]))
Advanced Usage
Running the model on a single / multi GPU
# pip install accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
tokenizer = AutoTokenizer.from_pretrained("google/gemma-1.1-7b-it")
model = AutoModelForCausalLM.from_pretrained(
"google/gemma-1.1-7b-it",
device_map="auto",
torch_dtype=torch.bfloat16
)
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))
Quantized Versions through bitsandbytes
Using 8 - bit precision (int8)
# pip install bitsandbytes accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
tokenizer = AutoTokenizer.from_pretrained("google/gemma-1.1-7b-it")
model = AutoModelForCausalLM.from_pretrained(
"google/gemma-1.1-7b-it",
quantization_config=quantization_config
)
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))
Using 4 - bit precision
# pip install bitsandbytes accelerate
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
quantization_config = BitsAndBytesConfig(load_in_4bit=True)
tokenizer = AutoTokenizer.from_pretrained("google/gemma-1.1-7b-it")
model = AutoModelForCausalLM.from_pretrained(
"google/gemma-1.1-7b-it",
quantization_config=quantization_config
)
input_text = "Write me a poem about Machine Learning."
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
outputs = model.generate(**input_ids)
print(tokenizer.decode(outputs[0]))
Other optimizations
Flash Attention 2
First make sure to install flash-attn
in your environment pip install flash-attn
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.float16,
+ attn_implementation="flash_attention_2"
).to(0)
Running the model in JAX / Flax
import jax.numpy as jnp
from transformers import AutoTokenizer, FlaxGemmaForCausalLM
model_id = "google/gemma-1.1-7b-it"
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.padding_side = "left"
model, params = FlaxGemmaForCausalLM.from_pretrained(
model_id,
dtype=jnp.bfloat16,
revision="flax",
_do_init=False,
)
inputs = tokenizer("Valencia and Málaga are", return_tensors="np", padding=True)
output = model.generate(**inputs, params=params, max_new_tokens=20, do_sample=False)
output_text = tokenizer.batch_decode(output.sequences, skip_special_tokens=True)
Check this notebook for a comprehensive walkthrough on how to parallelize JAX inference.
Chat Template
from transformers import AutoTokenizer, AutoModelForCausalLM
import transformers
import torch
model_id = "google/gemma-1.1-7b-it"
dtype = torch.bfloat16
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="cuda",
torch_dtype=dtype,
)
chat = [
{ "role": "user", "content": "Write a hello world program" },
]
prompt = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
At this point, the prompt contains the following text:
<bos><start_of_turn>user
Write a hello world program<end_of_turn>
<start_of_turn>model
After the prompt is ready, generation can be performed like this:
inputs = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
outputs = model.generate(input_ids=inputs.to(model.device), max_new_tokens=150)
📚 Documentation
Model Information
Description
Gemma is a family of lightweight, state - of - the - art open models from Google, built from the same research and technology used to create the Gemini models. They are text - to - text, decoder - only large language models, available in English, with open weights, pre - trained variants, and instruction - tuned variants. Gemma models are well - suited for a variety of text generation tasks, including question answering, summarization, and reasoning. Their relatively small size makes it possible to deploy them in environments with limited resources such as a laptop, desktop or your own cloud infrastructure, democratizing access to state of the art AI models and helping foster innovation for everyone.
Inputs and outputs
Property | Details |
---|---|
Input | Text string, such as a question, a prompt, or a document to be summarized. |
Output | Generated English - language text in response to the input, such as an answer to a question, or a summary of a document. |
Model Data
Training Dataset
These models were trained on a dataset of text data that includes a wide variety of sources, totaling 6 trillion tokens. The key components are:
- Web Documents: A diverse collection of web text ensures the model is exposed to a broad range of linguistic styles, topics, and vocabulary. Primarily English - language content.
- Code: Exposing the model to code helps it to learn the syntax and patterns of programming languages, which improves its ability to generate code or understand code - related questions.
- Mathematics: Training on mathematical text helps the model learn logical reasoning, symbolic representation, and to address mathematical queries.
Data Preprocessing
The key data cleaning and filtering methods applied to the training data are:
- CSAM Filtering: Rigorous CSAM (Child Sexual Abuse Material) filtering was applied at multiple stages in the data preparation process to ensure the exclusion of harmful and illegal content.
- Sensitive Data Filtering: As part of making Gemma pre - trained models safe and reliable, automated techniques were used to filter out certain personal information and other sensitive data from training sets.
- Additional methods: Filtering based on content quality and safety in line with our policies.
Implementation Information
Hardware
Gemma was trained using the latest generation of Tensor Processing Unit (TPU) hardware (TPUv5e). Training large language models requires significant computational power. TPUs, designed specifically for matrix operations common in machine learning, offer several advantages:
- Performance: TPUs are specifically designed to handle the massive computations involved in training LLMs. They can speed up training considerably compared to CPUs.
- Memory: TPUs often come with large amounts of high - bandwidth memory, allowing for the handling of large models and batch sizes during training. This can lead to better model quality.
- Scalability: TPU Pods (large clusters of TPUs) provide a scalable solution for handling the growing complexity of large foundation models. You can distribute training across multiple TPU devices for faster and more efficient processing.
- Cost - effectiveness: In many scenarios, TPUs can provide a more cost - effective solution for training large models compared to CPU - based infrastructure, especially when considering the time and resources saved due to faster training.
- These advantages are aligned with Google's commitments to operate sustainably.
Software
Training was done using JAX and ML Pathways. JAX allows researchers to take advantage of the latest generation of hardware, including TPUs, for faster and more efficient training of large models. ML Pathways is Google's latest effort to build artificially intelligent systems capable of generalizing across multiple tasks. This is specially suitable for foundation models, including large language models like these ones. Together, JAX and ML Pathways are used as described in the paper about the Gemini family of models; "the 'single controller' programming model of Jax and Pathways allows a single Python process to orchestrate the entire training run, dramatically simplifying the development workflow."
Evaluation
Benchmark Results
Benchmark | Metric | Gemma PT 2B | Gemma PT 7B |
---|---|---|---|
MMLU | 5 - shot, top - 1 | 42.3 | 64.3 |
HellaSwag | 0 - shot | 71.4 | 81.2 |
PIQA | 0 - shot | 77.3 | 81.2 |
SocialIQA | 0 - shot | 49.7 | 51.8 |
BoolQ | 0 - shot | 69.4 | 83.2 |
WinoGrande | partial score | 65.4 | 72.3 |
CommonsenseQA | 7 - shot | 65.3 | 71.3 |
OpenBookQA | 47.8 | 52.8 | |
ARC - e | 73.2 | 81.5 | |
ARC - c | 42.1 | 53.2 | |
TriviaQA | 5 - shot | 53.2 | 63.4 |
Natural Questions | 5 - shot | 12.5 | 23.0 |
HumanEval | pass@1 | 22.0 | 32.3 |
MBPP | 3 - shot | 29.2 | 44.4 |
GSM8K | maj@1 | 17.7 | 46.4 |
MATH | 4 - shot | 11.8 | 24.3 |
AGIEval | 24.2 | 41.7 | |
BIG - Bench | 35.2 | 55.1 | |
------------------------------ | ------------- | ----------- | ----------- |
Average | 44.9 | 56.4 |
Ethics and Safety
Evaluation Approach
Our evaluation methods include structured evaluations and internal red - teaming testing of relevant content policies. Red - teaming was conducted by a number of different teams, each with different goals and human evaluation metrics. These models were evaluated against a number of different categories relevant to ethics and safety, including:
- Text - to - Text Content Safety: Human evaluation on prompts covering safety policies including child sexual abuse and exploitation, harassment, violence and gore, and hate speech.
- Text - to - Text Representational Harms: Benchmark against relevant academic datasets such as WinoBias and BBQ Dataset.
- Memorization: Automated evaluation of memorization of training data, including the risk of personally identifiable information exposure.
- Large - scale harm: Tests for "dangerous capabilities," such as chemical, biological, radiological, and nuclear (CBRN) risks.
Evaluation Results
The results of ethics and safety evaluations are within acceptable thresholds for meeting internal policies for categories such as child safety, content safety, representational harms, memorization, large - scale harms. On top of robust internal evaluations, the results of well - known safety benchmarks like BBQ, BOLD, Winogender, Winobias, RealToxicity, and TruthfulQA are shown here.
Gemma 1.0
Benchmark | Metric | Gemma 1.0 IT 2B | Gemma 1.0 IT 7B |
---|---|---|---|
[RealToxicity][realtox] | average | 6.86 | 7.90 |
📄 License
Model Page: Gemma
This model card corresponds to the latest 7B instruct version of the Gemma model. Here you can find other models in the Gemma family:
Base | Instruct | |
---|---|---|
2B | [gemma - 2b](https://huggingface.co/google/gemma - 2b) | [gemma - 1.1 - 2b - it](https://huggingface.co/google/gemma - 1.1 - 2b - it) |
7B | [gemma - 7b](https://huggingface.co/google/gemma - 7b) | [gemma - 1.1 - 7b - it](https://huggingface.co/google/gemma - 1.1 - 7b - it) |
Release Notes
This is Gemma 1.1 7B (IT), an update over the original instruction - tuned Gemma release. Gemma 1.1 was trained using a novel RLHF method, leading to substantial gains on quality, coding capabilities, factuality, instruction following and multi - turn conversation quality. We also fixed a bug in multi - turn conversations, and made sure that model responses don't always start with "Sure,"
. We believe this release represents an improvement for most use cases, but we encourage users to test in their particular applications. The previous model [will continue to be available in the same repo](https://huggingface.co/google/gemma - 7b - it). We appreciate the enthusiastic adoption of Gemma, and we continue to welcome all feedback from the community.
Resources and Technical Documentation:
- Responsible Generative AI Toolkit
- Gemma on Kaggle
- [Gemma on Vertex Model Garden](https://console.cloud.google.com/vertex - ai/publishers/google/model - garden/335)
Authors: Google

