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other
|
3 |
+
quantized_by: jartine
|
4 |
+
license_link: LICENSE
|
5 |
+
library_name: transformers
|
6 |
+
base_model: google/gemma-2-27b-it
|
7 |
+
prompt_template: |
|
8 |
+
<start_of_turn>system
|
9 |
+
{{prompt}}<end_of_turn>
|
10 |
+
{{history}}
|
11 |
+
<start_of_turn>{{char}}
|
12 |
+
history_template: |
|
13 |
+
<start_of_turn>{{name}}
|
14 |
+
{{message}}<end_of_turn>
|
15 |
+
tags:
|
16 |
+
- llamafile
|
17 |
+
---
|
18 |
+
|
19 |
+
# Gemma v2 27b Instruct - llamafile
|
20 |
+
|
21 |
+
Gemma v2 is a large language model released by Google on Jun 27th 2024.
|
22 |
+
|
23 |
+
- Model creator: [Google](https://huggingface.co/google/)
|
24 |
+
- Original model: [google/gemma-2-27b-it](https://huggingface.co/google/gemma-2-27b-it)
|
25 |
+
|
26 |
+
The model is packaged into executable weights, which we call
|
27 |
+
[llamafiles](https://github.com/Mozilla-Ocho/llamafile). This makes it
|
28 |
+
easy to use the model on Linux, MacOS, Windows, FreeBSD, OpenBSD 7.3,
|
29 |
+
and NetBSD for AMD64 and ARM64.
|
30 |
+
|
31 |
+
*Software Last Updated: 2024-11-01*
|
32 |
+
|
33 |
+
## Quickstart
|
34 |
+
|
35 |
+
To get started, you need both the Gemma weights, and the llamafile
|
36 |
+
software. Both of them are included in a single file, which can be
|
37 |
+
downloaded and run as follows:
|
38 |
+
|
39 |
+
```
|
40 |
+
wget https://huggingface.co/Mozilla/gemma-2-27b-it-llamafile/resolve/main/gemma-2-27b-it.Q6_K.llamafile
|
41 |
+
chmod +x gemma-2-27b-it.Q6_K.llamafile
|
42 |
+
./gemma-2-27b-it.Q6_K.llamafile
|
43 |
+
```
|
44 |
+
|
45 |
+
The default mode of operation for these llamafiles is our new command
|
46 |
+
line chatbot interface.
|
47 |
+
|
48 |
+
![Screenshot of Gemma 2b llamafile on MacOS](llamafile-gemma.png)
|
49 |
+
|
50 |
+
Having **trouble?** See the ["Gotchas"
|
51 |
+
section](https://github.com/mozilla-ocho/llamafile/?tab=readme-ov-file#gotchas-and-troubleshooting)
|
52 |
+
of the README.
|
53 |
+
|
54 |
+
## Usage
|
55 |
+
|
56 |
+
By default, llamafile launches a chatbot in the terminal, and a server
|
57 |
+
in the background. The chatbot is mostly self-explanatory. You can type
|
58 |
+
`/help` for further details. See the [llamafile v0.8.15 release
|
59 |
+
notes](https://github.com/Mozilla-Ocho/llamafile/releases/tag/0.8.15)
|
60 |
+
for documentation on our newest chatbot features.
|
61 |
+
|
62 |
+
To instruct Gemma to do role playing, you can customize the system
|
63 |
+
prompt as follows:
|
64 |
+
|
65 |
+
```
|
66 |
+
./gemma-2-27b-it.Q6_K.llamafile --chat -p "you are mosaic's godzilla"
|
67 |
+
```
|
68 |
+
|
69 |
+
To view the man page, run:
|
70 |
+
|
71 |
+
```
|
72 |
+
./gemma-2-27b-it.Q6_K.llamafile --help
|
73 |
+
```
|
74 |
+
|
75 |
+
To send a request to the OpenAI API compatible llamafile server, try:
|
76 |
+
|
77 |
+
```
|
78 |
+
curl http://localhost:8080/v1/chat/completions \
|
79 |
+
-H "Content-Type: application/json" \
|
80 |
+
-d '{
|
81 |
+
"model": "gemma-27b-it",
|
82 |
+
"messages": [{"role": "user", "content": "Say this is a test!"}],
|
83 |
+
"temperature": 0.0
|
84 |
+
}'
|
85 |
+
```
|
86 |
+
|
87 |
+
If you don't want the chatbot and you only want to run the server:
|
88 |
+
|
89 |
+
```
|
90 |
+
./gemma-2-27b-it.Q6_K.llamafile --server --nobrowser --host 0.0.0.0
|
91 |
+
```
|
92 |
+
|
93 |
+
An advanced CLI mode is provided that's useful for shell scripting. You
|
94 |
+
can use it by passing the `--cli` flag. For additional help on how it
|
95 |
+
may be used, pass the `--help` flag.
|
96 |
+
|
97 |
+
```
|
98 |
+
./gemma-2-27b-it.Q6_K.llamafile --cli -p 'four score and seven' --log-disable
|
99 |
+
```
|
100 |
+
|
101 |
+
You then need to fill out the prompt / history template (see below).
|
102 |
+
|
103 |
+
For further information, please see the [llamafile
|
104 |
+
README](https://github.com/mozilla-ocho/llamafile/).
|
105 |
+
|
106 |
+
## Troubleshooting
|
107 |
+
|
108 |
+
Having **trouble?** See the ["Gotchas"
|
109 |
+
section](https://github.com/mozilla-ocho/llamafile/?tab=readme-ov-file#gotchas-and-troubleshooting)
|
110 |
+
of the README.
|
111 |
+
|
112 |
+
On Linux, the way to avoid run-detector errors is to install the APE
|
113 |
+
interpreter.
|
114 |
+
|
115 |
+
```sh
|
116 |
+
sudo wget -O /usr/bin/ape https://cosmo.zip/pub/cosmos/bin/ape-$(uname -m).elf
|
117 |
+
sudo chmod +x /usr/bin/ape
|
118 |
+
sudo sh -c "echo ':APE:M::MZqFpD::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register"
|
119 |
+
sudo sh -c "echo ':APE-jart:M::jartsr::/usr/bin/ape:' >/proc/sys/fs/binfmt_misc/register"
|
120 |
+
```
|
121 |
+
|
122 |
+
On Windows there's a 4GB limit on executable sizes. This means you
|
123 |
+
should download the Q2\_K llamafile. For better quality, consider
|
124 |
+
instead downloading the official llamafile release binary from
|
125 |
+
<https://github.com/Mozilla-Ocho/llamafile/releases>, renaming it to
|
126 |
+
have the .exe file extension, and then saying:
|
127 |
+
|
128 |
+
```
|
129 |
+
.\llamafile-0.8.15.exe -m gemma-2-27b-it.Q6_K.llamafile
|
130 |
+
```
|
131 |
+
|
132 |
+
That will overcome the Windows 4GB file size limit, allowing you to
|
133 |
+
benefit from bigger better models.
|
134 |
+
|
135 |
+
## Context Window
|
136 |
+
|
137 |
+
This model has a max context window size of 8k tokens. By default, a
|
138 |
+
context window size of 8192 tokens is used. You may limit the context
|
139 |
+
window size by passing the `-c N` flag.
|
140 |
+
|
141 |
+
## GPU Acceleration
|
142 |
+
|
143 |
+
On GPUs with sufficient RAM, the `-ngl 999` flag may be passed to use
|
144 |
+
the system's NVIDIA or AMD GPU(s). On Windows, only the graphics card
|
145 |
+
driver needs to be installed if you own an NVIDIA GPU. On Windows, if
|
146 |
+
you have an AMD GPU, you should install the ROCm SDK v6.1 and then pass
|
147 |
+
the flags `--recompile --gpu amd` the first time you run your llamafile.
|
148 |
+
|
149 |
+
On NVIDIA GPUs, by default, the prebuilt tinyBLAS library is used to
|
150 |
+
perform matrix multiplications. This is open source software, but it
|
151 |
+
doesn't go as fast as closed source cuBLAS. If you have the CUDA SDK
|
152 |
+
installed on your system, then you can pass the `--recompile` flag to
|
153 |
+
build a GGML CUDA library just for your system that uses cuBLAS. This
|
154 |
+
ensures you get maximum performance.
|
155 |
+
|
156 |
+
For further information, please see the [llamafile
|
157 |
+
README](https://github.com/mozilla-ocho/llamafile/).
|
158 |
+
|
159 |
+
## About Upload Limits
|
160 |
+
|
161 |
+
Files which exceed the Hugging Face 50GB upload limit have a .cat𝑋
|
162 |
+
extension. You need to use the `cat` command locally to turn them back
|
163 |
+
into a single file, using the same order.
|
164 |
+
|
165 |
+
## About llamafile
|
166 |
+
|
167 |
+
llamafile is a new format introduced by Mozilla on Nov 20th 2023. It
|
168 |
+
uses Cosmopolitan Libc to turn LLM weights into runnable llama.cpp
|
169 |
+
binaries that run on the stock installs of six OSes for both ARM64 and
|
170 |
+
AMD64.
|
171 |
+
|
172 |
+
## About Quantization Formats
|
173 |
+
|
174 |
+
This model works well with any quantization format. Q6\_K is the best
|
175 |
+
choice overall here.
|
176 |
+
|
177 |
+
## Testing
|
178 |
+
|
179 |
+
We tested that the gemma2 27b q6\_k llamafile produces nearly identical
|
180 |
+
responses to the Gemma2 model hosted by Google on aistudio.google.com
|
181 |
+
when temperature is set to zero.
|
182 |
+
|
183 |
+
![screenshot of llamafile producing same output as google's hosted gemma service](gemma-proof.png)
|
184 |
+
|
185 |
+
Therefore, it is our belief, that the llamafile software faithfully
|
186 |
+
implements the gemma model. If you should encounter any divergences,
|
187 |
+
then try using the BF16 weights, which have the original fidelity.
|
188 |
+
|
189 |
+
## See Also
|
190 |
+
|
191 |
+
- <https://huggingface.co/Mozilla/gemma-2-2b-it-llamafile>
|
192 |
+
- <https://huggingface.co/Mozilla/gemma-2-9b-it-llamafile>
|
193 |
+
|
194 |
+
## License
|
195 |
+
|
196 |
+
The llamafile software is open source and permissively licensed. However
|
197 |
+
the weights embedded inside the llamafiles are governed by Google's
|
198 |
+
Gemma License and Gemma Prohibited Use Policy. See the
|
199 |
+
[LICENSE](LICENSE) file for further details.
|
200 |
+
|
201 |
+
---
|
202 |
+
|
203 |
+
# Gemma 2 model card
|
204 |
+
|
205 |
+
**Model Page**: [Gemma](https://ai.google.dev/gemma/docs)
|
206 |
+
|
207 |
+
**Resources and Technical Documentation**:
|
208 |
+
|
209 |
+
* [Responsible Generative AI Toolkit][rai-toolkit]
|
210 |
+
* [Gemma on Kaggle][kaggle-gemma]
|
211 |
+
* [Gemma on Vertex Model Garden][vertex-mg-gemma]
|
212 |
+
|
213 |
+
**Terms of Use**: [Terms](https://www.kaggle.com/models/google/gemma/license/consent/verify/huggingface?returnModelRepoId=google/gemma-2-27b-it)
|
214 |
+
|
215 |
+
**Authors**: Google
|
216 |
+
|
217 |
+
## Model Information
|
218 |
+
|
219 |
+
Summary description and brief definition of inputs and outputs.
|
220 |
+
|
221 |
+
### Description
|
222 |
+
|
223 |
+
Gemma is a family of lightweight, state-of-the-art open models from Google,
|
224 |
+
built from the same research and technology used to create the Gemini models.
|
225 |
+
They are text-to-text, decoder-only large language models, available in English,
|
226 |
+
with open weights for both pre-trained variants and instruction-tuned variants.
|
227 |
+
Gemma models are well-suited for a variety of text generation tasks, including
|
228 |
+
question answering, summarization, and reasoning. Their relatively small size
|
229 |
+
makes it possible to deploy them in environments with limited resources such as
|
230 |
+
a laptop, desktop or your own cloud infrastructure, democratizing access to
|
231 |
+
state of the art AI models and helping foster innovation for everyone.
|
232 |
+
|
233 |
+
### Usage
|
234 |
+
|
235 |
+
Below we share some code snippets on how to get quickly started with running the model. First make sure to `pip install -U transformers`, then copy the snippet from the section that is relevant for your usecase.
|
236 |
+
|
237 |
+
|
238 |
+
#### Running the model on a single / multi GPU
|
239 |
+
|
240 |
+
|
241 |
+
```python
|
242 |
+
# pip install accelerate
|
243 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
244 |
+
import torch
|
245 |
+
|
246 |
+
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-27b-it")
|
247 |
+
model = AutoModelForCausalLM.from_pretrained(
|
248 |
+
"google/gemma-2-27b-it",
|
249 |
+
device_map="auto",
|
250 |
+
torch_dtype=torch.bfloat16
|
251 |
+
)
|
252 |
+
|
253 |
+
input_text = "Write me a poem about Machine Learning."
|
254 |
+
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
|
255 |
+
|
256 |
+
outputs = model.generate(**input_ids)
|
257 |
+
print(tokenizer.decode(outputs[0]))
|
258 |
+
```
|
259 |
+
|
260 |
+
<a name="precisions"></a>
|
261 |
+
#### Running the model on a GPU using different precisions
|
262 |
+
|
263 |
+
The native weights of this model were exported in `bfloat16` precision. You can use `float16`, which may be faster on certain hardware, indicating the `torch_dtype` when loading the model. For convenience, the `float16` revision of the repo contains a copy of the weights already converted to that precision.
|
264 |
+
|
265 |
+
You can also use `float32` if you skip the dtype, but no precision increase will occur (model weights will just be upcasted to `float32`). See examples below.
|
266 |
+
|
267 |
+
* _Using `torch.float16`_
|
268 |
+
|
269 |
+
```python
|
270 |
+
# pip install accelerate
|
271 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
272 |
+
import torch
|
273 |
+
|
274 |
+
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-27b-it")
|
275 |
+
model = AutoModelForCausalLM.from_pretrained(
|
276 |
+
"google/gemma-2-27b-it",
|
277 |
+
device_map="auto",
|
278 |
+
torch_dtype=torch.float16,
|
279 |
+
revision="float16",
|
280 |
+
)
|
281 |
+
|
282 |
+
input_text = "Write me a poem about Machine Learning."
|
283 |
+
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
|
284 |
+
|
285 |
+
outputs = model.generate(**input_ids)
|
286 |
+
print(tokenizer.decode(outputs[0]))
|
287 |
+
```
|
288 |
+
|
289 |
+
* _Using `torch.bfloat16`_
|
290 |
+
|
291 |
+
```python
|
292 |
+
# pip install accelerate
|
293 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
294 |
+
|
295 |
+
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-27b-it")
|
296 |
+
model = AutoModelForCausalLM.from_pretrained(
|
297 |
+
"google/gemma-2-27b-it",
|
298 |
+
device_map="auto",
|
299 |
+
torch_dtype=torch.bfloat16)
|
300 |
+
|
301 |
+
input_text = "Write me a poem about Machine Learning."
|
302 |
+
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
|
303 |
+
|
304 |
+
outputs = model.generate(**input_ids)
|
305 |
+
print(tokenizer.decode(outputs[0]))
|
306 |
+
```
|
307 |
+
|
308 |
+
* _Upcasting to `torch.float32`_
|
309 |
+
|
310 |
+
```python
|
311 |
+
# pip install accelerate
|
312 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
313 |
+
|
314 |
+
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-27b-it")
|
315 |
+
model = AutoModelForCausalLM.from_pretrained(
|
316 |
+
"google/gemma-2-27b-it",
|
317 |
+
device_map="auto"
|
318 |
+
)
|
319 |
+
|
320 |
+
input_text = "Write me a poem about Machine Learning."
|
321 |
+
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
|
322 |
+
|
323 |
+
outputs = model.generate(**input_ids)
|
324 |
+
print(tokenizer.decode(outputs[0]))
|
325 |
+
```
|
326 |
+
|
327 |
+
#### Quantized Versions through `bitsandbytes`
|
328 |
+
|
329 |
+
* _Using 8-bit precision (int8)_
|
330 |
+
|
331 |
+
```python
|
332 |
+
# pip install bitsandbytes accelerate
|
333 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
334 |
+
|
335 |
+
quantization_config = BitsAndBytesConfig(load_in_8bit=True)
|
336 |
+
|
337 |
+
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-27b-it")
|
338 |
+
model = AutoModelForCausalLM.from_pretrained(
|
339 |
+
"google/gemma-2-27b-it",
|
340 |
+
quantization_config=quantization_config)
|
341 |
+
|
342 |
+
input_text = "Write me a poem about Machine Learning."
|
343 |
+
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
|
344 |
+
|
345 |
+
outputs = model.generate(**input_ids)
|
346 |
+
print(tokenizer.decode(outputs[0]))
|
347 |
+
```
|
348 |
+
|
349 |
+
* _Using 4-bit precision_
|
350 |
+
|
351 |
+
```python
|
352 |
+
# pip install bitsandbytes accelerate
|
353 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
354 |
+
|
355 |
+
quantization_config = BitsAndBytesConfig(load_in_4bit=True)
|
356 |
+
|
357 |
+
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2-27b-it")
|
358 |
+
model = AutoModelForCausalLM.from_pretrained(
|
359 |
+
"google/gemma-2-27b-it",
|
360 |
+
quantization_config=quantization_config)
|
361 |
+
|
362 |
+
input_text = "Write me a poem about Machine Learning."
|
363 |
+
input_ids = tokenizer(input_text, return_tensors="pt").to("cuda")
|
364 |
+
|
365 |
+
outputs = model.generate(**input_ids)
|
366 |
+
print(tokenizer.decode(outputs[0]))
|
367 |
+
```
|
368 |
+
|
369 |
+
|
370 |
+
#### Other optimizations
|
371 |
+
|
372 |
+
* _Flash Attention 2_
|
373 |
+
|
374 |
+
First make sure to install `flash-attn` in your environment `pip install flash-attn`
|
375 |
+
|
376 |
+
```diff
|
377 |
+
model = AutoModelForCausalLM.from_pretrained(
|
378 |
+
model_id,
|
379 |
+
torch_dtype=torch.float16,
|
380 |
+
+ attn_implementation="flash_attention_2"
|
381 |
+
).to(0)
|
382 |
+
```
|
383 |
+
|
384 |
+
### Chat Template
|
385 |
+
|
386 |
+
The instruction-tuned models use a chat template that must be adhered to for conversational use.
|
387 |
+
The easiest way to apply it is using the tokenizer's built-in chat template, as shown in the following snippet.
|
388 |
+
|
389 |
+
Let's load the model and apply the chat template to a conversation. In this example, we'll start with a single user interaction:
|
390 |
+
|
391 |
+
```py
|
392 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
393 |
+
import transformers
|
394 |
+
import torch
|
395 |
+
|
396 |
+
model_id = "google/gemma-2-27b-it"
|
397 |
+
dtype = torch.bfloat16
|
398 |
+
|
399 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
400 |
+
model = AutoModelForCausalLM.from_pretrained(
|
401 |
+
model_id,
|
402 |
+
device_map="cuda",
|
403 |
+
torch_dtype=dtype,
|
404 |
+
)
|
405 |
+
|
406 |
+
chat = [
|
407 |
+
{ "role": "user", "content": "Write a hello world program" },
|
408 |
+
]
|
409 |
+
prompt = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
|
410 |
+
```
|
411 |
+
|
412 |
+
At this point, the prompt contains the following text:
|
413 |
+
|
414 |
+
```
|
415 |
+
<bos><start_of_turn>user
|
416 |
+
Write a hello world program<end_of_turn>
|
417 |
+
<start_of_turn>model
|
418 |
+
```
|
419 |
+
|
420 |
+
As you can see, each turn is preceded by a `<start_of_turn>` delimiter and then the role of the entity
|
421 |
+
(either `user`, for content supplied by the user, or `model` for LLM responses). Turns finish with
|
422 |
+
the `<end_of_turn>` token.
|
423 |
+
|
424 |
+
You can follow this format to build the prompt manually, if you need to do it without the tokenizer's
|
425 |
+
chat template.
|
426 |
+
|
427 |
+
After the prompt is ready, generation can be performed like this:
|
428 |
+
|
429 |
+
```py
|
430 |
+
inputs = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
|
431 |
+
outputs = model.generate(input_ids=inputs.to(model.device), max_new_tokens=150)
|
432 |
+
print(tokenizer.decode(outputs[0]))
|
433 |
+
```
|
434 |
+
|
435 |
+
### Inputs and outputs
|
436 |
+
|
437 |
+
* **Input:** Text string, such as a question, a prompt, or a document to be
|
438 |
+
summarized.
|
439 |
+
* **Output:** Generated English-language text in response to the input, such
|
440 |
+
as an answer to a question, or a summary of a document.
|
441 |
+
|
442 |
+
### Citation
|
443 |
+
|
444 |
+
```none
|
445 |
+
@article{gemma_2024,
|
446 |
+
title={Gemma},
|
447 |
+
url={https://www.kaggle.com/m/3301},
|
448 |
+
DOI={10.34740/KAGGLE/M/3301},
|
449 |
+
publisher={Kaggle},
|
450 |
+
author={Gemma Team},
|
451 |
+
year={2024}
|
452 |
+
}
|
453 |
+
```
|
454 |
+
|
455 |
+
## Model Data
|
456 |
+
|
457 |
+
Data used for model training and how the data was processed.
|
458 |
+
|
459 |
+
### Training Dataset
|
460 |
+
|
461 |
+
These models were trained on a dataset of text data that includes a wide variety of sources. The 27B model was trained with 13 trillion tokens and the 9B model was trained with 8 trillion tokens.
|
462 |
+
Here are the key components:
|
463 |
+
|
464 |
+
* Web Documents: A diverse collection of web text ensures the model is exposed
|
465 |
+
to a broad range of linguistic styles, topics, and vocabulary. Primarily
|
466 |
+
English-language content.
|
467 |
+
* Code: Exposing the model to code helps it to learn the syntax and patterns of
|
468 |
+
programming languages, which improves its ability to generate code or
|
469 |
+
understand code-related questions.
|
470 |
+
* Mathematics: Training on mathematical text helps the model learn logical
|
471 |
+
reasoning, symbolic representation, and to address mathematical queries.
|
472 |
+
|
473 |
+
The combination of these diverse data sources is crucial for training a powerful
|
474 |
+
language model that can handle a wide variety of different tasks and text
|
475 |
+
formats.
|
476 |
+
|
477 |
+
### Data Preprocessing
|
478 |
+
|
479 |
+
Here are the key data cleaning and filtering methods applied to the training
|
480 |
+
data:
|
481 |
+
|
482 |
+
* CSAM Filtering: Rigorous CSAM (Child Sexual Abuse Material) filtering was
|
483 |
+
applied at multiple stages in the data preparation process to ensure the
|
484 |
+
exclusion of harmful and illegal content.
|
485 |
+
* Sensitive Data Filtering: As part of making Gemma pre-trained models safe and
|
486 |
+
reliable, automated techniques were used to filter out certain personal
|
487 |
+
information and other sensitive data from training sets.
|
488 |
+
* Additional methods: Filtering based on content quality and safety in line with
|
489 |
+
[our policies][safety-policies].
|
490 |
+
|
491 |
+
## Implementation Information
|
492 |
+
|
493 |
+
Details about the model internals.
|
494 |
+
|
495 |
+
### Hardware
|
496 |
+
|
497 |
+
Gemma was trained using the latest generation of
|
498 |
+
[Tensor Processing Unit (TPU)][tpu] hardware (TPUv5p).
|
499 |
+
|
500 |
+
Training large language models requires significant computational power. TPUs,
|
501 |
+
designed specifically for matrix operations common in machine learning, offer
|
502 |
+
several advantages in this domain:
|
503 |
+
|
504 |
+
* Performance: TPUs are specifically designed to handle the massive computations
|
505 |
+
involved in training LLMs. They can speed up training considerably compared to
|
506 |
+
CPUs.
|
507 |
+
* Memory: TPUs often come with large amounts of high-bandwidth memory, allowing
|
508 |
+
for the handling of large models and batch sizes during training. This can
|
509 |
+
lead to better model quality.
|
510 |
+
* Scalability: TPU Pods (large clusters of TPUs) provide a scalable solution for
|
511 |
+
handling the growing complexity of large foundation models. You can distribute
|
512 |
+
training across multiple TPU devices for faster and more efficient processing.
|
513 |
+
* Cost-effectiveness: In many scenarios, TPUs can provide a more cost-effective
|
514 |
+
solution for training large models compared to CPU-based infrastructure,
|
515 |
+
especially when considering the time and resources saved due to faster
|
516 |
+
training.
|
517 |
+
* These advantages are aligned with
|
518 |
+
[Google's commitments to operate sustainably][sustainability].
|
519 |
+
|
520 |
+
### Software
|
521 |
+
|
522 |
+
Training was done using [JAX][jax] and [ML Pathways][ml-pathways].
|
523 |
+
|
524 |
+
JAX allows researchers to take advantage of the latest generation of hardware,
|
525 |
+
including TPUs, for faster and more efficient training of large models.
|
526 |
+
|
527 |
+
ML Pathways is Google's latest effort to build artificially intelligent systems
|
528 |
+
capable of generalizing across multiple tasks. This is specially suitable for
|
529 |
+
[foundation models][foundation-models], including large language models like
|
530 |
+
these ones.
|
531 |
+
|
532 |
+
Together, JAX and ML Pathways are used as described in the
|
533 |
+
[paper about the Gemini family of models][gemini-2-paper]; "the 'single
|
534 |
+
controller' programming model of Jax and Pathways allows a single Python
|
535 |
+
process to orchestrate the entire training run, dramatically simplifying the
|
536 |
+
development workflow."
|
537 |
+
|
538 |
+
## Evaluation
|
539 |
+
|
540 |
+
Model evaluation metrics and results.
|
541 |
+
|
542 |
+
### Benchmark Results
|
543 |
+
|
544 |
+
These models were evaluated against a large collection of different datasets and
|
545 |
+
metrics to cover different aspects of text generation:
|
546 |
+
|
547 |
+
| Benchmark | Metric | Gemma PT 9B | Gemma PT 27B |
|
548 |
+
| ------------------------------ | ------------- | ----------- | ------------ |
|
549 |
+
| [MMLU][mmlu] | 5-shot, top-1 | 71.3 | 75.2 |
|
550 |
+
| [HellaSwag][hellaswag] | 10-shot | 81.9 | 86.4 |
|
551 |
+
| [PIQA][piqa] | 0-shot | 81.7 | 83.2 |
|
552 |
+
| [SocialIQA][socialiqa] | 0-shot | 53.4 | 53.7 |
|
553 |
+
| [BoolQ][boolq] | 0-shot | 84.2 | 84.8 |
|
554 |
+
| [WinoGrande][winogrande] | partial score | 80.6 | 83.7 |
|
555 |
+
| [ARC-e][arc] | 0-shot | 88.0 | 88.6 |
|
556 |
+
| [ARC-c][arc] | 25-shot | 68.4 | 71.4 |
|
557 |
+
| [TriviaQA][triviaqa] | 5-shot | 76.6 | 83.7 |
|
558 |
+
| [Natural Questions][naturalq] | 5-shot | 29.2 | 34.5 |
|
559 |
+
| [HumanEval][humaneval] | pass@1 | 40.2 | 51.8 |
|
560 |
+
| [MBPP][mbpp] | 3-shot | 52.4 | 62.6 |
|
561 |
+
| [GSM8K][gsm8k] | 5-shot, maj@1 | 68.6 | 74.0 |
|
562 |
+
| [MATH][math] | 4-shot | 36.6 | 42.3 |
|
563 |
+
| [AGIEval][agieval] | 3-5-shot | 52.8 | 55.1 |
|
564 |
+
| [BIG-Bench][big-bench] | 3-shot, CoT | 68.2 | 74.9 |
|
565 |
+
| ------------------------------ | ------------- | ----------- | ------------ |
|
566 |
+
|
567 |
+
## Ethics and Safety
|
568 |
+
|
569 |
+
Ethics and safety evaluation approach and results.
|
570 |
+
|
571 |
+
### Evaluation Approach
|
572 |
+
|
573 |
+
Our evaluation methods include structured evaluations and internal red-teaming
|
574 |
+
testing of relevant content policies. Red-teaming was conducted by a number of
|
575 |
+
different teams, each with different goals and human evaluation metrics. These
|
576 |
+
models were evaluated against a number of different categories relevant to
|
577 |
+
ethics and safety, including:
|
578 |
+
|
579 |
+
* Text-to-Text Content Safety: Human evaluation on prompts covering safety
|
580 |
+
policies including child sexual abuse and exploitation, harassment, violence
|
581 |
+
and gore, and hate speech.
|
582 |
+
* Text-to-Text Representational Harms: Benchmark against relevant academic
|
583 |
+
datasets such as [WinoBias][winobias] and [BBQ Dataset][bbq].
|
584 |
+
* Memorization: Automated evaluation of memorization of training data, including
|
585 |
+
the risk of personally identifiable information exposure.
|
586 |
+
* Large-scale harm: Tests for "dangerous capabilities," such as chemical,
|
587 |
+
biological, radiological, and nuclear (CBRN) risks.
|
588 |
+
|
589 |
+
### Evaluation Results
|
590 |
+
|
591 |
+
The results of ethics and safety evaluations are within acceptable thresholds
|
592 |
+
for meeting [internal policies][safety-policies] for categories such as child
|
593 |
+
safety, content safety, representational harms, memorization, large-scale harms.
|
594 |
+
On top of robust internal evaluations, the results of well-known safety
|
595 |
+
benchmarks like BBQ, BOLD, Winogender, Winobias, RealToxicity, and TruthfulQA
|
596 |
+
are shown here.
|
597 |
+
|
598 |
+
#### Gemma 2.0
|
599 |
+
|
600 |
+
| Benchmark | Metric | Gemma 2 IT 9B | Gemma 2 IT 27B |
|
601 |
+
| ------------------------ | ------------- | --------------- | ---------------- |
|
602 |
+
| [RealToxicity][realtox] | average | 8.25 | 8.84 |
|
603 |
+
| [CrowS-Pairs][crows] | top-1 | 37.47 | 36.67 |
|
604 |
+
| [BBQ Ambig][bbq] | 1-shot, top-1 | 88.58 | 85.99 |
|
605 |
+
| [BBQ Disambig][bbq] | top-1 | 82.67 | 86.94 |
|
606 |
+
| [Winogender][winogender] | top-1 | 79.17 | 77.22 |
|
607 |
+
| [TruthfulQA][truthfulqa] | | 50.27 | 51.60 |
|
608 |
+
| [Winobias 1_2][winobias] | | 78.09 | 81.94 |
|
609 |
+
| [Winobias 2_2][winobias] | | 95.32 | 97.22 |
|
610 |
+
| [Toxigen][toxigen] | | 39.30 | 38.42 |
|
611 |
+
| ------------------------ | ------------- | --------------- | ---------------- |
|
612 |
+
|
613 |
+
## Usage and Limitations
|
614 |
+
|
615 |
+
These models have certain limitations that users should be aware of.
|
616 |
+
|
617 |
+
### Intended Usage
|
618 |
+
|
619 |
+
Open Large Language Models (LLMs) have a wide range of applications across
|
620 |
+
various industries and domains. The following list of potential uses is not
|
621 |
+
comprehensive. The purpose of this list is to provide contextual information
|
622 |
+
about the possible use-cases that the model creators considered as part of model
|
623 |
+
training and development.
|
624 |
+
|
625 |
+
* Content Creation and Communication
|
626 |
+
* Text Generation: These models can be used to generate creative text formats
|
627 |
+
such as poems, scripts, code, marketing copy, and email drafts.
|
628 |
+
* Chatbots and Conversational AI: Power conversational interfaces for customer
|
629 |
+
service, virtual assistants, or interactive applications.
|
630 |
+
* Text Summarization: Generate concise summaries of a text corpus, research
|
631 |
+
papers, or reports.
|
632 |
+
* Research and Education
|
633 |
+
* Natural Language Processing (NLP) Research: These models can serve as a
|
634 |
+
foundation for researchers to experiment with NLP techniques, develop
|
635 |
+
algorithms, and contribute to the advancement of the field.
|
636 |
+
* Language Learning Tools: Support interactive language learning experiences,
|
637 |
+
aiding in grammar correction or providing writing practice.
|
638 |
+
* Knowledge Exploration: Assist researchers in exploring large bodies of text
|
639 |
+
by generating summaries or answering questions about specific topics.
|
640 |
+
|
641 |
+
### Limitations
|
642 |
+
|
643 |
+
* Training Data
|
644 |
+
* The quality and diversity of the training data significantly influence the
|
645 |
+
model's capabilities. Biases or gaps in the training data can lead to
|
646 |
+
limitations in the model's responses.
|
647 |
+
* The scope of the training dataset determines the subject areas the model can
|
648 |
+
handle effectively.
|
649 |
+
* Context and Task Complexity
|
650 |
+
* LLMs are better at tasks that can be framed with clear prompts and
|
651 |
+
instructions. Open-ended or highly complex tasks might be challenging.
|
652 |
+
* A model's performance can be influenced by the amount of context provided
|
653 |
+
(longer context generally leads to better outputs, up to a certain point).
|
654 |
+
* Language Ambiguity and Nuance
|
655 |
+
* Natural language is inherently complex. LLMs might struggle to grasp subtle
|
656 |
+
nuances, sarcasm, or figurative language.
|
657 |
+
* Factual Accuracy
|
658 |
+
* LLMs generate responses based on information they learned from their
|
659 |
+
training datasets, but they are not knowledge bases. They may generate
|
660 |
+
incorrect or outdated factual statements.
|
661 |
+
* Common Sense
|
662 |
+
* LLMs rely on statistical patterns in language. They might lack the ability
|
663 |
+
to apply common sense reasoning in certain situations.
|
664 |
+
|
665 |
+
### Ethical Considerations and Risks
|
666 |
+
|
667 |
+
The development of large language models (LLMs) raises several ethical concerns.
|
668 |
+
In creating an open model, we have carefully considered the following:
|
669 |
+
|
670 |
+
* Bias and Fairness
|
671 |
+
* LLMs trained on large-scale, real-world text data can reflect socio-cultural
|
672 |
+
biases embedded in the training material. These models underwent careful
|
673 |
+
scrutiny, input data pre-processing described and posterior evaluations
|
674 |
+
reported in this card.
|
675 |
+
* Misinformation and Misuse
|
676 |
+
* LLMs can be misused to generate text that is false, misleading, or harmful.
|
677 |
+
* Guidelines are provided for responsible use with the model, see the
|
678 |
+
[Responsible Generative AI Toolkit][rai-toolkit].
|
679 |
+
* Transparency and Accountability:
|
680 |
+
* This model card summarizes details on the models' architecture,
|
681 |
+
capabilities, limitations, and evaluation processes.
|
682 |
+
* A responsibly developed open model offers the opportunity to share
|
683 |
+
innovation by making LLM technology accessible to developers and researchers
|
684 |
+
across the AI ecosystem.
|
685 |
+
|
686 |
+
Risks identified and mitigations:
|
687 |
+
|
688 |
+
* Perpetuation of biases: It's encouraged to perform continuous monitoring
|
689 |
+
(using evaluation metrics, human review) and the exploration of de-biasing
|
690 |
+
techniques during model training, fine-tuning, and other use cases.
|
691 |
+
* Generation of harmful content: Mechanisms and guidelines for content safety
|
692 |
+
are essential. Developers are encouraged to exercise caution and implement
|
693 |
+
appropriate content safety safeguards based on their specific product policies
|
694 |
+
and application use cases.
|
695 |
+
* Misuse for malicious purposes: Technical limitations and developer and
|
696 |
+
end-user education can help mitigate ag
|
697 |
+
ainst malicious applications of LLMs.
|
698 |
+
Educational resources and reporting mechanisms for users to flag misuse are
|
699 |
+
provided. Prohibited uses of Gemma models are outlined in the
|
700 |
+
[Gemma Prohibited Use Policy][prohibited-use].
|
701 |
+
* Privacy violations: Models were trained on data filtered for removal of PII
|
702 |
+
(Personally Identifiable Information). Developers are encouraged to adhere to
|
703 |
+
privacy regulations with privacy-preserving techniques.
|
704 |
+
|
705 |
+
### Benefits
|
706 |
+
|
707 |
+
At the time of release, this family of models provides high-performance open
|
708 |
+
large language model implementations designed from the ground up for Responsible
|
709 |
+
AI development compared to similarly sized models.
|
710 |
+
|
711 |
+
Using the benchmark evaluation metrics described in this document, these models
|
712 |
+
have shown to provide superior performance to other, comparably-sized open model
|
713 |
+
alternatives.
|
714 |
+
|
715 |
+
[rai-toolkit]: https://ai.google.dev/responsible
|
716 |
+
[kaggle-gemma]: https://www.kaggle.com/models/google/gemma-2
|
717 |
+
[terms]: https://ai.google.dev/gemma/terms
|
718 |
+
[vertex-mg-gemma]: https://console.cloud.google.com/vertex-ai/publishers/google/model-garden/335
|
719 |
+
[sensitive-info]: https://cloud.google.com/dlp/docs/high-sensitivity-infotypes-reference
|
720 |
+
[safety-policies]: https://storage.googleapis.com/gweb-uniblog-publish-prod/documents/2023_Google_AI_Principles_Progress_Update.pdf#page=11
|
721 |
+
[prohibited-use]: https://ai.google.dev/gemma/prohibited_use_policy
|
722 |
+
[tpu]: https://cloud.google.com/tpu/docs/intro-to-tpu
|
723 |
+
[sustainability]: https://sustainability.google/operating-sustainably/
|
724 |
+
[jax]: https://github.com/google/jax
|
725 |
+
[ml-pathways]: https://blog.google/technology/ai/introducing-pathways-next-generation-ai-architecture/
|
726 |
+
[sustainability]: https://sustainability.google/operating-sustainably/
|
727 |
+
[foundation-models]: https://ai.google/discover/foundation-models/
|
728 |
+
[gemini-2-paper]: https://goo.gle/gemma2report
|
729 |
+
[mmlu]: https://arxiv.org/abs/2009.03300
|
730 |
+
[hellaswag]: https://arxiv.org/abs/1905.07830
|
731 |
+
[piqa]: https://arxiv.org/abs/1911.11641
|
732 |
+
[socialiqa]: https://arxiv.org/abs/1904.09728
|
733 |
+
[boolq]: https://arxiv.org/abs/1905.10044
|
734 |
+
[winogrande]: https://arxiv.org/abs/1907.10641
|
735 |
+
[commonsenseqa]: https://arxiv.org/abs/1811.00937
|
736 |
+
[openbookqa]: https://arxiv.org/abs/1809.02789
|
737 |
+
[arc]: https://arxiv.org/abs/1911.01547
|
738 |
+
[triviaqa]: https://arxiv.org/abs/1705.03551
|
739 |
+
[naturalq]: https://github.com/google-research-datasets/natural-questions
|
740 |
+
[humaneval]: https://arxiv.org/abs/2107.03374
|
741 |
+
[mbpp]: https://arxiv.org/abs/2108.07732
|
742 |
+
[gsm8k]: https://arxiv.org/abs/2110.14168
|
743 |
+
[realtox]: https://arxiv.org/abs/2009.11462
|
744 |
+
[bold]: https://arxiv.org/abs/2101.11718
|
745 |
+
[crows]: https://aclanthology.org/2020.emnlp-main.154/
|
746 |
+
[bbq]: https://arxiv.org/abs/2110.08193v2
|
747 |
+
[winogender]: https://arxiv.org/abs/1804.09301
|
748 |
+
[truthfulqa]: https://arxiv.org/abs/2109.07958
|
749 |
+
[winobias]: https://arxiv.org/abs/1804.06876
|
750 |
+
[math]: https://arxiv.org/abs/2103.03874
|
751 |
+
[agieval]: https://arxiv.org/abs/2304.06364
|
752 |
+
[big-bench]: https://arxiv.org/abs/2206.04615
|
753 |
+
[toxigen]: https://arxiv.org/abs/2203.09509
|