File size: 3,644 Bytes
d9604ae
 
 
 
e84bb59
d9604ae
 
 
 
 
e84bb59
d9604ae
 
 
 
 
 
 
 
 
2a4d3e8
d9604ae
 
 
2a4d3e8
d9604ae
e84bb59
d9604ae
 
 
 
 
 
 
e84bb59
d9604ae
 
 
 
 
 
 
 
 
e84bb59
d9604ae
e84bb59
 
d9604ae
 
 
 
 
 
 
 
e84bb59
d9604ae
 
 
e84bb59
 
 
 
 
 
d9604ae
 
 
e84bb59
d9604ae
 
e84bb59
d9604ae
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
---
pipeline_tag: text-generation
license: gemma
base_model: google/gemma-7b-it
library_name: zeromodels
extra_gated_heading: Access Gemma on Hugging Face
language:
- en
tags:
- keras
- zeromodels
- gemma
- gemma-7b
- text-generation
- arxiv:2403.08295
- pytorch
- jax
- tf
---

## ***See [our collection](https://huggingface.co/collections/zeromodels/gemma-6a8eaf9c6a69375e61349bbf) for all versions of Gemma.***

# Run Gemma with Keras 3: JAX, PyTorch, or TensorFlow

[![GitHub](https://img.shields.io/badge/GitHub-ZeroModels-black?logo=github)](https://github.com/IMvision12/ZeroModels) [![Docs](https://img.shields.io/badge/Docs-Gemma-blue)](https://imvision12.github.io/ZeroModels/gemma/) [![Collection](https://img.shields.io/badge/HF-Gemma%20collection-yellow)](https://huggingface.co/collections/zeromodels/gemma-6a8eaf9c6a69375e61349bbf)

# zeromodels/gemma-7b-it

Paper: [Gemma: Open Models Based on Gemini Research and Technology (arXiv:2403.08295)](https://arxiv.org/abs/2403.08295) · [HF Papers](https://huggingface.co/papers/2403.08295)

Gemma is Google's open decoder-only LLM family (RMSNorm, GeGLU, RoPE, multi-query attention). Base checkpoints are for completion; `-it` / `1.1` variants are instruction-tuned for chat.

For more details on the model, please go to Google's original [model card](https://huggingface.co/google/gemma-7b-it).

Pure-**Keras 3** conversion of [`google/gemma-7b-it`](https://huggingface.co/google/gemma-7b-it) for [zeromodels](https://github.com/IMvision12/ZeroModels). One implementation runs unmodified on **TensorFlow / Torch / JAX**.

This is an **instruction-tuned** checkpoint: use the chat template via `GemmaTokenizer`.

## ✨ Quick start

```python
import os
os.environ["KERAS_BACKEND"] = "torch"  # or "jax" / "tensorflow"

from zeromodels.models.gemma import GemmaTextGenerate, GemmaTokenizer

model = GemmaTextGenerate.from_weights("zeromodels/gemma-7b-it")
tokenizer = GemmaTokenizer.from_weights("zeromodels/gemma-7b-it")

inputs = tokenizer([
    {"role": "user", "content": "Explain rotary embeddings in one sentence."}
])
outputs = model.generate(**inputs, max_new_tokens=64)
print(tokenizer.decode(outputs[0]))
```

Load any Gemma v1 variant the same way with `from_weights("zeromodels/<variant>")`:

| Variant | Hub | Type |
|---|---|---|
| `gemma-2b` | [`zeromodels/gemma-2b`](https://huggingface.co/zeromodels/gemma-2b) | base |
| `gemma-2b-it` | [`zeromodels/gemma-2b-it`](https://huggingface.co/zeromodels/gemma-2b-it) | instruct |
| `gemma-1.1-2b-it` | [`zeromodels/gemma-1.1-2b-it`](https://huggingface.co/zeromodels/gemma-1.1-2b-it) | instruct (1.1) |
| `gemma-7b` | [`zeromodels/gemma-7b`](https://huggingface.co/zeromodels/gemma-7b) | base |
| `gemma-7b-it` | [`zeromodels/gemma-7b-it`](https://huggingface.co/zeromodels/gemma-7b-it) | instruct |
| `gemma-1.1-7b-it` | [`zeromodels/gemma-1.1-7b-it`](https://huggingface.co/zeromodels/gemma-1.1-7b-it) | instruct (1.1) |

## Tips

- Set `KERAS_BACKEND` **before** importing Keras / zeromodels.
- Prefer `GemmaTokenizer.from_weights(...)` so the chat template matches.
- Larger checkpoints: try `load_dtype="bfloat16"` or `quantization="int8"`.
- See [Gemma docs](https://imvision12.github.io/ZeroModels/gemma/) and [Loading Weights](https://imvision12.github.io/ZeroModels/loading_weights/).
- Community / upstream safetensors still work via the `hf:` prefix, e.g. `GemmaTextGenerate.from_weights("hf:google/gemma-7b-it")`.

## Special Thanks

A huge thank you to the Google Gemma authors for creating and releasing these models.

License: Gemma (gated). Accept the license on the upstream Hub card before downloading.