-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathexpend_embedding.py
78 lines (71 loc) · 2.73 KB
/
expend_embedding.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
model_id = "outputs/ckpt/tiny_llm_sft_92m"
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
new_tokenizer = AutoTokenizer.from_pretrained("tokenizer/tinyllm_tokenizer_hf")
print(len(new_tokenizer)) # 49958
print(model)
"""
TinyllmForCausalLM(
(model): TinyllmModel(
(embed_tokens): Embedding(64798, 512)
(layers): ModuleList(
(0-7): 8 x TinyllmDecoderLayer(
(self_attn): TinyllmSdpaAttention(
(q_proj): Linear(in_features=512, out_features=512, bias=True)
(k_proj): Linear(in_features=512, out_features=512, bias=True)
(v_proj): Linear(in_features=512, out_features=512, bias=True)
(o_proj): Linear(in_features=512, out_features=512, bias=False)
(rotary_emb): TinyllmRotaryEmbedding()
)
(mlp): TinyllmMLP(
(gate_proj): Linear(in_features=512, out_features=1408, bias=False)
(up_proj): Linear(in_features=512, out_features=1408, bias=False)
(down_proj): Linear(in_features=1408, out_features=512, bias=False)
(act_fn): SiLU()
)
(input_layernorm): TinyllmRMSNorm()
(post_attention_layernorm): TinyllmRMSNorm()
)
)
(norm): TinyllmRMSNorm()
)
(lm_head): Linear(in_features=512, out_features=64798, bias=False)
)
"""
embeddings = model.get_input_embeddings()
model.resize_token_embeddings(49958)
model.config.vocab_size = 49958
print(model)
"""
TinyllmForCausalLM(
(model): TinyllmModel(
(embed_tokens): Embedding(49958, 512)
(layers): ModuleList(
(0-7): 8 x TinyllmDecoderLayer(
(self_attn): TinyllmSdpaAttention(
(q_proj): Linear(in_features=512, out_features=512, bias=True)
(k_proj): Linear(in_features=512, out_features=512, bias=True)
(v_proj): Linear(in_features=512, out_features=512, bias=True)
(o_proj): Linear(in_features=512, out_features=512, bias=False)
(rotary_emb): TinyllmRotaryEmbedding()
)
(mlp): TinyllmMLP(
(gate_proj): Linear(in_features=512, out_features=1408, bias=False)
(up_proj): Linear(in_features=512, out_features=1408, bias=False)
(down_proj): Linear(in_features=1408, out_features=512, bias=False)
(act_fn): SiLU()
)
(input_layernorm): TinyllmRMSNorm()
(post_attention_layernorm): TinyllmRMSNorm()
)
)
(norm): TinyllmRMSNorm()
)
(lm_head): Linear(in_features=512, out_features=49958, bias=False)
)
"""
output_dir = "outputs/sft_92m_llama"
model.save_pretrained(output_dir)
new_tokenizer.save_pretrained(output_dir)