Upload folder using huggingface_hub
Browse files- README.md +142 -0
- config.json +41 -0
- generation_config.json +11 -0
- model.safetensors +3 -0
- special_tokens_map.json +30 -0
- tokenizer.json +0 -0
- tokenizer_config.json +132 -0
README.md
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
library_name: transformers
|
3 |
+
pipeline_tag: text-generation
|
4 |
+
inference: true
|
5 |
+
widget:
|
6 |
+
- text: Hello!
|
7 |
+
example_title: Hello world
|
8 |
+
group: Python
|
9 |
+
base_model:
|
10 |
+
- microsoft/Phi-tiny-MoE-instruct
|
11 |
+
---
|
12 |
+
|
13 |
+
This tiny model is for debugging. It is randomly initialized with the config adapted from [microsoft/Phi-tiny-MoE-instruct](https://huggingface.co/microsoft/Phi-tiny-MoE-instruct).
|
14 |
+
|
15 |
+
### Example usage:
|
16 |
+
|
17 |
+
```python
|
18 |
+
import torch
|
19 |
+
|
20 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
21 |
+
|
22 |
+
model_id = "tiny-random/phi-moe"
|
23 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
24 |
+
model = AutoModelForCausalLM.from_pretrained(
|
25 |
+
model_id,
|
26 |
+
torch_dtype=torch.bfloat16,
|
27 |
+
trust_remote_code=True,
|
28 |
+
)
|
29 |
+
pipe = pipeline('text-generation', model=model, tokenizer=tokenizer, trust_remote_code=True)
|
30 |
+
print(pipe('Write an article about Artificial Intelligence.'))
|
31 |
+
```
|
32 |
+
|
33 |
+
### Codes to create this repo:
|
34 |
+
|
35 |
+
```python
|
36 |
+
import json
|
37 |
+
from pathlib import Path
|
38 |
+
|
39 |
+
import torch
|
40 |
+
|
41 |
+
import accelerate
|
42 |
+
from huggingface_hub import file_exists, hf_hub_download
|
43 |
+
from transformers import (
|
44 |
+
AutoConfig,
|
45 |
+
AutoModelForCausalLM,
|
46 |
+
AutoTokenizer,
|
47 |
+
GenerationConfig,
|
48 |
+
set_seed,
|
49 |
+
)
|
50 |
+
|
51 |
+
source_model_id = "microsoft/Phi-tiny-MoE-instruct"
|
52 |
+
save_folder = "/tmp/tiny-random/phi-moe"
|
53 |
+
|
54 |
+
processor = AutoTokenizer.from_pretrained(source_model_id)
|
55 |
+
processor.save_pretrained(save_folder)
|
56 |
+
|
57 |
+
with open(hf_hub_download(source_model_id, filename='config.json', repo_type='model'), 'r', encoding='utf-8') as f:
|
58 |
+
config_json = json.load(f)
|
59 |
+
|
60 |
+
for k, v in config_json['auto_map'].items():
|
61 |
+
config_json['auto_map'][k] = f'{source_model_id}--{v}'
|
62 |
+
config_json['head_dim'] = 32
|
63 |
+
config_json['hidden_size'] = 64
|
64 |
+
config_json['intermediate_size'] = 128
|
65 |
+
config_json['num_attention_heads'] = 2
|
66 |
+
config_json['num_experts_per_tok'] = 2
|
67 |
+
config_json['num_hidden_layers'] = 2
|
68 |
+
config_json['num_key_value_heads'] = 1
|
69 |
+
config_json['num_local_experts'] = 8
|
70 |
+
config_json['tie_word_embeddings'] = True
|
71 |
+
|
72 |
+
with open(f"{save_folder}/config.json", "w", encoding='utf-8') as f:
|
73 |
+
json.dump(config_json, f, indent=2)
|
74 |
+
|
75 |
+
config = AutoConfig.from_pretrained(
|
76 |
+
save_folder,
|
77 |
+
trust_remote_code=True,
|
78 |
+
)
|
79 |
+
print(config)
|
80 |
+
automap = config_json['auto_map']
|
81 |
+
torch.set_default_dtype(torch.bfloat16)
|
82 |
+
model = AutoModelForCausalLM.from_config(config, trust_remote_code=True)
|
83 |
+
torch.set_default_dtype(torch.float32)
|
84 |
+
# according to source model, gat is in FP32
|
85 |
+
for i in range(config.num_hidden_layers):
|
86 |
+
model.model.layers[i].block_sparse_moe.gate.float()
|
87 |
+
if file_exists(filename="generation_config.json", repo_id=source_model_id, repo_type='model'):
|
88 |
+
model.generation_config = GenerationConfig.from_pretrained(
|
89 |
+
source_model_id, trust_remote_code=True,
|
90 |
+
)
|
91 |
+
set_seed(42)
|
92 |
+
model = model.cpu() # cpu is more stable for random initialization across machines
|
93 |
+
with torch.no_grad():
|
94 |
+
for name, p in sorted(model.named_parameters()):
|
95 |
+
torch.nn.init.normal_(p, 0, 0.2)
|
96 |
+
print(name, p.shape)
|
97 |
+
model.save_pretrained(save_folder)
|
98 |
+
print(model)
|
99 |
+
with open(f"{save_folder}/config.json", "r", encoding='utf-8') as f:
|
100 |
+
config_json = json.load(f)
|
101 |
+
config_json['auto_map'] = automap
|
102 |
+
with open(f"{save_folder}/config.json", "w", encoding='utf-8') as f:
|
103 |
+
json.dump(config_json, f, indent=2)
|
104 |
+
for python_file in Path(save_folder).glob('*.py'):
|
105 |
+
python_file.unlink()
|
106 |
+
```
|
107 |
+
|
108 |
+
### Printing the model:
|
109 |
+
|
110 |
+
```text
|
111 |
+
PhiMoEForCausalLM(
|
112 |
+
(model): PhiMoEModel(
|
113 |
+
(embed_tokens): Embedding(32064, 64)
|
114 |
+
(layers): ModuleList(
|
115 |
+
(0-1): 2 x PhiMoEDecoderLayer(
|
116 |
+
(self_attn): PhiMoESdpaAttention(
|
117 |
+
(q_proj): Linear(in_features=64, out_features=64, bias=True)
|
118 |
+
(k_proj): Linear(in_features=64, out_features=32, bias=True)
|
119 |
+
(v_proj): Linear(in_features=64, out_features=32, bias=True)
|
120 |
+
(o_proj): Linear(in_features=64, out_features=64, bias=True)
|
121 |
+
(rotary_emb): PhiMoERotaryEmbedding()
|
122 |
+
)
|
123 |
+
(block_sparse_moe): PhiMoESparseMoeBlock(
|
124 |
+
(gate): Linear(in_features=64, out_features=8, bias=False)
|
125 |
+
(experts): ModuleList(
|
126 |
+
(0-7): 8 x PhiMoEBlockSparseTop2MLP(
|
127 |
+
(w1): Linear(in_features=64, out_features=128, bias=False)
|
128 |
+
(w2): Linear(in_features=128, out_features=64, bias=False)
|
129 |
+
(w3): Linear(in_features=64, out_features=128, bias=False)
|
130 |
+
(act_fn): SiLU()
|
131 |
+
)
|
132 |
+
)
|
133 |
+
)
|
134 |
+
(input_layernorm): LayerNorm((64,), eps=1e-05, elementwise_affine=True)
|
135 |
+
(post_attention_layernorm): LayerNorm((64,), eps=1e-05, elementwise_affine=True)
|
136 |
+
)
|
137 |
+
)
|
138 |
+
(norm): LayerNorm((64,), eps=1e-05, elementwise_affine=True)
|
139 |
+
)
|
140 |
+
(lm_head): Linear(in_features=64, out_features=32064, bias=True)
|
141 |
+
)
|
142 |
+
```
|
config.json
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"PhiMoEForCausalLM"
|
4 |
+
],
|
5 |
+
"attention_bias": true,
|
6 |
+
"attention_dropout": 0.0,
|
7 |
+
"auto_map": {
|
8 |
+
"AutoConfig": "microsoft/Phi-tiny-MoE-instruct--configuration_slimmoe.PhiMoEConfig",
|
9 |
+
"AutoModelForCausalLM": "microsoft/Phi-tiny-MoE-instruct--modeling_slimmoe.PhiMoEForCausalLM"
|
10 |
+
},
|
11 |
+
"bos_token_id": 1,
|
12 |
+
"eos_token_id": 32000,
|
13 |
+
"expert_dropout": 0.0,
|
14 |
+
"head_dim": 32,
|
15 |
+
"hidden_act": "silu",
|
16 |
+
"hidden_dropout": 0.0,
|
17 |
+
"hidden_size": 64,
|
18 |
+
"initializer_range": 0.02,
|
19 |
+
"input_jitter_noise": 0.01,
|
20 |
+
"intermediate_size": 128,
|
21 |
+
"lm_head_bias": true,
|
22 |
+
"max_position_embeddings": 4096,
|
23 |
+
"model_type": "phimoe",
|
24 |
+
"num_attention_heads": 2,
|
25 |
+
"num_experts_per_tok": 2,
|
26 |
+
"num_hidden_layers": 2,
|
27 |
+
"num_key_value_heads": 1,
|
28 |
+
"num_local_experts": 8,
|
29 |
+
"output_router_logits": false,
|
30 |
+
"rms_norm_eps": 1e-05,
|
31 |
+
"rope_scaling": null,
|
32 |
+
"rope_theta": 10000.0,
|
33 |
+
"router_aux_loss_coef": 0.0,
|
34 |
+
"router_jitter_noise": 0.01,
|
35 |
+
"sliding_window": 2047,
|
36 |
+
"tie_word_embeddings": true,
|
37 |
+
"torch_dtype": "bfloat16",
|
38 |
+
"transformers_version": "4.51.3",
|
39 |
+
"use_cache": true,
|
40 |
+
"vocab_size": 32064
|
41 |
+
}
|
generation_config.json
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_from_model_config": true,
|
3 |
+
"bos_token_id": 1,
|
4 |
+
"eos_token_id": [
|
5 |
+
32000,
|
6 |
+
32001,
|
7 |
+
32007
|
8 |
+
],
|
9 |
+
"pad_token_id": 32000,
|
10 |
+
"transformers_version": "4.51.3"
|
11 |
+
}
|
model.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bdf2f1fe515fa0b55cc3be523a7e19a36d27053254bfcdf6f0ecada43c393bcd
|
3 |
+
size 5018960
|
special_tokens_map.json
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token": {
|
3 |
+
"content": "<s>",
|
4 |
+
"lstrip": false,
|
5 |
+
"normalized": false,
|
6 |
+
"rstrip": false,
|
7 |
+
"single_word": false
|
8 |
+
},
|
9 |
+
"eos_token": {
|
10 |
+
"content": "<|endoftext|>",
|
11 |
+
"lstrip": false,
|
12 |
+
"normalized": false,
|
13 |
+
"rstrip": false,
|
14 |
+
"single_word": false
|
15 |
+
},
|
16 |
+
"pad_token": {
|
17 |
+
"content": "<|endoftext|>",
|
18 |
+
"lstrip": false,
|
19 |
+
"normalized": false,
|
20 |
+
"rstrip": false,
|
21 |
+
"single_word": false
|
22 |
+
},
|
23 |
+
"unk_token": {
|
24 |
+
"content": "<unk>",
|
25 |
+
"lstrip": false,
|
26 |
+
"normalized": false,
|
27 |
+
"rstrip": false,
|
28 |
+
"single_word": false
|
29 |
+
}
|
30 |
+
}
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer_config.json
ADDED
@@ -0,0 +1,132 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_bos_token": false,
|
3 |
+
"add_eos_token": false,
|
4 |
+
"add_prefix_space": null,
|
5 |
+
"added_tokens_decoder": {
|
6 |
+
"0": {
|
7 |
+
"content": "<unk>",
|
8 |
+
"lstrip": false,
|
9 |
+
"normalized": false,
|
10 |
+
"rstrip": false,
|
11 |
+
"single_word": false,
|
12 |
+
"special": true
|
13 |
+
},
|
14 |
+
"1": {
|
15 |
+
"content": "<s>",
|
16 |
+
"lstrip": false,
|
17 |
+
"normalized": false,
|
18 |
+
"rstrip": false,
|
19 |
+
"single_word": false,
|
20 |
+
"special": true
|
21 |
+
},
|
22 |
+
"2": {
|
23 |
+
"content": "</s>",
|
24 |
+
"lstrip": false,
|
25 |
+
"normalized": false,
|
26 |
+
"rstrip": true,
|
27 |
+
"single_word": false,
|
28 |
+
"special": false
|
29 |
+
},
|
30 |
+
"32000": {
|
31 |
+
"content": "<|endoftext|>",
|
32 |
+
"lstrip": false,
|
33 |
+
"normalized": false,
|
34 |
+
"rstrip": false,
|
35 |
+
"single_word": false,
|
36 |
+
"special": true
|
37 |
+
},
|
38 |
+
"32001": {
|
39 |
+
"content": "<|assistant|>",
|
40 |
+
"lstrip": false,
|
41 |
+
"normalized": false,
|
42 |
+
"rstrip": true,
|
43 |
+
"single_word": false,
|
44 |
+
"special": true
|
45 |
+
},
|
46 |
+
"32002": {
|
47 |
+
"content": "<|placeholder1|>",
|
48 |
+
"lstrip": false,
|
49 |
+
"normalized": false,
|
50 |
+
"rstrip": true,
|
51 |
+
"single_word": false,
|
52 |
+
"special": true
|
53 |
+
},
|
54 |
+
"32003": {
|
55 |
+
"content": "<|placeholder2|>",
|
56 |
+
"lstrip": false,
|
57 |
+
"normalized": false,
|
58 |
+
"rstrip": true,
|
59 |
+
"single_word": false,
|
60 |
+
"special": true
|
61 |
+
},
|
62 |
+
"32004": {
|
63 |
+
"content": "<|placeholder3|>",
|
64 |
+
"lstrip": false,
|
65 |
+
"normalized": false,
|
66 |
+
"rstrip": true,
|
67 |
+
"single_word": false,
|
68 |
+
"special": true
|
69 |
+
},
|
70 |
+
"32005": {
|
71 |
+
"content": "<|placeholder4|>",
|
72 |
+
"lstrip": false,
|
73 |
+
"normalized": false,
|
74 |
+
"rstrip": true,
|
75 |
+
"single_word": false,
|
76 |
+
"special": true
|
77 |
+
},
|
78 |
+
"32006": {
|
79 |
+
"content": "<|system|>",
|
80 |
+
"lstrip": false,
|
81 |
+
"normalized": false,
|
82 |
+
"rstrip": true,
|
83 |
+
"single_word": false,
|
84 |
+
"special": true
|
85 |
+
},
|
86 |
+
"32007": {
|
87 |
+
"content": "<|end|>",
|
88 |
+
"lstrip": false,
|
89 |
+
"normalized": false,
|
90 |
+
"rstrip": true,
|
91 |
+
"single_word": false,
|
92 |
+
"special": true
|
93 |
+
},
|
94 |
+
"32008": {
|
95 |
+
"content": "<|placeholder5|>",
|
96 |
+
"lstrip": false,
|
97 |
+
"normalized": false,
|
98 |
+
"rstrip": true,
|
99 |
+
"single_word": false,
|
100 |
+
"special": true
|
101 |
+
},
|
102 |
+
"32009": {
|
103 |
+
"content": "<|placeholder6|>",
|
104 |
+
"lstrip": false,
|
105 |
+
"normalized": false,
|
106 |
+
"rstrip": true,
|
107 |
+
"single_word": false,
|
108 |
+
"special": true
|
109 |
+
},
|
110 |
+
"32010": {
|
111 |
+
"content": "<|user|>",
|
112 |
+
"lstrip": false,
|
113 |
+
"normalized": false,
|
114 |
+
"rstrip": true,
|
115 |
+
"single_word": false,
|
116 |
+
"special": true
|
117 |
+
}
|
118 |
+
},
|
119 |
+
"bos_token": "<s>",
|
120 |
+
"chat_template": "{% for message in messages %}{{'<|' + message['role'] + '|>' + '\n' + message['content'] + '<|end|>\n' }}{% endfor %}{% if add_generation_prompt %}{{ '<|assistant|>\n' }}{% else %}{{ eos_token }}{% endif %}",
|
121 |
+
"clean_up_tokenization_spaces": false,
|
122 |
+
"eos_token": "<|endoftext|>",
|
123 |
+
"extra_special_tokens": {},
|
124 |
+
"legacy": false,
|
125 |
+
"model_max_length": 4096,
|
126 |
+
"pad_token": "<|endoftext|>",
|
127 |
+
"padding_side": "left",
|
128 |
+
"sp_model_kwargs": {},
|
129 |
+
"tokenizer_class": "LlamaTokenizerFast",
|
130 |
+
"unk_token": "<unk>",
|
131 |
+
"use_default_system_prompt": false
|
132 |
+
}
|