Commit
•
da3476d
0
Parent(s):
Duplicate from Alibaba-NLP/gte-Qwen2-1.5B-instruct
Browse filesCo-authored-by: yanzhao <zyznull@users.noreply.huggingface.co>
- .gitattributes +35 -0
- 1_Pooling/config.json +10 -0
- README.md +0 -0
- added_tokens.json +5 -0
- config.json +32 -0
- config_sentence_transformers.json +11 -0
- generation_config.json +6 -0
- merges.txt +0 -0
- model-00001-of-00002.safetensors +3 -0
- model-00002-of-00002.safetensors +3 -0
- model.safetensors.index.json +346 -0
- modeling_qwen.py +1429 -0
- modules.json +20 -0
- scripts/eval_mteb.py +732 -0
- sentence_bert_config.json +4 -0
- special_tokens_map.json +20 -0
- tokenization_qwen.py +267 -0
- tokenizer.json +0 -0
- tokenizer_config.json +47 -0
- vocab.json +0 -0
.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz 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
|
1_Pooling/config.json
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"word_embedding_dimension": 1536,
|
3 |
+
"pooling_mode_cls_token": false,
|
4 |
+
"pooling_mode_mean_tokens": false,
|
5 |
+
"pooling_mode_max_tokens": false,
|
6 |
+
"pooling_mode_mean_sqrt_len_tokens": false,
|
7 |
+
"pooling_mode_weightedmean_tokens": false,
|
8 |
+
"pooling_mode_lasttoken": true,
|
9 |
+
"include_prompt": true
|
10 |
+
}
|
README.md
ADDED
The diff for this file is too large to render.
See raw diff
|
|
added_tokens.json
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"<|endoftext|>": 151643,
|
3 |
+
"<|im_end|>": 151645,
|
4 |
+
"<|im_start|>": 151644
|
5 |
+
}
|
config.json
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"architectures": [
|
3 |
+
"Qwen2ForCausalLM"
|
4 |
+
],
|
5 |
+
"attention_dropout": 0.0,
|
6 |
+
"auto_map": {
|
7 |
+
"AutoModel": "modeling_qwen.Qwen2Model",
|
8 |
+
"AutoModelForCausalLM": "modeling_qwen.Qwen2ForCausalLM",
|
9 |
+
"AutoModelForSequenceClassification": "modeling_qwen.Qwen2ForSequenceClassification"
|
10 |
+
},
|
11 |
+
"bos_token_id": 151643,
|
12 |
+
"eos_token_id": 151643,
|
13 |
+
"hidden_act": "silu",
|
14 |
+
"hidden_size": 1536,
|
15 |
+
"initializer_range": 0.02,
|
16 |
+
"intermediate_size": 8960,
|
17 |
+
"max_position_embeddings": 131072,
|
18 |
+
"max_window_layers": 21,
|
19 |
+
"model_type": "qwen2",
|
20 |
+
"num_attention_heads": 12,
|
21 |
+
"num_hidden_layers": 28,
|
22 |
+
"num_key_value_heads": 2,
|
23 |
+
"rms_norm_eps": 1e-06,
|
24 |
+
"rope_theta": 1000000.0,
|
25 |
+
"sliding_window": 131072,
|
26 |
+
"tie_word_embeddings": false,
|
27 |
+
"torch_dtype": "float32",
|
28 |
+
"transformers_version": "4.41.2",
|
29 |
+
"use_cache": true,
|
30 |
+
"use_sliding_window": false,
|
31 |
+
"vocab_size": 151646
|
32 |
+
}
|
config_sentence_transformers.json
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"__version__": {
|
3 |
+
"sentence_transformers": "2.7.0",
|
4 |
+
"transformers": "4.39.3",
|
5 |
+
"pytorch": "2.1.0+cu121"
|
6 |
+
},
|
7 |
+
"prompts": {
|
8 |
+
"query": "Instruct: Given a web search query, retrieve relevant passages that answer the query\nQuery: "
|
9 |
+
},
|
10 |
+
"default_prompt_name": null
|
11 |
+
}
|
generation_config.json
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token_id": 151643,
|
3 |
+
"eos_token_id": 151643,
|
4 |
+
"max_new_tokens": 2048,
|
5 |
+
"transformers_version": "4.41.2"
|
6 |
+
}
|
merges.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
model-00001-of-00002.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0842014813f9ecd814eac671e2577d84281cd02d74f70436583c044fac724072
|
3 |
+
size 4994888704
|
model-00002-of-00002.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:661830fd8d0426f38747a747ec37cbdc05466331790dcf7b83e12a0e000ce0cc
|
3 |
+
size 2109938216
|
model.safetensors.index.json
ADDED
@@ -0,0 +1,346 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"metadata": {
|
3 |
+
"total_size": 7104788480
|
4 |
+
},
|
5 |
+
"weight_map": {
|
6 |
+
"lm_head.weight": "model-00002-of-00002.safetensors",
|
7 |
+
"model.embed_tokens.weight": "model-00001-of-00002.safetensors",
|
8 |
+
"model.layers.0.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
9 |
+
"model.layers.0.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
10 |
+
"model.layers.0.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
11 |
+
"model.layers.0.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
12 |
+
"model.layers.0.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
13 |
+
"model.layers.0.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
14 |
+
"model.layers.0.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
15 |
+
"model.layers.0.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
16 |
+
"model.layers.0.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
17 |
+
"model.layers.0.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
18 |
+
"model.layers.0.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
19 |
+
"model.layers.0.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
20 |
+
"model.layers.1.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
21 |
+
"model.layers.1.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
22 |
+
"model.layers.1.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
23 |
+
"model.layers.1.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
24 |
+
"model.layers.1.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
25 |
+
"model.layers.1.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
26 |
+
"model.layers.1.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
27 |
+
"model.layers.1.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
28 |
+
"model.layers.1.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
29 |
+
"model.layers.1.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
30 |
+
"model.layers.1.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
31 |
+
"model.layers.1.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
32 |
+
"model.layers.10.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
33 |
+
"model.layers.10.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
34 |
+
"model.layers.10.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
35 |
+
"model.layers.10.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
36 |
+
"model.layers.10.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
37 |
+
"model.layers.10.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
38 |
+
"model.layers.10.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
39 |
+
"model.layers.10.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
40 |
+
"model.layers.10.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
41 |
+
"model.layers.10.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
42 |
+
"model.layers.10.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
43 |
+
"model.layers.10.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
44 |
+
"model.layers.11.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
45 |
+
"model.layers.11.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
46 |
+
"model.layers.11.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
47 |
+
"model.layers.11.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
48 |
+
"model.layers.11.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
49 |
+
"model.layers.11.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
50 |
+
"model.layers.11.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
51 |
+
"model.layers.11.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
52 |
+
"model.layers.11.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
53 |
+
"model.layers.11.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
54 |
+
"model.layers.11.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
55 |
+
"model.layers.11.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
56 |
+
"model.layers.12.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
57 |
+
"model.layers.12.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
58 |
+
"model.layers.12.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
59 |
+
"model.layers.12.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
60 |
+
"model.layers.12.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
61 |
+
"model.layers.12.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
62 |
+
"model.layers.12.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
63 |
+
"model.layers.12.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
64 |
+
"model.layers.12.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
65 |
+
"model.layers.12.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
66 |
+
"model.layers.12.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
67 |
+
"model.layers.12.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
68 |
+
"model.layers.13.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
69 |
+
"model.layers.13.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
70 |
+
"model.layers.13.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
71 |
+
"model.layers.13.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
72 |
+
"model.layers.13.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
73 |
+
"model.layers.13.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
74 |
+
"model.layers.13.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
75 |
+
"model.layers.13.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
76 |
+
"model.layers.13.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
77 |
+
"model.layers.13.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
78 |
+
"model.layers.13.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
79 |
+
"model.layers.13.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
80 |
+
"model.layers.14.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
81 |
+
"model.layers.14.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
82 |
+
"model.layers.14.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
83 |
+
"model.layers.14.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
84 |
+
"model.layers.14.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
85 |
+
"model.layers.14.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
86 |
+
"model.layers.14.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
87 |
+
"model.layers.14.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
88 |
+
"model.layers.14.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
89 |
+
"model.layers.14.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
90 |
+
"model.layers.14.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
91 |
+
"model.layers.14.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
92 |
+
"model.layers.15.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
93 |
+
"model.layers.15.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
94 |
+
"model.layers.15.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
95 |
+
"model.layers.15.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
96 |
+
"model.layers.15.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
97 |
+
"model.layers.15.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
98 |
+
"model.layers.15.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
99 |
+
"model.layers.15.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
100 |
+
"model.layers.15.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
101 |
+
"model.layers.15.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
102 |
+
"model.layers.15.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
103 |
+
"model.layers.15.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
104 |
+
"model.layers.16.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
105 |
+
"model.layers.16.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
106 |
+
"model.layers.16.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
107 |
+
"model.layers.16.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
108 |
+
"model.layers.16.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
109 |
+
"model.layers.16.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
110 |
+
"model.layers.16.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
111 |
+
"model.layers.16.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
112 |
+
"model.layers.16.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
113 |
+
"model.layers.16.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
114 |
+
"model.layers.16.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
115 |
+
"model.layers.16.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
116 |
+
"model.layers.17.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
117 |
+
"model.layers.17.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
118 |
+
"model.layers.17.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
119 |
+
"model.layers.17.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
120 |
+
"model.layers.17.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
121 |
+
"model.layers.17.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
122 |
+
"model.layers.17.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
123 |
+
"model.layers.17.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
124 |
+
"model.layers.17.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
125 |
+
"model.layers.17.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
126 |
+
"model.layers.17.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
127 |
+
"model.layers.17.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
128 |
+
"model.layers.18.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
129 |
+
"model.layers.18.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
130 |
+
"model.layers.18.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
131 |
+
"model.layers.18.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
132 |
+
"model.layers.18.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
133 |
+
"model.layers.18.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
134 |
+
"model.layers.18.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
135 |
+
"model.layers.18.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
136 |
+
"model.layers.18.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
137 |
+
"model.layers.18.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
138 |
+
"model.layers.18.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
139 |
+
"model.layers.18.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
140 |
+
"model.layers.19.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
141 |
+
"model.layers.19.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
142 |
+
"model.layers.19.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
143 |
+
"model.layers.19.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
144 |
+
"model.layers.19.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
145 |
+
"model.layers.19.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
146 |
+
"model.layers.19.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
147 |
+
"model.layers.19.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
148 |
+
"model.layers.19.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
149 |
+
"model.layers.19.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
150 |
+
"model.layers.19.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
151 |
+
"model.layers.19.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
152 |
+
"model.layers.2.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
153 |
+
"model.layers.2.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
154 |
+
"model.layers.2.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
155 |
+
"model.layers.2.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
156 |
+
"model.layers.2.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
157 |
+
"model.layers.2.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
158 |
+
"model.layers.2.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
159 |
+
"model.layers.2.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
160 |
+
"model.layers.2.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
161 |
+
"model.layers.2.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
162 |
+
"model.layers.2.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
163 |
+
"model.layers.2.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
164 |
+
"model.layers.20.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
165 |
+
"model.layers.20.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
166 |
+
"model.layers.20.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
167 |
+
"model.layers.20.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
168 |
+
"model.layers.20.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
169 |
+
"model.layers.20.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
170 |
+
"model.layers.20.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
171 |
+
"model.layers.20.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
172 |
+
"model.layers.20.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
173 |
+
"model.layers.20.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
174 |
+
"model.layers.20.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
175 |
+
"model.layers.20.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
176 |
+
"model.layers.21.input_layernorm.weight": "model-00002-of-00002.safetensors",
|
177 |
+
"model.layers.21.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
|
178 |
+
"model.layers.21.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
179 |
+
"model.layers.21.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
180 |
+
"model.layers.21.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
|
181 |
+
"model.layers.21.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
182 |
+
"model.layers.21.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
183 |
+
"model.layers.21.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
184 |
+
"model.layers.21.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
185 |
+
"model.layers.21.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
186 |
+
"model.layers.21.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
187 |
+
"model.layers.21.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
188 |
+
"model.layers.22.input_layernorm.weight": "model-00002-of-00002.safetensors",
|
189 |
+
"model.layers.22.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
|
190 |
+
"model.layers.22.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
|
191 |
+
"model.layers.22.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
|
192 |
+
"model.layers.22.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
|
193 |
+
"model.layers.22.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
|
194 |
+
"model.layers.22.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
|
195 |
+
"model.layers.22.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
|
196 |
+
"model.layers.22.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
|
197 |
+
"model.layers.22.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
|
198 |
+
"model.layers.22.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
|
199 |
+
"model.layers.22.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
|
200 |
+
"model.layers.23.input_layernorm.weight": "model-00002-of-00002.safetensors",
|
201 |
+
"model.layers.23.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
|
202 |
+
"model.layers.23.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
|
203 |
+
"model.layers.23.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
|
204 |
+
"model.layers.23.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
|
205 |
+
"model.layers.23.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
|
206 |
+
"model.layers.23.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
|
207 |
+
"model.layers.23.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
|
208 |
+
"model.layers.23.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
|
209 |
+
"model.layers.23.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
|
210 |
+
"model.layers.23.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
|
211 |
+
"model.layers.23.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
|
212 |
+
"model.layers.24.input_layernorm.weight": "model-00002-of-00002.safetensors",
|
213 |
+
"model.layers.24.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
|
214 |
+
"model.layers.24.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
|
215 |
+
"model.layers.24.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
|
216 |
+
"model.layers.24.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
|
217 |
+
"model.layers.24.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
|
218 |
+
"model.layers.24.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
|
219 |
+
"model.layers.24.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
|
220 |
+
"model.layers.24.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
|
221 |
+
"model.layers.24.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
|
222 |
+
"model.layers.24.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
|
223 |
+
"model.layers.24.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
|
224 |
+
"model.layers.25.input_layernorm.weight": "model-00002-of-00002.safetensors",
|
225 |
+
"model.layers.25.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
|
226 |
+
"model.layers.25.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
|
227 |
+
"model.layers.25.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
|
228 |
+
"model.layers.25.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
|
229 |
+
"model.layers.25.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
|
230 |
+
"model.layers.25.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
|
231 |
+
"model.layers.25.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
|
232 |
+
"model.layers.25.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
|
233 |
+
"model.layers.25.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
|
234 |
+
"model.layers.25.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
|
235 |
+
"model.layers.25.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
|
236 |
+
"model.layers.26.input_layernorm.weight": "model-00002-of-00002.safetensors",
|
237 |
+
"model.layers.26.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
|
238 |
+
"model.layers.26.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
|
239 |
+
"model.layers.26.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
|
240 |
+
"model.layers.26.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
|
241 |
+
"model.layers.26.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
|
242 |
+
"model.layers.26.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
|
243 |
+
"model.layers.26.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
|
244 |
+
"model.layers.26.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
|
245 |
+
"model.layers.26.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
|
246 |
+
"model.layers.26.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
|
247 |
+
"model.layers.26.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
|
248 |
+
"model.layers.27.input_layernorm.weight": "model-00002-of-00002.safetensors",
|
249 |
+
"model.layers.27.mlp.down_proj.weight": "model-00002-of-00002.safetensors",
|
250 |
+
"model.layers.27.mlp.gate_proj.weight": "model-00002-of-00002.safetensors",
|
251 |
+
"model.layers.27.mlp.up_proj.weight": "model-00002-of-00002.safetensors",
|
252 |
+
"model.layers.27.post_attention_layernorm.weight": "model-00002-of-00002.safetensors",
|
253 |
+
"model.layers.27.self_attn.k_proj.bias": "model-00002-of-00002.safetensors",
|
254 |
+
"model.layers.27.self_attn.k_proj.weight": "model-00002-of-00002.safetensors",
|
255 |
+
"model.layers.27.self_attn.o_proj.weight": "model-00002-of-00002.safetensors",
|
256 |
+
"model.layers.27.self_attn.q_proj.bias": "model-00002-of-00002.safetensors",
|
257 |
+
"model.layers.27.self_attn.q_proj.weight": "model-00002-of-00002.safetensors",
|
258 |
+
"model.layers.27.self_attn.v_proj.bias": "model-00002-of-00002.safetensors",
|
259 |
+
"model.layers.27.self_attn.v_proj.weight": "model-00002-of-00002.safetensors",
|
260 |
+
"model.layers.3.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
261 |
+
"model.layers.3.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
262 |
+
"model.layers.3.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
263 |
+
"model.layers.3.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
264 |
+
"model.layers.3.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
265 |
+
"model.layers.3.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
266 |
+
"model.layers.3.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
267 |
+
"model.layers.3.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
268 |
+
"model.layers.3.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
269 |
+
"model.layers.3.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
270 |
+
"model.layers.3.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
271 |
+
"model.layers.3.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
272 |
+
"model.layers.4.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
273 |
+
"model.layers.4.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
274 |
+
"model.layers.4.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
275 |
+
"model.layers.4.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
276 |
+
"model.layers.4.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
277 |
+
"model.layers.4.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
278 |
+
"model.layers.4.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
279 |
+
"model.layers.4.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
280 |
+
"model.layers.4.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
281 |
+
"model.layers.4.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
282 |
+
"model.layers.4.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
283 |
+
"model.layers.4.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
284 |
+
"model.layers.5.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
285 |
+
"model.layers.5.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
286 |
+
"model.layers.5.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
287 |
+
"model.layers.5.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
288 |
+
"model.layers.5.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
289 |
+
"model.layers.5.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
290 |
+
"model.layers.5.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
291 |
+
"model.layers.5.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
292 |
+
"model.layers.5.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
293 |
+
"model.layers.5.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
294 |
+
"model.layers.5.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
295 |
+
"model.layers.5.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
296 |
+
"model.layers.6.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
297 |
+
"model.layers.6.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
298 |
+
"model.layers.6.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
299 |
+
"model.layers.6.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
300 |
+
"model.layers.6.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
301 |
+
"model.layers.6.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
302 |
+
"model.layers.6.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
303 |
+
"model.layers.6.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
304 |
+
"model.layers.6.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
305 |
+
"model.layers.6.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
306 |
+
"model.layers.6.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
307 |
+
"model.layers.6.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
308 |
+
"model.layers.7.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
309 |
+
"model.layers.7.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
310 |
+
"model.layers.7.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
311 |
+
"model.layers.7.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
312 |
+
"model.layers.7.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
313 |
+
"model.layers.7.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
314 |
+
"model.layers.7.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
315 |
+
"model.layers.7.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
316 |
+
"model.layers.7.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
317 |
+
"model.layers.7.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
318 |
+
"model.layers.7.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
319 |
+
"model.layers.7.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
320 |
+
"model.layers.8.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
321 |
+
"model.layers.8.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
322 |
+
"model.layers.8.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
323 |
+
"model.layers.8.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
324 |
+
"model.layers.8.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
325 |
+
"model.layers.8.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
326 |
+
"model.layers.8.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
327 |
+
"model.layers.8.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
328 |
+
"model.layers.8.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
329 |
+
"model.layers.8.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
330 |
+
"model.layers.8.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
331 |
+
"model.layers.8.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
332 |
+
"model.layers.9.input_layernorm.weight": "model-00001-of-00002.safetensors",
|
333 |
+
"model.layers.9.mlp.down_proj.weight": "model-00001-of-00002.safetensors",
|
334 |
+
"model.layers.9.mlp.gate_proj.weight": "model-00001-of-00002.safetensors",
|
335 |
+
"model.layers.9.mlp.up_proj.weight": "model-00001-of-00002.safetensors",
|
336 |
+
"model.layers.9.post_attention_layernorm.weight": "model-00001-of-00002.safetensors",
|
337 |
+
"model.layers.9.self_attn.k_proj.bias": "model-00001-of-00002.safetensors",
|
338 |
+
"model.layers.9.self_attn.k_proj.weight": "model-00001-of-00002.safetensors",
|
339 |
+
"model.layers.9.self_attn.o_proj.weight": "model-00001-of-00002.safetensors",
|
340 |
+
"model.layers.9.self_attn.q_proj.bias": "model-00001-of-00002.safetensors",
|
341 |
+
"model.layers.9.self_attn.q_proj.weight": "model-00001-of-00002.safetensors",
|
342 |
+
"model.layers.9.self_attn.v_proj.bias": "model-00001-of-00002.safetensors",
|
343 |
+
"model.layers.9.self_attn.v_proj.weight": "model-00001-of-00002.safetensors",
|
344 |
+
"model.norm.weight": "model-00002-of-00002.safetensors"
|
345 |
+
}
|
346 |
+
}
|
modeling_qwen.py
ADDED
@@ -0,0 +1,1429 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2024 The Qwen team, Alibaba Group and the HuggingFace Inc. team. All rights reserved.
|
3 |
+
#
|
4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
5 |
+
# and OPT implementations in this library. It has been modified from its
|
6 |
+
# original forms to accommodate minor architectural differences compared
|
7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
8 |
+
#
|
9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10 |
+
# you may not use this file except in compliance with the License.
|
11 |
+
# You may obtain a copy of the License at
|
12 |
+
#
|
13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14 |
+
#
|
15 |
+
# Unless required by applicable law or agreed to in writing, software
|
16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18 |
+
# See the License for the specific language governing permissions and
|
19 |
+
# limitations under the License.
|
20 |
+
""" PyTorch Qwen2 model."""
|
21 |
+
from transformers import Qwen2Config
|
22 |
+
import inspect
|
23 |
+
import math
|
24 |
+
import os
|
25 |
+
import warnings
|
26 |
+
from typing import List, Optional, Tuple, Union
|
27 |
+
|
28 |
+
import torch
|
29 |
+
import torch.nn.functional as F
|
30 |
+
import torch.utils.checkpoint
|
31 |
+
from torch import nn
|
32 |
+
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
|
33 |
+
|
34 |
+
from transformers.activations import ACT2FN
|
35 |
+
from transformers.cache_utils import Cache, DynamicCache
|
36 |
+
from transformers.modeling_attn_mask_utils import _prepare_4d_causal_attention_mask, _prepare_4d_causal_attention_mask_for_sdpa, _prepare_4d_attention_mask, _prepare_4d_attention_mask_for_sdpa
|
37 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, SequenceClassifierOutputWithPast
|
38 |
+
from transformers.modeling_utils import PreTrainedModel
|
39 |
+
from transformers.utils import (
|
40 |
+
add_start_docstrings,
|
41 |
+
add_start_docstrings_to_model_forward,
|
42 |
+
is_flash_attn_2_available,
|
43 |
+
is_flash_attn_greater_or_equal_2_10,
|
44 |
+
logging,
|
45 |
+
replace_return_docstrings,
|
46 |
+
)
|
47 |
+
|
48 |
+
|
49 |
+
if is_flash_attn_2_available():
|
50 |
+
from flash_attn import flash_attn_func, flash_attn_varlen_func
|
51 |
+
from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa
|
52 |
+
|
53 |
+
_flash_supports_window_size = "window_size" in list(inspect.signature(flash_attn_func).parameters)
|
54 |
+
|
55 |
+
|
56 |
+
logger = logging.get_logger(__name__)
|
57 |
+
|
58 |
+
|
59 |
+
_CHECKPOINT_FOR_DOC = "Qwen/Qwen2-7B-beta"
|
60 |
+
_CONFIG_FOR_DOC = "Qwen2Config"
|
61 |
+
|
62 |
+
QWEN2_PRETRAINED_MODEL_ARCHIVE_LIST = [
|
63 |
+
"Qwen/Qwen2-7B-beta",
|
64 |
+
# See all Qwen2 models at https://huggingface.co/models?filter=qwen2
|
65 |
+
]
|
66 |
+
|
67 |
+
|
68 |
+
# Copied from transformers.models.llama.modeling_llama._get_unpad_data
|
69 |
+
def _get_unpad_data(attention_mask):
|
70 |
+
seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
|
71 |
+
indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten()
|
72 |
+
max_seqlen_in_batch = seqlens_in_batch.max().item()
|
73 |
+
cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.int32), (1, 0))
|
74 |
+
return (
|
75 |
+
indices,
|
76 |
+
cu_seqlens,
|
77 |
+
max_seqlen_in_batch,
|
78 |
+
)
|
79 |
+
|
80 |
+
|
81 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaRMSNorm with Llama->Qwen2
|
82 |
+
class Qwen2RMSNorm(nn.Module):
|
83 |
+
def __init__(self, hidden_size, eps=1e-6):
|
84 |
+
"""
|
85 |
+
Qwen2RMSNorm is equivalent to T5LayerNorm
|
86 |
+
"""
|
87 |
+
super().__init__()
|
88 |
+
self.weight = nn.Parameter(torch.ones(hidden_size))
|
89 |
+
self.variance_epsilon = eps
|
90 |
+
|
91 |
+
def forward(self, hidden_states):
|
92 |
+
input_dtype = hidden_states.dtype
|
93 |
+
hidden_states = hidden_states.to(torch.float32)
|
94 |
+
variance = hidden_states.pow(2).mean(-1, keepdim=True)
|
95 |
+
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
|
96 |
+
return self.weight * hidden_states.to(input_dtype)
|
97 |
+
|
98 |
+
|
99 |
+
# Copied from transformers.models.mistral.modeling_mistral.MistralRotaryEmbedding with Mistral->Qwen2
|
100 |
+
class Qwen2RotaryEmbedding(nn.Module):
|
101 |
+
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
|
102 |
+
super().__init__()
|
103 |
+
|
104 |
+
self.dim = dim
|
105 |
+
self.max_position_embeddings = max_position_embeddings
|
106 |
+
self.base = base
|
107 |
+
inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2, dtype=torch.int64).float().to(device) / self.dim))
|
108 |
+
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
109 |
+
|
110 |
+
# Build here to make `torch.jit.trace` work.
|
111 |
+
self._set_cos_sin_cache(
|
112 |
+
seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
|
113 |
+
)
|
114 |
+
|
115 |
+
def _set_cos_sin_cache(self, seq_len, device, dtype):
|
116 |
+
self.max_seq_len_cached = seq_len
|
117 |
+
t = torch.arange(self.max_seq_len_cached, device=device, dtype=torch.int64).type_as(self.inv_freq)
|
118 |
+
|
119 |
+
freqs = torch.outer(t, self.inv_freq)
|
120 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
121 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
122 |
+
self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False)
|
123 |
+
self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False)
|
124 |
+
|
125 |
+
def forward(self, x, seq_len=None):
|
126 |
+
# x: [bs, num_attention_heads, seq_len, head_size]
|
127 |
+
if seq_len > self.max_seq_len_cached:
|
128 |
+
self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype)
|
129 |
+
|
130 |
+
return (
|
131 |
+
self.cos_cached[:seq_len].to(dtype=x.dtype),
|
132 |
+
self.sin_cached[:seq_len].to(dtype=x.dtype),
|
133 |
+
)
|
134 |
+
|
135 |
+
|
136 |
+
# Copied from transformers.models.llama.modeling_llama.rotate_half
|
137 |
+
def rotate_half(x):
|
138 |
+
"""Rotates half the hidden dims of the input."""
|
139 |
+
x1 = x[..., : x.shape[-1] // 2]
|
140 |
+
x2 = x[..., x.shape[-1] // 2 :]
|
141 |
+
return torch.cat((-x2, x1), dim=-1)
|
142 |
+
|
143 |
+
|
144 |
+
# Copied from transformers.models.mistral.modeling_mistral.apply_rotary_pos_emb
|
145 |
+
def apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1):
|
146 |
+
"""Applies Rotary Position Embedding to the query and key tensors.
|
147 |
+
|
148 |
+
Args:
|
149 |
+
q (`torch.Tensor`): The query tensor.
|
150 |
+
k (`torch.Tensor`): The key tensor.
|
151 |
+
cos (`torch.Tensor`): The cosine part of the rotary embedding.
|
152 |
+
sin (`torch.Tensor`): The sine part of the rotary embedding.
|
153 |
+
position_ids (`torch.Tensor`):
|
154 |
+
The position indices of the tokens corresponding to the query and key tensors. For example, this can be
|
155 |
+
used to pass offsetted position ids when working with a KV-cache.
|
156 |
+
unsqueeze_dim (`int`, *optional*, defaults to 1):
|
157 |
+
The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
|
158 |
+
sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
|
159 |
+
that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
|
160 |
+
k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
|
161 |
+
cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
|
162 |
+
the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
|
163 |
+
Returns:
|
164 |
+
`tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
|
165 |
+
"""
|
166 |
+
cos = cos[position_ids].unsqueeze(unsqueeze_dim)
|
167 |
+
sin = sin[position_ids].unsqueeze(unsqueeze_dim)
|
168 |
+
q_embed = (q * cos) + (rotate_half(q) * sin)
|
169 |
+
k_embed = (k * cos) + (rotate_half(k) * sin)
|
170 |
+
return q_embed, k_embed
|
171 |
+
|
172 |
+
|
173 |
+
# Copied from transformers.models.mistral.modeling_mistral.MistralMLP with Mistral->Qwen2
|
174 |
+
class Qwen2MLP(nn.Module):
|
175 |
+
def __init__(self, config):
|
176 |
+
super().__init__()
|
177 |
+
self.config = config
|
178 |
+
self.hidden_size = config.hidden_size
|
179 |
+
self.intermediate_size = config.intermediate_size
|
180 |
+
self.gate_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
181 |
+
self.up_proj = nn.Linear(self.hidden_size, self.intermediate_size, bias=False)
|
182 |
+
self.down_proj = nn.Linear(self.intermediate_size, self.hidden_size, bias=False)
|
183 |
+
self.act_fn = ACT2FN[config.hidden_act]
|
184 |
+
|
185 |
+
def forward(self, x):
|
186 |
+
return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
|
187 |
+
|
188 |
+
|
189 |
+
# Copied from transformers.models.llama.modeling_llama.repeat_kv
|
190 |
+
def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor:
|
191 |
+
"""
|
192 |
+
This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
|
193 |
+
num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
|
194 |
+
"""
|
195 |
+
batch, num_key_value_heads, slen, head_dim = hidden_states.shape
|
196 |
+
if n_rep == 1:
|
197 |
+
return hidden_states
|
198 |
+
hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim)
|
199 |
+
return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim)
|
200 |
+
|
201 |
+
|
202 |
+
class Qwen2Attention(nn.Module):
|
203 |
+
"""
|
204 |
+
Multi-headed attention from 'Attention Is All You Need' paper. Modified to use sliding window attention: Longformer
|
205 |
+
and "Generating Long Sequences with Sparse Transformers".
|
206 |
+
"""
|
207 |
+
|
208 |
+
def __init__(self, config: Qwen2Config, layer_idx: Optional[int] = None):
|
209 |
+
super().__init__()
|
210 |
+
self.config = config
|
211 |
+
self.layer_idx = layer_idx
|
212 |
+
if layer_idx is None:
|
213 |
+
logger.warning_once(
|
214 |
+
f"Instantiating {self.__class__.__name__} without passing `layer_idx` is not recommended and will "
|
215 |
+
"to errors during the forward call, if caching is used. Please make sure to provide a `layer_idx` "
|
216 |
+
"when creating this class."
|
217 |
+
)
|
218 |
+
|
219 |
+
self.hidden_size = config.hidden_size
|
220 |
+
self.num_heads = config.num_attention_heads
|
221 |
+
self.head_dim = self.hidden_size // self.num_heads
|
222 |
+
self.num_key_value_heads = config.num_key_value_heads
|
223 |
+
self.num_key_value_groups = self.num_heads // self.num_key_value_heads
|
224 |
+
self.max_position_embeddings = config.max_position_embeddings
|
225 |
+
self.rope_theta = config.rope_theta
|
226 |
+
self.is_causal = True
|
227 |
+
self.attention_dropout = config.attention_dropout
|
228 |
+
|
229 |
+
if (self.head_dim * self.num_heads) != self.hidden_size:
|
230 |
+
raise ValueError(
|
231 |
+
f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
|
232 |
+
f" and `num_heads`: {self.num_heads})."
|
233 |
+
)
|
234 |
+
self.q_proj = nn.Linear(self.hidden_size, self.num_heads * self.head_dim, bias=True)
|
235 |
+
self.k_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=True)
|
236 |
+
self.v_proj = nn.Linear(self.hidden_size, self.num_key_value_heads * self.head_dim, bias=True)
|
237 |
+
self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
|
238 |
+
|
239 |
+
self.rotary_emb = Qwen2RotaryEmbedding(
|
240 |
+
self.head_dim,
|
241 |
+
max_position_embeddings=self.max_position_embeddings,
|
242 |
+
base=self.rope_theta,
|
243 |
+
)
|
244 |
+
|
245 |
+
def forward(
|
246 |
+
self,
|
247 |
+
hidden_states: torch.Tensor,
|
248 |
+
attention_mask: Optional[torch.Tensor] = None,
|
249 |
+
position_ids: Optional[torch.LongTensor] = None,
|
250 |
+
past_key_value: Optional[Cache] = None,
|
251 |
+
output_attentions: bool = False,
|
252 |
+
use_cache: bool = False,
|
253 |
+
**kwargs,
|
254 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
255 |
+
if "padding_mask" in kwargs:
|
256 |
+
warnings.warn(
|
257 |
+
"Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
|
258 |
+
)
|
259 |
+
bsz, q_len, _ = hidden_states.size()
|
260 |
+
|
261 |
+
query_states = self.q_proj(hidden_states)
|
262 |
+
key_states = self.k_proj(hidden_states)
|
263 |
+
value_states = self.v_proj(hidden_states)
|
264 |
+
|
265 |
+
query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
|
266 |
+
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
267 |
+
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
268 |
+
|
269 |
+
kv_seq_len = key_states.shape[-2]
|
270 |
+
if past_key_value is not None:
|
271 |
+
if self.layer_idx is None:
|
272 |
+
raise ValueError(
|
273 |
+
f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
|
274 |
+
"for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
|
275 |
+
"with a layer index."
|
276 |
+
)
|
277 |
+
kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
|
278 |
+
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
279 |
+
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
|
280 |
+
|
281 |
+
if past_key_value is not None:
|
282 |
+
cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
|
283 |
+
key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
|
284 |
+
|
285 |
+
# repeat k/v heads if n_kv_heads < n_heads
|
286 |
+
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
287 |
+
value_states = repeat_kv(value_states, self.num_key_value_groups)
|
288 |
+
|
289 |
+
attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)
|
290 |
+
|
291 |
+
if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
|
292 |
+
raise ValueError(
|
293 |
+
f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
|
294 |
+
f" {attn_weights.size()}"
|
295 |
+
)
|
296 |
+
|
297 |
+
if attention_mask is not None:
|
298 |
+
if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
|
299 |
+
raise ValueError(
|
300 |
+
f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
|
301 |
+
)
|
302 |
+
|
303 |
+
attn_weights = attn_weights + attention_mask
|
304 |
+
|
305 |
+
# upcast attention to fp32
|
306 |
+
attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
|
307 |
+
attn_weights = nn.functional.dropout(attn_weights, p=self.attention_dropout, training=self.training)
|
308 |
+
attn_output = torch.matmul(attn_weights, value_states)
|
309 |
+
|
310 |
+
if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
|
311 |
+
raise ValueError(
|
312 |
+
f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
|
313 |
+
f" {attn_output.size()}"
|
314 |
+
)
|
315 |
+
|
316 |
+
attn_output = attn_output.transpose(1, 2).contiguous()
|
317 |
+
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
|
318 |
+
|
319 |
+
attn_output = self.o_proj(attn_output)
|
320 |
+
|
321 |
+
if not output_attentions:
|
322 |
+
attn_weights = None
|
323 |
+
|
324 |
+
return attn_output, attn_weights, past_key_value
|
325 |
+
|
326 |
+
|
327 |
+
class Qwen2FlashAttention2(Qwen2Attention):
|
328 |
+
"""
|
329 |
+
Qwen2 flash attention module, following Qwen2 attention module. This module inherits from `Qwen2Attention`
|
330 |
+
as the weights of the module stays untouched. The only required change would be on the forward pass
|
331 |
+
where it needs to correctly call the public API of flash attention and deal with padding tokens
|
332 |
+
in case the input contains any of them. Additionally, for sliding window attention, we apply SWA only to the bottom
|
333 |
+
config.max_window_layers layers.
|
334 |
+
"""
|
335 |
+
|
336 |
+
# Copied from transformers.models.llama.modeling_llama.LlamaFlashAttention2.__init__
|
337 |
+
def __init__(self, *args, **kwargs):
|
338 |
+
super().__init__(*args, **kwargs)
|
339 |
+
|
340 |
+
# TODO: Should be removed once Flash Attention for RoCm is bumped to 2.1.
|
341 |
+
# flash_attn<2.1 generates top-left aligned causal mask, while what is needed here is bottom-right alignement, that was made default for flash_attn>=2.1. This attribute is used to handle this difference. Reference: https://github.com/Dao-AILab/flash-attention/releases/tag/v2.1.0.
|
342 |
+
# Beware that with flash_attn<2.1, using q_seqlen != k_seqlen (except for the case q_seqlen == 1) produces a wrong mask (top-left).
|
343 |
+
self._flash_attn_uses_top_left_mask = not is_flash_attn_greater_or_equal_2_10()
|
344 |
+
|
345 |
+
def forward(
|
346 |
+
self,
|
347 |
+
hidden_states: torch.Tensor,
|
348 |
+
attention_mask: Optional[torch.Tensor] = None,
|
349 |
+
position_ids: Optional[torch.LongTensor] = None,
|
350 |
+
past_key_value: Optional[Cache] = None,
|
351 |
+
output_attentions: bool = False,
|
352 |
+
use_cache: bool = False,
|
353 |
+
is_causal: bool = True,
|
354 |
+
**kwargs,
|
355 |
+
):
|
356 |
+
if "padding_mask" in kwargs:
|
357 |
+
warnings.warn(
|
358 |
+
"Passing `padding_mask` is deprecated and will be removed in v4.37. Please make sure use `attention_mask` instead.`"
|
359 |
+
)
|
360 |
+
|
361 |
+
# overwrite attention_mask with padding_mask
|
362 |
+
attention_mask = kwargs.pop("padding_mask")
|
363 |
+
bsz, q_len, _ = hidden_states.size()
|
364 |
+
|
365 |
+
query_states = self.q_proj(hidden_states)
|
366 |
+
key_states = self.k_proj(hidden_states)
|
367 |
+
value_states = self.v_proj(hidden_states)
|
368 |
+
|
369 |
+
query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
|
370 |
+
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
371 |
+
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
372 |
+
|
373 |
+
kv_seq_len = key_states.shape[-2]
|
374 |
+
if past_key_value is not None:
|
375 |
+
if self.layer_idx is None:
|
376 |
+
raise ValueError(
|
377 |
+
f"The cache structure has changed since version v4.36. If you are using {self.__class__.__name__} "
|
378 |
+
"for auto-regressive decoding with k/v caching, please make sure to initialize the attention class "
|
379 |
+
"with a layer index."
|
380 |
+
)
|
381 |
+
kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
|
382 |
+
|
383 |
+
# Because the input can be padded, the absolute sequence length depends on the max position id.
|
384 |
+
rotary_seq_len = max(kv_seq_len, position_ids[:, -1].max().item()) + 1
|
385 |
+
cos, sin = self.rotary_emb(value_states, seq_len=rotary_seq_len)
|
386 |
+
|
387 |
+
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
|
388 |
+
|
389 |
+
use_sliding_windows = (
|
390 |
+
_flash_supports_window_size
|
391 |
+
and getattr(self.config, "sliding_window", None) is not None
|
392 |
+
and kv_seq_len > self.config.sliding_window
|
393 |
+
and self.config.use_sliding_window
|
394 |
+
)
|
395 |
+
|
396 |
+
if not _flash_supports_window_size:
|
397 |
+
logger.warning_once(
|
398 |
+
"The current flash attention version does not support sliding window attention, for a more memory efficient implementation"
|
399 |
+
" make sure to upgrade flash-attn library."
|
400 |
+
)
|
401 |
+
|
402 |
+
if past_key_value is not None:
|
403 |
+
# Activate slicing cache only if the config has a value `sliding_windows` attribute
|
404 |
+
cache_has_contents = past_key_value.get_seq_length(self.layer_idx) > 0
|
405 |
+
if (
|
406 |
+
getattr(self.config, "sliding_window", None) is not None
|
407 |
+
and kv_seq_len > self.config.sliding_window
|
408 |
+
and cache_has_contents
|
409 |
+
):
|
410 |
+
slicing_tokens = 1 - self.config.sliding_window
|
411 |
+
|
412 |
+
past_key = past_key_value[self.layer_idx][0]
|
413 |
+
past_value = past_key_value[self.layer_idx][1]
|
414 |
+
|
415 |
+
past_key = past_key[:, :, slicing_tokens:, :].contiguous()
|
416 |
+
past_value = past_value[:, :, slicing_tokens:, :].contiguous()
|
417 |
+
|
418 |
+
if past_key.shape[-2] != self.config.sliding_window - 1:
|
419 |
+
raise ValueError(
|
420 |
+
f"past key must have a shape of (`batch_size, num_heads, self.config.sliding_window-1, head_dim`), got"
|
421 |
+
f" {past_key.shape}"
|
422 |
+
)
|
423 |
+
|
424 |
+
if attention_mask is not None:
|
425 |
+
attention_mask = attention_mask[:, slicing_tokens:]
|
426 |
+
attention_mask = torch.cat([attention_mask, torch.ones_like(attention_mask[:, -1:])], dim=-1)
|
427 |
+
|
428 |
+
cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
|
429 |
+
key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
|
430 |
+
|
431 |
+
# repeat k/v heads if n_kv_heads < n_heads
|
432 |
+
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
433 |
+
value_states = repeat_kv(value_states, self.num_key_value_groups)
|
434 |
+
dropout_rate = 0.0 if not self.training else self.attention_dropout
|
435 |
+
|
436 |
+
# In PEFT, usually we cast the layer norms in float32 for training stability reasons
|
437 |
+
# therefore the input hidden states gets silently casted in float32. Hence, we need
|
438 |
+
# cast them back in float16 just to be sure everything works as expected.
|
439 |
+
input_dtype = query_states.dtype
|
440 |
+
if input_dtype == torch.float32:
|
441 |
+
if torch.is_autocast_enabled():
|
442 |
+
target_dtype = torch.get_autocast_gpu_dtype()
|
443 |
+
# Handle the case where the model is quantized
|
444 |
+
elif hasattr(self.config, "_pre_quantization_dtype"):
|
445 |
+
target_dtype = self.config._pre_quantization_dtype
|
446 |
+
else:
|
447 |
+
target_dtype = self.q_proj.weight.dtype
|
448 |
+
|
449 |
+
logger.warning_once(
|
450 |
+
f"The input hidden states seems to be silently casted in float32, this might be related to"
|
451 |
+
f" the fact you have upcasted embedding or layer norm layers in float32. We will cast back the input in"
|
452 |
+
f" {target_dtype}."
|
453 |
+
)
|
454 |
+
|
455 |
+
query_states = query_states.to(target_dtype)
|
456 |
+
key_states = key_states.to(target_dtype)
|
457 |
+
value_states = value_states.to(target_dtype)
|
458 |
+
|
459 |
+
# Reashape to the expected shape for Flash Attention
|
460 |
+
query_states = query_states.transpose(1, 2)
|
461 |
+
key_states = key_states.transpose(1, 2)
|
462 |
+
value_states = value_states.transpose(1, 2)
|
463 |
+
|
464 |
+
attn_output = self._flash_attention_forward(
|
465 |
+
query_states,
|
466 |
+
key_states,
|
467 |
+
value_states,
|
468 |
+
attention_mask,
|
469 |
+
q_len,
|
470 |
+
dropout=dropout_rate,
|
471 |
+
use_sliding_windows=use_sliding_windows,
|
472 |
+
is_causal=is_causal
|
473 |
+
)
|
474 |
+
|
475 |
+
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size).contiguous()
|
476 |
+
attn_output = self.o_proj(attn_output)
|
477 |
+
|
478 |
+
if not output_attentions:
|
479 |
+
attn_weights = None
|
480 |
+
|
481 |
+
return attn_output, attn_weights, past_key_value
|
482 |
+
|
483 |
+
def _flash_attention_forward(
|
484 |
+
self,
|
485 |
+
query_states,
|
486 |
+
key_states,
|
487 |
+
value_states,
|
488 |
+
attention_mask,
|
489 |
+
query_length,
|
490 |
+
dropout=0.0,
|
491 |
+
softmax_scale=None,
|
492 |
+
use_sliding_windows=False,
|
493 |
+
is_causal=True,
|
494 |
+
):
|
495 |
+
"""
|
496 |
+
Calls the forward method of Flash Attention - if the input hidden states contain at least one padding token
|
497 |
+
first unpad the input, then computes the attention scores and pad the final attention scores.
|
498 |
+
|
499 |
+
Args:
|
500 |
+
query_states (`torch.Tensor`):
|
501 |
+
Input query states to be passed to Flash Attention API
|
502 |
+
key_states (`torch.Tensor`):
|
503 |
+
Input key states to be passed to Flash Attention API
|
504 |
+
value_states (`torch.Tensor`):
|
505 |
+
Input value states to be passed to Flash Attention API
|
506 |
+
attention_mask (`torch.Tensor`):
|
507 |
+
The padding mask - corresponds to a tensor of size `(batch_size, seq_len)` where 0 stands for the
|
508 |
+
position of padding tokens and 1 for the position of non-padding tokens.
|
509 |
+
dropout (`int`, *optional*):
|
510 |
+
Attention dropout
|
511 |
+
softmax_scale (`float`, *optional*):
|
512 |
+
The scaling of QK^T before applying softmax. Default to 1 / sqrt(head_dim)
|
513 |
+
use_sliding_windows (`bool`, *optional*):
|
514 |
+
Whether to activate sliding window attention.
|
515 |
+
"""
|
516 |
+
if not self._flash_attn_uses_top_left_mask:
|
517 |
+
causal = is_causal
|
518 |
+
else:
|
519 |
+
# TODO: Remove the `query_length != 1` check once Flash Attention for RoCm is bumped to 2.1. For details, please see the comment in LlamaFlashAttention2 __init__.
|
520 |
+
causal = is_causal and query_length != 1
|
521 |
+
|
522 |
+
# Decide whether to use SWA or not by layer index.
|
523 |
+
if use_sliding_windows and self.layer_idx >= self.config.max_window_layers:
|
524 |
+
use_sliding_windows = False
|
525 |
+
|
526 |
+
# Contains at least one padding token in the sequence
|
527 |
+
if attention_mask is not None:
|
528 |
+
batch_size = query_states.shape[0]
|
529 |
+
query_states, key_states, value_states, indices_q, cu_seq_lens, max_seq_lens = self._upad_input(
|
530 |
+
query_states, key_states, value_states, attention_mask, query_length
|
531 |
+
)
|
532 |
+
|
533 |
+
cu_seqlens_q, cu_seqlens_k = cu_seq_lens
|
534 |
+
max_seqlen_in_batch_q, max_seqlen_in_batch_k = max_seq_lens
|
535 |
+
|
536 |
+
if not use_sliding_windows:
|
537 |
+
attn_output_unpad = flash_attn_varlen_func(
|
538 |
+
query_states,
|
539 |
+
key_states,
|
540 |
+
value_states,
|
541 |
+
cu_seqlens_q=cu_seqlens_q,
|
542 |
+
cu_seqlens_k=cu_seqlens_k,
|
543 |
+
max_seqlen_q=max_seqlen_in_batch_q,
|
544 |
+
max_seqlen_k=max_seqlen_in_batch_k,
|
545 |
+
dropout_p=dropout,
|
546 |
+
softmax_scale=softmax_scale,
|
547 |
+
causal=causal,
|
548 |
+
)
|
549 |
+
else:
|
550 |
+
attn_output_unpad = flash_attn_varlen_func(
|
551 |
+
query_states,
|
552 |
+
key_states,
|
553 |
+
value_states,
|
554 |
+
cu_seqlens_q=cu_seqlens_q,
|
555 |
+
cu_seqlens_k=cu_seqlens_k,
|
556 |
+
max_seqlen_q=max_seqlen_in_batch_q,
|
557 |
+
max_seqlen_k=max_seqlen_in_batch_k,
|
558 |
+
dropout_p=dropout,
|
559 |
+
softmax_scale=softmax_scale,
|
560 |
+
causal=causal,
|
561 |
+
window_size=(self.config.sliding_window, self.config.sliding_window),
|
562 |
+
)
|
563 |
+
|
564 |
+
attn_output = pad_input(attn_output_unpad, indices_q, batch_size, query_length)
|
565 |
+
else:
|
566 |
+
if not use_sliding_windows:
|
567 |
+
attn_output = flash_attn_func(
|
568 |
+
query_states,
|
569 |
+
key_states,
|
570 |
+
value_states,
|
571 |
+
dropout,
|
572 |
+
softmax_scale=softmax_scale,
|
573 |
+
causal=causal,
|
574 |
+
)
|
575 |
+
else:
|
576 |
+
attn_output = flash_attn_func(
|
577 |
+
query_states,
|
578 |
+
key_states,
|
579 |
+
value_states,
|
580 |
+
dropout,
|
581 |
+
softmax_scale=softmax_scale,
|
582 |
+
causal=causal,
|
583 |
+
window_size=(self.config.sliding_window, self.config.sliding_window),
|
584 |
+
)
|
585 |
+
|
586 |
+
return attn_output
|
587 |
+
|
588 |
+
# Copied from transformers.models.mistral.modeling_mistral.MistralFlashAttention2._upad_input
|
589 |
+
def _upad_input(self, query_layer, key_layer, value_layer, attention_mask, query_length):
|
590 |
+
batch_size, kv_seq_len, num_heads, head_dim = key_layer.shape
|
591 |
+
|
592 |
+
# On the first iteration we need to properly re-create the padding mask
|
593 |
+
# by slicing it on the proper place
|
594 |
+
if kv_seq_len != attention_mask.shape[-1]:
|
595 |
+
attention_mask_num_tokens = attention_mask.shape[-1]
|
596 |
+
attention_mask = attention_mask[:, attention_mask_num_tokens - kv_seq_len :]
|
597 |
+
|
598 |
+
indices_k, cu_seqlens_k, max_seqlen_in_batch_k = _get_unpad_data(attention_mask)
|
599 |
+
|
600 |
+
key_layer = index_first_axis(key_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k)
|
601 |
+
value_layer = index_first_axis(value_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k)
|
602 |
+
|
603 |
+
if query_length == kv_seq_len:
|
604 |
+
query_layer = index_first_axis(
|
605 |
+
query_layer.reshape(batch_size * kv_seq_len, num_heads, head_dim), indices_k
|
606 |
+
)
|
607 |
+
cu_seqlens_q = cu_seqlens_k
|
608 |
+
max_seqlen_in_batch_q = max_seqlen_in_batch_k
|
609 |
+
indices_q = indices_k
|
610 |
+
elif query_length == 1:
|
611 |
+
max_seqlen_in_batch_q = 1
|
612 |
+
cu_seqlens_q = torch.arange(
|
613 |
+
batch_size + 1, dtype=torch.int32, device=query_layer.device
|
614 |
+
) # There is a memcpy here, that is very bad.
|
615 |
+
indices_q = cu_seqlens_q[:-1]
|
616 |
+
query_layer = query_layer.squeeze(1)
|
617 |
+
else:
|
618 |
+
# The -q_len: slice assumes left padding.
|
619 |
+
attention_mask = attention_mask[:, -query_length:]
|
620 |
+
query_layer, indices_q, cu_seqlens_q, max_seqlen_in_batch_q = unpad_input(query_layer, attention_mask)
|
621 |
+
|
622 |
+
return (
|
623 |
+
query_layer,
|
624 |
+
key_layer,
|
625 |
+
value_layer,
|
626 |
+
indices_q,
|
627 |
+
(cu_seqlens_q, cu_seqlens_k),
|
628 |
+
(max_seqlen_in_batch_q, max_seqlen_in_batch_k),
|
629 |
+
)
|
630 |
+
|
631 |
+
|
632 |
+
# Copied from transformers.models.mistral.modeling_mistral.MistralSdpaAttention with Mistral->Qwen2
|
633 |
+
class Qwen2SdpaAttention(Qwen2Attention):
|
634 |
+
"""
|
635 |
+
Qwen2 attention module using torch.nn.functional.scaled_dot_product_attention. This module inherits from
|
636 |
+
`Qwen2Attention` as the weights of the module stays untouched. The only changes are on the forward pass to adapt to
|
637 |
+
SDPA API.
|
638 |
+
"""
|
639 |
+
|
640 |
+
# Adapted from Qwen2Attention.forward
|
641 |
+
def forward(
|
642 |
+
self,
|
643 |
+
hidden_states: torch.Tensor,
|
644 |
+
attention_mask: Optional[torch.Tensor] = None,
|
645 |
+
position_ids: Optional[torch.LongTensor] = None,
|
646 |
+
past_key_value: Optional[Cache] = None,
|
647 |
+
output_attentions: bool = False,
|
648 |
+
use_cache: bool = False,
|
649 |
+
is_causal: bool = False,
|
650 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
651 |
+
if output_attentions:
|
652 |
+
# TODO: Improve this warning with e.g. `model.config.attn_implementation = "manual"` once this is implemented.
|
653 |
+
logger.warning_once(
|
654 |
+
"Qwen2Model is using Qwen2SdpaAttention, but `torch.nn.functional.scaled_dot_product_attention` does not support `output_attentions=True`. Falling back to the manual attention implementation, "
|
655 |
+
'but specifying the manual implementation will be required from Transformers version v5.0.0 onwards. This warning can be removed using the argument `attn_implementation="eager"` when loading the model.'
|
656 |
+
)
|
657 |
+
return super().forward(
|
658 |
+
hidden_states=hidden_states,
|
659 |
+
attention_mask=attention_mask,
|
660 |
+
position_ids=position_ids,
|
661 |
+
past_key_value=past_key_value,
|
662 |
+
output_attentions=output_attentions,
|
663 |
+
use_cache=use_cache,
|
664 |
+
is_causal=is_causal
|
665 |
+
)
|
666 |
+
|
667 |
+
bsz, q_len, _ = hidden_states.size()
|
668 |
+
|
669 |
+
query_states = self.q_proj(hidden_states)
|
670 |
+
key_states = self.k_proj(hidden_states)
|
671 |
+
value_states = self.v_proj(hidden_states)
|
672 |
+
|
673 |
+
query_states = query_states.view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
|
674 |
+
key_states = key_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
675 |
+
value_states = value_states.view(bsz, q_len, self.num_key_value_heads, self.head_dim).transpose(1, 2)
|
676 |
+
|
677 |
+
kv_seq_len = key_states.shape[-2]
|
678 |
+
if past_key_value is not None:
|
679 |
+
kv_seq_len += past_key_value.get_usable_length(kv_seq_len, self.layer_idx)
|
680 |
+
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
681 |
+
|
682 |
+
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
|
683 |
+
|
684 |
+
if past_key_value is not None:
|
685 |
+
cache_kwargs = {"sin": sin, "cos": cos} # Specific to RoPE models
|
686 |
+
key_states, value_states = past_key_value.update(key_states, value_states, self.layer_idx, cache_kwargs)
|
687 |
+
|
688 |
+
key_states = repeat_kv(key_states, self.num_key_value_groups)
|
689 |
+
value_states = repeat_kv(value_states, self.num_key_value_groups)
|
690 |
+
|
691 |
+
if attention_mask is not None:
|
692 |
+
if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
|
693 |
+
raise ValueError(
|
694 |
+
f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
|
695 |
+
)
|
696 |
+
|
697 |
+
# SDPA with memory-efficient backend is currently (torch==2.1.2) bugged with non-contiguous inputs with custom attn_mask,
|
698 |
+
# Reference: https://github.com/pytorch/pytorch/issues/112577.
|
699 |
+
if query_states.device.type == "cuda" and attention_mask is not None:
|
700 |
+
query_states = query_states.contiguous()
|
701 |
+
key_states = key_states.contiguous()
|
702 |
+
value_states = value_states.contiguous()
|
703 |
+
|
704 |
+
attn_output = torch.nn.functional.scaled_dot_product_attention(
|
705 |
+
query_states,
|
706 |
+
key_states,
|
707 |
+
value_states,
|
708 |
+
attn_mask=attention_mask,
|
709 |
+
dropout_p=self.attention_dropout if self.training else 0.0,
|
710 |
+
# The q_len > 1 is necessary to match with AttentionMaskConverter.to_causal_4d that does not create a causal mask in case q_len == 1.
|
711 |
+
is_causal=is_causal and attention_mask is None and q_len > 1,
|
712 |
+
)
|
713 |
+
|
714 |
+
attn_output = attn_output.transpose(1, 2).contiguous()
|
715 |
+
attn_output = attn_output.view(bsz, q_len, self.hidden_size)
|
716 |
+
|
717 |
+
attn_output = self.o_proj(attn_output)
|
718 |
+
|
719 |
+
return attn_output, None, past_key_value
|
720 |
+
|
721 |
+
|
722 |
+
QWEN2_ATTENTION_CLASSES = {
|
723 |
+
"eager": Qwen2Attention,
|
724 |
+
"flash_attention_2": Qwen2FlashAttention2,
|
725 |
+
"sdpa": Qwen2SdpaAttention,
|
726 |
+
}
|
727 |
+
|
728 |
+
|
729 |
+
class Qwen2DecoderLayer(nn.Module):
|
730 |
+
def __init__(self, config: Qwen2Config, layer_idx: int):
|
731 |
+
super().__init__()
|
732 |
+
self.hidden_size = config.hidden_size
|
733 |
+
|
734 |
+
if config.use_sliding_window and config._attn_implementation != "flash_attention_2":
|
735 |
+
logger.warning_once(
|
736 |
+
f"Sliding Window Attention is enabled but not implemented for `{config._attn_implementation}`; "
|
737 |
+
"unexpected results may be encountered."
|
738 |
+
)
|
739 |
+
self.self_attn = QWEN2_ATTENTION_CLASSES[config._attn_implementation](config, layer_idx)
|
740 |
+
|
741 |
+
self.mlp = Qwen2MLP(config)
|
742 |
+
self.input_layernorm = Qwen2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
743 |
+
self.post_attention_layernorm = Qwen2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
744 |
+
|
745 |
+
def forward(
|
746 |
+
self,
|
747 |
+
hidden_states: torch.Tensor,
|
748 |
+
attention_mask: Optional[torch.Tensor] = None,
|
749 |
+
position_ids: Optional[torch.LongTensor] = None,
|
750 |
+
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
751 |
+
output_attentions: Optional[bool] = False,
|
752 |
+
use_cache: Optional[bool] = False,
|
753 |
+
is_causal: Optional[bool] = True,
|
754 |
+
**kwargs,
|
755 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
756 |
+
if "padding_mask" in kwargs:
|
757 |
+
warnings.warn(
|
758 |
+
"Passing `padding_mask` is deprecated and will be removed in v4.37. "
|
759 |
+
"Please make sure use `attention_mask` instead.`"
|
760 |
+
)
|
761 |
+
"""
|
762 |
+
Args:
|
763 |
+
hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
|
764 |
+
attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
|
765 |
+
`(batch, sequence_length)` where padding elements are indicated by 0.
|
766 |
+
output_attentions (`bool`, *optional*):
|
767 |
+
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
|
768 |
+
returned tensors for more detail.
|
769 |
+
use_cache (`bool`, *optional*):
|
770 |
+
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
|
771 |
+
(see `past_key_values`).
|
772 |
+
past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
|
773 |
+
"""
|
774 |
+
|
775 |
+
residual = hidden_states
|
776 |
+
|
777 |
+
hidden_states = self.input_layernorm(hidden_states)
|
778 |
+
|
779 |
+
# Self Attention
|
780 |
+
hidden_states, self_attn_weights, present_key_value = self.self_attn(
|
781 |
+
hidden_states=hidden_states,
|
782 |
+
attention_mask=attention_mask,
|
783 |
+
position_ids=position_ids,
|
784 |
+
past_key_value=past_key_value,
|
785 |
+
output_attentions=output_attentions,
|
786 |
+
use_cache=use_cache,
|
787 |
+
is_causal=is_causal,
|
788 |
+
)
|
789 |
+
hidden_states = residual + hidden_states
|
790 |
+
|
791 |
+
# Fully Connected
|
792 |
+
residual = hidden_states
|
793 |
+
hidden_states = self.post_attention_layernorm(hidden_states)
|
794 |
+
hidden_states = self.mlp(hidden_states)
|
795 |
+
hidden_states = residual + hidden_states
|
796 |
+
|
797 |
+
outputs = (hidden_states,)
|
798 |
+
|
799 |
+
if output_attentions:
|
800 |
+
outputs += (self_attn_weights,)
|
801 |
+
|
802 |
+
if use_cache:
|
803 |
+
outputs += (present_key_value,)
|
804 |
+
|
805 |
+
return outputs
|
806 |
+
|
807 |
+
|
808 |
+
QWEN2_START_DOCSTRING = r"""
|
809 |
+
This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
|
810 |
+
library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
|
811 |
+
etc.)
|
812 |
+
|
813 |
+
This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
|
814 |
+
Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
|
815 |
+
and behavior.
|
816 |
+
|
817 |
+
Parameters:
|
818 |
+
config ([`Qwen2Config`]):
|
819 |
+
Model configuration class with all the parameters of the model. Initializing with a config file does not
|
820 |
+
load the weights associated with the model, only the configuration. Check out the
|
821 |
+
[`~PreTrainedModel.from_pretrained`] method to load the model weights.
|
822 |
+
"""
|
823 |
+
|
824 |
+
|
825 |
+
@add_start_docstrings(
|
826 |
+
"The bare Qwen2 Model outputting raw hidden-states without any specific head on top.",
|
827 |
+
QWEN2_START_DOCSTRING,
|
828 |
+
)
|
829 |
+
class Qwen2PreTrainedModel(PreTrainedModel):
|
830 |
+
config_class = Qwen2Config
|
831 |
+
base_model_prefix = "model"
|
832 |
+
supports_gradient_checkpointing = True
|
833 |
+
_no_split_modules = ["Qwen2DecoderLayer"]
|
834 |
+
_skip_keys_device_placement = "past_key_values"
|
835 |
+
_supports_flash_attn_2 = True
|
836 |
+
_supports_sdpa = True
|
837 |
+
_supports_cache_class = True
|
838 |
+
|
839 |
+
def _init_weights(self, module):
|
840 |
+
std = self.config.initializer_range
|
841 |
+
if isinstance(module, nn.Linear):
|
842 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
843 |
+
if module.bias is not None:
|
844 |
+
module.bias.data.zero_()
|
845 |
+
elif isinstance(module, nn.Embedding):
|
846 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
847 |
+
if module.padding_idx is not None:
|
848 |
+
module.weight.data[module.padding_idx].zero_()
|
849 |
+
|
850 |
+
|
851 |
+
QWEN2_INPUTS_DOCSTRING = r"""
|
852 |
+
Args:
|
853 |
+
input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
|
854 |
+
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide
|
855 |
+
it.
|
856 |
+
|
857 |
+
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
858 |
+
[`PreTrainedTokenizer.__call__`] for details.
|
859 |
+
|
860 |
+
[What are input IDs?](../glossary#input-ids)
|
861 |
+
attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
|
862 |
+
Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
|
863 |
+
|
864 |
+
- 1 for tokens that are **not masked**,
|
865 |
+
- 0 for tokens that are **masked**.
|
866 |
+
|
867 |
+
[What are attention masks?](../glossary#attention-mask)
|
868 |
+
|
869 |
+
Indices can be obtained using [`AutoTokenizer`]. See [`PreTrainedTokenizer.encode`] and
|
870 |
+
[`PreTrainedTokenizer.__call__`] for details.
|
871 |
+
|
872 |
+
If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see
|
873 |
+
`past_key_values`).
|
874 |
+
|
875 |
+
If you want to change padding behavior, you should read [`modeling_opt._prepare_decoder_attention_mask`]
|
876 |
+
and modify to your needs. See diagram 1 in [the paper](https://arxiv.org/abs/1910.13461) for more
|
877 |
+
information on the default strategy.
|
878 |
+
|
879 |
+
- 1 indicates the head is **not masked**,
|
880 |
+
- 0 indicates the head is **masked**.
|
881 |
+
position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
882 |
+
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0,
|
883 |
+
config.n_positions - 1]`.
|
884 |
+
|
885 |
+
[What are position IDs?](../glossary#position-ids)
|
886 |
+
past_key_values (`Cache` or `tuple(tuple(torch.FloatTensor))`, *optional*):
|
887 |
+
Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention
|
888 |
+
blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values`
|
889 |
+
returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`.
|
890 |
+
|
891 |
+
Two formats are allowed:
|
892 |
+
- a [`~cache_utils.Cache`] instance;
|
893 |
+
- Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of
|
894 |
+
shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`). This is also known as the legacy
|
895 |
+
cache format.
|
896 |
+
|
897 |
+
The model will output the same cache format that is fed as input. If no `past_key_values` are passed, the
|
898 |
+
legacy cache format will be returned.
|
899 |
+
|
900 |
+
If `past_key_values` are used, the user can optionally input only the last `input_ids` (those that don't
|
901 |
+
have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `input_ids`
|
902 |
+
of shape `(batch_size, sequence_length)`.
|
903 |
+
inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, *optional*):
|
904 |
+
Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This
|
905 |
+
is useful if you want more control over how to convert `input_ids` indices into associated vectors than the
|
906 |
+
model's internal embedding lookup matrix.
|
907 |
+
use_cache (`bool`, *optional*):
|
908 |
+
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see
|
909 |
+
`past_key_values`).
|
910 |
+
output_attentions (`bool`, *optional*):
|
911 |
+
Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
|
912 |
+
tensors for more detail.
|
913 |
+
output_hidden_states (`bool`, *optional*):
|
914 |
+
Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
|
915 |
+
more detail.
|
916 |
+
return_dict (`bool`, *optional*):
|
917 |
+
Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
|
918 |
+
"""
|
919 |
+
|
920 |
+
|
921 |
+
@add_start_docstrings(
|
922 |
+
"The bare Qwen2 Model outputting raw hidden-states without any specific head on top.",
|
923 |
+
QWEN2_START_DOCSTRING,
|
924 |
+
)
|
925 |
+
class Qwen2Model(Qwen2PreTrainedModel):
|
926 |
+
"""
|
927 |
+
Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`Qwen2DecoderLayer`]
|
928 |
+
|
929 |
+
Args:
|
930 |
+
config: Qwen2Config
|
931 |
+
"""
|
932 |
+
|
933 |
+
def __init__(self, config: Qwen2Config):
|
934 |
+
super().__init__(config)
|
935 |
+
self.padding_idx = config.pad_token_id
|
936 |
+
self.vocab_size = config.vocab_size
|
937 |
+
|
938 |
+
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
939 |
+
self.layers = nn.ModuleList(
|
940 |
+
[Qwen2DecoderLayer(config, layer_idx) for layer_idx in range(config.num_hidden_layers)]
|
941 |
+
)
|
942 |
+
self._attn_implementation = config._attn_implementation
|
943 |
+
self.norm = Qwen2RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
944 |
+
|
945 |
+
self.gradient_checkpointing = False
|
946 |
+
# Initialize weights and apply final processing
|
947 |
+
self.post_init()
|
948 |
+
|
949 |
+
def get_input_embeddings(self):
|
950 |
+
return self.embed_tokens
|
951 |
+
|
952 |
+
def set_input_embeddings(self, value):
|
953 |
+
self.embed_tokens = value
|
954 |
+
|
955 |
+
@add_start_docstrings_to_model_forward(QWEN2_INPUTS_DOCSTRING)
|
956 |
+
def forward(
|
957 |
+
self,
|
958 |
+
input_ids: torch.LongTensor = None,
|
959 |
+
attention_mask: Optional[torch.Tensor] = None,
|
960 |
+
position_ids: Optional[torch.LongTensor] = None,
|
961 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
962 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
963 |
+
use_cache: Optional[bool] = None,
|
964 |
+
output_attentions: Optional[bool] = None,
|
965 |
+
output_hidden_states: Optional[bool] = None,
|
966 |
+
return_dict: Optional[bool] = None,
|
967 |
+
labels: Optional[torch.LongTensor] = None,
|
968 |
+
is_causal: Optional[bool] = True,
|
969 |
+
) -> Union[Tuple, BaseModelOutputWithPast]:
|
970 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
971 |
+
output_hidden_states = (
|
972 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
973 |
+
)
|
974 |
+
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
975 |
+
|
976 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
977 |
+
|
978 |
+
# retrieve input_ids and inputs_embeds
|
979 |
+
if input_ids is not None and inputs_embeds is not None:
|
980 |
+
raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time")
|
981 |
+
elif input_ids is not None:
|
982 |
+
batch_size, seq_length = input_ids.shape
|
983 |
+
elif inputs_embeds is not None:
|
984 |
+
batch_size, seq_length, _ = inputs_embeds.shape
|
985 |
+
else:
|
986 |
+
raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
|
987 |
+
|
988 |
+
if self.gradient_checkpointing and self.training:
|
989 |
+
if use_cache:
|
990 |
+
logger.warning_once(
|
991 |
+
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
992 |
+
)
|
993 |
+
use_cache = False
|
994 |
+
|
995 |
+
past_key_values_length = 0
|
996 |
+
|
997 |
+
if use_cache:
|
998 |
+
use_legacy_cache = not isinstance(past_key_values, Cache)
|
999 |
+
if use_legacy_cache:
|
1000 |
+
past_key_values = DynamicCache.from_legacy_cache(past_key_values)
|
1001 |
+
past_key_values_length = past_key_values.get_usable_length(seq_length)
|
1002 |
+
|
1003 |
+
if position_ids is None:
|
1004 |
+
device = input_ids.device if input_ids is not None else inputs_embeds.device
|
1005 |
+
position_ids = torch.arange(
|
1006 |
+
past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
|
1007 |
+
)
|
1008 |
+
position_ids = position_ids.unsqueeze(0).view(-1, seq_length)
|
1009 |
+
else:
|
1010 |
+
position_ids = position_ids.view(-1, seq_length).long()
|
1011 |
+
|
1012 |
+
if inputs_embeds is None:
|
1013 |
+
inputs_embeds = self.embed_tokens(input_ids)
|
1014 |
+
|
1015 |
+
if attention_mask is not None and self._attn_implementation == "flash_attention_2" and use_cache:
|
1016 |
+
is_padding_right = attention_mask[:, -1].sum().item() != batch_size
|
1017 |
+
if is_padding_right:
|
1018 |
+
raise ValueError(
|
1019 |
+
"You are attempting to perform batched generation with padding_side='right'"
|
1020 |
+
" this may lead to unexpected behaviour for Flash Attention version of Qwen2. Make sure to "
|
1021 |
+
" call `tokenizer.padding_side = 'left'` before tokenizing the input. "
|
1022 |
+
)
|
1023 |
+
|
1024 |
+
if self._attn_implementation == "flash_attention_2":
|
1025 |
+
# 2d mask is passed through the layers
|
1026 |
+
attention_mask = attention_mask if (attention_mask is not None and 0 in attention_mask) else None
|
1027 |
+
elif self._attn_implementation == "sdpa" and not output_attentions:
|
1028 |
+
# output_attentions=True can not be supported when using SDPA, and we fall back on
|
1029 |
+
# the manual implementation that requires a 4D causal mask in all cases.
|
1030 |
+
if is_causal:
|
1031 |
+
attention_mask = _prepare_4d_causal_attention_mask_for_sdpa(
|
1032 |
+
attention_mask,
|
1033 |
+
(batch_size, seq_length),
|
1034 |
+
inputs_embeds,
|
1035 |
+
past_key_values_length,
|
1036 |
+
)
|
1037 |
+
else:
|
1038 |
+
attention_mask = _prepare_4d_attention_mask_for_sdpa(
|
1039 |
+
attention_mask, inputs_embeds.dtype
|
1040 |
+
)
|
1041 |
+
else:
|
1042 |
+
# 4d mask is passed through the layers
|
1043 |
+
if is_causal:
|
1044 |
+
# Causal mask with -3.3895e+38 where no attention should be
|
1045 |
+
attention_mask = _prepare_4d_causal_attention_mask(
|
1046 |
+
attention_mask,
|
1047 |
+
(batch_size, seq_length),
|
1048 |
+
inputs_embeds,
|
1049 |
+
past_key_values_length,
|
1050 |
+
sliding_window=self.config.sliding_window,
|
1051 |
+
)
|
1052 |
+
else:
|
1053 |
+
# Shape: batch_size, 1, query_length, key_value_length
|
1054 |
+
attention_mask = _prepare_4d_attention_mask(
|
1055 |
+
attention_mask, inputs_embeds.dtype
|
1056 |
+
)
|
1057 |
+
|
1058 |
+
hidden_states = inputs_embeds
|
1059 |
+
|
1060 |
+
# decoder layers
|
1061 |
+
all_hidden_states = () if output_hidden_states else None
|
1062 |
+
all_self_attns = () if output_attentions else None
|
1063 |
+
next_decoder_cache = None
|
1064 |
+
|
1065 |
+
for decoder_layer in self.layers:
|
1066 |
+
if output_hidden_states:
|
1067 |
+
all_hidden_states += (hidden_states,)
|
1068 |
+
|
1069 |
+
if self.gradient_checkpointing and self.training:
|
1070 |
+
layer_outputs = self._gradient_checkpointing_func(
|
1071 |
+
decoder_layer.__call__,
|
1072 |
+
hidden_states,
|
1073 |
+
attention_mask,
|
1074 |
+
position_ids,
|
1075 |
+
past_key_values,
|
1076 |
+
output_attentions,
|
1077 |
+
use_cache,
|
1078 |
+
is_causal,
|
1079 |
+
)
|
1080 |
+
else:
|
1081 |
+
layer_outputs = decoder_layer(
|
1082 |
+
hidden_states,
|
1083 |
+
attention_mask=attention_mask,
|
1084 |
+
position_ids=position_ids,
|
1085 |
+
past_key_value=past_key_values,
|
1086 |
+
output_attentions=output_attentions,
|
1087 |
+
use_cache=use_cache,
|
1088 |
+
is_causal=is_causal,
|
1089 |
+
)
|
1090 |
+
|
1091 |
+
hidden_states = layer_outputs[0]
|
1092 |
+
|
1093 |
+
if use_cache:
|
1094 |
+
next_decoder_cache = layer_outputs[2 if output_attentions else 1]
|
1095 |
+
|
1096 |
+
if output_attentions:
|
1097 |
+
all_self_attns += (layer_outputs[1],)
|
1098 |
+
|
1099 |
+
hidden_states = self.norm(hidden_states)
|
1100 |
+
|
1101 |
+
# add hidden states from the last decoder layer
|
1102 |
+
if output_hidden_states:
|
1103 |
+
all_hidden_states += (hidden_states,)
|
1104 |
+
|
1105 |
+
next_cache = None
|
1106 |
+
if use_cache:
|
1107 |
+
next_cache = next_decoder_cache.to_legacy_cache() if use_legacy_cache else next_decoder_cache
|
1108 |
+
|
1109 |
+
if not return_dict:
|
1110 |
+
return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
|
1111 |
+
return BaseModelOutputWithPast(
|
1112 |
+
last_hidden_state=hidden_states,
|
1113 |
+
past_key_values=next_cache,
|
1114 |
+
hidden_states=all_hidden_states,
|
1115 |
+
attentions=all_self_attns,
|
1116 |
+
)
|
1117 |
+
|
1118 |
+
|
1119 |
+
class Qwen2ForCausalLM(Qwen2PreTrainedModel):
|
1120 |
+
_tied_weights_keys = ["lm_head.weight"]
|
1121 |
+
|
1122 |
+
def __init__(self, config):
|
1123 |
+
super().__init__(config)
|
1124 |
+
self.model = Qwen2Model(config)
|
1125 |
+
self.vocab_size = config.vocab_size
|
1126 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
1127 |
+
|
1128 |
+
# Initialize weights and apply final processing
|
1129 |
+
self.post_init()
|
1130 |
+
|
1131 |
+
def get_input_embeddings(self):
|
1132 |
+
return self.model.embed_tokens
|
1133 |
+
|
1134 |
+
def set_input_embeddings(self, value):
|
1135 |
+
self.model.embed_tokens = value
|
1136 |
+
|
1137 |
+
def get_output_embeddings(self):
|
1138 |
+
return self.lm_head
|
1139 |
+
|
1140 |
+
def set_output_embeddings(self, new_embeddings):
|
1141 |
+
self.lm_head = new_embeddings
|
1142 |
+
|
1143 |
+
def set_decoder(self, decoder):
|
1144 |
+
self.model = decoder
|
1145 |
+
|
1146 |
+
def get_decoder(self):
|
1147 |
+
return self.model
|
1148 |
+
|
1149 |
+
@add_start_docstrings_to_model_forward(QWEN2_INPUTS_DOCSTRING)
|
1150 |
+
@replace_return_docstrings(output_type=CausalLMOutputWithPast, config_class=_CONFIG_FOR_DOC)
|
1151 |
+
def forward(
|
1152 |
+
self,
|
1153 |
+
input_ids: torch.LongTensor = None,
|
1154 |
+
attention_mask: Optional[torch.Tensor] = None,
|
1155 |
+
position_ids: Optional[torch.LongTensor] = None,
|
1156 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
1157 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
1158 |
+
labels: Optional[torch.LongTensor] = None,
|
1159 |
+
use_cache: Optional[bool] = None,
|
1160 |
+
output_attentions: Optional[bool] = None,
|
1161 |
+
output_hidden_states: Optional[bool] = None,
|
1162 |
+
return_dict: Optional[bool] = None,
|
1163 |
+
is_causal: Optional[bool] = True,
|
1164 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
1165 |
+
r"""
|
1166 |
+
Args:
|
1167 |
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
1168 |
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
1169 |
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
1170 |
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
1171 |
+
|
1172 |
+
Returns:
|
1173 |
+
|
1174 |
+
Example:
|
1175 |
+
|
1176 |
+
```python
|
1177 |
+
>>> from transformers import AutoTokenizer, Qwen2ForCausalLM
|
1178 |
+
|
1179 |
+
>>> model = Qwen2ForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
|
1180 |
+
>>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
|
1181 |
+
|
1182 |
+
>>> prompt = "Hey, are you conscious? Can you talk to me?"
|
1183 |
+
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
1184 |
+
|
1185 |
+
>>> # Generate
|
1186 |
+
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
1187 |
+
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
1188 |
+
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
|
1189 |
+
```"""
|
1190 |
+
|
1191 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
1192 |
+
output_hidden_states = (
|
1193 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
1194 |
+
)
|
1195 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
1196 |
+
|
1197 |
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
1198 |
+
outputs = self.model(
|
1199 |
+
input_ids=input_ids,
|
1200 |
+
attention_mask=attention_mask,
|
1201 |
+
position_ids=position_ids,
|
1202 |
+
past_key_values=past_key_values,
|
1203 |
+
inputs_embeds=inputs_embeds,
|
1204 |
+
use_cache=use_cache,
|
1205 |
+
output_attentions=output_attentions,
|
1206 |
+
output_hidden_states=output_hidden_states,
|
1207 |
+
return_dict=return_dict,
|
1208 |
+
is_causal=is_causal,
|
1209 |
+
)
|
1210 |
+
|
1211 |
+
hidden_states = outputs[0]
|
1212 |
+
logits = self.lm_head(hidden_states)
|
1213 |
+
logits = logits.float()
|
1214 |
+
|
1215 |
+
loss = None
|
1216 |
+
if labels is not None:
|
1217 |
+
# Shift so that tokens < n predict n
|
1218 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
1219 |
+
shift_labels = labels[..., 1:].contiguous()
|
1220 |
+
# Flatten the tokens
|
1221 |
+
loss_fct = CrossEntropyLoss()
|
1222 |
+
shift_logits = shift_logits.view(-1, self.config.vocab_size)
|
1223 |
+
shift_labels = shift_labels.view(-1)
|
1224 |
+
# Enable model parallelism
|
1225 |
+
shift_labels = shift_labels.to(shift_logits.device)
|
1226 |
+
loss = loss_fct(shift_logits, shift_labels)
|
1227 |
+
|
1228 |
+
if not return_dict:
|
1229 |
+
output = (logits,) + outputs[1:]
|
1230 |
+
return (loss,) + output if loss is not None else output
|
1231 |
+
|
1232 |
+
return CausalLMOutputWithPast(
|
1233 |
+
loss=loss,
|
1234 |
+
logits=logits,
|
1235 |
+
past_key_values=outputs.past_key_values,
|
1236 |
+
hidden_states=outputs.hidden_states,
|
1237 |
+
attentions=outputs.attentions,
|
1238 |
+
)
|
1239 |
+
|
1240 |
+
def prepare_inputs_for_generation(
|
1241 |
+
self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
|
1242 |
+
):
|
1243 |
+
# Omit tokens covered by past_key_values
|
1244 |
+
if past_key_values is not None:
|
1245 |
+
if isinstance(past_key_values, Cache):
|
1246 |
+
cache_length = past_key_values.get_seq_length()
|
1247 |
+
past_length = past_key_values.seen_tokens
|
1248 |
+
max_cache_length = past_key_values.get_max_length()
|
1249 |
+
else:
|
1250 |
+
cache_length = past_length = past_key_values[0][0].shape[2]
|
1251 |
+
max_cache_length = None
|
1252 |
+
|
1253 |
+
# Keep only the unprocessed tokens:
|
1254 |
+
# 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where
|
1255 |
+
# some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as
|
1256 |
+
# input)
|
1257 |
+
if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]:
|
1258 |
+
input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]
|
1259 |
+
# 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard
|
1260 |
+
# input_ids based on the past_length.
|
1261 |
+
elif past_length < input_ids.shape[1]:
|
1262 |
+
input_ids = input_ids[:, past_length:]
|
1263 |
+
# 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens.
|
1264 |
+
|
1265 |
+
# If we are about to go beyond the maximum cache length, we need to crop the input attention mask.
|
1266 |
+
if (
|
1267 |
+
max_cache_length is not None
|
1268 |
+
and attention_mask is not None
|
1269 |
+
and cache_length + input_ids.shape[1] > max_cache_length
|
1270 |
+
):
|
1271 |
+
attention_mask = attention_mask[:, -max_cache_length:]
|
1272 |
+
|
1273 |
+
position_ids = kwargs.get("position_ids", None)
|
1274 |
+
if attention_mask is not None and position_ids is None:
|
1275 |
+
# create position_ids on the fly for batch generation
|
1276 |
+
position_ids = attention_mask.long().cumsum(-1) - 1
|
1277 |
+
position_ids.masked_fill_(attention_mask == 0, 1)
|
1278 |
+
if past_key_values:
|
1279 |
+
position_ids = position_ids[:, -input_ids.shape[1] :]
|
1280 |
+
|
1281 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
1282 |
+
if inputs_embeds is not None and past_key_values is None:
|
1283 |
+
model_inputs = {"inputs_embeds": inputs_embeds}
|
1284 |
+
else:
|
1285 |
+
model_inputs = {"input_ids": input_ids}
|
1286 |
+
|
1287 |
+
model_inputs.update(
|
1288 |
+
{
|
1289 |
+
"position_ids": position_ids,
|
1290 |
+
"past_key_values": past_key_values,
|
1291 |
+
"use_cache": kwargs.get("use_cache"),
|
1292 |
+
"attention_mask": attention_mask,
|
1293 |
+
}
|
1294 |
+
)
|
1295 |
+
return model_inputs
|
1296 |
+
|
1297 |
+
@staticmethod
|
1298 |
+
def _reorder_cache(past_key_values, beam_idx):
|
1299 |
+
reordered_past = ()
|
1300 |
+
for layer_past in past_key_values:
|
1301 |
+
reordered_past += (
|
1302 |
+
tuple(past_state.index_select(0, beam_idx.to(past_state.device)) for past_state in layer_past),
|
1303 |
+
)
|
1304 |
+
return reordered_past
|
1305 |
+
|
1306 |
+
|
1307 |
+
@add_start_docstrings(
|
1308 |
+
"""
|
1309 |
+
The Qwen2 Model transformer with a sequence classification head on top (linear layer).
|
1310 |
+
|
1311 |
+
[`Qwen2ForSequenceClassification`] uses the last token in order to do the classification, as other causal models
|
1312 |
+
(e.g. GPT-2) do.
|
1313 |
+
|
1314 |
+
Since it does classification on the last token, it requires to know the position of the last token. If a
|
1315 |
+
`pad_token_id` is defined in the configuration, it finds the last token that is not a padding token in each row. If
|
1316 |
+
no `pad_token_id` is defined, it simply takes the last value in each row of the batch. Since it cannot guess the
|
1317 |
+
padding tokens when `inputs_embeds` are passed instead of `input_ids`, it does the same (take the last value in
|
1318 |
+
each row of the batch).
|
1319 |
+
""",
|
1320 |
+
QWEN2_START_DOCSTRING,
|
1321 |
+
)
|
1322 |
+
class Qwen2ForSequenceClassification(Qwen2PreTrainedModel):
|
1323 |
+
def __init__(self, config):
|
1324 |
+
super().__init__(config)
|
1325 |
+
self.num_labels = config.num_labels
|
1326 |
+
self.model = Qwen2Model(config)
|
1327 |
+
self.score = nn.Linear(config.hidden_size, self.num_labels, bias=False)
|
1328 |
+
|
1329 |
+
# Initialize weights and apply final processing
|
1330 |
+
self.post_init()
|
1331 |
+
|
1332 |
+
def get_input_embeddings(self):
|
1333 |
+
return self.model.embed_tokens
|
1334 |
+
|
1335 |
+
def set_input_embeddings(self, value):
|
1336 |
+
self.model.embed_tokens = value
|
1337 |
+
|
1338 |
+
@add_start_docstrings_to_model_forward(QWEN2_INPUTS_DOCSTRING)
|
1339 |
+
def forward(
|
1340 |
+
self,
|
1341 |
+
input_ids: torch.LongTensor = None,
|
1342 |
+
attention_mask: Optional[torch.Tensor] = None,
|
1343 |
+
position_ids: Optional[torch.LongTensor] = None,
|
1344 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
1345 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
1346 |
+
labels: Optional[torch.LongTensor] = None,
|
1347 |
+
use_cache: Optional[bool] = None,
|
1348 |
+
output_attentions: Optional[bool] = None,
|
1349 |
+
output_hidden_states: Optional[bool] = None,
|
1350 |
+
return_dict: Optional[bool] = None,
|
1351 |
+
is_causal: Optional[bool] = True,
|
1352 |
+
) -> Union[Tuple, SequenceClassifierOutputWithPast]:
|
1353 |
+
r"""
|
1354 |
+
labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
|
1355 |
+
Labels for computing the sequence classification/regression loss. Indices should be in `[0, ...,
|
1356 |
+
config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
|
1357 |
+
`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
|
1358 |
+
"""
|
1359 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
1360 |
+
|
1361 |
+
transformer_outputs = self.model(
|
1362 |
+
input_ids,
|
1363 |
+
attention_mask=attention_mask,
|
1364 |
+
position_ids=position_ids,
|
1365 |
+
past_key_values=past_key_values,
|
1366 |
+
inputs_embeds=inputs_embeds,
|
1367 |
+
use_cache=use_cache,
|
1368 |
+
output_attentions=output_attentions,
|
1369 |
+
output_hidden_states=output_hidden_states,
|
1370 |
+
return_dict=return_dict,
|
1371 |
+
is_causal=is_causal,
|
1372 |
+
)
|
1373 |
+
hidden_states = transformer_outputs[0]
|
1374 |
+
logits = self.score(hidden_states)
|
1375 |
+
|
1376 |
+
if input_ids is not None:
|
1377 |
+
batch_size = input_ids.shape[0]
|
1378 |
+
else:
|
1379 |
+
batch_size = inputs_embeds.shape[0]
|
1380 |
+
|
1381 |
+
if self.config.pad_token_id is None and batch_size != 1:
|
1382 |
+
raise ValueError("Cannot handle batch sizes > 1 if no padding token is defined.")
|
1383 |
+
if self.config.pad_token_id is None:
|
1384 |
+
sequence_lengths = -1
|
1385 |
+
else:
|
1386 |
+
if input_ids is not None:
|
1387 |
+
# if no pad token found, use modulo instead of reverse indexing for ONNX compatibility
|
1388 |
+
sequence_lengths = torch.eq(input_ids, self.config.pad_token_id).int().argmax(-1) - 1
|
1389 |
+
sequence_lengths = sequence_lengths % input_ids.shape[-1]
|
1390 |
+
sequence_lengths = sequence_lengths.to(logits.device)
|
1391 |
+
else:
|
1392 |
+
sequence_lengths = -1
|
1393 |
+
|
1394 |
+
pooled_logits = logits[torch.arange(batch_size, device=logits.device), sequence_lengths]
|
1395 |
+
|
1396 |
+
loss = None
|
1397 |
+
if labels is not None:
|
1398 |
+
labels = labels.to(logits.device)
|
1399 |
+
if self.config.problem_type is None:
|
1400 |
+
if self.num_labels == 1:
|
1401 |
+
self.config.problem_type = "regression"
|
1402 |
+
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
|
1403 |
+
self.config.problem_type = "single_label_classification"
|
1404 |
+
else:
|
1405 |
+
self.config.problem_type = "multi_label_classification"
|
1406 |
+
|
1407 |
+
if self.config.problem_type == "regression":
|
1408 |
+
loss_fct = MSELoss()
|
1409 |
+
if self.num_labels == 1:
|
1410 |
+
loss = loss_fct(pooled_logits.squeeze(), labels.squeeze())
|
1411 |
+
else:
|
1412 |
+
loss = loss_fct(pooled_logits, labels)
|
1413 |
+
elif self.config.problem_type == "single_label_classification":
|
1414 |
+
loss_fct = CrossEntropyLoss()
|
1415 |
+
loss = loss_fct(pooled_logits.view(-1, self.num_labels), labels.view(-1))
|
1416 |
+
elif self.config.problem_type == "multi_label_classification":
|
1417 |
+
loss_fct = BCEWithLogitsLoss()
|
1418 |
+
loss = loss_fct(pooled_logits, labels)
|
1419 |
+
if not return_dict:
|
1420 |
+
output = (pooled_logits,) + transformer_outputs[1:]
|
1421 |
+
return ((loss,) + output) if loss is not None else output
|
1422 |
+
|
1423 |
+
return SequenceClassifierOutputWithPast(
|
1424 |
+
loss=loss,
|
1425 |
+
logits=pooled_logits,
|
1426 |
+
past_key_values=transformer_outputs.past_key_values,
|
1427 |
+
hidden_states=transformer_outputs.hidden_states,
|
1428 |
+
attentions=transformer_outputs.attentions,
|
1429 |
+
)
|
modules.json
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[
|
2 |
+
{
|
3 |
+
"idx": 0,
|
4 |
+
"name": "0",
|
5 |
+
"path": "",
|
6 |
+
"type": "sentence_transformers.models.Transformer"
|
7 |
+
},
|
8 |
+
{
|
9 |
+
"idx": 1,
|
10 |
+
"name": "1",
|
11 |
+
"path": "1_Pooling",
|
12 |
+
"type": "sentence_transformers.models.Pooling"
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"idx": 2,
|
16 |
+
"name": "2",
|
17 |
+
"path": "2_Normalize",
|
18 |
+
"type": "sentence_transformers.models.Normalize"
|
19 |
+
}
|
20 |
+
]
|
scripts/eval_mteb.py
ADDED
@@ -0,0 +1,732 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
from collections import defaultdict
|
3 |
+
import json
|
4 |
+
import logging
|
5 |
+
import math
|
6 |
+
import os
|
7 |
+
import sys
|
8 |
+
import queue
|
9 |
+
from typing import Dict, List, Optional, Union
|
10 |
+
|
11 |
+
from tqdm.autonotebook import trange
|
12 |
+
import datasets
|
13 |
+
import numpy as np
|
14 |
+
import torch
|
15 |
+
import torch.multiprocessing as mp
|
16 |
+
from transformers import AutoModel, AutoTokenizer
|
17 |
+
from transformers import AutoModelForCausalLM
|
18 |
+
from mteb import MTEB, CrosslingualTask, MultilingualTask
|
19 |
+
|
20 |
+
TASK_LIST_CLASSIFICATION = [
|
21 |
+
"AmazonCounterfactualClassification",
|
22 |
+
"AmazonPolarityClassification",
|
23 |
+
"AmazonReviewsClassification",
|
24 |
+
"Banking77Classification",
|
25 |
+
"EmotionClassification",
|
26 |
+
"ImdbClassification",
|
27 |
+
"MassiveIntentClassification",
|
28 |
+
"MassiveScenarioClassification",
|
29 |
+
"MTOPDomainClassification",
|
30 |
+
"MTOPIntentClassification",
|
31 |
+
"ToxicConversationsClassification",
|
32 |
+
"TweetSentimentExtractionClassification",
|
33 |
+
]
|
34 |
+
|
35 |
+
TASK_LIST_CLUSTERING = [
|
36 |
+
"ArxivClusteringP2P",
|
37 |
+
"ArxivClusteringS2S",
|
38 |
+
"BiorxivClusteringP2P",
|
39 |
+
"BiorxivClusteringS2S",
|
40 |
+
"MedrxivClusteringP2P",
|
41 |
+
"MedrxivClusteringS2S",
|
42 |
+
"RedditClustering",
|
43 |
+
"RedditClusteringP2P",
|
44 |
+
"StackExchangeClustering",
|
45 |
+
"StackExchangeClusteringP2P",
|
46 |
+
"TwentyNewsgroupsClustering",
|
47 |
+
]
|
48 |
+
|
49 |
+
TASK_LIST_PAIR_CLASSIFICATION = [
|
50 |
+
"SprintDuplicateQuestions",
|
51 |
+
"TwitterSemEval2015",
|
52 |
+
"TwitterURLCorpus",
|
53 |
+
]
|
54 |
+
|
55 |
+
TASK_LIST_RERANKING = [
|
56 |
+
"AskUbuntuDupQuestions",
|
57 |
+
"MindSmallReranking",
|
58 |
+
"SciDocsRR",
|
59 |
+
"StackOverflowDupQuestions",
|
60 |
+
]
|
61 |
+
|
62 |
+
TASK_LIST_RETRIEVAL = [
|
63 |
+
"ArguAna",
|
64 |
+
"ClimateFEVER",
|
65 |
+
"CQADupstackAndroidRetrieval",
|
66 |
+
"CQADupstackEnglishRetrieval",
|
67 |
+
"CQADupstackGamingRetrieval",
|
68 |
+
"CQADupstackGisRetrieval",
|
69 |
+
"CQADupstackMathematicaRetrieval",
|
70 |
+
"CQADupstackPhysicsRetrieval",
|
71 |
+
"CQADupstackProgrammersRetrieval",
|
72 |
+
"CQADupstackStatsRetrieval",
|
73 |
+
"CQADupstackTexRetrieval",
|
74 |
+
"CQADupstackUnixRetrieval",
|
75 |
+
"CQADupstackWebmastersRetrieval",
|
76 |
+
"CQADupstackWordpressRetrieval",
|
77 |
+
"DBPedia",
|
78 |
+
"FEVER",
|
79 |
+
"FiQA2018",
|
80 |
+
"HotpotQA",
|
81 |
+
"MSMARCO",
|
82 |
+
"NFCorpus",
|
83 |
+
"NQ",
|
84 |
+
"QuoraRetrieval",
|
85 |
+
"SCIDOCS",
|
86 |
+
"SciFact",
|
87 |
+
"Touche2020",
|
88 |
+
"TRECCOVID",
|
89 |
+
]
|
90 |
+
|
91 |
+
TASK_LIST_STS = [
|
92 |
+
"BIOSSES",
|
93 |
+
"SICK-R",
|
94 |
+
"STS12",
|
95 |
+
"STS13",
|
96 |
+
"STS14",
|
97 |
+
"STS15",
|
98 |
+
"STS16",
|
99 |
+
"STS17",
|
100 |
+
"STS22",
|
101 |
+
"STSBenchmark",
|
102 |
+
"SummEval",
|
103 |
+
]
|
104 |
+
|
105 |
+
MTEB_TASK_LIST = (
|
106 |
+
TASK_LIST_CLASSIFICATION
|
107 |
+
+ TASK_LIST_CLUSTERING
|
108 |
+
+ TASK_LIST_PAIR_CLASSIFICATION
|
109 |
+
+ TASK_LIST_RERANKING
|
110 |
+
+ TASK_LIST_RETRIEVAL
|
111 |
+
+ TASK_LIST_STS
|
112 |
+
)
|
113 |
+
|
114 |
+
|
115 |
+
CMTEB_TASK_LIST = ['TNews', 'IFlyTek', 'MultilingualSentiment', 'JDReview', 'OnlineShopping', 'Waimai','AmazonReviewsClassification', 'MassiveIntentClassification', 'MassiveScenarioClassification', 'MultilingualSentiment',
|
116 |
+
'CLSClusteringS2S', 'CLSClusteringP2P', 'ThuNewsClusteringS2S', 'ThuNewsClusteringP2P',
|
117 |
+
'Ocnli', 'Cmnli',
|
118 |
+
'T2Reranking', 'MmarcoReranking', 'CMedQAv1', 'CMedQAv2',
|
119 |
+
'T2Retrieval', 'MMarcoRetrieval', 'DuRetrieval', 'CovidRetrieval', 'CmedqaRetrieval', 'EcomRetrieval', 'MedicalRetrieval', 'VideoRetrieval',
|
120 |
+
'ATEC', 'BQ', 'LCQMC', 'PAWSX', 'STSB', 'AFQMC', 'QBQTC', 'STS22']
|
121 |
+
|
122 |
+
|
123 |
+
MTEB_PL = [
|
124 |
+
"CBD","PolEmo2.0-IN","PolEmo2.0-OUT","AllegroReviews","PAC","MassiveIntentClassification","MassiveScenarioClassification",
|
125 |
+
"SICK-E-PL","PPC","CDSC-E","PSC","8TagsClustering","SICK-R-PL","CDSC-R","STS22",
|
126 |
+
"ArguAna-PL","DBPedia-PL","FiQA-PL","HotpotQA-PL","MSMARCO-PL","NFCorpus-PL","NQ-PL","Quora-PL","SCIDOCS-PL","SciFact-PL","TRECCOVID-PL"
|
127 |
+
]
|
128 |
+
|
129 |
+
MTEB_FR = [
|
130 |
+
"AmazonReviewsClassification","MasakhaNEWSClassification","MassiveIntentClassification",
|
131 |
+
"MassiveScenarioClassification","MTOPDomainClassification","MTOPIntentClassification","OpusparcusPC","PawsX",
|
132 |
+
"AlloProfClusteringP2P","AlloProfClusteringS2S","HALClusteringS2S","MasakhaNEWSClusteringP2P","MasakhaNEWSClusteringS2S","MLSUMClusteringP2P","MLSUMClusteringS2S",
|
133 |
+
"SyntecReranking","AlloprofReranking","AlloprofRetrieval","BSARDRetrieval","SyntecRetrieval","XPQARetrieval","MintakaRetrieval",
|
134 |
+
"SummEvalFr","STSBenchmarkMultilingualSTS","STS22","SICKFr"
|
135 |
+
]
|
136 |
+
|
137 |
+
logging.basicConfig(
|
138 |
+
level=logging.INFO,
|
139 |
+
format='%(asctime)s - %(levelname)s - %(name)s : %(message)s'
|
140 |
+
)
|
141 |
+
|
142 |
+
logger = logging.getLogger('eval_mteb_qwen.py')
|
143 |
+
|
144 |
+
def get_detailed_instruct(task_description: str) -> str:
|
145 |
+
if not task_description:
|
146 |
+
return ''
|
147 |
+
|
148 |
+
return 'Instruct: {}\nQuery: '.format(task_description)
|
149 |
+
|
150 |
+
def get_task_def_by_task_name_and_type(task_name: str, task_type: str, default_instruct='Given a web search query, retrieve relevant passages that answer the query') -> str:
|
151 |
+
if task_type in ['STS']:
|
152 |
+
return "Retrieve semantically similar text"
|
153 |
+
|
154 |
+
if task_type in ['Summarization']:
|
155 |
+
return "Given a news summary, retrieve other semantically similar summaries"
|
156 |
+
|
157 |
+
if task_type in ['BitextMining']:
|
158 |
+
return "Retrieve parallel sentences"
|
159 |
+
|
160 |
+
if task_type in ['Classification']:
|
161 |
+
task_name_to_instruct: Dict[str, str] = {
|
162 |
+
'AmazonCounterfactualClassification': 'Classify a given Amazon customer review text as either counterfactual or not-counterfactual',
|
163 |
+
'AmazonPolarityClassification': 'Classify Amazon reviews into positive or negative sentiment',
|
164 |
+
'AmazonReviewsClassification': 'Classify the given Amazon review into its appropriate rating category',
|
165 |
+
'Banking77Classification': 'Given a online banking query, find the corresponding intents',
|
166 |
+
'EmotionClassification': 'Classify the emotion expressed in the given Twitter message into one of the six emotions: anger, fear, joy, love, sadness, and surprise',
|
167 |
+
'ImdbClassification': 'Classify the sentiment expressed in the given movie review text from the IMDB dataset',
|
168 |
+
'MassiveIntentClassification': 'Given a user utterance as query, find the user intents',
|
169 |
+
'MassiveScenarioClassification': 'Given a user utterance as query, find the user scenarios',
|
170 |
+
'MTOPDomainClassification': 'Classify the intent domain of the given utterance in task-oriented conversation',
|
171 |
+
'MTOPIntentClassification': 'Classify the intent of the given utterance in task-oriented conversation',
|
172 |
+
'ToxicConversationsClassification': 'Classify the given comments as either toxic or not toxic',
|
173 |
+
'TweetSentimentExtractionClassification': 'Classify the sentiment of a given tweet as either positive, negative, or neutral',
|
174 |
+
# C-MTEB eval instructions
|
175 |
+
'TNews': 'Classify the fine-grained category of the given news title',
|
176 |
+
'IFlyTek': 'Given an App description text, find the appropriate fine-grained category',
|
177 |
+
'MultilingualSentiment': 'Classify sentiment of the customer review into positive, neutral, or negative',
|
178 |
+
'JDReview': 'Classify the customer review for iPhone on e-commerce platform into positive or negative',
|
179 |
+
'OnlineShopping': 'Classify the customer review for online shopping into positive or negative',
|
180 |
+
'Waimai': 'Classify the customer review from a food takeaway platform into positive or negative',
|
181 |
+
# MTEB-pl eval instructions
|
182 |
+
"CBD":"Classify the sentiment of polish tweet reviews",
|
183 |
+
"PolEmo2.0-IN": "Classify the sentiment of in-domain (medicine and hotels) online reviews",
|
184 |
+
"PolEmo2.0-OUT":"Classify the sentiment of out-of-domain (products and school) online reviews",
|
185 |
+
"AllegroReviews": "Classify the sentiment of reviews from e-commerce marketplace Allegro",
|
186 |
+
"PAC": "Classify the sentence into one of the two types: \"BEZPIECZNE_POSTANOWIENIE_UMOWNE\" and \"KLAUZULA_ABUZYWNA\"",
|
187 |
+
|
188 |
+
}
|
189 |
+
return task_name_to_instruct[task_name]
|
190 |
+
|
191 |
+
if task_type in ['Clustering']:
|
192 |
+
task_name_to_instruct: Dict[str, str] = {
|
193 |
+
'ArxivClusteringP2P': 'Identify the main and secondary category of Arxiv papers based on the titles and abstracts',
|
194 |
+
'ArxivClusteringS2S': 'Identify the main and secondary category of Arxiv papers based on the titles',
|
195 |
+
'BiorxivClusteringP2P': 'Identify the main category of Biorxiv papers based on the titles and abstracts',
|
196 |
+
'BiorxivClusteringS2S': 'Identify the main category of Biorxiv papers based on the titles',
|
197 |
+
'MedrxivClusteringP2P': 'Identify the main category of Medrxiv papers based on the titles and abstracts',
|
198 |
+
'MedrxivClusteringS2S': 'Identify the main category of Medrxiv papers based on the titles',
|
199 |
+
'RedditClustering': 'Identify the topic or theme of Reddit posts based on the titles',
|
200 |
+
'RedditClusteringP2P': 'Identify the topic or theme of Reddit posts based on the titles and posts',
|
201 |
+
'StackExchangeClustering': 'Identify the topic or theme of StackExchange posts based on the titles',
|
202 |
+
'StackExchangeClusteringP2P': 'Identify the topic or theme of StackExchange posts based on the given paragraphs',
|
203 |
+
'TwentyNewsgroupsClustering': 'Identify the topic or theme of the given news articles',
|
204 |
+
# C-MTEB eval instructions
|
205 |
+
'CLSClusteringS2S': 'Identify the main category of scholar papers based on the titles',
|
206 |
+
'CLSClusteringP2P': 'Identify the main category of scholar papers based on the titles and abstracts',
|
207 |
+
'ThuNewsClusteringS2S': 'Identify the topic or theme of the given news articles based on the titles',
|
208 |
+
'ThuNewsClusteringP2P': 'Identify the topic or theme of the given news articles based on the titles and contents',
|
209 |
+
# MTEB-fr eval instructions
|
210 |
+
"AlloProfClusteringP2P": "Identify the main category of Allo Prof document based on the titles and descriptions",
|
211 |
+
"AlloProfClusteringS2S": "Identify the main category of Allo Prof document based on the titles",
|
212 |
+
"HALClusteringS2S": "Identify the main category of academic passage based on the titles and contents",
|
213 |
+
"MasakhaNEWSClusteringP2P": "Identify the topic or theme of the given news articles based on the titles and contents",
|
214 |
+
"MasakhaNEWSClusteringS2S": "Identify the topic or theme of the given news articles based on the titles",
|
215 |
+
"MLSUMClusteringP2P": "Identify the topic or theme of the given articles based on the titles and contents",
|
216 |
+
"MLSUMClusteringS2S": "Identify the topic or theme of the given articles based on the titles",
|
217 |
+
# MTEB-pl eval instructions
|
218 |
+
"8TagsClustering": "Identify of headlines from social media posts in Polish into 8 categories: film, history, food, medicine, motorization, work, sport and technology",
|
219 |
+
}
|
220 |
+
return task_name_to_instruct[task_name]
|
221 |
+
|
222 |
+
if task_type in ['Reranking', 'PairClassification']:
|
223 |
+
task_name_to_instruct: Dict[str, str] = {
|
224 |
+
'AskUbuntuDupQuestions': 'Retrieve duplicate questions from AskUbuntu forum',
|
225 |
+
'MindSmallReranking': 'Retrieve relevant news articles based on user browsing history',
|
226 |
+
'SciDocsRR': 'Given a title of a scientific paper, retrieve the titles of other relevant papers',
|
227 |
+
'StackOverflowDupQuestions': 'Retrieve duplicate questions from StackOverflow forum',
|
228 |
+
'SprintDuplicateQuestions': 'Retrieve duplicate questions from Sprint forum',
|
229 |
+
'TwitterSemEval2015': 'Retrieve tweets that are semantically similar to the given tweet',
|
230 |
+
'TwitterURLCorpus': 'Retrieve tweets that are semantically similar to the given tweet',
|
231 |
+
# C-MTEB eval instructions
|
232 |
+
'T2Reranking': 'Given a Chinese search query, retrieve web passages that answer the question',
|
233 |
+
'MmarcoReranking': 'Given a Chinese search query, retrieve web passages that answer the question',
|
234 |
+
'CMedQAv1': 'Given a Chinese community medical question, retrieve replies that best answer the question',
|
235 |
+
'CMedQAv2': 'Given a Chinese community medical question, retrieve replies that best answer the question',
|
236 |
+
'Ocnli': 'Retrieve semantically similar text.',
|
237 |
+
'Cmnli': 'Retrieve semantically similar text.',
|
238 |
+
# MTEB-fr eval instructions
|
239 |
+
"AlloprofReranking": "Given a question, retrieve passages that answer the question",
|
240 |
+
"OpusparcusPC":"Retrieve semantically similar text",
|
241 |
+
"PawsX":"Retrieve semantically similar text",
|
242 |
+
"SyntecReranking": "Given a question, retrieve passages that answer the question",
|
243 |
+
# MTEB-pl eval instructions
|
244 |
+
"SICK-E-PL": "Retrieve semantically similar text",
|
245 |
+
"PPC": "Retrieve semantically similar text",
|
246 |
+
"CDSC-E": "Retrieve semantically similar text",
|
247 |
+
"PSC": "Retrieve semantically similar text",
|
248 |
+
}
|
249 |
+
return task_name_to_instruct[task_name]
|
250 |
+
|
251 |
+
if task_type in ['Retrieval']:
|
252 |
+
if task_name.lower().startswith('cqadupstack'):
|
253 |
+
return 'Given a question, retrieve detailed question descriptions from Stackexchange that are duplicates to the given question'
|
254 |
+
|
255 |
+
task_name_to_instruct: Dict[str, str] = {
|
256 |
+
'ArguAna': 'Given a claim, find documents that refute the claim',
|
257 |
+
'ClimateFEVER': 'Given a claim about climate change, retrieve documents that support or refute the claim',
|
258 |
+
'DBPedia': 'Given a query, retrieve relevant entity descriptions from DBPedia',
|
259 |
+
'FEVER': 'Given a claim, retrieve documents that support or refute the claim',
|
260 |
+
'FiQA2018': 'Given a financial question, retrieve user replies that best answer the question',
|
261 |
+
'HotpotQA': 'Given a multi-hop question, retrieve documents that can help answer the question',
|
262 |
+
'MSMARCO': 'Given a web search query, retrieve relevant passages that answer the query',
|
263 |
+
'NFCorpus': 'Given a question, retrieve relevant documents that best answer the question',
|
264 |
+
'NQ': 'Given a question, retrieve Wikipedia passages that answer the question',
|
265 |
+
'QuoraRetrieval': 'Given a question, retrieve questions that are semantically equivalent to the given question',
|
266 |
+
'SCIDOCS': 'Given a scientific paper title, retrieve paper abstracts that are cited by the given paper',
|
267 |
+
'SciFact': 'Given a scientific claim, retrieve documents that support or refute the claim',
|
268 |
+
'Touche2020': 'Given a question, retrieve detailed and persuasive arguments that answer the question',
|
269 |
+
'TRECCOVID': 'Given a query on COVID-19, retrieve documents that answer the query',
|
270 |
+
# C-MTEB eval instructions
|
271 |
+
'T2Retrieval': 'Given a Chinese search query, retrieve web passages that answer the question',
|
272 |
+
'MMarcoRetrieval': 'Given a web search query, retrieve relevant passages that answer the query',
|
273 |
+
'DuRetrieval': 'Given a Chinese search query, retrieve web passages that answer the question',
|
274 |
+
'CovidRetrieval': 'Given a question on COVID-19, retrieve news articles that answer the question',
|
275 |
+
'CmedqaRetrieval': 'Given a Chinese community medical question, retrieve replies that best answer the question',
|
276 |
+
'EcomRetrieval': 'Given a user query from an e-commerce website, retrieve description sentences of relevant products',
|
277 |
+
'MedicalRetrieval': 'Given a medical question, retrieve user replies that best answer the question',
|
278 |
+
'VideoRetrieval': 'Given a video search query, retrieve the titles of relevant videos',
|
279 |
+
# MTEB-fr eval instructions
|
280 |
+
"AlloprofRetrieval": "Given a question, retrieve passages that answer the question",
|
281 |
+
"BSARDRetrieval": "Given a question, retrieve passages that answer the question",
|
282 |
+
"SyntecRetrieval": "Given a question, retrieve passages that answer the question",
|
283 |
+
"XPQARetrieval": "Given a question, retrieve passages that answer the question",
|
284 |
+
"MintakaRetrieval": "Given a question, retrieve passages that answer the question",
|
285 |
+
# MTEB-pl eval instructions
|
286 |
+
"ArguAna-PL": "Given a claim, find documents that refute the claim",
|
287 |
+
"DBPedia-PL": "Given a query, retrieve relevant entity descriptions from DBPedia",
|
288 |
+
"FiQA-PL": "Given a financial question, retrieve user replies that best answer the question",
|
289 |
+
"HotpotQA-PL": "Given a multi-hop question, retrieve documents that can help answer the question",
|
290 |
+
"MSMARCO-PL": "Given a web search query, retrieve relevant passages that answer the query",
|
291 |
+
"NFCorpus-PL": "Given a question, retrieve relevant documents that best answer the question",
|
292 |
+
"NQ-PL": "Given a question, retrieve Wikipedia passages that answer the question",
|
293 |
+
"Quora-PL": "Given a question, retrieve questions that are semantically equivalent to the given question",
|
294 |
+
"SCIDOCS-PL": "Given a scientific paper title, retrieve paper abstracts that are cited by the given paper",
|
295 |
+
"SciFact-PL": "Given a scientific claim, retrieve documents that support or refute the claim",
|
296 |
+
"TRECCOVID-PL": "Given a query on COVID-19, retrieve documents that answer the query"
|
297 |
+
}
|
298 |
+
|
299 |
+
# add lower case keys to match some beir names
|
300 |
+
task_name_to_instruct.update({k.lower(): v for k, v in task_name_to_instruct.items()})
|
301 |
+
# other cases where lower case match still doesn't work
|
302 |
+
task_name_to_instruct['trec-covid'] = task_name_to_instruct['TRECCOVID']
|
303 |
+
task_name_to_instruct['climate-fever'] = task_name_to_instruct['ClimateFEVER']
|
304 |
+
task_name_to_instruct['dbpedia-entity'] = task_name_to_instruct['DBPedia']
|
305 |
+
task_name_to_instruct['webis-touche2020'] = task_name_to_instruct['Touche2020']
|
306 |
+
task_name_to_instruct['fiqa'] = task_name_to_instruct['FiQA2018']
|
307 |
+
task_name_to_instruct['quora'] = task_name_to_instruct['QuoraRetrieval']
|
308 |
+
|
309 |
+
# for miracl evaluation
|
310 |
+
task_name_to_instruct['miracl'] = 'Given a question, retrieve Wikipedia passages that answer the question'
|
311 |
+
|
312 |
+
return task_name_to_instruct[task_name]
|
313 |
+
logging.warning(f"No instruction config for task {task_name} with type {task_type}, use default instruction.")
|
314 |
+
return default_instruct
|
315 |
+
|
316 |
+
class Encoder(torch.nn.Module):
|
317 |
+
def __init__(self, name_or_path:str, pooling: str):
|
318 |
+
super().__init__()
|
319 |
+
self.model = AutoModel.from_pretrained(name_or_path, trust_remote_code=True)
|
320 |
+
self.model = self.model.half()
|
321 |
+
self.model.eval()
|
322 |
+
self.pooling = pooling
|
323 |
+
|
324 |
+
def forward(self, **features) -> torch.Tensor:
|
325 |
+
output = self.model(**features, output_hidden_states=True, return_dict=True)
|
326 |
+
hidden_state = output.hidden_states[-1]
|
327 |
+
embeddings = self.pooler(hidden_state, **features)
|
328 |
+
return embeddings
|
329 |
+
|
330 |
+
def pooler(
|
331 |
+
self,
|
332 |
+
hidden_state: torch.Tensor,
|
333 |
+
attention_mask: torch.Tensor,
|
334 |
+
**kwargs
|
335 |
+
) -> torch.Tensor:
|
336 |
+
if attention_mask.ndim == 2:
|
337 |
+
mask_expanded = attention_mask.unsqueeze(-1).expand(hidden_state.size())
|
338 |
+
elif attention_mask.ndim == 3:
|
339 |
+
mask_expanded = attention_mask
|
340 |
+
else:
|
341 |
+
raise RuntimeError(f"Unexpected {attention_mask.ndim=}")
|
342 |
+
|
343 |
+
hidden_state = hidden_state * mask_expanded
|
344 |
+
|
345 |
+
if self.pooling == 'first':
|
346 |
+
pooled_output = hidden_state[:, 0]
|
347 |
+
|
348 |
+
elif self.pooling == 'last':
|
349 |
+
left_padding = (attention_mask[:, -1].sum() == attention_mask.shape[0])
|
350 |
+
if left_padding:
|
351 |
+
return hidden_state[:, -1]
|
352 |
+
else:
|
353 |
+
sequence_lengths = attention_mask.sum(dim=1) - 1
|
354 |
+
batch_size = hidden_state.shape[0]
|
355 |
+
return hidden_state[torch.arange(batch_size, device=hidden_state.device), sequence_lengths]
|
356 |
+
elif self.pooling == 'mean':
|
357 |
+
# TODO: weight
|
358 |
+
lengths = mask_expanded.sum(1).clamp(min=1e-9)
|
359 |
+
pooled_output = hidden_state.sum(dim=1) / lengths
|
360 |
+
|
361 |
+
elif self.pooling == 'weightedmean':
|
362 |
+
input_mask_expanded = attention_mask.unsqueeze(-1).expand(hidden_state.size()).float()
|
363 |
+
# hidden_state shape: bs, seq, hidden_dim
|
364 |
+
weights = (
|
365 |
+
torch.arange(start=1, end=hidden_state.shape[1] + 1)
|
366 |
+
.unsqueeze(0)
|
367 |
+
.unsqueeze(-1)
|
368 |
+
.expand(hidden_state.size())
|
369 |
+
.float().to(hidden_state.device)
|
370 |
+
)
|
371 |
+
assert weights.shape == hidden_state.shape == input_mask_expanded.shape
|
372 |
+
input_mask_expanded = input_mask_expanded * weights
|
373 |
+
|
374 |
+
sum_embeddings = torch.sum(hidden_state * input_mask_expanded, 1)
|
375 |
+
sum_mask = input_mask_expanded.sum(1)
|
376 |
+
sum_mask = torch.clamp(sum_mask, min=1e-9)
|
377 |
+
pooled_output = sum_embeddings / sum_mask
|
378 |
+
|
379 |
+
else:
|
380 |
+
raise ValueError(f"Wrong pooler mode : {self.pooling}")
|
381 |
+
return pooled_output
|
382 |
+
|
383 |
+
|
384 |
+
class Wrapper:
|
385 |
+
def __init__(
|
386 |
+
self,
|
387 |
+
tokenizer,
|
388 |
+
encoder: Encoder,
|
389 |
+
batch_size: int,
|
390 |
+
max_seq_len: int = 512,
|
391 |
+
normalize_embeddings: bool = False,
|
392 |
+
default_query: bool = False,
|
393 |
+
force_default: bool = False,
|
394 |
+
sep: str = " ",
|
395 |
+
mp_tensor_to_cuda: bool = False,
|
396 |
+
instruction: str = None,
|
397 |
+
attn_type: str = None
|
398 |
+
):
|
399 |
+
self.tokenizer = tokenizer
|
400 |
+
self.model = encoder
|
401 |
+
self.batch_size = batch_size
|
402 |
+
self.max_seq_len = max_seq_len
|
403 |
+
self.pool: dict = None
|
404 |
+
self.normalize_embeddings = normalize_embeddings
|
405 |
+
self.mp_tensor_to_cuda = mp_tensor_to_cuda
|
406 |
+
self._target_device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
407 |
+
self.eod_id = self.tokenizer.convert_tokens_to_ids("<|endoftext|>")
|
408 |
+
self.instruction = instruction
|
409 |
+
self.default_query = default_query
|
410 |
+
self.force_default = force_default
|
411 |
+
|
412 |
+
if self.tokenizer.padding_side != 'right':
|
413 |
+
logger.warning(f"Change tokenizer.padding_side from {self.tokenizer.padding_side} to right")
|
414 |
+
self.tokenizer.padding_side = 'right'
|
415 |
+
if self.tokenizer.pad_token is None:
|
416 |
+
logger.warning(f"Set tokenizer.pad_token as eos_token {self.tokenizer.eos_token}")
|
417 |
+
self.tokenizer.pad_token='<|endoftext|>'
|
418 |
+
|
419 |
+
def start(self, target_devices: Optional[List[str]] = None):
|
420 |
+
"""
|
421 |
+
Starts multi process to process the encoding with several, independent processes.
|
422 |
+
This method is recommended if you want to encode on multiple GPUs. It is advised
|
423 |
+
to start only one process per GPU. This method works together with encode_multi_process
|
424 |
+
|
425 |
+
:param target_devices: PyTorch target devices, e.g. cuda:0, cuda:1... If None, all available CUDA devices will be used
|
426 |
+
:return: Returns a dict with the target processes, an input queue and and output queue.
|
427 |
+
"""
|
428 |
+
if target_devices is None:
|
429 |
+
if torch.cuda.is_available():
|
430 |
+
target_devices = ['cuda:{}'.format(i) for i in range(torch.cuda.device_count())]
|
431 |
+
else:
|
432 |
+
logger.info("CUDA is not available. Start 4 CPU worker")
|
433 |
+
target_devices = ['cpu']*4
|
434 |
+
|
435 |
+
logger.info("Start multi-process pool on devices: {}".format(', '.join(map(str, target_devices))))
|
436 |
+
print('multi instruction', self.instruction)
|
437 |
+
ctx = mp.get_context('spawn')
|
438 |
+
input_queue = ctx.Queue()
|
439 |
+
output_queue = ctx.Queue()
|
440 |
+
processes = []
|
441 |
+
|
442 |
+
for cuda_id in target_devices:
|
443 |
+
p = ctx.Process(
|
444 |
+
target=self._encode_multi_process_worker,
|
445 |
+
args=(cuda_id, self, input_queue, output_queue),
|
446 |
+
daemon=True
|
447 |
+
)
|
448 |
+
p.start()
|
449 |
+
processes.append(p)
|
450 |
+
|
451 |
+
self.pool = {'input': input_queue, 'output': output_queue, 'processes': processes}
|
452 |
+
|
453 |
+
def stop(self):
|
454 |
+
"""
|
455 |
+
Stops all processes started with start_multi_process_pool
|
456 |
+
"""
|
457 |
+
for p in self.pool['processes']:
|
458 |
+
p.terminate()
|
459 |
+
|
460 |
+
for p in self.pool['processes']:
|
461 |
+
p.join()
|
462 |
+
p.close()
|
463 |
+
|
464 |
+
self.pool['input'].close()
|
465 |
+
self.pool['output'].close()
|
466 |
+
|
467 |
+
@staticmethod
|
468 |
+
def _encode_multi_process_worker(target_device: str, model, input_queue, results_queue):
|
469 |
+
"""
|
470 |
+
Internal working process to encode sentences in multi-process setup
|
471 |
+
"""
|
472 |
+
while True:
|
473 |
+
try:
|
474 |
+
id, sentences, kwargs = input_queue.get()
|
475 |
+
kwargs.update(device=target_device, show_progress_bar=False, convert_to_numpy=True)
|
476 |
+
embeddings = model._encode(sentences, **kwargs)
|
477 |
+
results_queue.put([id, embeddings])
|
478 |
+
except queue.Empty:
|
479 |
+
break
|
480 |
+
|
481 |
+
def encode_multi_process(
|
482 |
+
self,
|
483 |
+
sentences: List[str],
|
484 |
+
**kwargs
|
485 |
+
):
|
486 |
+
"""
|
487 |
+
This method allows to run encode() on multiple GPUs. The sentences are chunked into smaller packages
|
488 |
+
and sent to individual processes, which encode these on the different GPUs. This method is only suitable
|
489 |
+
for encoding large sets of sentences
|
490 |
+
|
491 |
+
:param sentences: List of sentences
|
492 |
+
:param pool: A pool of workers started with SentenceTransformer.start_multi_process_pool
|
493 |
+
:param chunk_size: Sentences are chunked and sent to the individual processes. If none, it determine a sensible size.
|
494 |
+
:param kwargs: other keyword arguments for model.encode() such as batch_size
|
495 |
+
:return: Numpy matrix with all embeddings
|
496 |
+
"""
|
497 |
+
part_size = math.ceil(len(sentences) / len(self.pool["processes"]))
|
498 |
+
chunk_size = part_size if part_size < 3200 else 3200 # for retrieval chunk 50000
|
499 |
+
|
500 |
+
logger.debug(f"Chunk data into {math.ceil(len(sentences) / chunk_size)} packages of size {chunk_size}")
|
501 |
+
|
502 |
+
input_queue = self.pool['input']
|
503 |
+
last_chunk_id = 0
|
504 |
+
chunk = []
|
505 |
+
|
506 |
+
for sentence in sentences:
|
507 |
+
chunk.append(sentence)
|
508 |
+
if len(chunk) >= chunk_size:
|
509 |
+
input_queue.put([last_chunk_id, chunk, kwargs])
|
510 |
+
last_chunk_id += 1
|
511 |
+
chunk = []
|
512 |
+
|
513 |
+
if len(chunk) > 0:
|
514 |
+
input_queue.put([last_chunk_id, chunk, kwargs])
|
515 |
+
last_chunk_id += 1
|
516 |
+
|
517 |
+
output_queue = self.pool['output']
|
518 |
+
results_list = sorted([output_queue.get() for _ in range(last_chunk_id)], key=lambda x: x[0])
|
519 |
+
embeddings = np.concatenate([result[1] for result in results_list])
|
520 |
+
return embeddings
|
521 |
+
|
522 |
+
@staticmethod
|
523 |
+
def batch_to_device(batch, target_device):
|
524 |
+
"""
|
525 |
+
send a pytorch batch to a device (CPU/GPU)
|
526 |
+
"""
|
527 |
+
for key in batch:
|
528 |
+
if isinstance(batch[key], torch.Tensor):
|
529 |
+
batch[key] = batch[key].to(target_device)
|
530 |
+
return batch
|
531 |
+
|
532 |
+
def _text_length(self, text: Union[List[int], List[List[int]]]):
|
533 |
+
"""
|
534 |
+
Help function to get the length for the input text. Text can be either
|
535 |
+
a list of ints (which means a single text as input), or a tuple of list of ints
|
536 |
+
(representing several text inputs to the model).
|
537 |
+
"""
|
538 |
+
|
539 |
+
if isinstance(text, dict): #{key: value} case
|
540 |
+
return len(next(iter(text.values())))
|
541 |
+
elif not hasattr(text, '__len__'): #Object has no len() method
|
542 |
+
return 1
|
543 |
+
elif len(text) == 0 or isinstance(text[0], int): #Empty string or list of ints
|
544 |
+
return len(text)
|
545 |
+
else:
|
546 |
+
return sum([len(t) for t in text]) #Sum of length of individual strings
|
547 |
+
|
548 |
+
def _tokenize(self, sentences: List[str], is_query: bool):
|
549 |
+
|
550 |
+
batch_dict = self.tokenizer(sentences, max_length=self.max_seq_len - 1, return_attention_mask=False, padding=False, truncation=True)
|
551 |
+
batch_dict['input_ids'] = [input_ids + [self.tokenizer.eos_token_id] for input_ids in batch_dict['input_ids']]
|
552 |
+
batch_dict = self.tokenizer.pad(batch_dict, padding=True, return_attention_mask=True, return_tensors='pt')
|
553 |
+
batch_dict['is_causal'] = False
|
554 |
+
return batch_dict
|
555 |
+
|
556 |
+
|
557 |
+
def _encode(
|
558 |
+
self,
|
559 |
+
sentences: List[str],
|
560 |
+
is_query: bool,
|
561 |
+
convert_to_numpy: bool = True,
|
562 |
+
convert_to_tensor: bool = False,
|
563 |
+
device: str = None,
|
564 |
+
show_progress_bar: bool = True,
|
565 |
+
**kwargs
|
566 |
+
):
|
567 |
+
"""
|
568 |
+
Computes sentence embeddings
|
569 |
+
|
570 |
+
:param sentences: the sentences to embed
|
571 |
+
:param batch_size: the batch size used for the computation
|
572 |
+
:param show_progress_bar: Output a progress bar when encode sentences
|
573 |
+
:param output_value: Default sentence_embedding, to get sentence embeddings. Can be set to token_embeddings to get wordpiece token embeddings. Set to None, to get all output values
|
574 |
+
:param convert_to_numpy: If true, the output is a list of numpy vectors. Else, it is a list of pytorch tensors.
|
575 |
+
:param convert_to_tensor: If true, you get one large tensor as return. Overwrites any setting from convert_to_numpy
|
576 |
+
:param device: Which torch.device to use for the computation
|
577 |
+
:param normalize_embeddings: If set to true, returned vectors will have length 1. In that case, the faster dot-product (util.dot_score) instead of cosine similarity can be used.
|
578 |
+
|
579 |
+
:return:
|
580 |
+
By default, a list of tensors is returned. If convert_to_tensor, a stacked tensor is returned. If convert_to_numpy, a numpy matrix is returned.
|
581 |
+
"""
|
582 |
+
self.model.eval()
|
583 |
+
|
584 |
+
if convert_to_tensor:
|
585 |
+
convert_to_numpy = False
|
586 |
+
|
587 |
+
input_was_string = False
|
588 |
+
if isinstance(sentences, str) or not hasattr(sentences, '__len__'): #Cast an individual sentence to a list with length 1
|
589 |
+
sentences = [sentences]
|
590 |
+
input_was_string = True
|
591 |
+
|
592 |
+
if device is None:
|
593 |
+
device = self._target_device
|
594 |
+
|
595 |
+
self.model.to(device)
|
596 |
+
|
597 |
+
all_embeddings = []
|
598 |
+
length_sorted_idx = np.argsort([-self._text_length(s) for s in sentences])
|
599 |
+
sentences_sorted = [sentences[idx] for idx in length_sorted_idx]
|
600 |
+
|
601 |
+
for start_index in trange(0, len(sentences), self.batch_size, desc="Batches", disable=not show_progress_bar):
|
602 |
+
sentences_batch = sentences_sorted[start_index:start_index + self.batch_size]
|
603 |
+
features = self._tokenize(sentences_batch, is_query)
|
604 |
+
features = self.batch_to_device(features, device)
|
605 |
+
|
606 |
+
with torch.no_grad():
|
607 |
+
embeddings = self.model(**features)
|
608 |
+
|
609 |
+
if self.normalize_embeddings:
|
610 |
+
embeddings = torch.nn.functional.normalize(embeddings, p=2, dim=1)
|
611 |
+
|
612 |
+
# fixes for #522 and #487 to avoid oom problems on gpu with large datasets
|
613 |
+
if convert_to_numpy:
|
614 |
+
embeddings = embeddings.cpu()
|
615 |
+
|
616 |
+
all_embeddings.extend(embeddings)
|
617 |
+
|
618 |
+
all_embeddings = [all_embeddings[idx] for idx in np.argsort(length_sorted_idx)]
|
619 |
+
|
620 |
+
if convert_to_tensor:
|
621 |
+
all_embeddings = torch.stack(all_embeddings)
|
622 |
+
elif convert_to_numpy:
|
623 |
+
#all_embeddings = np.asarray([emb.numpy() for emb in all_embeddings])
|
624 |
+
all_embeddings = np.asarray([emb.to(torch.float).numpy() for emb in all_embeddings])
|
625 |
+
if input_was_string:
|
626 |
+
all_embeddings = all_embeddings[0]
|
627 |
+
|
628 |
+
return all_embeddings
|
629 |
+
|
630 |
+
def encode(
|
631 |
+
self,
|
632 |
+
sentences: List[str],
|
633 |
+
is_query: Optional[bool] = None,
|
634 |
+
convert_to_tensor: bool = False,
|
635 |
+
**kwargs
|
636 |
+
):
|
637 |
+
is_query = self.default_query if is_query is None else is_query
|
638 |
+
if is_query and self.instruction:
|
639 |
+
sentences = [self.instruction + sent for sent in sentences]
|
640 |
+
kwargs.update(is_query=is_query)
|
641 |
+
if self.pool is not None:
|
642 |
+
kwargs.update(show_progress_bar=False)
|
643 |
+
embeddings = self.encode_multi_process(sentences, **kwargs)
|
644 |
+
if convert_to_tensor:
|
645 |
+
embeddings = torch.from_numpy(embeddings)
|
646 |
+
if self.mp_tensor_to_cuda and torch.cuda.is_available():
|
647 |
+
embeddings = embeddings.to(torch.device('cuda')) # default 0-th gpu
|
648 |
+
return embeddings
|
649 |
+
|
650 |
+
return self._encode(sentences, convert_to_tensor=convert_to_tensor, **kwargs)
|
651 |
+
|
652 |
+
def encode_queries(self, queries: List[str], **kwargs):
|
653 |
+
is_query = self.default_query if self.force_default else True
|
654 |
+
return self.encode(queries, is_query=is_query, **kwargs)
|
655 |
+
|
656 |
+
def encode_corpus(self, corpus: List[Dict[str, str]], **kwargs):
|
657 |
+
# borrowed from mteb.abstasks.AbsTaskRetrieval.DRESModel
|
658 |
+
if type(corpus) is dict:
|
659 |
+
sentences = [
|
660 |
+
(corpus["title"][i] + self.sep + corpus["text"][i]).strip()
|
661 |
+
if "title" in corpus
|
662 |
+
else corpus["text"][i].strip()
|
663 |
+
for i in range(len(corpus["text"]))
|
664 |
+
]
|
665 |
+
elif isinstance(corpus[0], dict):
|
666 |
+
sentences = [
|
667 |
+
(doc["title"] + self.sep + doc["text"]).strip() if "title" in doc else doc["text"].strip()
|
668 |
+
for doc in corpus
|
669 |
+
]
|
670 |
+
else:
|
671 |
+
sentences = corpus
|
672 |
+
is_query = self.default_query if self.force_default else False
|
673 |
+
return self.encode(sentences, is_query=is_query, **kwargs)
|
674 |
+
|
675 |
+
def main(args):
|
676 |
+
tokenizer = AutoTokenizer.from_pretrained(args.model, trust_remote_code=True)
|
677 |
+
encoder = Encoder(args.model, args.pooling)
|
678 |
+
model = Wrapper(
|
679 |
+
tokenizer, encoder,
|
680 |
+
batch_size=args.batch_size,
|
681 |
+
max_seq_len=args.max_seq_len,
|
682 |
+
normalize_embeddings=args.norm
|
683 |
+
)
|
684 |
+
|
685 |
+
if args.task == 'mteb':
|
686 |
+
task_names = MTEB_TASK_LIST
|
687 |
+
lang = ['en']
|
688 |
+
elif args.task == 'cmteb':
|
689 |
+
task_names = CMTEB_TASK_LIST
|
690 |
+
lang = ['zh','zh-CN']
|
691 |
+
elif args.task == 'mteb-fr':
|
692 |
+
tas_names = MTEB_FR
|
693 |
+
lang = ['fr']
|
694 |
+
elif args.task == 'mteb-pl':
|
695 |
+
lang = ['pl']
|
696 |
+
else:
|
697 |
+
task_names = [args.task]
|
698 |
+
lang = ['en','zh','zh-CN','pl','fr']
|
699 |
+
for task in task_names:
|
700 |
+
evaluation = MTEB(tasks=[task], task_langs=lang)
|
701 |
+
task_cls = evaluation.tasks[0]
|
702 |
+
task_name: str = task_cls.description['name']
|
703 |
+
task_type: str = task_cls.description['type']
|
704 |
+
instruction = get_task_def_by_task_name_and_type(task_name, task_type)
|
705 |
+
model.instruction = get_detailed_instruct(instruction)
|
706 |
+
if task == 'MSMARCO':
|
707 |
+
eval_splits = ["dev"]
|
708 |
+
elif task in CMTEB_TASK_LIST:
|
709 |
+
eval_splits = task_cls.description['eval_splits']
|
710 |
+
else:
|
711 |
+
eval_splits = ["test"]
|
712 |
+
|
713 |
+
evaluation.run(model, output_folder=args.output_dir, eval_splits=eval_splits)
|
714 |
+
print('\n')
|
715 |
+
|
716 |
+
|
717 |
+
if __name__ == "__main__":
|
718 |
+
_PARSER = argparse.ArgumentParser()
|
719 |
+
_PARSER.add_argument(
|
720 |
+
"-m", "--model", type=str, default=None
|
721 |
+
)
|
722 |
+
_PARSER.add_argument("--pooling", type=str, default='last')
|
723 |
+
_PARSER.add_argument("--output_dir", type=str, default=None)
|
724 |
+
_PARSER.add_argument("--default_type", type=str, default='query')
|
725 |
+
_PARSER.add_argument("--max_seq_len", type=int, default=512)
|
726 |
+
_PARSER.add_argument("-b", "--batch_size", type=int, default=32)
|
727 |
+
_PARSER.add_argument(
|
728 |
+
"-t", "--task", type=str, default=None # None for running default tasks
|
729 |
+
)
|
730 |
+
_PARSER.add_argument("--norm", action="store_true")
|
731 |
+
_ARGS = _PARSER.parse_args()
|
732 |
+
main(_ARGS)
|
sentence_bert_config.json
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"max_seq_length": 32768,
|
3 |
+
"do_lower_case": false
|
4 |
+
}
|
special_tokens_map.json
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"additional_special_tokens": [
|
3 |
+
"<|im_start|>",
|
4 |
+
"<|im_end|>"
|
5 |
+
],
|
6 |
+
"eos_token": {
|
7 |
+
"content": "<|endoftext|>",
|
8 |
+
"lstrip": false,
|
9 |
+
"normalized": false,
|
10 |
+
"rstrip": false,
|
11 |
+
"single_word": false
|
12 |
+
},
|
13 |
+
"pad_token": {
|
14 |
+
"content": "<|endoftext|>",
|
15 |
+
"lstrip": false,
|
16 |
+
"normalized": false,
|
17 |
+
"rstrip": false,
|
18 |
+
"single_word": false
|
19 |
+
}
|
20 |
+
}
|
tokenization_qwen.py
ADDED
@@ -0,0 +1,267 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from typing import List, Optional
|
3 |
+
from transformers.models.qwen2.tokenization_qwen2 import Qwen2Tokenizer as OriginalQwen2Tokenizer
|
4 |
+
from transformers.models.qwen2.tokenization_qwen2_fast import Qwen2TokenizerFast as OriginalQwen2TokenizerFast
|
5 |
+
from tokenizers import processors
|
6 |
+
|
7 |
+
VOCAB_FILES_NAMES = {
|
8 |
+
"vocab_file": "vocab.json",
|
9 |
+
"merges_file": "merges.txt",
|
10 |
+
"tokenizer_file": "tokenizer.json",
|
11 |
+
}
|
12 |
+
|
13 |
+
class Qwen2Tokenizer(OriginalQwen2Tokenizer):
|
14 |
+
"""
|
15 |
+
Construct a Qwen2 tokenizer. Based on byte-level Byte-Pair-Encoding.
|
16 |
+
|
17 |
+
Same with GPT2Tokenizer, this tokenizer has been trained to treat spaces like parts of the tokens so a word will
|
18 |
+
be encoded differently whether it is at the beginning of the sentence (without space) or not:
|
19 |
+
|
20 |
+
```python
|
21 |
+
>>> from transformers import Qwen2Tokenizer
|
22 |
+
|
23 |
+
>>> tokenizer = Qwen2Tokenizer.from_pretrained("Qwen/Qwen-tokenizer")
|
24 |
+
>>> tokenizer("Hello world")["input_ids"]
|
25 |
+
[9707, 1879]
|
26 |
+
|
27 |
+
>>> tokenizer(" Hello world")["input_ids"]
|
28 |
+
[21927, 1879]
|
29 |
+
```
|
30 |
+
This is expected.
|
31 |
+
|
32 |
+
You should not use GPT2Tokenizer instead, because of the different pretokenization rules.
|
33 |
+
|
34 |
+
This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. Users should refer to
|
35 |
+
this superclass for more information regarding those methods.
|
36 |
+
|
37 |
+
Args:
|
38 |
+
vocab_file (`str`):
|
39 |
+
Path to the vocabulary file.
|
40 |
+
merges_file (`str`):
|
41 |
+
Path to the merges file.
|
42 |
+
errors (`str`, *optional*, defaults to `"replace"`):
|
43 |
+
Paradigm to follow when decoding bytes to UTF-8. See
|
44 |
+
[bytes.decode](https://docs.python.org/3/library/stdtypes.html#bytes.decode) for more information.
|
45 |
+
unk_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
|
46 |
+
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
47 |
+
token instead.
|
48 |
+
bos_token (`str`, *optional*):
|
49 |
+
The beginning of sequence token. Not applicable for this tokenizer.
|
50 |
+
eos_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
|
51 |
+
The end of sequence token.
|
52 |
+
pad_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
|
53 |
+
The token used for padding, for example when batching sequences of different lengths.
|
54 |
+
clean_up_tokenization_spaces (`bool`, *optional*, defaults to `False`):
|
55 |
+
Whether or not the model should cleanup the spaces that were added when splitting the input text during the
|
56 |
+
tokenization process. Not applicable to this tokenizer, since tokenization does not add spaces.
|
57 |
+
split_special_tokens (`bool`, *optional*, defaults to `False`):
|
58 |
+
Whether or not the special tokens should be split during the tokenization process. The default behavior is
|
59 |
+
to not split special tokens. This means that if `<|endoftext|>` is the `eos_token`, then `tokenizer.tokenize("<|endoftext|>") =
|
60 |
+
['<|endoftext|>`]. Otherwise, if `split_special_tokens=True`, then `tokenizer.tokenize("<|endoftext|>")` will be give `['<',
|
61 |
+
'|', 'endo', 'ft', 'ext', '|', '>']`. This argument is only supported for `slow` tokenizers for the moment.
|
62 |
+
add_eos_token (`bool`, *optional*, defaults to `False`):
|
63 |
+
Whether or not to add an `eos_token` at the end of sequences.
|
64 |
+
"""
|
65 |
+
|
66 |
+
def __init__(
|
67 |
+
self,
|
68 |
+
vocab_file,
|
69 |
+
merges_file,
|
70 |
+
errors="replace",
|
71 |
+
unk_token="<|endoftext|>",
|
72 |
+
bos_token=None,
|
73 |
+
eos_token="<|endoftext|>",
|
74 |
+
pad_token="<|endoftext|>",
|
75 |
+
clean_up_tokenization_spaces=False,
|
76 |
+
split_special_tokens=False,
|
77 |
+
add_eos_token=False,
|
78 |
+
**kwargs,
|
79 |
+
):
|
80 |
+
# The add_eos_token code was inspired by the LlamaTokenizer
|
81 |
+
self.add_eos_token = add_eos_token
|
82 |
+
|
83 |
+
super().__init__(
|
84 |
+
vocab_file=vocab_file,
|
85 |
+
merges_file=merges_file,
|
86 |
+
errors=errors,
|
87 |
+
unk_token=unk_token,
|
88 |
+
bos_token=bos_token,
|
89 |
+
eos_token=eos_token,
|
90 |
+
pad_token=pad_token,
|
91 |
+
clean_up_tokenization_spaces=clean_up_tokenization_spaces,
|
92 |
+
split_special_tokens=split_special_tokens,
|
93 |
+
add_eos_token=add_eos_token,
|
94 |
+
**kwargs,
|
95 |
+
)
|
96 |
+
|
97 |
+
def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
|
98 |
+
eos_token_id = [self.eos_token_id] if self.add_eos_token else []
|
99 |
+
|
100 |
+
output = token_ids_0 + eos_token_id
|
101 |
+
|
102 |
+
if token_ids_1 is not None:
|
103 |
+
output = output + token_ids_1 + eos_token_id
|
104 |
+
|
105 |
+
return output
|
106 |
+
|
107 |
+
def get_special_tokens_mask(
|
108 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
109 |
+
) -> List[int]:
|
110 |
+
"""
|
111 |
+
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
|
112 |
+
special tokens using the tokenizer `prepare_for_model` method.
|
113 |
+
|
114 |
+
Args:
|
115 |
+
token_ids_0 (`List[int]`):
|
116 |
+
List of IDs.
|
117 |
+
token_ids_1 (`List[int]`, *optional*):
|
118 |
+
Optional second list of IDs for sequence pairs.
|
119 |
+
already_has_special_tokens (`bool`, *optional*, defaults to `False`):
|
120 |
+
Whether or not the token list is already formatted with special tokens for the model.
|
121 |
+
|
122 |
+
Returns:
|
123 |
+
`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
124 |
+
"""
|
125 |
+
if already_has_special_tokens:
|
126 |
+
return super().get_special_tokens_mask(
|
127 |
+
token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True
|
128 |
+
)
|
129 |
+
|
130 |
+
eos_token_id = [1] if self.add_eos_token else []
|
131 |
+
|
132 |
+
if token_ids_1 is None:
|
133 |
+
return ([0] * len(token_ids_0)) + eos_token_id
|
134 |
+
return (
|
135 |
+
([0] * len(token_ids_0))
|
136 |
+
+ eos_token_id
|
137 |
+
+ ([0] * len(token_ids_1))
|
138 |
+
+ eos_token_id
|
139 |
+
)
|
140 |
+
|
141 |
+
def create_token_type_ids_from_sequences(
|
142 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
143 |
+
) -> List[int]:
|
144 |
+
"""
|
145 |
+
Creates a mask from the two sequences passed to be used in a sequence-pair classification task. An ALBERT
|
146 |
+
sequence pair mask has the following format:
|
147 |
+
|
148 |
+
```
|
149 |
+
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
|
150 |
+
| first sequence | second sequence |
|
151 |
+
```
|
152 |
+
|
153 |
+
if token_ids_1 is None, only returns the first portion of the mask (0s).
|
154 |
+
|
155 |
+
Args:
|
156 |
+
token_ids_0 (`List[int]`):
|
157 |
+
List of ids.
|
158 |
+
token_ids_1 (`List[int]`, *optional*):
|
159 |
+
Optional second list of IDs for sequence pairs.
|
160 |
+
|
161 |
+
Returns:
|
162 |
+
`List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s).
|
163 |
+
"""
|
164 |
+
eos_token_id = [self.eos_token_id] if self.add_eos_token else []
|
165 |
+
|
166 |
+
output = [0] * len(token_ids_0 + eos_token_id)
|
167 |
+
|
168 |
+
if token_ids_1 is not None:
|
169 |
+
output += [1] * len(token_ids_1 + eos_token_id)
|
170 |
+
|
171 |
+
return output
|
172 |
+
|
173 |
+
class Qwen2TokenizerFast(OriginalQwen2TokenizerFast):
|
174 |
+
"""
|
175 |
+
Construct a "fast" Qwen2 tokenizer (backed by HuggingFace's *tokenizers* library). Based on byte-level
|
176 |
+
Byte-Pair-Encoding.
|
177 |
+
|
178 |
+
Same with GPT2Tokenizer, this tokenizer has been trained to treat spaces like parts of the tokens so a word will
|
179 |
+
be encoded differently whether it is at the beginning of the sentence (without space) or not:
|
180 |
+
|
181 |
+
```python
|
182 |
+
>>> from transformers import Qwen2TokenizerFast
|
183 |
+
|
184 |
+
>>> tokenizer = Qwen2TokenizerFast.from_pretrained("Qwen/Qwen-tokenizer")
|
185 |
+
>>> tokenizer("Hello world")["input_ids"]
|
186 |
+
[9707, 1879]
|
187 |
+
|
188 |
+
>>> tokenizer(" Hello world")["input_ids"]
|
189 |
+
[21927, 1879]
|
190 |
+
```
|
191 |
+
This is expected.
|
192 |
+
|
193 |
+
This tokenizer inherits from [`PreTrainedTokenizerFast`] which contains most of the main methods. Users should
|
194 |
+
refer to this superclass for more information regarding those methods.
|
195 |
+
|
196 |
+
Args:
|
197 |
+
vocab_file (`str`, *optional*):
|
198 |
+
Path to the vocabulary file.
|
199 |
+
merges_file (`str`, *optional*):
|
200 |
+
Path to the merges file.
|
201 |
+
tokenizer_file (`str`, *optional*):
|
202 |
+
Path to [tokenizers](https://github.com/huggingface/tokenizers) file (generally has a .json extension) that
|
203 |
+
contains everything needed to load the tokenizer.
|
204 |
+
unk_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
|
205 |
+
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this
|
206 |
+
token instead. Not applicable to this tokenizer.
|
207 |
+
bos_token (`str`, *optional*):
|
208 |
+
The beginning of sequence token. Not applicable for this tokenizer.
|
209 |
+
eos_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
|
210 |
+
The end of sequence token.
|
211 |
+
pad_token (`str`, *optional*, defaults to `"<|endoftext|>"`):
|
212 |
+
The token used for padding, for example when batching sequences of different lengths.
|
213 |
+
add_eos_token (`bool`, *optional*, defaults to `False`):
|
214 |
+
Whether or not to add an `eos_token` at the end of sequences.
|
215 |
+
"""
|
216 |
+
|
217 |
+
slow_tokenizer_class = Qwen2Tokenizer
|
218 |
+
padding_side = "left"
|
219 |
+
|
220 |
+
def __init__(
|
221 |
+
self,
|
222 |
+
vocab_file=None,
|
223 |
+
merges_file=None,
|
224 |
+
tokenizer_file=None,
|
225 |
+
unk_token="<|endoftext|>",
|
226 |
+
bos_token=None,
|
227 |
+
eos_token="<|endoftext|>",
|
228 |
+
pad_token="<|endoftext|>",
|
229 |
+
add_eos_token=False,
|
230 |
+
**kwargs,
|
231 |
+
):
|
232 |
+
super().__init__(
|
233 |
+
vocab_file=vocab_file,
|
234 |
+
merges_file=merges_file,
|
235 |
+
tokenizer_file=tokenizer_file,
|
236 |
+
unk_token=unk_token,
|
237 |
+
bos_token=bos_token,
|
238 |
+
eos_token=eos_token,
|
239 |
+
pad_token=pad_token,
|
240 |
+
**kwargs,
|
241 |
+
)
|
242 |
+
|
243 |
+
self._add_eos_token = add_eos_token
|
244 |
+
self.update_post_processor()
|
245 |
+
|
246 |
+
def update_post_processor(self):
|
247 |
+
"""
|
248 |
+
Updates the underlying post processor with the current `eos_token`.
|
249 |
+
"""
|
250 |
+
eos = self.eos_token
|
251 |
+
eos_token_id = self.eos_token_id
|
252 |
+
if eos is None and self.add_eos_token:
|
253 |
+
raise ValueError("add_eos_token = True but eos_token = None")
|
254 |
+
|
255 |
+
single = f"$A:0{(' '+eos+':0') if self.add_eos_token else ''}"
|
256 |
+
pair = f"{single} $B:1{(' '+eos+':1') if self.add_eos_token else ''}"
|
257 |
+
|
258 |
+
special_tokens = []
|
259 |
+
if self.add_eos_token:
|
260 |
+
special_tokens.append((eos, eos_token_id))
|
261 |
+
self._tokenizer.post_processor = processors.TemplateProcessing(
|
262 |
+
single=single, pair=pair, special_tokens=special_tokens
|
263 |
+
)
|
264 |
+
|
265 |
+
@property
|
266 |
+
def add_eos_token(self):
|
267 |
+
return self._add_eos_token
|
tokenizer.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
tokenizer_config.json
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_eos_token": true,
|
3 |
+
"add_prefix_space": false,
|
4 |
+
"added_tokens_decoder": {
|
5 |
+
"151643": {
|
6 |
+
"content": "<|endoftext|>",
|
7 |
+
"lstrip": false,
|
8 |
+
"normalized": false,
|
9 |
+
"rstrip": false,
|
10 |
+
"single_word": false,
|
11 |
+
"special": true
|
12 |
+
},
|
13 |
+
"151644": {
|
14 |
+
"content": "<|im_start|>",
|
15 |
+
"lstrip": false,
|
16 |
+
"normalized": false,
|
17 |
+
"rstrip": false,
|
18 |
+
"single_word": false,
|
19 |
+
"special": true
|
20 |
+
},
|
21 |
+
"151645": {
|
22 |
+
"content": "<|im_end|>",
|
23 |
+
"lstrip": false,
|
24 |
+
"normalized": false,
|
25 |
+
"rstrip": false,
|
26 |
+
"single_word": false,
|
27 |
+
"special": true
|
28 |
+
}
|
29 |
+
},
|
30 |
+
"additional_special_tokens": [
|
31 |
+
"<|im_start|>",
|
32 |
+
"<|im_end|>"
|
33 |
+
],
|
34 |
+
"auto_map": {
|
35 |
+
"AutoTokenizer": ["tokenization_qwen.Qwen2Tokenizer", "tokenization_qwen.Qwen2TokenizerFast"]
|
36 |
+
},
|
37 |
+
"bos_token": null,
|
38 |
+
"chat_template": "{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content'] + '<|im_end|>' + '\n'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}",
|
39 |
+
"clean_up_tokenization_spaces": false,
|
40 |
+
"eos_token": "<|endoftext|>",
|
41 |
+
"errors": "replace",
|
42 |
+
"model_max_length": 32768,
|
43 |
+
"pad_token": "<|endoftext|>",
|
44 |
+
"split_special_tokens": false,
|
45 |
+
"tokenizer_class": "Qwen2Tokenizer",
|
46 |
+
"unk_token": null
|
47 |
+
}
|
vocab.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|