oilpig commited on
Commit
2f58c6c
1 Parent(s): aaf0503

awq version for baichuan2 rag enhance

Browse files
config.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "_name_or_path": "/home/ubuntu/cuda_gpu_ubuntu_uw2_workplace/models/buffer-baichuan2-13b-rag",
4
+ "architectures": [
5
+ "BaichuanForCausalLM"
6
+ ],
7
+ "auto_map": {
8
+ "AutoConfig": "configuration_baichuan.BaichuanConfig",
9
+ "AutoModelForCausalLM": "modeling_baichuan.BaichuanForCausalLM"
10
+ },
11
+ "bos_token_id": 1,
12
+ "eos_token_id": 2,
13
+ "hidden_act": "silu",
14
+ "hidden_size": 5120,
15
+ "initializer_range": 0.02,
16
+ "intermediate_size": 13696,
17
+ "model_max_length": 4096,
18
+ "model_type": "baichuan",
19
+ "num_attention_heads": 40,
20
+ "num_hidden_layers": 40,
21
+ "pad_token_id": 0,
22
+ "quantization_config": {
23
+ "bits": 4,
24
+ "group_size": 128,
25
+ "quant_method": "awq",
26
+ "version": "gemm",
27
+ "zero_point": true
28
+ },
29
+ "rms_norm_eps": 1e-06,
30
+ "tie_word_embeddings": false,
31
+ "tokenizer_class": "BaichuanTokenizer",
32
+ "torch_dtype": "float16",
33
+ "transformers_version": "4.35.2",
34
+ "use_cache": false,
35
+ "vocab_size": 125696,
36
+ "z_loss_weight": 0
37
+ }
configuration_baichuan.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2023, Baichuan Intelligent Technology. All rights reserved.
2
+
3
+ from transformers.configuration_utils import PretrainedConfig
4
+
5
+
6
+ class BaichuanConfig(PretrainedConfig):
7
+ model_type = "baichuan"
8
+ keys_to_ignore_at_inference = ["past_key_values"]
9
+
10
+ def __init__(
11
+ self,
12
+ vocab_size=125696,
13
+ hidden_size=5120,
14
+ intermediate_size=13696,
15
+ num_hidden_layers=40,
16
+ num_attention_heads=40,
17
+ hidden_act="silu",
18
+ model_max_length=4096,
19
+ initializer_range=0.02,
20
+ rms_norm_eps=1e-6,
21
+ use_cache=True,
22
+ pad_token_id=0,
23
+ bos_token_id=1,
24
+ eos_token_id=2,
25
+ tie_word_embeddings=False,
26
+ gradient_checkpointing=False,
27
+ z_loss_weight=0,
28
+ **kwargs,
29
+ ):
30
+ self.vocab_size = vocab_size
31
+ self.model_max_length = model_max_length
32
+ self.hidden_size = hidden_size
33
+ self.intermediate_size = intermediate_size
34
+ self.num_hidden_layers = num_hidden_layers
35
+ self.num_attention_heads = num_attention_heads
36
+ self.hidden_act = hidden_act
37
+ self.initializer_range = initializer_range
38
+ self.rms_norm_eps = rms_norm_eps
39
+ self.use_cache = use_cache
40
+ self.z_loss_weight = z_loss_weight
41
+ self.gradient_checkpointing = (gradient_checkpointing,)
42
+ super().__init__(
43
+ pad_token_id=pad_token_id,
44
+ bos_token_id=bos_token_id,
45
+ eos_token_id=eos_token_id,
46
+ tie_word_embeddings=tie_word_embeddings,
47
+ **kwargs,
48
+ )
generation_config.json ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "assistant_token_id": 196,
3
+ "bos_token_id": 1,
4
+ "do_sample": true,
5
+ "eos_token_id": 2,
6
+ "max_new_tokens": 2048,
7
+ "pad_token_id": 0,
8
+ "repetition_penalty": 1.05,
9
+ "temperature": 0.3,
10
+ "top_k": 5,
11
+ "top_p": 0.85,
12
+ "transformers_version": "4.35.2",
13
+ "user_token_id": 195
14
+ }
generation_utils.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+ from queue import Queue
3
+
4
+ import torch
5
+
6
+
7
+ def build_chat_input(model, tokenizer, messages: List[dict], max_new_tokens: int=0):
8
+ def _parse_messages(messages, split_role="user"):
9
+ system, rounds = "", []
10
+ round = []
11
+ for i, message in enumerate(messages):
12
+ if message["role"] == "system":
13
+ assert i == 0
14
+ system = message["content"]
15
+ continue
16
+ if message["role"] == split_role and round:
17
+ rounds.append(round)
18
+ round = []
19
+ round.append(message)
20
+ if round:
21
+ rounds.append(round)
22
+ return system, rounds
23
+
24
+ max_new_tokens = max_new_tokens or model.generation_config.max_new_tokens
25
+ max_input_tokens = model.config.model_max_length - max_new_tokens
26
+ system, rounds = _parse_messages(messages, split_role="user")
27
+ system_tokens = tokenizer.encode(system)
28
+ max_history_tokens = max_input_tokens - len(system_tokens)
29
+
30
+ history_tokens = []
31
+ for round in rounds[::-1]:
32
+ round_tokens = []
33
+ for message in round:
34
+ if message["role"] == "user":
35
+ if message.get('context', None):
36
+ round_tokens.extend([197] + tokenizer.encode(message["context"]))
37
+ round_tokens.append(model.generation_config.user_token_id)
38
+ else:
39
+ round_tokens.append(model.generation_config.assistant_token_id)
40
+ round_tokens.extend(tokenizer.encode(message["content"]))
41
+ if len(history_tokens) == 0 or len(history_tokens) + len(round_tokens) <= max_history_tokens:
42
+ history_tokens = round_tokens + history_tokens # concat left
43
+ if len(history_tokens) < max_history_tokens:
44
+ continue
45
+ break
46
+
47
+ input_tokens = system_tokens + history_tokens
48
+ if messages[-1]["role"] != "assistant":
49
+ input_tokens.append(model.generation_config.assistant_token_id)
50
+ input_tokens = input_tokens[-max_input_tokens:] # truncate left
51
+ return torch.LongTensor([input_tokens]).to(model.device)
52
+
53
+
54
+ class TextIterStreamer:
55
+ def __init__(self, tokenizer, skip_prompt=False, skip_special_tokens=False):
56
+ self.tokenizer = tokenizer
57
+ self.skip_prompt = skip_prompt
58
+ self.skip_special_tokens = skip_special_tokens
59
+ self.tokens = []
60
+ self.text_queue = Queue()
61
+ self.next_tokens_are_prompt = True
62
+
63
+ def put(self, value):
64
+ if self.skip_prompt and self.next_tokens_are_prompt:
65
+ self.next_tokens_are_prompt = False
66
+ else:
67
+ if len(value.shape) > 1:
68
+ value = value[0]
69
+ self.tokens.extend(value.tolist())
70
+ self.text_queue.put(
71
+ self.tokenizer.decode(self.tokens, skip_special_tokens=self.skip_special_tokens))
72
+
73
+ def end(self):
74
+ self.text_queue.put(None)
75
+
76
+ def __iter__(self):
77
+ return self
78
+
79
+ def __next__(self):
80
+ value = self.text_queue.get()
81
+ if value is None:
82
+ raise StopIteration()
83
+ else:
84
+ return value
85
+
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e1cf7b0d992265d5118f396ec223a238838caf345eaad38e05e577c6ad66b08b
3
+ size 9125996344
modeling_baichuan.py ADDED
@@ -0,0 +1,830 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2023, Baichuan Intelligent Technology. All rights reserved.
2
+
3
+ from .configuration_baichuan import BaichuanConfig
4
+ from .generation_utils import build_chat_input, TextIterStreamer
5
+
6
+ import math
7
+ from threading import Thread
8
+ from typing import List, Optional, Tuple, Union
9
+
10
+ import torch
11
+ from torch import nn
12
+ from torch.nn import CrossEntropyLoss
13
+ from torch.nn import functional as F
14
+ from transformers import PreTrainedModel, PretrainedConfig
15
+ from transformers.activations import ACT2FN
16
+ from transformers.generation.utils import GenerationConfig
17
+ from transformers.modeling_outputs import BaseModelOutputWithPast, CausalLMOutputWithPast
18
+ from transformers.utils import logging, ContextManagers
19
+
20
+ import os
21
+ from contextlib import contextmanager
22
+ from accelerate import init_empty_weights
23
+
24
+ logger = logging.get_logger(__name__)
25
+
26
+ try:
27
+ from xformers import ops as xops
28
+ except ImportError:
29
+ xops = None
30
+ logger.warning(
31
+ "Xformers is not installed correctly. If you want to use memory_efficient_attention to accelerate training use the following command to install Xformers\npip install xformers."
32
+ )
33
+
34
+
35
+ def _get_interleave(n):
36
+ def _get_interleave_power_of_2(n):
37
+ start = 2 ** (-(2 ** -(math.log2(n) - 3)))
38
+ ratio = start
39
+ return [start * ratio**i for i in range(n)]
40
+
41
+ if math.log2(n).is_integer():
42
+ return _get_interleave_power_of_2(n)
43
+ else:
44
+ closest_power_of_2 = 2 ** math.floor(math.log2(n))
45
+ return (
46
+ _get_interleave_power_of_2(closest_power_of_2)
47
+ + _get_interleave(2 * closest_power_of_2)[0::2][: n - closest_power_of_2]
48
+ )
49
+
50
+
51
+ def _fill_with_neg_inf(t):
52
+ """FP16-compatible function that fills a tensor with -inf."""
53
+ return t.float().fill_(float("-inf")).type_as(t)
54
+
55
+
56
+ def _buffered_future_mask(tensor, maxpos, alibi, attn_heads):
57
+ _future_mask = torch.triu(_fill_with_neg_inf(torch.zeros([maxpos, maxpos])), 1)
58
+ _future_mask = _future_mask.unsqueeze(0) + alibi
59
+ new_future_mask = _future_mask.to(tensor)
60
+ return new_future_mask[: tensor.shape[0] * attn_heads, :maxpos, :maxpos]
61
+
62
+
63
+ def _gen_alibi_mask(tensor, n_head, max_pos):
64
+ slopes = torch.Tensor(_get_interleave(n_head))
65
+ position_point = torch.arange(max_pos) - max_pos + 1
66
+ position_point = position_point.unsqueeze(0).unsqueeze(0).expand(n_head, -1, -1)
67
+ diag = torch.diag(position_point[0])
68
+ position_point = position_point - diag.unsqueeze(0).unsqueeze(0).transpose(-1, -2)
69
+ alibi = slopes.unsqueeze(1).unsqueeze(1) * position_point
70
+ alibi = alibi.view(n_head, 1, max_pos)
71
+ alibi_mask = torch.triu(_fill_with_neg_inf(torch.zeros([max_pos, max_pos])), 1)
72
+ alibi_mask = alibi_mask.unsqueeze(0) + alibi
73
+ return alibi_mask
74
+
75
+
76
+ class RMSNorm(torch.nn.Module):
77
+ def __init__(self, hidden_size, epsilon=1e-6):
78
+ super().__init__()
79
+ self.weight = torch.nn.Parameter(torch.empty(hidden_size))
80
+ self.epsilon = epsilon
81
+
82
+ def forward(self, hidden_states):
83
+ variance = hidden_states.to(torch.float32).pow(2).mean(-1, keepdim=True)
84
+ hidden_states = hidden_states * torch.rsqrt(variance + self.epsilon)
85
+
86
+ # convert into half-precision
87
+ if self.weight.dtype in [torch.float16, torch.bfloat16]:
88
+ hidden_states = hidden_states.to(self.weight.dtype)
89
+
90
+ return self.weight * hidden_states
91
+
92
+
93
+ class MLP(torch.nn.Module):
94
+ def __init__(
95
+ self,
96
+ hidden_size: int,
97
+ intermediate_size: int,
98
+ hidden_act: str,
99
+ ):
100
+ super().__init__()
101
+ self.gate_proj = torch.nn.Linear(hidden_size, intermediate_size, bias=False)
102
+ self.down_proj = torch.nn.Linear(intermediate_size, hidden_size, bias=False)
103
+ self.up_proj = torch.nn.Linear(hidden_size, intermediate_size, bias=False)
104
+ self.act_fn = ACT2FN[hidden_act]
105
+
106
+ def forward(self, x):
107
+ return self.down_proj(self.act_fn(self.gate_proj(x)) * self.up_proj(x))
108
+
109
+
110
+ class BaichuanAttention(torch.nn.Module):
111
+ def __init__(self, config: BaichuanConfig):
112
+ super().__init__()
113
+ self.config = config
114
+ self.hidden_size = config.hidden_size
115
+ self.num_heads = config.num_attention_heads
116
+ self.head_dim = self.hidden_size // self.num_heads
117
+ self.max_position_embeddings = config.model_max_length
118
+
119
+ if (self.head_dim * self.num_heads) != self.hidden_size:
120
+ raise ValueError(
121
+ f"hidden_size {self.hidden_size} is not divisible by num_heads {self.num_heads}"
122
+ )
123
+ self.W_pack = torch.nn.Linear(
124
+ self.hidden_size, 3 * self.hidden_size, bias=False
125
+ )
126
+ self.o_proj = torch.nn.Linear(
127
+ self.num_heads * self.head_dim, self.hidden_size, bias=False
128
+ )
129
+
130
+ def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int):
131
+ return (
132
+ tensor.view(bsz, seq_len, self.num_heads, self.head_dim)
133
+ .transpose(1, 2)
134
+ .contiguous()
135
+ )
136
+
137
+ def forward(
138
+ self,
139
+ hidden_states: torch.Tensor,
140
+ attention_mask: Optional[torch.Tensor] = None,
141
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
142
+ output_attentions: bool = False,
143
+ use_cache: bool = False,
144
+ ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]:
145
+ bsz, q_len, _ = hidden_states.size()
146
+
147
+ proj = self.W_pack(hidden_states)
148
+ proj = (
149
+ proj.unflatten(-1, (3, self.hidden_size))
150
+ .unsqueeze(0)
151
+ .transpose(0, -2)
152
+ .squeeze(-2)
153
+ )
154
+ query_states = (
155
+ proj[0].view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
156
+ )
157
+ key_states = (
158
+ proj[1].view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
159
+ )
160
+ value_states = (
161
+ proj[2].view(bsz, q_len, self.num_heads, self.head_dim).transpose(1, 2)
162
+ )
163
+
164
+ kv_seq_len = key_states.shape[-2]
165
+ if past_key_value is not None:
166
+ kv_seq_len += past_key_value[0].shape[-2]
167
+
168
+ if past_key_value is not None:
169
+ # reuse k, v, self_attention
170
+ key_states = torch.cat([past_key_value[0], key_states], dim=2)
171
+ value_states = torch.cat([past_key_value[1], value_states], dim=2)
172
+
173
+ past_key_value = (key_states, value_states) if use_cache else None
174
+ if xops is not None and self.training:
175
+ attn_weights = None
176
+ # query_states = query_states.transpose(1, 2)
177
+ # key_states = key_states.transpose(1, 2)
178
+ # value_states = value_states.transpose(1, 2)
179
+ # attn_output = xops.memory_efficient_attention(
180
+ # query_states, key_states, value_states, attn_bias=attention_mask
181
+ # )
182
+ with torch.backends.cuda.sdp_kernel(enable_flash=True, enable_math=True, enable_mem_efficient=True):
183
+ attn_output = F.scaled_dot_product_attention(query_states, key_states, value_states, attn_mask = attention_mask)
184
+ attn_output = attn_output.transpose(1, 2)
185
+ else:
186
+ attn_weights = torch.matmul(
187
+ query_states, key_states.transpose(2, 3)
188
+ ) / math.sqrt(self.head_dim)
189
+
190
+ if attention_mask is not None:
191
+ if q_len == 1: # inference with cache
192
+ if len(attention_mask.size()) == 4:
193
+ attention_mask = attention_mask[:, :, -1:, :]
194
+ else:
195
+ attention_mask = attention_mask[:, -1:, :]
196
+ attn_weights = attn_weights + attention_mask
197
+ attn_weights = torch.max(
198
+ attn_weights, torch.tensor(torch.finfo(attn_weights.dtype).min)
199
+ )
200
+
201
+ attn_weights = torch.nn.functional.softmax(attn_weights, dim=-1)
202
+ attn_output = torch.matmul(attn_weights, value_states)
203
+
204
+ attn_output = attn_output.transpose(1, 2)
205
+ attn_output = attn_output.reshape(bsz, q_len, self.hidden_size)
206
+ attn_output = self.o_proj(attn_output)
207
+
208
+ if not output_attentions:
209
+ attn_weights = None
210
+
211
+ return attn_output, attn_weights, past_key_value
212
+
213
+
214
+ class BaichuanLayer(torch.nn.Module):
215
+ def __init__(self, config: BaichuanConfig):
216
+ super().__init__()
217
+ self.hidden_size = config.hidden_size
218
+ self.self_attn = BaichuanAttention(config=config)
219
+ self.mlp = MLP(
220
+ hidden_size=self.hidden_size,
221
+ intermediate_size=config.intermediate_size,
222
+ hidden_act=config.hidden_act,
223
+ )
224
+ self.input_layernorm = RMSNorm(config.hidden_size, epsilon=config.rms_norm_eps)
225
+ self.post_attention_layernorm = RMSNorm(
226
+ config.hidden_size, epsilon=config.rms_norm_eps
227
+ )
228
+
229
+ def forward(
230
+ self,
231
+ hidden_states: torch.Tensor,
232
+ attention_mask: Optional[torch.Tensor] = None,
233
+ past_key_value: Optional[Tuple[torch.Tensor]] = None,
234
+ output_attentions: Optional[bool] = False,
235
+ use_cache: Optional[bool] = False,
236
+ ) -> Tuple[
237
+ torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]
238
+ ]:
239
+ residual = hidden_states
240
+
241
+ hidden_states = self.input_layernorm(hidden_states)
242
+
243
+ # Self Attention
244
+ hidden_states, self_attn_weights, present_key_value = self.self_attn(
245
+ hidden_states=hidden_states,
246
+ attention_mask=attention_mask,
247
+ past_key_value=past_key_value,
248
+ output_attentions=output_attentions,
249
+ use_cache=use_cache,
250
+ )
251
+ hidden_states = residual + hidden_states
252
+
253
+ # Fully Connected
254
+ residual = hidden_states
255
+ hidden_states = self.post_attention_layernorm(hidden_states)
256
+ hidden_states = self.mlp(hidden_states)
257
+ hidden_states = residual + hidden_states
258
+
259
+ outputs = (hidden_states,)
260
+
261
+ if use_cache:
262
+ outputs += (present_key_value,)
263
+
264
+ return outputs
265
+
266
+
267
+ class BaichuanPreTrainedModel(PreTrainedModel):
268
+ config_class = BaichuanConfig
269
+ base_model_prefix = "model"
270
+ supports_gradient_checkpointing = True
271
+ _no_split_modules = ["BaichuanLayer"]
272
+ _keys_to_ignore_on_load_unexpected = [r"decoder\.version"]
273
+
274
+ def _init_weights(self, module):
275
+ std = self.config.initializer_range
276
+ if isinstance(module, torch.nn.Linear):
277
+ module.weight.data.normal_(mean=0.0, std=std)
278
+ if module.bias is not None:
279
+ module.bias.data.zero_()
280
+ elif isinstance(module, torch.nn.Embedding):
281
+ module.weight.data.normal_(mean=0.0, std=std)
282
+ if module.padding_idx is not None:
283
+ module.weight.data[module.padding_idx].zero_()
284
+
285
+ def _set_gradient_checkpointing(self, gradient_checkpointing_func, enable=False):
286
+ if isinstance(gradient_checkpointing_func, BaichuanModel):
287
+ gradient_checkpointing_func.gradient_checkpointing = enable
288
+ # def _set_gradient_checkpointing(self, module, value=False):
289
+ # if isinstance(module, BaichuanModel):
290
+ # module.gradient_checkpointing = value
291
+
292
+
293
+ class BaichuanModel(BaichuanPreTrainedModel):
294
+ def __init__(self, config: BaichuanConfig):
295
+ super().__init__(config)
296
+ self.padding_idx = config.pad_token_id
297
+ self.vocab_size = config.vocab_size
298
+ self.n_head = config.num_attention_heads
299
+ self.embed_tokens = torch.nn.Embedding(
300
+ config.vocab_size, config.hidden_size, self.padding_idx
301
+ )
302
+ self.layers = torch.nn.ModuleList(
303
+ [BaichuanLayer(config) for _ in range(config.num_hidden_layers)]
304
+ )
305
+ self.norm = RMSNorm(config.hidden_size, epsilon=config.rms_norm_eps)
306
+
307
+ self.gradient_checkpointing = config.gradient_checkpointing
308
+ self.post_init()
309
+ self.max_cache_pos = config.model_max_length
310
+ self.first_run = True
311
+ self.alibi_mask = None
312
+
313
+ def get_input_embeddings(self):
314
+ return self.embed_tokens
315
+
316
+ def set_input_embeddings(self, value):
317
+ self.embed_tokens = value
318
+
319
+ def get_alibi_mask(self, tensor, seq_length_with_past):
320
+ if self.training:
321
+ slopes = torch.Tensor(_get_interleave(self.n_head))
322
+ position_point = (
323
+ torch.arange(seq_length_with_past) - seq_length_with_past + 1
324
+ )
325
+ position_point = (
326
+ position_point.unsqueeze(0)
327
+ .unsqueeze(0)
328
+ .expand(self.n_head, seq_length_with_past, -1)
329
+ )
330
+ diag = torch.diag(position_point[0])
331
+ position_point = position_point - diag.unsqueeze(0).unsqueeze(0).transpose(
332
+ -1, -2
333
+ )
334
+ alibi = slopes.unsqueeze(1).unsqueeze(1) * position_point
335
+ mask = _buffered_future_mask(
336
+ tensor, seq_length_with_past, alibi, self.n_head
337
+ )
338
+ else:
339
+ if self.first_run:
340
+ self.first_run = False
341
+ self.register_buffer(
342
+ "future_mask",
343
+ _gen_alibi_mask(tensor, self.n_head, self.max_cache_pos).to(
344
+ tensor
345
+ ),
346
+ persistent=False,
347
+ )
348
+ if seq_length_with_past > self.max_cache_pos:
349
+ self.max_cache_pos = seq_length_with_past
350
+ self.register_buffer(
351
+ "future_mask",
352
+ _gen_alibi_mask(tensor, self.n_head, self.max_cache_pos).to(
353
+ tensor
354
+ ),
355
+ persistent=False,
356
+ )
357
+ mask = self.future_mask[
358
+ : self.n_head, :seq_length_with_past, :seq_length_with_past
359
+ ]
360
+ return mask
361
+
362
+ def forward(
363
+ self,
364
+ input_ids: torch.LongTensor = None,
365
+ attention_mask: Optional[torch.Tensor] = None,
366
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
367
+ inputs_embeds: Optional[torch.FloatTensor] = None,
368
+ use_cache: Optional[bool] = False,
369
+ output_attentions: Optional[bool] = False,
370
+ output_hidden_states: Optional[bool] = False,
371
+ return_dict: Optional[bool] = True,
372
+ ) -> Union[Tuple, BaseModelOutputWithPast]:
373
+ if input_ids is not None and inputs_embeds is not None:
374
+ raise ValueError(
375
+ "You cannot provide both input_ids and inputs_embeds simultaneously"
376
+ )
377
+ elif input_ids is not None:
378
+ batch_size, seq_length = input_ids.shape
379
+ elif inputs_embeds is not None:
380
+ batch_size, seq_length, _ = inputs_embeds.shape
381
+ else:
382
+ raise ValueError("You need to provide input_ids or inputs_embeds")
383
+
384
+ return_dict = (
385
+ return_dict if return_dict is not None else self.config.use_return_dict
386
+ )
387
+
388
+ seq_length_with_past = seq_length
389
+
390
+ if past_key_values is not None:
391
+ past_key_values_length = past_key_values[0][0].shape[2]
392
+ seq_length_with_past = seq_length_with_past + past_key_values_length
393
+
394
+ if inputs_embeds is None:
395
+ inputs_embeds = self.embed_tokens(input_ids)
396
+
397
+ if self.training:
398
+ if (
399
+ self.alibi_mask is None
400
+ or self.alibi_mask.shape[-1] != seq_length_with_past
401
+ ):
402
+ self.alibi_mask = self.get_alibi_mask(
403
+ inputs_embeds, seq_length_with_past
404
+ )
405
+ alibi_mask = self.alibi_mask
406
+ else:
407
+ alibi_mask = self.get_alibi_mask(inputs_embeds, seq_length_with_past)
408
+
409
+ if attention_mask is not None:
410
+ if len(attention_mask.shape) == 2:
411
+ expanded_mask = attention_mask.to(alibi_mask.dtype)
412
+ expanded_mask = torch.tril(
413
+ torch.gt(expanded_mask[:, :, None] * expanded_mask[:, None, :], 0)
414
+ ) * torch.eq(expanded_mask[:, :, None] - expanded_mask[:, None, :], 0)
415
+ else:
416
+ expanded_mask = attention_mask
417
+ bsz = inputs_embeds.size(0)
418
+ src_len, tgt_len = alibi_mask.size()[-2:]
419
+ expanded_mask = (
420
+ expanded_mask.unsqueeze(1)
421
+ .expand(bsz, 1, src_len, tgt_len)
422
+ .to(alibi_mask.dtype)
423
+ )
424
+ inverted_mask = 1.0 - expanded_mask
425
+ inverted_mask = inverted_mask.masked_fill(
426
+ inverted_mask.to(torch.bool), torch.finfo(alibi_mask.dtype).min
427
+ )
428
+ attention_mask = inverted_mask + alibi_mask.unsqueeze(0)
429
+ else:
430
+ attention_mask = alibi_mask
431
+
432
+ hidden_states = inputs_embeds
433
+
434
+ if self.gradient_checkpointing and self.training:
435
+ if use_cache:
436
+ logger.warning_once(
437
+ "`use_cache=True` is incompatible with gradient checkpointing. Setting `use_cache=False`..."
438
+ )
439
+ use_cache = False
440
+
441
+ # decoder layers
442
+ all_hidden_states = () if output_hidden_states else None
443
+ all_self_attns = () if output_attentions else None
444
+ next_decoder_cache = () if use_cache else None
445
+
446
+ for idx, decoder_layer in enumerate(self.layers):
447
+ if output_hidden_states:
448
+ all_hidden_states += (hidden_states,)
449
+
450
+ past_key_value = (
451
+ past_key_values[idx] if past_key_values is not None else None
452
+ )
453
+
454
+ if self.gradient_checkpointing and self.training:
455
+
456
+ def create_custom_forward(module):
457
+ def custom_forward(*inputs):
458
+ # None for past_key_value
459
+ return module(*inputs, output_attentions, None)
460
+
461
+ return custom_forward
462
+
463
+ layer_outputs = torch.utils.checkpoint.checkpoint(
464
+ create_custom_forward(decoder_layer),
465
+ hidden_states,
466
+ attention_mask,
467
+ None,
468
+ )
469
+ else:
470
+ layer_outputs = decoder_layer(
471
+ hidden_states,
472
+ attention_mask=attention_mask,
473
+ past_key_value=past_key_value,
474
+ output_attentions=output_attentions,
475
+ use_cache=use_cache,
476
+ )
477
+
478
+ hidden_states = layer_outputs[0]
479
+
480
+ if use_cache:
481
+ next_decoder_cache += (layer_outputs[2 if output_attentions else 1],)
482
+
483
+ if output_attentions:
484
+ all_self_attns += (layer_outputs[1],)
485
+
486
+ hidden_states = self.norm(hidden_states)
487
+
488
+ # add hidden states from the last decoder layer
489
+ if output_hidden_states:
490
+ all_hidden_states += (hidden_states,)
491
+
492
+ next_cache = next_decoder_cache if use_cache else None
493
+ if not return_dict:
494
+ return tuple(
495
+ v
496
+ for v in [hidden_states, next_cache, all_hidden_states, all_self_attns]
497
+ if v is not None
498
+ )
499
+ return BaseModelOutputWithPast(
500
+ last_hidden_state=hidden_states,
501
+ past_key_values=next_cache,
502
+ hidden_states=all_hidden_states,
503
+ attentions=all_self_attns,
504
+ )
505
+
506
+
507
+ class NormHead(nn.Module):
508
+ def __init__(self, hidden_size, vocab_size, bias=False):
509
+ super().__init__()
510
+ self.weight = nn.Parameter(torch.empty((vocab_size, hidden_size)))
511
+ nn.init.kaiming_uniform_(self.weight, a=math.sqrt(5))
512
+ self.first_flag = True
513
+
514
+ def forward(self, hidden_states):
515
+ if self.training:
516
+ norm_weight = nn.functional.normalize(self.weight)
517
+ self.first_flag = True
518
+ elif self.first_flag:
519
+ self.first_flag = False
520
+ self.weight.data = nn.functional.normalize(self.weight)
521
+ norm_weight = self.weight
522
+ else:
523
+ norm_weight = self.weight
524
+ return nn.functional.linear(hidden_states, norm_weight)
525
+
526
+ _init_weights = True
527
+ @contextmanager
528
+ def no_init_weights(_enable=True):
529
+ global _init_weights
530
+ old_init_weights = _init_weights
531
+ if _enable:
532
+ _init_weights = False
533
+ try:
534
+ yield
535
+ finally:
536
+ _init_weights = old_init_weights
537
+
538
+
539
+ class BaichuanForCausalLM(BaichuanPreTrainedModel):
540
+ def __init__(self, config, *model_args, **model_kwargs):
541
+ super().__init__(config, *model_args, **model_kwargs)
542
+ self.model = BaichuanModel(config)
543
+ self.lm_head = NormHead(config.hidden_size, config.vocab_size, bias=False)
544
+ #if hasattr(config, "quantization_config") and config.quantization_config['load_in_4bit']:
545
+ if hasattr(config, "quantization_config") and isinstance(config.quantization_config, dict) and config.quantization_config.get('load_in_4bit', False):
546
+ try:
547
+ from .quantizer import quantize_offline, init_model_weight_int4
548
+ except ImportError:
549
+ raise ImportError(f"Needs quantize_offline to run quantize.")
550
+ quantize_offline(self, 4)
551
+ # Initialize weights and apply final processing
552
+ self.post_init()
553
+
554
+ def get_input_embeddings(self):
555
+ return self.model.embed_tokens
556
+
557
+ def set_input_embeddings(self, value):
558
+ self.model.embed_tokens = value
559
+
560
+ def get_output_embeddings(self):
561
+ return self.lm_head
562
+
563
+ def set_output_embeddings(self, new_embeddings):
564
+ self.lm_head = new_embeddings
565
+
566
+ def set_decoder(self, decoder):
567
+ self.model = decoder
568
+
569
+ def get_decoder(self):
570
+ return self.model
571
+
572
+ @classmethod
573
+ def from_pretrained(
574
+ cls,
575
+ pretrained_model_name_or_path: Optional[Union[str, os.PathLike]],
576
+ *model_args,
577
+ config: Optional[Union[PretrainedConfig, str, os.PathLike]] = None,
578
+ cache_dir: Optional[Union[str, os.PathLike]] = None,
579
+ ignore_mismatched_sizes: bool = False,
580
+ force_download: bool = False,
581
+ local_files_only: bool = False,
582
+ token: Optional[Union[str, bool]] = None,
583
+ revision: str = "main",
584
+ use_safetensors: bool = None,
585
+ **kwargs,
586
+ ):
587
+
588
+ # Load config if we don't provide a configuration
589
+ if not isinstance(config, PretrainedConfig):
590
+ config_path = config if config is not None else pretrained_model_name_or_path
591
+ config, model_kwargs = cls.config_class.from_pretrained(
592
+ config_path,
593
+ cache_dir=cache_dir,
594
+ return_unused_kwargs=True,
595
+ force_download=force_download,
596
+ resume_download=False,
597
+ proxies=None,
598
+ local_files_only=local_files_only,
599
+ token=token,
600
+ revision=revision,
601
+ subfolder="",
602
+ _from_auto=False,
603
+ _from_pipeline=None,
604
+ **kwargs,
605
+ )
606
+ else:
607
+ model_kwargs = kwargs
608
+
609
+ if hasattr(config, "quantization_config") and config.quantization_config['load_in_4bit']:
610
+ try:
611
+ from .quantizer import init_model_weight_int4
612
+ from accelerate import init_empty_weights, dispatch_model, infer_auto_device_map
613
+ from accelerate.utils import CustomDtype
614
+ from accelerate.utils import get_balanced_memory
615
+ except ImportError:
616
+ raise ImportError(f"Needs import model weight init func to run quantize.")
617
+ # Instantiate model.
618
+ init_contexts = [no_init_weights(_enable=True)]
619
+ init_contexts.append(init_empty_weights())
620
+ with ContextManagers(init_contexts):
621
+ model = cls(config)
622
+
623
+ model_file = os.path.join(pretrained_model_name_or_path, 'pytorch_model.bin')
624
+ state_dict = torch.load(model_file, map_location="cpu")
625
+ model.is_quantized = True
626
+
627
+ device_map = kwargs.pop("device_map", None)
628
+ torch_dtype = kwargs.pop("torch_dtype", None)
629
+ if device_map is not None:
630
+ kwargs = {"no_split_module_classes": model._no_split_modules}
631
+ target_dtype = CustomDtype.INT4
632
+ max_memory = get_balanced_memory(
633
+ model,
634
+ dtype=target_dtype,
635
+ low_zero=(device_map == "balanced_low_0"),
636
+ max_memory=None,
637
+ **kwargs,
638
+ )
639
+ kwargs["max_memory"] = max_memory
640
+ device_map = infer_auto_device_map(model, dtype=target_dtype, **kwargs)
641
+ model = init_model_weight_int4(config, model, state_dict)
642
+
643
+ # Set model in evaluation mode to deactivate DropOut modules by default
644
+ model.eval()
645
+ # If it is a model with generation capabilities, attempt to load the generation config
646
+ if model.can_generate():
647
+ try:
648
+ model.generation_config = GenerationConfig.from_pretrained(
649
+ pretrained_model_name_or_path,
650
+ cache_dir=cache_dir,
651
+ force_download=force_download,
652
+ resume_download=False,
653
+ proxies=None,
654
+ local_files_only=local_files_only,
655
+ token=token,
656
+ revision=revision,
657
+ subfolder="",
658
+ _from_auto=False,
659
+ _from_pipeline=None,
660
+ **kwargs,
661
+ )
662
+ except (OSError, TypeError):
663
+ logger.info(
664
+ "Generation config file not found, using a generation config created from the model config."
665
+ )
666
+ pass
667
+
668
+ if device_map is not None:
669
+ dispatch_model(model, device_map=device_map)
670
+
671
+ return model
672
+
673
+ return super(BaichuanForCausalLM, cls).from_pretrained(pretrained_model_name_or_path, *model_args,
674
+ config=config, cache_dir=cache_dir, ignore_mismatched_sizes=ignore_mismatched_sizes,
675
+ force_download=force_download, local_files_only=local_files_only, token=token, revision=revision,
676
+ use_safetensors=use_safetensors, **kwargs)
677
+
678
+ def forward(
679
+ self,
680
+ input_ids: torch.LongTensor = None,
681
+ attention_mask: Optional[torch.Tensor] = None,
682
+ past_key_values: Optional[List[torch.FloatTensor]] = None,
683
+ inputs_embeds: Optional[torch.FloatTensor] = None,
684
+ labels: Optional[torch.LongTensor] = None,
685
+ use_cache: Optional[bool] = None,
686
+ output_attentions: Optional[bool] = False,
687
+ output_hidden_states: Optional[bool] = False,
688
+ return_dict: Optional[bool] = True,
689
+ **kwargs,
690
+ ) -> Union[Tuple, CausalLMOutputWithPast]:
691
+ return_dict = (
692
+ return_dict if return_dict is not None else self.config.use_return_dict
693
+ )
694
+
695
+ # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn)
696
+ outputs = self.model(
697
+ input_ids=input_ids,
698
+ attention_mask=attention_mask,
699
+ past_key_values=past_key_values,
700
+ inputs_embeds=inputs_embeds,
701
+ use_cache=use_cache,
702
+ output_attentions=output_attentions,
703
+ output_hidden_states=output_hidden_states,
704
+ return_dict=return_dict,
705
+ )
706
+
707
+ hidden_states = outputs[0]
708
+ logits = self.lm_head(hidden_states)
709
+ loss = None
710
+ if labels is not None:
711
+ # Shift so that tokens < n predict n
712
+ shift_logits = logits[..., :-1, :].contiguous()
713
+ shift_labels = labels[..., 1:].contiguous()
714
+ # Flatten the tokens
715
+ loss_fct = CrossEntropyLoss()
716
+ shift_logits = shift_logits.view(-1, self.config.vocab_size)
717
+ shift_labels = shift_labels.view(-1)
718
+ softmax_normalizer = shift_logits.max(-1).values ** 2
719
+ z_loss = self.config.z_loss_weight * softmax_normalizer.mean()
720
+ # Enable model parallelism
721
+ shift_labels = shift_labels.to(shift_logits.device)
722
+ loss = loss_fct(shift_logits, shift_labels) + z_loss
723
+
724
+ if not return_dict:
725
+ output = (logits,) + outputs[1:]
726
+ return (loss,) + output if loss is not None else output
727
+
728
+ return CausalLMOutputWithPast(
729
+ loss=loss,
730
+ logits=logits,
731
+ past_key_values=outputs.past_key_values,
732
+ hidden_states=outputs.hidden_states,
733
+ attentions=outputs.attentions,
734
+ )
735
+
736
+ def quantize(self, bits: int):
737
+ try:
738
+ from .quantizer import quantize_online
739
+ except ImportError:
740
+ raise ImportError(f"Needs QLinear to run quantize.")
741
+ return quantize_online(self, bits)
742
+
743
+ def prepare_inputs_for_generation(
744
+ self,
745
+ input_ids: torch.LongTensor,
746
+ past_key_values: Optional[torch.Tensor] = None,
747
+ attention_mask: Optional[torch.Tensor] = None,
748
+ inputs_embeds: Optional[torch.Tensor] = None,
749
+ **kwargs,
750
+ ):
751
+ if past_key_values:
752
+ input_ids = input_ids[:, -1:]
753
+
754
+ # if `inputs_embeds` are passed, we only want to use them in the 1st generation step
755
+ if inputs_embeds is not None and past_key_values is None:
756
+ model_inputs = {"inputs_embeds": inputs_embeds}
757
+ else:
758
+ model_inputs = {"input_ids": input_ids}
759
+
760
+ model_inputs.update(
761
+ {
762
+ "past_key_values": past_key_values,
763
+ "use_cache": kwargs.get("use_cache"),
764
+ "attention_mask": attention_mask,
765
+ }
766
+ )
767
+ return model_inputs
768
+
769
+ @staticmethod
770
+ def _reorder_cache(past_key_values, beam_idx):
771
+ return tuple(
772
+ tuple(past_state.index_select(0, beam_idx) for past_state in layer_past)
773
+ for layer_past in past_key_values
774
+ )
775
+
776
+ def _build_chat_input(
777
+ self, tokenizer, messages: List[dict], max_new_tokens: int = 0
778
+ ):
779
+ max_new_tokens = max_new_tokens or self.generation_config.max_new_tokens
780
+ max_input_tokens = self.config.model_max_length - max_new_tokens
781
+ max_input_tokens = max(self.config.model_max_length // 2, max_input_tokens)
782
+ total_input, round_input = [], []
783
+ for i, message in enumerate(messages[::-1]):
784
+ content_tokens = tokenizer.encode(message["content"])
785
+ if message["role"] == "user":
786
+ round_input = (
787
+ [self.generation_config.user_token_id]
788
+ + content_tokens
789
+ + round_input
790
+ )
791
+ if (
792
+ total_input
793
+ and len(total_input) + len(round_input) > max_input_tokens
794
+ ):
795
+ break
796
+ else:
797
+ total_input = round_input + total_input
798
+ if len(total_input) >= max_input_tokens:
799
+ break
800
+ else:
801
+ round_input = []
802
+ elif message["role"] == "assistant":
803
+ round_input = (
804
+ [self.generation_config.assistant_token_id]
805
+ + content_tokens
806
+ + [self.generation_config.eos_token_id]
807
+ + round_input
808
+ )
809
+ else:
810
+ raise ValueError(f"message role not supported yet: {message['role']}")
811
+ total_input = total_input[-max_input_tokens:] # truncate left
812
+ total_input.append(self.generation_config.assistant_token_id)
813
+ total_input = torch.LongTensor([total_input]).to(self.device)
814
+ return total_input
815
+
816
+ def chat(self, tokenizer, messages: List[dict], stream=False,
817
+ generation_config: Optional[GenerationConfig]=None):
818
+ generation_config = generation_config or self.generation_config
819
+ input_ids = build_chat_input(self, tokenizer, messages, generation_config.max_new_tokens)
820
+ if stream:
821
+ streamer = TextIterStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
822
+ Thread(target=self.generate, kwargs=dict(
823
+ inputs=input_ids, streamer=streamer,
824
+ generation_config=generation_config,
825
+ )).start()
826
+ return streamer
827
+ else:
828
+ outputs = self.generate(input_ids, generation_config=generation_config)
829
+ response = tokenizer.decode(outputs[0][len(input_ids[0]):], skip_special_tokens=True)
830
+ return response
quant_config.json ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ {
2
+ "zero_point": true,
3
+ "q_group_size": 128,
4
+ "w_bit": 4,
5
+ "version": "GEMM"
6
+ }
quantizer.py ADDED
@@ -0,0 +1,211 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import bitsandbytes as bnb
2
+ from accelerate import init_empty_weights
3
+ from bitsandbytes.nn.modules import Params4bit, Int8Params
4
+ import torch
5
+
6
+ def Params4bitCuda(self, device):
7
+ self.data = self.data.cuda(device)
8
+ self.quant_state[0] = self.quant_state[0].cuda(device)
9
+ self.quant_state[4][0] = self.quant_state[4][0].cuda(device)
10
+ self.quant_state[4][1][0] = self.quant_state[4][1][0].cuda(device)
11
+ self.quant_state[4][1][1] = self.quant_state[4][1][1].cuda(device)
12
+
13
+ self.quant_state[6] = self.quant_state[6].cuda(device)
14
+ return self
15
+
16
+ class Linear4bitOnline(torch.nn.Module):
17
+ def __init__(self, weight, bias, quant_type):
18
+ super().__init__()
19
+ self.weight = Params4bit(
20
+ weight.data, requires_grad=False, compress_statistics=True, quant_type=quant_type
21
+ )
22
+ self.compute_dtype = None
23
+ #self.weight.cuda(weight.device)
24
+ self.bias = bias
25
+
26
+ def forward(self, x: torch.Tensor):
27
+ # weights are cast automatically as Int8Params, but the bias has to be cast manually
28
+ if self.bias is not None and self.bias.dtype != x.dtype:
29
+ self.bias.data = self.bias.data.to(x.dtype)
30
+
31
+ if getattr(self.weight, "quant_state", None) is None:
32
+ print(
33
+ "FP4 quantization state not initialized. Please call .cuda() or .to(device) on the LinearFP4 layer first."
34
+ )
35
+ inp_dtype = x.dtype
36
+ if self.compute_dtype is not None:
37
+ x = x.to(self.compute_dtype)
38
+
39
+ bias = None if self.bias is None else self.bias.to(self.compute_dtype)
40
+ out = bnb.matmul_4bit(
41
+ x, self.weight.t(), bias=bias, quant_state=self.weight.quant_state
42
+ )
43
+
44
+ out = out.to(inp_dtype)
45
+
46
+ return out
47
+
48
+ class Linear8bitLtOnline(torch.nn.Module):
49
+ def __init__(
50
+ self,
51
+ weight,
52
+ bias,
53
+ has_fp16_weights=True,
54
+ memory_efficient_backward=False,
55
+ threshold=0.0,
56
+ index=None,
57
+ ):
58
+ super().__init__()
59
+ assert (
60
+ not memory_efficient_backward
61
+ ), "memory_efficient_backward is no longer required and the argument is deprecated in 0.37.0 and will be removed in 0.39.0"
62
+ self.state = bnb.MatmulLtState()
63
+ self.index = index
64
+
65
+ # Necessary for stacked layers
66
+ self.state.threshold = threshold
67
+ self.state.has_fp16_weights = has_fp16_weights
68
+ self.state.memory_efficient_backward = memory_efficient_backward
69
+ if threshold > 0.0 and not has_fp16_weights:
70
+ self.state.use_pool = True
71
+
72
+ self.weight = Int8Params(
73
+ weight.data,
74
+ has_fp16_weights=has_fp16_weights,
75
+ requires_grad=has_fp16_weights,
76
+ )
77
+ self.bias = bias
78
+
79
+ def init_8bit_state(self):
80
+ self.state.CB = self.weight.CB
81
+ self.state.SCB = self.weight.SCB
82
+ self.weight.CB = None
83
+ self.weight.SCB = None
84
+
85
+ def forward(self, x: torch.Tensor):
86
+ self.state.is_training = self.training
87
+ if self.weight.CB is not None:
88
+ self.init_8bit_state()
89
+
90
+ # weights are cast automatically as Int8Params, but the bias has to be cast manually
91
+ if self.bias is not None and self.bias.dtype != x.dtype:
92
+ self.bias.data = self.bias.data.to(x.dtype)
93
+
94
+ out = bnb.matmul(x, self.weight, bias=self.bias, state=self.state)
95
+
96
+ if not self.state.has_fp16_weights:
97
+ if self.state.CB is not None and self.state.CxB is not None:
98
+ # we converted 8-bit row major to turing/ampere format in the first inference pass
99
+ # we no longer need the row-major weight
100
+ del self.state.CB
101
+ self.weight.data = self.state.CxB
102
+ return out
103
+
104
+ def quantize_offline(model, bits: int):
105
+ assert (bits == 4), f'bits: {bits} is not supported'
106
+
107
+ for i, layer in enumerate(model.model.layers):
108
+ layer.self_attn.W_pack = bnb.nn.Linear4bit(
109
+ layer.self_attn.W_pack.weight.shape[1],
110
+ layer.self_attn.W_pack.weight.shape[0],
111
+ False,
112
+ torch.float16,
113
+ compress_statistics=True,
114
+ quant_type="nf4",
115
+ )
116
+ layer.self_attn.o_proj = bnb.nn.Linear4bit(
117
+ layer.self_attn.o_proj.weight.shape[1],
118
+ layer.self_attn.o_proj.weight.shape[0],
119
+ False,
120
+ torch.float16,
121
+ compress_statistics=True,
122
+ quant_type="nf4",
123
+ )
124
+
125
+ layer.mlp.gate_proj = bnb.nn.Linear4bit(
126
+ layer.mlp.gate_proj.weight.shape[1],
127
+ layer.mlp.gate_proj.weight.shape[0],
128
+ False,
129
+ torch.float16,
130
+ compress_statistics=True,
131
+ quant_type="nf4",
132
+ )
133
+ layer.mlp.down_proj = bnb.nn.Linear4bit(
134
+ layer.mlp.down_proj.weight.shape[1],
135
+ layer.mlp.down_proj.weight.shape[0],
136
+ False,
137
+ torch.float16,
138
+ compress_statistics=True,
139
+ quant_type="nf4",
140
+ )
141
+ layer.mlp.up_proj = bnb.nn.Linear4bit(
142
+ layer.mlp.up_proj.weight.shape[1],
143
+ layer.mlp.up_proj.weight.shape[0],
144
+ False,
145
+ torch.float16,
146
+ compress_statistics=True,
147
+ quant_type="nf4",
148
+ )
149
+ return model
150
+
151
+ def quantize_online(model, bits: int):
152
+ def quant(weight, bias=None):
153
+ if bits == 8:
154
+ linear = Linear8bitLtOnline(
155
+ weight,
156
+ bias,
157
+ has_fp16_weights=False,
158
+ threshold=6.0,
159
+ )
160
+ if bias is not None:
161
+ linear.bias = torch.nn.Parameter(bias)
162
+ elif bits == 4:
163
+ linear = Linear4bitOnline(
164
+ weight,
165
+ bias,
166
+ quant_type="nf4", #fp4/nf4
167
+ )
168
+ else:
169
+ raise ValueError("quantize only support 4/8 bit")
170
+ return linear
171
+
172
+ for i, layer in enumerate(model.model.layers):
173
+ layer.self_attn.W_pack = quant(layer.self_attn.W_pack.weight)
174
+ layer.self_attn.o_proj = quant(layer.self_attn.o_proj.weight)
175
+ layer.mlp.gate_proj = quant(layer.mlp.gate_proj.weight)
176
+ layer.mlp.down_proj = quant(layer.mlp.down_proj.weight)
177
+ layer.mlp.up_proj = quant(layer.mlp.up_proj.weight)
178
+ return model
179
+
180
+ def init_model_weight_int4(config, model, state_dict):
181
+ #replace Params4bit.cuda with Params4bitCuda
182
+ Params4bit.cuda = Params4bitCuda
183
+
184
+ for i in range(config.num_hidden_layers):
185
+ weight_data = state_dict[f'model.layers.{i}.self_attn.W_pack.weight.data']
186
+ weight_quant_state = state_dict[f'model.layers.{i}.self_attn.W_pack.weight.quant_state']
187
+ model.model.layers[i].self_attn.W_pack.weight = Params4bit(weight_data, requires_grad=False, quant_state=weight_quant_state)
188
+
189
+ weight_data = state_dict[f'model.layers.{i}.self_attn.o_proj.weight.data']
190
+ weight_quant_state = state_dict[f'model.layers.{i}.self_attn.o_proj.weight.quant_state']
191
+ model.model.layers[i].self_attn.o_proj.weight = Params4bit(weight_data, requires_grad=False, quant_state=weight_quant_state)
192
+
193
+ weight_data = state_dict[f'model.layers.{i}.mlp.gate_proj.weight.data']
194
+ weight_quant_state = state_dict[f'model.layers.{i}.mlp.gate_proj.weight.quant_state']
195
+ model.model.layers[i].mlp.gate_proj.weight = Params4bit(weight_data, requires_grad=False, quant_state=weight_quant_state)
196
+
197
+ weight_data = state_dict[f'model.layers.{i}.mlp.up_proj.weight.data']
198
+ weight_quant_state = state_dict[f'model.layers.{i}.mlp.up_proj.weight.quant_state']
199
+ model.model.layers[i].mlp.up_proj.weight = Params4bit(weight_data, requires_grad=False, quant_state=weight_quant_state)
200
+
201
+ weight_data = state_dict[f'model.layers.{i}.mlp.down_proj.weight.data']
202
+ weight_quant_state = state_dict[f'model.layers.{i}.mlp.down_proj.weight.quant_state']
203
+ model.model.layers[i].mlp.down_proj.weight = Params4bit(weight_data, requires_grad=False, quant_state=weight_quant_state)
204
+
205
+ model.model.layers[i].input_layernorm.weight = state_dict[f'model.layers.{i}.input_layernorm.weight']
206
+ model.model.layers[i].post_attention_layernorm.weight = state_dict[f'model.layers.{i}.post_attention_layernorm.weight']
207
+
208
+ model.model.embed_tokens.weight = state_dict['model.embed_tokens.weight']
209
+ model.model.norm.weight = state_dict['model.norm.weight']
210
+ model.lm_head.weight = state_dict['lm_head.weight']
211
+ return model
special_tokens_map.json ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "bos_token": {
3
+ "content": "<s>",
4
+ "lstrip": false,
5
+ "normalized": true,
6
+ "rstrip": false,
7
+ "single_word": true
8
+ },
9
+ "eos_token": {
10
+ "content": "</s>",
11
+ "lstrip": false,
12
+ "normalized": true,
13
+ "rstrip": false,
14
+ "single_word": true
15
+ },
16
+ "pad_token": {
17
+ "content": "<unk>",
18
+ "lstrip": false,
19
+ "normalized": true,
20
+ "rstrip": false,
21
+ "single_word": true
22
+ },
23
+ "unk_token": {
24
+ "content": "<unk>",
25
+ "lstrip": false,
26
+ "normalized": true,
27
+ "rstrip": false,
28
+ "single_word": true
29
+ }
30
+ }
tokenization_baichuan.py ADDED
@@ -0,0 +1,258 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright (c) 2023, Baichuan Intelligent Technology. All rights reserved.
2
+
3
+ import os
4
+ from shutil import copyfile
5
+ from typing import Any, Dict, List, Optional, Tuple
6
+
7
+ import sentencepiece as spm
8
+ from transformers.tokenization_utils import AddedToken, PreTrainedTokenizer
9
+ from transformers.utils import logging
10
+
11
+
12
+ logger = logging.get_logger(__name__)
13
+
14
+ VOCAB_FILES_NAMES = {"vocab_file": "tokenizer.model"}
15
+
16
+ PRETRAINED_VOCAB_FILES_MAP = {
17
+ "vocab_file": {},
18
+ "tokenizer_file": {},
19
+ }
20
+ PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES = {}
21
+
22
+
23
+ class BaichuanTokenizer(PreTrainedTokenizer):
24
+ """
25
+ Construct a Baichuan tokenizer. Based on byte-level Byte-Pair-Encoding.
26
+
27
+ Args:
28
+ vocab_file (`str`):
29
+ Path to the vocabulary file.
30
+ """
31
+
32
+ vocab_files_names = VOCAB_FILES_NAMES
33
+ pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP
34
+ max_model_input_sizes = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
35
+ model_input_names = ["input_ids", "attention_mask"]
36
+
37
+ def __init__(
38
+ self,
39
+ vocab_file,
40
+ unk_token="<unk>",
41
+ bos_token="<s>",
42
+ eos_token="</s>",
43
+ pad_token=None,
44
+ sp_model_kwargs: Optional[Dict[str, Any]] = None,
45
+ add_bos_token=True,
46
+ add_eos_token=False,
47
+ clean_up_tokenization_spaces=False,
48
+ **kwargs,
49
+ ):
50
+ self.sp_model_kwargs = {} if sp_model_kwargs is None else sp_model_kwargs
51
+ bos_token = (
52
+ AddedToken(bos_token, lstrip=False, rstrip=False)
53
+ if isinstance(bos_token, str)
54
+ else bos_token
55
+ )
56
+ eos_token = (
57
+ AddedToken(eos_token, lstrip=False, rstrip=False)
58
+ if isinstance(eos_token, str)
59
+ else eos_token
60
+ )
61
+ unk_token = (
62
+ AddedToken(unk_token, lstrip=False, rstrip=False)
63
+ if isinstance(unk_token, str)
64
+ else unk_token
65
+ )
66
+ pad_token = (
67
+ AddedToken(pad_token, lstrip=False, rstrip=False)
68
+ if isinstance(pad_token, str)
69
+ else pad_token
70
+ )
71
+ self.vocab_file = vocab_file
72
+ self.add_bos_token = add_bos_token
73
+ self.add_eos_token = add_eos_token
74
+ self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs)
75
+ self.sp_model.Load(vocab_file)
76
+ super().__init__(
77
+ bos_token=bos_token,
78
+ eos_token=eos_token,
79
+ unk_token=unk_token,
80
+ pad_token=pad_token,
81
+ add_bos_token=add_bos_token,
82
+ add_eos_token=add_eos_token,
83
+ sp_model_kwargs=self.sp_model_kwargs,
84
+ clean_up_tokenization_spaces=clean_up_tokenization_spaces,
85
+ **kwargs,
86
+ )
87
+
88
+ def __getstate__(self):
89
+ state = self.__dict__.copy()
90
+ state["sp_model"] = None
91
+ return state
92
+
93
+ def __setstate__(self, d):
94
+ self.__dict__ = d
95
+ self.sp_model = spm.SentencePieceProcessor(**self.sp_model_kwargs)
96
+ self.sp_model.Load(self.vocab_file)
97
+
98
+ @property
99
+ def vocab_size(self):
100
+ """Returns vocab size"""
101
+ return self.sp_model.get_piece_size()
102
+
103
+ def get_vocab(self):
104
+ """Returns vocab as a dict"""
105
+ vocab = {self.convert_ids_to_tokens(i): i for i in range(self.vocab_size)}
106
+ vocab.update(self.added_tokens_encoder)
107
+ return vocab
108
+
109
+ def _tokenize(self, text):
110
+ """Returns a tokenized string."""
111
+ return self.sp_model.encode(text, out_type=str)
112
+
113
+ def _convert_token_to_id(self, token):
114
+ """Converts a token (str) in an id using the vocab."""
115
+ return self.sp_model.piece_to_id(token)
116
+
117
+ def _convert_id_to_token(self, index):
118
+ """Converts an index (integer) in a token (str) using the vocab."""
119
+ token = self.sp_model.IdToPiece(index)
120
+ return token
121
+
122
+ def convert_tokens_to_string(self, tokens):
123
+ """Converts a sequence of tokens (string) in a single string."""
124
+ current_sub_tokens = []
125
+ out_string = ""
126
+ prev_is_special = False
127
+ for i, token in enumerate(tokens):
128
+ # make sure that special tokens are not decoded using sentencepiece model
129
+ if token in self.all_special_tokens:
130
+ if not prev_is_special and i != 0:
131
+ out_string += " "
132
+ out_string += self.sp_model.decode(current_sub_tokens) + token
133
+ prev_is_special = True
134
+ current_sub_tokens = []
135
+ else:
136
+ current_sub_tokens.append(token)
137
+ prev_is_special = False
138
+ out_string += self.sp_model.decode(current_sub_tokens)
139
+ return out_string
140
+
141
+ def save_vocabulary(
142
+ self, save_directory, filename_prefix: Optional[str] = None
143
+ ) -> Tuple[str]:
144
+ """
145
+ Save the vocabulary and special tokens file to a directory.
146
+
147
+ Args:
148
+ save_directory (`str`):
149
+ The directory in which to save the vocabulary.
150
+
151
+ Returns:
152
+ `Tuple(str)`: Paths to the files saved.
153
+ """
154
+ if not os.path.isdir(save_directory):
155
+ logger.error(f"Vocabulary path ({save_directory}) should be a directory")
156
+ return
157
+ out_vocab_file = os.path.join(
158
+ save_directory,
159
+ (filename_prefix + "-" if filename_prefix else "")
160
+ + VOCAB_FILES_NAMES["vocab_file"],
161
+ )
162
+
163
+ if os.path.abspath(self.vocab_file) != os.path.abspath(
164
+ out_vocab_file
165
+ ) and os.path.isfile(self.vocab_file):
166
+ copyfile(self.vocab_file, out_vocab_file)
167
+ elif not os.path.isfile(self.vocab_file):
168
+ with open(out_vocab_file, "wb") as fi:
169
+ content_spiece_model = self.sp_model.serialized_model_proto()
170
+ fi.write(content_spiece_model)
171
+
172
+ return (out_vocab_file,)
173
+
174
+ def build_inputs_with_special_tokens(self, token_ids_0, token_ids_1=None):
175
+ bos_token_id = [self.bos_token_id] if self.add_bos_token else []
176
+ eos_token_id = [self.eos_token_id] if self.add_eos_token else []
177
+
178
+ output = bos_token_id + token_ids_0 + eos_token_id
179
+
180
+ if token_ids_1 is not None:
181
+ output = output + bos_token_id + token_ids_1 + eos_token_id
182
+
183
+ return output
184
+
185
+ def get_special_tokens_mask(
186
+ self,
187
+ token_ids_0: List[int],
188
+ token_ids_1: Optional[List[int]] = None,
189
+ already_has_special_tokens: bool = False,
190
+ ) -> List[int]:
191
+ """
192
+ Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
193
+ special tokens using the tokenizer `prepare_for_model` method.
194
+
195
+ Args:
196
+ token_ids_0 (`List[int]`):
197
+ List of IDs.
198
+ token_ids_1 (`List[int]`, *optional*):
199
+ Optional second list of IDs for sequence pairs.
200
+ already_has_special_tokens (`bool`, *optional*, defaults to `False`):
201
+ Whether or not the token list is already formatted with special tokens for the model.
202
+
203
+ Returns:
204
+ `List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token.
205
+ """
206
+ if already_has_special_tokens:
207
+ return super().get_special_tokens_mask(
208
+ token_ids_0=token_ids_0,
209
+ token_ids_1=token_ids_1,
210
+ already_has_special_tokens=True,
211
+ )
212
+
213
+ bos_token_id = [1] if self.add_bos_token else []
214
+ eos_token_id = [1] if self.add_eos_token else []
215
+
216
+ if token_ids_1 is None:
217
+ return bos_token_id + ([0] * len(token_ids_0)) + eos_token_id
218
+ return (
219
+ bos_token_id
220
+ + ([0] * len(token_ids_0))
221
+ + eos_token_id
222
+ + bos_token_id
223
+ + ([0] * len(token_ids_1))
224
+ + eos_token_id
225
+ )
226
+
227
+ def create_token_type_ids_from_sequences(
228
+ self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
229
+ ) -> List[int]:
230
+ """
231
+ Creates a mask from the two sequences passed to be used in a sequence-pair classification task. An ALBERT
232
+ sequence pair mask has the following format:
233
+
234
+ ```
235
+ 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
236
+ | first sequence | second sequence |
237
+ ```
238
+
239
+ if token_ids_1 is None, only returns the first portion of the mask (0s).
240
+
241
+ Args:
242
+ token_ids_0 (`List[int]`):
243
+ List of ids.
244
+ token_ids_1 (`List[int]`, *optional*):
245
+ Optional second list of IDs for sequence pairs.
246
+
247
+ Returns:
248
+ `List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given sequence(s).
249
+ """
250
+ bos_token_id = [self.bos_token_id] if self.add_bos_token else []
251
+ eos_token_id = [self.eos_token_id] if self.add_eos_token else []
252
+
253
+ output = [0] * len(bos_token_id + token_ids_0 + eos_token_id)
254
+
255
+ if token_ids_1 is not None:
256
+ output += [1] * len(bos_token_id + token_ids_1 + eos_token_id)
257
+
258
+ return output
tokenizer.model ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:79452955be6b419a65984273a9f08af86042e1c2a75ee3ba989cbf620a133cc2
3
+ size 2001107
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": true,
11
+ "special": true
12
+ },
13
+ "1": {
14
+ "content": "<s>",
15
+ "lstrip": false,
16
+ "normalized": true,
17
+ "rstrip": false,
18
+ "single_word": true,
19
+ "special": true
20
+ },
21
+ "2": {
22
+ "content": "</s>",
23
+ "lstrip": false,
24
+ "normalized": true,
25
+ "rstrip": false,
26
+ "single_word": true,
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": 4096,
40
+ "pad_token": "<unk>",
41
+ "sp_model_kwargs": {},
42
+ "tokenizer_class": "BaichuanTokenizer",
43
+ "unk_token": "<unk>"
44
+ }