danielhanchen commited on
Commit
25ea97b
·
verified ·
1 Parent(s): 66bf451

Add files using upload-large-folder tool

Browse files
Files changed (4) hide show
  1. README.md +52 -0
  2. config.json +9 -8
  3. generation_config.json +1 -1
  4. tokenizer_config.json +27 -49
README.md CHANGED
@@ -117,6 +117,58 @@ Gemma 4 models handle a broad range of tasks across text, vision, and audio. Key
117
  * **Multilingual** – Out-of-the-box support for 35+ languages, pre-trained on 140+ languages.
118
  * **Audio** (E2B and E4B only) – Automatic speech recognition (ASR) and speech-to-translated-text translation across multiple languages.
119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  ## **Best Practices**
121
 
122
  For the best performance, use these configurations and best practices:
 
117
  * **Multilingual** – Out-of-the-box support for 35+ languages, pre-trained on 140+ languages.
118
  * **Audio** (E2B and E4B only) – Automatic speech recognition (ASR) and speech-to-translated-text translation across multiple languages.
119
 
120
+ ## Getting Started
121
+
122
+ You can use all Gemma 4 models with the latest version of Transformers. To get started, install the necessary dependencies in your environment:
123
+
124
+ `pip install -U transformers torch accelerate`
125
+
126
+ Once you have everything installed, you can proceed to load the model with the code below:
127
+
128
+ ```python
129
+ import torch
130
+ from transformers import AutoProcessor, AutoModelForCausalLM
131
+
132
+ MODEL_ID = "google/gemma-4-E4B-it"
133
+
134
+ # Load model
135
+ processor = AutoProcessor.from_pretrained(MODEL_ID)
136
+ model = AutoModelForCausalLM.from_pretrained(
137
+ MODEL_ID,
138
+ dtype=torch.bfloat16,
139
+ device_map="auto"
140
+ )
141
+ ```
142
+
143
+ Once the model is loaded, you can start generating output:
144
+
145
+ ```python
146
+ # Prompt
147
+ messages = [
148
+ {"role": "system", "content": "You are a helpful assistant."},
149
+ {"role": "user", "content": "Write a short joke about saving RAM."},
150
+ ]
151
+
152
+ # Process input
153
+ text = processor.apply_chat_template(
154
+ messages,
155
+ tokenize=False,
156
+ add_generation_prompt=True,
157
+ enable_thinking=False
158
+ )
159
+ inputs = processor(text=text, return_tensors="pt").to(model.device)
160
+ input_len = inputs["input_ids"].shape[-1]
161
+
162
+ # Generate output
163
+ outputs = model.generate(**inputs, max_new_tokens=1024)
164
+ response = processor.decode(outputs[0][input_len:], skip_special_tokens=False)
165
+
166
+ # Parse thinking
167
+ processor.parse_response(response)
168
+ ```
169
+
170
+ To enable reasoning, set `enable_thinking=True` and the `parse_response` function will take care of parsing the thinking output.
171
+
172
  ## **Best Practices**
173
 
174
  For the best performance, use these configurations and best practices:
config.json CHANGED
@@ -12,7 +12,7 @@
12
  "attention_logit_cap": 50.0,
13
  "chunk_size_feed_forward": 0,
14
  "conv_kernel_size": 5,
15
- "torch_dtype": "bfloat16",
16
  "gradient_clipping": 10000000000.0,
17
  "hidden_act": "silu",
18
  "hidden_size": 1024,
@@ -45,21 +45,23 @@
45
  "audio_token_id": 258881,
46
  "boa_token_id": 256000,
47
  "boi_token_id": 255999,
48
- "torch_dtype": "bfloat16",
49
  "eoa_token_id": 258883,
50
  "eoa_token_index": 258883,
51
  "eoi_token_id": 258882,
52
- "eos_token_id": 106,
 
 
 
53
  "image_token_id": 258880,
54
  "initializer_range": 0.02,
55
  "model_type": "gemma4",
56
- "pad_token_id": 0,
57
  "text_config": {
58
  "attention_bias": false,
59
  "attention_dropout": 0.0,
60
  "attention_k_eq_v": false,
61
  "bos_token_id": 2,
62
- "torch_dtype": "bfloat16",
63
  "enable_moe_block": false,
64
  "eos_token_id": 1,
65
  "expert_intermediate_size": null,
@@ -147,7 +149,6 @@
147
  },
