Add files from e3
Browse files- config.json +27 -0
- configuration_baichuan.py +66 -0
- generation_config.json +7 -0
- model-00001-of-00003.safetensors +3 -0
- model-00002-of-00003.safetensors +3 -0
- model-00003-of-00003.safetensors +3 -0
- model.safetensors.index.json +266 -0
- modeling_baichuan.py +671 -0
- special_tokens_map.json +24 -0
- tokenization_baichuan.py +251 -0
- tokenizer.model +3 -0
- tokenizer_config.json +44 -0
- training_args.bin +3 -0
config.json
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "/fast/rolmedo/models/baichuan-7b/",
|
3 |
+
"architectures": [
|
4 |
+
"BaiChuanForCausalLM"
|
5 |
+
],
|
6 |
+
"auto_map": {
|
7 |
+
"AutoConfig": "configuration_baichuan.BaiChuanConfig",
|
8 |
+
"AutoModelForCausalLM": "modeling_baichuan.BaiChuanForCausalLM"
|
9 |
+
},
|
10 |
+
"bos_token_id": 1,
|
11 |
+
"eos_token_id": 2,
|
12 |
+
"hidden_act": "silu",
|
13 |
+
"hidden_size": 4096,
|
14 |
+
"initializer_range": 0.02,
|
15 |
+
"intermediate_size": 11008,
|
16 |
+
"max_position_embeddings": 4096,
|
17 |
+
"model_type": "baichuan",
|
18 |
+
"num_attention_heads": 32,
|
19 |
+
"num_hidden_layers": 32,
|
20 |
+
"pad_token_id": 0,
|
21 |
+
"rms_norm_eps": 1e-06,
|
22 |
+
"tie_word_embeddings": false,
|
23 |
+
"torch_dtype": "bfloat16",
|
24 |
+
"transformers_version": "4.41.1",
|
25 |
+
"use_cache": true,
|
26 |
+
"vocab_size": 64000
|
27 |
+
}
|
configuration_baichuan.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
3 |
+
#
|
4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
5 |
+
# and OPT implementations in this library. It has been modified from its
|
6 |
+
# original forms to accommodate minor architectural differences compared
|
7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
8 |
+
#
|
9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10 |
+
# you may not use this file except in compliance with the License.
|
11 |
+
# You may obtain a copy of the License at
|
12 |
+
#
|
13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14 |
+
#
|
15 |
+
# Unless required by applicable law or agreed to in writing, software
|
16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18 |
+
# See the License for the specific language governing permissions and
|
19 |
+
# limitations under the License.
|
20 |
+
|
21 |
+
from transformers.configuration_utils import PretrainedConfig
|
22 |
+
from transformers.utils import logging
|
23 |
+
|
24 |
+
|
25 |
+
logger = logging.get_logger(__name__)
|
26 |
+
|
27 |
+
|
28 |
+
class BaiChuanConfig(PretrainedConfig):
|
29 |
+
model_type = "baichuan"
|
30 |
+
keys_to_ignore_at_inference = ["past_key_values"]
|
31 |
+
|
32 |
+
def __init__(
|
33 |
+
self,
|
34 |
+
vocab_size=64000,
|
35 |
+
hidden_size=4096,
|
36 |
+
intermediate_size=11008,
|
37 |
+
num_hidden_layers=32,
|
38 |
+
num_attention_heads=32,
|
39 |
+
hidden_act="silu",
|
40 |
+
max_position_embeddings=4096,
|
41 |
+
initializer_range=0.02,
|
42 |
+
rms_norm_eps=1e-6,
|
43 |
+
use_cache=True,
|
44 |
+
pad_token_id=0,
|
45 |
+
bos_token_id=1,
|
46 |
+
eos_token_id=2,
|
47 |
+
tie_word_embeddings=False,
|
48 |
+
**kwargs,
|
49 |
+
):
|
50 |
+
self.vocab_size = vocab_size
|
51 |
+
self.max_position_embeddings = max_position_embeddings
|
52 |
+
self.hidden_size = hidden_size
|
53 |
+
self.intermediate_size = intermediate_size
|
54 |
+
self.num_hidden_layers = num_hidden_layers
|
55 |
+
self.num_attention_heads = num_attention_heads
|
56 |
+
self.hidden_act = hidden_act
|
57 |
+
self.initializer_range = initializer_range
|
58 |
+
self.rms_norm_eps = rms_norm_eps
|
59 |
+
self.use_cache = use_cache
|
60 |
+
super().__init__(
|
61 |
+
pad_token_id=pad_token_id,
|
62 |
+
bos_token_id=bos_token_id,
|
63 |
+
eos_token_id=eos_token_id,
|
64 |
+
tie_word_embeddings=tie_word_embeddings,
|
65 |
+
**kwargs,
|
66 |
+
)
|
generation_config.json
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_from_model_config": true,
|
3 |
+
"bos_token_id": 1,
|
4 |
+
"eos_token_id": 2,
|
5 |
+
"pad_token_id": 0,
|
6 |
+
"transformers_version": "4.41.1"
|
7 |
+
}
|
model-00001-of-00003.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:63d3c162a8a58ed537bf9f17717bb5be81829551d5d8571c2864813396b5f871
|
3 |
+
size 4976734888
|
model-00002-of-00003.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:24a66fb17af987d4bd6aab84b9cda0004bb8cce020cd00e652abb043a46d2529
|
3 |
+
size 4991433064
|
model-00003-of-00003.safetensors
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:89214c298b52437dc45601c9ba2135ed59f80dbc1fa036b94669730403a21ba9
|
3 |
+
size 4032989128
|
model.safetensors.index.json
ADDED
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"metadata": {
|
3 |
+
"total_size": 14001127424
|
4 |
+
},
|
5 |
+
"weight_map": {
|
6 |
+
"lm_head.weight": "model-00003-of-00003.safetensors",
|
7 |
+
"model.embed_tokens.weight": "model-00001-of-00003.safetensors",
|
8 |
+
"model.layers.0.input_layernorm.weight": "model-00001-of-00003.safetensors",
|
9 |
+
"model.layers.0.mlp.down_proj.weight": "model-00001-of-00003.safetensors",
|
10 |
+
"model.layers.0.mlp.gate_proj.weight": "model-00001-of-00003.safetensors",
|
11 |
+
"model.layers.0.mlp.up_proj.weight": "model-00001-of-00003.safetensors",
|
12 |
+
"model.layers.0.post_attention_layernorm.weight": "model-00001-of-00003.safetensors",
|
13 |
+
"model.layers.0.self_attn.W_pack.weight": "model-00001-of-00003.safetensors",
|
14 |
+
"model.layers.0.self_attn.o_proj.weight": "model-00001-of-00003.safetensors",
|
15 |
+
"model.layers.0.self_attn.rotary_emb.inv_freq": "model-00001-of-00003.safetensors",
|
16 |
+
"model.layers.1.input_layernorm.weight": "model-00001-of-00003.safetensors",
|
17 |
+
"model.layers.1.mlp.down_proj.weight": "model-00001-of-00003.safetensors",
|
18 |
+
"model.layers.1.mlp.gate_proj.weight": "model-00001-of-00003.safetensors",
|
19 |
+
"model.layers.1.mlp.up_proj.weight": "model-00001-of-00003.safetensors",
|
20 |
+
"model.layers.1.post_attention_layernorm.weight": "model-00001-of-00003.safetensors",
|
21 |
+
"model.layers.1.self_attn.W_pack.weight": "model-00001-of-00003.safetensors",
|
22 |
+
"model.layers.1.self_attn.o_proj.weight": "model-00001-of-00003.safetensors",
|
23 |
+
"model.layers.1.self_attn.rotary_emb.inv_freq": "model-00001-of-00003.safetensors",
|
24 |
+
"model.layers.10.input_layernorm.weight": "model-00001-of-00003.safetensors",
|
25 |
+
"model.layers.10.mlp.down_proj.weight": "model-00001-of-00003.safetensors",
|
26 |
+
"model.layers.10.mlp.gate_proj.weight": "model-00001-of-00003.safetensors",
|
27 |
+
"model.layers.10.mlp.up_proj.weight": "model-00001-of-00003.safetensors",
|
28 |
+
"model.layers.10.post_attention_layernorm.weight": "model-00001-of-00003.safetensors",
|
29 |
+
"model.layers.10.self_attn.W_pack.weight": "model-00001-of-00003.safetensors",
|
30 |
+
"model.layers.10.self_attn.o_proj.weight": "model-00001-of-00003.safetensors",
|
31 |
+
"model.layers.10.self_attn.rotary_emb.inv_freq": "model-00001-of-00003.safetensors",
|
32 |
+
"model.layers.11.input_layernorm.weight": "model-00002-of-00003.safetensors",
|
33 |
+
"model.layers.11.mlp.down_proj.weight": "model-00002-of-00003.safetensors",
|
34 |
+
"model.layers.11.mlp.gate_proj.weight": "model-00002-of-00003.safetensors",
|
35 |
+
"model.layers.11.mlp.up_proj.weight": "model-00002-of-00003.safetensors",
|
36 |
+
"model.layers.11.post_attention_layernorm.weight": "model-00002-of-00003.safetensors",
|
37 |
+
"model.layers.11.self_attn.W_pack.weight": "model-00002-of-00003.safetensors",
|
38 |
+
"model.layers.11.self_attn.o_proj.weight": "model-00002-of-00003.safetensors",
|
39 |
+
"model.layers.11.self_attn.rotary_emb.inv_freq": "model-00002-of-00003.safetensors",
|
40 |
+
"model.layers.12.input_layernorm.weight": "model-00002-of-00003.safetensors",
|
41 |
+
"model.layers.12.mlp.down_proj.weight": "model-00002-of-00003.safetensors",
|
42 |
+
"model.layers.12.mlp.gate_proj.weight": "model-00002-of-00003.safetensors",
|
43 |
+
"model.layers.12.mlp.up_proj.weight": "model-00002-of-00003.safetensors",
|
44 |
+
"model.layers.12.post_attention_layernorm.weight": "model-00002-of-00003.safetensors",
|
45 |
+
"model.layers.12.self_attn.W_pack.weight": "model-00002-of-00003.safetensors",
|
46 |
+
"model.layers.12.self_attn.o_proj.weight": "model-00002-of-00003.safetensors",
|
47 |
+
"model.layers.12.self_attn.rotary_emb.inv_freq": "model-00002-of-00003.safetensors",
|
48 |
+
"model.layers.13.input_layernorm.weight": "model-00002-of-00003.safetensors",
|
49 |
+
"model.layers.13.mlp.down_proj.weight": "model-00002-of-00003.safetensors",
|
50 |
+
"model.layers.13.mlp.gate_proj.weight": "model-00002-of-00003.safetensors",
|
51 |
+
"model.layers.13.mlp.up_proj.weight": "model-00002-of-00003.safetensors",
|
52 |
+
"model.layers.13.post_attention_layernorm.weight": "model-00002-of-00003.safetensors",
|
53 |
+
"model.layers.13.self_attn.W_pack.weight": "model-00002-of-00003.safetensors",
|
54 |
+
"model.layers.13.self_attn.o_proj.weight": "model-00002-of-00003.safetensors",
|
55 |
+
"model.layers.13.self_attn.rotary_emb.inv_freq": "model-00002-of-00003.safetensors",
|
56 |
+
"model.layers.14.input_layernorm.weight": "model-00002-of-00003.safetensors",
|
57 |
+
"model.layers.14.mlp.down_proj.weight": "model-00002-of-00003.safetensors",
|
58 |
+
"model.layers.14.mlp.gate_proj.weight": "model-00002-of-00003.safetensors",
|
59 |
+
"model.layers.14.mlp.up_proj.weight": "model-00002-of-00003.safetensors",
|
60 |
+
"model.layers.14.post_attention_layernorm.weight": "model-00002-of-00003.safetensors",
|
61 |
+
"model.layers.14.self_attn.W_pack.weight": "model-00002-of-00003.safetensors",
|
62 |
+
"model.layers.14.self_attn.o_proj.weight": "model-00002-of-00003.safetensors",
|
63 |
+
"model.layers.14.self_attn.rotary_emb.inv_freq": "model-00002-of-00003.safetensors",
|
64 |
+
"model.layers.15.input_layernorm.weight": "model-00002-of-00003.safetensors",
|
65 |
+
"model.layers.15.mlp.down_proj.weight": "model-00002-of-00003.safetensors",
|
66 |
+
"model.layers.15.mlp.gate_proj.weight": "model-00002-of-00003.safetensors",
|
67 |
+
"model.layers.15.mlp.up_proj.weight": "model-00002-of-00003.safetensors",
|
68 |
+
"model.layers.15.post_attention_layernorm.weight": "model-00002-of-00003.safetensors",
|
69 |
+
"model.layers.15.self_attn.W_pack.weight": "model-00002-of-00003.safetensors",
|
70 |
+
"model.layers.15.self_attn.o_proj.weight": "model-00002-of-00003.safetensors",
|
71 |
+
"model.layers.15.self_attn.rotary_emb.inv_freq": "model-00002-of-00003.safetensors",
|
72 |
+
"model.layers.16.input_layernorm.weight": "model-00002-of-00003.safetensors",
|
73 |
+
"model.layers.16.mlp.down_proj.weight": "model-00002-of-00003.safetensors",
|
74 |
+
"model.layers.16.mlp.gate_proj.weight": "model-00002-of-00003.safetensors",
|
75 |
+
"model.layers.16.mlp.up_proj.weight": "model-00002-of-00003.safetensors",
|
76 |
+
"model.layers.16.post_attention_layernorm.weight": "model-00002-of-00003.safetensors",
|
77 |
+
"model.layers.16.self_attn.W_pack.weight": "model-00002-of-00003.safetensors",
|
78 |
+
"model.layers.16.self_attn.o_proj.weight": "model-00002-of-00003.safetensors",
|
79 |
+
"model.layers.16.self_attn.rotary_emb.inv_freq": "model-00002-of-00003.safetensors",
|
80 |
+
"model.layers.17.input_layernorm.weight": "model-00002-of-00003.safetensors",
|
81 |
+
"model.layers.17.mlp.down_proj.weight": "model-00002-of-00003.safetensors",
|
82 |
+
"model.layers.17.mlp.gate_proj.weight": "model-00002-of-00003.safetensors",
|
83 |
+
"model.layers.17.mlp.up_proj.weight": "model-00002-of-00003.safetensors",
|
84 |
+
"model.layers.17.post_attention_layernorm.weight": "model-00002-of-00003.safetensors",
|
85 |
+
"model.layers.17.self_attn.W_pack.weight": "model-00002-of-00003.safetensors",
|
86 |
+
"model.layers.17.self_attn.o_proj.weight": "model-00002-of-00003.safetensors",
|
87 |
+
"model.layers.17.self_attn.rotary_emb.inv_freq": "model-00002-of-00003.safetensors",
|
88 |
+
"model.layers.18.input_layernorm.weight": "model-00002-of-00003.safetensors",
|
89 |
+
"model.layers.18.mlp.down_proj.weight": "model-00002-of-00003.safetensors",
|
90 |
+
"model.layers.18.mlp.gate_proj.weight": "model-00002-of-00003.safetensors",
|
91 |
+
"model.layers.18.mlp.up_proj.weight": "model-00002-of-00003.safetensors",
|
92 |
+
"model.layers.18.post_attention_layernorm.weight": "model-00002-of-00003.safetensors",
|
93 |
+
"model.layers.18.self_attn.W_pack.weight": "model-00002-of-00003.safetensors",
|
94 |
+
"model.layers.18.self_attn.o_proj.weight": "model-00002-of-00003.safetensors",
|
95 |
+
"model.layers.18.self_attn.rotary_emb.inv_freq": "model-00002-of-00003.safetensors",
|
96 |
+
"model.layers.19.input_layernorm.weight": "model-00002-of-00003.safetensors",
|
97 |
+
"model.layers.19.mlp.down_proj.weight": "model-00002-of-00003.safetensors",
|
98 |
+
"model.layers.19.mlp.gate_proj.weight": "model-00002-of-00003.safetensors",
|
99 |
+
"model.layers.19.mlp.up_proj.weight": "model-00002-of-00003.safetensors",
|
100 |
+
"model.layers.19.post_attention_layernorm.weight": "model-00002-of-00003.safetensors",
|
101 |
+
"model.layers.19.self_attn.W_pack.weight": "model-00002-of-00003.safetensors",
|
102 |
+
"model.layers.19.self_attn.o_proj.weight": "model-00002-of-00003.safetensors",
|
103 |
+
"model.layers.19.self_attn.rotary_emb.inv_freq": "model-00002-of-00003.safetensors",
|
104 |
+
"model.layers.2.input_layernorm.weight": "model-00001-of-00003.safetensors",
|
105 |
+
"model.layers.2.mlp.down_proj.weight": "model-00001-of-00003.safetensors",
|
106 |
+
"model.layers.2.mlp.gate_proj.weight": "model-00001-of-00003.safetensors",
|
107 |
+
"model.layers.2.mlp.up_proj.weight": "model-00001-of-00003.safetensors",
|
108 |
+
"model.layers.2.post_attention_layernorm.weight": "model-00001-of-00003.safetensors",
|
109 |
+
"model.layers.2.self_attn.W_pack.weight": "model-00001-of-00003.safetensors",
|
110 |
+
"model.layers.2.self_attn.o_proj.weight": "model-00001-of-00003.safetensors",
|
111 |
+
"model.layers.2.self_attn.rotary_emb.inv_freq": "model-00001-of-00003.safetensors",
|
112 |
+
"model.layers.20.input_layernorm.weight": "model-00002-of-00003.safetensors",
|
113 |
+
"model.layers.20.mlp.down_proj.weight": "model-00002-of-00003.safetensors",
|
114 |
+
"model.layers.20.mlp.gate_proj.weight": "model-00002-of-00003.safetensors",
|
115 |
+
"model.layers.20.mlp.up_proj.weight": "model-00002-of-00003.safetensors",
|
116 |
+
"model.layers.20.post_attention_layernorm.weight": "model-00002-of-00003.safetensors",
|
117 |
+
"model.layers.20.self_attn.W_pack.weight": "model-00002-of-00003.safetensors",
|
118 |
+
"model.layers.20.self_attn.o_proj.weight": "model-00002-of-00003.safetensors",
|
119 |
+
"model.layers.20.self_attn.rotary_emb.inv_freq": "model-00002-of-00003.safetensors",
|
120 |
+
"model.layers.21.input_layernorm.weight": "model-00002-of-00003.safetensors",
|
121 |
+
"model.layers.21.mlp.down_proj.weight": "model-00002-of-00003.safetensors",
|
122 |
+
"model.layers.21.mlp.gate_proj.weight": "model-00002-of-00003.safetensors",
|
123 |
+
"model.layers.21.mlp.up_proj.weight": "model-00002-of-00003.safetensors",
|
124 |
+
"model.layers.21.post_attention_layernorm.weight": "model-00002-of-00003.safetensors",
|
125 |
+
"model.layers.21.self_attn.W_pack.weight": "model-00002-of-00003.safetensors",
|
126 |
+
"model.layers.21.self_attn.o_proj.weight": "model-00002-of-00003.safetensors",
|
127 |
+
"model.layers.21.self_attn.rotary_emb.inv_freq": "model-00002-of-00003.safetensors",
|
128 |
+
"model.layers.22.input_layernorm.weight": "model-00002-of-00003.safetensors",
|
129 |
+
"model.layers.22.mlp.down_proj.weight": "model-00002-of-00003.safetensors",
|
130 |
+
"model.layers.22.mlp.gate_proj.weight": "model-00002-of-00003.safetensors",
|
131 |
+
"model.layers.22.mlp.up_proj.weight": "model-00002-of-00003.safetensors",
|
132 |
+
"model.layers.22.post_attention_layernorm.weight": "model-00002-of-00003.safetensors",
|
133 |
+
"model.layers.22.self_attn.W_pack.weight": "model-00002-of-00003.safetensors",
|
134 |
+
"model.layers.22.self_attn.o_proj.weight": "model-00002-of-00003.safetensors",
|
135 |
+
"model.layers.22.self_attn.rotary_emb.inv_freq": "model-00002-of-00003.safetensors",
|
136 |
+
"model.layers.23.input_layernorm.weight": "model-00003-of-00003.safetensors",
|
137 |
+
"model.layers.23.mlp.down_proj.weight": "model-00003-of-00003.safetensors",
|
138 |
+
"model.layers.23.mlp.gate_proj.weight": "model-00003-of-00003.safetensors",
|
139 |
+
"model.layers.23.mlp.up_proj.weight": "model-00003-of-00003.safetensors",
|
140 |
+
"model.layers.23.post_attention_layernorm.weight": "model-00003-of-00003.safetensors",
|
141 |
+
"model.layers.23.self_attn.W_pack.weight": "model-00002-of-00003.safetensors",
|
142 |
+
"model.layers.23.self_attn.o_proj.weight": "model-00002-of-00003.safetensors",
|
143 |
+
"model.layers.23.self_attn.rotary_emb.inv_freq": "model-00002-of-00003.safetensors",
|
144 |
+
"model.layers.24.input_layernorm.weight": "model-00003-of-00003.safetensors",
|
145 |
+
"model.layers.24.mlp.down_proj.weight": "model-00003-of-00003.safetensors",
|
146 |
+
"model.layers.24.mlp.gate_proj.weight": "model-00003-of-00003.safetensors",
|
147 |
+
"model.layers.24.mlp.up_proj.weight": "model-00003-of-00003.safetensors",
|
148 |
+
"model.layers.24.post_attention_layernorm.weight": "model-00003-of-00003.safetensors",
|
149 |
+
"model.layers.24.self_attn.W_pack.weight": "model-00003-of-00003.safetensors",
|
150 |
+
"model.layers.24.self_attn.o_proj.weight": "model-00003-of-00003.safetensors",
|
151 |
+
"model.layers.24.self_attn.rotary_emb.inv_freq": "model-00003-of-00003.safetensors",
|
152 |
+
"model.layers.25.input_layernorm.weight": "model-00003-of-00003.safetensors",
|
153 |
+
"model.layers.25.mlp.down_proj.weight": "model-00003-of-00003.safetensors",
|
154 |
+
"model.layers.25.mlp.gate_proj.weight": "model-00003-of-00003.safetensors",
|
155 |
+
"model.layers.25.mlp.up_proj.weight": "model-00003-of-00003.safetensors",
|
156 |
+
"model.layers.25.post_attention_layernorm.weight": "model-00003-of-00003.safetensors",
|
157 |
+
"model.layers.25.self_attn.W_pack.weight": "model-00003-of-00003.safetensors",
|
158 |
+
"model.layers.25.self_attn.o_proj.weight": "model-00003-of-00003.safetensors",
|
159 |
+
"model.layers.25.self_attn.rotary_emb.inv_freq": "model-00003-of-00003.safetensors",
|
160 |
+
"model.layers.26.input_layernorm.weight": "model-00003-of-00003.safetensors",
|
161 |
+
"model.layers.26.mlp.down_proj.weight": "model-00003-of-00003.safetensors",
|
162 |
+
"model.layers.26.mlp.gate_proj.weight": "model-00003-of-00003.safetensors",
|
163 |
+
"model.layers.26.mlp.up_proj.weight": "model-00003-of-00003.safetensors",
|
164 |
+
"model.layers.26.post_attention_layernorm.weight": "model-00003-of-00003.safetensors",
|
165 |
+
"model.layers.26.self_attn.W_pack.weight": "model-00003-of-00003.safetensors",
|
166 |
+
"model.layers.26.self_attn.o_proj.weight": "model-00003-of-00003.safetensors",
|
167 |
+
"model.layers.26.self_attn.rotary_emb.inv_freq": "model-00003-of-00003.safetensors",
|
168 |
+
"model.layers.27.input_layernorm.weight": "model-00003-of-00003.safetensors",
|
169 |
+
"model.layers.27.mlp.down_proj.weight": "model-00003-of-00003.safetensors",
|
170 |
+
"model.layers.27.mlp.gate_proj.weight": "model-00003-of-00003.safetensors",
|
171 |
+
"model.layers.27.mlp.up_proj.weight": "model-00003-of-00003.safetensors",
|
172 |
+
"model.layers.27.post_attention_layernorm.weight": "model-00003-of-00003.safetensors",
|
173 |
+
"model.layers.27.self_attn.W_pack.weight": "model-00003-of-00003.safetensors",
|
174 |
+
"model.layers.27.self_attn.o_proj.weight": "model-00003-of-00003.safetensors",
|
175 |
+
"model.layers.27.self_attn.rotary_emb.inv_freq": "model-00003-of-00003.safetensors",
|
176 |
+
"model.layers.28.input_layernorm.weight": "model-00003-of-00003.safetensors",
|
177 |
+
"model.layers.28.mlp.down_proj.weight": "model-00003-of-00003.safetensors",
|
178 |
+
"model.layers.28.mlp.gate_proj.weight": "model-00003-of-00003.safetensors",
|
179 |
+
"model.layers.28.mlp.up_proj.weight": "model-00003-of-00003.safetensors",
|
180 |
+
"model.layers.28.post_attention_layernorm.weight": "model-00003-of-00003.safetensors",
|
181 |
+
"model.layers.28.self_attn.W_pack.weight": "model-00003-of-00003.safetensors",
|
182 |
+
"model.layers.28.self_attn.o_proj.weight": "model-00003-of-00003.safetensors",
|
183 |
+
"model.layers.28.self_attn.rotary_emb.inv_freq": "model-00003-of-00003.safetensors",
|
184 |
+
"model.layers.29.input_layernorm.weight": "model-00003-of-00003.safetensors",
|
185 |
+
"model.layers.29.mlp.down_proj.weight": "model-00003-of-00003.safetensors",
|
186 |
+
"model.layers.29.mlp.gate_proj.weight": "model-00003-of-00003.safetensors",
|
187 |
+
"model.layers.29.mlp.up_proj.weight": "model-00003-of-00003.safetensors",
|
188 |
+
"model.layers.29.post_attention_layernorm.weight": "model-00003-of-00003.safetensors",
|
189 |
+
"model.layers.29.self_attn.W_pack.weight": "model-00003-of-00003.safetensors",
|
190 |
+
"model.layers.29.self_attn.o_proj.weight": "model-00003-of-00003.safetensors",
|
191 |
+
"model.layers.29.self_attn.rotary_emb.inv_freq": "model-00003-of-00003.safetensors",
|
192 |
+
"model.layers.3.input_layernorm.weight": "model-00001-of-00003.safetensors",
|
193 |
+
"model.layers.3.mlp.down_proj.weight": "model-00001-of-00003.safetensors",
|
194 |
+
"model.layers.3.mlp.gate_proj.weight": "model-00001-of-00003.safetensors",
|
195 |
+
"model.layers.3.mlp.up_proj.weight": "model-00001-of-00003.safetensors",
|
196 |
+
"model.layers.3.post_attention_layernorm.weight": "model-00001-of-00003.safetensors",
|
197 |
+
"model.layers.3.self_attn.W_pack.weight": "model-00001-of-00003.safetensors",
|
198 |
+
"model.layers.3.self_attn.o_proj.weight": "model-00001-of-00003.safetensors",
|
199 |
+
"model.layers.3.self_attn.rotary_emb.inv_freq": "model-00001-of-00003.safetensors",
|
200 |
+
"model.layers.30.input_layernorm.weight": "model-00003-of-00003.safetensors",
|
201 |
+
"model.layers.30.mlp.down_proj.weight": "model-00003-of-00003.safetensors",
|
202 |
+
"model.layers.30.mlp.gate_proj.weight": "model-00003-of-00003.safetensors",
|
203 |
+
"model.layers.30.mlp.up_proj.weight": "model-00003-of-00003.safetensors",
|
204 |
+
"model.layers.30.post_attention_layernorm.weight": "model-00003-of-00003.safetensors",
|
205 |
+
"model.layers.30.self_attn.W_pack.weight": "model-00003-of-00003.safetensors",
|
206 |
+
"model.layers.30.self_attn.o_proj.weight": "model-00003-of-00003.safetensors",
|
207 |
+
"model.layers.30.self_attn.rotary_emb.inv_freq": "model-00003-of-00003.safetensors",
|
208 |
+
"model.layers.31.input_layernorm.weight": "model-00003-of-00003.safetensors",
|
209 |
+
"model.layers.31.mlp.down_proj.weight": "model-00003-of-00003.safetensors",
|
210 |
+
"model.layers.31.mlp.gate_proj.weight": "model-00003-of-00003.safetensors",
|
211 |
+
"model.layers.31.mlp.up_proj.weight": "model-00003-of-00003.safetensors",
|
212 |
+
"model.layers.31.post_attention_layernorm.weight": "model-00003-of-00003.safetensors",
|
213 |
+
"model.layers.31.self_attn.W_pack.weight": "model-00003-of-00003.safetensors",
|
214 |
+
"model.layers.31.self_attn.o_proj.weight": "model-00003-of-00003.safetensors",
|
215 |
+
"model.layers.31.self_attn.rotary_emb.inv_freq": "model-00003-of-00003.safetensors",
|
216 |
+
"model.layers.4.input_layernorm.weight": "model-00001-of-00003.safetensors",
|
217 |
+
"model.layers.4.mlp.down_proj.weight": "model-00001-of-00003.safetensors",
|
218 |
+
"model.layers.4.mlp.gate_proj.weight": "model-00001-of-00003.safetensors",
|
219 |
+
"model.layers.4.mlp.up_proj.weight": "model-00001-of-00003.safetensors",
|
220 |
+
"model.layers.4.post_attention_layernorm.weight": "model-00001-of-00003.safetensors",
|
221 |
+
"model.layers.4.self_attn.W_pack.weight": "model-00001-of-00003.safetensors",
|
222 |
+
"model.layers.4.self_attn.o_proj.weight": "model-00001-of-00003.safetensors",
|
223 |
+
"model.layers.4.self_attn.rotary_emb.inv_freq": "model-00001-of-00003.safetensors",
|
224 |
+
"model.layers.5.input_layernorm.weight": "model-00001-of-00003.safetensors",
|
225 |
+
"model.layers.5.mlp.down_proj.weight": "model-00001-of-00003.safetensors",
|
226 |
+
"model.layers.5.mlp.gate_proj.weight": "model-00001-of-00003.safetensors",
|
227 |
+
"model.layers.5.mlp.up_proj.weight": "model-00001-of-00003.safetensors",
|
228 |
+
"model.layers.5.post_attention_layernorm.weight": "model-00001-of-00003.safetensors",
|
229 |
+
"model.layers.5.self_attn.W_pack.weight": "model-00001-of-00003.safetensors",
|
230 |
+
"model.layers.5.self_attn.o_proj.weight": "model-00001-of-00003.safetensors",
|
231 |
+
"model.layers.5.self_attn.rotary_emb.inv_freq": "model-00001-of-00003.safetensors",
|
232 |
+
"model.layers.6.input_layernorm.weight": "model-00001-of-00003.safetensors",
|
233 |
+
"model.layers.6.mlp.down_proj.weight": "model-00001-of-00003.safetensors",
|
234 |
+
"model.layers.6.mlp.gate_proj.weight": "model-00001-of-00003.safetensors",
|
235 |
+
"model.layers.6.mlp.up_proj.weight": "model-00001-of-00003.safetensors",
|
236 |
+
"model.layers.6.post_attention_layernorm.weight": "model-00001-of-00003.safetensors",
|
237 |
+
"model.layers.6.self_attn.W_pack.weight": "model-00001-of-00003.safetensors",
|
238 |
+
"model.layers.6.self_attn.o_proj.weight": "model-00001-of-00003.safetensors",
|
239 |
+
"model.layers.6.self_attn.rotary_emb.inv_freq": "model-00001-of-00003.safetensors",
|
240 |
+
"model.layers.7.input_layernorm.weight": "model-00001-of-00003.safetensors",
|
241 |
+
"model.layers.7.mlp.down_proj.weight": "model-00001-of-00003.safetensors",
|
242 |
+
"model.layers.7.mlp.gate_proj.weight": "model-00001-of-00003.safetensors",
|
243 |
+
"model.layers.7.mlp.up_proj.weight": "model-00001-of-00003.safetensors",
|
244 |
+
"model.layers.7.post_attention_layernorm.weight": "model-00001-of-00003.safetensors",
|
245 |
+
"model.layers.7.self_attn.W_pack.weight": "model-00001-of-00003.safetensors",
|
246 |
+
"model.layers.7.self_attn.o_proj.weight": "model-00001-of-00003.safetensors",
|
247 |
+
"model.layers.7.self_attn.rotary_emb.inv_freq": "model-00001-of-00003.safetensors",
|
248 |
+
"model.layers.8.input_layernorm.weight": "model-00001-of-00003.safetensors",
|
249 |
+
"model.layers.8.mlp.down_proj.weight": "model-00001-of-00003.safetensors",
|
250 |
+
"model.layers.8.mlp.gate_proj.weight": "model-00001-of-00003.safetensors",
|
251 |
+
"model.layers.8.mlp.up_proj.weight": "model-00001-of-00003.safetensors",
|
252 |
+
"model.layers.8.post_attention_layernorm.weight": "model-00001-of-00003.safetensors",
|
253 |
+
"model.layers.8.self_attn.W_pack.weight": "model-00001-of-00003.safetensors",
|
254 |
+
"model.layers.8.self_attn.o_proj.weight": "model-00001-of-00003.safetensors",
|
255 |
+
"model.layers.8.self_attn.rotary_emb.inv_freq": "model-00001-of-00003.safetensors",
|
256 |
+
"model.layers.9.input_layernorm.weight": "model-00001-of-00003.safetensors",
|
257 |
+
"model.layers.9.mlp.down_proj.weight": "model-00001-of-00003.safetensors",
|
258 |
+
"model.layers.9.mlp.gate_proj.weight": "model-00001-of-00003.safetensors",
|
259 |
+
"model.layers.9.mlp.up_proj.weight": "model-00001-of-00003.safetensors",
|
260 |
+
"model.layers.9.post_attention_layernorm.weight": "model-00001-of-00003.safetensors",
|
261 |
+
"model.layers.9.self_attn.W_pack.weight": "model-00001-of-00003.safetensors",
|
262 |
+
"model.layers.9.self_attn.o_proj.weight": "model-00001-of-00003.safetensors",
|
263 |
+
"model.layers.9.self_attn.rotary_emb.inv_freq": "model-00001-of-00003.safetensors",
|
264 |
+
"model.norm.weight": "model-00003-of-00003.safetensors"
|
265 |
+
}
|
266 |
+
}
|
modeling_baichuan.py
ADDED
@@ -0,0 +1,671 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
3 |
+
#
|
4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
5 |
+
# and OPT implementations in this library. It has been modified from its
|
6 |
+
# original forms to accommodate minor architectural differences compared
|
7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
8 |
+
#
|
9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10 |
+
# you may not use this file except in compliance with the License.
|
11 |
+
# You may obtain a copy of the License at
|
12 |
+
#
|
13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14 |
+
#
|
15 |
+
# Unless required by applicable law or agreed to in writing, software
|
16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18 |
+
# See the License for the specific language governing permissions and
|
19 |
+
# limitations under the License.
|
20 |
+
from .configuration_baichuan import BaiChuanConfig
|
21 |
+
from transformers import PreTrainedModel, add_start_docstrings
|
22 |
+
from transformers.activations import ACT2FN
|
23 |
+
from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast, \
|
24 |
+
SequenceClassifierOutputWithPast
|
25 |
+
from transformers.utils import logging, add_start_docstrings_to_model_forward, replace_return_docstrings
|
26 |
+
|
27 |
+
import math
|
28 |
+
from typing import List, Optional, Tuple, Union
|
29 |
+
|
30 |
+
import torch
|
31 |
+
import torch.utils.checkpoint
|
32 |
+
from torch import nn
|
33 |
+
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss
|
34 |
+
|
35 |
+
|
36 |
+
logger = logging.get_logger(__name__)
|
37 |
+
|
38 |
+
# Copied from transformers.models.bart.modeling_bart._make_causal_mask
|
39 |
+
def _make_causal_mask(
|
40 |
+
input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device, past_key_values_length: int = 0
|
41 |
+
):
|
42 |
+
"""
|
43 |
+
Make causal mask used for bi-directional self-attention.
|
44 |
+
"""
|
45 |
+
bsz, tgt_len = input_ids_shape
|
46 |
+
mask = torch.full((tgt_len, tgt_len), torch.tensor(torch.finfo(dtype).min, device=device), device=device)
|
47 |
+
mask_cond = torch.arange(mask.size(-1), device=device)
|
48 |
+
mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0)
|
49 |
+
mask = mask.to(dtype)
|
50 |
+
|
51 |
+
if past_key_values_length > 0:
|
52 |
+
mask = torch.cat([torch.zeros(tgt_len, past_key_values_length, dtype=dtype, device=device), mask], dim=-1)
|
53 |
+
return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len + past_key_values_length)
|
54 |
+
|
55 |
+
|
56 |
+
# Copied from transformers.models.bart.modeling_bart._expand_mask
|
57 |
+
def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None):
|
58 |
+
"""
|
59 |
+
Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`.
|
60 |
+
"""
|
61 |
+
bsz, src_len = mask.size()
|
62 |
+
tgt_len = tgt_len if tgt_len is not None else src_len
|
63 |
+
|
64 |
+
expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype)
|
65 |
+
|
66 |
+
inverted_mask = 1.0 - expanded_mask
|
67 |
+
|
68 |
+
return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min)
|
69 |
+
|
70 |
+
|
71 |
+
class RMSNorm(nn.Module):
|
72 |
+
def __init__(self, hidden_size, eps=1e-6):
|
73 |
+
"""
|
74 |
+
RMSNorm is equivalent to T5LayerNorm
|
75 |
+
"""
|
76 |
+
super().__init__()
|
77 |
+
self.weight = nn.Parameter(torch.ones(hidden_size))
|
78 |
+
self.variance_epsilon = eps
|
79 |
+
|
80 |
+
def forward(self, hidden_states):
|
81 |
+
variance = hidden_states.to(torch.float32).pow(2).mean(-1, keepdim=True)
|
82 |
+
hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon)
|
83 |
+
|
84 |
+
# convert into half-precision if necessary
|
85 |
+
if self.weight.dtype in [torch.float16, torch.bfloat16]:
|
86 |
+
hidden_states = hidden_states.to(self.weight.dtype)
|
87 |
+
|
88 |
+
return self.weight * hidden_states
|
89 |
+
|
90 |
+
|
91 |
+
class RotaryEmbedding(torch.nn.Module):
|
92 |
+
def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None):
|
93 |
+
super().__init__()
|
94 |
+
inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float().to(device) / dim))
|
95 |
+
self.register_buffer("inv_freq", inv_freq)
|
96 |
+
|
97 |
+
# Build here to make `torch.jit.trace` work.
|
98 |
+
self.max_seq_len_cached = max_position_embeddings
|
99 |
+
t = torch.arange(self.max_seq_len_cached, device=self.inv_freq.device, dtype=self.inv_freq.dtype)
|
100 |
+
freqs = torch.einsum("i,j->ij", t, self.inv_freq)
|
101 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
102 |
+
emb = torch.cat((freqs, freqs), dim=-1)
|
103 |
+
self.register_buffer("cos_cached", emb.cos()[None, None, :, :], persistent=False)
|
104 |
+
self.register_buffer("sin_cached", emb.sin()[None, None, :, :], persistent=False)
|
105 |
+
|
106 |
+
def forward(self, x, seq_len=None):
|
107 |
+
# x: [bs, num_attention_heads, seq_len, head_size]
|
108 |
+
# This `if` block is unlikely to be run after we build sin/cos in `__init__`. Keep the logic here just in case.
|
109 |
+
if seq_len > self.max_seq_len_cached:
|
110 |
+
self.max_seq_len_cached = seq_len
|
111 |
+
t = torch.arange(self.max_seq_len_cached, device=x.device, dtype=self.inv_freq.dtype)
|
112 |
+
freqs = torch.einsum("i,j->ij", t, self.inv_freq)
|
113 |
+
# Different from paper, but it uses a different permutation in order to obtain the same calculation
|
114 |
+
emb = torch.cat((freqs, freqs), dim=-1).to(x.device)
|
115 |
+
self.register_buffer("cos_cached", emb.cos()[None, None, :, :], persistent=False)
|
116 |
+
self.register_buffer("sin_cached", emb.sin()[None, None, :, :], persistent=False)
|
117 |
+
return (
|
118 |
+
self.cos_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
|
119 |
+
self.sin_cached[:, :, :seq_len, ...].to(dtype=x.dtype),
|
120 |
+
)
|
121 |
+
|
122 |
+
|
123 |
+
def rotate_half(x):
|
124 |
+
"""Rotates half the hidden dims of the input."""
|
125 |
+
x1 = x[..., : x.shape[-1] // 2]
|
126 |
+
x2 = x[..., x.shape[-1] // 2:]
|
127 |
+
return torch.cat((-x2, x1), dim=-1)
|
128 |
+
|
129 |
+
|
130 |
+
def apply_rotary_pos_emb(q, k, cos, sin, position_ids):
|
131 |
+
# The first two dimensions of cos and sin are always 1, so we can `squeeze` them.
|
132 |
+
cos = cos.squeeze(1).squeeze(0) # [seq_len, dim]
|
133 |
+
sin = sin.squeeze(1).squeeze(0) # [seq_len, dim]
|
134 |
+
cos = cos[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
|
135 |
+
sin = sin[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim]
|
136 |
+
q_embed = (q * cos) + (rotate_half(q) * sin)
|
137 |
+
k_embed = (k * cos) + (rotate_half(k) * sin)
|
138 |
+
return q_embed, k_embed
|
139 |
+
|
140 |
+
|
141 |
+
class MLP(nn.Module):
|
142 |
+
def __init__(
|
143 |
+
self,
|
144 |
+
hidden_size: int,
|
145 |
+
intermediate_size: int,
|
146 |
+
hidden_act: str,
|
147 |
+
):
|
148 |
+
super().__init__()
|
149 |
+
self.gate_proj = nn.Linear(hidden_size, intermediate_size, bias=False)
|
150 |
+
self.down_proj = nn.Linear(intermediate_size, hidden_size, bias=False)
|
151 |
+
self.up_proj = nn.Linear(hidden_size, intermediate_size, bias=False)
|
152 |
+
self.act_fn = ACT2FN[hidden_act]
|
153 |
+
|
154 |
+
def forward(self, x):
|
155 |
+
return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
|
156 |
+
|
157 |
+
|
158 |
+
class Attention(nn.Module):
|
159 |
+
"""Multi-headed attention from 'Attention Is All You Need' paper"""
|
160 |
+
|
161 |
+
def __init__(self, config: BaiChuanConfig):
|
162 |
+
super().__init__()
|
163 |
+
self.config = config
|
164 |
+
self.hidden_size = config.hidden_size
|
165 |
+
self.num_heads = config.num_attention_heads
|
166 |
+
self.head_dim = self.hidden_size // self.num_heads
|
167 |
+
self.max_position_embeddings = config.max_position_embeddings
|
168 |
+
|
169 |
+
if (self.head_dim * self.num_heads) != self.hidden_size:
|
170 |
+
raise ValueError(
|
171 |
+
f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}"
|
172 |
+
f" and `num_heads`: {self.num_heads})."
|
173 |
+
)
|
174 |
+
self.W_pack = nn.Linear(self.hidden_size, 3 * self.hidden_size, bias=False)
|
175 |
+
self.o_proj = nn.Linear(self.num_heads * self.head_dim, self.hidden_size, bias=False)
|
176 |
+
self.rotary_emb = RotaryEmbedding(self.head_dim, max_position_embeddings=self.max_position_embeddings)
|
177 |
+
|
178 |
+
def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
|
179 |
+
return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous()
|
180 |
+
|
181 |
+
def forward(
|
182 |
+
self,
|
183 |
+
hidden_states: torch.Tensor,
|
184 |
+
attention_mask: Optional[torch.Tensor] = None,
|
185 |
+
position_ids: Optional[torch.LongTensor] = None,
|
186 |
+
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
187 |
+
output_attentions: bool = False,
|
188 |
+
use_cache: bool = False,
|
189 |
+
) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
|
190 |
+
bsz, q_len, _ = hidden_states.size()
|
191 |
+
|
192 |
+
proj = self.W_pack(hidden_states)
|
193 |
+
proj = proj.unflatten(-1, (3, self.hidden_size)).unsqueeze(0).transpose(0, -2).squeeze(-2)
|
194 |
+
query_states = proj[0].view(bsz, q_len, self.num_heads, self.head_dim).transpose(1,
|
195 |
+
2) # batch_size x source_len x hidden_size
|
196 |
+
key_states = proj[1].view(bsz, q_len, self.num_heads, self.head_dim).transpose(1,
|
197 |
+
2) # batch_size x target_len x head_size
|
198 |
+
value_states = proj[2].view(bsz, q_len, self.num_heads, self.head_dim).transpose(1,
|
199 |
+
2) # batch_size x source_len x hidden_size
|
200 |
+
|
201 |
+
kv_seq_len = key_states.shape[-2]
|
202 |
+
if past_key_value is not None:
|
203 |
+
kv_seq_len += past_key_value[0].shape[-2]
|
204 |
+
cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len)
|
205 |
+
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids)
|
206 |
+
# [bsz, nh, t, hd]
|
207 |
+
|
208 |
+
if past_key_value is not None:
|
209 |
+
# reuse k, v, self_attention
|
210 |
+
key_states = torch.cat([past_key_value[0], key_states], dim=2)
|
211 |
+
value_states = torch.cat([past_key_value[1], value_states], dim=2)
|
212 |
+
|
213 |
+
past_key_value = (key_states, value_states) if use_cache else None
|
214 |
+
|
215 |
+
attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim)
|
216 |
+
|
217 |
+
if attn_weights.size() != (bsz, self.num_heads, q_len, kv_seq_len):
|
218 |
+
raise ValueError(
|
219 |
+
f"Attention weights should be of size {(bsz, self.num_heads, q_len, kv_seq_len)}, but is"
|
220 |
+
f" {attn_weights.size()}"
|
221 |
+
)
|
222 |
+
|
223 |
+
if attention_mask is not None:
|
224 |
+
if attention_mask.size() != (bsz, 1, q_len, kv_seq_len):
|
225 |
+
raise ValueError(
|
226 |
+
f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}"
|
227 |
+
)
|
228 |
+
attn_weights = attn_weights + attention_mask
|
229 |
+
attn_weights = torch.max(attn_weights, torch.tensor(torch.finfo(attn_weights.dtype).min))
|
230 |
+
|
231 |
+
# upcast attention to fp32
|
232 |
+
attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype)
|
233 |
+
attn_output = torch.matmul(attn_weights, value_states)
|
234 |
+
|
235 |
+
if attn_output.size() != (bsz, self.num_heads, q_len, self.head_dim):
|
236 |
+
raise ValueError(
|
237 |
+
f"`attn_output` should be of size {(bsz, self.num_heads, q_len, self.head_dim)}, but is"
|
238 |
+
f" {attn_output.size()}"
|
239 |
+
)
|
240 |
+
|
241 |
+
attn_output = attn_output.transpose(1, 2)
|
242 |
+
attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
|
243 |
+
|
244 |
+
attn_output = self.o_proj(attn_output)
|
245 |
+
|
246 |
+
if not output_attentions:
|
247 |
+
attn_weights = None
|
248 |
+
|
249 |
+
return attn_output, attn_weights, past_key_value
|
250 |
+
|
251 |
+
|
252 |
+
class DecoderLayer(nn.Module):
|
253 |
+
def __init__(self, config: BaiChuanConfig):
|
254 |
+
super().__init__()
|
255 |
+
self.hidden_size = config.hidden_size
|
256 |
+
self.self_attn = Attention(config=config)
|
257 |
+
self.mlp = MLP(
|
258 |
+
hidden_size=self.hidden_size,
|
259 |
+
intermediate_size=config.intermediate_size,
|
260 |
+
hidden_act=config.hidden_act,
|
261 |
+
)
|
262 |
+
self.input_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
263 |
+
self.post_attention_layernorm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
264 |
+
|
265 |
+
def forward(
|
266 |
+
self,
|
267 |
+
hidden_states: torch.Tensor,
|
268 |
+
attention_mask: Optional[torch.Tensor] = None,
|
269 |
+
position_ids: Optional[torch.LongTensor] = None,
|
270 |
+
past_key_value: Optional[Tuple[torch.Tensor]] = None,
|
271 |
+
output_attentions: Optional[bool] = False,
|
272 |
+
use_cache: Optional[bool] = False,
|
273 |
+
) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]:
|
274 |
+
"""
|
275 |
+
Args:
|
276 |
+
hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
|
277 |
+
attention_mask (`torch.FloatTensor`, *optional*): attention mask of size
|
278 |
+
`(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
|
279 |
+
output_attentions (`bool`, *optional*):
|
280 |
+
Whether or not to return the attentions tensors of all attention layers. See `attentions` under
|
281 |
+
returned tensors for more detail.
|
282 |
+
use_cache (`bool`, *optional*):
|
283 |
+
If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding
|
284 |
+
(see `past_key_values`).
|
285 |
+
past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states
|
286 |
+
"""
|
287 |
+
|
288 |
+
residual = hidden_states
|
289 |
+
|
290 |
+
hidden_states = self.input_layernorm(hidden_states)
|
291 |
+
|
292 |
+
# Self Attention
|
293 |
+
hidden_states, self_attn_weights, present_key_value = self.self_attn(
|
294 |
+
hidden_states=hidden_states,
|
295 |
+
attention_mask=attention_mask,
|
296 |
+
position_ids=position_ids,
|
297 |
+
past_key_value=past_key_value,
|
298 |
+
output_attentions=output_attentions,
|
299 |
+
use_cache=use_cache,
|
300 |
+
)
|
301 |
+
hidden_states = residual + hidden_states
|
302 |
+
|
303 |
+
# Fully Connected
|
304 |
+
residual = hidden_states
|
305 |
+
hidden_states = self.post_attention_layernorm(hidden_states)
|
306 |
+
hidden_states = self.mlp(hidden_states)
|
307 |
+
hidden_states = residual + hidden_states
|
308 |
+
|
309 |
+
outputs = (hidden_states,)
|
310 |
+
|
311 |
+
if output_attentions:
|
312 |
+
outputs += (self_attn_weights,)
|
313 |
+
|
314 |
+
if use_cache:
|
315 |
+
outputs += (present_key_value,)
|
316 |
+
|
317 |
+
return outputs
|
318 |
+
|
319 |
+
|
320 |
+
class PreTrainedModel(PreTrainedModel):
|
321 |
+
config_class = BaiChuanConfig
|
322 |
+
base_model_prefix = "model"
|
323 |
+
supports_gradient_checkpointing = True
|
324 |
+
_no_split_modules = ["DecoderLayer"]
|
325 |
+
_keys_to_ignore_on_load_unexpected = [r"decoder\.version"]
|
326 |
+
|
327 |
+
def _init_weights(self, module):
|
328 |
+
std = self.config.initializer_range
|
329 |
+
if isinstance(module, nn.Linear):
|
330 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
331 |
+
if module.bias is not None:
|
332 |
+
module.bias.data.zero_()
|
333 |
+
elif isinstance(module, nn.Embedding):
|
334 |
+
module.weight.data.normal_(mean=0.0, std=std)
|
335 |
+
if module.padding_idx is not None:
|
336 |
+
module.weight.data[module.padding_idx].zero_()
|
337 |
+
|
338 |
+
def _set_gradient_checkpointing(self, module, value=False):
|
339 |
+
if isinstance(module, Model):
|
340 |
+
module.gradient_checkpointing = value
|
341 |
+
|
342 |
+
|
343 |
+
class Model(PreTrainedModel):
|
344 |
+
"""
|
345 |
+
Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`DecoderLayer`]
|
346 |
+
|
347 |
+
Args:
|
348 |
+
config: BaiChuanConfig
|
349 |
+
"""
|
350 |
+
|
351 |
+
def __init__(self, config: BaiChuanConfig):
|
352 |
+
super().__init__(config)
|
353 |
+
self.padding_idx = config.pad_token_id
|
354 |
+
self.vocab_size = config.vocab_size
|
355 |
+
|
356 |
+
self.embed_tokens = nn.Embedding(config.vocab_size, config.hidden_size, self.padding_idx)
|
357 |
+
self.layers = nn.ModuleList([DecoderLayer(config) for _ in range(config.num_hidden_layers)])
|
358 |
+
self.norm = RMSNorm(config.hidden_size, eps=config.rms_norm_eps)
|
359 |
+
|
360 |
+
self.gradient_checkpointing = False
|
361 |
+
# Initialize weights and apply final processing
|
362 |
+
self.post_init()
|
363 |
+
|
364 |
+
def get_input_embeddings(self):
|
365 |
+
return self.embed_tokens
|
366 |
+
|
367 |
+
def set_input_embeddings(self, value):
|
368 |
+
self.embed_tokens = value
|
369 |
+
|
370 |
+
# Copied from transformers.models.bart.modeling_bart.BartDecoder._prepare_decoder_attention_mask
|
371 |
+
def _prepare_decoder_attention_mask(self, attention_mask, input_shape, inputs_embeds, past_key_values_length):
|
372 |
+
# create causal mask
|
373 |
+
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
|
374 |
+
combined_attention_mask = None
|
375 |
+
if input_shape[-1] > 1:
|
376 |
+
combined_attention_mask = _make_causal_mask(
|
377 |
+
input_shape,
|
378 |
+
inputs_embeds.dtype,
|
379 |
+
device=inputs_embeds.device,
|
380 |
+
past_key_values_length=past_key_values_length,
|
381 |
+
)
|
382 |
+
|
383 |
+
if attention_mask is not None:
|
384 |
+
# [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len]
|
385 |
+
expanded_attn_mask = _expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to(
|
386 |
+
inputs_embeds.device
|
387 |
+
)
|
388 |
+
combined_attention_mask = (
|
389 |
+
expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask
|
390 |
+
)
|
391 |
+
|
392 |
+
return combined_attention_mask
|
393 |
+
|
394 |
+
def forward(
|
395 |
+
self,
|
396 |
+
input_ids: torch.LongTensor = None,
|
397 |
+
attention_mask: Optional[torch.Tensor] = None,
|
398 |
+
position_ids: Optional[torch.LongTensor] = None,
|
399 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
400 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
401 |
+
use_cache: Optional[bool] = None,
|
402 |
+
output_attentions: Optional[bool] = None,
|
403 |
+
output_hidden_states: Optional[bool] = None,
|
404 |
+
return_dict: Optional[bool] = None,
|
405 |
+
) -> Union[Tuple, BaseModelOutputWithPast]:
|
406 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
407 |
+
output_hidden_states = (
|
408 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
409 |
+
)
|
410 |
+
use_cache = use_cache if use_cache is not None else self.config.use_cache
|
411 |
+
|
412 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
413 |
+
|
414 |
+
# retrieve input_ids and inputs_embeds
|
415 |
+
if input_ids is not None and inputs_embeds is not None:
|
416 |
+
raise ValueError("You cannot specify both decoder_input_ids and decoder_inputs_embeds at the same time")
|
417 |
+
elif input_ids is not None:
|
418 |
+
batch_size, seq_length = input_ids.shape
|
419 |
+
elif inputs_embeds is not None:
|
420 |
+
batch_size, seq_length, _ = inputs_embeds.shape
|
421 |
+
else:
|
422 |
+
raise ValueError("You have to specify either decoder_input_ids or decoder_inputs_embeds")
|
423 |
+
|
424 |
+
seq_length_with_past = seq_length
|
425 |
+
past_key_values_length = 0
|
426 |
+
|
427 |
+
if past_key_values is not None:
|
428 |
+
past_key_values_length = past_key_values[0][0].shape[2]
|
429 |
+
seq_length_with_past = seq_length_with_past + past_key_values_length
|
430 |
+
|
431 |
+
if position_ids is None:
|
432 |
+
device = input_ids.device if input_ids is not None else inputs_embeds.device
|
433 |
+
position_ids = torch.arange(
|
434 |
+
past_key_values_length, seq_length + past_key_values_length, dtype=torch.long, device=device
|
435 |
+
)
|
436 |
+
position_ids = position_ids.unsqueeze(0).view(-1, seq_length)
|
437 |
+
else:
|
438 |
+
position_ids = position_ids.view(-1, seq_length).long()
|
439 |
+
|
440 |
+
if inputs_embeds is None:
|
441 |
+
inputs_embeds = self.embed_tokens(input_ids)
|
442 |
+
# embed positions
|
443 |
+
if attention_mask is None:
|
444 |
+
attention_mask = torch.ones(
|
445 |
+
(batch_size, seq_length_with_past), dtype=torch.bool, device=inputs_embeds.device
|
446 |
+
)
|
447 |
+
attention_mask = self._prepare_decoder_attention_mask(
|
448 |
+
attention_mask, (batch_size, seq_length), inputs_embeds, past_key_values_length
|
449 |
+
)
|
450 |
+
|
451 |
+
hidden_states = inputs_embeds
|
452 |
+
|
453 |
+
if self.gradient_checkpointing and self.training:
|
454 |
+
if use_cache:
|
455 |
+
logger.warning_once(
|
456 |
+
"`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
|
457 |
+
)
|
458 |
+
use_cache = False
|
459 |
+
|
460 |
+
# decoder layers
|
461 |
+
all_hidden_states = () if output_hidden_states else None
|
462 |
+
all_self_attns = () if output_attentions else None
|
463 |
+
next_decoder_cache = () if use_cache else None
|
464 |
+
|
465 |
+
for idx, decoder_layer in enumerate(self.layers):
|
466 |
+
if output_hidden_states:
|
467 |
+
all_hidden_states += (hidden_states,)
|
468 |
+
|
469 |
+
past_key_value = past_key_values[idx] if past_key_values is not None else None
|
470 |
+
|
471 |
+
if self.gradient_checkpointing and self.training:
|
472 |
+
|
473 |
+
def create_custom_forward(module):
|
474 |
+
def custom_forward(*inputs):
|
475 |
+
# None for past_key_value
|
476 |
+
return module(*inputs, output_attentions, None)
|
477 |
+
|
478 |
+
return custom_forward
|
479 |
+
|
480 |
+
layer_outputs = torch.utils.checkpoint.checkpoint(
|
481 |
+
create_custom_forward(decoder_layer),
|
482 |
+
hidden_states,
|
483 |
+
attention_mask,
|
484 |
+
position_ids,
|
485 |
+
None,
|
486 |
+
)
|
487 |
+
else:
|
488 |
+
layer_outputs = decoder_layer(
|
489 |
+
hidden_states,
|
490 |
+
attention_mask=attention_mask,
|
491 |
+
position_ids=position_ids,
|
492 |
+
past_key_value=past_key_value,
|
493 |
+
output_attentions=output_attentions,
|
494 |
+
use_cache=use_cache,
|
495 |
+
)
|
496 |
+
|
497 |
+
hidden_states = layer_outputs[0]
|
498 |
+
|
499 |
+
if use_cache:
|
500 |
+
next_decoder_cache += (layer_outputs[2 if output_attentions else 1],)
|
501 |
+
|
502 |
+
if output_attentions:
|
503 |
+
all_self_attns += (layer_outputs[1],)
|
504 |
+
|
505 |
+
hidden_states = self.norm(hidden_states)
|
506 |
+
|
507 |
+
# add hidden states from the last decoder layer
|
508 |
+
if output_hidden_states:
|
509 |
+
all_hidden_states += (hidden_states,)
|
510 |
+
|
511 |
+
next_cache = next_decoder_cache if use_cache else None
|
512 |
+
if not return_dict:
|
513 |
+
return tuple(v for v in [hidden_states, next_cache, all_hidden_states, all_self_attns] if v is not None)
|
514 |
+
return BaseModelOutputWithPast(
|
515 |
+
last_hidden_state=hidden_states,
|
516 |
+
past_key_values=next_cache,
|
517 |
+
hidden_states=all_hidden_states,
|
518 |
+
attentions=all_self_attns,
|
519 |
+
)
|
520 |
+
|
521 |
+
|
522 |
+
class BaiChuanForCausalLM(PreTrainedModel):
|
523 |
+
def __init__(self, config):
|
524 |
+
super().__init__(config)
|
525 |
+
self.model = Model(config)
|
526 |
+
|
527 |
+
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
|
528 |
+
|
529 |
+
# Initialize weights and apply final processing
|
530 |
+
self.post_init()
|
531 |
+
|
532 |
+
def get_input_embeddings(self):
|
533 |
+
return self.model.embed_tokens
|
534 |
+
|
535 |
+
def set_input_embeddings(self, value):
|
536 |
+
self.model.embed_tokens = value
|
537 |
+
|
538 |
+
def get_output_embeddings(self):
|
539 |
+
return self.lm_head
|
540 |
+
|
541 |
+
def set_output_embeddings(self, new_embeddings):
|
542 |
+
self.lm_head = new_embeddings
|
543 |
+
|
544 |
+
def set_decoder(self, decoder):
|
545 |
+
self.model = decoder
|
546 |
+
|
547 |
+
def get_decoder(self):
|
548 |
+
return self.model
|
549 |
+
|
550 |
+
def forward(
|
551 |
+
self,
|
552 |
+
input_ids: torch.LongTensor = None,
|
553 |
+
attention_mask: Optional[torch.Tensor] = None,
|
554 |
+
position_ids: Optional[torch.LongTensor] = None,
|
555 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
556 |
+
inputs_embeds: Optional[torch.FloatTensor] = None,
|
557 |
+
labels: Optional[torch.LongTensor] = None,
|
558 |
+
use_cache: Optional[bool] = None,
|
559 |
+
output_attentions: Optional[bool] = None,
|
560 |
+
output_hidden_states: Optional[bool] = None,
|
561 |
+
return_dict: Optional[bool] = None,
|
562 |
+
) -> Union[Tuple, CausalLMOutputWithPast]:
|
563 |
+
r"""
|
564 |
+
Args:
|
565 |
+
labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*):
|
566 |
+
Labels for computing the masked language modeling loss. Indices should either be in `[0, ...,
|
567 |
+
config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored
|
568 |
+
(masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.
|
569 |
+
|
570 |
+
Returns:
|
571 |
+
|
572 |
+
Example:
|
573 |
+
|
574 |
+
```python
|
575 |
+
>>> from transformers import AutoTokenizer, ModelForCausalLM
|
576 |
+
|
577 |
+
>>> model = ModelForCausalLM.from_pretrained(PATH_TO_CONVERTED_WEIGHTS)
|
578 |
+
>>> tokenizer = AutoTokenizer.from_pretrained(PATH_TO_CONVERTED_TOKENIZER)
|
579 |
+
|
580 |
+
>>> prompt = "Hey, are you consciours? Can you talk to me?"
|
581 |
+
>>> inputs = tokenizer(prompt, return_tensors="pt")
|
582 |
+
|
583 |
+
>>> # Generate
|
584 |
+
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
|
585 |
+
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
586 |
+
"Hey, are you consciours? Can you talk to me?\nI'm not consciours, but I can talk to you."
|
587 |
+
```"""
|
588 |
+
|
589 |
+
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
590 |
+
output_hidden_states = (
|
591 |
+
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
592 |
+
)
|
593 |
+
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
594 |
+
|
595 |
+
# decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
|
596 |
+
outputs = self.model(
|
597 |
+
input_ids=input_ids,
|
598 |
+
attention_mask=attention_mask,
|
599 |
+
position_ids=position_ids,
|
600 |
+
past_key_values=past_key_values,
|
601 |
+
inputs_embeds=inputs_embeds,
|
602 |
+
use_cache=use_cache,
|
603 |
+
output_attentions=output_attentions,
|
604 |
+
output_hidden_states=output_hidden_states,
|
605 |
+
return_dict=return_dict,
|
606 |
+
)
|
607 |
+
|
608 |
+
hidden_states = outputs[0]
|
609 |
+
logits = self.lm_head(hidden_states)
|
610 |
+
|
611 |
+
loss = None
|
612 |
+
if labels is not None:
|
613 |
+
# Shift so that tokens < n predict n
|
614 |
+
shift_logits = logits[..., :-1, :].contiguous()
|
615 |
+
shift_labels = labels[..., 1:].contiguous()
|
616 |
+
# Flatten the tokens
|
617 |
+
loss_fct = CrossEntropyLoss()
|
618 |
+
shift_logits = shift_logits.view(-1, self.config.vocab_size)
|
619 |
+
shift_labels = shift_labels.view(-1)
|
620 |
+
# Enable model parallelism
|
621 |
+
shift_labels = shift_labels.to(shift_logits.device)
|
622 |
+
loss = loss_fct(shift_logits, shift_labels)
|
623 |
+
|
624 |
+
if not return_dict:
|
625 |
+
output = (logits,) + outputs[1:]
|
626 |
+
return (loss,) + output if loss is not None else output
|
627 |
+
|
628 |
+
return CausalLMOutputWithPast(
|
629 |
+
loss=loss,
|
630 |
+
logits=logits,
|
631 |
+
past_key_values=outputs.past_key_values,
|
632 |
+
hidden_states=outputs.hidden_states,
|
633 |
+
attentions=outputs.attentions,
|
634 |
+
)
|
635 |
+
|
636 |
+
def prepare_inputs_for_generation(
|
637 |
+
self, input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs
|
638 |
+
):
|
639 |
+
if past_key_values:
|
640 |
+
input_ids = input_ids[:, -1:]
|
641 |
+
|
642 |
+
position_ids = kwargs.get("position_ids", None)
|
643 |
+
if attention_mask is not None and position_ids is None:
|
644 |
+
# create position_ids on the fly for batch generation
|
645 |
+
position_ids = attention_mask.long().cumsum(-1) - 1
|
646 |
+
position_ids.masked_fill_(attention_mask == 0, 1)
|
647 |
+
if past_key_values:
|
648 |
+
position_ids = position_ids[:, -1].unsqueeze(-1)
|
649 |
+
|
650 |
+
# if `inputs_embeds` are passed, we only want to use them in the 1st generation step
|
651 |
+
if inputs_embeds is not None and past_key_values is None:
|
652 |
+
model_inputs = {"inputs_embeds": inputs_embeds}
|
653 |
+
else:
|
654 |
+
model_inputs = {"input_ids": input_ids}
|
655 |
+
|
656 |
+
model_inputs.update(
|
657 |
+
{
|
658 |
+
"position_ids": position_ids,
|
659 |
+
"past_key_values": past_key_values,
|
660 |
+
"use_cache": kwargs.get("use_cache"),
|
661 |
+
"attention_mask": attention_mask,
|
662 |
+
}
|
663 |
+
)
|
664 |
+
return model_inputs
|
665 |
+
|
666 |
+
@staticmethod
|
667 |
+
def _reorder_cache(past_key_values, beam_idx):
|
668 |
+
reordered_past = ()
|
669 |
+
for layer_past in past_key_values:
|
670 |
+
reordered_past += (tuple(past_state.index_select(0, beam_idx) for past_state in layer_past),)
|
671 |
+
return reordered_past
|
special_tokens_map.json
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token": {
|
3 |
+
"content": "<s>",
|
4 |
+
"lstrip": false,
|
5 |
+
"normalized": true,
|
6 |
+
"rstrip": false,
|
7 |
+
"single_word": false
|
8 |
+
},
|
9 |
+
"eos_token": {
|
10 |
+
"content": "</s>",
|
11 |
+
"lstrip": false,
|
12 |
+
"normalized": true,
|
13 |
+
"rstrip": false,
|
14 |
+
"single_word": false
|
15 |
+
},
|
16 |
+
"pad_token": "</s>",
|
17 |
+
"unk_token": {
|
18 |
+
"content": "<unk>",
|
19 |
+
"lstrip": false,
|
20 |
+
"normalized": true,
|
21 |
+
"rstrip": false,
|
22 |
+
"single_word": false
|
23 |
+
}
|
24 |
+
}
|
tokenization_baichuan.py
ADDED
@@ -0,0 +1,251 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
|
3 |
+
#
|
4 |
+
# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
|
5 |
+
# and OPT implementations in this library. It has been modified from its
|
6 |
+
# original forms to accommodate minor architectural differences compared
|
7 |
+
# to GPT-NeoX and OPT used by the Meta AI team that trained the model.
|
8 |
+
#
|
9 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
10 |
+
# you may not use this file except in compliance with the License.
|
11 |
+
# You may obtain a copy of the License at
|
12 |
+
#
|
13 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
14 |
+
#
|
15 |
+
# Unless required by applicable law or agreed to in writing, software
|
16 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
17 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
18 |
+
# See the License for the specific language governing permissions and
|
19 |
+
# limitations under the License.
|
20 |
+
|
21 |
+
import os
|
22 |
+
from shutil import copyfile
|
23 |
+
from typing import Any, Dict, List, Optional, Tuple
|
24 |
+
|
25 |
+
import sentencepiece as spm
|
26 |
+
|
27 |
+
from transformers.tokenization_utils import AddedToken, PreTrainedTokenizer
|
28 |
+
from transformers.utils import logging
|
29 |
+
|
30 |
+
|
31 |
+
logger = logging.get_logger(__name__)
|
32 |
+
|
33 |
+
VOCAB_FILES_NAMES = {"vocab_file": "tokenizer.model"}
|
34 |
+
|
35 |
+
PRETRAINED_VOCAB_FILES_MAP = {
|
36 |
+
"vocab_file": {},
|
37 |
+
"tokenizer_file": {},
|
38 |
+
}
|
39 |
+
PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES = {}
|
40 |
+
|
41 |
+
|
42 |
+
class BaiChuanTokenizer(PreTrainedTokenizer):
|
43 |
+
"""
|
44 |
+
Construct a BaiChuan tokenizer. Based on byte-level Byte-Pair-Encoding.
|
45 |
+
|
46 |
+
Args:
|
47 |
+
vocab_file (`str`):
|
48 |
+
Path to the vocabulary file.
|
49 |
+
"""
|
50 |
+
|
51 |
+
vocab_files_names = VOCAB_FILES_NAMES
|
52 |
+
pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
|
53 |
+
max_model_input_sizes = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
|
54 |
+
model_input_names = ["input_ids", "attention_mask"]
|
55 |
+
|
56 |
+
def __init__(
|
57 |
+
self,
|
58 |
+
vocab_file,
|
59 |
+
unk_token="<unk>",
|
60 |
+
bos_token="<s>",
|
61 |
+
eos_token="</s>",
|
62 |
+
pad_token=None,
|
63 |
+
sp_model_kwargs: Optional[Dict[str, Any]] = None,
|
64 |
+
add_bos_token=True,
|
65 |
+
add_eos_token=False,
|
66 |
+
clean_up_tokenization_spaces=False,
|
67 |
+
**kwargs,
|
68 |
+
):
|
69 |
+
self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs
|
70 |
+
bos_token = AddedToken(bos_token, lstrip=False, rstrip=False) if isinstance(bos_token, str) else bos_token
|
71 |
+
eos_token = AddedToken(eos_token, lstrip=False, rstrip=False) if isinstance(eos_token, str) else eos_token
|
72 |
+
unk_token = AddedToken(unk_token, lstrip=False, rstrip=False) if isinstance(unk_token, str) else unk_token
|
73 |
+
pad_token = AddedToken(pad_token, lstrip=False, rstrip=False) if isinstance(pad_token, str) else pad_token
|
74 |
+
self.vocab_file = vocab_file
|
75 |
+
self.add_bos_token = add_bos_token
|
76 |
+
self.add_eos_token = add_eos_token
|
77 |
+
self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs)
|
78 |
+
self.sp_model.Load(vocab_file)
|
79 |
+
|
80 |
+
super().__init__(
|
81 |
+
bos_token=bos_token,
|
82 |
+
eos_token=eos_token,
|
83 |
+
unk_token=unk_token,
|
84 |
+
pad_token=pad_token,
|
85 |
+
add_bos_token=add_bos_token,
|
86 |
+
add_eos_token=add_eos_token,
|
87 |
+
sp_model_kwargs=self.sp_model_kwargs,
|
88 |
+
clean_up_tokenization_spaces=clean_up_tokenization_spaces,
|
89 |
+
**kwargs,
|
90 |
+
)
|
91 |
+
|
92 |
+
def __getstate__(self):
|
93 |
+
state = self.__dict__.copy()
|
94 |
+
state["sp_model"] = None
|
95 |
+
return state
|
96 |
+
|
97 |
+
def __setstate__(self, d):
|
98 |
+
self.__dict__ = d
|
99 |
+
self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs)
|
100 |
+
self.sp_model.Load(self.vocab_file)
|
101 |
+
|
102 |
+
@property
|
103 |
+
def vocab_size(self):
|
104 |
+
"""Returns vocab size"""
|
105 |
+
return self.sp_model.get_piece_size()
|
106 |
+
|
107 |
+
def get_vocab(self):
|
108 |
+
"""Returns vocab as a dict"""
|
109 |
+
vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
|
110 |
+
vocab.update(self.added_tokens_encoder)
|
111 |
+
return vocab
|
112 |
+
|
113 |
+
def _tokenize(self, text):
|
114 |
+
"""Returns a tokenized string."""
|
115 |
+
return self.sp_model.encode(text, out_type=str)
|
116 |
+
|
117 |
+
def _convert_token_to_id(self, token):
|
118 |
+
"""Converts a token (str) in an id using the vocab."""
|
119 |
+
return self.sp_model.piece_to_id(token)
|
120 |
+
|
121 |
+
def _convert_id_to_token(self, index):
|
122 |
+
"""Converts an index (integer) in a token (str) using the vocab."""
|
123 |
+
token = self.sp_model.IdToPiece(index)
|
124 |
+
return token
|
125 |
+
|
126 |
+
def convert_tokens_to_string(self, tokens):
|
127 |
+
"""Converts a sequence of tokens (string) in a single string."""
|
128 |
+
current_sub_tokens = []
|
129 |
+
out_string = ""
|
130 |
+
prev_is_special = False
|
131 |
+
for i, token in enumerate(tokens):
|
132 |
+
# make sure that special tokens are not decoded using sentencepiece model
|
133 |
+
if token in self.all_special_tokens:
|
134 |
+
if not prev_is_special and i != 0:
|
135 |
+
out_string += " "
|
136 |
+
out_string += self.sp_model.decode(current_sub_tokens) + token
|
137 |
+
prev_is_special = True
|
138 |
+
current_sub_tokens = []
|
139 |
+
else:
|
140 |
+
current_sub_tokens.append(token)
|
141 |
+
prev_is_special = False
|
142 |
+
out_string += self.sp_model.decode(current_sub_tokens)
|
143 |
+
return out_string
|
144 |
+
|
145 |
+
def save_vocabulary(self, save_directory, filename_prefix: Optional[str] = None) -> Tuple[str]:
|
146 |
+
"""
|
147 |
+
Save the vocabulary and special tokens file to a directory.
|
148 |
+
|
149 |
+
Args:
|
150 |
+
save_directory (`str`):
|
151 |
+
The directory in which to save the vocabulary.
|
152 |
+
|
153 |
+
Returns:
|
154 |
+
`Tuple(str)`: Paths to the files saved.
|
155 |
+
"""
|
156 |
+
if not os.path.isdir(save_directory):
|
157 |
+
logger.error(f"Vocabulary path ({save_directory}) should be a directory")
|
158 |
+
return
|
159 |
+
out_vocab_file = os.path.join(
|
160 |
+
save_directory, (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]
|
161 |
+
)
|
162 |
+
|
163 |
+
if os.path.abspath(self.vocab_file) != os.path.abspath(out_vocab_file) and os.path.isfile(self.vocab_file):
|
164 |
+
copyfile(self.vocab_file, out_vocab_file)
|
165 |
+
elif not os.path.isfile(self.vocab_file):
|
166 |
+
with open(out_vocab_file, "wb") as fi:
|
167 |
+
content_spiece_model = self.sp_model.serialized_model_proto()
|
168 |
+
fi.write(content_spiece_model)
|
169 |
+
|
170 |
+
return (out_vocab_file,)
|
171 |
+
|
172 |
+
def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
|
173 |
+
bos_token_id = [self.bos_token_id] if self.add_bos_token else []
|
174 |
+
eos_token_id = [self.eos_token_id] if self.add_eos_token else []
|
175 |
+
|
176 |
+
output = bos_token_id + token_ids_0 + eos_token_id
|
177 |
+
|
178 |
+
if token_ids_1 is not None:
|
179 |
+
output = output + bos_token_id + token_ids_1 + eos_token_id
|
180 |
+
|
181 |
+
return output
|
182 |
+
|
183 |
+
def get_special_tokens_mask(
|
184 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None, already_has_special_tokens: bool = False
|
185 |
+
) -> List[int]:
|
186 |
+
"""
|
187 |
+
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
|
188 |
+
special tokens using the tokenizer `prepare_for_model` method.
|
189 |
+
|
190 |
+
Args:
|
191 |
+
token_ids_0 (`List[int]`):
|
192 |
+
List of IDs.
|
193 |
+
token_ids_1 (`List[int]`, *optional*):
|
194 |
+
Optional second list of IDs for sequence pairs.
|
195 |
+
already_has_special_tokens (`bool`, *optional*, defaults to `False`):
|
196 |
+
Whether or not the token list is already formatted with special tokens for the model.
|
197 |
+
|
198 |
+
Returns:
|
199 |
+
`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
|
200 |
+
"""
|
201 |
+
if already_has_special_tokens:
|
202 |
+
return super().get_special_tokens_mask(
|
203 |
+
token_ids_0=token_ids_0, token_ids_1=token_ids_1, already_has_special_tokens=True
|
204 |
+
)
|
205 |
+
|
206 |
+
bos_token_id = [1] if self.add_bos_token else []
|
207 |
+
eos_token_id = [1] if self.add_eos_token else []
|
208 |
+
|
209 |
+
if token_ids_1 is None:
|
210 |
+
return bos_token_id + ([0] * len(token_ids_0)) + eos_token_id
|
211 |
+
return (
|
212 |
+
bos_token_id
|
213 |
+
+ ([0] * len(token_ids_0))
|
214 |
+
+ eos_token_id
|
215 |
+
+ bos_token_id
|
216 |
+
+ ([0] * len(token_ids_1))
|
217 |
+
+ eos_token_id
|
218 |
+
)
|
219 |
+
|
220 |
+
def create_token_type_ids_from_sequences(
|
221 |
+
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
|
222 |
+
) -> List[int]:
|
223 |
+
"""
|
224 |
+
Creates a mask from the two sequences passed to be used in a sequence-pair classification task. An ALBERT
|
225 |
+
sequence pair mask has the following format:
|
226 |
+
|
227 |
+
```
|
228 |
+
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
|
229 |
+
| first sequence | second sequence |
|
230 |
+
```
|
231 |
+
|
232 |
+
if token_ids_1 is None, only returns the first portion of the mask (0s).
|
233 |
+
|
234 |
+
Args:
|
235 |
+
token_ids_0 (`List[int]`):
|
236 |
+
List of ids.
|
237 |
+
token_ids_1 (`List[int]`, *optional*):
|
238 |
+
Optional second list of IDs for sequence pairs.
|
239 |
+
|
240 |
+
Returns:
|
241 |
+
`List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s).
|
242 |
+
"""
|
243 |
+
bos_token_id = [self.bos_token_id] if self.add_bos_token else []
|
244 |
+
eos_token_id = [self.eos_token_id] if self.add_eos_token else []
|
245 |
+
|
246 |
+
output = [0] * len(bos_token_id + token_ids_0 + eos_token_id)
|
247 |
+
|
248 |
+
if token_ids_1 is not None:
|
249 |
+
output += [1] * len(bos_token_id + token_ids_1 + eos_token_id)
|
250 |
+
|
251 |
+
return output
|
tokenizer.model
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4be54af290d93c113bcbf421115ae9eed9d6340408f564898f1e966dc738ef01
|
3 |
+
size 1136699
|
tokenizer_config.json
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_bos_token": false,
|
3 |
+
"add_eos_token": false,
|
4 |
+
"added_tokens_decoder": {
|
5 |
+
"0": {
|
6 |
+
"content": "<unk>",
|
7 |
+
"lstrip": false,
|
8 |
+
"normalized": true,
|
9 |
+
"rstrip": false,
|
10 |
+
"single_word": false,
|
11 |
+
"special": true
|
12 |
+
},
|
13 |
+
"1": {
|
14 |
+
"content": "<s>",
|
15 |
+
"lstrip": false,
|
16 |
+
"normalized": true,
|
17 |
+
"rstrip": false,
|
18 |
+
"single_word": false,
|
19 |
+
"special": true
|
20 |
+
},
|
21 |
+
"2": {
|
22 |
+
"content": "</s>",
|
23 |
+
"lstrip": false,
|
24 |
+
"normalized": true,
|
25 |
+
"rstrip": false,
|
26 |
+
"single_word": false,
|
27 |
+
"special": true
|
28 |
+
}
|
29 |
+
},
|
30 |
+
"auto_map": {
|
31 |
+
"AutoTokenizer": [
|
32 |
+
"tokenization_baichuan.BaiChuanTokenizer",
|
33 |
+
null
|
34 |
+
]
|
35 |
+
},
|
36 |
+
"bos_token": "<s>",
|
37 |
+
"clean_up_tokenization_spaces": false,
|
38 |
+
"eos_token": "</s>",
|
39 |
+
"model_max_length": 1000000000000000019884624838656,
|
40 |
+
"pad_token": "</s>",
|
41 |
+
"sp_model_kwargs": {},
|
42 |
+
"tokenizer_class": "BaiChuanTokenizer",
|
43 |
+
"unk_token": "<unk>"
|
44 |
+
}
|
training_args.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:b4105b5a980568a741dc5dee42b6e135a688d01160032c61f1681329a42ff81a
|
3 |
+
size 5240
|