yujiepan commited on
Commit
d7e58e0
·
verified ·
1 Parent(s): 588757e

Upload folder using huggingface_hub

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* 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
 
 
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,358 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ base_model:
4
+ - google/gemma-4-26B-A4B-it
5
+ ---
6
+
7
+ This tiny model is intended for debugging. It is randomly initialized using the configuration adapted from [google/gemma-4-26B-A4B-it](https://huggingface.co/google/gemma-4-26B-A4B-it).
8
+
9
+ | File path | Size |
10
+ |------|------|
11
+ | model.safetensors | 5.4MB |
12
+
13
+
14
+ ### Example usage:
15
+
16
+ ```python
17
+ import torch
18
+ from transformers import AutoModelForCausalLM, AutoProcessor
19
+
20
+ model_id = "tiny-random/gemma-4-moe"
21
+ processor = AutoProcessor.from_pretrained(model_id)
22
+ model = AutoModelForCausalLM.from_pretrained(
23
+ model_id, dtype=torch.bfloat16, device_map="auto"
24
+ )
25
+ messages = [
26
+ {
27
+ "role": "user",
28
+ "content": [
29
+ {
30
+ "type": "image",
31
+ "url": "https://raw.githubusercontent.com/google-gemma/cookbook/refs/heads/main/Demos/sample-data/GoldenGate.png",
32
+ },
33
+ {"type": "text", "text": "What is shown in this image?"},
34
+ ],
35
+ },
36
+ {
37
+ "role": "assistant",
38
+ "content": [{"type": "text", "text": "Dummy response for image"}],
39
+ },
40
+ {
41
+ "role": "user",
42
+ "content": [
43
+ {
44
+ "type": "video",
45
+ "video": "https://github.com/bebechien/gemma/raw/refs/heads/main/videos/ForBiggerBlazes.mp4",
46
+ },
47
+ {"type": "text", "text": "Describe this video."},
48
+ ],
49
+ },
50
+ ]
51
+ inputs = processor.apply_chat_template(
52
+ messages,
53
+ tokenize=True,
54
+ return_dict=True,
55
+ return_tensors="pt",
56
+ add_generation_prompt=True,
57
+ ).to(model.device)
58
+ input_len = inputs["input_ids"].shape[-1]
59
+ print("input_len:", input_len)
60
+ outputs = model.generate(**inputs, max_new_tokens=32)
61
+ response = processor.decode(outputs[0], skip_special_tokens=False)
62
+ response = response.replace("<|image|>", "I")
63
+ response = response.replace("<|video|>", "V")
64
+ print(response)
65
+ ```
66
+
67
+ ### Codes to create this repo:
68
+
69
+ <details>
70
+ <summary>Click to expand</summary>
71
+
72
+ ```python
73
+ import json
74
+ from pathlib import Path
75
+
76
+ import torch
77
+ from huggingface_hub import file_exists, hf_hub_download
78
+
79
+ from transformers import (
80
+ AutoConfig,
81
+ AutoModelForCausalLM,
82
+ AutoProcessor,
83
+ AutoTokenizer,
84
+ Gemma4ForConditionalGeneration,
85
+ GenerationConfig,
86
+ set_seed,
87
+ )
88
+
89
+ source_model_id = "google/gemma-4-26B-A4B-it"
90
+ save_folder = "/tmp/tiny-random/gemma-4-moe"
91
+
92
+ processor = AutoProcessor.from_pretrained(source_model_id)
93
+ processor.save_pretrained(save_folder)
94
+
95
+ with open(
96
+ hf_hub_download(source_model_id, filename="config.json", repo_type="model"),
97
+ "r",
98
+ encoding="utf-8",
99
+ ) as f:
100
+ config_json = json.load(f)
101
+
102
+ config_json["text_config"].update(
103
+ {
104
+ "global_head_dim": 64,
105
+ "head_dim": 32,
106
+ "hidden_size": 8,
107
+ # "hidden_size_per_layer_input": 0, # only "E" variants have this
108
+ "intermediate_size": 64,
109
+ "layer_types": [
110
+ "sliding_attention",
111
+ "full_attention",
112
+ "sliding_attention",
113
+ "full_attention",
114
+ ],
115
+ 'moe_intermediate_size': 32,
116
+ "num_attention_heads": 8,
117
+ "num_hidden_layers": 4,
118
+ "num_key_value_heads": 4,
119
+ # "num_kv_shared_layers": 0, # only "E" variants have this
120
+ }
121
+ )
122
+ config_json["vision_config"].update(
123
+ {
124
+ "num_hidden_layers": 2,
125
+ "hidden_size": 8,
126
+ "intermediate_size": 64,
127
+ "head_dim": 32,
128
+ "global_head_dim": 32,
129
+ "num_attention_heads": 4,
130
+ "num_key_value_heads": 4,
131
+ }
132
+ )
133
+
134
+ with open(f"{save_folder}/config.json", "w", encoding="utf-8") as f:
135
+ json.dump(config_json, f, indent=2)
136
+
137
+ config = AutoConfig.from_pretrained(
138
+ save_folder,
139
+ trust_remote_code=True,
140
+ )
141
+ print(config)
142
+
143
+ torch.set_default_dtype(torch.bfloat16)
144
+ model = Gemma4ForConditionalGeneration(config)
145
+ torch.set_default_dtype(torch.float32)
146
+ if file_exists(
147
+ filename="generation_config.json", repo_id=source_model_id, repo_type="model"
148
+ ):
149
+ model.generation_config = GenerationConfig.from_pretrained(
150
+ source_model_id,
151
+ trust_remote_code=True,
152
+ )
153
+ set_seed(42)
154
+ model = model.cpu()
155
+ all_numels = 0
156
+ for name, p in sorted(model.named_parameters()):
157
+ all_numels += p.numel()
158
+ with torch.no_grad():
159
+ for name, p in sorted(model.named_parameters()):
160
+ torch.nn.init.normal_(p, 0, 0.2)
161
+ print(name, p.shape, f"{p.numel() / all_numels * 100: .4f}%")
162
+ model.save_pretrained(save_folder)
163
+ ```
164
+
165
+ </details>
166
+
167
+ ### Printing the model:
168
+
169
+ <details><summary>Click to expand</summary>
170
+
171
+ ```text
172
+ Gemma4ForConditionalGeneration(
173
+ (model): Gemma4Model(
174
+ (language_model): Gemma4TextModel(
175
+ (embed_tokens): Gemma4TextScaledWordEmbedding(262144, 8, padding_idx=0)
176
+ (layers): ModuleList(
177
+ (0): Gemma4TextDecoderLayer(
178
+ (self_attn): Gemma4TextAttention(
179
+ (q_norm): Gemma4RMSNorm()
180
+ (k_norm): Gemma4RMSNorm()
181
+ (v_norm): Gemma4RMSNorm()
182
+ (k_proj): Linear(in_features=8, out_features=128, bias=False)
183
+ (q_proj): Linear(in_features=8, out_features=256, bias=False)
184
+ (v_proj): Linear(in_features=8, out_features=128, bias=False)
185
+ (o_proj): Linear(in_features=256, out_features=8, bias=False)
186
+ )
187
+ (mlp): Gemma4TextMLP(
188
+ (gate_proj): Linear(in_features=8, out_features=64, bias=False)
189
+ (up_proj): Linear(in_features=8, out_features=64, bias=False)
190
+ (down_proj): Linear(in_features=64, out_features=8, bias=False)
191
+ (act_fn): GELUTanh()
192
+ )
193
+ (input_layernorm): Gemma4RMSNorm()
194
+ (post_attention_layernorm): Gemma4RMSNorm()
195
+ (pre_feedforward_layernorm): Gemma4RMSNorm()
196
+ (post_feedforward_layernorm): Gemma4RMSNorm()
197
+ (router): Gemma4TextRouter(
198
+ (norm): Gemma4RMSNorm()
199
+ (proj): Linear(in_features=8, out_features=128, bias=False)
200
+ )
201
+ (experts): Gemma4TextExperts(
202
+ (act_fn): GELUTanh()
203
+ )
204
+ (post_feedforward_layernorm_1): Gemma4RMSNorm()
205
+ (post_feedforward_layernorm_2): Gemma4RMSNorm()
206
+ (pre_feedforward_layernorm_2): Gemma4RMSNorm()
207
+ )
208
+ (1): Gemma4TextDecoderLayer(
209
+ (self_attn): Gemma4TextAttention(
210
+ (q_norm): Gemma4RMSNorm()
211
+ (k_norm): Gemma4RMSNorm()
212
+ (v_norm): Gemma4RMSNorm()
213
+ (k_proj): Linear(in_features=8, out_features=128, bias=False)
214
+ (q_proj): Linear(in_features=8, out_features=512, bias=False)
215
+ (o_proj): Linear(in_features=512, out_features=8, bias=False)
216
+ )
217
+ (mlp): Gemma4TextMLP(
218
+ (gate_proj): Linear(in_features=8, out_features=64, bias=False)
219
+ (up_proj): Linear(in_features=8, out_features=64, bias=False)
220
+ (down_proj): Linear(in_features=64, out_features=8, bias=False)
221
+ (act_fn): GELUTanh()
222
+ )
223
+ (input_layernorm): Gemma4RMSNorm()
224
+ (post_attention_layernorm): Gemma4RMSNorm()
225
+ (pre_feedforward_layernorm): Gemma4RMSNorm()
226
+ (post_feedforward_layernorm): Gemma4RMSNorm()
227
+ (router): Gemma4TextRouter(
228
+ (norm): Gemma4RMSNorm()
229
+ (proj): Linear(in_features=8, out_features=128, bias=False)
230
+ )
231
+ (experts): Gemma4TextExperts(
232
+ (act_fn): GELUTanh()
233
+ )
234
+ (post_feedforward_layernorm_1): Gemma4RMSNorm()
235
+ (post_feedforward_layernorm_2): Gemma4RMSNorm()
236
+ (pre_feedforward_layernorm_2): Gemma4RMSNorm()
237
+ )
238
+ (2): Gemma4TextDecoderLayer(
239
+ (self_attn): Gemma4TextAttention(
240
+ (q_norm): Gemma4RMSNorm()
241
+ (k_norm): Gemma4RMSNorm()
242
+ (v_norm): Gemma4RMSNorm()
243
+ (k_proj): Linear(in_features=8, out_features=128, bias=False)
244
+ (q_proj): Linear(in_features=8, out_features=256, bias=False)
245
+ (v_proj): Linear(in_features=8, out_features=128, bias=False)
246
+ (o_proj): Linear(in_features=256, out_features=8, bias=False)
247
+ )
248
+ (mlp): Gemma4TextMLP(
249
+ (gate_proj): Linear(in_features=8, out_features=64, bias=False)
250
+ (up_proj): Linear(in_features=8, out_features=64, bias=False)
251
+ (down_proj): Linear(in_features=64, out_features=8, bias=False)
252
+ (act_fn): GELUTanh()
253
+ )
254
+ (input_layernorm): Gemma4RMSNorm()
255
+ (post_attention_layernorm): Gemma4RMSNorm()
256
+ (pre_feedforward_layernorm): Gemma4RMSNorm()
257
+ (post_feedforward_layernorm): Gemma4RMSNorm()
258
+ (router): Gemma4TextRouter(
259
+ (norm): Gemma4RMSNorm()
260
+ (proj): Linear(in_features=8, out_features=128, bias=False)
261
+ )
262
+ (experts): Gemma4TextExperts(
263
+ (act_fn): GELUTanh()
264
+ )
265
+ (post_feedforward_layernorm_1): Gemma4RMSNorm()
266
+ (post_feedforward_layernorm_2): Gemma4RMSNorm()
267
+ (pre_feedforward_layernorm_2): Gemma4RMSNorm()
268
+ )
269
+ (3): Gemma4TextDecoderLayer(
270
+ (self_attn): Gemma4TextAttention(
271
+ (q_norm): Gemma4RMSNorm()
272
+ (k_norm): Gemma4RMSNorm()
273
+ (v_norm): Gemma4RMSNorm()
274
+ (k_proj): Linear(in_features=8, out_features=128, bias=False)
275
+ (q_proj): Linear(in_features=8, out_features=512, bias=False)
276
+ (o_proj): Linear(in_features=512, out_features=8, bias=False)
277
+ )
278
+ (mlp): Gemma4TextMLP(
279
+ (gate_proj): Linear(in_features=8, out_features=64, bias=False)
280
+ (up_proj): Linear(in_features=8, out_features=64, bias=False)
281
+ (down_proj): Linear(in_features=64, out_features=8, bias=False)
282
+ (act_fn): GELUTanh()
283
+ )
284
+ (input_layernorm): Gemma4RMSNorm()
285
+ (post_attention_layernorm): Gemma4RMSNorm()
286
+ (pre_feedforward_layernorm): Gemma4RMSNorm()
287
+ (post_feedforward_layernorm): Gemma4RMSNorm()
288
+ (router): Gemma4TextRouter(
289
+ (norm): Gemma4RMSNorm()
290
+ (proj): Linear(in_features=8, out_features=128, bias=False)
291
+ )
292
+ (experts): Gemma4TextExperts(
293
+ (act_fn): GELUTanh()
294
+ )
295
+ (post_feedforward_layernorm_1): Gemma4RMSNorm()
296
+ (post_feedforward_layernorm_2): Gemma4RMSNorm()
297
+ (pre_feedforward_layernorm_2): Gemma4RMSNorm()
298
+ )
299
+ )
300
+ (norm): Gemma4RMSNorm()
301
+ (rotary_emb): Gemma4TextRotaryEmbedding()
302
+ )
303
+ (vision_tower): Gemma4VisionModel(
304
+ (patch_embedder): Gemma4VisionPatchEmbedder(
305
+ (input_proj): Linear(in_features=768, out_features=8, bias=False)
306
+ )
307
+ (encoder): Gemma4VisionEncoder(
308
+ (rotary_emb): Gemma4VisionRotaryEmbedding()
309
+ (layers): ModuleList(
310
+ (0-1): 2 x Gemma4VisionEncoderLayer(
311
+ (self_attn): Gemma4VisionAttention(
312
+ (q_proj): Gemma4ClippableLinear(
313
+ (linear): Linear(in_features=8, out_features=128, bias=False)
314
+ )
315
+ (k_proj): Gemma4ClippableLinear(
316
+ (linear): Linear(in_features=8, out_features=128, bias=False)
317
+ )
318
+ (v_proj): Gemma4ClippableLinear(
319
+ (linear): Linear(in_features=8, out_features=128, bias=False)
320
+ )
321
+ (o_proj): Gemma4ClippableLinear(
322
+ (linear): Linear(in_features=128, out_features=8, bias=False)
323
+ )
324
+ (q_norm): Gemma4RMSNorm()
325
+ (k_norm): Gemma4RMSNorm()
326
+ (v_norm): Gemma4RMSNorm()
327
+ )
328
+ (mlp): Gemma4VisionMLP(
329
+ (gate_proj): Gemma4ClippableLinear(
330
+ (linear): Linear(in_features=8, out_features=64, bias=False)
331
+ )
332
+ (up_proj): Gemma4ClippableLinear(
333
+ (linear): Linear(in_features=8, out_features=64, bias=False)
334
+ )
335
+ (down_proj): Gemma4ClippableLinear(
336
+ (linear): Linear(in_features=64, out_features=8, bias=False)
337
+ )
338
+ (act_fn): GELUTanh()
339
+ )
340
+ (input_layernorm): Gemma4RMSNorm()
341
+ (post_attention_layernorm): Gemma4RMSNorm()
342
+ (pre_feedforward_layernorm): Gemma4RMSNorm()
343
+ (post_feedforward_layernorm): Gemma4RMSNorm()
344
+ )
345
+ )
346
+ )
347
+ (pooler): Gemma4VisionPooler()
348
+ )
349
+ (embed_vision): Gemma4MultimodalEmbedder(
350
+ (embedding_projection): Linear(in_features=8, out_features=8, bias=False)
351
+ (embedding_pre_projection_norm): Gemma4RMSNorm()
352
+ )
353
+ )
354
+ (lm_head): Linear(in_features=8, out_features=262144, bias=False)
355
+ )
356
+ ```
357
+
358
+ </details>
chat_template.jinja ADDED
@@ -0,0 +1,266 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- macro format_parameters(properties, required) -%}
2
+ {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}
3
+ {%- set ns = namespace(found_first=false) -%}
4
+ {%- for key, value in properties | dictsort -%}
5
+ {%- set add_comma = false -%}
6
+ {%- if key not in standard_keys -%}
7
+ {%- if ns.found_first %},{% endif -%}
8
+ {%- set ns.found_first = true -%}
9
+ {{ key }}:{
10
+ {%- if value['description'] -%}
11
+ description:<|"|>{{ value['description'] }}<|"|>
12
+ {%- set add_comma = true -%}
13
+ {%- endif -%}
14
+ {%- if value['nullable'] %}
15
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
16
+ nullable:true
17
+ {%- endif -%}
18
+ {%- if value['type'] | upper == 'STRING' -%}
19
+ {%- if value['enum'] -%}
20
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
21
+ enum:{{ format_argument(value['enum']) }}
22
+ {%- endif -%}
23
+ {%- elif value['type'] | upper == 'OBJECT' -%}
24
+ ,properties:{
25
+ {%- if value['properties'] is defined and value['properties'] is mapping -%}
26
+ {{- format_parameters(value['properties'], value['required'] | default([])) -}}
27
+ {%- elif value is mapping -%}
28
+ {{- format_parameters(value, value['required'] | default([])) -}}
29
+ {%- endif -%}
30
+ }
31
+ {%- if value['required'] -%}
32
+ ,required:[
33
+ {%- for item in value['required'] | default([]) -%}
34
+ <|"|>{{- item -}}<|"|>
35
+ {%- if not loop.last %},{% endif -%}
36
+ {%- endfor -%}
37
+ ]
38
+ {%- endif -%}
39
+ {%- elif value['type'] | upper == 'ARRAY' -%}
40
+ {%- if value['items'] is mapping and value['items'] -%}
41
+ ,items:{
42
+ {%- set ns_items = namespace(found_first=false) -%}
43
+ {%- for item_key, item_value in value['items'] | dictsort -%}
44
+ {%- if item_value is not none -%}
45
+ {%- if ns_items.found_first %},{% endif -%}
46
+ {%- set ns_items.found_first = true -%}
47
+ {%- if item_key == 'properties' -%}
48
+ properties:{
49
+ {%- if item_value is mapping -%}
50
+ {{- format_parameters(item_value, value['items']['required'] | default([])) -}}
51
+ {%- endif -%}
52
+ }
53
+ {%- elif item_key == 'required' -%}
54
+ required:[
55
+ {%- for req_item in item_value -%}
56
+ <|"|>{{- req_item -}}<|"|>
57
+ {%- if not loop.last %},{% endif -%}
58
+ {%- endfor -%}
59
+ ]
60
+ {%- elif item_key == 'type' -%}
61
+ {%- if item_value is string -%}
62
+ type:{{ format_argument(item_value | upper) }}
63
+ {%- else -%}
64
+ type:{{ format_argument(item_value | map('upper') | list) }}
65
+ {%- endif -%}
66
+ {%- else -%}
67
+ {{ item_key }}:{{ format_argument(item_value) }}
68
+ {%- endif -%}
69
+ {%- endif -%}
70
+ {%- endfor -%}
71
+ }
72
+ {%- endif -%}
73
+ {%- endif -%}
74
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
75
+ type:<|"|>{{ value['type'] | upper }}<|"|>}
76
+ {%- endif -%}
77
+ {%- endfor -%}
78
+ {%- endmacro -%}
79
+ {%- macro format_function_declaration(tool_data) -%}
80
+ declaration:{{- tool_data['function']['name'] -}}{description:<|"|>{{- tool_data['function']['description'] -}}<|"|>
81
+ {%- set params = tool_data['function']['parameters'] -%}
82
+ {%- if params -%}
83
+ ,parameters:{
84
+ {%- if params['properties'] -%}
85
+ properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
86
+ {%- endif -%}
87
+ {%- if params['required'] -%}
88
+ required:[
89
+ {%- for item in params['required'] -%}
90
+ <|"|>{{- item -}}<|"|>
91
+ {{- ',' if not loop.last -}}
92
+ {%- endfor -%}
93
+ ],
94
+ {%- endif -%}
95
+ {%- if params['type'] -%}
96
+ type:<|"|>{{- params['type'] | upper -}}<|"|>}
97
+ {%- endif -%}
98
+ {%- endif -%}
99
+ {%- if 'response' in tool_data['function'] -%}
100
+ {%- set response_declaration = tool_data['function']['response'] -%}
101
+ ,response:{
102
+ {%- if response_declaration['description'] -%}
103
+ description:<|"|>{{- response_declaration['description'] -}}<|"|>,
104
+ {%- endif -%}
105
+ {%- if response_declaration['type'] | upper == 'OBJECT' -%}
106
+ type:<|"|>{{- response_declaration['type'] | upper -}}<|"|>}
107
+ {%- endif -%}
108
+ {%- endif -%}
109
+ }
110
+ {%- endmacro -%}
111
+ {%- macro format_argument(argument, escape_keys=True) -%}
112
+ {%- if argument is string -%}
113
+ {{- '<|"|>' + argument + '<|"|>' -}}
114
+ {%- elif argument is boolean -%}
115
+ {{- 'true' if argument else 'false' -}}
116
+ {%- elif argument is mapping -%}
117
+ {{- '{' -}}
118
+ {%- set ns = namespace(found_first=false) -%}
119
+ {%- for key, value in argument | dictsort -%}
120
+ {%- if ns.found_first %},{% endif -%}
121
+ {%- set ns.found_first = true -%}
122
+ {%- if escape_keys -%}
123
+ {{- '<|"|>' + key + '<|"|>' -}}
124
+ {%- else -%}
125
+ {{- key -}}
126
+ {%- endif -%}
127
+ :{{- format_argument(value, escape_keys=escape_keys) -}}
128
+ {%- endfor -%}
129
+ {{- '}' -}}
130
+ {%- elif argument is sequence -%}
131
+ {{- '[' -}}
132
+ {%- for item in argument -%}
133
+ {{- format_argument(item, escape_keys=escape_keys) -}}
134
+ {%- if not loop.last %},{% endif -%}
135
+ {%- endfor -%}
136
+ {{- ']' -}}
137
+ {%- else -%}
138
+ {{- argument -}}
139
+ {%- endif -%}
140
+ {%- endmacro -%}
141
+ {%- macro strip_thinking(text) -%}
142
+ {%- set ns = namespace(result='') -%}
143
+ {%- for part in text.split('<channel|>') -%}
144
+ {%- if '<|channel>' in part -%}
145
+ {%- set ns.result = ns.result + part.split('<|channel>')[0] -%}
146
+ {%- else -%}
147
+ {%- set ns.result = ns.result + part -%}
148
+ {%- endif -%}
149
+ {%- endfor -%}
150
+ {{- ns.result | trim -}}
151
+ {%- endmacro -%}
152
+
153
+ {%- set ns = namespace(prev_message_type=None) -%}
154
+ {%- set loop_messages = messages -%}
155
+ {{ bos_token }}
156
+ {#- Handle System/Tool Definitions Block -#}
157
+ {%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
158
+ {{- '<|turn>system\n' -}}
159
+
160
+ {#- Inject Thinking token at the very top of the FIRST system turn -#}
161
+ {%- if enable_thinking is defined and enable_thinking -%}
162
+ {{- '<|think|>' -}}
163
+ {%- set ns.prev_message_type = 'think' -%}
164
+ {%- endif -%}
165
+
166
+ {%- if messages[0]['role'] in ['system', 'developer'] -%}
167
+ {{- messages[0]['content'] | trim -}}
168
+ {%- set loop_messages = messages[1:] -%}
169
+ {%- endif -%}
170
+
171
+ {%- if tools -%}
172
+ {%- for tool in tools %}
173
+ {{- '<|tool>' -}}
174
+ {{- format_function_declaration(tool) | trim -}}
175
+ {{- '<tool|>' -}}
176
+ {%- endfor %}
177
+ {%- set ns.prev_message_type = 'tool' -%}
178
+ {%- endif -%}
179
+
180
+ {{- '<turn|>\n' -}}
181
+ {%- endif %}
182
+
183
+ {#- Loop through messages -#}
184
+ {%- for message in loop_messages -%}
185
+ {%- set ns.prev_message_type = None -%}
186
+ {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
187
+ {{- '<|turn>' + role + '\n' }}
188
+
189
+ {%- if message['tool_calls'] -%}
190
+ {%- for tool_call in message['tool_calls'] -%}
191
+ {%- set function = tool_call['function'] -%}
192
+ {{- '<|tool_call>call:' + function['name'] + '{' -}}
193
+ {%- if function['arguments'] is mapping -%}
194
+ {%- set ns_args = namespace(found_first=false) -%}
195
+ {%- for key, value in function['arguments'] | dictsort -%}
196
+ {%- if ns_args.found_first %},{% endif -%}
197
+ {%- set ns_args.found_first = true -%}
198
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
199
+ {%- endfor -%}
200
+ {%- elif function['arguments'] is string -%}
201
+ {{- function['arguments'] -}}
202
+ {%- endif -%}
203
+ {{- '}<tool_call|>' -}}
204
+ {%- endfor -%}
205
+ {%- set ns.prev_message_type = 'tool_call' -%}
206
+ {%- endif -%}
207
+
208
+ {%- if message['tool_responses'] -%}
209
+ {#- Tool Response handling -#}
210
+ {%- for tool_response in message['tool_responses'] -%}
211
+ {{- '<|tool_response>' -}}
212
+ {%- if tool_response['response'] is mapping -%}
213
+ {{- 'response:' + tool_response['name'] | default('unknown') + '{' -}}
214
+ {%- for key, value in tool_response['response'] | dictsort -%}
215
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
216
+ {%- if not loop.last %},{% endif -%}
217
+ {%- endfor -%}
218
+ {{- '}' -}}
219
+ {%- else -%}
220
+ {{- 'response:' + tool_response['name'] | default('unknown') + '{value:' + format_argument(tool_response['response'], escape_keys=False) + '}' -}}
221
+ {%- endif -%}
222
+ {{- '<tool_response|>' -}}
223
+ {%- endfor -%}
224
+ {%- set ns.prev_message_type = 'tool_response' -%}
225
+ {%- endif -%}
226
+
227
+ {%- if message['content'] is string -%}
228
+ {%- if role == 'model' -%}
229
+ {{- strip_thinking(message['content']) -}}
230
+ {%- else -%}
231
+ {{- message['content'] | trim -}}
232
+ {%- endif -%}
233
+ {%- elif message['content'] is sequence -%}
234
+ {%- for item in message['content'] -%}
235
+ {%- if item['type'] == 'text' -%}
236
+ {%- if role == 'model' -%}
237
+ {{- strip_thinking(item['text']) -}}
238
+ {%- else -%}
239
+ {{- item['text'] | trim -}}
240
+ {%- endif -%}
241
+ {%- elif item['type'] == 'image' -%}
242
+ {{- '\n\n<|image|>\n\n' -}}
243
+ {%- set ns.prev_message_type = 'image' -%}
244
+ {%- elif item['type'] == 'audio' -%}
245
+ {{- '<|audio|>' -}}
246
+ {%- set ns.prev_message_type = 'audio' -%}
247
+ {%- elif item['type'] == 'video' -%}
248
+ {{- '\n\n<|video|>\n\n' -}}
249
+ {%- set ns.prev_message_type = 'video' -%}
250
+ {%- endif -%}
251
+ {%- endfor -%}
252
+ {%- endif -%}
253
+
254
+ {%- if not (message['tool_responses'] and not message['content']) -%}
255
+ {{- '<turn|>\n' -}}
256
+ {%- endif -%}
257
+ {%- endfor -%}
258
+
259
+ {%- if add_generation_prompt -%}
260
+ {%- if ns.prev_message_type != 'tool_response' -%}
261
+ {{- '<|turn>model\n' -}}
262
+ {%- endif -%}
263
+ {%- if not enable_thinking | default(false) -%}
264
+ {{- '<|channel>thought\n<channel|>' -}}
265
+ {%- endif -%}
266
+ {%- endif -%}
config.json ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Gemma4ForConditionalGeneration"
4
+ ],
5
+ "audio_config": null,
6
+ "audio_token_id": 258881,
7
+ "boa_token_id": 256000,
8
+ "boi_token_id": 255999,
9
+ "dtype": "bfloat16",
10
+ "eoa_token_id": 258883,
11
+ "eoa_token_index": 258883,
12
+ "eoi_token_id": 258882,
13
+ "eos_token_id": [
14
+ 1,
15
+ 106
16
+ ],
17
+ "image_token_id": 258880,
18
+ "initializer_range": 0.02,
19
+ "model_type": "gemma4",
20
+ "text_config": {
21
+ "attention_bias": false,
22
+ "attention_dropout": 0.0,
23
+ "attention_k_eq_v": true,
24
+ "bos_token_id": 2,
25
+ "dtype": "bfloat16",
26
+ "enable_moe_block": true,
27
+ "eos_token_id": 1,
28
+ "final_logit_softcapping": 30.0,
29
+ "global_head_dim": 64,
30
+ "head_dim": 32,
31
+ "hidden_activation": "gelu_pytorch_tanh",
32
+ "hidden_size": 8,
33
+ "hidden_size_per_layer_input": 0,
34
+ "initializer_range": 0.02,
35
+ "intermediate_size": 64,
36
+ "layer_types": [
37
+ "sliding_attention",
38
+ "full_attention",
39
+ "sliding_attention",
40
+ "full_attention"
41
+ ],
42
+ "max_position_embeddings": 262144,
43
+ "model_type": "gemma4_text",
44
+ "moe_intermediate_size": 32,
45
+ "num_attention_heads": 8,
46
+ "num_experts": 128,
47
+ "num_global_key_value_heads": 2,
48
+ "num_hidden_layers": 4,
49
+ "num_key_value_heads": 4,
50
+ "num_kv_shared_layers": 0,
51
+ "pad_token_id": 0,
52
+ "rms_norm_eps": 1e-06,
53
+ "rope_parameters": {
54
+ "full_attention": {
55
+ "partial_rotary_factor": 0.25,
56
+ "rope_theta": 1000000.0,
57
+ "rope_type": "proportional"
58
+ },
59
+ "sliding_attention": {
60
+ "rope_theta": 10000.0,
61
+ "rope_type": "default"
62
+ }
63
+ },
64
+ "sliding_window": 1024,
65
+ "tie_word_embeddings": true,
66
+ "top_k_experts": 8,
67
+ "use_bidirectional_attention": "vision",
68
+ "use_cache": true,
69
+ "use_double_wide_mlp": false,
70
+ "vocab_size": 262144,
71
+ "vocab_size_per_layer_input": 262144
72
+ },
73
+ "tie_word_embeddings": true,
74
+ "transformers_version": "5.5.0",
75
+ "video_token_id": 258884,
76
+ "vision_config": {
77
+ "_name_or_path": "",
78
+ "architectures": null,
79
+ "attention_bias": false,
80
+ "attention_dropout": 0.0,
81
+ "chunk_size_feed_forward": 0,
82
+ "default_output_length": 280,
83
+ "dtype": "bfloat16",
84
+ "global_head_dim": 32,
85
+ "head_dim": 32,
86
+ "hidden_activation": "gelu_pytorch_tanh",
87
+ "hidden_size": 8,
88
+ "id2label": {
89
+ "0": "LABEL_0",
90
+ "1": "LABEL_1"
91
+ },
92
+ "initializer_range": 0.02,
93
+ "intermediate_size": 64,
94
+ "is_encoder_decoder": false,
95
+ "label2id": {
96
+ "LABEL_0": 0,
97
+ "LABEL_1": 1
98
+ },
99
+ "max_position_embeddings": 131072,
100
+ "model_type": "gemma4_vision",
101
+ "num_attention_heads": 4,
102
+ "num_hidden_layers": 2,
103
+ "num_key_value_heads": 4,
104
+ "output_attentions": false,
105
+ "output_hidden_states": false,
106
+ "patch_size": 16,
107
+ "pooling_kernel_size": 3,
108
+ "position_embedding_size": 10240,
109
+ "problem_type": null,
110
+ "return_dict": true,
111
+ "rms_norm_eps": 1e-06,
112
+ "rope_parameters": {
113
+ "rope_theta": 100.0,
114
+ "rope_type": "default"
115
+ },
116
+ "standardize": true,
117
+ "use_clipped_linears": false
118
+ },
119
+ "vision_soft_tokens_per_image": 280
120
+ }
generation_config.json ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token_id": 2,
3
+ "do_sample": true,
4
+ "eos_token_id": [
5
+ 1,
6
+ 106,
7
+ 50
8
+ ],
9
+ "pad_token_id": 0,
10
+ "temperature": 1.0,
11
+ "top_k": 64,
12
+ "top_p": 0.95,
13
+ "transformers_version": "5.5.0",
14
+ "trust_remote_code": true
15
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:081898490d9ac4d3c2c636aecef806571d15c1c700cd4ce9c7fda741be8266df
3
+ size 5442600
processor_config.json ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "audio_ms_per_token": 40,
3
+ "audio_seq_length": 750,
4
+ "feature_extractor": {
5
+ "dither": 0.0,
6
+ "feature_extractor_type": "Gemma4AudioFeatureExtractor",
7
+ "feature_size": 128,
8
+ "fft_length": 512,
9
+ "fft_overdrive": false,
10
+ "frame_length": 320,
11
+ "hop_length": 160,
12
+ "input_scale_factor": 1.0,
13
+ "max_frequency": 8000.0,
14
+ "mel_floor": 0.001,
15
+ "min_frequency": 0.0,
16
+ "padding_side": "right",
17
+ "padding_value": 0.0,
18
+ "per_bin_mean": null,
19
+ "per_bin_stddev": null,
20
+ "preemphasis": 0.0,
21
+ "preemphasis_htk_flavor": true,
22
+ "return_attention_mask": true,
23
+ "sampling_rate": 16000
24
+ },
25
+ "image_processor": {
26
+ "do_convert_rgb": true,
27
+ "do_normalize": false,
28
+ "do_rescale": true,
29
+ "do_resize": true,
30
+ "image_mean": [
31
+ 0.0,
32
+ 0.0,
33
+ 0.0
34
+ ],
35
+ "image_processor_type": "Gemma4ImageProcessor",
36
+ "image_seq_length": 280,
37
+ "image_std": [
38
+ 1.0,
39
+ 1.0,
40
+ 1.0
41
+ ],
42
+ "max_soft_tokens": 280,
43
+ "patch_size": 16,
44
+ "pooling_kernel_size": 3,
45
+ "resample": 3,
46
+ "rescale_factor": 0.00392156862745098
47
+ },
48
+ "image_seq_length": 280,
49
+ "processor_class": "Gemma4Processor",
50
+ "video_processor": {
51
+ "do_convert_rgb": true,
52
+ "do_normalize": true,
53
+ "do_rescale": true,
54
+ "do_resize": true,
55
+ "do_sample_frames": true,
56
+ "image_mean": [
57
+ 0.0,
58
+ 0.0,
59
+ 0.0
60
+ ],
61
+ "image_std": [
62
+ 1.0,
63
+ 1.0,
64
+ 1.0
65
+ ],
66
+ "max_soft_tokens": 70,
67
+ "num_frames": 32,
68
+ "patch_size": 16,
69
+ "pooling_kernel_size": 3,
70
+ "resample": 3,
71
+ "rescale_factor": 0.00392156862745098,
72
+ "return_metadata": false,
73
+ "video_processor_type": "Gemma4VideoProcessor"
74
+ }
75
+ }
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:cc8d3a0ce36466ccc1278bf987df5f71db1719b9ca6b4118264f45cb627bfe0f
3
+ size 32169626
tokenizer_config.json ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "audio_token": "<|audio|>",
3
+ "backend": "tokenizers",
4
+ "boa_token": "<|audio>",
5
+ "boi_token": "<|image>",
6
+ "bos_token": "<bos>",
7
+ "eoa_token": "<audio|>",
8
+ "eoc_token": "<channel|>",
9
+ "eoi_token": "<image|>",
10
+ "eos_token": "<eos>",
11
+ "eot_token": "<turn|>",
12
+ "escape_token": "<|\"|>",
13
+ "etc_token": "<tool_call|>",
14
+ "etd_token": "<tool|>",
15
+ "etr_token": "<tool_response|>",
16
+ "extra_special_tokens": [
17
+ "<|video|>"
18
+ ],
19
+ "image_token": "<|image|>",
20
+ "is_local": false,
21
+ "mask_token": "<mask>",
22
+ "model_max_length": 1000000000000000019884624838656,
23
+ "model_specific_special_tokens": {
24
+ "audio_token": "<|audio|>",
25
+ "boa_token": "<|audio>",
26
+ "boi_token": "<|image>",
27
+ "eoa_token": "<audio|>",
28
+ "eoc_token": "<channel|>",
29
+ "eoi_token": "<image|>",
30
+ "eot_token": "<turn|>",
31
+ "escape_token": "<|\"|>",
32
+ "etc_token": "<tool_call|>",
33
+ "etd_token": "<tool|>",
34
+ "etr_token": "<tool_response|>",
35
+ "image_token": "<|image|>",
36
+ "soc_token": "<|channel>",
37
+ "sot_token": "<|turn>",
38
+ "stc_token": "<|tool_call>",
39
+ "std_token": "<|tool>",
40
+ "str_token": "<|tool_response>",
41
+ "think_token": "<|think|>"
42
+ },
43
+ "pad_token": "<pad>",
44
+ "padding_side": "left",
45
+ "processor_class": "Gemma4Processor",
46
+ "response_schema": {
47
+ "properties": {
48
+ "content": {
49
+ "type": "string"
50
+ },
51
+ "role": {
52
+ "const": "assistant"
53
+ },
54
+ "thinking": {
55
+ "type": "string"
56
+ },
57
+ "tool_calls": {
58
+ "items": {
59
+ "properties": {
60
+ "function": {
61
+ "properties": {
62
+ "arguments": {
63
+ "additionalProperties": {},
64
+ "type": "object",
65
+ "x-parser": "gemma4-tool-call"
66
+ },
67
+ "name": {
68
+ "type": "string"
69
+ }
70
+ },
71
+ "type": "object",
72
+ "x-regex": "call\\:(?P<name>\\w+)(?P<arguments>\\{.*\\})"
73
+ },
74
+ "type": {
75
+ "const": "function"
76
+ }
77
+ },
78
+ "type": "object"
79
+ },
80
+ "type": "array",
81
+ "x-regex-iterator": "<\\|tool_call>(.*?)<tool_call\\|>"
82
+ }
83
+ },
84
+ "type": "object",
85
+ "x-regex": "(\\<\\|channel\\>thought\\n(?P<thinking>.*?)\\<channel\\|\\>)?(?P<content>(?:(?!\\<\\|tool_call\\>)(?!\\<turn\\|\\>).)+)?(?P<tool_calls>\\<\\|tool_call\\>.*\\<tool_call\\|\\>)?(?:\\<turn\\|\\>)?"
86
+ },
87
+ "soc_token": "<|channel>",
88
+ "sot_token": "<|turn>",
89
+ "stc_token": "<|tool_call>",
90
+ "std_token": "<|tool>",
91
+ "str_token": "<|tool_response>",
92
+ "think_token": "<|think|>",
93
+ "tokenizer_class": "GemmaTokenizer",
94
+ "unk_token": "<unk>"
95
+ }