148
  "tie_word_embeddings": true,
149
  "transformers_version": "5.5.0.dev0",
150
- "unsloth_fixed": true,
151
  "video_token_id": 258884,
152
  "vision_config": {
153
  "_name_or_path": "",
@@ -156,7 +157,7 @@
156
  "attention_dropout": 0.0,
157
  "chunk_size_feed_forward": 0,
158
  "default_output_length": 280,
159
- "torch_dtype": "bfloat16",
160
  "global_head_dim": 64,
161
  "head_dim": 64,
162
  "hidden_activation": "gelu_pytorch_tanh",
@@ -193,4 +194,4 @@
193
  "use_clipped_linears": true
194
  },
195
  "vision_soft_tokens_per_image": 280
196
- }
 
12
  "attention_logit_cap": 50.0,
13
  "chunk_size_feed_forward": 0,
14
  "conv_kernel_size": 5,
15
+ "dtype": "bfloat16",
16
  "gradient_clipping": 10000000000.0,
17
  "hidden_act": "silu",
18
  "hidden_size": 1024,
 
45
  "audio_token_id": 258881,
46
  "boa_token_id": 256000,
47
  "boi_token_id": 255999,
48
+ "dtype": "bfloat16",
49
  "eoa_token_id": 258883,
50
  "eoa_token_index": 258883,
51
  "eoi_token_id": 258882,
52
+ "eos_token_id": [
53
+ 1,
54
+ 106
55
+ ],
56
  "image_token_id": 258880,
57
  "initializer_range": 0.02,
58
  "model_type": "gemma4",
 
59
  "text_config": {
60
  "attention_bias": false,
61
  "attention_dropout": 0.0,
62
  "attention_k_eq_v": false,
63
  "bos_token_id": 2,
64
+ "dtype": "bfloat16",
65
  "enable_moe_block": false,
66
  "eos_token_id": 1,
67
  "expert_intermediate_size": null,
 
149
  },
150
  "tie_word_embeddings": true,
151
  "transformers_version": "5.5.0.dev0",
 
152
  "video_token_id": 258884,
153
  "vision_config": {
154
  "_name_or_path": "",
 
157
  "attention_dropout": 0.0,
158
  "chunk_size_feed_forward": 0,
159
  "default_output_length": 280,
160
+ "dtype": "bfloat16",
161
  "global_head_dim": 64,
162
  "head_dim": 64,
163
  "hidden_activation": "gelu_pytorch_tanh",
 
194
  "use_clipped_linears": true
195
  },
196
  "vision_soft_tokens_per_image": 280
197
+ }
generation_config.json CHANGED
@@ -11,4 +11,4 @@
11
  "top_k": 64,
12
  "top_p": 0.95,
13
  "transformers_version": "5.5.0.dev0"
14
- }
 
11
  "top_k": 64,
12
  "top_p": 0.95,
13
  "transformers_version": "5.5.0.dev0"
14
+ }
tokenizer_config.json CHANGED
@@ -7,7 +7,7 @@
7
  "eoa_token": "<audio|>",
8
  "eoc_token": "<channel|>",
9
  "eoi_token": "<image|>",
10
- "eos_token": "<turn|>",
11
  "eot_token": "<turn|>",
12
  "escape_token": "<|\"|>",
13
  "etc_token": "<tool_call|>",
@@ -17,72 +17,51 @@
17
  "<|video|>"
18
  ],
19
  "image_token": "<|image|>",
20
- "is_local": true,
21
  "mask_token": "<mask>",
22
- "model_max_length": 131072,
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
  "role": {
49
  "const": "assistant"
50
  },
51
  "thinking": {
52
- "type": "string",
53
- "x-regex": "<\\|channel\\>(?:thought\\n)?(.+?)<channel\\|>"
 
 
54
  },
55
  "tool_calls": {
 
 
56
  "items": {
 
57
  "properties": {
 
 
 
58
  "function": {
 
 
59
  "properties": {
60
- "arguments": {
61
- "type": "string",
62
- "x-mapping-regex": {
63
- "(\\{|,)\\s*([a-zA-Z_]\\w+):": "\\1\"\\2\":",
64
- "<\\|\\\"\\|>": "\""
65
- },
66
- "x-regex": "call:[^{]+(\\{.*\\})"
67
- },
68
  "name": {
69
- "type": "string",
70
- "x-regex": "call:([^{]+)"
 
 
 
 
71
  }
72
- },
73
- "type": "object"
74
- },
75
- "type": {
76
- "const": "function"
77
  }
78
- },
79
- "type": "object"
80
- },
81
- "type": "array",
82
- "x-regex-iterator": "<\\|tool_call\\>(.*?)<tool_call\\|>"
83
  }
