yangapku commited on
Commit
abcb6d6
0 Parent(s):

update model

Browse files
.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
config.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "QWenLMHeadModel"
4
+ ],
5
+ "auto_map": {
6
+ "AutoConfig": "configuration_qwen.QWenConfig",
7
+ "AutoModelForCausalLM": "modeling_qwen.QWenLMHeadModel"
8
+ },
9
+ "attn_dropout_prob": 0.0,
10
+ "bf16": false,
11
+ "emb_dropout_prob": 0.0,
12
+ "fp16": false,
13
+ "fp32": false,
14
+ "hidden_size": 4096,
15
+ "intermediate_size": 22016,
16
+ "initializer_range": 0.02,
17
+ "kv_channels": 128,
18
+ "layer_norm_epsilon": 1e-06,
19
+ "max_position_embeddings": 8192,
20
+ "model_type": "qwen",
21
+ "no_bias": true,
22
+ "num_attention_heads": 32,
23
+ "num_hidden_layers": 32,
24
+ "onnx_safe": null,
25
+ "rotary_emb_base": 10000,
26
+ "rotary_pct": 1.0,
27
+ "scale_attn_weights": true,
28
+ "seq_length": 8192,
29
+ "tie_word_embeddings": false,
30
+ "tokenizer_class": "QWenTokenizer",
31
+ "transformers_version": "4.32.0",
32
+ "use_cache": true,
33
+ "use_dynamic_ntk": true,
34
+ "use_flash_attn": "auto",
35
+ "use_logn_attn": true,
36
+ "vocab_size": 151936
37
+ }
configuration_qwen.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) Alibaba Cloud.
2
+ #
3
+ # This source code is licensed under the license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+
6
+ from transformers import PretrainedConfig
7
+
8
+
9
+ class QWenConfig(PretrainedConfig):
10
+ model_type = "qwen"
11
+ keys_to_ignore_at_inference = ["past_key_values"]
12
+
13
+ def __init__(
14
+ self,
15
+ vocab_size=151936,
16
+ hidden_size=4096,
17
+ num_hidden_layers=32,
18
+ num_attention_heads=32,
19
+ emb_dropout_prob=0.0,
20
+ attn_dropout_prob=0.0,
21
+ layer_norm_epsilon=1e-6,
22
+ initializer_range=0.02,
23
+ max_position_embeddings=8192,
24
+ scale_attn_weights=True,
25
+ use_cache=True,
26
+ bf16=False,
27
+ fp16=False,
28
+ fp32=False,
29
+ kv_channels=128,
30
+ rotary_pct=1.0,
31
+ rotary_emb_base=10000,
32
+ use_dynamic_ntk=True,
33
+ use_logn_attn=True,
34
+ use_flash_attn="auto",
35
+ intermediate_size=22016,
36
+ no_bias=True,
37
+ tie_word_embeddings=False,
38
+ **kwargs,
39
+ ):
40
+ self.vocab_size = vocab_size
41
+ self.hidden_size = hidden_size
42
+ self.intermediate_size = intermediate_size
43
+ self.num_hidden_layers = num_hidden_layers
44
+ self.num_attention_heads = num_attention_heads
45
+ self.emb_dropout_prob = emb_dropout_prob
46
+ self.attn_dropout_prob = attn_dropout_prob
47
+ self.layer_norm_epsilon = layer_norm_epsilon
48
+ self.initializer_range = initializer_range
49
+ self.scale_attn_weights = scale_attn_weights
50
+ self.use_cache = use_cache
51
+ self.max_position_embeddings = max_position_embeddings
52
+ self.bf16 = bf16
53
+ self.fp16 = fp16
54
+ self.fp32 = fp32
55
+ self.kv_channels = kv_channels
56
+ self.rotary_pct = rotary_pct
57
+ self.rotary_emb_base = rotary_emb_base
58
+ self.use_dynamic_ntk = use_dynamic_ntk
59
+ self.use_logn_attn = use_logn_attn
60
+ self.use_flash_attn = use_flash_attn
61
+ self.no_bias = no_bias
62
+ super().__init__(
63
+ tie_word_embeddings=tie_word_embeddings,
64
+ **kwargs
65
+ )
generation_config.json ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "chat_format": "raw",
3
+ "eos_token_id": 151643,
4
+ "pad_token_id": 151643,
5
+ "stop_words_ids": [[151643]],
6
+ "max_new_tokens": 512,
7
+ "do_sample": true,
8
+ "top_k": 0,
9
+ "top_p": 0.8,
10
+ "transformers_version": "4.31.0"
11
+ }
12
+
model-00001-of-00008.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9dfd6266bcf80de9c3e5cd4e60300d839d03e459e48975b08d3e3b286044a306
3
+ size 1964066488
model-00002-of-00008.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:3dedac66034371aa3b284a7886e9ce0fde9245ebac60f507f089b33ef82a2912
3
+ size 2023960808
model-00003-of-00008.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:81b25b14a58b62300d11b16c66933a8b631400bf846b27a2a5c5344629cd26e8
3
+ size 2023960816
model-00004-of-00008.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:59a22cb822f9e6d0a6a8415a7bab7b8448ce9672fc71f09266db264afc26ce48
3
+ size 2023960848
model-00005-of-00008.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e61126f7ad8c520112c49808a73d59bee18c6da7693d9a963a4867f389c91e4a
3
+ size 2023960848
model-00006-of-00008.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0ced56cc3265fe03ee73658f845dc23c713389b5d68087e918d24d0e2dea624f
3
+ size 2023960848
model-00007-of-00008.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b0999d47ea087bf79a075ed10889aa3497caff2356200204b032fba208109517
3
+ size 2023960848
model-00008-of-00008.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7bc05473a78ade06d526cc206e4a01722563abe99099367cdcbb9b3bf670a5de
3
+ size 1334845784
model.safetensors.index.json ADDED
@@ -0,0 +1,266 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_size": 15442649088
4
+ },
5
+ "weight_map": {
6
+ "lm_head.weight": "model-00008-of-00008.safetensors",
7
+ "transformer.h.0.attn.c_attn.bias": "model-00001-of-00008.safetensors",
8
+ "transformer.h.0.attn.c_attn.weight": "model-00001-of-00008.safetensors",
9
+ "transformer.h.0.attn.c_proj.weight": "model-00001-of-00008.safetensors",
10
+ "transformer.h.0.ln_1.weight": "model-00001-of-00008.safetensors",
11
+ "transformer.h.0.ln_2.weight": "model-00001-of-00008.safetensors",
12
+ "transformer.h.0.mlp.c_proj.weight": "model-00001-of-00008.safetensors",
13
+ "transformer.h.0.mlp.w1.weight": "model-00001-of-00008.safetensors",
14
+ "transformer.h.0.mlp.w2.weight": "model-00001-of-00008.safetensors",
15
+ "transformer.h.1.attn.c_attn.bias": "model-00001-of-00008.safetensors",
16
+ "transformer.h.1.attn.c_attn.weight": "model-00001-of-00008.safetensors",
17
+ "transformer.h.1.attn.c_proj.weight": "model-00001-of-00008.safetensors",
18
+ "transformer.h.1.ln_1.weight": "model-00001-of-00008.safetensors",
19
+ "transformer.h.1.ln_2.weight": "model-00001-of-00008.safetensors",
20
+ "transformer.h.1.mlp.c_proj.weight": "model-00002-of-00008.safetensors",
21
+ "transformer.h.1.mlp.w1.weight": "model-00001-of-00008.safetensors",
22
+ "transformer.h.1.mlp.w2.weight": "model-00001-of-00008.safetensors",
23
+ "transformer.h.10.attn.c_attn.bias": "model-00003-of-00008.safetensors",
24
+ "transformer.h.10.attn.c_attn.weight": "model-00003-of-00008.safetensors",
25
+ "transformer.h.10.attn.c_proj.weight": "model-00003-of-00008.safetensors",
26
+ "transformer.h.10.ln_1.weight": "model-00003-of-00008.safetensors",
27
+ "transformer.h.10.ln_2.weight": "model-00003-of-00008.safetensors",
28
+ "transformer.h.10.mlp.c_proj.weight": "model-00003-of-00008.safetensors",
29
+ "transformer.h.10.mlp.w1.weight": "model-00003-of-00008.safetensors",
30
+ "transformer.h.10.mlp.w2.weight": "model-00003-of-00008.safetensors",
31
+ "transformer.h.11.attn.c_attn.bias": "model-00003-of-00008.safetensors",
32
+ "transformer.h.11.attn.c_attn.weight": "model-00003-of-00008.safetensors",
33
+ "transformer.h.11.attn.c_proj.weight": "model-00003-of-00008.safetensors",
34
+ "transformer.h.11.ln_1.weight": "model-00003-of-00008.safetensors",
35
+ "transformer.h.11.ln_2.weight": "model-00003-of-00008.safetensors",
36
+ "transformer.h.11.mlp.c_proj.weight": "model-00004-of-00008.safetensors",
37
+ "transformer.h.11.mlp.w1.weight": "model-00003-of-00008.safetensors",
38
+ "transformer.h.11.mlp.w2.weight": "model-00003-of-00008.safetensors",
39
+ "transformer.h.12.attn.c_attn.bias": "model-00004-of-00008.safetensors",
40
+ "transformer.h.12.attn.c_attn.weight": "model-00004-of-00008.safetensors",
41
+ "transformer.h.12.attn.c_proj.weight": "model-00004-of-00008.safetensors",
42
+ "transformer.h.12.ln_1.weight": "model-00004-of-00008.safetensors",
43
+ "transformer.h.12.ln_2.weight": "model-00004-of-00008.safetensors",
44
+ "transformer.h.12.mlp.c_proj.weight": "model-00004-of-00008.safetensors",
45
+ "transformer.h.12.mlp.w1.weight": "model-00004-of-00008.safetensors",
46
+ "transformer.h.12.mlp.w2.weight": "model-00004-of-00008.safetensors",
47
+ "transformer.h.13.attn.c_attn.bias": "model-00004-of-00008.safetensors",
48
+ "transformer.h.13.attn.c_attn.weight": "model-00004-of-00008.safetensors",
49
+ "transformer.h.13.attn.c_proj.weight": "model-00004-of-00008.safetensors",
50
+ "transformer.h.13.ln_1.weight": "model-00004-of-00008.safetensors",
51
+ "transformer.h.13.ln_2.weight": "model-00004-of-00008.safetensors",
52
+ "transformer.h.13.mlp.c_proj.weight": "model-00004-of-00008.safetensors",
53
+ "transformer.h.13.mlp.w1.weight": "model-00004-of-00008.safetensors",
54
+ "transformer.h.13.mlp.w2.weight": "model-00004-of-00008.safetensors",
55
+ "transformer.h.14.attn.c_attn.bias": "model-00004-of-00008.safetensors",
56
+ "transformer.h.14.attn.c_attn.weight": "model-00004-of-00008.safetensors",
57
+ "transformer.h.14.attn.c_proj.weight": "model-00004-of-00008.safetensors",
58
+ "transformer.h.14.ln_1.weight": "model-00004-of-00008.safetensors",
59
+ "transformer.h.14.ln_2.weight": "model-00004-of-00008.safetensors",
60
+ "transformer.h.14.mlp.c_proj.weight": "model-00004-of-00008.safetensors",
61
+ "transformer.h.14.mlp.w1.weight": "model-00004-of-00008.safetensors",
62
+ "transformer.h.14.mlp.w2.weight": "model-00004-of-00008.safetensors",
63
+ "transformer.h.15.attn.c_attn.bias": "model-00004-of-00008.safetensors",
64
+ "transformer.h.15.attn.c_attn.weight": "model-00004-of-00008.safetensors",
65
+ "transformer.h.15.attn.c_proj.weight": "model-00004-of-00008.safetensors",
66
+ "transformer.h.15.ln_1.weight": "model-00004-of-00008.safetensors",
67
+ "transformer.h.15.ln_2.weight": "model-00004-of-00008.safetensors",
68
+ "transformer.h.15.mlp.c_proj.weight": "model-00004-of-00008.safetensors",
69
+ "transformer.h.15.mlp.w1.weight": "model-00004-of-00008.safetensors",
70
+ "transformer.h.15.mlp.w2.weight": "model-00004-of-00008.safetensors",
71
+ "transformer.h.16.attn.c_attn.bias": "model-00004-of-00008.safetensors",
72
+ "transformer.h.16.attn.c_attn.weight": "model-00004-of-00008.safetensors",
73
+ "transformer.h.16.attn.c_proj.weight": "model-00004-of-00008.safetensors",
74
+ "transformer.h.16.ln_1.weight": "model-00004-of-00008.safetensors",
75
+ "transformer.h.16.ln_2.weight": "model-00004-of-00008.safetensors",
76
+ "transformer.h.16.mlp.c_proj.weight": "model-00005-of-00008.safetensors",
77
+ "transformer.h.16.mlp.w1.weight": "model-00004-of-00008.safetensors",
78
+ "transformer.h.16.mlp.w2.weight": "model-00004-of-00008.safetensors",
79
+ "transformer.h.17.attn.c_attn.bias": "model-00005-of-00008.safetensors",
80
+ "transformer.h.17.attn.c_attn.weight": "model-00005-of-00008.safetensors",
81
+ "transformer.h.17.attn.c_proj.weight": "model-00005-of-00008.safetensors",
82
+ "transformer.h.17.ln_1.weight": "model-00005-of-00008.safetensors",
83
+ "transformer.h.17.ln_2.weight": "model-00005-of-00008.safetensors",
84
+ "transformer.h.17.mlp.c_proj.weight": "model-00005-of-00008.safetensors",
85
+ "transformer.h.17.mlp.w1.weight": "model-00005-of-00008.safetensors",
86
+ "transformer.h.17.mlp.w2.weight": "model-00005-of-00008.safetensors",
87
+ "transformer.h.18.attn.c_attn.bias": "model-00005-of-00008.safetensors",
88
+ "transformer.h.18.attn.c_attn.weight": "model-00005-of-00008.safetensors",
89
+ "transformer.h.18.attn.c_proj.weight": "model-00005-of-00008.safetensors",
90
+ "transformer.h.18.ln_1.weight": "model-00005-of-00008.safetensors",
91
+ "transformer.h.18.ln_2.weight": "model-00005-of-00008.safetensors",
92
+ "transformer.h.18.mlp.c_proj.weight": "model-00005-of-00008.safetensors",
93
+ "transformer.h.18.mlp.w1.weight": "model-00005-of-00008.safetensors",
94
+ "transformer.h.18.mlp.w2.weight": "model-00005-of-00008.safetensors",
95
+ "transformer.h.19.attn.c_attn.bias": "model-00005-of-00008.safetensors",
96
+ "transformer.h.19.attn.c_attn.weight": "model-00005-of-00008.safetensors",
97
+ "transformer.h.19.attn.c_proj.weight": "model-00005-of-00008.safetensors",
98
+ "transformer.h.19.ln_1.weight": "model-00005-of-00008.safetensors",
99
+ "transformer.h.19.ln_2.weight": "model-00005-of-00008.safetensors",
100
+ "transformer.h.19.mlp.c_proj.weight": "model-00005-of-00008.safetensors",
101
+ "transformer.h.19.mlp.w1.weight": "model-00005-of-00008.safetensors",
102
+ "transformer.h.19.mlp.w2.weight": "model-00005-of-00008.safetensors",
103
+ "transformer.h.2.attn.c_attn.bias": "model-00002-of-00008.safetensors",
104
+ "transformer.h.2.attn.c_attn.weight": "model-00002-of-00008.safetensors",
105
+ "transformer.h.2.attn.c_proj.weight": "model-00002-of-00008.safetensors",
106
+ "transformer.h.2.ln_1.weight": "model-00002-of-00008.safetensors",
107
+ "transformer.h.2.ln_2.weight": "model-00002-of-00008.safetensors",
108
+ "transformer.h.2.mlp.c_proj.weight": "model-00002-of-00008.safetensors",
109
+ "transformer.h.2.mlp.w1.weight": "model-00002-of-00008.safetensors",
110
+ "transformer.h.2.mlp.w2.weight": "model-00002-of-00008.safetensors",
111
+ "transformer.h.20.attn.c_attn.bias": "model-00005-of-00008.safetensors",
112
+ "transformer.h.20.attn.c_attn.weight": "model-00005-of-00008.safetensors",
113
+ "transformer.h.20.attn.c_proj.weight": "model-00005-of-00008.safetensors",
114
+ "transformer.h.20.ln_1.weight": "model-00005-of-00008.safetensors",
115
+ "transformer.h.20.ln_2.weight": "model-00005-of-00008.safetensors",
116
+ "transformer.h.20.mlp.c_proj.weight": "model-00005-of-00008.safetensors",
117
+ "transformer.h.20.mlp.w1.weight": "model-00005-of-00008.safetensors",
118
+ "transformer.h.20.mlp.w2.weight": "model-00005-of-00008.safetensors",
119
+ "transformer.h.21.attn.c_attn.bias": "model-00005-of-00008.safetensors",
120
+ "transformer.h.21.attn.c_attn.weight": "model-00005-of-00008.safetensors",
121
+ "transformer.h.21.attn.c_proj.weight": "model-00005-of-00008.safetensors",
122
+ "transformer.h.21.ln_1.weight": "model-00005-of-00008.safetensors",
123
+ "transformer.h.21.ln_2.weight": "model-00005-of-00008.safetensors",
124
+ "transformer.h.21.mlp.c_proj.weight": "model-00006-of-00008.safetensors",
125
+ "transformer.h.21.mlp.w1.weight": "model-00005-of-00008.safetensors",
126
+ "transformer.h.21.mlp.w2.weight": "model-00005-of-00008.safetensors",
127
+ "transformer.h.22.attn.c_attn.bias": "model-00006-of-00008.safetensors",
128
+ "transformer.h.22.attn.c_attn.weight": "model-00006-of-00008.safetensors",
129
+ "transformer.h.22.attn.c_proj.weight": "model-00006-of-00008.safetensors",
130
+ "transformer.h.22.ln_1.weight": "model-00006-of-00008.safetensors",
131
+ "transformer.h.22.ln_2.weight": "model-00006-of-00008.safetensors",
132
+ "transformer.h.22.mlp.c_proj.weight": "model-00006-of-00008.safetensors",
133
+ "transformer.h.22.mlp.w1.weight": "model-00006-of-00008.safetensors",
134
+ "transformer.h.22.mlp.w2.weight": "model-00006-of-00008.safetensors",
135
+ "transformer.h.23.attn.c_attn.bias": "model-00006-of-00008.safetensors",
136
+ "transformer.h.23.attn.c_attn.weight": "model-00006-of-00008.safetensors",
137
+ "transformer.h.23.attn.c_proj.weight": "model-00006-of-00008.safetensors",
138
+ "transformer.h.23.ln_1.weight": "model-00006-of-00008.safetensors",
139
+ "transformer.h.23.ln_2.weight": "model-00006-of-00008.safetensors",
140
+ "transformer.h.23.mlp.c_proj.weight": "model-00006-of-00008.safetensors",
141
+ "transformer.h.23.mlp.w1.weight": "model-00006-of-00008.safetensors",
142
+ "transformer.h.23.mlp.w2.weight": "model-00006-of-00008.safetensors",
143
+ "transformer.h.24.attn.c_attn.bias": "model-00006-of-00008.safetensors",
144
+ "transformer.h.24.attn.c_attn.weight": "model-00006-of-00008.safetensors",
145
+ "transformer.h.24.attn.c_proj.weight": "model-00006-of-00008.safetensors",
146
+ "transformer.h.24.ln_1.weight": "model-00006-of-00008.safetensors",
147
+ "transformer.h.24.ln_2.weight": "model-00006-of-00008.safetensors",
148
+ "transformer.h.24.mlp.c_proj.weight": "model-00006-of-00008.safetensors",
149
+ "transformer.h.24.mlp.w1.weight": "model-00006-of-00008.safetensors",
150
+ "transformer.h.24.mlp.w2.weight": "model-00006-of-00008.safetensors",
151
+ "transformer.h.25.attn.c_attn.bias": "model-00006-of-00008.safetensors",
152
+ "transformer.h.25.attn.c_attn.weight": "model-00006-of-00008.safetensors",
153
+ "transformer.h.25.attn.c_proj.weight": "model-00006-of-00008.safetensors",
154
+ "transformer.h.25.ln_1.weight": "model-00006-of-00008.safetensors",
155
+ "transformer.h.25.ln_2.weight": "model-00006-of-00008.safetensors",
156
+ "transformer.h.25.mlp.c_proj.weight": "model-00006-of-00008.safetensors",
157
+ "transformer.h.25.mlp.w1.weight": "model-00006-of-00008.safetensors",
158
+ "transformer.h.25.mlp.w2.weight": "model-00006-of-00008.safetensors",
159
+ "transformer.h.26.attn.c_attn.bias": "model-00006-of-00008.safetensors",
160
+ "transformer.h.26.attn.c_attn.weight": "model-00006-of-00008.safetensors",
161
+ "transformer.h.26.attn.c_proj.weight": "model-00006-of-00008.safetensors",
162
+ "transformer.h.26.ln_1.weight": "model-00006-of-00008.safetensors",
163
+ "transformer.h.26.ln_2.weight": "model-00006-of-00008.safetensors",
164
+ "transformer.h.26.mlp.c_proj.weight": "model-00007-of-00008.safetensors",
165
+ "transformer.h.26.mlp.w1.weight": "model-00006-of-00008.safetensors",
166
+ "transformer.h.26.mlp.w2.weight": "model-00006-of-00008.safetensors",
167
+ "transformer.h.27.attn.c_attn.bias": "model-00007-of-00008.safetensors",
168
+ "transformer.h.27.attn.c_attn.weight": "model-00007-of-00008.safetensors",
169
+ "transformer.h.27.attn.c_proj.weight": "model-00007-of-00008.safetensors",
170
+ "transformer.h.27.ln_1.weight": "model-00007-of-00008.safetensors",
171
+ "transformer.h.27.ln_2.weight": "model-00007-of-00008.safetensors",
172
+ "transformer.h.27.mlp.c_proj.weight": "model-00007-of-00008.safetensors",
173
+ "transformer.h.27.mlp.w1.weight": "model-00007-of-00008.safetensors",
174
+ "transformer.h.27.mlp.w2.weight": "model-00007-of-00008.safetensors",
175
+ "transformer.h.28.attn.c_attn.bias": "model-00007-of-00008.safetensors",
176
+ "transformer.h.28.attn.c_attn.weight": "model-00007-of-00008.safetensors",
177
+ "transformer.h.28.attn.c_proj.weight": "model-00007-of-00008.safetensors",
178
+ "transformer.h.28.ln_1.weight": "model-00007-of-00008.safetensors",
179
+ "transformer.h.28.ln_2.weight": "model-00007-of-00008.safetensors",
180
+ "transformer.h.28.mlp.c_proj.weight": "model-00007-of-00008.safetensors",
181
+ "transformer.h.28.mlp.w1.weight": "model-00007-of-00008.safetensors",
182
+ "transformer.h.28.mlp.w2.weight": "model-00007-of-00008.safetensors",
183
+ "transformer.h.29.attn.c_attn.bias": "model-00007-of-00008.safetensors",
184
+ "transformer.h.29.attn.c_attn.weight": "model-00007-of-00008.safetensors",
185
+ "transformer.h.29.attn.c_proj.weight": "model-00007-of-00008.safetensors",
186
+ "transformer.h.29.ln_1.weight": "model-00007-of-00008.safetensors",
187
+ "transformer.h.29.ln_2.weight": "model-00007-of-00008.safetensors",
188
+ "transformer.h.29.mlp.c_proj.weight": "model-00007-of-00008.safetensors",
189
+ "transformer.h.29.mlp.w1.weight": "model-00007-of-00008.safetensors",
190
+ "transformer.h.29.mlp.w2.weight": "model-00007-of-00008.safetensors",
191
+ "transformer.h.3.attn.c_attn.bias": "model-00002-of-00008.safetensors",
192
+ "transformer.h.3.attn.c_attn.weight": "model-00002-of-00008.safetensors",
193
+ "transformer.h.3.attn.c_proj.weight": "model-00002-of-00008.safetensors",
194
+ "transformer.h.3.ln_1.weight": "model-00002-of-00008.safetensors",
195
+ "transformer.h.3.ln_2.weight": "model-00002-of-00008.safetensors",
196
+ "transformer.h.3.mlp.c_proj.weight": "model-00002-of-00008.safetensors",
197
+ "transformer.h.3.mlp.w1.weight": "model-00002-of-00008.safetensors",
198
+ "transformer.h.3.mlp.w2.weight": "model-00002-of-00008.safetensors",
199
+ "transformer.h.30.attn.c_attn.bias": "model-00007-of-00008.safetensors",
200
+ "transformer.h.30.attn.c_attn.weight": "model-00007-of-00008.safetensors",
201
+ "transformer.h.30.attn.c_proj.weight": "model-00007-of-00008.safetensors",
202
+ "transformer.h.30.ln_1.weight": "model-00007-of-00008.safetensors",
203
+ "transformer.h.30.ln_2.weight": "model-00007-of-00008.safetensors",
204
+ "transformer.h.30.mlp.c_proj.weight": "model-00007-of-00008.safetensors",
205
+ "transformer.h.30.mlp.w1.weight": "model-00007-of-00008.safetensors",
206
+ "transformer.h.30.mlp.w2.weight": "model-00007-of-00008.safetensors",
207
+ "transformer.h.31.attn.c_attn.bias": "model-00007-of-00008.safetensors",
208
+ "transformer.h.31.attn.c_attn.weight": "model-00007-of-00008.safetensors",
209
+ "transformer.h.31.attn.c_proj.weight": "model-00007-of-00008.safetensors",
210
+ "transformer.h.31.ln_1.weight": "model-00007-of-00008.safetensors",
211
+ "transformer.h.31.ln_2.weight": "model-00007-of-00008.safetensors",
212
+ "transformer.h.31.mlp.c_proj.weight": "model-00008-of-00008.safetensors",
213
+ "transformer.h.31.mlp.w1.weight": "model-00007-of-00008.safetensors",
214
+ "transformer.h.31.mlp.w2.weight": "model-00007-of-00008.safetensors",
215
+ "transformer.h.4.attn.c_attn.bias": "model-00002-of-00008.safetensors",
216
+ "transformer.h.4.attn.c_attn.weight": "model-00002-of-00008.safetensors",
217
+ "transformer.h.4.attn.c_proj.weight": "model-00002-of-00008.safetensors",
218
+ "transformer.h.4.ln_1.weight": "model-00002-of-00008.safetensors",
219
+ "transformer.h.4.ln_2.weight": "model-00002-of-00008.safetensors",
220
+ "transformer.h.4.mlp.c_proj.weight": "model-00002-of-00008.safetensors",
221
+ "transformer.h.4.mlp.w1.weight": "model-00002-of-00008.safetensors",
222
+ "transformer.h.4.mlp.w2.weight": "model-00002-of-00008.safetensors",
223
+ "transformer.h.5.attn.c_attn.bias": "model-00002-of-00008.safetensors",
224
+ "transformer.h.5.attn.c_attn.weight": "model-00002-of-00008.safetensors",
225
+ "transformer.h.5.attn.c_proj.weight": "model-00002-of-00008.safetensors",
226
+ "transformer.h.5.ln_1.weight": "model-00002-of-00008.safetensors",
227
+ "transformer.h.5.ln_2.weight": "model-00002-of-00008.safetensors",
228
+ "transformer.h.5.mlp.c_proj.weight": "model-00002-of-00008.safetensors",
229
+ "transformer.h.5.mlp.w1.weight": "model-00002-of-00008.safetensors",
230
+ "transformer.h.5.mlp.w2.weight": "model-00002-of-00008.safetensors",
231
+ "transformer.h.6.attn.c_attn.bias": "model-00002-of-00008.safetensors",
232
+ "transformer.h.6.attn.c_attn.weight": "model-00002-of-00008.safetensors",
233
+ "transformer.h.6.attn.c_proj.weight": "model-00002-of-00008.safetensors",
234
+ "transformer.h.6.ln_1.weight": "model-00002-of-00008.safetensors",
235
+ "transformer.h.6.ln_2.weight": "model-00002-of-00008.safetensors",
236
+ "transformer.h.6.mlp.c_proj.weight": "model-00003-of-00008.safetensors",
237
+ "transformer.h.6.mlp.w1.weight": "model-00002-of-00008.safetensors",
238
+ "transformer.h.6.mlp.w2.weight": "model-00002-of-00008.safetensors",
239
+ "transformer.h.7.attn.c_attn.bias": "model-00003-of-00008.safetensors",
240
+ "transformer.h.7.attn.c_attn.weight": "model-00003-of-00008.safetensors",
241
+ "transformer.h.7.attn.c_proj.weight": "model-00003-of-00008.safetensors",
242
+ "transformer.h.7.ln_1.weight": "model-00003-of-00008.safetensors",
243
+ "transformer.h.7.ln_2.weight": "model-00003-of-00008.safetensors",
244
+ "transformer.h.7.mlp.c_proj.weight": "model-00003-of-00008.safetensors",
245
+ "transformer.h.7.mlp.w1.weight": "model-00003-of-00008.safetensors",
246
+ "transformer.h.7.mlp.w2.weight": "model-00003-of-00008.safetensors",
247
+ "transformer.h.8.attn.c_attn.bias": "model-00003-of-00008.safetensors",
248
+ "transformer.h.8.attn.c_attn.weight": "model-00003-of-00008.safetensors",
249
+ "transformer.h.8.attn.c_proj.weight": "model-00003-of-00008.safetensors",
250
+ "transformer.h.8.ln_1.weight": "model-00003-of-00008.safetensors",
251
+ "transformer.h.8.ln_2.weight": "model-00003-of-00008.safetensors",
252
+ "transformer.h.8.mlp.c_proj.weight": "model-00003-of-00008.safetensors",
253
+ "transformer.h.8.mlp.w1.weight": "model-00003-of-00008.safetensors",
254
+ "transformer.h.8.mlp.w2.weight": "model-00003-of-00008.safetensors",
255
+ "transformer.h.9.attn.c_attn.bias": "model-00003-of-00008.safetensors",
256
+ "transformer.h.9.attn.c_attn.weight": "model-00003-of-00008.safetensors",
257
+ "transformer.h.9.attn.c_proj.weight": "model-00003-of-00008.safetensors",
258
+ "transformer.h.9.ln_1.weight": "model-00003-of-00008.safetensors",
259
+ "transformer.h.9.ln_2.weight": "model-00003-of-00008.safetensors",
260
+ "transformer.h.9.mlp.c_proj.weight": "model-00003-of-00008.safetensors",
261
+ "transformer.h.9.mlp.w1.weight": "model-00003-of-00008.safetensors",
262
+ "transformer.h.9.mlp.w2.weight": "model-00003-of-00008.safetensors",
263
+ "transformer.ln_f.weight": "model-00008-of-00008.safetensors",
264
+ "transformer.wte.weight": "model-00001-of-00008.safetensors"
265
+ }
266
+ }
modeling_qwen.py ADDED
@@ -0,0 +1,1232 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) Alibaba Cloud.
2
+ #
3
+ # This source code is licensed under the license found in the
4
+ # LICENSE file in the root directory of this source tree.
5
+
6
+ import importlib
7
+ import math
8
+ from typing import TYPE_CHECKING, Optional, Tuple, Union, Callable, List, Any, Generator
9
+
10
+ import torch
11
+ import torch.nn.functional as F
12
+ import torch.utils.checkpoint
13
+ from torch.cuda.amp import autocast
14
+
15
+ from torch.nn import CrossEntropyLoss
16
+ from transformers import PreTrainedTokenizer, GenerationConfig, StoppingCriteriaList
17
+ from transformers.generation.logits_process import LogitsProcessorList
18
+
19
+ if TYPE_CHECKING:
20
+ from transformers.generation.streamers import BaseStreamer
21
+ from transformers.generation.utils import GenerateOutput
22
+ from transformers.modeling_outputs import (
23
+ BaseModelOutputWithPast,
24
+ CausalLMOutputWithPast,
25
+ )
26
+ from transformers.modeling_utils import PreTrainedModel
27
+ from transformers.utils import logging
28
+
29
+ try:
30
+ from einops import rearrange
31
+ except ImportError:
32
+ rearrange = None
33
+ from torch import nn
34
+
35
+ SUPPORT_CUDA = torch.cuda.is_available()
36
+ SUPPORT_BF16 = SUPPORT_CUDA and torch.cuda.is_bf16_supported()
37
+ SUPPORT_FP16 = SUPPORT_CUDA and torch.cuda.get_device_capability(0)[0] >= 7
38
+
39
+ from .configuration_qwen import QWenConfig
40
+ from .qwen_generation_utils import (
41
+ HistoryType,
42
+ make_context,
43
+ decode_tokens,
44
+ get_stop_words_ids,
45
+ StopWordsLogitsProcessor,
46
+ )
47
+
48
+
49
+ logger = logging.get_logger(__name__)
50
+
51
+ _CHECKPOINT_FOR_DOC = "qwen"
52
+ _CONFIG_FOR_DOC = "QWenConfig"
53
+
54
+ QWen_PRETRAINED_MODEL_ARCHIVE_LIST = ["qwen-7b"]
55
+
56
+ _ERROR_BAD_CHAT_FORMAT = """\
57
+ We detect you are probably using the pretrained model (rather than chat model) for chatting, since the chat_format in generation_config is not "chatml".
58
+ If you are directly using the model downloaded from Huggingface, please make sure you are using our "Qwen/Qwen-7B-Chat" Huggingface model (rather than "Qwen/Qwen-7B") when you call model.chat().
59
+ 我们检测到您可能在使用预训练模型(而非chat模型)进行多轮chat,因为您当前在generation_config指定的chat_format,并未设置为我们在对话中所支持的"chatml"格式。
60
+ 如果您在直接使用我们从Huggingface提供的模型,请确保您在调用model.chat()时,使用的是"Qwen/Qwen-7B-Chat"模型(而非"Qwen/Qwen-7B"预训练模型)。
61
+ """
62
+
63
+ _SENTINEL = object()
64
+ _ERROR_STREAM_IN_CHAT = """\
65
+ Pass argument `stream` to model.chat() is buggy, deprecated, and marked for removal. Please use model.chat_stream(...) instead of model.chat(..., stream=True).
66
+ 向model.chat()传入参数stream的用法可能存在Bug,该用法已被废弃,将在未来被移除。请使用model.chat_stream(...)代替model.chat(..., stream=True)。
67
+ """
68
+
69
+ _ERROR_INPUT_CPU_QUERY_WITH_FLASH_ATTN_ACTIVATED = """\
70
+ We detect you have activated flash attention support, but running model computation on CPU. Please make sure that your input data has been placed on GPU. If you actually want to run CPU computation, please following the readme and set device_map="cpu" to disable flash attention when loading the model (calling AutoModelForCausalLM.from_pretrained).
71
+ 检测到您的模型已激活了flash attention支持,但正在执行CPU运算任务。如使用flash attention,请您确认模型输入已经传到GPU上。如果您确认要执行CPU运算,请您在载入模型(调用AutoModelForCausalLM.from_pretrained)时,按照readme说法,指定device_map="cpu"以禁用flash attention。
72
+ """
73
+
74
+ apply_rotary_emb_func = None
75
+ rms_norm = None
76
+ flash_attn_unpadded_func = None
77
+
78
+
79
+ def _import_flash_attn():
80
+ global apply_rotary_emb_func, rms_norm, flash_attn_unpadded_func
81
+ try:
82
+ from flash_attn.layers.rotary import apply_rotary_emb_func as __apply_rotary_emb_func
83
+ apply_rotary_emb_func = __apply_rotary_emb_func
84
+ except ImportError:
85
+ logger.warn(
86
+ "Warning: import flash_attn rotary fail, please install FlashAttention rotary to get higher efficiency "
87
+ "https://github.com/Dao-AILab/flash-attention/tree/main/csrc/rotary"
88
+ )
89
+
90
+ try:
91
+ from flash_attn.ops.rms_norm import rms_norm as __rms_norm
92
+ rms_norm = __rms_norm
93
+ except ImportError:
94
+ logger.warn(
95
+ "Warning: import flash_attn rms_norm fail, please install FlashAttention layer_norm to get higher efficiency "
96
+ "https://github.com/Dao-AILab/flash-attention/tree/main/csrc/layer_norm"
97
+ )
98
+
99
+ try:
100
+ import flash_attn
101
+ if not hasattr(flash_attn, '__version__'):
102
+ from flash_attn.flash_attn_interface import flash_attn_unpadded_func as __flash_attn_unpadded_func
103
+ else:
104
+ if int(flash_attn.__version__.split(".")[0]) >= 2:
105
+ from flash_attn.flash_attn_interface import flash_attn_varlen_func as __flash_attn_unpadded_func
106
+ else:
107
+ from flash_attn.flash_attn_interface import flash_attn_unpadded_func as __flash_attn_unpadded_func
108
+ flash_attn_unpadded_func = __flash_attn_unpadded_func
109
+ except ImportError:
110
+ logger.warn(
111
+ "Warning: import flash_attn fail, please install FlashAttention to get higher efficiency "
112
+ "https://github.com/Dao-AILab/flash-attention"
113
+ )
114
+
115
+
116
+ class FlashSelfAttention(torch.nn.Module):
117
+ def __init__(
118
+ self,
119
+ causal=False,
120
+ softmax_scale=None,
121
+ attention_dropout=0.0,
122
+ ):
123
+ super().__init__()
124
+ assert flash_attn_unpadded_func is not None, (
125
+ "Please install FlashAttention first, " "e.g., with pip install flash-attn"
126
+ )
127
+ assert (
128
+ rearrange is not None
129
+ ), "Please install einops first, e.g., with pip install einops"
130
+ self.causal = causal
131
+ self.softmax_scale = softmax_scale
132
+ self.dropout_p = attention_dropout
133
+
134
+ def forward(self, q, k, v):
135
+ assert all((i.dtype in [torch.float16, torch.bfloat16] for i in (q, k, v)))
136
+ assert all((i.is_cuda for i in (q, k, v)))
137
+ batch_size, seqlen_q = q.shape[0], q.shape[1]
138
+ seqlen_k = k.shape[1]
139
+
140
+ q, k, v = [rearrange(x, "b s ... -> (b s) ...") for x in [q, k, v]]
141
+ cu_seqlens_q = torch.arange(
142
+ 0,
143
+ (batch_size + 1) * seqlen_q,
144
+ step=seqlen_q,
145
+ dtype=torch.int32,
146
+ device=q.device,
147
+ )
148
+
149
+ if self.training:
150
+ assert seqlen_k == seqlen_q
151
+
152
+ is_causal = self.causal
153
+ cu_seqlens_k = cu_seqlens_q
154
+ else:
155
+ is_causal = seqlen_q == seqlen_k
156
+ cu_seqlens_k = torch.arange(
157
+ 0,
158
+ (batch_size + 1) * seqlen_k,
159
+ step=seqlen_k,
160
+ dtype=torch.int32,
161
+ device=q.device,
162
+ )
163
+ self.dropout_p = 0
164
+
165
+ output = flash_attn_unpadded_func(
166
+ q,
167
+ k,
168
+ v,
169
+ cu_seqlens_q,
170
+ cu_seqlens_k,
171
+ seqlen_q,
172
+ seqlen_k,
173
+ self.dropout_p,
174
+ softmax_scale=self.softmax_scale,
175
+ causal=is_causal,
176
+ )
177
+
178
+ new_shape = (batch_size, output.shape[0] // batch_size) + output.shape[1:]
179
+ output = output.view(new_shape)
180
+ return output
181
+
182
+
183
+ class QWenAttention(nn.Module):
184
+ def __init__(self, config):
185
+ super().__init__()
186
+
187
+ self.register_buffer("masked_bias", torch.tensor(-1e4), persistent=False)
188
+ self.seq_length = config.seq_length
189
+
190
+ self.hidden_size = config.hidden_size
191
+ self.split_size = config.hidden_size
192
+ self.num_heads = config.num_attention_heads
193
+ self.head_dim = self.hidden_size // self.num_heads
194
+
195
+ self.use_flash_attn = config.use_flash_attn
196
+ self.scale_attn_weights = True
197
+
198
+ self.projection_size = config.kv_channels * config.num_attention_heads
199
+
200
+ assert self.projection_size % config.num_attention_heads == 0
201
+ self.hidden_size_per_attention_head = (
202
+ self.projection_size // config.num_attention_heads
203
+ )
204
+
205
+ self.c_attn = nn.Linear(config.hidden_size, 3 * self.projection_size)
206
+
207
+ self.c_proj = nn.Linear(
208
+ config.hidden_size, self.projection_size, bias=not config.no_bias
209
+ )
210
+
211
+ self.is_fp32 = not (config.bf16 or config.fp16)
212
+ if (
213
+ self.use_flash_attn
214
+ and flash_attn_unpadded_func is not None
215
+ and not self.is_fp32
216
+ ):
217
+ self.core_attention_flash = FlashSelfAttention(
218
+ causal=True, attention_dropout=config.attn_dropout_prob
219
+ )
220
+ self.bf16 = config.bf16
221
+
222
+ self.use_dynamic_ntk = config.use_dynamic_ntk
223
+ self.use_logn_attn = config.use_logn_attn
224
+
225
+ logn_list = [
226
+ math.log(i, self.seq_length) if i > self.seq_length else 1
227
+ for i in range(1, 32768)
228
+ ]
229
+ self.logn_tensor = torch.tensor(logn_list)[None, :, None, None]
230
+
231
+ self.attn_dropout = nn.Dropout(config.attn_dropout_prob)
232
+
233
+ def _attn(self, query, key, value, registered_causal_mask, attention_mask=None, head_mask=None):
234
+ attn_weights = torch.matmul(query, key.transpose(-1, -2))
235
+
236
+ if self.scale_attn_weights:
237
+ attn_weights = attn_weights / torch.full(
238
+ [],
239
+ value.size(-1) ** 0.5,
240
+ dtype=attn_weights.dtype,
241
+ device=attn_weights.device,
242
+ )
243
+
244
+ query_length, key_length = query.size(-2), key.size(-2)
245
+ causal_mask = registered_causal_mask[
246
+ :, :, key_length - query_length : key_length, :key_length
247
+ ]
248
+ mask_value = torch.finfo(attn_weights.dtype).min
249
+ mask_value = torch.full([], mask_value, dtype=attn_weights.dtype).to(
250
+ attn_weights.device
251
+ )
252
+ attn_weights = torch.where(
253
+ causal_mask, attn_weights.to(attn_weights.dtype), mask_value
254
+ )
255
+
256
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1)
257
+
258
+ attn_weights = attn_weights.type(value.dtype)
259
+ attn_weights = self.attn_dropout(attn_weights)
260
+
261
+ if head_mask is not None:
262
+ attn_weights = attn_weights * head_mask
263
+
264
+ attn_output = torch.matmul(attn_weights, value)
265
+ attn_output = attn_output.transpose(1, 2)
266
+
267
+ return attn_output, attn_weights
268
+
269
+ def _upcast_and_reordered_attn(
270
+ self, query, key, value, registered_causal_mask, attention_mask=None, head_mask=None
271
+ ):
272
+ bsz, num_heads, q_seq_len, dk = query.size()
273
+ _, _, k_seq_len, _ = key.size()
274
+
275
+ attn_weights = torch.empty(
276
+ bsz * num_heads,
277
+ q_seq_len,
278
+ k_seq_len,
279
+ dtype=torch.float32,
280
+ device=query.device,
281
+ )
282
+
283
+ scale_factor = 1.0
284
+ if self.scale_attn_weights:
285
+ scale_factor /= float(value.size(-1)) ** 0.5
286
+
287
+ with autocast(enabled=False):
288
+ q, k = query.reshape(-1, q_seq_len, dk), key.transpose(-1, -2).reshape(
289
+ -1, dk, k_seq_len
290
+ )
291
+ attn_weights = torch.baddbmm(
292
+ attn_weights, q.float(), k.float(), beta=0, alpha=scale_factor
293
+ )
294
+ attn_weights = attn_weights.reshape(bsz, num_heads, q_seq_len, k_seq_len)
295
+
296
+ query_length, key_length = query.size(-2), key.size(-2)
297
+ causal_mask = registered_causal_mask[
298
+ :, :, key_length - query_length : key_length, :key_length
299
+ ]
300
+ mask_value = torch.finfo(attn_weights.dtype).min
301
+ mask_value = torch.tensor(mask_value, dtype=attn_weights.dtype).to(
302
+ attn_weights.device
303
+ )
304
+ attn_weights = torch.where(causal_mask, attn_weights, mask_value)
305
+
306
+ if attention_mask is not None:
307
+ attn_weights = attn_weights + attention_mask
308
+
309
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1)
310
+
311
+ if attn_weights.dtype != torch.float32:
312
+ raise RuntimeError(
313
+ "Error with upcasting, attn_weights does not have dtype torch.float32"
314
+ )
315
+ attn_weights = attn_weights.type(value.dtype)
316
+ attn_weights = self.attn_dropout(attn_weights)
317
+
318
+ if head_mask is not None:
319
+ attn_weights = attn_weights * head_mask
320
+
321
+ attn_output = torch.matmul(attn_weights, value)
322
+
323
+ return attn_output, attn_weights
324
+
325
+ def _split_heads(self, tensor, num_heads, attn_head_size):
326
+ new_shape = tensor.size()[:-1] + (num_heads, attn_head_size)
327
+ tensor = tensor.view(new_shape)
328
+ return tensor
329
+
330
+ def _merge_heads(self, tensor, num_heads, attn_head_size):
331
+ tensor = tensor.contiguous()
332
+ new_shape = tensor.size()[:-2] + (num_heads * attn_head_size,)
333
+ return tensor.view(new_shape)
334
+
335
+ def forward(
336
+ self,
337
+ hidden_states: Optional[Tuple[torch.FloatTensor]],
338
+ rotary_pos_emb: Optional[List[torch.Tensor]] = None,
339
+ registered_causal_mask: Optional[torch.Tensor] = None,
340
+ layer_past: Optional[Tuple[torch.Tensor]] = None,
341
+ attention_mask: Optional[torch.FloatTensor] = None,
342
+ head_mask: Optional[torch.FloatTensor] = None,
343
+ encoder_hidden_states: Optional[torch.Tensor] = None,
344
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
345
+ output_attentions: Optional[bool] = False,
346
+ use_cache: Optional[bool] = False,
347
+ ):
348
+
349
+ mixed_x_layer = self.c_attn(hidden_states)
350
+
351
+ query, key, value = mixed_x_layer.split(self.split_size, dim=2)
352
+
353
+ query = self._split_heads(query, self.num_heads, self.head_dim)
354
+ key = self._split_heads(key, self.num_heads, self.head_dim)
355
+ value = self._split_heads(value, self.num_heads, self.head_dim)
356
+
357
+ if rotary_pos_emb is not None:
358
+ cur_len = query.shape[1]
359
+ rotary_pos_emb = [i[:, -cur_len:, :, :] for i in rotary_pos_emb]
360
+ rotary_pos_emb = (rotary_pos_emb,) * 2
361
+ q_pos_emb, k_pos_emb = rotary_pos_emb
362
+ # Slice the pos emb for current inference
363
+ query = apply_rotary_pos_emb(query, q_pos_emb)
364
+ key = apply_rotary_pos_emb(key, k_pos_emb)
365
+
366
+ if layer_past is not None:
367
+ past_key, past_value = layer_past[0], layer_past[1]
368
+ key = torch.cat((past_key, key), dim=1)
369
+ value = torch.cat((past_value, value), dim=1)
370
+
371
+ if use_cache:
372
+ present = (key, value)
373
+ else:
374
+ present = None
375
+
376
+ if self.use_logn_attn and not self.training:
377
+ if self.logn_tensor.device != query.device or self.logn_tensor.dtype != query.dtype:
378
+ self.logn_tensor = self.logn_tensor.to(query.device).type_as(query)
379
+ seq_start = key.size(1) - query.size(1)
380
+ seq_end = key.size(1)
381
+ logn_tensor = self.logn_tensor[:, seq_start:seq_end, :, :]
382
+ query = query * logn_tensor.expand_as(query)
383
+
384
+ if (
385
+ self.use_flash_attn
386
+ and flash_attn_unpadded_func is not None
387
+ and not self.is_fp32
388
+ and query.is_cuda
389
+ ):
390
+ q, k, v = query, key, value
391
+ context_layer = self.core_attention_flash(q, k, v)
392
+
393
+ # b s h d -> b s (h d)
394
+ context_layer = context_layer.flatten(2,3).contiguous()
395
+
396
+ else:
397
+ query = query.permute(0, 2, 1, 3)
398
+ key = key.permute(0, 2, 1, 3)
399
+ value = value.permute(0, 2, 1, 3)
400
+ if (
401
+ registered_causal_mask is None
402
+ and self.use_flash_attn
403
+ and flash_attn_unpadded_func is not None
404
+ and not self.is_fp32
405
+ and not query.is_cuda
406
+ ):
407
+ raise Exception(_ERROR_INPUT_CPU_QUERY_WITH_FLASH_ATTN_ACTIVATED)
408
+ attn_output, attn_weight = self._attn(
409
+ query, key, value, registered_causal_mask, attention_mask, head_mask
410
+ )
411
+ context_layer = self._merge_heads(
412
+ attn_output, self.num_heads, self.head_dim
413
+ )
414
+
415
+ attn_output = self.c_proj(context_layer)
416
+
417
+ outputs = (attn_output, present)
418
+ if output_attentions:
419
+ if (
420
+ self.use_flash_attn
421
+ and flash_attn_unpadded_func is not None
422
+ and not self.is_fp32
423
+ ):
424
+ raise ValueError("Cannot output attentions while using flash-attn")
425
+ else:
426
+ outputs += (attn_weight,)
427
+
428
+ return outputs
429
+
430
+
431
+ class QWenMLP(nn.Module):
432
+ def __init__(self, config):
433
+ super().__init__()
434
+ self.w1 = nn.Linear(
435
+ config.hidden_size, config.intermediate_size // 2, bias=not config.no_bias
436
+ )
437
+ self.w2 = nn.Linear(
438
+ config.hidden_size, config.intermediate_size // 2, bias=not config.no_bias
439
+ )
440
+ ff_dim_in = config.intermediate_size // 2
441
+ self.c_proj = nn.Linear(ff_dim_in, config.hidden_size, bias=not config.no_bias)
442
+
443
+ def forward(self, hidden_states):
444
+ a1 = self.w1(hidden_states)
445
+ a2 = self.w2(hidden_states)
446
+ intermediate_parallel = a1 * F.silu(a2)
447
+ output = self.c_proj(intermediate_parallel)
448
+ return output
449
+
450
+ class QWenBlock(nn.Module):
451
+ def __init__(self, config):
452
+ super().__init__()
453
+ hidden_size = config.hidden_size
454
+ self.bf16 = config.bf16
455
+
456
+ self.ln_1 = RMSNorm(
457
+ hidden_size,
458
+ eps=config.layer_norm_epsilon,
459
+ )
460
+ self.attn = QWenAttention(config)
461
+ self.ln_2 = RMSNorm(
462
+ hidden_size,
463
+ eps=config.layer_norm_epsilon,
464
+ )
465
+
466
+ self.mlp = QWenMLP(config)
467
+
468
+ def forward(
469
+ self,
470
+ hidden_states: Optional[Tuple[torch.FloatTensor]],
471
+ rotary_pos_emb: Optional[List[torch.Tensor]] = None,
472
+ registered_causal_mask: Optional[torch.Tensor] = None,
473
+ layer_past: Optional[Tuple[torch.Tensor]] = None,
474
+ attention_mask: Optional[torch.FloatTensor] = None,
475
+ head_mask: Optional[torch.FloatTensor] = None,
476
+ encoder_hidden_states: Optional[torch.Tensor] = None,
477
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
478
+ use_cache: Optional[bool] = False,
479
+ output_attentions: Optional[bool] = False,
480
+ ):
481
+ layernorm_output = self.ln_1(hidden_states)
482
+
483
+ attn_outputs = self.attn(
484
+ layernorm_output,
485
+ rotary_pos_emb,
486
+ registered_causal_mask=registered_causal_mask,
487
+ layer_past=layer_past,
488
+ at