andrzejmontano commited on
Commit
79e8ffd
·
verified ·
1 Parent(s): 0054c01

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,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: other
3
+ tags:
4
+ - gemma
5
+ - gemma-4
6
+ - multimodal
7
+ - vision
8
+ - audio
9
+ - mixed-quantization
10
+ - 8bit
11
+ - mlx
12
+ - inference
13
+ library_name: mlx
14
+ language:
15
+ - en
16
+ base_model:
17
+ - google/gemma-4-12b-it
18
+ ---
19
+
20
+ # gemma-4-12B-it-Mixed-8bit
21
+
22
+ **Mixed-precision 8-bit quantization of [Gemma 4 12B IT](https://huggingface.co/google/gemma-4-12b-it)** — Google's unified multimodal model (text + vision + audio + video), optimized for efficient local inference with minimal quality loss.
23
+
24
+ ## Quick Stats
25
+
26
+ | Metric | Value |
27
+ |--------|-------|
28
+ | Base model | [google/gemma-4-12b-it](https://huggingface.co/google/gemma-4-12b-it) |
29
+ | Architecture | Gemma4Unified (48-layer dense, hybrid attention) |
30
+ | Quantization | MLP-only 8-bit, group-size=64 |
31
+ | BF16 size | ~24 GB |
32
+ | **Mixed 8-bit size** | **~15 GB (≈38% reduction)** |
33
+ | Modalities | Text, Vision, Audio, Video |
34
+ | License | Gemma License (inherits from base model) |
35
+
36
+ ## Quantization Strategy
37
+
38
+ This model uses **surgical mixed-precision quantization**: only the SwiGLU MLP projection matrices (`gate_proj`, `up_proj`, `down_proj`) in the language model are quantized to 8-bit. All other components remain in BF16:
39
+
40
+ - ✅ **Quantized (8-bit)**: LLM MLP layers (144 tensors across 48 layers, ~60% of model mass)
41
+ - ✅ **Protected (BF16)**: All attention (sliding + full), vision tower, audio embedder, embeddings, LM head, MultimodalEmbedder, all normalization layers, layer_scalars
42
+
43
+ **Rationale**: MLP layers are "knowledge lookup" / feature transformation layers that are extremely quantization-tolerant. Attention mechanisms carry reasoning and context-window recall. Vision, audio, and multimodal projection layers are kept in BF16 to preserve modality fidelity.
44
+
45
+ ## Model Architecture
46
+
47
+ ### Language Model (Text)
48
+
49
+ - **48 dense layers** in pattern `[sliding_attention × 5, full_attention × 1] × 8`
50
+ - **Hybrid attention**: Sliding window attention (40 layers, head_dim=256) + full global attention (8 layers, head_dim=512)
51
+ - **Heads**: 16 query / 8 KV / 1 global KV
52
+ - **Hidden size**: 3840, **Intermediate**: 15360
53
+ - **Vocab**: 262,144, **Context**: 262,144 tokens
54
+ - **RoPE**: Default (θ=10,000) for sliding; Proportional (θ=1,000,000, partial_rotary=0.25) for full attention
55
+ - **No MoE** (dense architecture)
56
+
57
+ ### Vision Tower
58
+
59
+ - **ViT** with patch_size=16, pooling_kernel=3
60
+ - **280 soft tokens** per image, projected to 3840-dim
61
+ - Position embedding size: 1120
62
+
63
+ ### Audio Tower
64
+
65
+ - **Audio embedder**: 640-dim embedding, 640 samples per token
66
+ - Simple linear projection (no convolutional audio tower in this checkpoint)
67
+
68
+ ### Multimodal
69
+
70
+ - **Unified processor**: `Gemma4UnifiedProcessor` handling text, images, audio, and video
71
+ - **Special tokens**: `<|image|>`, `<|audio|>`, `<|video|>` with BOI/EOI, BOA/EOA delimiters
72
+ - **MultimodalEmbedder** projects vision and audio into the shared 3840-dim text space
73
+
74
+ ## MTP Speculative Decoding
75
+
76
+ This model supports **Multi-Token Prediction (MTP)** speculative decoding via the companion assistant/drafter model. This model achieves 20-30% faster token generation with gemma-4-12B-it-assistant in BF16 format.
77
+
78
+ ## Usage
79
+
80
+ ### oMLX (Recommended)
81
+
82
+ Loads natively with MTP and vision support.
83
+
84
+ ### MLX / mlx-vlm
85
+
86
+ ```python
87
+ from mlx_vlm.utils import load
88
+
89
+ model, processor = load("your-username/gemma-4-12B-it-Mixed-8bit", lazy=True)
90
+ # Model loads in ~15 GB vs ~24 GB for BF16
91
+ ```
92
+
93
+ ## Files
94
+
95
+ | File | Description |
96
+ |------|-------------|
97
+ | `model-00001.safetensors` – `model-00003.safetensors` | Sharded model weights (~5 GB each) |
98
+ | `config.json` | Model configuration with quantization metadata |
99
+ | `tokenizer.json` / `tokenizer_config.json` | Tokenizer (262K vocab) |
100
+ | `chat_template.jinja` | Chat template with thinking/tool support |
101
+ | `generation_config.json` | Generation parameters |
102
+ | `processor_config.json` | Unified processor config |
103
+
104
+ ## Quantization Details
105
+
106
+ | Parameter | Value |
107
+ |-----------|-------|
108
+ | Quantization mode | Affine (8-bit) |
109
+ | Group size | 64 |
110
+ | Layers quantized | 48 (all MLP: gate_proj, up_proj, down_proj) |
111
+ | Tensors quantized | ~144 |
112
+ | Layers protected | Attention (Q/K/V/O), Vision, Audio, Embeddings, Norms, Scalars |
113
+ | Script | `quantization_scripts/mixed_quantize_gemma4_12b.py` |
114
+
115
+ ## Reproducing
116
+
117
+ ```bash
118
+ # Quantize from the BF16 base model
119
+ mlx-env/bin/python quantization_scripts/mixed_quantize_gemma4_12b.py \
120
+ --bits 8 --group-size 64
121
+ ```
122
+
123
+ The script:
124
+ 1. Lazily loads the BF16 checkpoint via `mlx_vlm.utils.load()`
125
+ 2. Applies 8-bit quantization to MLP projections only (via `class_predicate` filter)
126
+ 3. Saves config with quantization metadata
127
+ 4. Flattens, evaluates, and shards to 5 GB safetensors
128
+
129
+ ## Limitations
130
+
131
+ - Mixed quantization may show minor quality degradation on edge cases compared to BF16
132
+ - The vision tower, audio embedder, and multimodal projector are kept in BF16 — they are not quantized in this version
133
+ - Audio support uses a simple linear projection (no convolutional audio tower); complex audio understanding may be limited
134
+ - MTP speculative decoding requires the companion assistant model to be loaded alongside
135
+ - The base model uses `transformers_version: 5.10.0.dev0` — some fields may not be recognized by stable transformers releases yet
136
+
137
+ ## License
138
+
139
+ This model inherits the Apache 2.0 license of the base [google/gemma-4-12b-it](https://huggingface.co/google/gemma-4-12b-it).
chat_template.jinja ADDED
@@ -0,0 +1,363 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- macro format_parameters(properties, required, filter_keys=false) -%}
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 not filter_keys or 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['type'] | upper == 'STRING' -%}
15
+ {%- if value['enum'] -%}
16
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
17
+ enum:{{ format_argument(value['enum']) }}
18
+ {%- endif -%}
19
+ {%- elif value['type'] | upper == 'ARRAY' -%}
20
+ {%- if value['items'] is mapping and value['items'] -%}
21
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
22
+ items:{
23
+ {%- set ns_items = namespace(found_first=false) -%}
24
+ {%- for item_key, item_value in value['items'] | dictsort -%}
25
+ {%- if item_value is not none -%}
26
+ {%- if ns_items.found_first %},{% endif -%}
27
+ {%- set ns_items.found_first = true -%}
28
+ {%- if item_key == 'properties' -%}
29
+ properties:{
30
+ {%- if item_value is mapping -%}
31
+ {{- format_parameters(item_value, value['items']['required'] | default([])) -}}
32
+ {%- endif -%}
33
+ }
34
+ {%- elif item_key == 'required' -%}
35
+ required:[
36
+ {%- for req_item in item_value -%}
37
+ <|"|>{{- req_item -}}<|"|>
38
+ {%- if not loop.last %},{% endif -%}
39
+ {%- endfor -%}
40
+ ]
41
+ {%- elif item_key == 'type' -%}
42
+ {%- if item_value is string -%}
43
+ type:{{ format_argument(item_value | upper) }}
44
+ {%- else -%}
45
+ type:{{ format_argument(item_value | map('upper') | list) }}
46
+ {%- endif -%}
47
+ {%- else -%}
48
+ {{ item_key }}:{{ format_argument(item_value) }}
49
+ {%- endif -%}
50
+ {%- endif -%}
51
+ {%- endfor -%}
52
+ }
53
+ {%- endif -%}
54
+ {%- endif -%}
55
+ {%- if value['nullable'] %}
56
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
57
+ nullable:true
58
+ {%- endif -%}
59
+ {%- if value['type'] | upper == 'OBJECT' -%}
60
+ {%- if value['properties'] is defined and value['properties'] is mapping -%}
61
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
62
+ properties:{
63
+ {{- format_parameters(value['properties'], value['required'] | default([])) -}}
64
+ }
65
+ {%- elif value is mapping -%}
66
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
67
+ properties:{
68
+ {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
69
+ }
70
+ {%- endif -%}
71
+ {%- if value['required'] -%}
72
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
73
+ required:[
74
+ {%- for item in value['required'] | default([]) -%}
75
+ <|"|>{{- item -}}<|"|>
76
+ {%- if not loop.last %},{% endif -%}
77
+ {%- endfor -%}
78
+ ]
79
+ {%- endif -%}
80
+ {%- endif -%}
81
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
82
+ type:<|"|>{{ value['type'] | upper }}<|"|>}
83
+ {%- endif -%}
84
+ {%- endfor -%}
85
+ {%- endmacro -%}
86
+ {%- macro format_function_declaration(tool_data) -%}
87
+ declaration:{{- tool_data['function']['name'] -}}{description:<|"|>{{- tool_data['function']['description'] -}}<|"|>
88
+ {%- set params = tool_data['function']['parameters'] -%}
89
+ {%- if params -%}
90
+ ,parameters:{
91
+ {%- if params['properties'] -%}
92
+ properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
93
+ {%- endif -%}
94
+ {%- if params['required'] -%}
95
+ required:[
96
+ {%- for item in params['required'] -%}
97
+ <|"|>{{- item -}}<|"|>
98
+ {{- ',' if not loop.last -}}
99
+ {%- endfor -%}
100
+ ],
101
+ {%- endif -%}
102
+ {%- if params['type'] -%}
103
+ type:<|"|>{{- params['type'] | upper -}}<|"|>}
104
+ {%- endif -%}
105
+ {%- endif -%}
106
+ {%- if 'response' in tool_data['function'] -%}
107
+ {%- set response_declaration = tool_data['function']['response'] -%}
108
+ ,response:{
109
+ {%- if response_declaration['description'] -%}
110
+ description:<|"|>{{- response_declaration['description'] -}}<|"|>,
111
+ {%- endif -%}
112
+ {%- if response_declaration['type'] | upper == 'OBJECT' -%}
113
+ type:<|"|>{{- response_declaration['type'] | upper -}}<|"|>}
114
+ {%- endif -%}
115
+ {%- endif -%}
116
+ }
117
+ {%- endmacro -%}
118
+ {%- macro format_argument(argument, escape_keys=True) -%}
119
+ {%- if argument is string -%}
120
+ {{- '<|"|>' + argument + '<|"|>' -}}
121
+ {%- elif argument is boolean -%}
122
+ {{- 'true' if argument else 'false' -}}
123
+ {%- elif argument is mapping -%}
124
+ {{- '{' -}}
125
+ {%- set ns = namespace(found_first=false) -%}
126
+ {%- for key, value in argument | dictsort -%}
127
+ {%- if ns.found_first %},{% endif -%}
128
+ {%- set ns.found_first = true -%}
129
+ {%- if escape_keys -%}
130
+ {{- '<|"|>' + key + '<|"|>' -}}
131
+ {%- else -%}
132
+ {{- key -}}
133
+ {%- endif -%}
134
+ :{{- format_argument(value, escape_keys=escape_keys) -}}
135
+ {%- endfor -%}
136
+ {{- '}' -}}
137
+ {%- elif argument is sequence -%}
138
+ {{- '[' -}}
139
+ {%- for item in argument -%}
140
+ {{- format_argument(item, escape_keys=escape_keys) -}}
141
+ {%- if not loop.last %},{% endif -%}
142
+ {%- endfor -%}
143
+ {{- ']' -}}
144
+ {%- else -%}
145
+ {{- argument -}}
146
+ {%- endif -%}
147
+ {%- endmacro -%}
148
+ {%- macro strip_thinking(text) -%}
149
+ {%- set ns = namespace(result='') -%}
150
+ {%- for part in text.split('<channel|>') -%}
151
+ {%- if '<|channel>' in part -%}
152
+ {%- set ns.result = ns.result + part.split('<|channel>')[0] -%}
153
+ {%- else -%}
154
+ {%- set ns.result = ns.result + part -%}
155
+ {%- endif -%}
156
+ {%- endfor -%}
157
+ {{- ns.result | trim -}}
158
+ {%- endmacro -%}
159
+
160
+ {%- macro format_tool_response_block(tool_name, response) -%}
161
+ {{- '<|tool_response>' -}}
162
+ {%- if response is mapping -%}
163
+ {{- 'response:' + tool_name + '{' -}}
164
+ {%- for key, value in response | dictsort -%}
165
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
166
+ {%- if not loop.last %},{% endif -%}
167
+ {%- endfor -%}
168
+ {{- '}' -}}
169
+ {%- else -%}
170
+ {{- 'response:' + tool_name + '{value:' + format_argument(response, escape_keys=False) + '}' -}}
171
+ {%- endif -%}
172
+ {{- '<tool_response|>' -}}
173
+ {%- endmacro -%}
174
+
175
+ {%- set ns = namespace(prev_message_type=None) -%}
176
+ {%- set loop_messages = messages -%}
177
+ {{- bos_token -}}
178
+ {#- Handle System/Tool Definitions Block -#}
179
+ {%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}
180
+ {{- '<|turn>system\n' -}}
181
+ {#- Inject Thinking token at the very top of the FIRST system turn -#}
182
+ {%- if enable_thinking is defined and enable_thinking -%}
183
+ {{- '<|think|>\n' -}}
184
+ {%- set ns.prev_message_type = 'think' -%}
185
+ {%- endif -%}
186
+ {%- if messages[0]['role'] in ['system', 'developer'] -%}
187
+ {%- if messages[0]['content'] is string -%}
188
+ {{- messages[0]['content'] | trim -}}
189
+ {%- elif messages[0]['content'] is sequence -%}
190
+ {%- for item in messages[0]['content'] -%}
191
+ {{- item['text'] | trim + ' '-}}
192
+ {%- endfor -%}
193
+ {%- endif -%}
194
+ {%- set loop_messages = messages[1:] -%}
195
+ {%- endif -%}
196
+ {%- if tools -%}
197
+ {%- for tool in tools %}
198
+ {{- '<|tool>' -}}
199
+ {{- format_function_declaration(tool) | trim -}}
200
+ {{- '<tool|>' -}}
201
+ {%- endfor %}
202
+ {%- set ns.prev_message_type = 'tool' -%}
203
+ {%- endif -%}
204
+ {{- '<turn|>\n' -}}
205
+ {%- endif %}
206
+
207
+ {#- Pre-scan: find last user message index for reasoning guard -#}
208
+ {%- set ns_turn = namespace(last_user_idx=-1) -%}
209
+ {%- for i in range(loop_messages | length) -%}
210
+ {%- if loop_messages[i]['role'] == 'user' -%}
211
+ {%- set ns_turn.last_user_idx = i -%}
212
+ {%- endif -%}
213
+ {%- endfor -%}
214
+
215
+ {#- Loop through messages -#}
216
+ {%- for message in loop_messages -%}
217
+ {%- if message['role'] != 'tool' -%}
218
+ {%- set ns.prev_message_type = None -%}
219
+ {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}
220
+ {#- Detect continuation: suppress duplicate <|turn>model when previous non-tool message was also assistant -#}
221
+ {%- set prev_nt = namespace(role=None, found=false) -%}
222
+ {%- if loop.index0 > 0 -%}
223
+ {%- for j in range(loop.index0 - 1, -1, -1) -%}
224
+ {%- if not prev_nt.found -%}
225
+ {%- if loop_messages[j]['role'] != 'tool' -%}
226
+ {%- set prev_nt.role = loop_messages[j]['role'] -%}
227
+ {%- set prev_nt.found = true -%}
228
+ {%- endif -%}
229
+ {%- endif -%}
230
+ {%- endfor -%}
231
+ {%- endif -%}
232
+ {%- set continue_same_model_turn = (role == 'model' and prev_nt.role == 'assistant') -%}
233
+ {%- if not continue_same_model_turn -%}
234
+ {{- '<|turn>' + role + '\n' }}
235
+ {%- endif -%}
236
+
237
+ {#- Render reasoning/reasoning_content as thinking channel -#}
238
+ {%- set thinking_text = message.get('reasoning') or message.get('reasoning_content') -%}
239
+ {%- if thinking_text and loop.index0 > ns_turn.last_user_idx and message.get('tool_calls') -%}
240
+ {{- '<|channel>thought\n' + thinking_text + '\n<channel|>' -}}
241
+ {%- endif -%}
242
+
243
+ {%- if message['tool_calls'] -%}
244
+ {%- for tool_call in message['tool_calls'] -%}
245
+ {%- set function = tool_call['function'] -%}
246
+ {{- '<|tool_call>call:' + function['name'] + '{' -}}
247
+ {%- if function['arguments'] is mapping -%}
248
+ {%- set ns_args = namespace(found_first=false) -%}
249
+ {%- for key, value in function['arguments'] | dictsort -%}
250
+ {%- if ns_args.found_first %},{% endif -%}
251
+ {%- set ns_args.found_first = true -%}
252
+ {{- key -}}:{{- format_argument(value, escape_keys=False) -}}
253
+ {%- endfor -%}
254
+ {%- elif function['arguments'] is string -%}
255
+ {{- function['arguments'] -}}
256
+ {%- endif -%}
257
+ {{- '}<tool_call|>' -}}
258
+ {%- endfor -%}
259
+ {%- set ns.prev_message_type = 'tool_call' -%}
260
+ {%- endif -%}
261
+
262
+ {%- set ns_tr_out = namespace(flag=false) -%}
263
+ {%- if message.get('tool_responses') -%}
264
+ {#- Legacy: tool_responses embedded on the assistant message (Google/Gemma native) -#}
265
+ {%- for tool_response in message['tool_responses'] -%}
266
+ {{- format_tool_response_block(tool_response['name'] | default('unknown'), tool_response['response']) -}}
267
+ {%- set ns_tr_out.flag = true -%}
268
+ {%- set ns.prev_message_type = 'tool_response' -%}
269
+ {%- endfor -%}
270
+ {%- elif message.get('tool_calls') -%}
271
+ {#- OpenAI Chat Completions: forward-scan consecutive role:tool messages -#}
272
+ {%- set ns_tool_scan = namespace(stopped=false) -%}
273
+ {%- for k in range(loop.index0 + 1, loop_messages | length) -%}
274
+ {%- if ns_tool_scan.stopped -%}
275
+ {%- elif loop_messages[k]['role'] != 'tool' -%}
276
+ {%- set ns_tool_scan.stopped = true -%}
277
+ {%- else -%}
278
+ {%- set follow = loop_messages[k] -%}
279
+ {#- Resolve tool_call_id to function name -#}
280
+ {%- set ns_tname = namespace(name=follow.get('name') | default('unknown')) -%}
281
+ {%- for tc in message['tool_calls'] -%}
282
+ {%- if tc.get('id') == follow.get('tool_call_id') -%}
283
+ {%- set ns_tname.name = tc['function']['name'] -%}
284
+ {%- endif -%}
285
+ {%- endfor -%}
286
+ {#- Handle content as string or content-parts array -#}
287
+ {%- set tool_body = follow.get('content') -%}
288
+ {%- if tool_body is string -%}
289
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
290
+ {%- elif tool_body is sequence and tool_body is not string -%}
291
+ {%- set ns_txt = namespace(s='') -%}
292
+ {%- for part in tool_body -%}
293
+ {%- if part.get('type') == 'text' -%}
294
+ {%- set ns_txt.s = ns_txt.s + (part.get('text') | default('')) -%}
295
+ {%- endif -%}
296
+ {%- endfor -%}
297
+ {{- format_tool_response_block(ns_tname.name, ns_txt.s) -}}
298
+ {%- for part in tool_body -%}
299
+ {%- if part.get('type') == 'image' -%}
300
+ {{- '<|image|>' -}}
301
+ {%- elif part.get('type') == 'audio' -%}
302
+ {{- '<|audio|>' -}}
303
+ {%- elif part.get('type') == 'video' -%}
304
+ {{- '<|video|>' -}}
305
+ {%- endif -%}
306
+ {%- endfor -%}
307
+ {%- else -%}
308
+ {{- format_tool_response_block(ns_tname.name, tool_body) -}}
309
+ {%- endif -%}
310
+ {%- set ns_tr_out.flag = true -%}
311
+ {%- set ns.prev_message_type = 'tool_response' -%}
312
+ {%- endif -%}
313
+ {%- endfor -%}
314
+ {%- endif -%}
315
+
316
+ {%- set captured_content -%}
317
+ {%- if message['content'] is string -%}
318
+ {%- if role == 'model' -%}
319
+ {{- strip_thinking(message['content']) -}}
320
+ {%- else -%}
321
+ {{- message['content'] | trim -}}
322
+ {%- endif -%}
323
+ {%- elif message['content'] is sequence -%}
324
+ {%- for item in message['content'] -%}
325
+ {%- if item['type'] == 'text' -%}
326
+ {%- if role == 'model' -%}
327
+ {{- strip_thinking(item['text']) -}}
328
+ {%- else -%}
329
+ {{- item['text'] | trim -}}
330
+ {%- endif -%}
331
+ {%- elif item['type'] == 'image' -%}
332
+ {{- '<|image|>' -}}
333
+ {%- set ns.prev_message_type = 'image' -%}
334
+ {%- elif item['type'] == 'audio' -%}
335
+ {{- '<|audio|>' -}}
336
+ {%- set ns.prev_message_type = 'audio' -%}
337
+ {%- elif item['type'] == 'video' -%}
338
+ {{- '<|video|>' -}}
339
+ {%- set ns.prev_message_type = 'video' -%}
340
+ {%- endif -%}
341
+ {%- endfor -%}
342
+ {%- endif -%}
343
+ {%- endset -%}
344
+
345
+ {{- captured_content -}}
346
+ {%- set has_content = captured_content | trim | length > 0 -%}
347
+
348
+ {%- if ns.prev_message_type == 'tool_call' and not ns_tr_out.flag -%}
349
+ {{- '<|tool_response>' -}}
350
+ {%- elif not (ns_tr_out.flag and not has_content) -%}
351
+ {{- '<turn|>\n' -}}
352
+ {%- endif -%}
353
+ {%- endif -%}
354
+ {%- endfor -%}
355
+
356
+ {%- if add_generation_prompt -%}
357
+ {%- if ns.prev_message_type != 'tool_response' and ns.prev_message_type != 'tool_call' -%}
358
+ {{- '<|turn>model\n' -}}
359
+ {%- if not enable_thinking | default(false) -%}
360
+ {{- '<|channel>thought\n<channel|>' -}}
361
+ {%- endif -%}
362
+ {%- endif -%}
363
+ {%- endif -%}
config.json ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Gemma4UnifiedForConditionalGeneration"
4
+ ],
5
+ "audio_config": {
6
+ "_name_or_path": "",
7
+ "architectures": null,
8
+ "audio_embed_dim": 640,
9
+ "audio_samples_per_token": 640,
10
+ "chunk_size_feed_forward": 0,
11
+ "dtype": null,
12
+ "hidden_size": 640,
13
+ "id2label": {
14
+ "0": "LABEL_0",
15
+ "1": "LABEL_1"
16
+ },
17
+ "initializer_range": 0.02,
18
+ "is_encoder_decoder": false,
19
+ "label2id": {
20
+ "LABEL_0": 0,
21
+ "LABEL_1": 1
22
+ },
23
+ "model_type": "gemma4_unified_audio",
24
+ "output_attentions": false,
25
+ "output_hidden_states": false,
26
+ "output_proj_dims": 640,
27
+ "problem_type": null,
28
+ "return_dict": true,
29
+ "rms_norm_eps": 1e-06
30
+ },
31
+ "audio_token_id": 258881,
32
+ "boa_token_id": 256000,
33
+ "boi_token_id": 255999,
34
+ "dtype": "bfloat16",
35
+ "eoa_token_index": 258883,
36
+ "eoi_token_id": 258882,
37
+ "eos_token_id": [
38
+ 1,
39
+ 106
40
+ ],
41
+ "image_token_id": 258880,
42
+ "initializer_range": 0.02,
43
+ "model_type": "gemma4_unified",
44
+ "text_config": {
45
+ "attention_bias": false,
46
+ "attention_dropout": 0.0,
47
+ "attention_k_eq_v": true,
48
+ "bos_token_id": 2,
49
+ "enable_moe_block": false,
50
+ "eos_token_id": 1,
51
+ "final_logit_softcapping": 30.0,
52
+ "global_head_dim": 512,
53
+ "head_dim": 256,
54
+ "hidden_activation": "gelu_pytorch_tanh",
55
+ "hidden_size": 3840,
56
+ "hidden_size_per_layer_input": 0,
57
+ "initializer_range": 0.02,
58
+ "intermediate_size": 15360,
59
+ "layer_types": [
60
+ "sliding_attention",
61
+ "sliding_attention",
62
+ "sliding_attention",
63
+ "sliding_attention",
64
+ "sliding_attention",
65
+ "full_attention",
66
+ "sliding_attention",
67
+ "sliding_attention",
68
+ "sliding_attention",
69
+ "sliding_attention",
70
+ "sliding_attention",
71
+ "full_attention",
72
+ "sliding_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
+ "sliding_attention",
83
+ "full_attention",
84
+ "sliding_attention",
85
+ "sliding_attention",
86
+ "sliding_attention",
87
+ "sliding_attention",
88
+ "sliding_attention",
89
+ "full_attention",
90
+ "sliding_attention",
91
+ "sliding_attention",
92
+ "sliding_attention",
93
+ "sliding_attention",
94
+ "sliding_attention",
95
+ "full_attention",
96
+ "sliding_attention",
97
+ "sliding_attention",
98
+ "sliding_attention",
99
+ "sliding_attention",
100
+ "sliding_attention",
101
+ "full_attention",
102
+ "sliding_attention",
103
+ "sliding_attention",
104
+ "sliding_attention",
105
+ "sliding_attention",
106
+ "sliding_attention",
107
+ "full_attention"
108
+ ],
109
+ "max_position_embeddings": 262144,
110
+ "model_type": "gemma4_unified_text",
111
+ "moe_intermediate_size": null,
112
+ "num_attention_heads": 16,
113
+ "num_experts": null,
114
+ "num_global_key_value_heads": 1,
115
+ "num_hidden_layers": 48,
116
+ "num_key_value_heads": 8,
117
+ "num_kv_shared_layers": 0,
118
+ "pad_token_id": 0,
119
+ "rms_norm_eps": 1e-06,
120
+ "rope_parameters": {
121
+ "full_attention": {
122
+ "partial_rotary_factor": 0.25,
123
+ "rope_theta": 1000000.0,
124
+ "rope_type": "proportional"
125
+ },
126
+ "sliding_attention": {
127
+ "rope_theta": 10000.0,
128
+ "rope_type": "default"
129
+ }
130
+ },
131
+ "sliding_window": 1024,
132
+ "tie_word_embeddings": true,
133
+ "top_k_experts": null,
134
+ "use_bidirectional_attention": "vision",
135
+ "use_cache": true,
136
+ "use_double_wide_mlp": false,
137
+ "vocab_size": 262144,
138
+ "vocab_size_per_layer_input": 262144
139
+ },
140
+ "tie_word_embeddings": true,
141
+ "transformers_version": "5.10.0.dev0",
142
+ "video_token_id": 258884,
143
+ "vision_config": {
144
+ "_name_or_path": "",
145
+ "architectures": null,
146
+ "chunk_size_feed_forward": 0,
147
+ "dtype": null,
148
+ "id2label": {
149
+ "0": "LABEL_0",
150
+ "1": "LABEL_1"
151
+ },
152
+ "initializer_range": 0.02,
153
+ "is_encoder_decoder": false,
154
+ "label2id": {
155
+ "LABEL_0": 0,
156
+ "LABEL_1": 1
157
+ },
158
+ "mm_embed_dim": 3840,
159
+ "mm_posemb_size": 1120,
160
+ "model_patch_size": 48,
161
+ "model_type": "gemma4_unified_vision",
162
+ "num_soft_tokens": 280,
163
+ "output_attentions": false,
164
+ "output_hidden_states": false,
165
+ "output_proj_dims": 3840,
166
+ "patch_size": 16,
167
+ "pooling_kernel_size": 3,
168
+ "problem_type": null,
169
+ "return_dict": true,
170
+ "rms_norm_eps": 1e-06
171
+ },
172
+ "quantization": {
173
+ "group_size": 64,
174
+ "bits": 8,
175
+ "mode": "affine",
176
+ "type": "mixed",
177
+ "description": "Gemma 4 gemma-4-12B-it MLP-only 8-bit quantization. Attention, vision tower, audio tower, embeddings, norms, per-layer projections, and scalars remain BF16."
178
+ }
179
+ }
generation_config.json ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "suppress_tokens": [
11
+ 258883,
12
+ 258882
13
+ ],
14
+ "temperature": 1.0,
15
+ "top_k": 64,
16
+ "top_p": 0.95,
17
+ "transformers_version": "5.10.0.dev0"
18
+ }
model-00001.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1e122e2900f5bade2c44590d71a3690cebcaeb00cead1e4cc4360f71c0bfb53b
3
+ size 5406610101
model-00002.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5d073718dc5e3b16da97fca429682949a1ff379d2142f832eaf415c8283568ac
3
+ size 5409340654
model-00003.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:929ed7248ea5f9a11a190b96b0044529219eb308ee17e5475e6e6e0b94408f73
3
+ size 5141009866
processor_config.json ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "audio_ms_per_token": 40,
3
+ "audio_seq_length": 750,
4
+ "feature_extractor": {
5
+ "audio_samples_per_token": 640,
6
+ "feature_extractor_type": "Gemma4UnifiedAudioFeatureExtractor",
7
+ "feature_size": 640,
8
+ "padding_side": "right",
9
+ "padding_value": 0.0,
10
+ "return_attention_mask": true,
11
+ "sampling_rate": 16000
12
+ },
13
+ "image_processor": {
14
+ "do_convert_rgb": true,
15
+ "do_normalize": false,
16
+ "do_rescale": true,
17
+ "do_resize": true,
18
+ "image_mean": [
19
+ 0.0,
20
+ 0.0,
21
+ 0.0
22
+ ],
23
+ "image_processor_type": "Gemma4UnifiedImageProcessor",
24
+ "image_std": [
25
+ 1.0,
26
+ 1.0,
27
+ 1.0
28
+ ],
29
+ "max_soft_tokens": 280,
30
+ "patch_size": 16,
31
+ "pooling_kernel_size": 3,
32
+ "resample": 3,
33
+ "rescale_factor": 0.00392156862745098
34
+ },
35
+ "image_seq_length": 280,
36
+ "processor_class": "Gemma4UnifiedProcessor",
37
+ "video_processor": {
38
+ "do_normalize": true,
39
+ "do_rescale": true,
40
+ "image_mean": [
41
+ 0.0,
42
+ 0.0,
43
+ 0.0
44
+ ],
45
+ "image_std": [
46
+ 1.0,
47
+ 1.0,
48
+ 1.0
49
+ ],
50
+ "max_soft_tokens": 70,
51
+ "num_frames": 32,
52
+ "patch_size": 16,
53
+ "pooling_kernel_size": 3,
54
+ "rescale_factor": 0.00392156862745098
55
+ }
56
+ }
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,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "mask_token": "<mask>",
21
+ "model_max_length": 1000000000000000019884624838656,
22
+ "pad_token": "<pad>",
23
+ "padding_side": "left",
24
+ "processor_class": "Gemma4UnifiedProcessor",
25
+ "response_schema": {
26
+ "properties": {
27
+ "content": {
28
+ "type": "string"
29
+ },
30
+ "role": {
31
+ "const": "assistant"
32
+ },
33
+ "thinking": {
34
+ "type": "string"
35
+ },
36
+ "tool_calls": {
37
+ "items": {
38
+ "properties": {
39
+ "function": {
40
+ "properties": {
41
+ "arguments": {
42
+ "additionalProperties": {},
43
+ "type": "object",
44
+ "x-parser": "gemma4-tool-call"
45
+ },
46
+ "name": {
47
+ "type": "string"
48
+ }
49
+ },
50
+ "type": "object",
51
+ "x-regex": "call\\:(?P<name>\\w+)(?P<arguments>\\{.*\\})"
52
+ },
53
+ "type": {
54
+ "const": "function"
55
+ }
56
+ },
57
+ "type": "object"
58
+ },
59
+ "type": "array",
60
+ "x-regex-iterator": "<\\|tool_call>(.*?)<tool_call\\|>"
61
+ }
62
+ },
63
+ "type": "object",
64
+ "x-regex": "(\\<\\|channel\\>thought\\n(?P<thinking>.*?)\\<channel\\|\\>)?(?P<tool_calls>\\<\\|tool_call\\>.*\\<tool_call\\|\\>)?(?P<content>(?:(?!\\<turn\\|\\>)(?!\\<\\|tool_response\\>).)+)?(?:\\<turn\\|\\>|\\<\\|tool_response\\>)?"
65
+ },
66
+ "soc_token": "<|channel>",
67
+ "sot_token": "<|turn>",
68
+ "stc_token": "<|tool_call>",
69
+ "std_token": "<|tool>",
70
+ "str_token": "<|tool_response>",
71
+ "think_token": "<|think|>",
72
+ "tokenizer_class": "GemmaTokenizer",
73
+ "unk_token": "<unk>"
74
+ }