yujiepan commited on
Commit
fc00f3b
·
verified ·
1 Parent(s): a8075d4

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
.meta.json ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ {
2
+ "torch": "2.12.0",
3
+ "transformers": "5.9.0"
4
+ }
README.md ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ base_model:
4
+ - google/gemma-4-31B-it-assistant
5
+ ---
6
+
7
+ This tiny model is intended for debugging. It is randomly initialized using the configuration adapted from [google/gemma-4-31B-it-assistant](https://huggingface.co/google/gemma-4-31B-it-assistant).
8
+
9
+ | File path | Size |
10
+ |------|------|
11
+ | model.safetensors | 4.3MB |
12
+
13
+
14
+ ### Example usage:
15
+
16
+ ```python
17
+ import torch
18
+ from transformers import AutoModelForCausalLM, AutoProcessor, AutoModelForMultimodalLM
19
+
20
+ model_id = "tiny-random/gemma-4-assistant"
21
+ target_model_id = "tiny-random/gemma-4-moe"
22
+
23
+ processor = AutoProcessor.from_pretrained(target_model_id)
24
+ target_model = AutoModelForMultimodalLM.from_pretrained(
25
+ target_model_id,
26
+ dtype=torch.bfloat16,
27
+ device_map="auto",
28
+ )
29
+ assistant_model = AutoModelForCausalLM.from_pretrained(
30
+ model_id,
31
+ dtype=torch.bfloat16,
32
+ device_map="auto",
33
+ )
34
+ messages = [
35
+ {
36
+ "role": "user",
37
+ "content": [
38
+ {
39
+ "type": "image",
40
+ "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG",
41
+ },
42
+ {"type": "text", "text": "What is shown in this image?"},
43
+ ],
44
+ },
45
+ # {
46
+ # "role": "assistant",
47
+ # "content": [{"type": "text", "text": "Dummy response for image"}],
48
+ # },
49
+ # {
50
+ # "role": "user",
51
+ # "content": [
52
+ # {
53
+ # "type": "video",
54
+ # "video": "https://github.com/bebechien/gemma/raw/refs/heads/main/videos/ForBiggerBlazes.mp4",
55
+ # },
56
+ # {"type": "text", "text": "Describe this video."},
57
+ # ],
58
+ # },
59
+ ]
60
+ inputs = processor.apply_chat_template(
61
+ messages,
62
+ tokenize=True,
63
+ return_dict=True,
64
+ return_tensors="pt",
65
+ add_generation_prompt=True,
66
+ ).to(target_model.device)
67
+ input_len = inputs["input_ids"].shape[-1]
68
+ print("input_len:", input_len)
69
+ outputs = target_model.generate(
70
+ **inputs,
71
+ assistant_model=assistant_model,
72
+ max_new_tokens=32,
73
+ )
74
+ response = processor.decode(outputs[0], skip_special_tokens=False)
75
+ response = response.replace("<|image|>", "I")
76
+ response = response.replace("<|video|>", "V")
77
+ print(response)
78
+ ```
79
+
80
+ ### Codes to create this repo:
81
+
82
+ <details>
83
+ <summary>Click to expand</summary>
84
+
85
+ ```python
86
+ import json
87
+ from pathlib import Path
88
+
89
+ import torch
90
+ from huggingface_hub import file_exists, hf_hub_download
91
+
92
+ from transformers import (
93
+ AutoConfig,
94
+ AutoModelForCausalLM,
95
+ AutoProcessor,
96
+ AutoTokenizer,
97
+ Gemma4AssistantForCausalLM,
98
+ Gemma4ForConditionalGeneration,
99
+ GenerationConfig,
100
+ set_seed,
101
+ )
102
+
103
+ source_model_id = "google/gemma-4-31B-it-assistant"
104
+ save_folder = "/tmp/tiny-random/gemma-4-assistant"
105
+
106
+ processor = AutoProcessor.from_pretrained(source_model_id)
107
+ processor.save_pretrained(save_folder)
108
+
109
+ with open(
110
+ hf_hub_download(source_model_id, filename="config.json", repo_type="model"),
111
+ "r",
112
+ encoding="utf-8",
113
+ ) as f:
114
+ config_json = json.load(f)
115
+
116
+ config_json["backbone_hidden_size"] = 8
117
+ config_json["text_config"].update(
118
+ {
119
+ "global_head_dim": 64,
120
+ "head_dim": 32,
121
+ "hidden_size": 8,
122
+ "intermediate_size": 64,
123
+ "layer_types": [
124
+ "sliding_attention",
125
+ "sliding_attention",
126
+ "sliding_attention",
127
+ "full_attention",
128
+ ],
129
+ "moe_intermediate_size": 32,
130
+ "num_attention_heads": 8,
131
+ "num_hidden_layers": 4,
132
+ "num_key_value_heads": 4,
133
+ }
134
+ )
135
+
136
+ with open(f"{save_folder}/config.json", "w", encoding="utf-8") as f:
137
+ json.dump(config_json, f, indent=2)
138
+
139
+ config = AutoConfig.from_pretrained(
140
+ save_folder,
141
+ trust_remote_code=True,
142
+ )
143
+ print(config)
144
+
145
+ torch.set_default_dtype(torch.bfloat16)
146
+ model = Gemma4AssistantForCausalLM(config)
147
+ torch.set_default_dtype(torch.float32)
148
+ if file_exists(filename="generation_config.json", repo_id=source_model_id, repo_type="model"):
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 = sum(p.numel() for p in model.parameters())
156
+ with torch.no_grad():
157
+ for name, p in sorted(model.named_parameters()):
158
+ torch.nn.init.normal_(p, 0, 0.2)
159
+ print(name, p.shape, f"{p.numel() / all_numels * 100: .4f}%")
160
+ model.save_pretrained(save_folder)
161
+ ```
162
+
163
+ </details>
164
+
165
+ ### Printing the model:
166
+
167
+ <details><summary>Click to expand</summary>
168
+
169
+ ```text
170
+ Gemma4AssistantForCausalLM(
171
+ (model): Gemma4TextModel(
172
+ (embed_tokens): Gemma4TextScaledWordEmbedding(262144, 8, padding_idx=0)
173
+ (layers): ModuleList(
174
+ (0-2): 3 x Gemma4TextDecoderLayer(
175
+ (self_attn): Gemma4TextAttention(
176
+ (q_proj): Linear(in_features=8, out_features=256, bias=False)
177
+ (q_norm): Gemma4RMSNorm()
178
+ (o_proj): Linear(in_features=256, out_features=8, bias=False)
179
+ )
180
+ (mlp): Gemma4TextMLP(
181
+ (gate_proj): Linear(in_features=8, out_features=64, bias=False)
182
+ (up_proj): Linear(in_features=8, out_features=64, bias=False)
183
+ (down_proj): Linear(in_features=64, out_features=8, bias=False)
184
+ (act_fn): GELUTanh()
185
+ )
186
+ (input_layernorm): Gemma4RMSNorm()
187
+ (post_attention_layernorm): Gemma4RMSNorm()
188
+ (pre_feedforward_layernorm): Gemma4RMSNorm()
189
+ (post_feedforward_layernorm): Gemma4RMSNorm()
190
+ )
191
+ (3): Gemma4TextDecoderLayer(
192
+ (self_attn): Gemma4TextAttention(
193
+ (q_proj): Linear(in_features=8, out_features=512, bias=False)
194
+ (q_norm): Gemma4RMSNorm()
195
+ (o_proj): Linear(in_features=512, out_features=8, bias=False)
196
+ )
197
+ (mlp): Gemma4TextMLP(
198
+ (gate_proj): Linear(in_features=8, out_features=64, bias=False)
199
+ (up_proj): Linear(in_features=8, out_features=64, bias=False)
200
+ (down_proj): Linear(in_features=64, out_features=8, bias=False)
201
+ (act_fn): GELUTanh()
202
+ )
203
+ (input_layernorm): Gemma4RMSNorm()
204
+ (post_attention_layernorm): Gemma4RMSNorm()
205
+ (pre_feedforward_layernorm): Gemma4RMSNorm()
206
+ (post_feedforward_layernorm): Gemma4RMSNorm()
207
+ )
208
+ )
209
+ (norm): Gemma4RMSNorm()
210
+ (rotary_emb): Gemma4TextRotaryEmbedding()
211
+ )
212
+ (lm_head): Linear(in_features=8, out_features=262144, bias=False)
213
+ (pre_projection): Linear(in_features=16, out_features=8, bias=False)
214
+ (post_projection): Linear(in_features=8, out_features=8, bias=False)
215
+ )
216
+ ```
217
+
218
+ </details>
219
+
220
+ ### Test environment:
221
+
222
+ - torch: 2.12.0
223
+ - transformers: 5.9.0
config.json ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "Gemma4AssistantForCausalLM"
4
+ ],
5
+ "audio_token_id": 258881,
6
+ "backbone_hidden_size": 8,
7
+ "boa_token_id": 256000,
8
+ "boi_token_id": 255999,
9
+ "centroid_intermediate_top_k": 32,
10
+ "dtype": "bfloat16",
11
+ "eoa_token_id": 258883,
12
+ "eoi_token_id": 258882,
13
+ "image_token_id": 258880,
14
+ "model_type": "gemma4_assistant",
15
+ "num_centroids": 2048,
16
+ "text_config": {
17
+ "_name_or_path": "",
18
+ "architectures": null,
19
+ "attention_bias": false,
20
+ "attention_dropout": 0.0,
21
+ "attention_k_eq_v": true,
22
+ "bos_token_id": 2,
23
+ "chunk_size_feed_forward": 0,
24
+ "dtype": "bfloat16",
25
+ "enable_moe_block": false,
26
+ "eos_token_id": 1,
27
+ "final_logit_softcapping": null,
28
+ "global_head_dim": 64,
29
+ "head_dim": 32,
30
+ "hidden_activation": "gelu_pytorch_tanh",
31
+ "hidden_size": 8,
32
+ "hidden_size_per_layer_input": 0,
33
+ "id2label": {
34
+ "0": "LABEL_0",
35
+ "1": "LABEL_1"
36
+ },
37
+ "initializer_range": 0.02,
38
+ "intermediate_size": 64,
39
+ "is_encoder_decoder": false,
40
+ "label2id": {
41
+ "LABEL_0": 0,
42
+ "LABEL_1": 1
43
+ },
44
+ "layer_types": [
45
+ "sliding_attention",
46
+ "sliding_attention",
47
+ "sliding_attention",
48
+ "full_attention"
49
+ ],
50
+ "max_position_embeddings": 262144,
51
+ "model_type": "gemma4_text",
52
+ "moe_intermediate_size": 32,
53
+ "num_attention_heads": 8,
54
+ "num_experts": null,
55
+ "num_global_key_value_heads": 4,
56
+ "num_hidden_layers": 4,
57
+ "num_key_value_heads": 4,
58
+ "num_kv_shared_layers": 4,
59
+ "output_attentions": false,
60
+ "output_hidden_states": false,
61
+ "pad_token_id": 0,
62
+ "problem_type": null,
63
+ "return_dict": true,
64
+ "rms_norm_eps": 1e-06,
65
+ "rope_parameters": {
66
+ "full_attention": {
67
+ "partial_rotary_factor": 0.25,
68
+ "rope_theta": 1000000.0,
69
+ "rope_type": "proportional"
70
+ },
71
+ "sliding_attention": {
72
+ "rope_theta": 10000.0,
73
+ "rope_type": "default"
74
+ }
75
+ },
76
+ "sliding_window": 1024,
77
+ "tie_word_embeddings": true,
78
+ "top_k_experts": null,
79
+ "use_bidirectional_attention": null,
80
+ "use_cache": true,
81
+ "use_double_wide_mlp": false,
82
+ "vocab_size": 262144,
83
+ "vocab_size_per_layer_input": 0
84
+ },
85
+ "tie_word_embeddings": true,
86
+ "transformers_version": "5.9.0",
87
+ "use_ordered_embeddings": false
88
+ }
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
+ "is_assistant": true,
10
+ "num_assistant_tokens": 6,
11
+ "num_assistant_tokens_schedule": "constant",
12
+ "pad_token_id": 0,
13
+ "temperature": 1.0,
14
+ "top_k": 64,
15
+ "top_p": 0.95,
16
+ "transformers_version": "5.9.0",
17
+ "trust_remote_code": true
18
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c2d24ea45870ac8e6a035df98973bb49244814eabe093b8fbf5d85ab040d6348
3
+ size 4253552
tokenizer.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:75a6583c1a418e2bbd79c60d95d28e0f5bf549ad3f2990b5bdb5238c6c2bf70c
3
+ size 32169440
tokenizer_config.json ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "image_token": "<|image|>",
18
+ "is_local": false,
19
+ "local_files_only": false,
20
+ "mask_token": "<mask>",
21
+ "model_max_length": 1000000000000000019884624838656,
22
+ "model_specific_special_tokens": {
23
+ "audio_token": "<|audio|>",
24
+ "boa_token": "<|audio>",
25
+ "boi_token": "<|image>",
26
+ "eoa_token": "<audio|>",
27
+ "eoc_token": "<channel|>",
28
+ "eoi_token": "<image|>",
29
+ "eot_token": "<turn|>",
30
+ "escape_token": "<|\"|>",
31
+ "etc_token": "<tool_call|>",
32
+ "etd_token": "<tool|>",
33
+ "etr_token": "<tool_response|>",
34
+ "image_token": "<|image|>",
35
+ "soc_token": "<|channel>",
36
+ "sot_token": "<|turn>",
37
+ "stc_token": "<|tool_call>",
38
+ "std_token": "<|tool>",
39
+ "str_token": "<|tool_response>",
40
+ "think_token": "<|think|>"
41
+ },
42
+ "pad_token": "<pad>",
43
+ "padding_side": "left",
44
+ "soc_token": "<|channel>",
45
+ "sot_token": "<|turn>",
46
+ "stc_token": "<|tool_call>",
47
+ "std_token": "<|tool>",
48
+ "str_token": "<|tool_response>",
49
+ "think_token": "<|think|>",
50
+ "tokenizer_class": "GemmaTokenizer",
51
+ "unk_token": "<unk>"
52
+ }