xwk123 commited on
Commit
27065c8
·
verified ·
1 Parent(s): ce8e0fd

Upload folder using huggingface_hub

Browse files
config.json ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "/zk/corpus/ckpt/qwen_stage2_1task_0524-backup/checkpoint-6000",
3
+ "architectures": [
4
+ "QWenLMHeadModel"
5
+ ],
6
+ "attn_dropout_prob": 0.0,
7
+ "auto_map": {
8
+ "AutoConfig": "configuration_qwen.QWenConfig",
9
+ "AutoModelForCausalLM": "modeling_qwen.QWenLMHeadModel"
10
+ },
11
+ "bf16": true,
12
+ "emb_dropout_prob": 0.0,
13
+ "fp16": false,
14
+ "fp32": false,
15
+ "hidden_size": 4096,
16
+ "initializer_range": 0.02,
17
+ "intermediate_size": 22016,
18
+ "kv_channels": 128,
19
+ "layer_norm_epsilon": 1e-06,
20
+ "max_position_embeddings": 8192,
21
+ "model_type": "qwen",
22
+ "no_bias": true,
23
+ "num_attention_heads": 32,
24
+ "num_hidden_layers": 32,
25
+ "onnx_safe": null,
26
+ "rotary_emb_base": 10000,
27
+ "rotary_pct": 1.0,
28
+ "scale_attn_weights": true,
29
+ "seq_length": 2048,
30
+ "tie_word_embeddings": false,
31
+ "tokenizer_type": "QWenTokenizer",
32
+ "torch_dtype": "bfloat16",
33
+ "transformers_version": "4.36.2",
34
+ "use_cache": false,
35
+ "use_dynamic_ntk": true,
36
+ "use_flash_attn": false,
37
+ "use_logn_attn": true,
38
+ "visual": {
39
+ "heads": 16,
40
+ "image_size": 448,
41
+ "image_start_id": 151857,
42
+ "layers": 48,
43
+ "mlp_ratio": 4.9231,
44
+ "output_dim": 4096,
45
+ "patch_size": 14,
46
+ "width": 1664
47
+ },
48
+ "vocab_size": 151936
49
+ }
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,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "chat_format": "chatml",
3
+ "do_sample": true,
4
+ "eos_token_id": 151643,
5
+ "max_new_tokens": 512,
6
+ "max_window_size": 6144,
7
+ "pad_token_id": 151643,
8
+ "top_k": 0,
9
+ "top_p": 0.3,
10
+ "transformers_version": "4.36.2"
11
+ }
latest ADDED
@@ -0,0 +1 @@
 
 
1
+ global_step1800
model-00001-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:be7bda1e695b8d2c9625a014a5c3cfdf7ae9ffd4ddf1ead2d6f874a5f0a3e717
3
+ size 4988485656
model-00002-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:413611102a76c95b554795945ee32f5c20848390f289f0e0fe0f530e45a0b5d5
3
+ size 4981246520
model-00003-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b1b8034912ce5080c5a91e42c4251509021f67f4856408f28356a8e2ed8d91cb
3
+ size 4977360088
model-00004-of-00004.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:08ef9867631a4ce27660806e91ef8556a7ec9f1bc432cbdd9488b1ee6cc85df9
3
+ size 4366885504
model.safetensors.index.json ADDED
@@ -0,0 +1,860 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "total_size": 19313870336
4
+ },
5
+ "weight_map": {
6
+ "lm_head.weight": "model-00004-of-00004.safetensors",
7
+ "transformer.h.0.attn.c_attn.bias": "model-00001-of-00004.safetensors",
8
+ "transformer.h.0.attn.c_attn.weight": "model-00001-of-00004.safetensors",
9
+ "transformer.h.0.attn.c_proj.weight": "model-00001-of-00004.safetensors",
10
+ "transformer.h.0.ln_1.weight": "model-00001-of-00004.safetensors",
11
+ "transformer.h.0.ln_2.weight": "model-00001-of-00004.safetensors",
12
+ "transformer.h.0.mlp.c_proj.weight": "model-00001-of-00004.safetensors",
13
+ "transformer.h.0.mlp.w1.weight": "model-00001-of-00004.safetensors",
14
+ "transformer.h.0.mlp.w2.weight": "model-00001-of-00004.safetensors",
15
+ "transformer.h.1.attn.c_attn.bias": "model-00001-of-00004.safetensors",
16
+ "transformer.h.1.attn.c_attn.weight": "model-00001-of-00004.safetensors",
17
+ "transformer.h.1.attn.c_proj.weight": "model-00001-of-00004.safetensors",
18
+ "transformer.h.1.ln_1.weight": "model-00001-of-00004.safetensors",
19
+ "transformer.h.1.ln_2.weight": "model-00001-of-00004.safetensors",
20
+ "transformer.h.1.mlp.c_proj.weight": "model-00001-of-00004.safetensors",
21
+ "transformer.h.1.mlp.w1.weight": "model-00001-of-00004.safetensors",
22
+ "transformer.h.1.mlp.w2.weight": "model-00001-of-00004.safetensors",
23
+ "transformer.h.10.attn.c_attn.bias": "model-00002-of-00004.safetensors",
24
+ "transformer.h.10.attn.c_attn.weight": "model-00002-of-00004.safetensors",
25
+ "transformer.h.10.attn.c_proj.weight": "model-00002-of-00004.safetensors",
26
+ "transformer.h.10.ln_1.weight": "model-00002-of-00004.safetensors",
27
+ "transformer.h.10.ln_2.weight": "model-00002-of-00004.safetensors",
28
+ "transformer.h.10.mlp.c_proj.weight": "model-00002-of-00004.safetensors",
29
+ "transformer.h.10.mlp.w1.weight": "model-00002-of-00004.safetensors",
30
+ "transformer.h.10.mlp.w2.weight": "model-00002-of-00004.safetensors",
31
+ "transformer.h.11.attn.c_attn.bias": "model-00002-of-00004.safetensors",
32
+ "transformer.h.11.attn.c_attn.weight": "model-00002-of-00004.safetensors",
33
+ "transformer.h.11.attn.c_proj.weight": "model-00002-of-00004.safetensors",
34
+ "transformer.h.11.ln_1.weight": "model-00002-of-00004.safetensors",
35
+ "transformer.h.11.ln_2.weight": "model-00002-of-00004.safetensors",
36
+ "transformer.h.11.mlp.c_proj.weight": "model-00002-of-00004.safetensors",
37
+ "transformer.h.11.mlp.w1.weight": "model-00002-of-00004.safetensors",
38
+ "transformer.h.11.mlp.w2.weight": "model-00002-of-00004.safetensors",
39
+ "transformer.h.12.attn.c_attn.bias": "model-00002-of-00004.safetensors",
40
+ "transformer.h.12.attn.c_attn.weight": "model-00002-of-00004.safetensors",
41
+ "transformer.h.12.attn.c_proj.weight": "model-00002-of-00004.safetensors",
42
+ "transformer.h.12.ln_1.weight": "model-00002-of-00004.safetensors",
43
+ "transformer.h.12.ln_2.weight": "model-00002-of-00004.safetensors",
44
+ "transformer.h.12.mlp.c_proj.weight": "model-00002-of-00004.safetensors",
45
+ "transformer.h.12.mlp.w1.weight": "model-00002-of-00004.safetensors",
46
+ "transformer.h.12.mlp.w2.weight": "model-00002-of-00004.safetensors",
47
+ "transformer.h.13.attn.c_attn.bias": "model-00002-of-00004.safetensors",
48
+ "transformer.h.13.attn.c_attn.weight": "model-00002-of-00004.safetensors",
49
+ "transformer.h.13.attn.c_proj.weight": "model-00002-of-00004.safetensors",
50
+ "transformer.h.13.ln_1.weight": "model-00002-of-00004.safetensors",
51
+ "transformer.h.13.ln_2.weight": "model-00002-of-00004.safetensors",
52
+ "transformer.h.13.mlp.c_proj.weight": "model-00002-of-00004.safetensors",
53
+ "transformer.h.13.mlp.w1.weight": "model-00002-of-00004.safetensors",
54
+ "transformer.h.13.mlp.w2.weight": "model-00002-of-00004.safetensors",
55
+ "transformer.h.14.attn.c_attn.bias": "model-00002-of-00004.safetensors",
56
+ "transformer.h.14.attn.c_attn.weight": "model-00002-of-00004.safetensors",
57
+ "transformer.h.14.attn.c_proj.weight": "model-00002-of-00004.safetensors",
58
+ "transformer.h.14.ln_1.weight": "model-00002-of-00004.safetensors",
59
+ "transformer.h.14.ln_2.weight": "model-00002-of-00004.safetensors",
60
+ "transformer.h.14.mlp.c_proj.weight": "model-00002-of-00004.safetensors",
61
+ "transformer.h.14.mlp.w1.weight": "model-00002-of-00004.safetensors",
62
+ "transformer.h.14.mlp.w2.weight": "model-00002-of-00004.safetensors",
63
+ "transformer.h.15.attn.c_attn.bias": "model-00002-of-00004.safetensors",
64
+ "transformer.h.15.attn.c_attn.weight": "model-00002-of-00004.safetensors",
65
+ "transformer.h.15.attn.c_proj.weight": "model-00002-of-00004.safetensors",
66
+ "transformer.h.15.ln_1.weight": "model-00002-of-00004.safetensors",
67
+ "transformer.h.15.ln_2.weight": "model-00002-of-00004.safetensors",
68
+ "transformer.h.15.mlp.c_proj.weight": "model-00002-of-00004.safetensors",
69
+ "transformer.h.15.mlp.w1.weight": "model-00002-of-00004.safetensors",
70
+ "transformer.h.15.mlp.w2.weight": "model-00002-of-00004.safetensors",
71
+ "transformer.h.16.attn.c_attn.bias": "model-00002-of-00004.safetensors",
72
+ "transformer.h.16.attn.c_attn.weight": "model-00002-of-00004.safetensors",
73
+ "transformer.h.16.attn.c_proj.weight": "model-00002-of-00004.safetensors",
74
+ "transformer.h.16.ln_1.weight": "model-00002-of-00004.safetensors",
75
+ "transformer.h.16.ln_2.weight": "model-00002-of-00004.safetensors",
76
+ "transformer.h.16.mlp.c_proj.weight": "model-00002-of-00004.safetensors",
77
+ "transformer.h.16.mlp.w1.weight": "model-00002-of-00004.safetensors",
78
+ "transformer.h.16.mlp.w2.weight": "model-00002-of-00004.safetensors",
79
+ "transformer.h.17.attn.c_attn.bias": "model-00002-of-00004.safetensors",
80
+ "transformer.h.17.attn.c_attn.weight": "model-00002-of-00004.safetensors",
81
+ "transformer.h.17.attn.c_proj.weight": "model-00002-of-00004.safetensors",
82
+ "transformer.h.17.ln_1.weight": "model-00002-of-00004.safetensors",
83
+ "transformer.h.17.ln_2.weight": "model-00002-of-00004.safetensors",
84
+ "transformer.h.17.mlp.c_proj.weight": "model-00002-of-00004.safetensors",
85
+ "transformer.h.17.mlp.w1.weight": "model-00002-of-00004.safetensors",
86
+ "transformer.h.17.mlp.w2.weight": "model-00002-of-00004.safetensors",
87
+ "transformer.h.18.attn.c_attn.bias": "model-00002-of-00004.safetensors",
88
+ "transformer.h.18.attn.c_attn.weight": "model-00002-of-00004.safetensors",
89
+ "transformer.h.18.attn.c_proj.weight": "model-00002-of-00004.safetensors",
90
+ "transformer.h.18.ln_1.weight": "model-00002-of-00004.safetensors",
91
+ "transformer.h.18.ln_2.weight": "model-00002-of-00004.safetensors",
92
+ "transformer.h.18.mlp.c_proj.weight": "model-00002-of-00004.safetensors",
93
+ "transformer.h.18.mlp.w1.weight": "model-00002-of-00004.safetensors",
94
+ "transformer.h.18.mlp.w2.weight": "model-00002-of-00004.safetensors",
95
+ "transformer.h.19.attn.c_attn.bias": "model-00002-of-00004.safetensors",
96
+ "transformer.h.19.attn.c_attn.weight": "model-00002-of-00004.safetensors",
97
+ "transformer.h.19.attn.c_proj.weight": "model-00002-of-00004.safetensors",
98
+ "transformer.h.19.ln_1.weight": "model-00002-of-00004.safetensors",
99
+ "transformer.h.19.ln_2.weight": "model-00002-of-00004.safetensors",
100
+ "transformer.h.19.mlp.c_proj.weight": "model-00002-of-00004.safetensors",
101
+ "transformer.h.19.mlp.w1.weight": "model-00002-of-00004.safetensors",
102
+ "transformer.h.19.mlp.w2.weight": "model-00002-of-00004.safetensors",
103
+ "transformer.h.2.attn.c_attn.bias": "model-00001-of-00004.safetensors",
104
+ "transformer.h.2.attn.c_attn.weight": "model-00001-of-00004.safetensors",
105
+ "transformer.h.2.attn.c_proj.weight": "model-00001-of-00004.safetensors",
106
+ "transformer.h.2.ln_1.weight": "model-00001-of-00004.safetensors",
107
+ "transformer.h.2.ln_2.weight": "model-00001-of-00004.safetensors",
108
+ "transformer.h.2.mlp.c_proj.weight": "model-00001-of-00004.safetensors",
109
+ "transformer.h.2.mlp.w1.weight": "model-00001-of-00004.safetensors",
110
+ "transformer.h.2.mlp.w2.weight": "model-00001-of-00004.safetensors",
111
+ "transformer.h.20.attn.c_attn.bias": "model-00002-of-00004.safetensors",
112
+ "transformer.h.20.attn.c_attn.weight": "model-00002-of-00004.safetensors",
113
+ "transformer.h.20.attn.c_proj.weight": "model-00002-of-00004.safetensors",
114
+ "transformer.h.20.ln_1.weight": "model-00002-of-00004.safetensors",
115
+ "transformer.h.20.ln_2.weight": "model-00002-of-00004.safetensors",
116
+ "transformer.h.20.mlp.c_proj.weight": "model-00002-of-00004.safetensors",
117
+ "transformer.h.20.mlp.w1.weight": "model-00002-of-00004.safetensors",
118
+ "transformer.h.20.mlp.w2.weight": "model-00002-of-00004.safetensors",
119
+ "transformer.h.21.attn.c_attn.bias": "model-00002-of-00004.safetensors",
120
+ "transformer.h.21.attn.c_attn.weight": "model-00002-of-00004.safetensors",
121
+ "transformer.h.21.attn.c_proj.weight": "model-00002-of-00004.safetensors",
122
+ "transformer.h.21.ln_1.weight": "model-00002-of-00004.safetensors",
123
+ "transformer.h.21.ln_2.weight": "model-00002-of-00004.safetensors",
124
+ "transformer.h.21.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
125
+ "transformer.h.21.mlp.w1.weight": "model-00002-of-00004.safetensors",
126
+ "transformer.h.21.mlp.w2.weight": "model-00003-of-00004.safetensors",
127
+ "transformer.h.22.attn.c_attn.bias": "model-00003-of-00004.safetensors",
128
+ "transformer.h.22.attn.c_attn.weight": "model-00003-of-00004.safetensors",
129
+ "transformer.h.22.attn.c_proj.weight": "model-00003-of-00004.safetensors",
130
+ "transformer.h.22.ln_1.weight": "model-00003-of-00004.safetensors",
131
+ "transformer.h.22.ln_2.weight": "model-00003-of-00004.safetensors",
132
+ "transformer.h.22.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
133
+ "transformer.h.22.mlp.w1.weight": "model-00003-of-00004.safetensors",
134
+ "transformer.h.22.mlp.w2.weight": "model-00003-of-00004.safetensors",
135
+ "transformer.h.23.attn.c_attn.bias": "model-00003-of-00004.safetensors",
136
+ "transformer.h.23.attn.c_attn.weight": "model-00003-of-00004.safetensors",
137
+ "transformer.h.23.attn.c_proj.weight": "model-00003-of-00004.safetensors",
138
+ "transformer.h.23.ln_1.weight": "model-00003-of-00004.safetensors",
139
+ "transformer.h.23.ln_2.weight": "model-00003-of-00004.safetensors",
140
+ "transformer.h.23.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
141
+ "transformer.h.23.mlp.w1.weight": "model-00003-of-00004.safetensors",
142
+ "transformer.h.23.mlp.w2.weight": "model-00003-of-00004.safetensors",
143
+ "transformer.h.24.attn.c_attn.bias": "model-00003-of-00004.safetensors",
144
+ "transformer.h.24.attn.c_attn.weight": "model-00003-of-00004.safetensors",
145
+ "transformer.h.24.attn.c_proj.weight": "model-00003-of-00004.safetensors",
146
+ "transformer.h.24.ln_1.weight": "model-00003-of-00004.safetensors",
147
+ "transformer.h.24.ln_2.weight": "model-00003-of-00004.safetensors",
148
+ "transformer.h.24.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
149
+ "transformer.h.24.mlp.w1.weight": "model-00003-of-00004.safetensors",
150
+ "transformer.h.24.mlp.w2.weight": "model-00003-of-00004.safetensors",
151
+ "transformer.h.25.attn.c_attn.bias": "model-00003-of-00004.safetensors",
152
+ "transformer.h.25.attn.c_attn.weight": "model-00003-of-00004.safetensors",
153
+ "transformer.h.25.attn.c_proj.weight": "model-00003-of-00004.safetensors",
154
+ "transformer.h.25.ln_1.weight": "model-00003-of-00004.safetensors",
155
+ "transformer.h.25.ln_2.weight": "model-00003-of-00004.safetensors",
156
+ "transformer.h.25.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
157
+ "transformer.h.25.mlp.w1.weight": "model-00003-of-00004.safetensors",
158
+ "transformer.h.25.mlp.w2.weight": "model-00003-of-00004.safetensors",
159
+ "transformer.h.26.attn.c_attn.bias": "model-00003-of-00004.safetensors",
160
+ "transformer.h.26.attn.c_attn.weight": "model-00003-of-00004.safetensors",
161
+ "transformer.h.26.attn.c_proj.weight": "model-00003-of-00004.safetensors",
162
+ "transformer.h.26.ln_1.weight": "model-00003-of-00004.safetensors",
163
+ "transformer.h.26.ln_2.weight": "model-00003-of-00004.safetensors",
164
+ "transformer.h.26.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
165
+ "transformer.h.26.mlp.w1.weight": "model-00003-of-00004.safetensors",
166
+ "transformer.h.26.mlp.w2.weight": "model-00003-of-00004.safetensors",
167
+ "transformer.h.27.attn.c_attn.bias": "model-00003-of-00004.safetensors",
168
+ "transformer.h.27.attn.c_attn.weight": "model-00003-of-00004.safetensors",
169
+ "transformer.h.27.attn.c_proj.weight": "model-00003-of-00004.safetensors",
170
+ "transformer.h.27.ln_1.weight": "model-00003-of-00004.safetensors",
171
+ "transformer.h.27.ln_2.weight": "model-00003-of-00004.safetensors",
172
+ "transformer.h.27.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
173
+ "transformer.h.27.mlp.w1.weight": "model-00003-of-00004.safetensors",
174
+ "transformer.h.27.mlp.w2.weight": "model-00003-of-00004.safetensors",
175
+ "transformer.h.28.attn.c_attn.bias": "model-00003-of-00004.safetensors",
176
+ "transformer.h.28.attn.c_attn.weight": "model-00003-of-00004.safetensors",
177
+ "transformer.h.28.attn.c_proj.weight": "model-00003-of-00004.safetensors",
178
+ "transformer.h.28.ln_1.weight": "model-00003-of-00004.safetensors",
179
+ "transformer.h.28.ln_2.weight": "model-00003-of-00004.safetensors",
180
+ "transformer.h.28.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
181
+ "transformer.h.28.mlp.w1.weight": "model-00003-of-00004.safetensors",
182
+ "transformer.h.28.mlp.w2.weight": "model-00003-of-00004.safetensors",
183
+ "transformer.h.29.attn.c_attn.bias": "model-00003-of-00004.safetensors",
184
+ "transformer.h.29.attn.c_attn.weight": "model-00003-of-00004.safetensors",
185
+ "transformer.h.29.attn.c_proj.weight": "model-00003-of-00004.safetensors",
186
+ "transformer.h.29.ln_1.weight": "model-00003-of-00004.safetensors",
187
+ "transformer.h.29.ln_2.weight": "model-00003-of-00004.safetensors",
188
+ "transformer.h.29.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
189
+ "transformer.h.29.mlp.w1.weight": "model-00003-of-00004.safetensors",
190
+ "transformer.h.29.mlp.w2.weight": "model-00003-of-00004.safetensors",
191
+ "transformer.h.3.attn.c_attn.bias": "model-00001-of-00004.safetensors",
192
+ "transformer.h.3.attn.c_attn.weight": "model-00001-of-00004.safetensors",
193
+ "transformer.h.3.attn.c_proj.weight": "model-00001-of-00004.safetensors",
194
+ "transformer.h.3.ln_1.weight": "model-00001-of-00004.safetensors",
195
+ "transformer.h.3.ln_2.weight": "model-00001-of-00004.safetensors",
196
+ "transformer.h.3.mlp.c_proj.weight": "model-00001-of-00004.safetensors",
197
+ "transformer.h.3.mlp.w1.weight": "model-00001-of-00004.safetensors",
198
+ "transformer.h.3.mlp.w2.weight": "model-00001-of-00004.safetensors",
199
+ "transformer.h.30.attn.c_attn.bias": "model-00003-of-00004.safetensors",
200
+ "transformer.h.30.attn.c_attn.weight": "model-00003-of-00004.safetensors",
201
+ "transformer.h.30.attn.c_proj.weight": "model-00003-of-00004.safetensors",
202
+ "transformer.h.30.ln_1.weight": "model-00003-of-00004.safetensors",
203
+ "transformer.h.30.ln_2.weight": "model-00003-of-00004.safetensors",
204
+ "transformer.h.30.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
205
+ "transformer.h.30.mlp.w1.weight": "model-00003-of-00004.safetensors",
206
+ "transformer.h.30.mlp.w2.weight": "model-00003-of-00004.safetensors",
207
+ "transformer.h.31.attn.c_attn.bias": "model-00003-of-00004.safetensors",
208
+ "transformer.h.31.attn.c_attn.weight": "model-00003-of-00004.safetensors",
209
+ "transformer.h.31.attn.c_proj.weight": "model-00003-of-00004.safetensors",
210
+ "transformer.h.31.ln_1.weight": "model-00003-of-00004.safetensors",
211
+ "transformer.h.31.ln_2.weight": "model-00003-of-00004.safetensors",
212
+ "transformer.h.31.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
213
+ "transformer.h.31.mlp.w1.weight": "model-00003-of-00004.safetensors",
214
+ "transformer.h.31.mlp.w2.weight": "model-00003-of-00004.safetensors",
215
+ "transformer.h.4.attn.c_attn.bias": "model-00001-of-00004.safetensors",
216
+ "transformer.h.4.attn.c_attn.weight": "model-00001-of-00004.safetensors",
217
+ "transformer.h.4.attn.c_proj.weight": "model-00001-of-00004.safetensors",
218
+ "transformer.h.4.ln_1.weight": "model-00001-of-00004.safetensors",
219
+ "transformer.h.4.ln_2.weight": "model-00001-of-00004.safetensors",
220
+ "transformer.h.4.mlp.c_proj.weight": "model-00001-of-00004.safetensors",
221
+ "transformer.h.4.mlp.w1.weight": "model-00001-of-00004.safetensors",
222
+ "transformer.h.4.mlp.w2.weight": "model-00001-of-00004.safetensors",
223
+ "transformer.h.5.attn.c_attn.bias": "model-00001-of-00004.safetensors",
224
+ "transformer.h.5.attn.c_attn.weight": "model-00001-of-00004.safetensors",
225
+ "transformer.h.5.attn.c_proj.weight": "model-00001-of-00004.safetensors",
226
+ "transformer.h.5.ln_1.weight": "model-00001-of-00004.safetensors",
227
+ "transformer.h.5.ln_2.weight": "model-00001-of-00004.safetensors",
228
+ "transformer.h.5.mlp.c_proj.weight": "model-00001-of-00004.safetensors",
229
+ "transformer.h.5.mlp.w1.weight": "model-00001-of-00004.safetensors",
230
+ "transformer.h.5.mlp.w2.weight": "model-00001-of-00004.safetensors",
231
+ "transformer.h.6.attn.c_attn.bias": "model-00001-of-00004.safetensors",
232
+ "transformer.h.6.attn.c_attn.weight": "model-00001-of-00004.safetensors",
233
+ "transformer.h.6.attn.c_proj.weight": "model-00001-of-00004.safetensors",
234
+ "transformer.h.6.ln_1.weight": "model-00001-of-00004.safetensors",
235
+ "transformer.h.6.ln_2.weight": "model-00001-of-00004.safetensors",
236
+ "transformer.h.6.mlp.c_proj.weight": "model-00001-of-00004.safetensors",
237
+ "transformer.h.6.mlp.w1.weight": "model-00001-of-00004.safetensors",
238
+ "transformer.h.6.mlp.w2.weight": "model-00001-of-00004.safetensors",
239
+ "transformer.h.7.attn.c_attn.bias": "model-00001-of-00004.safetensors",
240
+ "transformer.h.7.attn.c_attn.weight": "model-00001-of-00004.safetensors",
241
+ "transformer.h.7.attn.c_proj.weight": "model-00001-of-00004.safetensors",
242
+ "transformer.h.7.ln_1.weight": "model-00001-of-00004.safetensors",
243
+ "transformer.h.7.ln_2.weight": "model-00001-of-00004.safetensors",
244
+ "transformer.h.7.mlp.c_proj.weight": "model-00001-of-00004.safetensors",
245
+ "transformer.h.7.mlp.w1.weight": "model-00001-of-00004.safetensors",
246
+ "transformer.h.7.mlp.w2.weight": "model-00001-of-00004.safetensors",
247
+ "transformer.h.8.attn.c_attn.bias": "model-00001-of-00004.safetensors",
248
+ "transformer.h.8.attn.c_attn.weight": "model-00001-of-00004.safetensors",
249
+ "transformer.h.8.attn.c_proj.weight": "model-00001-of-00004.safetensors",
250
+ "transformer.h.8.ln_1.weight": "model-00001-of-00004.safetensors",
251
+ "transformer.h.8.ln_2.weight": "model-00001-of-00004.safetensors",
252
+ "transformer.h.8.mlp.c_proj.weight": "model-00001-of-00004.safetensors",
253
+ "transformer.h.8.mlp.w1.weight": "model-00001-of-00004.safetensors",
254
+ "transformer.h.8.mlp.w2.weight": "model-00001-of-00004.safetensors",
255
+ "transformer.h.9.attn.c_attn.bias": "model-00001-of-00004.safetensors",
256
+ "transformer.h.9.attn.c_attn.weight": "model-00001-of-00004.safetensors",
257
+ "transformer.h.9.attn.c_proj.weight": "model-00002-of-00004.safetensors",
258
+ "transformer.h.9.ln_1.weight": "model-00001-of-00004.safetensors",
259
+ "transformer.h.9.ln_2.weight": "model-00002-of-00004.safetensors",
260
+ "transformer.h.9.mlp.c_proj.weight": "model-00002-of-00004.safetensors",
261
+ "transformer.h.9.mlp.w1.weight": "model-00002-of-00004.safetensors",
262
+ "transformer.h.9.mlp.w2.weight": "model-00002-of-00004.safetensors",
263
+ "transformer.ln_f.weight": "model-00003-of-00004.safetensors",
264
+ "transformer.visual.attn_pool.attn.in_proj_bias": "model-00004-of-00004.safetensors",
265
+ "transformer.visual.attn_pool.attn.in_proj_weight": "model-00004-of-00004.safetensors",
266
+ "transformer.visual.attn_pool.attn.out_proj.bias": "model-00004-of-00004.safetensors",
267
+ "transformer.visual.attn_pool.attn.out_proj.weight": "model-00004-of-00004.safetensors",
268
+ "transformer.visual.attn_pool.kv_proj.weight": "model-00004-of-00004.safetensors",
269
+ "transformer.visual.attn_pool.ln_kv.bias": "model-00004-of-00004.safetensors",
270
+ "transformer.visual.attn_pool.ln_kv.weight": "model-00004-of-00004.safetensors",
271
+ "transformer.visual.attn_pool.ln_q.bias": "model-00004-of-00004.safetensors",
272
+ "transformer.visual.attn_pool.ln_q.weight": "model-00004-of-00004.safetensors",
273
+ "transformer.visual.attn_pool.pos_embed": "model-00004-of-00004.safetensors",
274
+ "transformer.visual.attn_pool.query": "model-00004-of-00004.safetensors",
275
+ "transformer.visual.conv1.weight": "model-00003-of-00004.safetensors",
276
+ "transformer.visual.ln_post.bias": "model-00004-of-00004.safetensors",
277
+ "transformer.visual.ln_post.weight": "model-00004-of-00004.safetensors",
278
+ "transformer.visual.ln_pre.bias": "model-00003-of-00004.safetensors",
279
+ "transformer.visual.ln_pre.weight": "model-00003-of-00004.safetensors",
280
+ "transformer.visual.positional_embedding": "model-00003-of-00004.safetensors",
281
+ "transformer.visual.proj": "model-00003-of-00004.safetensors",
282
+ "transformer.visual.transformer.resblocks.0.attn.in_proj.bias": "model-00003-of-00004.safetensors",
283
+ "transformer.visual.transformer.resblocks.0.attn.in_proj.weight": "model-00003-of-00004.safetensors",
284
+ "transformer.visual.transformer.resblocks.0.attn.out_proj.bias": "model-00003-of-00004.safetensors",
285
+ "transformer.visual.transformer.resblocks.0.attn.out_proj.weight": "model-00003-of-00004.safetensors",
286
+ "transformer.visual.transformer.resblocks.0.ln_1.bias": "model-00003-of-00004.safetensors",
287
+ "transformer.visual.transformer.resblocks.0.ln_1.weight": "model-00003-of-00004.safetensors",
288
+ "transformer.visual.transformer.resblocks.0.ln_2.bias": "model-00003-of-00004.safetensors",
289
+ "transformer.visual.transformer.resblocks.0.ln_2.weight": "model-00003-of-00004.safetensors",
290
+ "transformer.visual.transformer.resblocks.0.mlp.c_fc.bias": "model-00003-of-00004.safetensors",
291
+ "transformer.visual.transformer.resblocks.0.mlp.c_fc.weight": "model-00003-of-00004.safetensors",
292
+ "transformer.visual.transformer.resblocks.0.mlp.c_proj.bias": "model-00003-of-00004.safetensors",
293
+ "transformer.visual.transformer.resblocks.0.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
294
+ "transformer.visual.transformer.resblocks.1.attn.in_proj.bias": "model-00003-of-00004.safetensors",
295
+ "transformer.visual.transformer.resblocks.1.attn.in_proj.weight": "model-00003-of-00004.safetensors",
296
+ "transformer.visual.transformer.resblocks.1.attn.out_proj.bias": "model-00003-of-00004.safetensors",
297
+ "transformer.visual.transformer.resblocks.1.attn.out_proj.weight": "model-00003-of-00004.safetensors",
298
+ "transformer.visual.transformer.resblocks.1.ln_1.bias": "model-00003-of-00004.safetensors",
299
+ "transformer.visual.transformer.resblocks.1.ln_1.weight": "model-00003-of-00004.safetensors",
300
+ "transformer.visual.transformer.resblocks.1.ln_2.bias": "model-00003-of-00004.safetensors",
301
+ "transformer.visual.transformer.resblocks.1.ln_2.weight": "model-00003-of-00004.safetensors",
302
+ "transformer.visual.transformer.resblocks.1.mlp.c_fc.bias": "model-00003-of-00004.safetensors",
303
+ "transformer.visual.transformer.resblocks.1.mlp.c_fc.weight": "model-00003-of-00004.safetensors",
304
+ "transformer.visual.transformer.resblocks.1.mlp.c_proj.bias": "model-00003-of-00004.safetensors",
305
+ "transformer.visual.transformer.resblocks.1.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
306
+ "transformer.visual.transformer.resblocks.10.attn.in_proj.bias": "model-00004-of-00004.safetensors",
307
+ "transformer.visual.transformer.resblocks.10.attn.in_proj.weight": "model-00004-of-00004.safetensors",
308
+ "transformer.visual.transformer.resblocks.10.attn.out_proj.bias": "model-00004-of-00004.safetensors",
309
+ "transformer.visual.transformer.resblocks.10.attn.out_proj.weight": "model-00004-of-00004.safetensors",
310
+ "transformer.visual.transformer.resblocks.10.ln_1.bias": "model-00004-of-00004.safetensors",
311
+ "transformer.visual.transformer.resblocks.10.ln_1.weight": "model-00004-of-00004.safetensors",
312
+ "transformer.visual.transformer.resblocks.10.ln_2.bias": "model-00004-of-00004.safetensors",
313
+ "transformer.visual.transformer.resblocks.10.ln_2.weight": "model-00004-of-00004.safetensors",
314
+ "transformer.visual.transformer.resblocks.10.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
315
+ "transformer.visual.transformer.resblocks.10.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
316
+ "transformer.visual.transformer.resblocks.10.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
317
+ "transformer.visual.transformer.resblocks.10.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
318
+ "transformer.visual.transformer.resblocks.11.attn.in_proj.bias": "model-00004-of-00004.safetensors",
319
+ "transformer.visual.transformer.resblocks.11.attn.in_proj.weight": "model-00004-of-00004.safetensors",
320
+ "transformer.visual.transformer.resblocks.11.attn.out_proj.bias": "model-00004-of-00004.safetensors",
321
+ "transformer.visual.transformer.resblocks.11.attn.out_proj.weight": "model-00004-of-00004.safetensors",
322
+ "transformer.visual.transformer.resblocks.11.ln_1.bias": "model-00004-of-00004.safetensors",
323
+ "transformer.visual.transformer.resblocks.11.ln_1.weight": "model-00004-of-00004.safetensors",
324
+ "transformer.visual.transformer.resblocks.11.ln_2.bias": "model-00004-of-00004.safetensors",
325
+ "transformer.visual.transformer.resblocks.11.ln_2.weight": "model-00004-of-00004.safetensors",
326
+ "transformer.visual.transformer.resblocks.11.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
327
+ "transformer.visual.transformer.resblocks.11.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
328
+ "transformer.visual.transformer.resblocks.11.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
329
+ "transformer.visual.transformer.resblocks.11.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
330
+ "transformer.visual.transformer.resblocks.12.attn.in_proj.bias": "model-00004-of-00004.safetensors",
331
+ "transformer.visual.transformer.resblocks.12.attn.in_proj.weight": "model-00004-of-00004.safetensors",
332
+ "transformer.visual.transformer.resblocks.12.attn.out_proj.bias": "model-00004-of-00004.safetensors",
333
+ "transformer.visual.transformer.resblocks.12.attn.out_proj.weight": "model-00004-of-00004.safetensors",
334
+ "transformer.visual.transformer.resblocks.12.ln_1.bias": "model-00004-of-00004.safetensors",
335
+ "transformer.visual.transformer.resblocks.12.ln_1.weight": "model-00004-of-00004.safetensors",
336
+ "transformer.visual.transformer.resblocks.12.ln_2.bias": "model-00004-of-00004.safetensors",
337
+ "transformer.visual.transformer.resblocks.12.ln_2.weight": "model-00004-of-00004.safetensors",
338
+ "transformer.visual.transformer.resblocks.12.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
339
+ "transformer.visual.transformer.resblocks.12.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
340
+ "transformer.visual.transformer.resblocks.12.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
341
+ "transformer.visual.transformer.resblocks.12.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
342
+ "transformer.visual.transformer.resblocks.13.attn.in_proj.bias": "model-00004-of-00004.safetensors",
343
+ "transformer.visual.transformer.resblocks.13.attn.in_proj.weight": "model-00004-of-00004.safetensors",
344
+ "transformer.visual.transformer.resblocks.13.attn.out_proj.bias": "model-00004-of-00004.safetensors",
345
+ "transformer.visual.transformer.resblocks.13.attn.out_proj.weight": "model-00004-of-00004.safetensors",
346
+ "transformer.visual.transformer.resblocks.13.ln_1.bias": "model-00004-of-00004.safetensors",
347
+ "transformer.visual.transformer.resblocks.13.ln_1.weight": "model-00004-of-00004.safetensors",
348
+ "transformer.visual.transformer.resblocks.13.ln_2.bias": "model-00004-of-00004.safetensors",
349
+ "transformer.visual.transformer.resblocks.13.ln_2.weight": "model-00004-of-00004.safetensors",
350
+ "transformer.visual.transformer.resblocks.13.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
351
+ "transformer.visual.transformer.resblocks.13.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
352
+ "transformer.visual.transformer.resblocks.13.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
353
+ "transformer.visual.transformer.resblocks.13.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
354
+ "transformer.visual.transformer.resblocks.14.attn.in_proj.bias": "model-00004-of-00004.safetensors",
355
+ "transformer.visual.transformer.resblocks.14.attn.in_proj.weight": "model-00004-of-00004.safetensors",
356
+ "transformer.visual.transformer.resblocks.14.attn.out_proj.bias": "model-00004-of-00004.safetensors",
357
+ "transformer.visual.transformer.resblocks.14.attn.out_proj.weight": "model-00004-of-00004.safetensors",
358
+ "transformer.visual.transformer.resblocks.14.ln_1.bias": "model-00004-of-00004.safetensors",
359
+ "transformer.visual.transformer.resblocks.14.ln_1.weight": "model-00004-of-00004.safetensors",
360
+ "transformer.visual.transformer.resblocks.14.ln_2.bias": "model-00004-of-00004.safetensors",
361
+ "transformer.visual.transformer.resblocks.14.ln_2.weight": "model-00004-of-00004.safetensors",
362
+ "transformer.visual.transformer.resblocks.14.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
363
+ "transformer.visual.transformer.resblocks.14.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
364
+ "transformer.visual.transformer.resblocks.14.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
365
+ "transformer.visual.transformer.resblocks.14.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
366
+ "transformer.visual.transformer.resblocks.15.attn.in_proj.bias": "model-00004-of-00004.safetensors",
367
+ "transformer.visual.transformer.resblocks.15.attn.in_proj.weight": "model-00004-of-00004.safetensors",
368
+ "transformer.visual.transformer.resblocks.15.attn.out_proj.bias": "model-00004-of-00004.safetensors",
369
+ "transformer.visual.transformer.resblocks.15.attn.out_proj.weight": "model-00004-of-00004.safetensors",
370
+ "transformer.visual.transformer.resblocks.15.ln_1.bias": "model-00004-of-00004.safetensors",
371
+ "transformer.visual.transformer.resblocks.15.ln_1.weight": "model-00004-of-00004.safetensors",
372
+ "transformer.visual.transformer.resblocks.15.ln_2.bias": "model-00004-of-00004.safetensors",
373
+ "transformer.visual.transformer.resblocks.15.ln_2.weight": "model-00004-of-00004.safetensors",
374
+ "transformer.visual.transformer.resblocks.15.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
375
+ "transformer.visual.transformer.resblocks.15.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
376
+ "transformer.visual.transformer.resblocks.15.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
377
+ "transformer.visual.transformer.resblocks.15.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
378
+ "transformer.visual.transformer.resblocks.16.attn.in_proj.bias": "model-00004-of-00004.safetensors",
379
+ "transformer.visual.transformer.resblocks.16.attn.in_proj.weight": "model-00004-of-00004.safetensors",
380
+ "transformer.visual.transformer.resblocks.16.attn.out_proj.bias": "model-00004-of-00004.safetensors",
381
+ "transformer.visual.transformer.resblocks.16.attn.out_proj.weight": "model-00004-of-00004.safetensors",
382
+ "transformer.visual.transformer.resblocks.16.ln_1.bias": "model-00004-of-00004.safetensors",
383
+ "transformer.visual.transformer.resblocks.16.ln_1.weight": "model-00004-of-00004.safetensors",
384
+ "transformer.visual.transformer.resblocks.16.ln_2.bias": "model-00004-of-00004.safetensors",
385
+ "transformer.visual.transformer.resblocks.16.ln_2.weight": "model-00004-of-00004.safetensors",
386
+ "transformer.visual.transformer.resblocks.16.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
387
+ "transformer.visual.transformer.resblocks.16.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
388
+ "transformer.visual.transformer.resblocks.16.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
389
+ "transformer.visual.transformer.resblocks.16.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
390
+ "transformer.visual.transformer.resblocks.17.attn.in_proj.bias": "model-00004-of-00004.safetensors",
391
+ "transformer.visual.transformer.resblocks.17.attn.in_proj.weight": "model-00004-of-00004.safetensors",
392
+ "transformer.visual.transformer.resblocks.17.attn.out_proj.bias": "model-00004-of-00004.safetensors",
393
+ "transformer.visual.transformer.resblocks.17.attn.out_proj.weight": "model-00004-of-00004.safetensors",
394
+ "transformer.visual.transformer.resblocks.17.ln_1.bias": "model-00004-of-00004.safetensors",
395
+ "transformer.visual.transformer.resblocks.17.ln_1.weight": "model-00004-of-00004.safetensors",
396
+ "transformer.visual.transformer.resblocks.17.ln_2.bias": "model-00004-of-00004.safetensors",
397
+ "transformer.visual.transformer.resblocks.17.ln_2.weight": "model-00004-of-00004.safetensors",
398
+ "transformer.visual.transformer.resblocks.17.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
399
+ "transformer.visual.transformer.resblocks.17.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
400
+ "transformer.visual.transformer.resblocks.17.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
401
+ "transformer.visual.transformer.resblocks.17.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
402
+ "transformer.visual.transformer.resblocks.18.attn.in_proj.bias": "model-00004-of-00004.safetensors",
403
+ "transformer.visual.transformer.resblocks.18.attn.in_proj.weight": "model-00004-of-00004.safetensors",
404
+ "transformer.visual.transformer.resblocks.18.attn.out_proj.bias": "model-00004-of-00004.safetensors",
405
+ "transformer.visual.transformer.resblocks.18.attn.out_proj.weight": "model-00004-of-00004.safetensors",
406
+ "transformer.visual.transformer.resblocks.18.ln_1.bias": "model-00004-of-00004.safetensors",
407
+ "transformer.visual.transformer.resblocks.18.ln_1.weight": "model-00004-of-00004.safetensors",
408
+ "transformer.visual.transformer.resblocks.18.ln_2.bias": "model-00004-of-00004.safetensors",
409
+ "transformer.visual.transformer.resblocks.18.ln_2.weight": "model-00004-of-00004.safetensors",
410
+ "transformer.visual.transformer.resblocks.18.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
411
+ "transformer.visual.transformer.resblocks.18.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
412
+ "transformer.visual.transformer.resblocks.18.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
413
+ "transformer.visual.transformer.resblocks.18.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
414
+ "transformer.visual.transformer.resblocks.19.attn.in_proj.bias": "model-00004-of-00004.safetensors",
415
+ "transformer.visual.transformer.resblocks.19.attn.in_proj.weight": "model-00004-of-00004.safetensors",
416
+ "transformer.visual.transformer.resblocks.19.attn.out_proj.bias": "model-00004-of-00004.safetensors",
417
+ "transformer.visual.transformer.resblocks.19.attn.out_proj.weight": "model-00004-of-00004.safetensors",
418
+ "transformer.visual.transformer.resblocks.19.ln_1.bias": "model-00004-of-00004.safetensors",
419
+ "transformer.visual.transformer.resblocks.19.ln_1.weight": "model-00004-of-00004.safetensors",
420
+ "transformer.visual.transformer.resblocks.19.ln_2.bias": "model-00004-of-00004.safetensors",
421
+ "transformer.visual.transformer.resblocks.19.ln_2.weight": "model-00004-of-00004.safetensors",
422
+ "transformer.visual.transformer.resblocks.19.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
423
+ "transformer.visual.transformer.resblocks.19.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
424
+ "transformer.visual.transformer.resblocks.19.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
425
+ "transformer.visual.transformer.resblocks.19.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
426
+ "transformer.visual.transformer.resblocks.2.attn.in_proj.bias": "model-00003-of-00004.safetensors",
427
+ "transformer.visual.transformer.resblocks.2.attn.in_proj.weight": "model-00003-of-00004.safetensors",
428
+ "transformer.visual.transformer.resblocks.2.attn.out_proj.bias": "model-00003-of-00004.safetensors",
429
+ "transformer.visual.transformer.resblocks.2.attn.out_proj.weight": "model-00003-of-00004.safetensors",
430
+ "transformer.visual.transformer.resblocks.2.ln_1.bias": "model-00003-of-00004.safetensors",
431
+ "transformer.visual.transformer.resblocks.2.ln_1.weight": "model-00003-of-00004.safetensors",
432
+ "transformer.visual.transformer.resblocks.2.ln_2.bias": "model-00003-of-00004.safetensors",
433
+ "transformer.visual.transformer.resblocks.2.ln_2.weight": "model-00003-of-00004.safetensors",
434
+ "transformer.visual.transformer.resblocks.2.mlp.c_fc.bias": "model-00003-of-00004.safetensors",
435
+ "transformer.visual.transformer.resblocks.2.mlp.c_fc.weight": "model-00003-of-00004.safetensors",
436
+ "transformer.visual.transformer.resblocks.2.mlp.c_proj.bias": "model-00003-of-00004.safetensors",
437
+ "transformer.visual.transformer.resblocks.2.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
438
+ "transformer.visual.transformer.resblocks.20.attn.in_proj.bias": "model-00004-of-00004.safetensors",
439
+ "transformer.visual.transformer.resblocks.20.attn.in_proj.weight": "model-00004-of-00004.safetensors",
440
+ "transformer.visual.transformer.resblocks.20.attn.out_proj.bias": "model-00004-of-00004.safetensors",
441
+ "transformer.visual.transformer.resblocks.20.attn.out_proj.weight": "model-00004-of-00004.safetensors",
442
+ "transformer.visual.transformer.resblocks.20.ln_1.bias": "model-00004-of-00004.safetensors",
443
+ "transformer.visual.transformer.resblocks.20.ln_1.weight": "model-00004-of-00004.safetensors",
444
+ "transformer.visual.transformer.resblocks.20.ln_2.bias": "model-00004-of-00004.safetensors",
445
+ "transformer.visual.transformer.resblocks.20.ln_2.weight": "model-00004-of-00004.safetensors",
446
+ "transformer.visual.transformer.resblocks.20.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
447
+ "transformer.visual.transformer.resblocks.20.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
448
+ "transformer.visual.transformer.resblocks.20.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
449
+ "transformer.visual.transformer.resblocks.20.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
450
+ "transformer.visual.transformer.resblocks.21.attn.in_proj.bias": "model-00004-of-00004.safetensors",
451
+ "transformer.visual.transformer.resblocks.21.attn.in_proj.weight": "model-00004-of-00004.safetensors",
452
+ "transformer.visual.transformer.resblocks.21.attn.out_proj.bias": "model-00004-of-00004.safetensors",
453
+ "transformer.visual.transformer.resblocks.21.attn.out_proj.weight": "model-00004-of-00004.safetensors",
454
+ "transformer.visual.transformer.resblocks.21.ln_1.bias": "model-00004-of-00004.safetensors",
455
+ "transformer.visual.transformer.resblocks.21.ln_1.weight": "model-00004-of-00004.safetensors",
456
+ "transformer.visual.transformer.resblocks.21.ln_2.bias": "model-00004-of-00004.safetensors",
457
+ "transformer.visual.transformer.resblocks.21.ln_2.weight": "model-00004-of-00004.safetensors",
458
+ "transformer.visual.transformer.resblocks.21.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
459
+ "transformer.visual.transformer.resblocks.21.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
460
+ "transformer.visual.transformer.resblocks.21.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
461
+ "transformer.visual.transformer.resblocks.21.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
462
+ "transformer.visual.transformer.resblocks.22.attn.in_proj.bias": "model-00004-of-00004.safetensors",
463
+ "transformer.visual.transformer.resblocks.22.attn.in_proj.weight": "model-00004-of-00004.safetensors",
464
+ "transformer.visual.transformer.resblocks.22.attn.out_proj.bias": "model-00004-of-00004.safetensors",
465
+ "transformer.visual.transformer.resblocks.22.attn.out_proj.weight": "model-00004-of-00004.safetensors",
466
+ "transformer.visual.transformer.resblocks.22.ln_1.bias": "model-00004-of-00004.safetensors",
467
+ "transformer.visual.transformer.resblocks.22.ln_1.weight": "model-00004-of-00004.safetensors",
468
+ "transformer.visual.transformer.resblocks.22.ln_2.bias": "model-00004-of-00004.safetensors",
469
+ "transformer.visual.transformer.resblocks.22.ln_2.weight": "model-00004-of-00004.safetensors",
470
+ "transformer.visual.transformer.resblocks.22.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
471
+ "transformer.visual.transformer.resblocks.22.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
472
+ "transformer.visual.transformer.resblocks.22.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
473
+ "transformer.visual.transformer.resblocks.22.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
474
+ "transformer.visual.transformer.resblocks.23.attn.in_proj.bias": "model-00004-of-00004.safetensors",
475
+ "transformer.visual.transformer.resblocks.23.attn.in_proj.weight": "model-00004-of-00004.safetensors",
476
+ "transformer.visual.transformer.resblocks.23.attn.out_proj.bias": "model-00004-of-00004.safetensors",
477
+ "transformer.visual.transformer.resblocks.23.attn.out_proj.weight": "model-00004-of-00004.safetensors",
478
+ "transformer.visual.transformer.resblocks.23.ln_1.bias": "model-00004-of-00004.safetensors",
479
+ "transformer.visual.transformer.resblocks.23.ln_1.weight": "model-00004-of-00004.safetensors",
480
+ "transformer.visual.transformer.resblocks.23.ln_2.bias": "model-00004-of-00004.safetensors",
481
+ "transformer.visual.transformer.resblocks.23.ln_2.weight": "model-00004-of-00004.safetensors",
482
+ "transformer.visual.transformer.resblocks.23.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
483
+ "transformer.visual.transformer.resblocks.23.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
484
+ "transformer.visual.transformer.resblocks.23.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
485
+ "transformer.visual.transformer.resblocks.23.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
486
+ "transformer.visual.transformer.resblocks.24.attn.in_proj.bias": "model-00004-of-00004.safetensors",
487
+ "transformer.visual.transformer.resblocks.24.attn.in_proj.weight": "model-00004-of-00004.safetensors",
488
+ "transformer.visual.transformer.resblocks.24.attn.out_proj.bias": "model-00004-of-00004.safetensors",
489
+ "transformer.visual.transformer.resblocks.24.attn.out_proj.weight": "model-00004-of-00004.safetensors",
490
+ "transformer.visual.transformer.resblocks.24.ln_1.bias": "model-00004-of-00004.safetensors",
491
+ "transformer.visual.transformer.resblocks.24.ln_1.weight": "model-00004-of-00004.safetensors",
492
+ "transformer.visual.transformer.resblocks.24.ln_2.bias": "model-00004-of-00004.safetensors",
493
+ "transformer.visual.transformer.resblocks.24.ln_2.weight": "model-00004-of-00004.safetensors",
494
+ "transformer.visual.transformer.resblocks.24.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
495
+ "transformer.visual.transformer.resblocks.24.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
496
+ "transformer.visual.transformer.resblocks.24.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
497
+ "transformer.visual.transformer.resblocks.24.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
498
+ "transformer.visual.transformer.resblocks.25.attn.in_proj.bias": "model-00004-of-00004.safetensors",
499
+ "transformer.visual.transformer.resblocks.25.attn.in_proj.weight": "model-00004-of-00004.safetensors",
500
+ "transformer.visual.transformer.resblocks.25.attn.out_proj.bias": "model-00004-of-00004.safetensors",
501
+ "transformer.visual.transformer.resblocks.25.attn.out_proj.weight": "model-00004-of-00004.safetensors",
502
+ "transformer.visual.transformer.resblocks.25.ln_1.bias": "model-00004-of-00004.safetensors",
503
+ "transformer.visual.transformer.resblocks.25.ln_1.weight": "model-00004-of-00004.safetensors",
504
+ "transformer.visual.transformer.resblocks.25.ln_2.bias": "model-00004-of-00004.safetensors",
505
+ "transformer.visual.transformer.resblocks.25.ln_2.weight": "model-00004-of-00004.safetensors",
506
+ "transformer.visual.transformer.resblocks.25.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
507
+ "transformer.visual.transformer.resblocks.25.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
508
+ "transformer.visual.transformer.resblocks.25.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
509
+ "transformer.visual.transformer.resblocks.25.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
510
+ "transformer.visual.transformer.resblocks.26.attn.in_proj.bias": "model-00004-of-00004.safetensors",
511
+ "transformer.visual.transformer.resblocks.26.attn.in_proj.weight": "model-00004-of-00004.safetensors",
512
+ "transformer.visual.transformer.resblocks.26.attn.out_proj.bias": "model-00004-of-00004.safetensors",
513
+ "transformer.visual.transformer.resblocks.26.attn.out_proj.weight": "model-00004-of-00004.safetensors",
514
+ "transformer.visual.transformer.resblocks.26.ln_1.bias": "model-00004-of-00004.safetensors",
515
+ "transformer.visual.transformer.resblocks.26.ln_1.weight": "model-00004-of-00004.safetensors",
516
+ "transformer.visual.transformer.resblocks.26.ln_2.bias": "model-00004-of-00004.safetensors",
517
+ "transformer.visual.transformer.resblocks.26.ln_2.weight": "model-00004-of-00004.safetensors",
518
+ "transformer.visual.transformer.resblocks.26.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
519
+ "transformer.visual.transformer.resblocks.26.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
520
+ "transformer.visual.transformer.resblocks.26.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
521
+ "transformer.visual.transformer.resblocks.26.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
522
+ "transformer.visual.transformer.resblocks.27.attn.in_proj.bias": "model-00004-of-00004.safetensors",
523
+ "transformer.visual.transformer.resblocks.27.attn.in_proj.weight": "model-00004-of-00004.safetensors",
524
+ "transformer.visual.transformer.resblocks.27.attn.out_proj.bias": "model-00004-of-00004.safetensors",
525
+ "transformer.visual.transformer.resblocks.27.attn.out_proj.weight": "model-00004-of-00004.safetensors",
526
+ "transformer.visual.transformer.resblocks.27.ln_1.bias": "model-00004-of-00004.safetensors",
527
+ "transformer.visual.transformer.resblocks.27.ln_1.weight": "model-00004-of-00004.safetensors",
528
+ "transformer.visual.transformer.resblocks.27.ln_2.bias": "model-00004-of-00004.safetensors",
529
+ "transformer.visual.transformer.resblocks.27.ln_2.weight": "model-00004-of-00004.safetensors",
530
+ "transformer.visual.transformer.resblocks.27.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
531
+ "transformer.visual.transformer.resblocks.27.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
532
+ "transformer.visual.transformer.resblocks.27.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
533
+ "transformer.visual.transformer.resblocks.27.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
534
+ "transformer.visual.transformer.resblocks.28.attn.in_proj.bias": "model-00004-of-00004.safetensors",
535
+ "transformer.visual.transformer.resblocks.28.attn.in_proj.weight": "model-00004-of-00004.safetensors",
536
+ "transformer.visual.transformer.resblocks.28.attn.out_proj.bias": "model-00004-of-00004.safetensors",
537
+ "transformer.visual.transformer.resblocks.28.attn.out_proj.weight": "model-00004-of-00004.safetensors",
538
+ "transformer.visual.transformer.resblocks.28.ln_1.bias": "model-00004-of-00004.safetensors",
539
+ "transformer.visual.transformer.resblocks.28.ln_1.weight": "model-00004-of-00004.safetensors",
540
+ "transformer.visual.transformer.resblocks.28.ln_2.bias": "model-00004-of-00004.safetensors",
541
+ "transformer.visual.transformer.resblocks.28.ln_2.weight": "model-00004-of-00004.safetensors",
542
+ "transformer.visual.transformer.resblocks.28.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
543
+ "transformer.visual.transformer.resblocks.28.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
544
+ "transformer.visual.transformer.resblocks.28.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
545
+ "transformer.visual.transformer.resblocks.28.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
546
+ "transformer.visual.transformer.resblocks.29.attn.in_proj.bias": "model-00004-of-00004.safetensors",
547
+ "transformer.visual.transformer.resblocks.29.attn.in_proj.weight": "model-00004-of-00004.safetensors",
548
+ "transformer.visual.transformer.resblocks.29.attn.out_proj.bias": "model-00004-of-00004.safetensors",
549
+ "transformer.visual.transformer.resblocks.29.attn.out_proj.weight": "model-00004-of-00004.safetensors",
550
+ "transformer.visual.transformer.resblocks.29.ln_1.bias": "model-00004-of-00004.safetensors",
551
+ "transformer.visual.transformer.resblocks.29.ln_1.weight": "model-00004-of-00004.safetensors",
552
+ "transformer.visual.transformer.resblocks.29.ln_2.bias": "model-00004-of-00004.safetensors",
553
+ "transformer.visual.transformer.resblocks.29.ln_2.weight": "model-00004-of-00004.safetensors",
554
+ "transformer.visual.transformer.resblocks.29.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
555
+ "transformer.visual.transformer.resblocks.29.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
556
+ "transformer.visual.transformer.resblocks.29.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
557
+ "transformer.visual.transformer.resblocks.29.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
558
+ "transformer.visual.transformer.resblocks.3.attn.in_proj.bias": "model-00003-of-00004.safetensors",
559
+ "transformer.visual.transformer.resblocks.3.attn.in_proj.weight": "model-00003-of-00004.safetensors",
560
+ "transformer.visual.transformer.resblocks.3.attn.out_proj.bias": "model-00003-of-00004.safetensors",
561
+ "transformer.visual.transformer.resblocks.3.attn.out_proj.weight": "model-00003-of-00004.safetensors",
562
+ "transformer.visual.transformer.resblocks.3.ln_1.bias": "model-00003-of-00004.safetensors",
563
+ "transformer.visual.transformer.resblocks.3.ln_1.weight": "model-00003-of-00004.safetensors",
564
+ "transformer.visual.transformer.resblocks.3.ln_2.bias": "model-00003-of-00004.safetensors",
565
+ "transformer.visual.transformer.resblocks.3.ln_2.weight": "model-00003-of-00004.safetensors",
566
+ "transformer.visual.transformer.resblocks.3.mlp.c_fc.bias": "model-00003-of-00004.safetensors",
567
+ "transformer.visual.transformer.resblocks.3.mlp.c_fc.weight": "model-00003-of-00004.safetensors",
568
+ "transformer.visual.transformer.resblocks.3.mlp.c_proj.bias": "model-00003-of-00004.safetensors",
569
+ "transformer.visual.transformer.resblocks.3.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
570
+ "transformer.visual.transformer.resblocks.30.attn.in_proj.bias": "model-00004-of-00004.safetensors",
571
+ "transformer.visual.transformer.resblocks.30.attn.in_proj.weight": "model-00004-of-00004.safetensors",
572
+ "transformer.visual.transformer.resblocks.30.attn.out_proj.bias": "model-00004-of-00004.safetensors",
573
+ "transformer.visual.transformer.resblocks.30.attn.out_proj.weight": "model-00004-of-00004.safetensors",
574
+ "transformer.visual.transformer.resblocks.30.ln_1.bias": "model-00004-of-00004.safetensors",
575
+ "transformer.visual.transformer.resblocks.30.ln_1.weight": "model-00004-of-00004.safetensors",
576
+ "transformer.visual.transformer.resblocks.30.ln_2.bias": "model-00004-of-00004.safetensors",
577
+ "transformer.visual.transformer.resblocks.30.ln_2.weight": "model-00004-of-00004.safetensors",
578
+ "transformer.visual.transformer.resblocks.30.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
579
+ "transformer.visual.transformer.resblocks.30.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
580
+ "transformer.visual.transformer.resblocks.30.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
581
+ "transformer.visual.transformer.resblocks.30.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
582
+ "transformer.visual.transformer.resblocks.31.attn.in_proj.bias": "model-00004-of-00004.safetensors",
583
+ "transformer.visual.transformer.resblocks.31.attn.in_proj.weight": "model-00004-of-00004.safetensors",
584
+ "transformer.visual.transformer.resblocks.31.attn.out_proj.bias": "model-00004-of-00004.safetensors",
585
+ "transformer.visual.transformer.resblocks.31.attn.out_proj.weight": "model-00004-of-00004.safetensors",
586
+ "transformer.visual.transformer.resblocks.31.ln_1.bias": "model-00004-of-00004.safetensors",
587
+ "transformer.visual.transformer.resblocks.31.ln_1.weight": "model-00004-of-00004.safetensors",
588
+ "transformer.visual.transformer.resblocks.31.ln_2.bias": "model-00004-of-00004.safetensors",
589
+ "transformer.visual.transformer.resblocks.31.ln_2.weight": "model-00004-of-00004.safetensors",
590
+ "transformer.visual.transformer.resblocks.31.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
591
+ "transformer.visual.transformer.resblocks.31.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
592
+ "transformer.visual.transformer.resblocks.31.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
593
+ "transformer.visual.transformer.resblocks.31.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
594
+ "transformer.visual.transformer.resblocks.32.attn.in_proj.bias": "model-00004-of-00004.safetensors",
595
+ "transformer.visual.transformer.resblocks.32.attn.in_proj.weight": "model-00004-of-00004.safetensors",
596
+ "transformer.visual.transformer.resblocks.32.attn.out_proj.bias": "model-00004-of-00004.safetensors",
597
+ "transformer.visual.transformer.resblocks.32.attn.out_proj.weight": "model-00004-of-00004.safetensors",
598
+ "transformer.visual.transformer.resblocks.32.ln_1.bias": "model-00004-of-00004.safetensors",
599
+ "transformer.visual.transformer.resblocks.32.ln_1.weight": "model-00004-of-00004.safetensors",
600
+ "transformer.visual.transformer.resblocks.32.ln_2.bias": "model-00004-of-00004.safetensors",
601
+ "transformer.visual.transformer.resblocks.32.ln_2.weight": "model-00004-of-00004.safetensors",
602
+ "transformer.visual.transformer.resblocks.32.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
603
+ "transformer.visual.transformer.resblocks.32.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
604
+ "transformer.visual.transformer.resblocks.32.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
605
+ "transformer.visual.transformer.resblocks.32.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
606
+ "transformer.visual.transformer.resblocks.33.attn.in_proj.bias": "model-00004-of-00004.safetensors",
607
+ "transformer.visual.transformer.resblocks.33.attn.in_proj.weight": "model-00004-of-00004.safetensors",
608
+ "transformer.visual.transformer.resblocks.33.attn.out_proj.bias": "model-00004-of-00004.safetensors",
609
+ "transformer.visual.transformer.resblocks.33.attn.out_proj.weight": "model-00004-of-00004.safetensors",
610
+ "transformer.visual.transformer.resblocks.33.ln_1.bias": "model-00004-of-00004.safetensors",
611
+ "transformer.visual.transformer.resblocks.33.ln_1.weight": "model-00004-of-00004.safetensors",
612
+ "transformer.visual.transformer.resblocks.33.ln_2.bias": "model-00004-of-00004.safetensors",
613
+ "transformer.visual.transformer.resblocks.33.ln_2.weight": "model-00004-of-00004.safetensors",
614
+ "transformer.visual.transformer.resblocks.33.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
615
+ "transformer.visual.transformer.resblocks.33.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
616
+ "transformer.visual.transformer.resblocks.33.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
617
+ "transformer.visual.transformer.resblocks.33.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
618
+ "transformer.visual.transformer.resblocks.34.attn.in_proj.bias": "model-00004-of-00004.safetensors",
619
+ "transformer.visual.transformer.resblocks.34.attn.in_proj.weight": "model-00004-of-00004.safetensors",
620
+ "transformer.visual.transformer.resblocks.34.attn.out_proj.bias": "model-00004-of-00004.safetensors",
621
+ "transformer.visual.transformer.resblocks.34.attn.out_proj.weight": "model-00004-of-00004.safetensors",
622
+ "transformer.visual.transformer.resblocks.34.ln_1.bias": "model-00004-of-00004.safetensors",
623
+ "transformer.visual.transformer.resblocks.34.ln_1.weight": "model-00004-of-00004.safetensors",
624
+ "transformer.visual.transformer.resblocks.34.ln_2.bias": "model-00004-of-00004.safetensors",
625
+ "transformer.visual.transformer.resblocks.34.ln_2.weight": "model-00004-of-00004.safetensors",
626
+ "transformer.visual.transformer.resblocks.34.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
627
+ "transformer.visual.transformer.resblocks.34.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
628
+ "transformer.visual.transformer.resblocks.34.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
629
+ "transformer.visual.transformer.resblocks.34.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
630
+ "transformer.visual.transformer.resblocks.35.attn.in_proj.bias": "model-00004-of-00004.safetensors",
631
+ "transformer.visual.transformer.resblocks.35.attn.in_proj.weight": "model-00004-of-00004.safetensors",
632
+ "transformer.visual.transformer.resblocks.35.attn.out_proj.bias": "model-00004-of-00004.safetensors",
633
+ "transformer.visual.transformer.resblocks.35.attn.out_proj.weight": "model-00004-of-00004.safetensors",
634
+ "transformer.visual.transformer.resblocks.35.ln_1.bias": "model-00004-of-00004.safetensors",
635
+ "transformer.visual.transformer.resblocks.35.ln_1.weight": "model-00004-of-00004.safetensors",
636
+ "transformer.visual.transformer.resblocks.35.ln_2.bias": "model-00004-of-00004.safetensors",
637
+ "transformer.visual.transformer.resblocks.35.ln_2.weight": "model-00004-of-00004.safetensors",
638
+ "transformer.visual.transformer.resblocks.35.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
639
+ "transformer.visual.transformer.resblocks.35.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
640
+ "transformer.visual.transformer.resblocks.35.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
641
+ "transformer.visual.transformer.resblocks.35.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
642
+ "transformer.visual.transformer.resblocks.36.attn.in_proj.bias": "model-00004-of-00004.safetensors",
643
+ "transformer.visual.transformer.resblocks.36.attn.in_proj.weight": "model-00004-of-00004.safetensors",
644
+ "transformer.visual.transformer.resblocks.36.attn.out_proj.bias": "model-00004-of-00004.safetensors",
645
+ "transformer.visual.transformer.resblocks.36.attn.out_proj.weight": "model-00004-of-00004.safetensors",
646
+ "transformer.visual.transformer.resblocks.36.ln_1.bias": "model-00004-of-00004.safetensors",
647
+ "transformer.visual.transformer.resblocks.36.ln_1.weight": "model-00004-of-00004.safetensors",
648
+ "transformer.visual.transformer.resblocks.36.ln_2.bias": "model-00004-of-00004.safetensors",
649
+ "transformer.visual.transformer.resblocks.36.ln_2.weight": "model-00004-of-00004.safetensors",
650
+ "transformer.visual.transformer.resblocks.36.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
651
+ "transformer.visual.transformer.resblocks.36.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
652
+ "transformer.visual.transformer.resblocks.36.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
653
+ "transformer.visual.transformer.resblocks.36.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
654
+ "transformer.visual.transformer.resblocks.37.attn.in_proj.bias": "model-00004-of-00004.safetensors",
655
+ "transformer.visual.transformer.resblocks.37.attn.in_proj.weight": "model-00004-of-00004.safetensors",
656
+ "transformer.visual.transformer.resblocks.37.attn.out_proj.bias": "model-00004-of-00004.safetensors",
657
+ "transformer.visual.transformer.resblocks.37.attn.out_proj.weight": "model-00004-of-00004.safetensors",
658
+ "transformer.visual.transformer.resblocks.37.ln_1.bias": "model-00004-of-00004.safetensors",
659
+ "transformer.visual.transformer.resblocks.37.ln_1.weight": "model-00004-of-00004.safetensors",
660
+ "transformer.visual.transformer.resblocks.37.ln_2.bias": "model-00004-of-00004.safetensors",
661
+ "transformer.visual.transformer.resblocks.37.ln_2.weight": "model-00004-of-00004.safetensors",
662
+ "transformer.visual.transformer.resblocks.37.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
663
+ "transformer.visual.transformer.resblocks.37.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
664
+ "transformer.visual.transformer.resblocks.37.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
665
+ "transformer.visual.transformer.resblocks.37.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
666
+ "transformer.visual.transformer.resblocks.38.attn.in_proj.bias": "model-00004-of-00004.safetensors",
667
+ "transformer.visual.transformer.resblocks.38.attn.in_proj.weight": "model-00004-of-00004.safetensors",
668
+ "transformer.visual.transformer.resblocks.38.attn.out_proj.bias": "model-00004-of-00004.safetensors",
669
+ "transformer.visual.transformer.resblocks.38.attn.out_proj.weight": "model-00004-of-00004.safetensors",
670
+ "transformer.visual.transformer.resblocks.38.ln_1.bias": "model-00004-of-00004.safetensors",
671
+ "transformer.visual.transformer.resblocks.38.ln_1.weight": "model-00004-of-00004.safetensors",
672
+ "transformer.visual.transformer.resblocks.38.ln_2.bias": "model-00004-of-00004.safetensors",
673
+ "transformer.visual.transformer.resblocks.38.ln_2.weight": "model-00004-of-00004.safetensors",
674
+ "transformer.visual.transformer.resblocks.38.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
675
+ "transformer.visual.transformer.resblocks.38.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
676
+ "transformer.visual.transformer.resblocks.38.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
677
+ "transformer.visual.transformer.resblocks.38.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
678
+ "transformer.visual.transformer.resblocks.39.attn.in_proj.bias": "model-00004-of-00004.safetensors",
679
+ "transformer.visual.transformer.resblocks.39.attn.in_proj.weight": "model-00004-of-00004.safetensors",
680
+ "transformer.visual.transformer.resblocks.39.attn.out_proj.bias": "model-00004-of-00004.safetensors",
681
+ "transformer.visual.transformer.resblocks.39.attn.out_proj.weight": "model-00004-of-00004.safetensors",
682
+ "transformer.visual.transformer.resblocks.39.ln_1.bias": "model-00004-of-00004.safetensors",
683
+ "transformer.visual.transformer.resblocks.39.ln_1.weight": "model-00004-of-00004.safetensors",
684
+ "transformer.visual.transformer.resblocks.39.ln_2.bias": "model-00004-of-00004.safetensors",
685
+ "transformer.visual.transformer.resblocks.39.ln_2.weight": "model-00004-of-00004.safetensors",
686
+ "transformer.visual.transformer.resblocks.39.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
687
+ "transformer.visual.transformer.resblocks.39.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
688
+ "transformer.visual.transformer.resblocks.39.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
689
+ "transformer.visual.transformer.resblocks.39.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
690
+ "transformer.visual.transformer.resblocks.4.attn.in_proj.bias": "model-00003-of-00004.safetensors",
691
+ "transformer.visual.transformer.resblocks.4.attn.in_proj.weight": "model-00003-of-00004.safetensors",
692
+ "transformer.visual.transformer.resblocks.4.attn.out_proj.bias": "model-00003-of-00004.safetensors",
693
+ "transformer.visual.transformer.resblocks.4.attn.out_proj.weight": "model-00003-of-00004.safetensors",
694
+ "transformer.visual.transformer.resblocks.4.ln_1.bias": "model-00003-of-00004.safetensors",
695
+ "transformer.visual.transformer.resblocks.4.ln_1.weight": "model-00003-of-00004.safetensors",
696
+ "transformer.visual.transformer.resblocks.4.ln_2.bias": "model-00003-of-00004.safetensors",
697
+ "transformer.visual.transformer.resblocks.4.ln_2.weight": "model-00003-of-00004.safetensors",
698
+ "transformer.visual.transformer.resblocks.4.mlp.c_fc.bias": "model-00003-of-00004.safetensors",
699
+ "transformer.visual.transformer.resblocks.4.mlp.c_fc.weight": "model-00003-of-00004.safetensors",
700
+ "transformer.visual.transformer.resblocks.4.mlp.c_proj.bias": "model-00003-of-00004.safetensors",
701
+ "transformer.visual.transformer.resblocks.4.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
702
+ "transformer.visual.transformer.resblocks.40.attn.in_proj.bias": "model-00004-of-00004.safetensors",
703
+ "transformer.visual.transformer.resblocks.40.attn.in_proj.weight": "model-00004-of-00004.safetensors",
704
+ "transformer.visual.transformer.resblocks.40.attn.out_proj.bias": "model-00004-of-00004.safetensors",
705
+ "transformer.visual.transformer.resblocks.40.attn.out_proj.weight": "model-00004-of-00004.safetensors",
706
+ "transformer.visual.transformer.resblocks.40.ln_1.bias": "model-00004-of-00004.safetensors",
707
+ "transformer.visual.transformer.resblocks.40.ln_1.weight": "model-00004-of-00004.safetensors",
708
+ "transformer.visual.transformer.resblocks.40.ln_2.bias": "model-00004-of-00004.safetensors",
709
+ "transformer.visual.transformer.resblocks.40.ln_2.weight": "model-00004-of-00004.safetensors",
710
+ "transformer.visual.transformer.resblocks.40.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
711
+ "transformer.visual.transformer.resblocks.40.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
712
+ "transformer.visual.transformer.resblocks.40.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
713
+ "transformer.visual.transformer.resblocks.40.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
714
+ "transformer.visual.transformer.resblocks.41.attn.in_proj.bias": "model-00004-of-00004.safetensors",
715
+ "transformer.visual.transformer.resblocks.41.attn.in_proj.weight": "model-00004-of-00004.safetensors",
716
+ "transformer.visual.transformer.resblocks.41.attn.out_proj.bias": "model-00004-of-00004.safetensors",
717
+ "transformer.visual.transformer.resblocks.41.attn.out_proj.weight": "model-00004-of-00004.safetensors",
718
+ "transformer.visual.transformer.resblocks.41.ln_1.bias": "model-00004-of-00004.safetensors",
719
+ "transformer.visual.transformer.resblocks.41.ln_1.weight": "model-00004-of-00004.safetensors",
720
+ "transformer.visual.transformer.resblocks.41.ln_2.bias": "model-00004-of-00004.safetensors",
721
+ "transformer.visual.transformer.resblocks.41.ln_2.weight": "model-00004-of-00004.safetensors",
722
+ "transformer.visual.transformer.resblocks.41.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
723
+ "transformer.visual.transformer.resblocks.41.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
724
+ "transformer.visual.transformer.resblocks.41.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
725
+ "transformer.visual.transformer.resblocks.41.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
726
+ "transformer.visual.transformer.resblocks.42.attn.in_proj.bias": "model-00004-of-00004.safetensors",
727
+ "transformer.visual.transformer.resblocks.42.attn.in_proj.weight": "model-00004-of-00004.safetensors",
728
+ "transformer.visual.transformer.resblocks.42.attn.out_proj.bias": "model-00004-of-00004.safetensors",
729
+ "transformer.visual.transformer.resblocks.42.attn.out_proj.weight": "model-00004-of-00004.safetensors",
730
+ "transformer.visual.transformer.resblocks.42.ln_1.bias": "model-00004-of-00004.safetensors",
731
+ "transformer.visual.transformer.resblocks.42.ln_1.weight": "model-00004-of-00004.safetensors",
732
+ "transformer.visual.transformer.resblocks.42.ln_2.bias": "model-00004-of-00004.safetensors",
733
+ "transformer.visual.transformer.resblocks.42.ln_2.weight": "model-00004-of-00004.safetensors",
734
+ "transformer.visual.transformer.resblocks.42.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
735
+ "transformer.visual.transformer.resblocks.42.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
736
+ "transformer.visual.transformer.resblocks.42.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
737
+ "transformer.visual.transformer.resblocks.42.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
738
+ "transformer.visual.transformer.resblocks.43.attn.in_proj.bias": "model-00004-of-00004.safetensors",
739
+ "transformer.visual.transformer.resblocks.43.attn.in_proj.weight": "model-00004-of-00004.safetensors",
740
+ "transformer.visual.transformer.resblocks.43.attn.out_proj.bias": "model-00004-of-00004.safetensors",
741
+ "transformer.visual.transformer.resblocks.43.attn.out_proj.weight": "model-00004-of-00004.safetensors",
742
+ "transformer.visual.transformer.resblocks.43.ln_1.bias": "model-00004-of-00004.safetensors",
743
+ "transformer.visual.transformer.resblocks.43.ln_1.weight": "model-00004-of-00004.safetensors",
744
+ "transformer.visual.transformer.resblocks.43.ln_2.bias": "model-00004-of-00004.safetensors",
745
+ "transformer.visual.transformer.resblocks.43.ln_2.weight": "model-00004-of-00004.safetensors",
746
+ "transformer.visual.transformer.resblocks.43.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
747
+ "transformer.visual.transformer.resblocks.43.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
748
+ "transformer.visual.transformer.resblocks.43.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
749
+ "transformer.visual.transformer.resblocks.43.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
750
+ "transformer.visual.transformer.resblocks.44.attn.in_proj.bias": "model-00004-of-00004.safetensors",
751
+ "transformer.visual.transformer.resblocks.44.attn.in_proj.weight": "model-00004-of-00004.safetensors",
752
+ "transformer.visual.transformer.resblocks.44.attn.out_proj.bias": "model-00004-of-00004.safetensors",
753
+ "transformer.visual.transformer.resblocks.44.attn.out_proj.weight": "model-00004-of-00004.safetensors",
754
+ "transformer.visual.transformer.resblocks.44.ln_1.bias": "model-00004-of-00004.safetensors",
755
+ "transformer.visual.transformer.resblocks.44.ln_1.weight": "model-00004-of-00004.safetensors",
756
+ "transformer.visual.transformer.resblocks.44.ln_2.bias": "model-00004-of-00004.safetensors",
757
+ "transformer.visual.transformer.resblocks.44.ln_2.weight": "model-00004-of-00004.safetensors",
758
+ "transformer.visual.transformer.resblocks.44.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
759
+ "transformer.visual.transformer.resblocks.44.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
760
+ "transformer.visual.transformer.resblocks.44.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
761
+ "transformer.visual.transformer.resblocks.44.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
762
+ "transformer.visual.transformer.resblocks.45.attn.in_proj.bias": "model-00004-of-00004.safetensors",
763
+ "transformer.visual.transformer.resblocks.45.attn.in_proj.weight": "model-00004-of-00004.safetensors",
764
+ "transformer.visual.transformer.resblocks.45.attn.out_proj.bias": "model-00004-of-00004.safetensors",
765
+ "transformer.visual.transformer.resblocks.45.attn.out_proj.weight": "model-00004-of-00004.safetensors",
766
+ "transformer.visual.transformer.resblocks.45.ln_1.bias": "model-00004-of-00004.safetensors",
767
+ "transformer.visual.transformer.resblocks.45.ln_1.weight": "model-00004-of-00004.safetensors",
768
+ "transformer.visual.transformer.resblocks.45.ln_2.bias": "model-00004-of-00004.safetensors",
769
+ "transformer.visual.transformer.resblocks.45.ln_2.weight": "model-00004-of-00004.safetensors",
770
+ "transformer.visual.transformer.resblocks.45.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
771
+ "transformer.visual.transformer.resblocks.45.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
772
+ "transformer.visual.transformer.resblocks.45.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
773
+ "transformer.visual.transformer.resblocks.45.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
774
+ "transformer.visual.transformer.resblocks.46.attn.in_proj.bias": "model-00004-of-00004.safetensors",
775
+ "transformer.visual.transformer.resblocks.46.attn.in_proj.weight": "model-00004-of-00004.safetensors",
776
+ "transformer.visual.transformer.resblocks.46.attn.out_proj.bias": "model-00004-of-00004.safetensors",
777
+ "transformer.visual.transformer.resblocks.46.attn.out_proj.weight": "model-00004-of-00004.safetensors",
778
+ "transformer.visual.transformer.resblocks.46.ln_1.bias": "model-00004-of-00004.safetensors",
779
+ "transformer.visual.transformer.resblocks.46.ln_1.weight": "model-00004-of-00004.safetensors",
780
+ "transformer.visual.transformer.resblocks.46.ln_2.bias": "model-00004-of-00004.safetensors",
781
+ "transformer.visual.transformer.resblocks.46.ln_2.weight": "model-00004-of-00004.safetensors",
782
+ "transformer.visual.transformer.resblocks.46.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
783
+ "transformer.visual.transformer.resblocks.46.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
784
+ "transformer.visual.transformer.resblocks.46.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
785
+ "transformer.visual.transformer.resblocks.46.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
786
+ "transformer.visual.transformer.resblocks.47.attn.in_proj.bias": "model-00004-of-00004.safetensors",
787
+ "transformer.visual.transformer.resblocks.47.attn.in_proj.weight": "model-00004-of-00004.safetensors",
788
+ "transformer.visual.transformer.resblocks.47.attn.out_proj.bias": "model-00004-of-00004.safetensors",
789
+ "transformer.visual.transformer.resblocks.47.attn.out_proj.weight": "model-00004-of-00004.safetensors",
790
+ "transformer.visual.transformer.resblocks.47.ln_1.bias": "model-00004-of-00004.safetensors",
791
+ "transformer.visual.transformer.resblocks.47.ln_1.weight": "model-00004-of-00004.safetensors",
792
+ "transformer.visual.transformer.resblocks.47.ln_2.bias": "model-00004-of-00004.safetensors",
793
+ "transformer.visual.transformer.resblocks.47.ln_2.weight": "model-00004-of-00004.safetensors",
794
+ "transformer.visual.transformer.resblocks.47.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
795
+ "transformer.visual.transformer.resblocks.47.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
796
+ "transformer.visual.transformer.resblocks.47.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
797
+ "transformer.visual.transformer.resblocks.47.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
798
+ "transformer.visual.transformer.resblocks.5.attn.in_proj.bias": "model-00003-of-00004.safetensors",
799
+ "transformer.visual.transformer.resblocks.5.attn.in_proj.weight": "model-00003-of-00004.safetensors",
800
+ "transformer.visual.transformer.resblocks.5.attn.out_proj.bias": "model-00003-of-00004.safetensors",
801
+ "transformer.visual.transformer.resblocks.5.attn.out_proj.weight": "model-00003-of-00004.safetensors",
802
+ "transformer.visual.transformer.resblocks.5.ln_1.bias": "model-00003-of-00004.safetensors",
803
+ "transformer.visual.transformer.resblocks.5.ln_1.weight": "model-00003-of-00004.safetensors",
804
+ "transformer.visual.transformer.resblocks.5.ln_2.bias": "model-00003-of-00004.safetensors",
805
+ "transformer.visual.transformer.resblocks.5.ln_2.weight": "model-00003-of-00004.safetensors",
806
+ "transformer.visual.transformer.resblocks.5.mlp.c_fc.bias": "model-00003-of-00004.safetensors",
807
+ "transformer.visual.transformer.resblocks.5.mlp.c_fc.weight": "model-00003-of-00004.safetensors",
808
+ "transformer.visual.transformer.resblocks.5.mlp.c_proj.bias": "model-00003-of-00004.safetensors",
809
+ "transformer.visual.transformer.resblocks.5.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
810
+ "transformer.visual.transformer.resblocks.6.attn.in_proj.bias": "model-00003-of-00004.safetensors",
811
+ "transformer.visual.transformer.resblocks.6.attn.in_proj.weight": "model-00003-of-00004.safetensors",
812
+ "transformer.visual.transformer.resblocks.6.attn.out_proj.bias": "model-00003-of-00004.safetensors",
813
+ "transformer.visual.transformer.resblocks.6.attn.out_proj.weight": "model-00003-of-00004.safetensors",
814
+ "transformer.visual.transformer.resblocks.6.ln_1.bias": "model-00003-of-00004.safetensors",
815
+ "transformer.visual.transformer.resblocks.6.ln_1.weight": "model-00003-of-00004.safetensors",
816
+ "transformer.visual.transformer.resblocks.6.ln_2.bias": "model-00003-of-00004.safetensors",
817
+ "transformer.visual.transformer.resblocks.6.ln_2.weight": "model-00003-of-00004.safetensors",
818
+ "transformer.visual.transformer.resblocks.6.mlp.c_fc.bias": "model-00003-of-00004.safetensors",
819
+ "transformer.visual.transformer.resblocks.6.mlp.c_fc.weight": "model-00003-of-00004.safetensors",
820
+ "transformer.visual.transformer.resblocks.6.mlp.c_proj.bias": "model-00003-of-00004.safetensors",
821
+ "transformer.visual.transformer.resblocks.6.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
822
+ "transformer.visual.transformer.resblocks.7.attn.in_proj.bias": "model-00003-of-00004.safetensors",
823
+ "transformer.visual.transformer.resblocks.7.attn.in_proj.weight": "model-00003-of-00004.safetensors",
824
+ "transformer.visual.transformer.resblocks.7.attn.out_proj.bias": "model-00003-of-00004.safetensors",
825
+ "transformer.visual.transformer.resblocks.7.attn.out_proj.weight": "model-00003-of-00004.safetensors",
826
+ "transformer.visual.transformer.resblocks.7.ln_1.bias": "model-00003-of-00004.safetensors",
827
+ "transformer.visual.transformer.resblocks.7.ln_1.weight": "model-00003-of-00004.safetensors",
828
+ "transformer.visual.transformer.resblocks.7.ln_2.bias": "model-00003-of-00004.safetensors",
829
+ "transformer.visual.transformer.resblocks.7.ln_2.weight": "model-00003-of-00004.safetensors",
830
+ "transformer.visual.transformer.resblocks.7.mlp.c_fc.bias": "model-00003-of-00004.safetensors",
831
+ "transformer.visual.transformer.resblocks.7.mlp.c_fc.weight": "model-00003-of-00004.safetensors",
832
+ "transformer.visual.transformer.resblocks.7.mlp.c_proj.bias": "model-00003-of-00004.safetensors",
833
+ "transformer.visual.transformer.resblocks.7.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
834
+ "transformer.visual.transformer.resblocks.8.attn.in_proj.bias": "model-00003-of-00004.safetensors",
835
+ "transformer.visual.transformer.resblocks.8.attn.in_proj.weight": "model-00003-of-00004.safetensors",
836
+ "transformer.visual.transformer.resblocks.8.attn.out_proj.bias": "model-00003-of-00004.safetensors",
837
+ "transformer.visual.transformer.resblocks.8.attn.out_proj.weight": "model-00003-of-00004.safetensors",
838
+ "transformer.visual.transformer.resblocks.8.ln_1.bias": "model-00003-of-00004.safetensors",
839
+ "transformer.visual.transformer.resblocks.8.ln_1.weight": "model-00003-of-00004.safetensors",
840
+ "transformer.visual.transformer.resblocks.8.ln_2.bias": "model-00003-of-00004.safetensors",
841
+ "transformer.visual.transformer.resblocks.8.ln_2.weight": "model-00003-of-00004.safetensors",
842
+ "transformer.visual.transformer.resblocks.8.mlp.c_fc.bias": "model-00003-of-00004.safetensors",
843
+ "transformer.visual.transformer.resblocks.8.mlp.c_fc.weight": "model-00003-of-00004.safetensors",
844
+ "transformer.visual.transformer.resblocks.8.mlp.c_proj.bias": "model-00003-of-00004.safetensors",
845
+ "transformer.visual.transformer.resblocks.8.mlp.c_proj.weight": "model-00003-of-00004.safetensors",
846
+ "transformer.visual.transformer.resblocks.9.attn.in_proj.bias": "model-00003-of-00004.safetensors",
847
+ "transformer.visual.transformer.resblocks.9.attn.in_proj.weight": "model-00003-of-00004.safetensors",
848
+ "transformer.visual.transformer.resblocks.9.attn.out_proj.bias": "model-00003-of-00004.safetensors",
849
+ "transformer.visual.transformer.resblocks.9.attn.out_proj.weight": "model-00003-of-00004.safetensors",
850
+ "transformer.visual.transformer.resblocks.9.ln_1.bias": "model-00003-of-00004.safetensors",
851
+ "transformer.visual.transformer.resblocks.9.ln_1.weight": "model-00003-of-00004.safetensors",
852
+ "transformer.visual.transformer.resblocks.9.ln_2.bias": "model-00003-of-00004.safetensors",
853
+ "transformer.visual.transformer.resblocks.9.ln_2.weight": "model-00003-of-00004.safetensors",
854
+ "transformer.visual.transformer.resblocks.9.mlp.c_fc.bias": "model-00004-of-00004.safetensors",
855
+ "transformer.visual.transformer.resblocks.9.mlp.c_fc.weight": "model-00004-of-00004.safetensors",
856
+ "transformer.visual.transformer.resblocks.9.mlp.c_proj.bias": "model-00004-of-00004.safetensors",
857
+ "transformer.visual.transformer.resblocks.9.mlp.c_proj.weight": "model-00004-of-00004.safetensors",
858
+ "transformer.wte.weight": "model-00001-of-00004.safetensors"
859
+ }
860
+ }
modeling_qwen.py ADDED
@@ -0,0 +1,1162 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ from .visual import VisionTransformer
48
+
49
+
50
+ logger = logging.get_logger(__name__)
51
+
52
+ _CHECKPOINT_FOR_DOC = "qwen"
53
+ _CONFIG_FOR_DOC = "QWenConfig"
54
+
55
+ QWen_PRETRAINED_MODEL_ARCHIVE_LIST = ["qwen-7b"]
56
+
57
+ _ERROR_BAD_CHAT_FORMAT = """\
58
+ 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".
59
+ 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().
60
+ 我们检测到您可能在使用预训练模型(而非chat模型)进行多轮chat,因为您当前在generation_config指定的chat_format,并未设置为我们在对话中所支持的"chatml"格式。
61
+ 如果您在直接使用我们从Huggingface提供的模型,请确保您在调用model.chat()时,使用的是"Qwen/Qwen-7B-Chat"模型(而非"Qwen/Qwen-7B"预训练模型)。
62
+ """
63
+
64
+ _SENTINEL = object()
65
+ _ERROR_STREAM_IN_CHAT = """\
66
+ Pass argument `stream` to model.chat() is buggy, deprecated, and marked for removal. Please use model.chat_stream(...) instead of model.chat(..., stream=True).
67
+ 向model.chat()传入参数stream的用法可能存在Bug,该用法已被废弃,将在未来被移除。请使用model.chat_stream(...)代替model.chat(..., stream=True)。
68
+ """
69
+
70
+ apply_rotary_emb_func = None
71
+ rms_norm = None
72
+
73
+
74
+ # Copied from transformers.models.bart.modeling_bart._make_causal_mask
75
+ def _make_causal_mask(
76
+ input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
77
+ ):
78
+ """
79
+ Make causal mask used for bi-directional self-attention.
80
+ """
81
+ bsz, tgt_len = input_ids_shape
82
+ mask = torch.full((tgt_len, tgt_len), torch.finfo(dtype).min, device=device)
83
+ mask_cond = torch.arange(mask.size(-1), device=device)
84
+ mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0)
85
+ mask = mask.to(dtype)
86
+
87
+ if past_key_values_length > 0:
88
+ mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1)
89
+ return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length)
90
+
91
+
92
+ # Copied from transformers.models.bart.modeling_bart._expand_mask
93
+ def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):
94
+ """
95
+ Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.
96
+ """
97
+ bsz, src_len = mask.size()
98
+ tgt_len = tgt_len if tgt_len is not None else src_len
99
+
100
+ expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype)
101
+
102
+ inverted_mask = 1.0 - expanded_mask
103
+
104
+ return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min)
105
+
106
+
107
+ class QWenAttention(nn.Module):
108
+ def __init__(self, config):
109
+ super().__init__()
110
+
111
+ self.register_buffer("masked_bias", torch.tensor(-1e4), persistent=False)
112
+ self.seq_length = config.seq_length
113
+
114
+ self.hidden_size = config.hidden_size
115
+ self.split_size = config.hidden_size
116
+ self.num_heads = config.num_attention_heads
117
+ self.head_dim = self.hidden_size // self.num_heads
118
+
119
+ self.scale_attn_weights = True
120
+
121
+ self.projection_size = config.kv_channels * config.num_attention_heads
122
+
123
+ assert self.projection_size % config.num_attention_heads == 0
124
+ self.hidden_size_per_attention_head = (
125
+ self.projection_size // config.num_attention_heads
126
+ )
127
+
128
+ self.c_attn = nn.Linear(config.hidden_size, 3 * self.projection_size)
129
+
130
+ self.c_proj = nn.Linear(
131
+ config.hidden_size, self.projection_size, bias=not config.no_bias
132
+ )
133
+
134
+ self.is_fp32 = not (config.bf16 or config.fp16)
135
+ self.bf16 = config.bf16
136
+
137
+ self.use_dynamic_ntk = config.use_dynamic_ntk
138
+ self.use_logn_attn = config.use_logn_attn
139
+
140
+ logn_list = [
141
+ math.log(i, self.seq_length) if i > self.seq_length else 1
142
+ for i in range(1, 32768)
143
+ ]
144
+ self.logn_tensor = torch.tensor(logn_list)[None, :, None, None]
145
+
146
+ self.attn_dropout = nn.Dropout(config.attn_dropout_prob)
147
+
148
+ def _attn(self, query, key, value, registered_causal_mask, attention_mask=None, head_mask=None):
149
+ attn_weights = torch.matmul(query, key.transpose(-1, -2))
150
+
151
+ if self.scale_attn_weights:
152
+ attn_weights = attn_weights / torch.full(
153
+ [],
154
+ value.size(-1) ** 0.5,
155
+ dtype=attn_weights.dtype,
156
+ device=attn_weights.device,
157
+ )
158
+
159
+ query_length, key_length = query.size(-2), key.size(-2)
160
+ # causal_mask = self.bias[
161
+ # :, :, key_length - query_length : key_length, :key_length
162
+ # ]
163
+ # mask_value = torch.finfo(attn_weights.dtype).min
164
+ # mask_value = torch.full([], mask_value, dtype=attn_weights.dtype).to(
165
+ # attn_weights.device
166
+ # )
167
+ # attn_weights = torch.where(
168
+ # causal_mask, attn_weights.to(attn_weights.dtype), mask_value
169
+ # )
170
+ attn_weights = attn_weights + attention_mask
171
+
172
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1)
173
+
174
+ attn_weights = attn_weights.type(value.dtype)
175
+ attn_weights = self.attn_dropout(attn_weights)
176
+
177
+ if head_mask is not None:
178
+ attn_weights = attn_weights * head_mask
179
+
180
+ attn_output = torch.matmul(attn_weights, value)
181
+ attn_output = attn_output.transpose(1, 2)
182
+
183
+ return attn_output, attn_weights
184
+
185
+ def _upcast_and_reordered_attn(
186
+ self, query, key, value, registered_causal_mask, attention_mask=None, head_mask=None
187
+ ):
188
+ bsz, num_heads, q_seq_len, dk = query.size()
189
+ _, _, k_seq_len, _ = key.size()
190
+
191
+ attn_weights = torch.empty(
192
+ bsz * num_heads,
193
+ q_seq_len,
194
+ k_seq_len,
195
+ dtype=torch.float32,
196
+ device=query.device,
197
+ )
198
+
199
+ scale_factor = 1.0
200
+ if self.scale_attn_weights:
201
+ scale_factor /= float(value.size(-1)) ** 0.5
202
+
203
+ with autocast(enabled=False):
204
+ q, k = query.reshape(-1, q_seq_len, dk), key.transpose(-1, -2).reshape(
205
+ -1, dk, k_seq_len
206
+ )
207
+ attn_weights = torch.baddbmm(
208
+ attn_weights, q.float(), k.float(), beta=0, alpha=scale_factor
209
+ )
210
+ attn_weights = attn_weights.reshape(bsz, num_heads, q_seq_len, k_seq_len)
211
+
212
+ query_length, key_length = query.size(-2), key.size(-2)
213
+ causal_mask = registered_causal_mask[
214
+ :, :, key_length - query_length : key_length, :key_length
215
+ ]
216
+ mask_value = torch.finfo(attn_weights.dtype).min
217
+ mask_value = torch.tensor(mask_value, dtype=attn_weights.dtype).to(
218
+ attn_weights.device
219
+ )
220
+ attn_weights = torch.where(causal_mask, attn_weights, mask_value)
221
+
222
+ if attention_mask is not None:
223
+ attn_weights = attn_weights + attention_mask
224
+
225
+ attn_weights = nn.functional.softmax(attn_weights, dim=-1)
226
+
227
+ if attn_weights.dtype != torch.float32:
228
+ raise RuntimeError(
229
+ "Error with upcasting, attn_weights does not have dtype torch.float32"
230
+ )
231
+ attn_weights = attn_weights.type(value.dtype)
232
+ attn_weights = self.attn_dropout(attn_weights)
233
+
234
+ if head_mask is not None:
235
+ attn_weights = attn_weights * head_mask
236
+
237
+ attn_output = torch.matmul(attn_weights, value)
238
+
239
+ return attn_output, attn_weights
240
+
241
+ def _split_heads(self, tensor, num_heads, attn_head_size):
242
+ new_shape = tensor.size()[:-1] + (num_heads, attn_head_size)
243
+ tensor = tensor.view(new_shape)
244
+ return tensor
245
+
246
+ def _merge_heads(self, tensor, num_heads, attn_head_size):
247
+ tensor = tensor.contiguous()
248
+ new_shape = tensor.size()[:-2] + (num_heads * attn_head_size,)
249
+ return tensor.view(new_shape)
250
+
251
+ def forward(
252
+ self,
253
+ hidden_states: Optional[Tuple[torch.FloatTensor]],
254
+ rotary_pos_emb: Optional[List[torch.Tensor]] = None,
255
+ registered_causal_mask: Optional[torch.Tensor] = None,
256
+ layer_past: Optional[Tuple[torch.Tensor]] = None,
257
+ attention_mask: Optional[torch.FloatTensor] = None,
258
+ head_mask: Optional[torch.FloatTensor] = None,
259
+ encoder_hidden_states: Optional[torch.Tensor] = None,
260
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
261
+ output_attentions: Optional[bool] = False,
262
+ use_cache: Optional[bool] = False,
263
+ ):
264
+
265
+ mixed_x_layer = self.c_attn(hidden_states)
266
+
267
+ query, key, value = mixed_x_layer.split(self.split_size, dim=2)
268
+
269
+ query = self._split_heads(query, self.num_heads, self.head_dim)
270
+ key = self._split_heads(key, self.num_heads, self.head_dim)
271
+ value = self._split_heads(value, self.num_heads, self.head_dim)
272
+
273
+ if rotary_pos_emb is not None:
274
+ cur_len = query.shape[1]
275
+ rotary_pos_emb = [i[:, -cur_len:, :, :] for i in rotary_pos_emb]
276
+ rotary_pos_emb = (rotary_pos_emb,) * 2
277
+ q_pos_emb, k_pos_emb = rotary_pos_emb
278
+ # Slice the pos emb for current inference
279
+ query = apply_rotary_pos_emb(query, q_pos_emb)
280
+ key = apply_rotary_pos_emb(key, k_pos_emb)
281
+
282
+ if layer_past is not None:
283
+ past_key, past_value = layer_past[0], layer_past[1]
284
+ key = torch.cat((past_key, key), dim=1)
285
+ value = torch.cat((past_value, value), dim=1)
286
+
287
+ if use_cache:
288
+ present = (key, value)
289
+ else:
290
+ present = None
291
+
292
+ if self.use_logn_attn and not self.training:
293
+ if self.logn_tensor.device != query.device or self.logn_tensor.dtype != query.dtype:
294
+ self.logn_tensor = self.logn_tensor.to(query.device).type_as(query)
295
+ seq_start = key.size(1) - query.size(1)
296
+ seq_end = key.size(1)
297
+ logn_tensor = self.logn_tensor[:, seq_start:seq_end, :, :]
298
+ query = query * logn_tensor.expand_as(query)
299
+
300
+ query = query.permute(0, 2, 1, 3)
301
+ key = key.permute(0, 2, 1, 3)
302
+ value = value.permute(0, 2, 1, 3)
303
+ attn_output, attn_weight = self._attn(
304
+ query, key, value, registered_causal_mask, attention_mask, head_mask
305
+ )
306
+ context_layer = self._merge_heads(
307
+ attn_output, self.num_heads, self.head_dim
308
+ )
309
+
310
+ attn_output = self.c_proj(context_layer)
311
+
312
+ outputs = (attn_output, present)
313
+ if output_attentions:
314
+ outputs += (attn_weight,)
315
+
316
+ return outputs
317
+
318
+
319
+ class QWenMLP(nn.Module):
320
+ def __init__(self, config):
321
+ super().__init__()
322
+ self.w1 = nn.Linear(
323
+ config.hidden_size, config.intermediate_size // 2, bias=not config.no_bias
324
+ )
325
+ self.w2 = nn.Linear(
326
+ config.hidden_size, config.intermediate_size // 2, bias=not config.no_bias
327
+ )
328
+ ff_dim_in = config.intermediate_size // 2
329
+ self.c_proj = nn.Linear(ff_dim_in, config.hidden_size, bias=not config.no_bias)
330
+
331
+ def forward(self, hidden_states):
332
+ a1 = self.w1(hidden_states)
333
+ a2 = self.w2(hidden_states)
334
+ intermediate_parallel = a1 * F.silu(a2)
335
+ output = self.c_proj(intermediate_parallel)
336
+ return output
337
+
338
+ class QWenBlock(nn.Module):
339
+ def __init__(self, config):
340
+ super().__init__()
341
+ hidden_size = config.hidden_size
342
+ self.bf16 = config.bf16
343
+
344
+ self.ln_1 = RMSNorm(
345
+ hidden_size,
346
+ eps=config.layer_norm_epsilon,
347
+ )
348
+ self.attn = QWenAttention(config)
349
+ self.ln_2 = RMSNorm(
350
+ hidden_size,
351
+ eps=config.layer_norm_epsilon,
352
+ )
353
+
354
+ self.mlp = QWenMLP(config)
355
+
356
+ def forward(
357
+ self,
358
+ hidden_states: Optional[Tuple[torch.FloatTensor]],
359
+ rotary_pos_emb: Optional[List[torch.Tensor]] = None,
360
+ registered_causal_mask: Optional[torch.Tensor] = None,
361
+ layer_past: Optional[Tuple[torch.Tensor]] = None,
362
+ attention_mask: Optional[torch.FloatTensor] = None,
363
+ head_mask: Optional[torch.FloatTensor] = None,
364
+ encoder_hidden_states: Optional[torch.Tensor] = None,
365
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
366
+ use_cache: Optional[bool] = False,
367
+ output_attentions: Optional[bool] = False,
368
+ ):
369
+ layernorm_output = self.ln_1(hidden_states)
370
+
371
+ attn_outputs = self.attn(
372
+ layernorm_output,
373
+ rotary_pos_emb,
374
+ registered_causal_mask=registered_causal_mask,
375
+ layer_past=layer_past,
376
+ attention_mask=attention_mask,
377
+ head_mask=head_mask,
378
+ use_cache=use_cache,
379
+ output_attentions=output_attentions,
380
+ )
381
+ attn_output = attn_outputs[0]
382
+
383
+ outputs = attn_outputs[1:]
384
+
385
+ residual = hidden_states
386
+ layernorm_input = attn_output + residual
387
+
388
+ layernorm_output = self.ln_2(layernorm_input)
389
+
390
+ residual = layernorm_input
391
+ mlp_output = self.mlp(layernorm_output)
392
+ hidden_states = residual + mlp_output
393
+
394
+ if use_cache:
395
+ outputs = (hidden_states,) + outputs
396
+ else:
397
+ outputs = (hidden_states,) + outputs[1:]
398
+
399
+ return outputs
400
+
401
+
402
+ class QWenPreTrainedModel(PreTrainedModel):
403
+ config_class = QWenConfig
404
+ base_model_prefix = "transformer"
405
+ is_parallelizable = False
406
+ supports_gradient_checkpointing = True
407
+ _no_split_modules = ["QWenBlock"]
408
+
409
+ def __init__(self, *inputs, **kwargs):
410
+ super().__init__(*inputs, **kwargs)
411
+
412
+ def _init_weights(self, module):
413
+ """Initialize the weights."""
414
+ if isinstance(module, nn.Linear):
415
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
416
+ if module.bias is not None:
417
+ module.bias.data.zero_()
418
+ elif isinstance(module, nn.Embedding):
419
+ module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
420
+ if module.padding_idx is not None:
421
+ module.weight.data[module.padding_idx].zero_()
422
+ elif isinstance(module, RMSNorm):
423
+ module.weight.data.fill_(1.0)
424
+
425
+ for name, p in module.named_parameters():
426
+ if name == "c_proj.weight":
427
+ p.data.normal_(
428
+ mean=0.0,
429
+ std=(
430
+ self.config.initializer_range
431
+ / math.sqrt(2 * self.config.num_hidden_layers)
432
+ ),
433
+ )
434
+
435
+ def _set_gradient_checkpointing(self, module, value=False):
436
+ if isinstance(module, QWenModel):
437
+ module.gradient_checkpointing = value
438
+
439
+
440
+ class QWenModel(QWenPreTrainedModel):
441
+ _keys_to_ignore_on_load_missing = ["attn.masked_bias"]
442
+
443
+ def __init__(self, config):
444
+ super().__init__(config)
445
+ self.vocab_size = config.vocab_size
446
+ self.num_hidden_layers = config.num_hidden_layers
447
+ self.embed_dim = config.hidden_size
448
+
449
+ self.gradient_checkpointing = False
450
+ self.use_dynamic_ntk = config.use_dynamic_ntk
451
+ self.seq_length = config.seq_length
452
+
453
+ self.wte = nn.Embedding(self.vocab_size, self.embed_dim)
454
+
455
+ self.drop = nn.Dropout(config.emb_dropout_prob)
456
+
457
+ if config.rotary_pct == 1.0:
458
+ self.rotary_ndims = None
459
+ else:
460
+ assert config.rotary_pct < 1
461
+ self.rotary_ndims = int(
462
+ config.kv_channels * config.rotary_pct
463
+ )
464
+ dim = (
465
+ self.rotary_ndims
466
+ if self.rotary_ndims is not None
467
+ else config.kv_channels
468
+ )
469
+ self.rotary_emb = RotaryEmbedding(dim, base=config.rotary_emb_base)
470
+
471
+ self.use_flash_attn = config.use_flash_attn
472
+ self.is_fp32 = not (config.bf16 or config.fp16)
473
+ self.registered_causal_mask = None
474
+ # if (
475
+ # self.use_flash_attn
476
+ # and flash_attn_unpadded_func is not None
477
+ # and not self.is_fp32
478
+ # ):
479
+ # self.registered_causal_mask = None
480
+ # else:
481
+ # max_positions = config.max_position_embeddings
482
+ # self.register_buffer(
483
+ # "registered_causal_mask",
484
+ # torch.tril(
485
+ # torch.ones((max_positions, max_positions), dtype=torch.bool)
486
+ # ).view(1, 1, max_positions, max_positions),
487
+ # persistent=False,
488
+ # )
489
+
490
+ self.h = nn.ModuleList(
491
+ [
492
+ QWenBlock(
493
+ config
494
+ )
495
+ for i in range(config.num_hidden_layers)
496
+ ]
497
+ )
498
+ self.ln_f = RMSNorm(
499
+ self.embed_dim,
500
+ eps=config.layer_norm_epsilon,
501
+ )
502
+
503
+ self.visual = VisionTransformer(**config.visual)
504
+
505
+ self.post_init()
506
+
507
+ def get_input_embeddings(self):
508
+ return self.wte
509
+
510
+ def set_input_embeddings(self, new_embeddings):
511
+ self.wte = new_embeddings
512
+
513
+ # Copied from transformers.models.bart.modeling_bart.BartDecoder._prepare_decoder_attention_mask
514
+ def _prepare_decoder_attention_mask(self, attention_mask, input_shape, inputs_embeds, past_key_values_length):
515
+ # create causal mask
516
+ # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
517
+ combined_attention_mask = None
518
+ if input_shape[-1] > 1:
519
+ combined_attention_mask = _make_causal_mask(
520
+ input_shape,
521
+ inputs_embeds.dtype,
522
+ device=inputs_embeds.device,
523
+ past_key_values_length=past_key_values_length,
524
+ )
525
+
526
+ if attention_mask is not None:
527
+ # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
528
+ expanded_attn_mask = _expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to(
529
+ inputs_embeds.device
530
+ )
531
+ combined_attention_mask = (
532
+ expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
533
+ )
534
+
535
+ return combined_attention_mask
536
+
537
+
538
+ def forward(
539
+ self,
540
+ input_ids: Optional[torch.LongTensor] = None,
541
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
542
+ attention_mask: Optional[torch.FloatTensor] = None,
543
+ token_type_ids: Optional[torch.LongTensor] = None,
544
+ position_ids: Optional[torch.LongTensor] = None,
545
+ head_mask: Optional[torch.FloatTensor] = None,
546
+ inputs_embeds: Optional[torch.FloatTensor] = None,
547
+ encoder_hidden_states: Optional[torch.Tensor] = None,
548
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
549
+ use_cache: Optional[bool] = None,
550
+ output_attentions: Optional[bool] = None,
551
+ output_hidden_states: Optional[bool] = None,
552
+ return_dict: Optional[bool] = None,
553
+ ):
554
+ if past_key_values is None and torch.any(input_ids == self.config.visual['image_start_id']):
555
+ bos_pos = torch.where(input_ids == self.config.visual['image_start_id'])
556
+ eos_pos = torch.where(input_ids == self.config.visual['image_start_id'] + 1)
557
+ assert (bos_pos[0] == eos_pos[0]).all()
558
+ img_pos = torch.stack((bos_pos[0], bos_pos[1], eos_pos[1]), dim=1)
559
+ images = []
560
+ for i, a, b in img_pos:
561
+ image = input_ids[i][a + 1 : b - 1].tolist()
562
+ image = image[ : image.index(self.config.visual['image_start_id'] + 2)]
563
+ images.append(bytes(image).decode('utf-8'))
564
+
565
+ images = self.visual.encode(images)
566
+ assert images.shape[0] == len(images)
567
+ fake_images = None
568
+ elif self.training:
569
+ fake_images=torch.zeros(1,3,224,224).to(
570
+ dtype=self.visual.conv1.weight.dtype, device=self.visual.conv1.weight.device)
571
+ images = self.visual(fake_images)
572
+ else:
573
+ fake_images = None
574
+ images = None
575
+
576
+ output_attentions = (
577
+ output_attentions
578
+ if output_attentions is not None
579
+ else self.config.output_attentions
580
+ )
581
+ output_hidden_states = (
582
+ output_hidden_states
583
+ if output_hidden_states is not None
584
+ else self.config.output_hidden_states
585
+ )
586
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
587
+ return_dict = (
588
+ return_dict if return_dict is not None else self.config.use_return_dict
589
+ )
590
+
591
+ if input_ids is not None and inputs_embeds is not None:
592
+ raise ValueError(
593
+ "You cannot specify both input_ids and inputs_embeds at the same time"
594
+ )
595
+ elif input_ids is not None:
596
+ input_shape = input_ids.size()
597
+ input_ids = input_ids.view(-1, input_shape[-1])
598
+ batch_size = input_ids.shape[0]
599
+ elif inputs_embeds is not None:
600
+ input_shape = inputs_embeds.size()[:-1]
601
+ batch_size = inputs_embeds.shape[0]
602
+ else:
603
+ raise ValueError("You have to specify either input_ids or inputs_embeds")
604
+
605
+ device = input_ids.device if input_ids is not None else inputs_embeds.device
606
+
607
+ if token_type_ids is not None:
608
+ token_type_ids = token_type_ids.view(-1, input_shape[-1])
609
+ if position_ids is not None:
610
+ position_ids = position_ids.view(-1, input_shape[-1])
611
+
612
+ if past_key_values is None:
613
+ past_length = 0
614
+ past_key_values = tuple([None] * len(self.h))
615
+ else:
616
+ past_length = past_key_values[0][0].size(-2)
617
+
618
+ if position_ids is None:
619
+ position_ids = torch.arange(
620
+ past_length,
621
+ input_shape[-1] + past_length,
622
+ dtype=torch.long,
623
+ device=device,
624
+ )
625
+ position_ids = position_ids.unsqueeze(0).view(-1, input_shape[-1])
626
+
627
+ encoder_attention_mask = None
628
+ head_mask = self.get_head_mask(head_mask, self.config.num_hidden_layers)
629
+
630
+ if inputs_embeds is None:
631
+ inputs_embeds = self.wte(input_ids)
632
+
633
+ if batch_size <= 0:
634
+ raise ValueError("batch_size has to be defined and > 0")
635
+ attention_mask = self._prepare_decoder_attention_mask(
636
+ attention_mask, input_shape, inputs_embeds, past_length
637
+ )
638
+
639
+ hidden_states = inputs_embeds
640
+
641
+ kv_seq_len = hidden_states.size()[1]
642
+ if past_key_values[0] is not None:
643
+ # past key values[0][0] shape: bs * seq_len * head_num * dim
644
+ kv_seq_len += past_key_values[0][0].shape[1]
645
+ if (
646
+ self.use_dynamic_ntk
647
+ and kv_seq_len == hidden_states.size()[1]
648
+ and not self.training
649
+ ):
650
+ context_value = math.log(kv_seq_len / self.seq_length, 2) + 1
651
+ ntk_alpha = 2 ** math.ceil(context_value) - 1
652
+ ntk_alpha = max(ntk_alpha, 1)
653
+ else:
654
+ ntk_alpha = self.rotary_emb._ntk_alpha_cached
655
+
656
+ rotary_pos_emb = self.rotary_emb(kv_seq_len, ntk_alpha=ntk_alpha)
657
+ for idx in range(len(rotary_pos_emb)):
658
+ rotary_pos_emb[idx] = rotary_pos_emb[idx].to(hidden_states.device)
659
+
660
+ hidden_states = self.drop(hidden_states).clone()
661
+ if fake_images is not None:
662
+ hidden_states = hidden_states + images.mean()*0
663
+ elif images is not None:
664
+ for idx, (i, a, b) in enumerate(img_pos):
665
+ hidden_states[i][a + 1 : b] = images[idx]
666
+ output_shape = input_shape + (hidden_states.size(-1),)
667
+
668
+ if self.gradient_checkpointing and self.training:
669
+ if use_cache:
670
+ logger.warning_once(
671
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
672
+ )
673
+ use_cache = False
674
+
675
+ presents = () if use_cache else None
676
+ all_self_attentions = () if output_attentions else None
677
+ all_hidden_states = () if output_hidden_states else None
678
+ for i, (block, layer_past) in enumerate(zip(self.h, past_key_values)):
679
+
680
+ if output_hidden_states:
681
+ all_hidden_states = all_hidden_states + (hidden_states,)
682
+
683
+ if self.gradient_checkpointing and self.training:
684
+
685
+ def create_custom_forward(module):
686
+ def custom_forward(*inputs):
687
+ # None for past_key_value
688
+ return module(*inputs, use_cache, output_attentions)
689
+
690
+ return custom_forward
691
+
692
+ outputs = torch.utils.checkpoint.checkpoint(
693
+ create_custom_forward(block),
694
+ hidden_states,
695
+ rotary_pos_emb,
696
+ self.registered_causal_mask,
697
+ None,
698
+ attention_mask,
699
+ head_mask[i],
700
+ encoder_hidden_states,
701
+ encoder_attention_mask,
702
+ )
703
+ else:
704
+ outputs = block(
705
+ hidden_states,
706
+ layer_past=layer_past,
707
+ rotary_pos_emb=rotary_pos_emb,
708
+ registered_causal_mask=self.registered_causal_mask,
709
+ attention_mask=attention_mask,
710
+ head_mask=head_mask[i],
711
+ encoder_hidden_states=encoder_hidden_states,
712
+ encoder_attention_mask=encoder_attention_mask,
713
+ use_cache=use_cache,
714
+ output_attentions=output_attentions,
715
+ )
716
+
717
+ hidden_states = outputs[0]
718
+ if use_cache is True:
719
+ presents = presents + (outputs[1],)
720
+
721
+ if output_attentions:
722
+ all_self_attentions = all_self_attentions + (outputs[2 if use_cache else 1],)
723
+
724
+ hidden_states = self.ln_f(hidden_states)
725
+ hidden_states = hidden_states.view(output_shape)
726
+ # Add last hidden state
727
+ if output_hidden_states:
728
+ all_hidden_states = all_hidden_states + (hidden_states,)
729
+
730
+ if not return_dict:
731
+ return tuple(
732
+ v for v in [hidden_states, presents, all_hidden_states] if v is not None
733
+ )
734
+
735
+ return BaseModelOutputWithPast(
736
+ last_hidden_state=hidden_states,
737
+ past_key_values=presents,
738
+ hidden_states=all_hidden_states,
739
+ attentions=all_self_attentions,
740
+ )
741
+
742
+
743
+ class QWenLMHeadModel(QWenPreTrainedModel):
744
+ _keys_to_ignore_on_load_missing = [r"h\.\d+\.attn\.rotary_emb\.inv_freq"]
745
+ _keys_to_ignore_on_load_unexpected = [r"h\.\d+\.attn\.masked_bias"]
746
+
747
+ def __init__(self, config):
748
+ super().__init__(config)
749
+ assert (
750
+ config.bf16 + config.fp16 + config.fp32 <= 1
751
+ ), "Only one of \"bf16\", \"fp16\", \"fp32\" can be true"
752
+
753
+ autoset_precision = config.bf16 + config.fp16 + config.fp32 == 0
754
+
755
+ if autoset_precision:
756
+ if SUPPORT_BF16:
757
+ logger.warn(
758
+ "The model is automatically converting to bf16 for faster inference. "
759
+ "If you want to disable the automatic precision, please manually add bf16/fp16/fp32=True to \"AutoModelForCausalLM.from_pretrained\"."
760
+ )
761
+ config.bf16 = True
762
+ elif SUPPORT_FP16:
763
+ logger.warn(
764
+ "The model is automatically converting to fp16 for faster inference. "
765
+ "If you want to disable the automatic precision, please manually add bf16/fp16/fp32=True to \"AutoModelForCausalLM.from_pretrained\"."
766
+ )
767
+ config.fp16 = True
768
+ else:
769
+ config.fp32 = True
770
+
771
+ if config.bf16 and SUPPORT_CUDA and not SUPPORT_BF16:
772
+ logger.warn("Your device does NOT seem to support bf16, you can switch to fp16 or fp32 by by passing fp16/fp32=True in \"AutoModelForCausalLM.from_pretrained\".")
773
+ if config.fp16 and SUPPORT_CUDA and not SUPPORT_FP16:
774
+ logger.warn("Your device does NOT support faster inference with fp16, please switch to fp32 which is likely to be faster")
775
+ if config.fp32:
776
+ if SUPPORT_BF16:
777
+ logger.warn("Your device support faster inference by passing bf16=True in \"AutoModelForCausalLM.from_pretrained\".")
778
+ elif SUPPORT_FP16:
779
+ logger.warn("Your device support faster inference by passing fp16=True in \"AutoModelForCausalLM.from_pretrained\".")
780
+
781
+ self.transformer = QWenModel(config)
782
+ self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
783
+
784
+ if config.bf16:
785
+ self.transformer.bfloat16()
786
+ self.lm_head.bfloat16()
787
+ if config.fp16:
788
+ self.transformer.half()
789
+ self.lm_head.half()
790
+ self.post_init()
791
+
792
+ def get_output_embeddings(self):
793
+ return self.lm_head
794
+
795
+ def set_output_embeddings(self, new_embeddings):
796
+ self.lm_head = new_embeddings
797
+
798
+ def prepare_inputs_for_generation(
799
+ self, input_ids, past_key_values=None, inputs_embeds=None, **kwargs
800
+ ):
801
+ token_type_ids = kwargs.get("token_type_ids", None)
802
+ if past_key_values:
803
+ input_ids = input_ids[:, -1].unsqueeze(-1)
804
+ if token_type_ids is not None:
805
+ token_type_ids = token_type_ids[:, -1].unsqueeze(-1)
806
+
807
+ attention_mask = kwargs.get("attention_mask", None)
808
+ position_ids = kwargs.get("position_ids", None)
809
+
810
+ if attention_mask is not None and position_ids is None:
811
+ position_ids = attention_mask.long().cumsum(-1) - 1
812
+ position_ids.masked_fill_(attention_mask == 0, 1)
813
+ if past_key_values:
814
+ position_ids = position_ids[:, -1].unsqueeze(-1)
815
+ else:
816
+ position_ids = None
817
+
818
+ if inputs_embeds is not None and past_key_values is None:
819
+ model_inputs = {"inputs_embeds": inputs_embeds}
820
+ else:
821
+ model_inputs = {"input_ids": input_ids}
822
+
823
+ model_inputs.update(
824
+ {
825
+ "past_key_values": past_key_values,
826
+ "use_cache": kwargs.get("use_cache"),
827
+ "position_ids": position_ids,
828
+ "attention_mask": attention_mask,
829
+ "token_type_ids": token_type_ids,
830
+ }
831
+ )
832
+ return model_inputs
833
+
834
+ def forward(
835
+ self,
836
+ input_ids: Optional[torch.LongTensor] = None,
837
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor]]] = None,
838
+ attention_mask: Optional[torch.FloatTensor] = None,
839
+ token_type_ids: Optional[torch.LongTensor] = None,
840
+ position_ids: Optional[torch.LongTensor] = None,
841
+ head_mask: Optional[torch.FloatTensor] = None,
842
+ inputs_embeds: Optional[torch.FloatTensor] = None,
843
+ encoder_hidden_states: Optional[torch.Tensor] = None,
844
+ encoder_attention_mask: Optional[torch.FloatTensor] = None,
845
+ labels: Optional[torch.LongTensor] = None,
846
+ use_cache: Optional[bool] = None,
847
+ output_attentions: Optional[bool] = None,
848
+ output_hidden_states: Optional[bool] = None,
849
+ return_dict: Optional[bool] = None,
850
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
851
+
852
+ return_dict = (
853
+ return_dict if return_dict is not None else self.config.use_return_dict
854
+ )
855
+
856
+ transformer_outputs = self.transformer(
857
+ input_ids,
858
+ past_key_values=past_key_values,
859
+ attention_mask=attention_mask,
860
+ token_type_ids=token_type_ids,
861
+ position_ids=position_ids,
862
+ head_mask=head_mask,
863
+ inputs_embeds=inputs_embeds,
864
+ encoder_hidden_states=encoder_hidden_states,
865
+ encoder_attention_mask=encoder_attention_mask,
866
+ use_cache=use_cache,
867
+ output_attentions=output_attentions,
868
+ output_hidden_states=output_hidden_states,
869
+ return_dict=return_dict,
870
+ )
871
+ hidden_states = transformer_outputs[0]
872
+
873
+ lm_logits = self.lm_head(hidden_states)
874
+
875
+ loss = None
876
+ if labels is not None:
877
+ labels = labels.to(lm_logits.device)
878
+ shift_logits = lm_logits[..., :-1, :].contiguous()
879
+ shift_labels = labels[..., 1:].contiguous()
880
+ loss_fct = CrossEntropyLoss()
881
+ loss = loss_fct(
882
+ shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1)
883
+ )
884
+
885
+ if not return_dict:
886
+ output = (lm_logits,) + transformer_outputs[1:]
887
+ return ((loss,) + output) if loss is not None else output
888
+
889
+ return CausalLMOutputWithPast(
890
+ loss=loss,
891
+ logits=lm_logits,
892
+ past_key_values=transformer_outputs.past_key_values,
893
+ hidden_states=transformer_outputs.hidden_states,
894
+ attentions=transformer_outputs.attentions,
895
+ )
896
+
897
+ @staticmethod
898
+ def _reorder_cache(
899
+ past_key_values: Tuple[Tuple[torch.Tensor]], beam_idx: torch.Tensor
900
+ ) -> Tuple[Tuple[torch.Tensor]]:
901
+
902
+ return tuple(
903
+ tuple(
904
+ past_state.index_select(0, beam_idx.to(past_state.device))
905
+ for past_state in layer_past
906
+ )
907
+ for layer_past in past_key_values
908
+ )
909
+
910
+ def chat(
911
+ self,
912
+ tokenizer: PreTrainedTokenizer,
913
+ query: str,
914
+ history: Optional[HistoryType],
915
+ system: str = "You are a helpful assistant.",
916
+ append_history: bool = True,
917
+ stream: Optional[bool] = _SENTINEL,
918
+ stop_words_ids: Optional[List[List[int]]] = None,
919
+ generation_config: Optional[GenerationConfig] = None,
920
+ **kwargs,
921
+ ) -> Tuple[str, HistoryType]:
922
+ generation_config = generation_config if generation_config is not None else self.generation_config
923
+
924
+ assert stream is _SENTINEL, _ERROR_STREAM_IN_CHAT
925
+ assert generation_config.chat_format == 'chatml', _ERROR_BAD_CHAT_FORMAT
926
+ if history is None:
927
+ history = []
928
+ if stop_words_ids is None:
929
+ stop_words_ids = []
930
+
931
+ max_window_size = kwargs.get('max_window_size', None)
932
+ if max_window_size is None:
933
+ max_window_size = generation_config.max_window_size
934
+ raw_text, context_tokens = make_context(
935
+ tokenizer,
936
+ query,
937
+ history=history,
938
+ system=system,
939
+ max_window_size=max_window_size,
940
+ chat_format=generation_config.chat_format,
941
+ )
942
+
943
+ stop_words_ids.extend(get_stop_words_ids(
944
+ generation_config.chat_format, tokenizer
945
+ ))
946
+ input_ids = torch.tensor([context_tokens]).to(self.device)
947
+ outputs = self.generate(
948
+ input_ids,
949
+ stop_words_ids=stop_words_ids,
950
+ return_dict_in_generate=False,
951
+ generation_config=generation_config,
952
+ **kwargs,
953
+ )
954
+
955
+ response = decode_tokens(
956
+ outputs[0],
957
+ tokenizer,
958
+ raw_text_len=len(raw_text),
959
+ context_length=len(context_tokens),
960
+ chat_format=generation_config.chat_format,
961
+ verbose=False,
962
+ errors='replace'
963
+ )
964
+
965
+ if append_history:
966
+ history.append((query, response))
967
+
968
+ return response, history
969
+
970
+ def chat_stream(
971
+ self,
972
+ tokenizer: PreTrainedTokenizer,
973
+ query: str,
974
+ history: Optional[HistoryType],
975
+ system: str = "You are a helpful assistant.",
976
+ stop_words_ids: Optional[List[List[int]]] = None,
977
+ logits_processor: Optional[LogitsProcessorList] = None,
978
+ generation_config: Optional[GenerationConfig] = None,
979
+ **kwargs,
980
+ ) -> Generator[str, Any, None]:
981
+ generation_config = generation_config if generation_config is not None else self.generation_config
982
+ assert generation_config.chat_format == 'chatml', _ERROR_BAD_CHAT_FORMAT
983
+ if history is None:
984
+ history = []
985
+ if stop_words_ids is None:
986
+ stop_words_ids = []
987
+
988
+ max_window_size = kwargs.get('max_window_size', None)
989
+ if max_window_size is None:
990
+ max_window_size = generation_config.max_window_size
991
+ raw_text, context_tokens = make_context(
992
+ tokenizer,
993
+ query,
994
+ history=history,
995
+ system=system,
996
+ max_window_size=max_window_size,
997
+ chat_format=generation_config.chat_format,
998
+ )
999
+
1000
+ stop_words_ids.extend(get_stop_words_ids(
1001
+ generation_config.chat_format, tokenizer
1002
+ ))
1003
+ if stop_words_ids is not None:
1004
+ stop_words_logits_processor = StopWordsLogitsProcessor(
1005
+ stop_words_ids=stop_words_ids,
1006
+ eos_token_id=generation_config.eos_token_id,
1007
+ )
1008
+ if logits_processor is None:
1009
+ logits_processor = LogitsProcessorList([stop_words_logits_processor])
1010
+ else:
1011
+ logits_processor.append(stop_words_logits_processor)
1012
+ input_ids = torch.tensor([context_tokens]).to(self.device)
1013
+
1014
+ from transformers_stream_generator.main import NewGenerationMixin, StreamGenerationConfig
1015
+ self.__class__.generate_stream = NewGenerationMixin.generate
1016
+ self.__class__.sample_stream = NewGenerationMixin.sample_stream
1017
+ stream_config = StreamGenerationConfig(**generation_config.to_dict(), do_stream=True)
1018
+
1019
+ def stream_generator():
1020
+ outputs = []
1021
+ for token in self.generate_stream(
1022
+ input_ids,
1023
+ return_dict_in_generate=False,
1024
+ generation_config=stream_config,
1025
+ logits_processor=logits_processor,
1026
+ seed=-1,
1027
+ **kwargs):
1028
+ outputs.append(token.item())
1029
+ yield tokenizer.decode(outputs, skip_special_tokens=True, errors='ignore', keep_image_special=True)
1030
+
1031
+ return stream_generator()
1032
+
1033
+ def generate(
1034
+ self,
1035
+ inputs: Optional[torch.Tensor] = None,
1036
+ generation_config: Optional[GenerationConfig] = None,
1037
+ logits_processor: Optional[LogitsProcessorList] = None,
1038
+ stopping_criteria: Optional[StoppingCriteriaList] = None,
1039
+ prefix_allowed_tokens_fn: Optional[
1040
+ Callable[[int, torch.Tensor], List[int]]
1041
+ ] = None,
1042
+ synced_gpus: Optional[bool] = None,
1043
+ assistant_model: Optional["PreTrainedModel"] = None,
1044
+ streamer: Optional["BaseStreamer"] = None,
1045
+ **kwargs,
1046
+ ) -> Union[GenerateOutput, torch.LongTensor]:
1047
+ generation_config = generation_config if generation_config is not None else self.generation_config
1048
+
1049
+ # Process stop_words_ids.
1050
+ stop_words_ids = kwargs.pop("stop_words_ids", None)
1051
+ if stop_words_ids is None and generation_config is not None:
1052
+ stop_words_ids = getattr(generation_config, "stop_words_ids", None)
1053
+ if stop_words_ids is None:
1054
+ stop_words_ids = getattr(generation_config, "stop_words_ids", None)
1055
+
1056
+ if stop_words_ids is not None:
1057
+ stop_words_logits_processor = StopWordsLogitsProcessor(
1058
+ stop_words_ids=stop_words_ids,
1059
+ eos_token_id=generation_config.eos_token_id,
1060
+ )
1061
+ if logits_processor is None:
1062
+ logits_processor = LogitsProcessorList([stop_words_logits_processor])
1063
+ else:
1064
+ logits_processor.append(stop_words_logits_processor)
1065
+
1066
+ return super().generate(
1067
+ inputs,
1068
+ generation_config=generation_config,
1069
+ logits_processor=logits_processor,
1070
+ stopping_criteria=stopping_criteria,
1071
+ prefix_allowed_tokens_fn=prefix_allowed_tokens_fn,
1072
+ synced_gpus=synced_gpus,
1073
+ assistant_model=assistant_model,
1074
+ streamer=streamer,
1075
+ **kwargs,
1076
+ )
1077
+
1078
+
1079
+ class RotaryEmbedding(torch.nn.Module):
1080
+ def __init__(self, dim, base=10000):
1081
+ super().__init__()
1082
+ self.dim = dim
1083
+ self.base = base
1084
+ self.inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
1085
+ if importlib.util.find_spec("einops") is None:
1086
+ raise RuntimeError("einops is required for Rotary Embedding")
1087
+
1088
+ self._rotary_pos_emb_cache = None
1089
+ self._seq_len_cached = 0
1090
+ self._ntk_alpha_cached = 1.0
1091
+
1092
+ def update_rotary_pos_emb_cache(self, max_seq_len, offset=0, ntk_alpha=1.0):
1093
+ seqlen = max_seq_len + offset
1094
+ if seqlen > self._seq_len_cached or ntk_alpha != self._ntk_alpha_cached:
1095
+ base = self.base * ntk_alpha ** (self.dim / (self.dim - 2))
1096
+ self.inv_freq = 1.0 / (
1097
+ base
1098
+ ** (
1099
+ torch.arange(0, self.dim, 2, device=self.inv_freq.device).float()
1100
+ / self.dim
1101
+ )
1102
+ )
1103
+ self._seq_len_cached = max(2 * seqlen, 16)
1104
+ self._ntk_alpha_cached = ntk_alpha
1105
+ seq = torch.arange(self._seq_len_cached, device=self.inv_freq.device)
1106
+ freqs = torch.outer(seq.type_as(self.inv_freq), self.inv_freq)
1107
+
1108
+ emb = torch.cat((freqs, freqs), dim=-1)
1109
+ from einops import rearrange
1110
+
1111
+ emb = rearrange(emb, "n d -> 1 n 1 d")
1112
+
1113
+ cos, sin = emb.cos(), emb.sin()
1114
+ self._rotary_pos_emb_cache = [cos, sin]
1115
+
1116
+ def forward(self, max_seq_len, offset=0, ntk_alpha=1.0):
1117
+ self.update_rotary_pos_emb_cache(max_seq_len, offset, ntk_alpha)
1118
+ cos, sin = self._rotary_pos_emb_cache
1119
+ return [cos[:, offset : offset + max_seq_len], sin[:, offset : offset + max_seq_len]]
1120
+
1121
+
1122
+ def _rotate_half(x):
1123
+ from einops import rearrange
1124
+
1125
+ x = rearrange(x, "... (j d) -> ... j d", j=2)
1126
+ x1, x2 = x.unbind(dim=-2)
1127
+ return torch.cat((-x2, x1), dim=-1)
1128
+
1129
+
1130
+ def apply_rotary_pos_emb(t, freqs):
1131
+ cos, sin = freqs
1132
+ if apply_rotary_emb_func is not None and t.is_cuda:
1133
+ t_ = t.float()
1134
+ cos = cos.squeeze(0).squeeze(1)[:, : cos.shape[-1] // 2]
1135
+ sin = sin.squeeze(0).squeeze(1)[:, : sin.shape[-1] // 2]
1136
+ output = apply_rotary_emb_func(t_, cos, sin).type_as(t)
1137
+ return output
1138
+ else:
1139
+ rot_dim = freqs[0].shape[-1]
1140
+ cos, sin = freqs
1141
+ t_, t_pass_ = t[..., :rot_dim], t[..., rot_dim:]
1142
+ t_ = t_.float()
1143
+ t_pass_ = t_pass_.float()
1144
+ t_ = (t_ * cos) + (_rotate_half(t_) * sin)
1145
+ return torch.cat((t_, t_pass_), dim=-1).type_as(t)
1146
+
1147
+
1148
+ class RMSNorm(torch.nn.Module):
1149
+ def __init__(self, dim: int, eps: float = 1e-6):
1150
+ super().__init__()
1151
+ self.eps = eps
1152
+ self.weight = nn.Parameter(torch.ones(dim))
1153
+
1154
+ def _norm(self, x):
1155
+ return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
1156
+
1157
+ def forward(self, x):
1158
+ if rms_norm is not None and x.is_cuda:
1159
+ return rms_norm(x, self.weight, self.eps)
1160
+ else:
1161
+ output = self._norm(x.float()).type_as(x)
1162
+ return output * self.weight
qwen.tiktoken ADDED
The diff for this file is too large to render. See raw diff
 
qwen_generation_utils.py ADDED
@@ -0,0 +1,420 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """Generation support."""
7
+
8
+ from typing import Tuple, List, Union, Iterable
9
+
10
+ import numpy as np
11
+ import torch
12
+ import torch.nn.functional as F
13
+ from transformers import PreTrainedTokenizer
14
+ from transformers import logging
15
+ from transformers.generation import LogitsProcessor
16
+
17
+ logger = logging.get_logger(__name__)
18
+
19
+ # Types.
20
+ HistoryType = List[Tuple[str, str]]
21
+ TokensType = List[int]
22
+ BatchTokensType = List[List[int]]
23
+
24
+
25
+ def pad_batch(batch: BatchTokensType, pad_id: int, seq_length: int) -> BatchTokensType:
26
+ for tokens in batch:
27
+ context_length = len(tokens)
28
+ if context_length < seq_length:
29
+ tokens.extend([pad_id] * (seq_length - context_length))
30
+ return batch
31
+
32
+
33
+ def get_ltor_masks_and_position_ids(
34
+ data,
35
+ eod_token,
36
+ reset_position_ids,
37
+ reset_attention_mask,
38
+ eod_mask_loss,
39
+ ):
40
+ """Build masks and position id for left to right model."""
41
+
42
+ # Extract batch size and sequence length.
43
+ micro_batch_size, seq_length = data.size()
44
+
45
+ # Attention mask (lower triangular).
46
+ if reset_attention_mask:
47
+ att_mask_batch = micro_batch_size
48
+ else:
49
+ att_mask_batch = 1
50
+ attention_mask = torch.tril(
51
+ torch.ones((att_mask_batch, seq_length, seq_length), device=data.device)
52
+ ).view(att_mask_batch, 1, seq_length, seq_length)
53
+
54
+ # Loss mask.
55
+ loss_mask = torch.ones(data.size(), dtype=torch.float, device=data.device)
56
+ if eod_mask_loss:
57
+ loss_mask[data == eod_token] = 0.0
58
+
59
+ # Position ids.
60
+ position_ids = torch.arange(seq_length, dtype=torch.long, device=data.device)
61
+ position_ids = position_ids.unsqueeze(0).expand_as(data)
62
+ # We need to clone as the ids will be modifed based on batch index.
63
+ if reset_position_ids:
64
+ position_ids = position_ids.clone()
65
+
66
+ if reset_position_ids or reset_attention_mask:
67
+ # Loop through the batches:
68
+ for b in range(micro_batch_size):
69
+
70
+ # Find indecies where EOD token is.
71
+ eod_index = position_ids[b, data[b] == eod_token]
72
+ # Detach indecies from positions if going to modify positions.
73
+ if reset_position_ids:
74
+ eod_index = eod_index.clone()
75
+
76
+ # Loop through EOD indecies:
77
+ prev_index = 0
78
+ for j in range(eod_index.size()[0]):
79
+ i = eod_index[j]
80
+ # Mask attention loss.
81
+ if reset_attention_mask:
82
+ attention_mask[b, 0, (i + 1) :, : (i + 1)] = 0
83
+ # Reset positions.
84
+ if reset_position_ids:
85
+ position_ids[b, (i + 1) :] -= i + 1 - prev_index
86
+ prev_index = i + 1
87
+
88
+ # Convert attention mask to binary:
89
+ attention_mask = attention_mask < 0.5
90
+
91
+ return attention_mask, loss_mask, position_ids
92
+
93
+
94
+ def get_batch(context_tokens: torch.LongTensor, eod_id: int):
95
+ """Generate batch from context tokens."""
96
+ # Move to GPU.
97
+ tokens = context_tokens.contiguous().to(context_tokens.device)
98
+ # Get the attention mask and postition ids.
99
+ attention_mask, _, position_ids = get_ltor_masks_and_position_ids(
100
+ tokens,
101
+ eod_id,
102
+ reset_position_ids=False,
103
+ reset_attention_mask=False,
104
+ eod_mask_loss=False,
105
+ )
106
+ return tokens, attention_mask, position_ids
107
+
108
+
109
+ def get_stop_words_ids(chat_format, tokenizer):
110
+ if chat_format == "raw":
111
+ stop_words_ids = [tokenizer.encode("Human:"), [tokenizer.eod_id]]
112
+ elif chat_format == "chatml":
113
+ stop_words_ids = [[tokenizer.im_end_id], [tokenizer.im_start_id]]
114
+ else:
115
+ raise NotImplementedError(f"Unknown chat format {chat_format!r}")
116
+ return stop_words_ids
117
+
118
+
119
+ def make_context(
120
+ tokenizer: PreTrainedTokenizer,
121
+ query: str,
122
+ history: List[Tuple[str, str]] = None,
123
+ system: str = "",
124
+ max_window_size: int = 6144,
125
+ chat_format: str = "chatml",
126
+ ):
127
+ if history is None:
128
+ history = []
129
+
130
+ if chat_format == "chatml":
131
+ im_start, im_end = "<|im_start|>", "<|im_end|>"
132
+ im_start_tokens = [tokenizer.im_start_id]
133
+ im_end_tokens = [tokenizer.im_end_id]
134
+ nl_tokens = tokenizer.encode("\n")
135
+
136
+ def _tokenize_str(role, content):
137
+ return f"{role}\n{content}", tokenizer.encode(
138
+ role, allowed_special=set(tokenizer.IMAGE_ST)
139
+ ) + nl_tokens + tokenizer.encode(content, allowed_special=set(tokenizer.IMAGE_ST))
140
+
141
+ system_text, system_tokens_part = _tokenize_str("system", system)
142
+ system_tokens = im_start_tokens + system_tokens_part + im_end_tokens
143
+
144
+ raw_text = ""
145
+ context_tokens = []
146
+
147
+ for turn_query, turn_response in reversed(history):
148
+ query_text, query_tokens_part = _tokenize_str("user", turn_query)
149
+ query_tokens = im_start_tokens + query_tokens_part + im_end_tokens
150
+ if turn_response is not None:
151
+ response_text, response_tokens_part = _tokenize_str(
152
+ "assistant", turn_response
153
+ )
154
+ response_tokens = im_start_tokens + response_tokens_part + im_end_tokens
155
+
156
+ next_context_tokens = nl_tokens + query_tokens + nl_tokens + response_tokens
157
+ prev_chat = (
158
+ f"\n{im_start}{query_text}{im_end}\n{im_start}{response_text}{im_end}"
159
+ )
160
+ else:
161
+ next_context_tokens = nl_tokens + query_tokens + nl_tokens
162
+ prev_chat = f"\n{im_start}{query_text}{im_end}\n"
163
+
164
+ current_context_size = (
165
+ len(system_tokens) + len(next_context_tokens) + len(context_tokens)
166
+ )
167
+ if current_context_size < max_window_size:
168
+ context_tokens = next_context_tokens + context_tokens
169
+ raw_text = prev_chat + raw_text
170
+ else:
171
+ break
172
+
173
+ context_tokens = system_tokens + context_tokens
174
+ raw_text = f"{im_start}{system_text}{im_end}" + raw_text
175
+ context_tokens += (
176
+ nl_tokens
177
+ + im_start_tokens
178
+ + _tokenize_str("user", query)[1]
179
+ + im_end_tokens
180
+ + nl_tokens
181
+ + im_start_tokens
182
+ + tokenizer.encode("assistant")
183
+ + nl_tokens
184
+ )
185
+ raw_text += f"\n{im_start}user\n{query}{im_end}\n{im_start}assistant\n"
186
+
187
+ elif chat_format == "raw":
188
+ raw_text = query
189
+ context_tokens = tokenizer.encode(raw_text)
190
+ else:
191
+ raise NotImplementedError(f"Unknown chat format {chat_format!r}")
192
+
193
+ return raw_text, context_tokens
194
+
195
+
196
+ def _decode_default(
197
+ tokens: List[int],
198
+ *,
199
+ stop_words: List[str],
200
+ eod_words: List[str],
201
+ tokenizer: PreTrainedTokenizer,
202
+ raw_text_len: int,
203
+ verbose: bool = False,
204
+ return_end_reason: bool = False,
205
+ errors: str='replace',
206
+ ):
207
+ trim_decode_tokens = tokenizer.decode(tokens, errors=errors)[raw_text_len:]
208
+ if verbose:
209
+ print("\nRaw Generate: ", trim_decode_tokens)
210
+
211
+ end_reason = f"Gen length {len(tokens)}"
212
+ for stop_word in stop_words:
213
+ trim_decode_tokens = trim_decode_tokens.replace(stop_word, "").strip()
214
+ for eod_word in eod_words:
215
+ if eod_word in trim_decode_tokens:
216
+ end_reason = f"Gen {eod_word!r}"
217
+ trim_decode_tokens = trim_decode_tokens.split(eod_word)[0]
218
+ trim_decode_tokens = trim_decode_tokens.strip()
219
+ if verbose:
220
+ print("\nEnd Reason:", end_reason)
221
+ print("\nGenerate: ", trim_decode_tokens)
222
+
223
+ if return_end_reason:
224
+ return trim_decode_tokens, end_reason
225
+ else:
226
+ return trim_decode_tokens
227
+
228
+
229
+ def _decode_chatml(
230
+ tokens: List[int],
231
+ *,
232
+ stop_words: List[str],
233
+ eod_token_ids: List[int],
234
+ tokenizer: PreTrainedTokenizer,
235
+ raw_text_len: int,
236
+ context_length: int,
237
+ verbose: bool = False,
238
+ return_end_reason: bool = False,
239
+ errors: str='replace'
240
+ ):
241
+ end_reason = f"Gen length {len(tokens)}"
242
+ eod_token_idx = context_length
243
+ for eod_token_idx in range(context_length, len(tokens)):
244
+ if tokens[eod_token_idx] in eod_token_ids:
245
+ end_reason = f"Gen {tokenizer.decode([tokens[eod_token_idx]])!r}"
246
+ break
247
+
248
+ trim_decode_tokens = tokenizer.decode(tokens[:eod_token_idx], errors=errors)[raw_text_len:]
249
+ if verbose:
250
+ print("\nRaw Generate w/o EOD:", tokenizer.decode(tokens, errors=errors)[raw_text_len:])
251
+ print("\nRaw Generate:", trim_decode_tokens)
252
+ print("\nEnd Reason:", end_reason)
253
+ for stop_word in stop_words:
254
+ trim_decode_tokens = trim_decode_tokens.replace(stop_word, "").strip()
255
+ trim_decode_tokens = trim_decode_tokens.strip()
256
+ if verbose:
257
+ print("\nGenerate:", trim_decode_tokens)
258
+
259
+ if return_end_reason:
260
+ return trim_decode_tokens, end_reason
261
+ else:
262
+ return trim_decode_tokens
263
+
264
+
265
+ def decode_tokens(
266
+ tokens: Union[torch.LongTensor, TokensType],
267
+ tokenizer: PreTrainedTokenizer,
268
+ raw_text_len: int,
269
+ context_length: int,
270
+ chat_format: str,
271
+ verbose: bool = False,
272
+ return_end_reason: bool = False,
273
+ errors: str="replace",
274
+ ) -> str:
275
+ if torch.is_tensor(tokens):
276
+ tokens = tokens.cpu().numpy().tolist()
277
+
278
+ if chat_format == "chatml":
279
+ return _decode_chatml(
280
+ tokens,
281
+ stop_words=[],
282
+ eod_token_ids=[tokenizer.im_start_id, tokenizer.im_end_id],
283
+ tokenizer=tokenizer,
284
+ raw_text_len=raw_text_len,
285
+ context_length=context_length,
286
+ verbose=verbose,
287
+ return_end_reason=return_end_reason,
288
+ errors=errors,
289
+ )
290
+ elif chat_format == "raw":
291
+ return _decode_default(
292
+ tokens,
293
+ stop_words=["<|endoftext|>"],
294
+ eod_words=["<|endoftext|>"],
295
+ tokenizer=tokenizer,
296
+ raw_text_len=raw_text_len,
297
+ verbose=verbose,
298
+ return_end_reason=return_end_reason,
299
+ errors=errors,
300
+ )
301
+ else:
302
+ raise NotImplementedError(f"Unknown chat format {chat_format!r}")
303
+
304
+
305
+ class StopWordsLogitsProcessor(LogitsProcessor):
306
+ """
307
+ :class:`transformers.LogitsProcessor` that enforces that when specified sequences appear, stop geration.
308
+
309
+ Args:
310
+ stop_words_ids (:obj:`List[List[int]]`):
311
+ List of list of token ids of stop ids. In order to get the tokens of the words
312
+ that should not appear in the generated text, use :obj:`tokenizer(bad_word,
313
+ add_prefix_space=True).input_ids`.
314
+ eos_token_id (:obj:`int`):
315
+ The id of the `end-of-sequence` token.
316
+ """
317
+
318
+ def __init__(self, stop_words_ids: Iterable[Iterable[int]], eos_token_id: int):
319
+
320
+ if not isinstance(stop_words_ids, List) or len(stop_words_ids) == 0:
321
+ raise ValueError(
322
+ f"`stop_words_ids` has to be a non-emtpy list, but is {stop_words_ids}."
323
+ )
324
+ if any(not isinstance(bad_word_ids, list) for bad_word_ids in stop_words_ids):
325
+ raise ValueError(
326
+ f"`stop_words_ids` has to be a list of lists, but is {stop_words_ids}."
327
+ )
328
+ if any(
329
+ any(
330
+ (not isinstance(token_id, (int, np.integer)) or token_id < 0)
331
+ for token_id in stop_word_ids
332
+ )
333
+ for stop_word_ids in stop_words_ids
334
+ ):
335
+ raise ValueError(
336
+ f"Each list in `stop_words_ids` has to be a list of positive integers, but is {stop_words_ids}."
337
+ )
338
+
339
+ self.stop_words_ids = list(
340
+ filter(
341
+ lambda bad_token_seq: bad_token_seq != [eos_token_id], stop_words_ids
342
+ )
343
+ )
344
+ self.eos_token_id = eos_token_id
345
+ for stop_token_seq in self.stop_words_ids:
346
+ assert (
347
+ len(stop_token_seq) > 0
348
+ ), "Stop words token sequences {} cannot have an empty list".format(
349
+ stop_words_ids
350
+ )
351
+
352
+ def __call__(
353
+ self, input_ids: torch.LongTensor, scores: torch.FloatTensor
354
+ ) -> torch.FloatTensor:
355
+ stopped_samples = self._calc_stopped_samples(input_ids)
356
+ for i, should_stop in enumerate(stopped_samples):
357
+ if should_stop:
358
+ scores[i, self.eos_token_id] = float(2**15)
359
+ return scores
360
+
361
+ def _tokens_match(self, prev_tokens: torch.LongTensor, tokens: List[int]) -> bool:
362
+ if len(tokens) == 0:
363
+ # if bad word tokens is just one token always ban it
364
+ return True
365
+ elif len(tokens) > len(prev_tokens):
366
+ # if bad word tokens are longer then prev input_ids they can't be equal
367
+ return False
368
+ elif prev_tokens[-len(tokens) :].tolist() == tokens:
369
+ # if tokens match
370
+ return True
371
+ else:
372
+ return False
373
+
374
+ def _calc_stopped_samples(self, prev_input_ids: Iterable[int]) -> Iterable[int]:
375
+ stopped_samples = []
376
+ for prev_input_ids_slice in prev_input_ids:
377
+ match = False
378
+ for stop_token_seq in self.stop_words_ids:
379
+ if self._tokens_match(prev_input_ids_slice, stop_token_seq):
380
+ # if tokens do not match continue
381
+ match = True
382
+ break
383
+ stopped_samples.append(match)
384
+
385
+ return stopped_samples
386
+
387
+
388
+ def top_k_logits(logits, top_k=0, top_p=0.0, filter_value=-float("Inf")):
389
+ """This function has been mostly taken from huggingface conversational
390
+ ai code at
391
+ https://medium.com/huggingface/how-to-build-a-state-of-the-art-
392
+ conversational-ai-with-transfer-learning-2d818ac26313"""
393
+
394
+ if top_k > 0:
395
+ # Remove all tokens with a probability less than the
396
+ # last token of the top-k
397
+ indices_to_remove = logits < torch.topk(logits, top_k)[0][..., -1, None]
398
+ logits[indices_to_remove] = filter_value
399
+
400
+ if top_p > 0.0:
401
+ # Cconvert to 1D
402
+ sorted_logits, sorted_indices = torch.sort(logits, descending=True, dim=-1)
403
+ cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
404
+
405
+ # Remove tokens with cumulative probability above the threshold
406
+ sorted_indices_to_remove = cumulative_probs > top_p
407
+ # Shift the indices to the right to keep also the first token
408
+ # above the threshold
409
+ sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
410
+ sorted_indices_to_remove[..., 0] = 0
411
+ for i in range(sorted_indices.size(0)):
412
+ indices_to_remove = sorted_indices[i][sorted_indices_to_remove[i]]
413
+ logits[i][indices_to_remove] = filter_value
414
+
415
+ return logits
416
+
417
+
418
+ def switch(val1, val2, boolean):
419
+ boolean = boolean.type_as(val1)
420
+ return (1 - boolean) * val1 + boolean * val2
rng_state_0.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:569e3f96e00ce6f71c26359f4c3d353fdf576b977f9f1713b440b0de9fb09ce8
3
+ size 21687
rng_state_1.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:73d87b20a91ecef4d643e58a3fd10c41ff2bf9c4064b760dc40e29ace2e594e6
3
+ size 21687
rng_state_2.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9bcf53341194f948fcf9a3e26ad99832df1a48167bd687bf0a3c6c2a25771aa2
3
+ size 21687
rng_state_3.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1b107a39e25d7ad4a8497408ddd779fafbe9135b11896245877b7a4b6c0d8d30
3
+ size 21687
rng_state_4.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:34d3297c91cb02dc5bc5651ee04fcf26c836094e58aea2d90af5392c51300cb7
3
+ size 21687
rng_state_5.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:feaf8db8c36694d11d8c5ae2f94c53c602030053aab3bbe19182febea03576f6
3
+ size 21687
rng_state_6.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6fe964ab071c98fecd13b4cc37d7060325abbdd909b545a66cf3beb9cda1c83b
3
+ size 21687
rng_state_7.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0f6fda2084a2af7ecf370c6a8aa825344fa1aec4d118137a27a983dfc78aca36
3
+ size 21687
special_tokens_map.json ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {
2
+ "pad_token": "<|endoftext|>"
3
+ }
tokenization_qwen.py ADDED
@@ -0,0 +1,611 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ """Tokenization classes for QWen."""
7
+
8
+ import base64
9
+ import logging
10
+ import os
11
+ import requests
12
+ import unicodedata
13
+ from typing import Collection, Dict, List, Set, Tuple, Union, Any, Callable, Optional
14
+
15
+ import tiktoken
16
+ import numpy as np
17
+ from PIL import Image
18
+ from PIL import ImageFont
19
+ from PIL import ImageDraw
20
+ from transformers import PreTrainedTokenizer, AddedToken
21
+ from transformers.utils import try_to_load_from_cache
22
+
23
+ import matplotlib.colors as mcolors
24
+ from matplotlib.font_manager import FontProperties
25
+
26
+ logger = logging.getLogger(__name__)
27
+
28
+
29
+ VOCAB_FILES_NAMES = {"vocab_file": "qwen.tiktoken", "ttf": "SimSun.ttf"}
30
+ FONT_PATH = try_to_load_from_cache("Qwen/Qwen-VL-Chat", "SimSun.ttf")
31
+ if FONT_PATH is None:
32
+ if not os.path.exists("SimSun.ttf"):
33
+ ttf = requests.get("https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/SimSun.ttf")
34
+ open("SimSun.ttf", "wb").write(ttf.content)
35
+ FONT_PATH = "SimSun.ttf"
36
+
37
+ PAT_STR = r"""(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\r\n\p{L}\p{N}]?\p{L}+|\p{N}| ?[^\s\p{L}\p{N}]+[\r\n]*|\s*[\r\n]+|\s+(?!\S)|\s+"""
38
+ ENDOFTEXT = "<|endoftext|>"
39
+ IMSTART = "<|im_start|>"
40
+ IMEND = "<|im_end|>"
41
+ # as the default behavior is changed to allow special tokens in
42
+ # regular texts, the surface forms of special tokens need to be
43
+ # as different as possible to minimize the impact
44
+ EXTRAS = tuple((f"<|extra_{i}|>" for i in range(205)))
45
+ SPECIAL_TOKENS = (
46
+ ENDOFTEXT,
47
+ IMSTART,
48
+ IMEND,
49
+ ) + EXTRAS
50
+ IMG_TOKEN_SPAN = 256
51
+
52
+
53
+ def _load_tiktoken_bpe(tiktoken_bpe_file: str) -> Dict[bytes, int]:
54
+ with open(tiktoken_bpe_file, "rb") as f:
55
+ contents = f.read()
56
+ return {
57
+ base64.b64decode(token): int(rank)
58
+ for token, rank in (line.split() for line in contents.splitlines() if line)
59
+ }
60
+
61
+ def _list_find(
62
+ input_list: List[Any],
63
+ candidates: Tuple[Any],
64
+ start: int = 0,
65
+ ):
66
+ for i in range(start, len(input_list)):
67
+ if input_list[i] in candidates:
68
+ return i
69
+ return -1
70
+
71
+ def _replace_closed_tag(
72
+ input_tokens: List[Any],
73
+ start_tags: Union[Any, Tuple[Any]],
74
+ end_tags: Union[Any, Tuple[Any]],
75
+ inclusive_replace_func: Callable,
76
+ exclusive_replace_func: Callable = lambda x: x,
77
+ ):
78
+ if isinstance(start_tags, (str, int)):
79
+ start_tags = (start_tags,)
80
+ if isinstance(end_tags, (str, int)):
81
+ end_tags = (end_tags,)
82
+ assert len(start_tags) == len(end_tags)
83
+
84
+ output_tokens = []
85
+ end = 0
86
+ while True:
87
+ start = _list_find(input_tokens, start_tags, end)
88
+ if start == -1:
89
+ break
90
+ output_tokens.extend(exclusive_replace_func(input_tokens[end : start]))
91
+ tag_idx = start_tags.index(input_tokens[start])
92
+ end = _list_find(input_tokens, (end_tags[tag_idx],), start)
93
+ if end == -1:
94
+ raise ValueError("Unclosed image token")
95
+ output_tokens.extend(inclusive_replace_func(input_tokens[start : end + 1]))
96
+ end += 1
97
+ output_tokens.extend(exclusive_replace_func(input_tokens[end : ]))
98
+ return output_tokens
99
+
100
+ class QWenTokenizer(PreTrainedTokenizer):
101
+ """QWen tokenizer."""
102
+
103
+ vocab_files_names = VOCAB_FILES_NAMES
104
+
105
+ def __init__(
106
+ self,
107
+ vocab_file,
108
+ errors="replace",
109
+ image_start_tag='<img>',
110
+ image_end_tag='</img>',
111
+ image_pad_tag='<imgpad>',
112
+ ref_start_tag='<ref>',
113
+ ref_end_tag='</ref>',
114
+ box_start_tag='<box>',
115
+ box_end_tag='</box>',
116
+ quad_start_tag='<quad>',
117
+ quad_end_tag='</quad>',
118
+ **kwargs,
119
+ ):
120
+ self.image_start_tag = image_start_tag
121
+ self.image_end_tag = image_end_tag
122
+ self.image_pad_tag = image_pad_tag
123
+ self.ref_start_tag = ref_start_tag
124
+ self.ref_end_tag = ref_end_tag
125
+ self.box_start_tag = box_start_tag
126
+ self.box_end_tag = box_end_tag
127
+ self.quad_start_tag = quad_start_tag
128
+ self.quad_end_tag = quad_end_tag
129
+ self.IMAGE_ST = (
130
+ ref_start_tag, ref_end_tag,
131
+ box_start_tag, box_end_tag,
132
+ quad_start_tag, quad_end_tag,
133
+ image_start_tag, image_end_tag,
134
+ image_pad_tag
135
+ )
136
+ super().__init__(**kwargs)
137
+
138
+ self.errors = errors # how to handle errors in decoding
139
+
140
+ self.mergeable_ranks = _load_tiktoken_bpe(vocab_file) # type: dict[bytes, int]
141
+ self.special_tokens = {
142
+ token: index
143
+ for index, token in enumerate(
144
+ SPECIAL_TOKENS + self.IMAGE_ST, start=len(self.mergeable_ranks)
145
+ )
146
+ }
147
+ self.img_start_id = self.special_tokens[self.image_start_tag]
148
+ self.img_end_id = self.special_tokens[self.image_end_tag]
149
+ self.img_pad_id = self.special_tokens[self.image_pad_tag]
150
+ self.ref_start_id = self.special_tokens[self.ref_start_tag]
151
+ self.ref_end_id = self.special_tokens[self.ref_end_tag]
152
+ self.box_start_id = self.special_tokens[self.box_start_tag]
153
+ self.box_end_id = self.special_tokens[self.box_end_tag]
154
+ self.quad_start_id = self.special_tokens[self.quad_start_tag]
155
+ self.quad_end_id = self.special_tokens[self.quad_end_tag]
156
+ self.image_special_tokens = set([
157
+ self.ref_start_id, self.ref_end_id, self.box_start_id, self.box_end_id,
158
+ self.quad_start_id, self.quad_end_id,
159
+ ])
160
+
161
+ enc = tiktoken.Encoding(
162
+ "Qwen",
163
+ pat_str=PAT_STR,
164
+ mergeable_ranks=self.mergeable_ranks,
165
+ special_tokens=self.special_tokens,
166
+ )
167
+ assert (
168
+ len(self.mergeable_ranks) + len(self.special_tokens) == enc.n_vocab
169
+ ), f"{len(self.mergeable_ranks) + len(self.special_tokens)} != {enc.n_vocab} in encoding"
170
+
171
+ self.decoder = {
172
+ v: k for k, v in self.mergeable_ranks.items()
173
+ } # type: dict[int, bytes|str]
174
+ self.decoder.update({v: k for k, v in self.special_tokens.items()})
175
+
176
+ self.tokenizer = enc # type: tiktoken.Encoding
177
+
178
+ self.eod_id = self.tokenizer.eot_token
179
+ self.im_start_id = self.special_tokens[IMSTART]
180
+ self.im_end_id = self.special_tokens[IMEND]
181
+
182
+ def __getstate__(self):
183
+ # for pickle lovers
184
+ state = self.__dict__.copy()
185
+ del state['tokenizer']
186
+ return state
187
+
188
+ def __setstate__(self, state):
189
+ # tokenizer is not python native; don't pass it; rebuild it
190
+ self.__dict__.update(state)
191
+ enc = tiktoken.Encoding(
192
+ "Qwen",
193
+ pat_str=PAT_STR,
194
+ mergeable_ranks=self.mergeable_ranks,
195
+ special_tokens=self.special_tokens,
196
+ )
197
+ self.tokenizer = enc
198
+
199
+
200
+ def __len__(self) -> int:
201
+ return self.tokenizer.n_vocab
202
+
203
+ def get_vocab(self) -> Dict[bytes, int]:
204
+ return self.mergeable_ranks
205
+
206
+ def convert_tokens_to_ids(
207
+ self, tokens: Union[bytes, str, List[Union[bytes, str]]]
208
+ ) -> List[int]:
209
+ ids = []
210
+ if isinstance(tokens, (str, bytes)):
211
+ if tokens in self.special_tokens:
212
+ return self.special_tokens[tokens]
213
+ else:
214
+ return self.mergeable_ranks.get(tokens)
215
+ for token in tokens:
216
+ if token in self.special_tokens:
217
+ ids.append(self.special_tokens[token])
218
+ else:
219
+ ids.append(self.mergeable_ranks.get(token))
220
+ return ids
221
+
222
+ def _add_tokens(self, new_tokens: Union[List[str], List[AddedToken]], special_tokens: bool = False) -> int:
223
+ if not special_tokens and new_tokens:
224
+ raise ValueError('Adding regular tokens is not supported')
225
+ for token in new_tokens:
226
+ surface_form = token.content if isinstance(token, AddedToken) else token
227
+ if surface_form not in SPECIAL_TOKENS + self.IMAGE_ST:
228
+ raise ValueError('Adding unknown special tokens is not supported')
229
+ return 0
230
+
231
+ def save_vocabulary(self, save_directory: str, **kwargs) -> Tuple[str]:
232
+ """
233
+ Save only the vocabulary of the tokenizer (vocabulary).
234
+
235
+ Returns:
236
+ `Tuple(str)`: Paths to the files saved.
237
+ """
238
+ file_path = os.path.join(save_directory, "qwen.tiktoken")
239
+ with open(file_path, "w", encoding="utf8") as w:
240
+ for k, v in self.mergeable_ranks.items():
241
+ line = base64.b64encode(k).decode("utf8") + " " + str(v) + "\n"
242
+ w.write(line)
243
+ return (file_path,)
244
+
245
+ def tokenize(
246
+ self,
247
+ text: str,
248
+ allowed_special: Union[Set, str] = "all",
249
+ disallowed_special: Union[Collection, str] = (),
250
+ **kwargs,
251
+ ) -> List[Union[bytes, str]]:
252
+ """
253
+ Converts a string in a sequence of tokens.
254
+
255
+ Args:
256
+ text (`str`):
257
+ The sequence to be encoded.
258
+ allowed_special (`Literal["all"]` or `set`):
259
+ The surface forms of the tokens to be encoded as special tokens in regular texts.
260
+ Default to "all".
261
+ disallowed_special (`Literal["all"]` or `Collection`):
262
+ The surface forms of the tokens that should not be in regular texts and trigger errors.
263
+ Default to an empty tuple.
264
+
265
+ kwargs (additional keyword arguments, *optional*):
266
+ Will be passed to the underlying model specific encode method.
267
+
268
+ Returns:
269
+ `List[bytes|str]`: The list of tokens.
270
+ """
271
+ tokens = []
272
+ text = unicodedata.normalize("NFC", text)
273
+
274
+ # this implementation takes a detour: text -> token id -> token surface forms
275
+ for t in self.tokenizer.encode(
276
+ text, allowed_special=allowed_special, disallowed_special=disallowed_special
277
+ ):
278
+ tokens.append(self.decoder[t])
279
+
280
+ def _encode_imgurl(img_tokens):
281
+ assert img_tokens[0] == self.image_start_tag and img_tokens[-1] == self.image_end_tag
282
+ img_tokens = img_tokens[1:-1]
283
+ img_url = b''.join(img_tokens)
284
+ out_img_tokens = list(map(self.decoder.get, img_url))
285
+ if len(out_img_tokens) > IMG_TOKEN_SPAN:
286
+ raise ValueError("The content in {}..{} is too long".format(
287
+ self.image_start_tag, self.image_end_tag))
288
+ out_img_tokens.extend([self.image_pad_tag] * (IMG_TOKEN_SPAN - len(out_img_tokens)))
289
+ out_img_tokens = [self.image_start_tag] + out_img_tokens + [self.image_end_tag]
290
+ return out_img_tokens
291
+
292
+ '''
293
+ def _encode_text(text_tokens):
294
+ temp_end=0
295
+ temp_end = _list_find(text_tokens, (self.image_end_tag,self.image_end_tag), temp_end)
296
+ if temp_end != -1:
297
+ text_tokens[temp_end]=self.img_pad_id
298
+ while temp_end !=-1:
299
+ temp_end = _list_find(text_tokens, (self.image_end_tag,self.image_end_tag), temp_end)
300
+ text_tokens[temp_end]=self.img_pad_id
301
+ return text_tokens
302
+ '''
303
+
304
+
305
+ return _replace_closed_tag(tokens, self.image_start_tag, self.image_end_tag, _encode_imgurl)#_encode_text
306
+
307
+ def convert_tokens_to_string(self, tokens: List[Union[bytes, str]]) -> str:
308
+ """
309
+ Converts a sequence of tokens in a single string.
310
+ """
311
+ text = ""
312
+ temp = b""
313
+ for t in tokens:
314
+ if isinstance(t, str):
315
+ if temp:
316
+ text += temp.decode("utf-8", errors=self.errors)
317
+ temp = b""
318
+ text += t
319
+ elif isinstance(t, bytes):
320
+ temp += t
321
+ else:
322
+ raise TypeError("token should only be of type types or str")
323
+ if temp:
324
+ text += temp.decode("utf-8", errors=self.errors)
325
+ return text
326
+
327
+ @property
328
+ def vocab_size(self):
329
+ return self.tokenizer.n_vocab
330
+
331
+ def _convert_id_to_token(self, index: int) -> Union[bytes, str]:
332
+ """Converts an id to a token, special tokens included"""
333
+ if index in self.decoder:
334
+ return self.decoder[index]
335
+ raise ValueError("unknown ids")
336
+
337
+ def _convert_token_to_id(self, token: Union[bytes, str]) -> int:
338
+ """Converts a token to an id using the vocab, special tokens included"""
339
+ if token in self.special_tokens:
340
+ return self.special_tokens[token]
341
+ if token in self.mergeable_ranks:
342
+ return self.mergeable_ranks[token]
343
+ raise ValueError("unknown token")
344
+
345
+ def _tokenize(self, text: str, **kwargs):
346
+ """
347
+ Converts a string in a sequence of tokens (string), using the tokenizer. Split in words for word-based
348
+ vocabulary or sub-words for sub-word-based vocabularies (BPE/SentencePieces/WordPieces).
349
+
350
+ Do NOT take care of added tokens.
351
+ """
352
+ raise NotImplementedError
353
+
354
+ def _decode(
355
+ self,
356
+ token_ids: Union[int, List[int]],
357
+ skip_special_tokens: bool = False,
358
+ errors: str = None,
359
+ **kwargs,
360
+ ) -> str:
361
+ if isinstance(token_ids, int):
362
+ token_ids = [token_ids]
363
+
364
+ def _decode_imgurl(img_token_ids):
365
+ assert img_token_ids[0] == self.img_start_id and img_token_ids[-1] == self.img_end_id
366
+ img_token_ids = img_token_ids[1:-1]
367
+ img_token_ids = img_token_ids[ : img_token_ids.index(self.img_pad_id)]
368
+ img_url = bytes(img_token_ids).decode('utf-8')
369
+ return [self.img_start_id] + self.tokenizer.encode(img_url) + [self.img_end_id]
370
+
371
+ token_ids = _replace_closed_tag(token_ids, self.img_start_id, self.img_end_id, _decode_imgurl)
372
+
373
+ if skip_special_tokens:
374
+ if kwargs.get('keep_image_special', False):
375
+ token_ids = [i for i in token_ids if i < self.eod_id
376
+ or i in self.image_special_tokens]
377
+ else:
378
+ token_ids = [i for i in token_ids if i < self.eod_id]
379
+ return self.tokenizer.decode(token_ids, errors=errors or self.errors)
380
+
381
+ def to_list_format(self, text: str):
382
+ text = unicodedata.normalize("NFC", text)
383
+ token_ids = self.tokenizer.encode(
384
+ text, allowed_special=set(self.IMAGE_ST + (ENDOFTEXT,)))
385
+
386
+ def _encode_vl_info(tokens):
387
+ if len(tokens) == 0:
388
+ return []
389
+ if tokens[0] == self.img_start_id and tokens[-1] == self.img_end_id:
390
+ key = 'image'
391
+ elif tokens[0] == self.ref_start_id and tokens[-1] == self.ref_end_id:
392
+ key = 'ref'
393
+ elif tokens[0] == self.box_start_id and tokens[-1] == self.box_end_id:
394
+ key = 'box'
395
+ elif tokens[0] == self.quad_start_id and tokens[-1] == self.quad_end_id:
396
+ key = 'quad'
397
+ else:
398
+ _tobytes = lambda x: x.encode('utf-8') if isinstance(x, str) else x
399
+ return [{'text': b''.join(map(_tobytes, map(self.decoder.get, tokens))).decode('utf-8')}]
400
+ _tobytes = lambda x: x.encode('utf-8') if isinstance(x, str) else x
401
+ val = b''.join(map(_tobytes, map(self.decoder.get, tokens[1:-1]))).decode('utf-8')
402
+ return [{key: val}]
403
+
404
+ return _replace_closed_tag(
405
+ token_ids,
406
+ (self.img_start_id, self.ref_start_id, self.box_start_id, self.quad_start_id),
407
+ (self.img_end_id, self.ref_end_id, self.box_end_id, self.quad_end_id),
408
+ _encode_vl_info,
409
+ _encode_vl_info,
410
+ )
411
+
412
+ def from_list_format(self, list_format: List[Dict]):
413
+ text = ''
414
+ num_images = 0
415
+ for ele in list_format:
416
+ if 'image' in ele:
417
+ num_images += 1
418
+ text += f'Picture {num_images}: '
419
+ text += self.image_start_tag + ele['image'] + self.image_end_tag
420
+ text += '\n'
421
+ elif 'text' in ele:
422
+ text += ele['text']
423
+ elif 'box' in ele:
424
+ if 'ref' in ele:
425
+ text += self.ref_start_tag + ele['ref'] + self.ref_end_tag
426
+ for box in ele['box']:
427
+ text += self.box_start_tag + '(%d,%d),(%d,%d)' % (box[0], box[1], box[2], box[3]) + self.box_end_tag
428
+ else:
429
+ raise ValueError("Unsupport element: " + str(ele))
430
+ return text
431
+
432
+ def _fetch_latest_picture(self, response, history):
433
+ if history is None:
434
+ history = []
435
+ _history = history + [(response, None)]
436
+ for q, r in _history[::-1]:
437
+ for ele in self.to_list_format(q)[::-1]:
438
+ if 'image' in ele:
439
+ return ele['image']
440
+ return None
441
+
442
+ def _fetch_all_box_with_ref(self, text):
443
+ list_format = self.to_list_format(text)
444
+ output = []
445
+ for i, ele in enumerate(list_format):
446
+ if 'box' in ele:
447
+ bbox = tuple(map(int, ele['box'].replace('(', '').replace(')', '').split(',')))
448
+ assert len(bbox) == 4
449
+ output.append({'box': bbox})
450
+ if i > 0 and 'ref' in list_format[i-1]:
451
+ output[-1]['ref'] = list_format[i-1]['ref'].strip()
452
+ return output
453
+
454
+ def draw_bbox_on_latest_picture(
455
+ self,
456
+ response,
457
+ history=None,
458
+ ) -> Optional[Image.Image]:
459
+ image = self._fetch_latest_picture(response, history)
460
+ if image is None:
461
+ return None
462
+ if image.startswith("http://") or image.startswith("https://"):
463
+ image = Image.open(requests.get(image, stream=True).raw).convert("RGB")
464
+ h, w = image.height, image.width
465
+ else:
466
+ image = np.asarray(Image.open(image).convert("RGB"))
467
+ h, w = image.shape[0], image.shape[1]
468
+ visualizer = Visualizer(image)
469
+
470
+ boxes = self._fetch_all_box_with_ref(response)
471
+ if not boxes:
472
+ return None
473
+ color = random.choice([_ for _ in mcolors.TABLEAU_COLORS.keys()]) # init color
474
+ for box in boxes:
475
+ if 'ref' in box: # random new color for new refexps
476
+ color = random.choice([_ for _ in mcolors.TABLEAU_COLORS.keys()])
477
+ x1, y1, x2, y2 = box['box']
478
+ x1, y1, x2, y2 = (int(x1 / 1000 * w), int(y1 / 1000 * h), int(x2 / 1000 * w), int(y2 / 1000 * h))
479
+ visualizer.draw_box((x1, y1, x2, y2), alpha=1, edge_color=color)
480
+ if 'ref' in box:
481
+ visualizer.draw_text(box['ref'], (x1, y1), color=color, horizontal_alignment="left")
482
+ return visualizer.output
483
+
484
+
485
+ import colorsys
486
+ import logging
487
+ import math
488
+ import numpy as np
489
+ import matplotlib as mpl
490
+ import matplotlib.colors as mplc
491
+ import matplotlib.figure as mplfigure
492
+ import torch
493
+ from matplotlib.backends.backend_agg import FigureCanvasAgg
494
+ from PIL import Image
495
+ import random
496
+
497
+ logger = logging.getLogger(__name__)
498
+
499
+
500
+ class VisImage:
501
+ def __init__(self, img, scale=1.0):
502
+ self.img = img
503
+ self.scale = scale
504
+ self.width, self.height = img.shape[1], img.shape[0]
505
+ self._setup_figure(img)
506
+
507
+ def _setup_figure(self, img):
508
+ fig = mplfigure.Figure(frameon=False)
509
+ self.dpi = fig.get_dpi()
510
+ # add a small 1e-2 to avoid precision lost due to matplotlib's truncation
511
+ # (https://github.com/matplotlib/matplotlib/issues/15363)
512
+ fig.set_size_inches(
513
+ (self.width * self.scale + 1e-2) / self.dpi,
514
+ (self.height * self.scale + 1e-2) / self.dpi,
515
+ )
516
+ self.canvas = FigureCanvasAgg(fig)
517
+ # self.canvas = mpl.backends.backend_cairo.FigureCanvasCairo(fig)
518
+ ax = fig.add_axes([0.0, 0.0, 1.0, 1.0])
519
+ ax.axis("off")
520
+ self.fig = fig
521
+ self.ax = ax
522
+ self.reset_image(img)
523
+
524
+ def reset_image(self, img):
525
+ img = img.astype("uint8")
526
+ self.ax.imshow(img, extent=(0, self.width, self.height, 0), interpolation="nearest")
527
+
528
+ def save(self, filepath):
529
+ self.fig.savefig(filepath)
530
+
531
+ def get_image(self):
532
+ canvas = self.canvas
533
+ s, (width, height) = canvas.print_to_buffer()
534
+
535
+ buffer = np.frombuffer(s, dtype="uint8")
536
+
537
+ img_rgba = buffer.reshape(height, width, 4)
538
+ rgb, alpha = np.split(img_rgba, [3], axis=2)
539
+ return rgb.astype("uint8")
540
+
541
+
542
+ class Visualizer:
543
+ def __init__(self, img_rgb, metadata=None, scale=1.0):
544
+ self.img = np.asarray(img_rgb).clip(0, 255).astype(np.uint8)
545
+ self.font_path = FONT_PATH
546
+ self.output = VisImage(self.img, scale=scale)
547
+ self.cpu_device = torch.device("cpu")
548
+
549
+ # too small texts are useless, therefore clamp to 14
550
+ self._default_font_size = max(
551
+ np.sqrt(self.output.height * self.output.width) // 30, 15 // scale
552
+ )
553
+
554
+ def draw_text(
555
+ self,
556
+ text,
557
+ position,
558
+ *,
559
+ font_size=None,
560
+ color="g",
561
+ horizontal_alignment="center",
562
+ rotation=0,
563
+ ):
564
+ if not font_size:
565
+ font_size = self._default_font_size
566
+
567
+ # since the text background is dark, we don't want the text to be dark
568
+ color = np.maximum(list(mplc.to_rgb(color)), 0.2)
569
+ color[np.argmax(color)] = max(0.8, np.max(color))
570
+
571
+ x, y = position
572
+ self.output.ax.text(
573
+ x,
574
+ y,
575
+ text,
576
+ size=font_size * self.output.scale,
577
+ fontproperties=FontProperties(fname=self.font_path),
578
+ bbox={"facecolor": "black", "alpha": 0.8, "pad": 0.7, "edgecolor": "none"},
579
+ verticalalignment="top",
580
+ horizontalalignment=horizontal_alignment,
581
+ color=color,
582
+ zorder=10,
583
+ rotation=rotation,
584
+ )
585
+ return self.output
586
+
587
+ def draw_box(self, box_coord, alpha=0.5, edge_color="g", line_style="-"):
588
+
589
+ x0, y0, x1, y1 = box_coord
590
+ width = x1 - x0
591
+ height = y1 - y0
592
+
593
+ linewidth = max(self._default_font_size / 4, 1)
594
+
595
+ self.output.ax.add_patch(
596
+ mpl.patches.Rectangle(
597
+ (x0, y0),
598
+ width,
599
+ height,
600
+ fill=False,
601
+ edgecolor=edge_color,
602
+ linewidth=linewidth * self.output.scale,
603
+ alpha=alpha,
604
+ linestyle=line_style,
605
+ )
606
+ )
607
+ return self.output
608
+
609
+ def get_output(self):
610
+
611
+ return self.output
tokenizer_config.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {},
3
+ "auto_map": {
4
+ "AutoTokenizer": [
5
+ "tokenization_qwen.QWenTokenizer",
6
+ null
7
+ ]
8
+ },
9
+ "clean_up_tokenization_spaces": true,
10
+ "model_max_length": 4096,
11
+ "pad_token": "<|endoftext|>",
12
+ "padding_side": "right",
13
+ "tokenizer_class": "QWenTokenizer"
14
+ }
trainer_state.json ADDED
The diff for this file is too large to render. See raw diff
 
training_args.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e09351479c54bd227be6fdeeccd53610f152da5bb37d4e2a24a712a4a91d864d
3
+ size 6651
visual.py ADDED
@@ -0,0 +1,448 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 collections import OrderedDict
7
+ import math
8
+ import requests
9
+ from io import BytesIO
10
+ from functools import partial
11
+ from PIL import Image
12
+ from typing import Callable, Optional, Sequence, Tuple, List
13
+ import numpy as np
14
+
15
+ import torch
16
+ from torch import nn
17
+ from torch.nn import functional as F
18
+ #from torch.nn.init import trunc_normal_
19
+ from torch.nn.init import normal_
20
+ from torchvision import transforms
21
+ from torchvision.transforms import InterpolationMode
22
+ import os
23
+
24
+
25
+ def get_abs_pos(abs_pos, tgt_size):
26
+ # abs_pos: L, C
27
+ # tgt_size: M
28
+ # return: M, C
29
+ src_size = int(math.sqrt(abs_pos.size(0)))
30
+ tgt_size = int(math.sqrt(tgt_size))
31
+ dtype = abs_pos.dtype
32
+
33
+ if src_size != tgt_size:
34
+ return F.interpolate(
35
+ abs_pos.float().reshape(1, src_size, src_size, -1).permute(0, 3, 1, 2),
36
+ size=(tgt_size, tgt_size),
37
+ mode="bicubic",
38
+ align_corners=False,
39
+ ).permute(0, 2, 3, 1).flatten(0, 2).to(dtype=dtype)
40
+ else:
41
+ return abs_pos
42
+
43
+ # https://github.com/facebookresearch/mae/blob/efb2a8062c206524e35e47d04501ed4f544c0ae8/util/pos_embed.py#L20
44
+ def get_2d_sincos_pos_embed(embed_dim, grid_size, cls_token=False):
45
+ """
46
+ grid_size: int of the grid height and width
47
+ return:
48
+ pos_embed: [grid_size*grid_size, embed_dim] or [1+grid_size*grid_size, embed_dim] (w/ or w/o cls_token)
49
+ """
50
+ grid_h = np.arange(grid_size, dtype=np.float32)
51
+ grid_w = np.arange(grid_size, dtype=np.float32)
52
+ grid = np.meshgrid(grid_w, grid_h) # here w goes first
53
+ grid = np.stack(grid, axis=0)
54
+
55
+ grid = grid.reshape([2, 1, grid_size, grid_size])
56
+ pos_embed = get_2d_sincos_pos_embed_from_grid(embed_dim, grid)
57
+ if cls_token:
58
+ pos_embed = np.concatenate([np.zeros([1, embed_dim]), pos_embed], axis=0)
59
+ return pos_embed
60
+
61
+
62
+ def get_2d_sincos_pos_embed_from_grid(embed_dim, grid):
63
+ assert embed_dim % 2 == 0
64
+
65
+ # use half of dimensions to encode grid_h
66
+ emb_h = get_1d_sincos_pos_embed_from_grid(embed_dim // 2, grid[0]) # (H*W, D/2)
67
+ emb_w = get_1d_sincos_pos_embed_from_grid(embed_dim // 2, grid[1]) # (H*W, D/2)
68
+
69
+ emb = np.concatenate([emb_h, emb_w], axis=1) # (H*W, D)
70
+ return emb
71
+
72
+
73
+ def get_1d_sincos_pos_embed_from_grid(embed_dim, pos):
74
+ """
75
+ embed_dim: output dimension for each position
76
+ pos: a list of positions to be encoded: size (M,)
77
+ out: (M, D)
78
+ """
79
+ assert embed_dim % 2 == 0
80
+ omega = np.arange(embed_dim // 2, dtype=np.float32)
81
+ omega /= embed_dim / 2.
82
+ omega = 1. / 10000**omega # (D/2,)
83
+
84
+ pos = pos.reshape(-1) # (M,)
85
+ out = np.einsum('m,d->md', pos, omega) # (M, D/2), outer product
86
+
87
+ emb_sin = np.sin(out) # (M, D/2)
88
+ emb_cos = np.cos(out) # (M, D/2)
89
+
90
+ emb = np.concatenate([emb_sin, emb_cos], axis=1) # (M, D)
91
+ return emb
92
+
93
+
94
+ class Resampler(nn.Module):
95
+ """
96
+ A 2D perceiver-resampler network with one cross attention layers by
97
+ (grid_size**2) learnable queries and 2d sincos pos_emb
98
+ Outputs:
99
+ A tensor with the shape of (grid_size**2, embed_dim)
100
+ """
101
+ def __init__(
102
+ self,
103
+ grid_size,
104
+ embed_dim,
105
+ num_heads,
106
+ kv_dim=None,
107
+ norm_layer=nn.LayerNorm
108
+ ):
109
+ super().__init__()
110
+ self.num_queries = grid_size ** 2
111
+ self.embed_dim = embed_dim
112
+ self.num_heads = num_heads
113
+
114
+ self.pos_embed = nn.Parameter(
115
+ torch.from_numpy(get_2d_sincos_pos_embed(embed_dim, grid_size)).float()
116
+ ).requires_grad_(False)
117
+
118
+ self.query = nn.Parameter(torch.zeros(self.num_queries, embed_dim))
119
+ #trunc_normal_(self.query, std=.02)
120
+ normal_(self.query, std=.02)
121
+
122
+ if kv_dim is not None and kv_dim != embed_dim:
123
+ self.kv_proj = nn.Linear(kv_dim, embed_dim, bias=False)
124
+ else:
125
+ self.kv_proj = nn.Identity()
126
+
127
+ self.attn = nn.MultiheadAttention(embed_dim, num_heads)
128
+ self.ln_q = norm_layer(embed_dim)
129
+ self.ln_kv = norm_layer(embed_dim)
130
+
131
+ # self.apply(self._init_weights)
132
+
133
+ def _init_weights(self, m):
134
+ if isinstance(m, nn.Linear):
135
+ #trunc_normal_(m.weight, std=.02)
136
+ normal_(m.weight, std=.02)
137
+ if isinstance(m, nn.Linear) and m.bias is not None:
138
+ nn.init.constant_(m.bias, 0)
139
+ elif isinstance(m, nn.LayerNorm):
140
+ nn.init.constant_(m.bias, 0)
141
+ nn.init.constant_(m.weight, 1.0)
142
+
143
+ def forward(self, x, attn_mask=None):
144
+
145
+ pos_embed = get_abs_pos(self.pos_embed, x.size(1))
146
+
147
+ x = self.kv_proj(x)
148
+ x = self.ln_kv(x).permute(1, 0, 2)
149
+
150
+ N = x.shape[1]
151
+ q = self.ln_q(self.query)
152
+ out = self.attn(
153
+ self._repeat(q, N) + self.pos_embed.unsqueeze(1),
154
+ x + pos_embed.unsqueeze(1),
155
+ x,
156
+ attn_mask=attn_mask)[0]
157
+ return out.permute(1, 0, 2)
158
+
159
+ def _repeat(self, query, N: int):
160
+ return query.unsqueeze(1).repeat(1, N, 1)
161
+
162
+
163
+ class VisualAttention(nn.Module):
164
+ """self-attention layer class.
165
+
166
+ Self-attention layer takes input with size [s, b, h]
167
+ and returns output of the same size.
168
+ """
169
+
170
+ def __init__(self, embed_dim, num_heads,
171
+ bias=True, kdim=None, vdim=None):
172
+ super(VisualAttention, self).__init__()
173
+ self.embed_dim = embed_dim
174
+ self.kdim = kdim if kdim is not None else embed_dim
175
+ self.vdim = vdim if vdim is not None else embed_dim
176
+ self._qkv_same_embed_dim = self.kdim == embed_dim and self.vdim == embed_dim
177
+
178
+ self.num_heads = num_heads
179
+
180
+ # Per attention head and per partition values.
181
+ assert embed_dim % num_heads == 0
182
+ self.hidden_size_per_attention_head = embed_dim // num_heads
183
+ self.num_attention_heads_per_partition = num_heads
184
+ self.hidden_size_per_partition = embed_dim
185
+
186
+ # Strided linear layer.
187
+ assert self._qkv_same_embed_dim, 'Only Support SelfAttention Currently'
188
+ self.in_proj = nn.Linear(embed_dim, 3 * embed_dim)
189
+ self.out_proj = nn.Linear(embed_dim, embed_dim)
190
+ self.norm_factor = math.sqrt(self.hidden_size_per_attention_head)
191
+
192
+ def forward(self, query, key, value, attn_mask = None):
193
+ # query/key/value: [sq, b, h]
194
+ sq, b, _ = query.size()
195
+
196
+ assert torch.allclose(query, key), 'Only Support Self-Attention Currently'
197
+ sk = sq
198
+ mixed_x_layer = self.in_proj(query)
199
+
200
+ # [sq, b, (np * 3 * hn)] --> [sq, b, np, 3 * hn]
201
+ new_tensor_shape = mixed_x_layer.size()[:-1] + \
202
+ (self.num_attention_heads_per_partition,
203
+ 3 * self.hidden_size_per_attention_head)
204
+ mixed_x_layer = mixed_x_layer.view(*new_tensor_shape)
205
+
206
+ # [sq, b, np, 3 * hn] --> 3 [sq, b, np, hn]
207
+ query_layer, key_layer, value_layer = mixed_x_layer.split(
208
+ self.hidden_size_per_attention_head, dim=-1)
209
+
210
+ # [sq, b, np, hn] -> [sq, b * np, hn]
211
+ query_layer = query_layer.view(sq,
212
+ b * self.num_attention_heads_per_partition,
213
+ self.hidden_size_per_attention_head).transpose(0, 1)
214
+ # [sk, b, np, hn] -> [sk, b * np, hn]
215
+ key_layer = key_layer.view(sk,
216
+ b * self.num_attention_heads_per_partition,
217
+ self.hidden_size_per_attention_head).transpose(0, 1)
218
+
219
+ q_scaled = query_layer / self.norm_factor
220
+ if attn_mask is not None:
221
+ attention_probs = torch.baddbmm(attn_mask, q_scaled, key_layer.transpose(-2, -1))
222
+ else:
223
+ attention_probs = torch.bmm(q_scaled, key_layer.transpose(-2, -1))
224
+ attention_probs = attention_probs.softmax(dim=-1)
225
+
226
+ value_layer = value_layer.view(sk,
227
+ b * self.num_attention_heads_per_partition,
228
+ self.hidden_size_per_attention_head).transpose(0, 1)
229
+
230
+ # matmul: [b * np, sq, hn]
231
+ context_layer = torch.bmm(attention_probs, value_layer)
232
+
233
+ # change view [b, np, sq, hn]
234
+ context_layer = context_layer.view(b,
235
+ self.num_attention_heads_per_partition,
236
+ sq, self.hidden_size_per_attention_head)
237
+
238
+ # [b, np, sq, hn] --> [sq, b, np, hn]
239
+ context_layer = context_layer.permute(2, 0, 1, 3).contiguous()
240
+
241
+ # [sq, b, np, hn] --> [sq, b, hp]
242
+ new_context_layer_shape = context_layer.size()[:-2] + \
243
+ (self.hidden_size_per_partition,)
244
+ context_layer = context_layer.view(*new_context_layer_shape)
245
+
246
+ output = self.out_proj(context_layer)
247
+
248
+ return output
249
+
250
+
251
+ class VisualAttentionBlock(nn.Module):
252
+ def __init__(
253
+ self,
254
+ d_model: int,
255
+ n_head: int,
256
+ mlp_ratio: float = 4.0,
257
+ act_layer: Callable = nn.GELU,
258
+ norm_layer: Callable = nn.LayerNorm,
259
+ is_cross_attention: bool = False,
260
+ ):
261
+ super().__init__()
262
+
263
+ self.ln_1 = norm_layer(d_model)
264
+ if is_cross_attention:
265
+ self.ln_1_kv = norm_layer(d_model)
266
+
267
+ self.ln_2 = norm_layer(d_model)
268
+ mlp_width = int(d_model * mlp_ratio)
269
+ self.attn = VisualAttention(d_model, n_head)
270
+ self.mlp = nn.Sequential(OrderedDict([
271
+ ("c_fc", nn.Linear(d_model, mlp_width)),
272
+ ("gelu", act_layer()),
273
+ ("c_proj", nn.Linear(mlp_width, d_model))
274
+ ]))
275
+
276
+ def attention(
277
+ self,
278
+ q_x: torch.Tensor,
279
+ k_x: Optional[torch.Tensor] = None,
280
+ v_x: Optional[torch.Tensor] = None,
281
+ attn_mask: Optional[torch.Tensor] = None,
282
+ ):
283
+ k_x = k_x if k_x is not None else q_x
284
+ v_x = v_x if v_x is not None else q_x
285
+
286
+ attn_mask = attn_mask.to(q_x.dtype) if attn_mask is not None else None
287
+ return self.attn(q_x, k_x, v_x, attn_mask=attn_mask)
288
+
289
+ def forward(
290
+ self,
291
+ q_x: torch.Tensor,
292
+ k_x: Optional[torch.Tensor] = None,
293
+ v_x: Optional[torch.Tensor] = None,
294
+ attn_mask: Optional[torch.Tensor] = None,
295
+ ):
296
+ k_x = self.ln_1_kv(k_x) if hasattr(self, "ln_1_kv") and k_x is not None else None
297
+ v_x = self.ln_1_kv(v_x) if hasattr(self, "ln_1_kv") and v_x is not None else None
298
+
299
+ x = q_x + self.attention(q_x=self.ln_1(q_x), k_x=k_x, v_x=v_x, attn_mask=attn_mask)
300
+ x = x + self.mlp(self.ln_2(x))
301
+ return x
302
+
303
+
304
+ class TransformerBlock(nn.Module):
305
+ def __init__(
306
+ self,
307
+ width: int,
308
+ layers: int,
309
+ heads: int,
310
+ mlp_ratio: float = 4.0,
311
+ act_layer: Callable = nn.GELU,
312
+ norm_layer: Callable = nn.LayerNorm,
313
+ ):
314
+ super().__init__()
315
+ self.width = width
316
+ self.layers = layers
317
+
318
+ self.resblocks = nn.ModuleList([
319
+ VisualAttentionBlock(
320
+ width, heads, mlp_ratio, act_layer=act_layer, norm_layer=norm_layer)
321
+ for _ in range(layers)
322
+ ])
323
+
324
+ def get_cast_dtype(self) -> torch.dtype:
325
+ return self.resblocks[0].mlp.c_fc.weight.dtype
326
+
327
+ def get_cast_device(self) -> torch.device:
328
+ return self.resblocks[0].mlp.c_fc.weight.device
329
+
330
+ def forward(self, x: torch.Tensor, attn_mask: Optional[torch.Tensor] = None):
331
+ for r in self.resblocks:
332
+ x = r(x, attn_mask=attn_mask)
333
+ return x
334
+
335
+
336
+ class VisionTransformer(nn.Module):
337
+
338
+ def __init__(
339
+ self,
340
+ image_size: int,
341
+ patch_size: int,
342
+ width: int,
343
+ layers: int,
344
+ heads: int,
345
+ mlp_ratio: float,
346
+ n_queries: int = 256,
347
+ output_dim: int = 512,
348
+ **kwargs
349
+ ):
350
+ super().__init__()
351
+ image_height, image_width = self.image_size = (image_size, image_size)
352
+ patch_height, patch_width = self.patch_size = (patch_size, patch_size)
353
+ self.grid_size = (image_height // patch_height, image_width // patch_width)
354
+ self.output_dim = output_dim
355
+
356
+ mean = (0.48145466, 0.4578275, 0.40821073)
357
+ std = (0.26862954, 0.26130258, 0.27577711)
358
+ self.image_transform = transforms.Compose([
359
+ transforms.Resize(
360
+ (image_size, image_size),
361
+ interpolation=InterpolationMode.BICUBIC
362
+ ),
363
+ transforms.ToTensor(),
364
+ transforms.Normalize(mean=mean, std=std),
365
+ ])
366
+
367
+ self.conv1 = nn.Conv2d(in_channels=3, out_channels=width, kernel_size=patch_size, stride=patch_size, bias=False)
368
+
369
+ # class embeddings and positional embeddings
370
+ scale = width ** -0.5
371
+ self.positional_embedding = nn.Parameter(scale * torch.randn(256, width))
372
+
373
+ norm_layer = partial(nn.LayerNorm, eps=1e-6)
374
+ act_layer = nn.GELU
375
+
376
+ self.ln_pre = norm_layer(width)
377
+ self.transformer = TransformerBlock(
378
+ width,
379
+ layers,
380
+ heads,
381
+ mlp_ratio,
382
+ act_layer=act_layer,
383
+ norm_layer=norm_layer,
384
+ )
385
+
386
+ self.attn_pool = Resampler(
387
+ grid_size=int(math.sqrt(n_queries)),
388
+ embed_dim=output_dim,
389
+ num_heads=output_dim // 128,
390
+ kv_dim=width,
391
+ norm_layer=norm_layer,
392
+ )
393
+ self.ln_post = norm_layer(output_dim)
394
+ self.proj = nn.Parameter((output_dim** -0.5) * torch.randn(output_dim, output_dim))
395
+
396
+ def forward(self, x: torch.Tensor):
397
+ x = x.to(
398
+ dtype=self.transformer.get_cast_dtype(),
399
+ device=self.transformer.get_cast_device(),
400
+ )
401
+ # to patches
402
+ x = self.conv1(x) # shape = [*, width, grid, grid]
403
+ x = x.reshape(x.shape[0], x.shape[1], -1) # shape = [*, width, grid ** 2]
404
+ x = x.permute(0, 2, 1) # shape = [*, grid ** 2, width]
405
+
406
+ x = x + get_abs_pos(self.positional_embedding, x.size(1))
407
+
408
+ x = self.ln_pre(x)
409
+
410
+ x = x.permute(1, 0, 2) # NLD -> LND
411
+ x = self.transformer(x)
412
+ x = x.permute(1, 0, 2) # LND -> NLD
413
+
414
+ x = self.attn_pool(x)
415
+ x = self.ln_post(x)
416
+ x = x @ self.proj
417
+
418
+ return x
419
+
420
+ def encode(self, image_paths: List[str]):
421
+ images = []
422
+ for image_path in image_paths:
423
+ if image_path.startswith("http://") or image_path.startswith("https://"):
424
+ image = Image.open(requests.get(image_path, stream=True).raw)
425
+ image = image.convert("RGB")
426
+ else:
427
+ try:
428
+ image = Image.open(image_path)
429
+ image = image.convert("RGB")
430
+ except:
431
+ try:
432
+ image_list=image_path.split("/")
433
+ image_name=image_list[-1].split("-screen.png")[0]
434
+ temp_list=image_name.split("_")
435
+ new_image="_".join(temp_list[:-1])+"-screen.png"
436
+ new_image_path=image_path.replace(image_list[-1],new_image)
437
+ image = Image.open(new_image_path)
438
+ image = image.convert("RGB")
439
+ except:
440
+ image_list=image_path.split("/")
441
+ image_name=image_list[-1].split("-screen.png")[0]
442
+ temp_name=image_name.split("_")[0]
443
+ new_image_path=image_path.replace(image_name,temp_name)
444
+ image = Image.open(new_image_path)
445
+ image = image.convert("RGB")
446
+ images.append(self.image_transform(image))
447
+ images = torch.stack(images, dim=0)
448
+ return self(images)
zero_to_fp32.py ADDED
@@ -0,0 +1,604 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ # Copyright (c) Microsoft Corporation.
4
+ # SPDX-License-Identifier: Apache-2.0
5
+
6
+ # DeepSpeed Team
7
+
8
+ # This script extracts fp32 consolidated weights from a zero 1, 2 and 3 DeepSpeed checkpoints. It gets
9
+ # copied into the top level checkpoint dir, so the user can easily do the conversion at any point in
10
+ # the future. Once extracted, the weights don't require DeepSpeed and can be used in any
11
+ # application.
12
+ #
13
+ # example: python zero_to_fp32.py . pytorch_model.bin
14
+
15
+ import argparse
16
+ import torch
17
+ import glob
18
+ import math
19
+ import os
20
+ import re
21
+ from collections import OrderedDict
22
+ from dataclasses import dataclass
23
+
24
+ # while this script doesn't use deepspeed to recover data, since the checkpoints are pickled with
25
+ # DeepSpeed data structures it has to be available in the current python environment.
26
+ from deepspeed.utils import logger
27
+ from deepspeed.checkpoint.constants import (DS_VERSION, OPTIMIZER_STATE_DICT, SINGLE_PARTITION_OF_FP32_GROUPS,
28
+ FP32_FLAT_GROUPS, ZERO_STAGE, PARTITION_COUNT, PARAM_SHAPES, BUFFER_NAMES,
29
+ FROZEN_PARAM_SHAPES, FROZEN_PARAM_FRAGMENTS)
30
+
31
+
32
+ @dataclass
33
+ class zero_model_state:
34
+ buffers: dict()
35
+ param_shapes: dict()
36
+ shared_params: list
37
+ ds_version: int
38
+ frozen_param_shapes: dict()
39
+ frozen_param_fragments: dict()
40
+
41
+
42
+ debug = 0
43
+
44
+ # load to cpu
45
+ device = torch.device('cpu')
46
+
47
+
48
+ def atoi(text):
49
+ return int(text) if text.isdigit() else text
50
+
51
+
52
+ def natural_keys(text):
53
+ '''
54
+ alist.sort(key=natural_keys) sorts in human order
55
+ http://nedbatchelder.com/blog/200712/human_sorting.html
56
+ (See Toothy's implementation in the comments)
57
+ '''
58
+ return [atoi(c) for c in re.split(r'(\d+)', text)]
59
+
60
+
61
+ def get_model_state_file(checkpoint_dir, zero_stage):
62
+ if not os.path.isdir(checkpoint_dir):
63
+ raise FileNotFoundError(f"Directory '{checkpoint_dir}' doesn't exist")
64
+
65
+ # there should be only one file
66
+ if zero_stage <= 2:
67
+ file = os.path.join(checkpoint_dir, "mp_rank_00_model_states.pt")
68
+ elif zero_stage == 3:
69
+ file = os.path.join(checkpoint_dir, "zero_pp_rank_0_mp_rank_00_model_states.pt")
70
+
71
+ if not os.path.exists(file):
72
+ raise FileNotFoundError(f"can't find model states file at '{file}'")
73
+
74
+ return file
75
+
76
+
77
+ def get_checkpoint_files(checkpoint_dir, glob_pattern):
78
+ # XXX: need to test that this simple glob rule works for multi-node setup too
79
+ ckpt_files = sorted(glob.glob(os.path.join(checkpoint_dir, glob_pattern)), key=natural_keys)
80
+
81
+ if len(ckpt_files) == 0:
82
+ raise FileNotFoundError(f"can't find {glob_pattern} files in directory '{checkpoint_dir}'")
83
+
84
+ return ckpt_files
85
+
86
+
87
+ def get_optim_files(checkpoint_dir):
88
+ return get_checkpoint_files(checkpoint_dir, "*_optim_states.pt")
89
+
90
+
91
+ def get_model_state_files(checkpoint_dir):
92
+ return get_checkpoint_files(checkpoint_dir, "*_model_states.pt")
93
+
94
+
95
+ def parse_model_states(files):
96
+ zero_model_states = []
97
+ for file in files:
98
+ state_dict = torch.load(file, map_location=device)
99
+
100
+ if BUFFER_NAMES not in state_dict:
101
+ raise ValueError(f"{file} is not a model state checkpoint")
102
+ buffer_names = state_dict[BUFFER_NAMES]
103
+ if debug:
104
+ print("Found buffers:", buffer_names)
105
+
106
+ # recover just the buffers while restoring them to fp32 if they were saved in fp16
107
+ buffers = {k: v.float() for k, v in state_dict["module"].items() if k in buffer_names}
108
+ param_shapes = state_dict[PARAM_SHAPES]
109
+
110
+ # collect parameters that are included in param_shapes
111
+ param_names = []
112
+ for s in param_shapes:
113
+ for name in s.keys():
114
+ param_names.append(name)
115
+
116
+ # update with frozen parameters
117
+ frozen_param_shapes = state_dict.get(FROZEN_PARAM_SHAPES, None)
118
+ if frozen_param_shapes is not None:
119
+ if debug:
120
+ print(f"Found frozen_param_shapes: {frozen_param_shapes}")
121
+ param_names += list(frozen_param_shapes.keys())
122
+
123
+ # handle shared params
124
+ shared_params = [[k, v] for k, v in state_dict["shared_params"].items()]
125
+
126
+ ds_version = state_dict.get(DS_VERSION, None)
127
+
128
+ frozen_param_fragments = state_dict.get(FROZEN_PARAM_FRAGMENTS, None)
129
+
130
+ z_model_state = zero_model_state(buffers=buffers,
131
+ param_shapes=param_shapes,
132
+ shared_params=shared_params,
133
+ ds_version=ds_version,
134
+ frozen_param_shapes=frozen_param_shapes,
135
+ frozen_param_fragments=frozen_param_fragments)
136
+ zero_model_states.append(z_model_state)
137
+
138
+ return zero_model_states
139
+
140
+
141
+ def parse_optim_states(files, ds_checkpoint_dir):
142
+
143
+ total_files = len(files)
144
+ state_dicts = []
145
+ for f in files:
146
+ state_dict = torch.load(f, map_location=device)
147
+ # immediately discard the potentially huge 2 optimizer states as we only care for fp32 master weights
148
+ # and also handle the case where it was already removed by another helper script
149
+ state_dict["optimizer_state_dict"].pop("optimizer_state_dict", None)
150
+ state_dicts.append(state_dict)
151
+
152
+ if not ZERO_STAGE in state_dicts[0][OPTIMIZER_STATE_DICT]:
153
+ raise ValueError(f"{files[0]} is not a zero checkpoint")
154
+ zero_stage = state_dicts[0][OPTIMIZER_STATE_DICT][ZERO_STAGE]
155
+ world_size = state_dicts[0][OPTIMIZER_STATE_DICT][PARTITION_COUNT]
156
+
157
+ # For ZeRO-2 each param group can have different partition_count as data parallelism for expert
158
+ # parameters can be different from data parallelism for non-expert parameters. So we can just
159
+ # use the max of the partition_count to get the dp world_size.
160
+
161
+ if type(world_size) is list:
162
+ world_size = max(world_size)
163
+
164
+ if world_size != total_files:
165
+ raise ValueError(
166
+ f"Expected {world_size} of '*_optim_states.pt' under '{ds_checkpoint_dir}' but found {total_files} files. "
167
+ "Possibly due to an overwrite of an old checkpoint, or a checkpoint didn't get saved by one or more processes."
168
+ )
169
+
170
+ # the groups are named differently in each stage
171
+ if zero_stage <= 2:
172
+ fp32_groups_key = SINGLE_PARTITION_OF_FP32_GROUPS
173
+ elif zero_stage == 3:
174
+ fp32_groups_key = FP32_FLAT_GROUPS
175
+ else:
176
+ raise ValueError(f"unknown zero stage {zero_stage}")
177
+
178
+ if zero_stage <= 2:
179
+ fp32_flat_groups = [state_dicts[i][OPTIMIZER_STATE_DICT][fp32_groups_key] for i in range(len(state_dicts))]
180
+ elif zero_stage == 3:
181
+ # if there is more than one param group, there will be multiple flattened tensors - one
182
+ # flattened tensor per group - for simplicity merge them into a single tensor
183
+ #
184
+ # XXX: could make the script more memory efficient for when there are multiple groups - it
185
+ # will require matching the sub-lists of param_shapes for each param group flattened tensor
186
+
187
+ fp32_flat_groups = [
188
+ torch.cat(state_dicts[i][OPTIMIZER_STATE_DICT][fp32_groups_key], 0) for i in range(len(state_dicts))
189
+ ]
190
+
191
+ return zero_stage, world_size, fp32_flat_groups
192
+
193
+
194
+ def _get_fp32_state_dict_from_zero_checkpoint(ds_checkpoint_dir, exclude_frozen_parameters):
195
+ """
196
+ Returns fp32 state_dict reconstructed from ds checkpoint
197
+
198
+ Args:
199
+ - ``ds_checkpoint_dir``: path to the deepspeed checkpoint folder (where the optimizer files are)
200
+
201
+ """
202
+ print(f"Processing zero checkpoint '{ds_checkpoint_dir}'")
203
+
204
+ optim_files = get_optim_files(ds_checkpoint_dir)
205
+ zero_stage, world_size, fp32_flat_groups = parse_optim_states(optim_files, ds_checkpoint_dir)
206
+ print(f"Detected checkpoint of type zero stage {zero_stage}, world_size: {world_size}")
207
+
208
+ model_files = get_model_state_files(ds_checkpoint_dir)
209
+
210
+ zero_model_states = parse_model_states(model_files)
211
+ print(f'Parsing checkpoint created by deepspeed=={zero_model_states[0].ds_version}')
212
+
213
+ if zero_stage <= 2:
214
+ return _get_fp32_state_dict_from_zero2_checkpoint(world_size, fp32_flat_groups, zero_model_states,
215
+ exclude_frozen_parameters)
216
+ elif zero_stage == 3:
217
+ return _get_fp32_state_dict_from_zero3_checkpoint(world_size, fp32_flat_groups, zero_model_states,
218
+ exclude_frozen_parameters)
219
+
220
+
221
+ def _zero2_merge_frozen_params(state_dict, zero_model_states):
222
+ if zero_model_states[0].frozen_param_shapes is None or len(zero_model_states[0].frozen_param_shapes) == 0:
223
+ return
224
+
225
+ frozen_param_shapes = zero_model_states[0].frozen_param_shapes
226
+ frozen_param_fragments = zero_model_states[0].frozen_param_fragments
227
+
228
+ if debug:
229
+ num_elem = sum(s.numel() for s in frozen_param_shapes.values())
230
+ print(f'rank 0: {FROZEN_PARAM_SHAPES}.numel = {num_elem}')
231
+
232
+ wanted_params = len(frozen_param_shapes)
233
+ wanted_numel = sum(s.numel() for s in frozen_param_shapes.values())
234
+ avail_numel = sum([p.numel() for p in frozen_param_fragments.values()])
235
+ print(f'Frozen params: Have {avail_numel} numels to process.')
236
+ print(f'Frozen params: Need {wanted_numel} numels in {wanted_params} params')
237
+
238
+ total_params = 0
239
+ total_numel = 0
240
+ for name, shape in frozen_param_shapes.items():
241
+ total_params += 1
242
+ unpartitioned_numel = shape.numel()
243
+ total_numel += unpartitioned_numel
244
+
245
+ state_dict[name] = frozen_param_fragments[name]
246
+
247
+ if debug:
248
+ print(f"{name} full shape: {shape} unpartitioned numel {unpartitioned_numel} ")
249
+
250
+ print(f"Reconstructed Frozen fp32 state dict with {total_params} params {total_numel} elements")
251
+
252
+
253
+ def _has_callable(obj, fn):
254
+ attr = getattr(obj, fn, None)
255
+ return callable(attr)
256
+
257
+
258
+ def _zero2_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states):
259
+ param_shapes = zero_model_states[0].param_shapes
260
+
261
+ # Reconstruction protocol:
262
+ #
263
+ # XXX: document this
264
+
265
+ if debug:
266
+ for i in range(world_size):
267
+ for j in range(len(fp32_flat_groups[0])):
268
+ print(f"{FP32_FLAT_GROUPS}[{i}][{j}].shape={fp32_flat_groups[i][j].shape}")
269
+
270
+ # XXX: memory usage doubles here (zero2)
271
+ num_param_groups = len(fp32_flat_groups[0])
272
+ merged_single_partition_of_fp32_groups = []
273
+ for i in range(num_param_groups):
274
+ merged_partitions = [sd[i] for sd in fp32_flat_groups]
275
+ full_single_fp32_vector = torch.cat(merged_partitions, 0)
276
+ merged_single_partition_of_fp32_groups.append(full_single_fp32_vector)
277
+ avail_numel = sum(
278
+ [full_single_fp32_vector.numel() for full_single_fp32_vector in merged_single_partition_of_fp32_groups])
279
+
280
+ if debug:
281
+ wanted_params = sum([len(shapes) for shapes in param_shapes])
282
+ wanted_numel = sum([sum(shape.numel() for shape in shapes.values()) for shapes in param_shapes])
283
+ # not asserting if there is a mismatch due to possible padding
284
+ print(f"Have {avail_numel} numels to process.")
285
+ print(f"Need {wanted_numel} numels in {wanted_params} params.")
286
+
287
+ # params
288
+ # XXX: for huge models that can't fit into the host's RAM we will have to recode this to support
289
+ # out-of-core computing solution
290
+ total_numel = 0
291
+ total_params = 0
292
+ for shapes, full_single_fp32_vector in zip(param_shapes, merged_single_partition_of_fp32_groups):
293
+ offset = 0
294
+ avail_numel = full_single_fp32_vector.numel()
295
+ for name, shape in shapes.items():
296
+
297
+ unpartitioned_numel = shape.numel() if _has_callable(shape, 'numel') else math.prod(shape)
298
+ total_numel += unpartitioned_numel
299
+ total_params += 1
300
+
301
+ if debug:
302
+ print(f"{name} full shape: {shape} unpartitioned numel {unpartitioned_numel} ")
303
+ state_dict[name] = full_single_fp32_vector.narrow(0, offset, unpartitioned_numel).view(shape)
304
+ offset += unpartitioned_numel
305
+
306
+ # Z2 started to align to 2*world_size to improve nccl performance. Therefore both offset and
307
+ # avail_numel can differ by anywhere between 0..2*world_size. Due to two unrelated complex
308
+ # paddings performed in the code it's almost impossible to predict the exact numbers w/o the
309
+ # live optimizer object, so we are checking that the numbers are within the right range
310
+ align_to = 2 * world_size
311
+
312
+ def zero2_align(x):
313
+ return align_to * math.ceil(x / align_to)
314
+
315
+ if debug:
316
+ print(f"original offset={offset}, avail_numel={avail_numel}")
317
+
318
+ offset = zero2_align(offset)
319
+ avail_numel = zero2_align(avail_numel)
320
+
321
+ if debug:
322
+ print(f"aligned offset={offset}, avail_numel={avail_numel}")
323
+
324
+ # Sanity check
325
+ if offset != avail_numel:
326
+ raise ValueError(f"consumed {offset} numels out of {avail_numel} - something is wrong")
327
+
328
+ print(f"Reconstructed fp32 state dict with {total_params} params {total_numel} elements")
329
+
330
+
331
+ def _get_fp32_state_dict_from_zero2_checkpoint(world_size, fp32_flat_groups, zero_model_states,
332
+ exclude_frozen_parameters):
333
+ state_dict = OrderedDict()
334
+
335
+ # buffers
336
+ buffers = zero_model_states[0].buffers
337
+ state_dict.update(buffers)
338
+ if debug:
339
+ print(f"added {len(buffers)} buffers")
340
+
341
+ if not exclude_frozen_parameters:
342
+ _zero2_merge_frozen_params(state_dict, zero_model_states)
343
+
344
+ _zero2_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states)
345
+
346
+ # recover shared parameters
347
+ for pair in zero_model_states[0].shared_params:
348
+ if pair[1] in state_dict:
349
+ state_dict[pair[0]] = state_dict[pair[1]]
350
+
351
+ return state_dict
352
+
353
+
354
+ def zero3_partitioned_param_info(unpartitioned_numel, world_size):
355
+ remainder = unpartitioned_numel % world_size
356
+ padding_numel = (world_size - remainder) if remainder else 0
357
+ partitioned_numel = math.ceil(unpartitioned_numel / world_size)
358
+ return partitioned_numel, padding_numel
359
+
360
+
361
+ def _zero3_merge_frozen_params(state_dict, world_size, zero_model_states):
362
+ if zero_model_states[0].frozen_param_shapes is None or len(zero_model_states[0].frozen_param_shapes) == 0:
363
+ return
364
+
365
+ if debug:
366
+ for i in range(world_size):
367
+ num_elem = sum(s.numel() for s in zero_model_states[i].frozen_param_fragments.values())
368
+ print(f'rank {i}: {FROZEN_PARAM_SHAPES}.numel = {num_elem}')
369
+
370
+ frozen_param_shapes = zero_model_states[0].frozen_param_shapes
371
+ wanted_params = len(frozen_param_shapes)
372
+ wanted_numel = sum(s.numel() for s in frozen_param_shapes.values())
373
+ avail_numel = sum([p.numel() for p in zero_model_states[0].frozen_param_fragments.values()]) * world_size
374
+ print(f'Frozen params: Have {avail_numel} numels to process.')
375
+ print(f'Frozen params: Need {wanted_numel} numels in {wanted_params} params')
376
+
377
+ total_params = 0
378
+ total_numel = 0
379
+ for name, shape in zero_model_states[0].frozen_param_shapes.items():
380
+ total_params += 1
381
+ unpartitioned_numel = shape.numel()
382
+ total_numel += unpartitioned_numel
383
+
384
+ param_frags = tuple(model_state.frozen_param_fragments[name] for model_state in zero_model_states)
385
+ state_dict[name] = torch.cat(param_frags, 0).narrow(0, 0, unpartitioned_numel).view(shape)
386
+
387
+ partitioned_numel, partitioned_padding_numel = zero3_partitioned_param_info(unpartitioned_numel, world_size)
388
+
389
+ if debug:
390
+ print(
391
+ f"Frozen params: {total_params} {name} full shape: {shape} partition0 numel={partitioned_numel} partitioned_padding_numel={partitioned_padding_numel}"
392
+ )
393
+
394
+ print(f"Reconstructed Frozen fp32 state dict with {total_params} params {total_numel} elements")
395
+
396
+
397
+ def _zero3_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states):
398
+ param_shapes = zero_model_states[0].param_shapes
399
+ avail_numel = fp32_flat_groups[0].numel() * world_size
400
+ # Reconstruction protocol: For zero3 we need to zip the partitions together at boundary of each
401
+ # param, re-consolidating each param, while dealing with padding if any
402
+
403
+ # merge list of dicts, preserving order
404
+ param_shapes = {k: v for d in param_shapes for k, v in d.items()}
405
+
406
+ if debug:
407
+ for i in range(world_size):
408
+ print(f"{FP32_FLAT_GROUPS}[{i}].shape={fp32_flat_groups[i].shape}")
409
+
410
+ wanted_params = len(param_shapes)
411
+ wanted_numel = sum(shape.numel() for shape in param_shapes.values())
412
+ # not asserting if there is a mismatch due to possible padding
413
+ avail_numel = fp32_flat_groups[0].numel() * world_size
414
+ print(f"Trainable params: Have {avail_numel} numels to process.")
415
+ print(f"Trainable params: Need {wanted_numel} numels in {wanted_params} params.")
416
+
417
+ # params
418
+ # XXX: for huge models that can't fit into the host's RAM we will have to recode this to support
419
+ # out-of-core computing solution
420
+ offset = 0
421
+ total_numel = 0
422
+ total_params = 0
423
+ for name, shape in param_shapes.items():
424
+
425
+ unpartitioned_numel = shape.numel()
426
+ total_numel += unpartitioned_numel
427
+ total_params += 1
428
+
429
+ partitioned_numel, partitioned_padding_numel = zero3_partitioned_param_info(unpartitioned_numel, world_size)
430
+
431
+ if debug:
432
+ print(
433
+ f"Trainable params: {total_params} {name} full shape: {shape} partition0 numel={partitioned_numel} partitioned_padding_numel={partitioned_padding_numel}"
434
+ )
435
+
436
+ # XXX: memory usage doubles here
437
+ state_dict[name] = torch.cat(
438
+ tuple(fp32_flat_groups[i].narrow(0, offset, partitioned_numel) for i in range(world_size)),
439
+ 0).narrow(0, 0, unpartitioned_numel).view(shape)
440
+ offset += partitioned_numel
441
+
442
+ offset *= world_size
443
+
444
+ # Sanity check
445
+ if offset != avail_numel:
446
+ raise ValueError(f"consumed {offset} numels out of {avail_numel} - something is wrong")
447
+
448
+ print(f"Reconstructed Trainable fp32 state dict with {total_params} params {total_numel} elements")
449
+
450
+
451
+ def _get_fp32_state_dict_from_zero3_checkpoint(world_size, fp32_flat_groups, zero_model_states,
452
+ exclude_frozen_parameters):
453
+ state_dict = OrderedDict()
454
+
455
+ # buffers
456
+ buffers = zero_model_states[0].buffers
457
+ state_dict.update(buffers)
458
+ if debug:
459
+ print(f"added {len(buffers)} buffers")
460
+
461
+ if not exclude_frozen_parameters:
462
+ _zero3_merge_frozen_params(state_dict, world_size, zero_model_states)
463
+
464
+ _zero3_merge_trainable_params(state_dict, world_size, fp32_flat_groups, zero_model_states)
465
+
466
+ # recover shared parameters
467
+ for pair in zero_model_states[0].shared_params:
468
+ if pair[1] in state_dict:
469
+ state_dict[pair[0]] = state_dict[pair[1]]
470
+
471
+ return state_dict
472
+
473
+
474
+ def get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag=None, exclude_frozen_parameters=False):
475
+ """
476
+ Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated state_dict that can be loaded with
477
+ ``load_state_dict()`` and used for training without DeepSpeed or shared with others, for example
478
+ via a model hub.
479
+
480
+ Args:
481
+ - ``checkpoint_dir``: path to the desired checkpoint folder
482
+ - ``tag``: checkpoint tag used as a unique identifier for checkpoint. If not provided will attempt to load tag in 'latest' file. e.g., ``global_step14``
483
+ - ``exclude_frozen_parameters``: exclude frozen parameters
484
+
485
+ Returns:
486
+ - pytorch ``state_dict``
487
+
488
+ Note: this approach may not work if your application doesn't have sufficient free CPU memory and
489
+ you may need to use the offline approach using the ``zero_to_fp32.py`` script that is saved with
490
+ the checkpoint.
491
+
492
+ A typical usage might be ::
493
+
494
+ from deepspeed.utils.zero_to_fp32 import get_fp32_state_dict_from_zero_checkpoint
495
+ # do the training and checkpoint saving
496
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir) # already on cpu
497
+ model = model.cpu() # move to cpu
498
+ model.load_state_dict(state_dict)
499
+ # submit to model hub or save the model to share with others
500
+
501
+ In this example the ``model`` will no longer be usable in the deepspeed context of the same
502
+ application. i.e. you will need to re-initialize the deepspeed engine, since
503
+ ``model.load_state_dict(state_dict)`` will remove all the deepspeed magic from it.
504
+
505
+ If you want it all done for you, use ``load_state_dict_from_zero_checkpoint`` instead.
506
+
507
+ """
508
+ if tag is None:
509
+ latest_path = os.path.join(checkpoint_dir, 'latest')
510
+ if os.path.isfile(latest_path):
511
+ with open(latest_path, 'r') as fd:
512
+ tag = fd.read().strip()
513
+ else:
514
+ raise ValueError(f"Unable to find 'latest' file at {latest_path}")
515
+
516
+ ds_checkpoint_dir = os.path.join(checkpoint_dir, tag)
517
+
518
+ if not os.path.isdir(ds_checkpoint_dir):
519
+ raise FileNotFoundError(f"Directory '{ds_checkpoint_dir}' doesn't exist")
520
+
521
+ return _get_fp32_state_dict_from_zero_checkpoint(ds_checkpoint_dir, exclude_frozen_parameters)
522
+
523
+
524
+ def convert_zero_checkpoint_to_fp32_state_dict(checkpoint_dir, output_file, tag=None, exclude_frozen_parameters=False):
525
+ """
526
+ Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated ``state_dict`` file that can be
527
+ loaded with ``torch.load(file)`` + ``load_state_dict()`` and used for training without DeepSpeed.
528
+
529
+ Args:
530
+ - ``checkpoint_dir``: path to the desired checkpoint folder. (one that contains the tag-folder, like ``global_step14``)
531
+ - ``output_file``: path to the pytorch fp32 state_dict output file (e.g. path/pytorch_model.bin)
532
+ - ``tag``: checkpoint tag used as a unique identifier for checkpoint. If not provided will attempt to load tag in the file named ``latest`` in the checkpoint folder, e.g., ``global_step14``
533
+ - ``exclude_frozen_parameters``: exclude frozen parameters
534
+ """
535
+
536
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag, exclude_frozen_parameters)
537
+ print(f"Saving fp32 state dict to {output_file}")
538
+ torch.save(state_dict, output_file)
539
+
540
+
541
+ def load_state_dict_from_zero_checkpoint(model, checkpoint_dir, tag=None):
542
+ """
543
+ 1. Put the provided model to cpu
544
+ 2. Convert ZeRO 2 or 3 checkpoint into a single fp32 consolidated ``state_dict``
545
+ 3. Load it into the provided model
546
+
547
+ Args:
548
+ - ``model``: the model object to update
549
+ - ``checkpoint_dir``: path to the desired checkpoint folder. (one that contains the tag-folder, like ``global_step14``)
550
+ - ``tag``: checkpoint tag used as a unique identifier for checkpoint. If not provided will attempt to load tag in the file named ``latest`` in the checkpoint folder, e.g., ``global_step14``
551
+
552
+ Returns:
553
+ - ``model`: modified model
554
+
555
+ Make sure you have plenty of CPU memory available before you call this function. If you don't
556
+ have enough use the ``zero_to_fp32.py`` utility to do the conversion. You will find it
557
+ conveniently placed for you in the checkpoint folder.
558
+
559
+ A typical usage might be ::
560
+
561
+ from deepspeed.utils.zero_to_fp32 import load_state_dict_from_zero_checkpoint
562
+ model = load_state_dict_from_zero_checkpoint(trainer.model, checkpoint_dir)
563
+ # submit to model hub or save the model to share with others
564
+
565
+ Note, that once this was run, the ``model`` will no longer be usable in the deepspeed context
566
+ of the same application. i.e. you will need to re-initialize the deepspeed engine, since
567
+ ``model.load_state_dict(state_dict)`` will remove all the deepspeed magic from it.
568
+
569
+ """
570
+ logger.info(f"Extracting fp32 weights")
571
+ state_dict = get_fp32_state_dict_from_zero_checkpoint(checkpoint_dir, tag)
572
+
573
+ logger.info(f"Overwriting model with fp32 weights")
574
+ model = model.cpu()
575
+ model.load_state_dict(state_dict, strict=False)
576
+
577
+ return model
578
+
579
+
580
+ if __name__ == "__main__":
581
+
582
+ parser = argparse.ArgumentParser()
583
+ parser.add_argument("checkpoint_dir",
584
+ type=str,
585
+ help="path to the desired checkpoint folder, e.g., path/checkpoint-12")
586
+ parser.add_argument(
587
+ "output_file",
588
+ type=str,
589
+ help="path to the pytorch fp32 state_dict output file (e.g. path/checkpoint-12/pytorch_model.bin)")
590
+ parser.add_argument("-t",
591
+ "--tag",
592
+ type=str,
593
+ default=None,
594
+ help="checkpoint tag used as a unique identifier for checkpoint. e.g., global_step1")
595
+ parser.add_argument("--exclude_frozen_parameters", action='store_true', help="exclude frozen parameters")
596
+ parser.add_argument("-d", "--debug", action='store_true', help="enable debug")
597
+ args = parser.parse_args()
598
+
599
+ debug = args.debug
600
+
601
+ convert_zero_checkpoint_to_fp32_state_dict(args.checkpoint_dir,
602
+ args.output_file,
603
+ tag=args.tag,
604
+ exclude_frozen_parameters=args.exclude_frozen_parameters)