84
  },
85
- "type": "object"
86
  },
87
  "soc_token": "<|channel>",
88
  "sot_token": "<|turn>",
@@ -91,6 +70,5 @@
91
  "str_token": "<|tool_response>",
92
  "think_token": "<|think|>",
93
  "tokenizer_class": "GemmaTokenizer",
94
- "unk_token": "<unk>",
95
- "chat_template": "{%- macro format_parameters(properties, required) -%}\n {%- set standard_keys = ['description', 'type', 'properties', 'required', 'nullable'] -%}\n {%- set ns = namespace(found_first=false) -%}\n {%- for key, value in properties | dictsort -%}\n {%- set add_comma = false -%}\n {%- if key not in standard_keys -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {{ key }}:{\n {%- if value['description'] -%}\n description:<|\"|>{{ value['description'] }}<|\"|>\n {%- set add_comma = true -%}\n {%- endif -%}\n {%- if value['nullable'] %}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n nullable:true\n {%- endif -%}\n {%- if value['type'] | upper == 'STRING' -%}\n {%- if value['enum'] -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n enum:{{ format_argument(value['enum']) }}\n {%- endif -%}\n {%- elif value['type'] | upper == 'OBJECT' -%}\n ,properties:{\n {%- if value['properties'] is defined and value['properties'] is mapping -%}\n {{- format_parameters(value['properties'], value['required'] | default([])) -}}\n {%- elif value is mapping -%}\n {{- format_parameters(value, value['required'] | default([])) -}}\n {%- endif -%}\n }\n {%- if value['required'] -%}\n ,required:[\n {%- for item in value['required'] | default([]) -%}\n <|\"|>{{- item -}}<|\"|>\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n {%- endif -%}\n {%- elif value['type'] | upper == 'ARRAY' -%}\n {%- if value['items'] is mapping and value['items'] -%}\n ,items:{\n {%- set ns_items = namespace(found_first=false) -%}\n {%- for item_key, item_value in value['items'] | dictsort -%}\n {%- if item_value is not none -%}\n {%- if ns_items.found_first %},{% endif -%}\n {%- set ns_items.found_first = true -%}\n {%- if item_key == 'properties' -%}\n properties:{\n {%- if item_value is mapping -%}\n {{- format_parameters(item_value, value['items']['required'] | default([])) -}}\n {%- endif -%}\n }\n {%- elif item_key == 'required' -%}\n required:[\n {%- for req_item in item_value -%}\n <|\"|>{{- req_item -}}<|\"|>\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n ]\n {%- elif item_key == 'type' -%}\n {%- if item_value is string -%}\n type:{{ format_argument(item_value | upper) }}\n {%- else -%}\n type:{{ format_argument(item_value | map('upper') | list) }}\n {%- endif -%}\n {%- else -%}\n {{ item_key }}:{{ format_argument(item_value) }}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n }\n {%- endif -%}\n {%- endif -%}\n {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}\n type:<|\"|>{{ value['type'] | upper }}<|\"|>}\n {%- endif -%}\n {%- endfor -%}\n{%- endmacro -%}\n{%- macro format_function_declaration(tool_data) -%}\n declaration:{{- tool_data['function']['name'] -}}{description:<|\"|>{{- tool_data['function']['description'] -}}<|\"|>\n {%- set params = tool_data['function']['parameters'] -%}\n {%- if params -%}\n ,parameters:{\n {%- if params['properties'] -%}\n properties:{ {{- format_parameters(params['properties'], params['required']) -}} },\n {%- endif -%}\n {%- if params['required'] -%}\n required:[\n {%- for item in params['required'] -%}\n <|\"|>{{- item -}}<|\"|>\n {{- ',' if not loop.last -}}\n {%- endfor -%}\n ],\n {%- endif -%}\n {%- if params['type'] -%}\n type:<|\"|>{{- params['type'] | upper -}}<|\"|>}\n {%- endif -%}\n {%- endif -%}\n {%- if 'response' in tool_data['function'] -%}\n {%- set response_declaration = tool_data['function']['response'] -%}\n ,response:{\n {%- if response_declaration['description'] -%}\n description:<|\"|>{{- response_declaration['description'] -}}<|\"|>,\n {%- endif -%}\n {%- if response_declaration['type'] | upper == 'OBJECT' -%}\n type:<|\"|>{{- response_declaration['type'] | upper -}}<|\"|>}\n {%- endif -%}\n {%- endif -%}\n }\n{%- endmacro -%}\n{%- macro format_argument(argument, escape_keys=True) -%}\n {%- if argument is string -%}\n {{- '<|\"|>' + argument + '<|\"|>' -}}\n {%- elif argument is boolean -%}\n {{- 'true' if argument else 'false' -}}\n {%- elif argument is mapping -%}\n {{- '{' -}}\n {%- set ns = namespace(found_first=false) -%}\n {%- for key, value in argument | dictsort -%}\n {%- if ns.found_first %},{% endif -%}\n {%- set ns.found_first = true -%}\n {%- if escape_keys -%}\n {{- '<|\"|>' + key + '<|\"|>' -}}\n {%- else -%}\n {{- key -}}\n {%- endif -%}\n :{{- format_argument(value, escape_keys=escape_keys) -}}\n {%- endfor -%}\n {{- '}' -}}\n {%- elif argument is sequence -%}\n {{- '[' -}}\n {%- for item in argument -%}\n {{- format_argument(item, escape_keys=escape_keys) -}}\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n {{- ']' -}}\n {%- else -%}\n {{- argument -}}\n {%- endif -%}\n{%- endmacro -%}\n{%- macro strip_thinking(text) -%}\n {%- set ns = namespace(result='') -%}\n {%- for part in text.split('<channel|>') -%}\n {%- if '<|channel>' in part -%}\n {%- set ns.result = ns.result + part.split('<|channel>')[0] -%}\n {%- else -%}\n {%- set ns.result = ns.result + part -%}\n {%- endif -%}\n {%- endfor -%}\n {{- ns.result | trim -}}\n{%- endmacro -%}\n\n{%- set ns = namespace(prev_message_type=None) -%}\n{%- set loop_messages = messages -%}\n{{ bos_token }}\n{#- Handle System/Tool Definitions Block -#}\n{%- if (enable_thinking is defined and enable_thinking) or tools or messages[0]['role'] in ['system', 'developer'] -%}\n {{- '<|turn>system\\n' -}}\n\n {#- Inject Thinking token at the very top of the FIRST system turn -#}\n {%- if enable_thinking is defined and enable_thinking -%}\n {{- '<|think|>' -}}\n {%- set ns.prev_message_type = 'think' -%}\n {%- endif -%}\n\n {%- if messages[0]['role'] in ['system', 'developer'] -%}\n {{- messages[0]['content'] | trim -}}\n {%- set loop_messages = messages[1:] -%}\n {%- endif -%}\n\n {%- if tools -%}\n {%- for tool in tools %}\n {{- '<|tool>' -}}\n {{- format_function_declaration(tool) | trim -}}\n {{- '<tool|>' -}}\n {%- endfor %}\n {%- set ns.prev_message_type = 'tool' -%}\n {%- endif -%}\n\n {{- '<turn|>\\n' -}}\n{%- endif %}\n\n{#- Loop through messages -#}\n{%- for message in loop_messages -%}\n {%- set ns.prev_message_type = None -%}\n {%- set role = 'model' if message['role'] == 'assistant' else message['role'] -%}\n {{- '<|turn>' + role + '\\n' }}\n\n {%- if message['tool_calls'] -%}\n {%- for tool_call in message['tool_calls'] -%}\n {%- set function = tool_call['function'] -%}\n {{- '<|tool_call>call:' + function['name'] + '{' -}}\n {%- if function['arguments'] is mapping -%}\n {%- set ns_args = namespace(found_first=false) -%}\n {%- for key, value in function['arguments'] | dictsort -%}\n {%- if ns_args.found_first %},{% endif -%}\n {%- set ns_args.found_first = true -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- endfor -%}\n {%- elif function['arguments'] is string -%}\n {{- function['arguments'] -}}\n {%- endif -%}\n {{- '}<tool_call|>' -}}\n {%- endfor -%}\n {%- set ns.prev_message_type = 'tool_call' -%}\n {%- endif -%}\n\n {%- if message['tool_responses'] -%}\n {#- Tool Response handling -#}\n {%- for tool_response in message['tool_responses'] -%}\n {{- '<|tool_response>' -}}\n {%- if tool_response['response'] is mapping -%}\n {{- 'response:' + tool_response['name'] | default('unknown') + '{' -}}\n {%- for key, value in tool_response['response'] | dictsort -%}\n {{- key -}}:{{- format_argument(value, escape_keys=False) -}}\n {%- if not loop.last %},{% endif -%}\n {%- endfor -%}\n {{- '}' -}}\n {%- else -%}\n {{- 'response:' + tool_response['name'] | default('unknown') + '{value:' + format_argument(tool_response['response'], escape_keys=False) + '}' -}}\n {%- endif -%}\n {{- '<tool_response|>' -}}\n {%- endfor -%}\n {%- set ns.prev_message_type = 'tool_response' -%}\n {%- endif -%}\n\n {%- if message['content'] is string -%}\n {%- if role == 'model' -%}\n {{- strip_thinking(message['content']) -}}\n {%- else -%}\n {{- message['content'] | trim -}}\n {%- endif -%}\n {%- elif message['content'] is sequence -%}\n {%- for item in message['content'] -%}\n {%- if item['type'] == 'text' -%}\n {%- if role == 'model' -%}\n {{- strip_thinking(item['text']) -}}\n {%- else -%}\n {{- item['text'] | trim -}}\n {%- endif -%}\n {%- elif item['type'] == 'image' -%}\n {{- '\\n\\n<|image|>\\n\\n' -}}\n {%- set ns.prev_message_type = 'image' -%}\n {%- elif item['type'] == 'audio' -%}\n {{- '<|audio|>' -}}\n {%- set ns.prev_message_type = 'audio' -%}\n {%- elif item['type'] == 'video' -%}\n {{- '\\n\\n<|video|>\\n\\n' -}}\n {%- set ns.prev_message_type = 'video' -%}\n {%- endif -%}\n {%- endfor -%}\n {%- endif -%}\n\n {%- if not (message['tool_responses'] and not message['content']) -%}\n {{- '<turn|>\\n' -}}\n {%- endif -%}\n{%- endfor -%}\n\n{%- if add_generation_prompt -%}\n {%- if ns.prev_message_type != 'tool_response' -%}\n {{- '<|turn>model\\n' -}}\n {%- endif -%}\n{%- endif -%}"
96
- }
 
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|>",
 
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": "Gemma4Processor",
25
  "response_schema": {
26
+ "type": "object",
27
  "properties": {
28
  "role": {
29
  "const": "assistant"
30
  },
31
  "thinking": {
32
+ "type": "string"
33
+ },
34
+ "content": {
35
+ "type": "string"
36
  },
37
  "tool_calls": {
38
+ "x-regex-iterator": "<\\|tool_call>(.*?)<tool_call\\|>",
39
+ "type": "array",
40
  "items": {
41
+ "type": "object",
42
  "properties": {
43
+ "type": {
44
+ "const": "function"
45
+ },
46
  "function": {
47
+ "type": "object",
48
+ "x-regex": "call\\:(?P<name>\\w+)(?P<arguments>\\{.*\\})",
49
  "properties": {
 
 
 
 
 
 
 
 
50
  "name": {
51
+ "type": "string"
52
+ },
53
+ "arguments": {
54
+ "type": "object",
55
+ "x-parser": "gemma4-tool-call",
56
+ "additionalProperties": {}
57
  }
58
+ }
 
 
 
 
59
  }
60
+ }
61
+ }
 
 
 
62
  }
63
  },
64
+ "x-regex": "(\\<\\|channel\\>thought\\n(?P<thinking>.*?)\\<channel\\|\\>)?(?P<content>(?:(?!\\<\\|tool_call\\>)(?!\\<turn\\|\\>).)+)?(?P<tool_calls>\\<\\|tool_call\\>.*\\<tool_call\\|\\>)?(?:\\<turn\\|\\>)?"
65
  },
66
  "soc_token": "<|channel>",
67
  "sot_token": "<|turn>",
 
70
  "str_token": "<|tool_response>",
71
  "think_token": "<|think|>",
72
  "tokenizer_class": "GemmaTokenizer",
73
+ "unk_token": "<unk>"
74
+ }