IMvision12 commited on
Commit
9b8cc2b
·
0 Parent(s):

Super-squash branch 'main' using huggingface_hub

Browse files
.gitattributes ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ tokenizer.json filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ pipeline_tag: image-text-to-text
3
+ license: gemma
4
+ base_model: google/gemma-3n-E4B
5
+ library_name: kerasformers
6
+ extra_gated_heading: Access Gemma on Hugging Face
7
+ extra_gated_prompt: >-
8
+ To access Gemma on Hugging Face, you're required to review and agree to
9
+ Google's usage license. To do this, please ensure you're logged in to Hugging
10
+ Face and click below. Requests are processed immediately.
11
+ extra_gated_button_content: Acknowledge license
12
+ license_link: https://ai.google.dev/gemma/terms
13
+ language:
14
+ - en
15
+ tags:
16
+ - keras
17
+ - kerasformers
18
+ - gemma3n
19
+ - gemma-3n
20
+ - image-text-to-text
21
+ - audio-text-to-text
22
+ - multimodal
23
+ - pytorch
24
+ - jax
25
+ - tf
26
+ ---
27
+
28
+ ## ***See [our collection](https://huggingface.co/collections/kerasformers/gemma-3n-6a7a507adf78dde12680accf) for all versions of Gemma 3n.***
29
+
30
+ # Run Gemma 3n with Keras 3: JAX, PyTorch, or TensorFlow
31
+
32
+ [![GitHub](https://img.shields.io/badge/GitHub-KerasFormers-black?logo=github)](https://github.com/IMvision12/KerasFormers) [![Docs](https://img.shields.io/badge/Docs-Gemma%203n-blue)](https://imvision12.github.io/KerasFormers/gemma3n/) [![Collection](https://img.shields.io/badge/HF-Gemma%203n%20collection-yellow)](https://huggingface.co/collections/kerasformers/gemma-3n-6a7a507adf78dde12680accf)
33
+
34
+ # kerasformers/gemma-3n-e4b
35
+
36
+ Gemma 3n is Google's on-device **multimodal** (image + audio + text) model. Its
37
+ decoder layers several on-device innovations on the Gemma shape: **AltUp**
38
+ (alternating updates over parallel hidden streams), **LAuReL** (learned augmented
39
+ residuals), **MatFormer** (nested per-layer widths), **Per-Layer Embeddings**, and
40
+ activation sparsity, with tail **KV-sharing** and a 5:1 sliding/global attention
41
+ schedule. Vision is a **MobileNet-V5** encoder and audio a **USM** conformer, both
42
+ feeding soft tokens into the decoder. Base checkpoints are for completion; `-it`
43
+ variants are instruction-tuned.
44
+
45
+ For more details, see Google's original [model card](https://huggingface.co/google/gemma-3n-E4B).
46
+
47
+ Pure-**Keras 3** conversion of [`google/gemma-3n-E4B`](https://huggingface.co/google/gemma-3n-E4B) for
48
+ [kerasformers](https://github.com/IMvision12/KerasFormers). One implementation runs unmodified on
49
+ **TensorFlow / Torch / JAX**.
50
+
51
+ This is a **base** (pretrained) checkpoint, for completion / fine-tuning.
52
+
53
+ ## ✨ Quick start
54
+
55
+ ### Text-only
56
+
57
+ ```python
58
+ import os
59
+ os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow"
60
+
61
+ from kerasformers.models.gemma3n import Gemma3nTextGenerate, Gemma3nTokenizer
62
+
63
+ model = Gemma3nTextGenerate.from_weights("kerasformers/gemma-3n-e4b")
64
+ tokenizer = Gemma3nTokenizer.from_weights("kerasformers/gemma-3n-e4b")
65
+
66
+ inputs = tokenizer([{"role": "user", "content": "Hello, who are you?"}])
67
+ outputs = model.generate(**inputs, max_new_tokens=64)
68
+ print(tokenizer.decode(outputs[0]))
69
+ ```
70
+
71
+ ### Image + text
72
+
73
+ ```python
74
+ import os
75
+ os.environ["KERAS_BACKEND"] = "torch" # or "jax" / "tensorflow"
76
+
77
+ from PIL import Image
78
+ from kerasformers.models.gemma3n import Gemma3nConditionalGenerate, Gemma3nProcessor
79
+
80
+ model = Gemma3nConditionalGenerate.from_weights("kerasformers/gemma-3n-e4b")
81
+ processor = Gemma3nProcessor.from_weights("kerasformers/gemma-3n-e4b")
82
+
83
+ inputs = processor(conversation=[
84
+ {"role": "user", "content": [
85
+ {"type": "image", "image": Image.open("cat.jpg")},
86
+ {"type": "text", "text": "Describe this image in one sentence."},
87
+ ]}
88
+ ])
89
+ outputs = model.generate(**inputs, max_new_tokens=64)
90
+ print(processor.decode(outputs[0]))
91
+ ```
92
+
93
+ All Gemma 3n variants load the same way with `from_weights("kerasformers/<variant>")`:
94
+
95
+ | Variant | Hub | Type |
96
+ |---|---|---|
97
+ | `gemma-3n-e2b` | [`kerasformers/gemma-3n-e2b`](https://huggingface.co/kerasformers/gemma-3n-e2b) | multimodal / base |
98
+ | `gemma-3n-e2b-it` | [`kerasformers/gemma-3n-e2b-it`](https://huggingface.co/kerasformers/gemma-3n-e2b-it) | multimodal / instruct |
99
+ | `gemma-3n-e4b` | [`kerasformers/gemma-3n-e4b`](https://huggingface.co/kerasformers/gemma-3n-e4b) | multimodal / base |
100
+ | `gemma-3n-e4b-it` | [`kerasformers/gemma-3n-e4b-it`](https://huggingface.co/kerasformers/gemma-3n-e4b-it) | multimodal / instruct |
101
+
102
+ ## Tips
103
+
104
+ - Set `KERAS_BACKEND` **before** importing Keras / kerasformers.
105
+ - Loads in **bfloat16** by default (the weights are bf16). Pass
106
+ `load_dtype="float32"` for full precision, or `quantization="int8"` to shrink further.
107
+ - Gemma 3n is **audio-capable** too: pass `audio` content items in the
108
+ conversation to transcribe / reason over speech.
109
+ - See [Gemma 3n docs](https://imvision12.github.io/KerasFormers/gemma3n/) and
110
+ [Loading Weights](https://imvision12.github.io/KerasFormers/loading_weights/).
111
+ - Upstream safetensors still work via the `hf:` prefix, e.g.
112
+ `Gemma3nConditionalGenerate.from_weights("hf:google/gemma-3n-E4B")`.
113
+
114
+ ## Special Thanks
115
+
116
+ A huge thank you to the Google Gemma authors for creating and releasing these models.
117
+
118
+ License: Gemma (gated). Accept the license on the upstream Hub card before downloading.
kf_config.json ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "library_name": "kerasformers",
3
+ "kerasformers_version": "1.2.1",
4
+ "model_module": "kerasformers.models.gemma3n",
5
+ "model_class": "Gemma3nConditionalGenerate",
6
+ "variant": "gemma-3n-e4b",
7
+ "weights": "model.weights.h5",
8
+ "schema_version": 2,
9
+ "weight_dtype": "bfloat16",
10
+ "model_type": "gemma3n",
11
+ "text_config": {
12
+ "vocab_size": 262400,
13
+ "embed_dim": 2048,
14
+ "mlp_dim": [
15
+ 16384,
16
+ 16384,
17
+ 16384,
18
+ 16384,
19
+ 16384,
20
+ 16384,
21
+ 16384,
22
+ 16384,
23
+ 16384,
24
+ 16384,
25
+ 16384,
26
+ 16384,
27
+ 16384,
28
+ 16384,
29
+ 16384,
30
+ 16384,
31
+ 16384,
32
+ 16384,
33
+ 16384,
34
+ 16384,
35
+ 16384,
36
+ 16384,
37
+ 16384,
38
+ 16384,
39
+ 16384,
40
+ 16384,
41
+ 16384,
42
+ 16384,
43
+ 16384,
44
+ 16384,
45
+ 16384,
46
+ 16384,
47
+ 16384,
48
+ 16384,
49
+ 16384
50
+ ],
51
+ "num_layers": 35,
52
+ "num_heads": 8,
53
+ "num_kv_heads": 2,
54
+ "head_dim": 256,
55
+ "sliding_window": 512,
56
+ "sliding_window_pattern": 5,
57
+ "layer_types": [
58
+ "sliding_attention",
59
+ "sliding_attention",
60
+ "sliding_attention",
61
+ "sliding_attention",
62
+ "full_attention",
63
+ "sliding_attention",
64
+ "sliding_attention",
65
+ "sliding_attention",
66
+ "sliding_attention",
67
+ "full_attention",
68
+ "sliding_attention",
69
+ "sliding_attention",
70
+ "sliding_attention",
71
+ "sliding_attention",
72
+ "full_attention",
73
+ "sliding_attention",
74
+ "sliding_attention",
75
+ "sliding_attention",
76
+ "sliding_attention",
77
+ "full_attention",
78
+ "sliding_attention",
79
+ "sliding_attention",
80
+ "sliding_attention",
81
+ "sliding_attention",
82
+ "full_attention",
83
+ "sliding_attention",
84
+ "sliding_attention",
85
+ "sliding_attention",
86
+ "sliding_attention",
87
+ "full_attention",
88
+ "sliding_attention",
89
+ "sliding_attention",
90
+ "sliding_attention",
91
+ "sliding_attention",
92
+ "full_attention"
93
+ ],
94
+ "final_logit_softcapping": 30.0,
95
+ "norm_eps": 1e-06,
96
+ "rope_theta": 1000000.0,
97
+ "rope_local_theta": 10000.0,
98
+ "hidden_activation": "gelu_pytorch_tanh",
99
+ "tie_embeddings": true,
100
+ "vocab_size_per_layer_input": 262144,
101
+ "hidden_size_per_layer_input": 256,
102
+ "altup_num_inputs": 4,
103
+ "altup_active_idx": 0,
104
+ "altup_coef_clip": 120.0,
105
+ "altup_correct_scale": true,
106
+ "num_kv_shared_layers": 15,
107
+ "laurel_rank": 64,
108
+ "activation_sparsity_pattern": [
109
+ 0.95,
110
+ 0.95,
111
+ 0.95,
112
+ 0.95,
113
+ 0.95,
114
+ 0.95,
115
+ 0.95,
116
+ 0.95,
117
+ 0.95,
118
+ 0.95,
119
+ 0.0,
120
+ 0.0,
121
+ 0.0,
122
+ 0.0,
123
+ 0.0,
124
+ 0.0,
125
+ 0.0,
126
+ 0.0,
127
+ 0.0,
128
+ 0.0,
129
+ 0.0,
130
+ 0.0,
131
+ 0.0,
132
+ 0.0,
133
+ 0.0,
134
+ 0.0,
135
+ 0.0,
136
+ 0.0,
137
+ 0.0,
138
+ 0.0,
139
+ 0.0,
140
+ 0.0,
141
+ 0.0,
142
+ 0.0,
143
+ 0.0
144
+ ]
145
+ },
146
+ "vision_config": {
147
+ "architecture": "mobilenetv5_300m_enc",
148
+ "hidden_size": 2048,
149
+ "vocab_size": 128,
150
+ "vocab_offset": 262144,
151
+ "rms_norm_eps": 1e-06,
152
+ "do_pooling": false
153
+ },
154
+ "audio_config": {
155
+ "vocab_size": 128,
156
+ "vocab_offset": 262272,
157
+ "input_feat_size": 128,
158
+ "hidden_size": 1536,
159
+ "rms_norm_eps": 1e-06,
160
+ "gradient_clipping": 10000000000.0,
161
+ "conf_attention_chunk_size": 12,
162
+ "conf_attention_context_left": 13,
163
+ "conf_attention_context_right": 0,
164
+ "conf_attention_logit_cap": 50.0,
165
+ "conf_num_attention_heads": 8,
166
+ "conf_num_hidden_layers": 12,
167
+ "conf_conv_kernel_size": 5,
168
+ "conf_reduction_factor": 4,
169
+ "conf_residual_weight": 0.5,
170
+ "sscp_conv_channel_size": [
171
+ 128,
172
+ 32
173
+ ],
174
+ "sscp_conv_group_norm_eps": 0.001,
175
+ "sscp_conv_kernel_size": [
176
+ [
177
+ 3,
178
+ 3
179
+ ],
180
+ [
181
+ 3,
182
+ 3
183
+ ]
184
+ ],
185
+ "sscp_conv_stride_size": [
186
+ [
187
+ 2,
188
+ 2
189
+ ],
190
+ [
191
+ 2,
192
+ 2
193
+ ]
194
+ ]
195
+ },
196
+ "audio_soft_tokens_per_image": 188,
197
+ "vision_soft_tokens_per_image": 256,
198
+ "boi_token_id": 255999,
199
+ "eoi_token_id": 262144,
200
+ "image_token_id": 262145,
201
+ "boa_token_id": 256000,
202
+ "eoa_token_id": 262272,
203
+ "audio_token_id": 262273,
204
+ "tie_word_embeddings": true
205
+ }
kf_preprocessor.json ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "library_name": "kerasformers",
3
+ "kerasformers_version": "1.2.1",
4
+ "preprocessor_module": "kerasformers.models.gemma3n",
5
+ "preprocessor_class": "Gemma3nImageProcessor",
6
+ "variant": "gemma-3n-e4b",
7
+ "size": 768,
8
+ "rescale_factor": 0.00392156862745098,
9
+ "image_mean": [
10
+ 0.5,
11
+ 0.5,
12
+ 0.5
13
+ ],
14
+ "image_std": [
15
+ 0.5,
16
+ 0.5,
17
+ 0.5
18
+ ]
19
+ }
model.weights.json ADDED
The diff for this file is too large to render. See raw diff
 
model_00000.weights.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f8eee670b27a5bbcb990bead5d3763d1a27c2997fce0e60b61711deb3e73c460
3
+ size 7330137936
model_00001.weights.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5cd61ae2932389c79c08471949effca6c26f04eb9fc96b6e2ee153b2bdf719e6
3
+ size 9605869776
model_00002.weights.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:dabe212f2b524596840803ef79a1c04806a152e82e18011c96e6571a1fd614db
3
+ size 9554579600
model_00003.weights.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2d83351c9fb153a0f65a35e7e200f28d1f02a31c35c24c26cdf0b44caac605ef
3
+ size 6099029520
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c4c19736bf24d1c6805cf49340e31bd02c70fb7857a2cb31065c90c2b5719c4e
3
+ size 33442559