ljsabc commited on
Commit
4544e1f
1 Parent(s): 6a9486d

first commit

Browse files
config.json ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "THUDM/chatglm-6b",
3
+ "architectures": [
4
+ "ChatGLMForConditionalGeneration"
5
+ ],
6
+ "auto_map": {
7
+ "AutoConfig": "configuration_chatglm.ChatGLMConfig",
8
+ "AutoModel": "modeling_chatglm.ChatGLMForConditionalGeneration",
9
+ "AutoModelForSeq2SeqLM": "modeling_chatglm.ChatGLMForConditionalGeneration"
10
+ },
11
+ "bos_token_id": 150004,
12
+ "eos_token_id": 150005,
13
+ "hidden_size": 4096,
14
+ "inner_hidden_size": 16384,
15
+ "layernorm_epsilon": 1e-05,
16
+ "max_sequence_length": 2048,
17
+ "model_type": "chatglm",
18
+ "num_attention_heads": 32,
19
+ "num_layers": 28,
20
+ "pad_token_id": 20003,
21
+ "position_encoding_2d": true,
22
+ "quantization_bit": 4,
23
+ "quantization_embeddings": false,
24
+ "torch_dtype": "float32",
25
+ "transformers_version": "4.27.1",
26
+ "use_cache": true,
27
+ "vocab_size": 150528
28
+ }
configuration_chatglm.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ ChatGLM model configuration """
2
+
3
+ from transformers.configuration_utils import PretrainedConfig
4
+ from transformers.utils import logging
5
+
6
+ logger = logging.get_logger(__name__)
7
+
8
+
9
+ class ChatGLMConfig(PretrainedConfig):
10
+ r"""
11
+ This is the configuration class to store the configuration of a [`~ChatGLMModel`].
12
+ It is used to instantiate an ChatGLM model according to the specified arguments, defining the model
13
+ architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of
14
+ the ChatGLM-6B [THUDM/ChatGLM-6B](https://huggingface.co/THUDM/chatglm-6b) architecture.
15
+
16
+ Configuration objects inherit from [`PretrainedConfig`] and can be used
17
+ to control the model outputs. Read the documentation from [`PretrainedConfig`]
18
+ for more information.
19
+
20
+
21
+ Args:
22
+ vocab_size (`int`, *optional*, defaults to 150528):
23
+ Vocabulary size of the ChatGLM-6B model. Defines the number of different tokens that can be represented by the
24
+ `inputs_ids` passed when calling [`~ChatGLMModel`] or
25
+ [`~TFChatGLMModel`].
26
+ hidden_size (`int`, *optional*, defaults to 4096):
27
+ Dimension of the encoder layers and the pooler layer.
28
+ num_hidden_layers (`int`, *optional*, defaults to 28):
29
+ Number of hidden layers in the Transformer encoder.
30
+ num_attention_heads (`int`, *optional*, defaults to 32):
31
+ Number of attention heads for each attention layer in the Transformer encoder.
32
+ inner_hidden_size (`int`, *optional*, defaults to 16384):
33
+ Dimension of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
34
+ max_sequence_length (`int`, *optional*, defaults to 512):
35
+ The maximum sequence length that this model might ever be used with.
36
+ Typically set this to something large just in case (e.g., 512 or 1024 or 2048).
37
+ layernorm_epsilon (`float`, *optional*, defaults to 1e-5):
38
+ The epsilon used by the layer normalization layers.
39
+ use_cache (`bool`, *optional*, defaults to `True`):
40
+ Whether the model should return the last key/values attentions (not used by all models).
41
+ Example:
42
+
43
+ ```python
44
+ >>> from configuration_chatglm import ChatGLMConfig
45
+ >>> from modeling_chatglm import ChatGLMModel
46
+
47
+ >>> # Initializing a ChatGLM-6B THUDM/ChatGLM-6B style configuration
48
+ >>> configuration = ChatGLMConfig()
49
+
50
+ >>> # Initializing a model from the THUDM/ChatGLM-6B style configuration
51
+ >>> model = ChatGLMModel(configuration)
52
+
53
+ >>> # Accessing the model configuration
54
+ >>> configuration = model.config
55
+ ```
56
+ """
57
+ model_type = "chatglm"
58
+
59
+ def __init__(
60
+ self,
61
+ vocab_size=150528,
62
+ hidden_size=4096,
63
+ num_layers=28,
64
+ num_attention_heads=32,
65
+ layernorm_epsilon=1e-5,
66
+ use_cache=False,
67
+ bos_token_id=150004,
68
+ eos_token_id=150005,
69
+ pad_token_id=0,
70
+ max_sequence_length=2048,
71
+ inner_hidden_size=16384,
72
+ position_encoding_2d=True,
73
+ quantization_bit=0,
74
+ quantization_embeddings=False,
75
+ **kwargs
76
+ ):
77
+ self.num_layers = num_layers
78
+ self.vocab_size = vocab_size
79
+ self.hidden_size = hidden_size
80
+ self.num_attention_heads = num_attention_heads
81
+ self.max_sequence_length = max_sequence_length
82
+ self.layernorm_epsilon = layernorm_epsilon
83
+ self.inner_hidden_size = inner_hidden_size
84
+ self.use_cache = use_cache
85
+ self.bos_token_id = bos_token_id
86
+ self.eos_token_id = eos_token_id
87
+ self.pad_token_id = pad_token_id
88
+ self.position_encoding_2d = position_encoding_2d
89
+ self.quantization_bit=quantization_bit
90
+ self.quantization_embeddings=quantization_embeddings
91
+ super().__init__(
92
+ pad_token_id=pad_token_id,
93
+ bos_token_id=bos_token_id,
94
+ eos_token_id=eos_token_id,
95
+ **kwargs
96
+ )
generation_config.json ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ {
2
+ "_from_model_config": true,
3
+ "bos_token_id": 150004,
4
+ "eos_token_id": 150005,
5
+ "pad_token_id": 20003,
6
+ "transformers_version": "4.27.1"
7
+ }
modeling_chatglm.py ADDED
@@ -0,0 +1,1318 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """ PyTorch ChatGLM model. """
2
+
3
+ import math
4
+ import copy
5
+ import os
6
+ import warnings
7
+ import re
8
+
9
+ import torch
10
+ import torch.utils.checkpoint
11
+ import torch.nn.functional as F
12
+ from torch import nn
13
+ from torch.nn import CrossEntropyLoss, LayerNorm
14
+ from torch.nn.utils import skip_init
15
+ from typing import Optional, Tuple, Union, List, Callable
16
+
17
+ from transformers.utils import (
18
+ add_code_sample_docstrings,
19
+ add_start_docstrings,
20
+ add_start_docstrings_to_model_forward,
21
+ )
22
+ from transformers.modeling_outputs import (
23
+ BaseModelOutputWithPast,
24
+ CausalLMOutputWithPast,
25
+ BaseModelOutputWithPastAndCrossAttentions,
26
+ )
27
+ from transformers.modeling_utils import PreTrainedModel
28
+ from transformers.utils import logging
29
+ from transformers.generation.logits_process import LogitsProcessor
30
+ from transformers.generation.utils import LogitsProcessorList, StoppingCriteriaList, GenerationConfig
31
+
32
+ from configuration_chatglm import ChatGLMConfig
33
+
34
+
35
+ # flags required to enable jit fusion kernels
36
+ torch._C._jit_set_profiling_mode(False)
37
+ torch._C._jit_set_profiling_executor(False)
38
+ torch._C._jit_override_can_fuse_on_cpu(True)
39
+ torch._C._jit_override_can_fuse_on_gpu(True)
40
+
41
+ logger = logging.get_logger(__name__)
42
+
43
+ _CHECKPOINT_FOR_DOC = "THUDM/ChatGLM-6B"
44
+ _CONFIG_FOR_DOC = "ChatGLM6BConfig"
45
+
46
+ CHATGLM_6B_PRETRAINED_MODEL_ARCHIVE_LIST = [
47
+ "THUDM/chatglm-6b",
48
+ # See all ChatGLM-6B models at https://huggingface.co/models?filter=chatglm
49
+ ]
50
+
51
+
52
+ class InvalidScoreLogitsProcessor(LogitsProcessor):
53
+ def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor) -> torch.FloatTensor:
54
+ if torch.isnan(scores).any() or torch.isinf(scores).any():
55
+ scores.zero_()
56
+ scores[..., 20005] = 5e4
57
+ return scores
58
+
59
+
60
+ def load_tf_weights_in_chatglm_6b(model, config, tf_checkpoint_path):
61
+ """Load tf checkpoints in a pytorch model."""
62
+ try:
63
+ import re
64
+
65
+ import numpy as np
66
+ import tensorflow as tf
67
+ except ImportError:
68
+ logger.error(
69
+ "Loading a TensorFlow model in PyTorch, requires TensorFlow to be installed. Please see "
70
+ "https://www.tensorflow.org/install/ for installation instructions."
71
+ )
72
+ raise
73
+ tf_path = os.path.abspath(tf_checkpoint_path)
74
+ logger.info(f"Converting TensorFlow checkpoint from {tf_path}")
75
+ # Load weights from TF model
76
+ init_vars = tf.train.list_variables(tf_path)
77
+ names = []
78
+ arrays = []
79
+ for name, shape in init_vars:
80
+ logger.info(f"Loading TF weight {name} with shape {shape}")
81
+ array = tf.train.load_variable(tf_path, name)
82
+ names.append(name)
83
+ arrays.append(array)
84
+
85
+ for name, array in zip(names, arrays):
86
+ name = name.split("/")
87
+ # adam_v and adam_m are variables used in AdamWeightDecayOptimizer to calculated m and v
88
+ # which are not required for using pretrained model
89
+ if any(
90
+ n in ["adam_v", "adam_m", "AdamWeightDecayOptimizer", "AdamWeightDecayOptimizer_1", "global_step"]
91
+ for n in name
92
+ ):
93
+ logger.info(f"Skipping {'/'.join(name)}")
94
+ continue
95
+ pointer = model
96
+ for m_name in name:
97
+ if re.fullmatch(r"[A-Za-z]+_\d+", m_name):
98
+ scope_names = re.split(r"_(\d+)", m_name)
99
+ else:
100
+ scope_names = [m_name]
101
+ if scope_names[0] == "kernel" or scope_names[0] == "gamma":
102
+ pointer = getattr(pointer, "weight")
103
+ elif scope_names[0] == "output_bias" or scope_names[0] == "beta":
104
+ pointer = getattr(pointer, "bias")
105
+ elif scope_names[0] == "output_weights":
106
+ pointer = getattr(pointer, "weight")
107
+ elif scope_names[0] == "squad":
108
+ pointer = getattr(pointer, "classifier")
109
+ else:
110
+ try:
111
+ pointer = getattr(pointer, scope_names[0])
112
+ except AttributeError:
113
+ logger.info(f"Skipping {'/'.join(name)}")
114
+ continue
115
+ if len(scope_names) >= 2:
116
+ num = int(scope_names[1])
117
+ pointer = pointer[num]
118
+ if m_name[-11:] == "_embeddings":
119
+ pointer = getattr(pointer, "weight")
120
+ elif m_name == "kernel":
121
+ array = np.transpose(array)
122
+ try:
123
+ assert (
124
+ pointer.shape == array.shape
125
+ ), f"Pointer shape {pointer.shape} and array shape {array.shape} mismatched"
126
+ except AssertionError as e:
127
+ e.args += (pointer.shape, array.shape)
128
+ raise
129
+ logger.info(f"Initialize PyTorch weight {name}")
130
+ pointer.data = torch.from_numpy(array)
131
+ return model
132
+
133
+
134
+ @torch.jit.script
135
+ def gelu_impl(x):
136
+ """OpenAI's gelu implementation."""
137
+ return 0.5 * x * (1.0 + torch.tanh(0.7978845608028654 * x *
138
+ (1.0 + 0.044715 * x * x)))
139
+
140
+
141
+ def gelu(x):
142
+ return gelu_impl(x)
143
+
144
+
145
+ class RotaryEmbedding(torch.nn.Module):
146
+ def __init__(self, dim, base=10000, precision=torch.half, learnable=False):
147
+ super().__init__()
148
+ inv_freq = 1. / (base ** (torch.arange(0, dim, 2).float() / dim))
149
+ inv_freq = inv_freq.half()
150
+ self.learnable = learnable
151
+ if learnable:
152
+ self.inv_freq = torch.nn.Parameter(inv_freq)
153
+ self.max_seq_len_cached = None
154
+ else:
155
+ self.register_buffer('inv_freq', inv_freq)
156
+ self.max_seq_len_cached = None
157
+ self.cos_cached = None
158
+ self.sin_cached = None
159
+ self.precision = precision
160
+
161
+ def _load_from_state_dict(self, state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys,
162
+ error_msgs):
163
+ pass
164
+
165
+ def forward(self, x, seq_dim=1, seq_len=None):
166
+ if seq_len is None:
167
+ seq_len = x.shape[seq_dim]
168
+ if self.max_seq_len_cached is None or (seq_len > self.max_seq_len_cached):
169
+ self.max_seq_len_cached = None if self.learnable else seq_len
170
+ t = torch.arange(seq_len, device=x.device, dtype=self.inv_freq.dtype)
171
+ freqs = torch.einsum('i,j->ij', t, self.inv_freq)
172
+ # Different from paper, but it uses a different permutation in order to obtain the same calculation
173
+ emb = torch.cat((freqs, freqs), dim=-1).to(x.device)
174
+ if self.precision == torch.bfloat16:
175
+ emb = emb.float()
176
+
177
+ # [sx, 1 (b * np), hn]
178
+ cos_cached = emb.cos()[:, None, :]
179
+ sin_cached = emb.sin()[:, None, :]
180
+ if self.precision == torch.bfloat16:
181
+ cos_cached = cos_cached.bfloat16()
182
+ sin_cached = sin_cached.bfloat16()
183
+ if self.learnable:
184
+ return cos_cached, sin_cached
185
+ self.cos_cached, self.sin_cached = cos_cached, sin_cached
186
+ return self.cos_cached[:seq_len, ...], self.sin_cached[:seq_len, ...]
187
+
188
+ def _apply(self, fn):
189
+ if self.cos_cached is not None:
190
+ self.cos_cached = fn(self.cos_cached)
191
+ if self.sin_cached is not None:
192
+ self.sin_cached = fn(self.sin_cached)
193
+ return super()._apply(fn)
194
+
195
+ def rotate_half(x):
196
+ x1, x2 = x[..., :x.shape[-1] // 2], x[..., x.shape[-1] // 2:]
197
+ return torch.cat((-x2, x1), dim=x1.ndim - 1) # dim=-1 triggers a bug in earlier torch versions
198
+
199
+
200
+ @torch.jit.script
201
+ def apply_rotary_pos_emb_index(q, k, cos, sin, position_id):
202
+ # position_id: [sq, b], q, k: [sq, b, np, hn], cos: [sq, 1, hn] -> [sq, b, 1, hn]
203
+ cos, sin = F.embedding(position_id, cos.squeeze(1)).unsqueeze(2), \
204
+ F.embedding(position_id, sin.squeeze(1)).unsqueeze(2)
205
+ q, k = (q * cos) + (rotate_half(q) * sin), (k * cos) + (rotate_half(k) * sin)
206
+ return q, k
207
+
208
+
209
+ def attention_fn(
210
+ self,
211
+ query_layer,
212
+ key_layer,
213
+ value_layer,
214
+ attention_mask,
215
+ hidden_size_per_partition,
216
+ layer_id,
217
+ layer_past=None,
218
+ scaling_attention_score=True,
219
+ use_cache=False,
220
+ ):
221
+ if layer_past is not None:
222
+ past_key, past_value = layer_past
223
+ key_layer = torch.cat((past_key, key_layer), dim=0)
224
+ value_layer = torch.cat((past_value, value_layer), dim=0)
225
+
226
+ # seqlen, batch, num_attention_heads, hidden_size_per_attention_head
227
+ seq_len, b, nh, hidden_size = key_layer.shape
228
+
229
+ if use_cache:
230
+ present = (key_layer, value_layer)
231
+ else:
232
+ present = None
233
+
234
+ query_key_layer_scaling_coeff = float(layer_id + 1)
235
+ if scaling_attention_score:
236
+ query_layer = query_layer / (math.sqrt(hidden_size) * query_key_layer_scaling_coeff)
237
+
238
+ # ===================================
239
+ # Raw attention scores. [b, np, s, s]
240
+ # ===================================
241
+
242
+ # [b, np, sq, sk]
243
+ output_size = (query_layer.size(1), query_layer.size(2), query_layer.size(0), key_layer.size(0))
244
+
245
+ # [sq, b, np, hn] -> [sq, b * np, hn]
246
+ query_layer = query_layer.view(output_size[2], output_size[0] * output_size[1], -1)
247
+ # [sk, b, np, hn] -> [sk, b * np, hn]
248
+ key_layer = key_layer.view(output_size[3], output_size[0] * output_size[1], -1)
249
+
250
+ matmul_result = torch.empty(
251
+ output_size[0] * output_size[1],
252
+ output_size[2],
253
+ output_size[3],
254
+ dtype=query_layer.dtype,
255
+ device=query_layer.device,
256
+ )
257
+
258
+ matmul_result = torch.baddbmm(
259
+ matmul_result,
260
+ query_layer.transpose(0, 1), # [b * np, sq, hn]
261
+ key_layer.transpose(0, 1).transpose(1, 2), # [b * np, hn, sk]
262
+ beta=0.0,
263
+ alpha=1.0,
264
+ )
265
+
266
+ # change view to [b, np, sq, sk]
267
+ attention_scores = matmul_result.view(*output_size)
268
+
269
+ if self.scale_mask_softmax:
270
+ self.scale_mask_softmax.scale = query_key_layer_scaling_coeff
271
+ attention_probs = self.scale_mask_softmax(attention_scores, attention_mask.contiguous())
272
+ else:
273
+ if not (attention_mask == 0).all():
274
+ # if auto-regressive, skip
275
+ attention_scores.masked_fill_(attention_mask, -10000.0)
276
+ dtype = attention_scores.type()
277
+ attention_scores = attention_scores.float()
278
+ attention_scores = attention_scores * query_key_layer_scaling_coeff
279
+
280
+ attention_probs = F.softmax(attention_scores, dim=-1)
281
+
282
+ attention_probs = attention_probs.type(dtype)
283
+
284
+ # =========================
285
+ # Context layer. [sq, b, hp]
286
+ # =========================
287
+
288
+ # value_layer -> context layer.
289
+ # [sk, b, np, hn] --> [b, np, sq, hn]
290
+
291
+ # context layer shape: [b, np, sq, hn]
292
+ output_size = (value_layer.size(1), value_layer.size(2), query_layer.size(0), value_layer.size(3))
293
+
294
+ # change view [sk, b * np, hn]
295
+ value_layer = value_layer.view(value_layer.size(0), output_size[0] * output_size[1], -1)
296
+
297
+ # change view [b * np, sq, sk]
298
+ attention_probs = attention_probs.view(output_size[0] * output_size[1], output_size[2], -1)
299
+
300
+ # matmul: [b * np, sq, hn]
301
+ context_layer = torch.bmm(attention_probs, value_layer.transpose(0, 1))
302
+
303
+ # change view [b, np, sq, hn]
304
+ context_layer = context_layer.view(*output_size)
305
+
306
+ # [b, np, sq, hn] --> [sq, b, np, hn]
307
+ context_layer = context_layer.permute(2, 0, 1, 3).contiguous()
308
+
309
+ # [sq, b, np, hn] --> [sq, b, hp]
310
+ new_context_layer_shape = context_layer.size()[:-2] + (hidden_size_per_partition,)
311
+ context_layer = context_layer.view(*new_context_layer_shape)
312
+
313
+ outputs = (context_layer, present, attention_probs)
314
+
315
+ return outputs
316
+
317
+
318
+ class SelfAttention(torch.nn.Module):
319
+ def __init__(self, hidden_size, num_attention_heads,
320
+ layer_id, hidden_size_per_attention_head=None, bias=True,
321
+ params_dtype=torch.float, position_encoding_2d=True):
322
+ super(SelfAttention, self).__init__()
323
+
324
+ self.layer_id = layer_id
325
+ self.hidden_size = hidden_size
326
+ self.hidden_size_per_partition = hidden_size
327
+ self.num_attention_heads = num_attention_heads
328
+ self.num_attention_heads_per_partition = num_attention_heads
329
+ self.position_encoding_2d = position_encoding_2d
330
+ self.rotary_emb = RotaryEmbedding(
331
+ self.hidden_size // (self.num_attention_heads * 2)
332
+ if position_encoding_2d
333
+ else self.hidden_size // self.num_attention_heads,
334
+ base=10000,
335
+ precision=torch.half,
336
+ learnable=False,
337
+ )
338
+
339
+ self.scale_mask_softmax = None
340
+
341
+ if hidden_size_per_attention_head is None:
342
+ self.hidden_size_per_attention_head = hidden_size // num_attention_heads
343
+ else:
344
+ self.hidden_size_per_attention_head = hidden_size_per_attention_head
345
+
346
+ self.inner_hidden_size = num_attention_heads * self.hidden_size_per_attention_head
347
+
348
+ # Strided linear layer.
349
+ self.query_key_value = skip_init(
350
+ torch.nn.Linear,
351
+ hidden_size,
352
+ 3 * self.inner_hidden_size,
353
+ bias=bias,
354
+ dtype=params_dtype,
355
+ )
356
+
357
+ self.dense = skip_init(
358
+ torch.nn.Linear,
359
+ self.inner_hidden_size,
360
+ hidden_size,
361
+ bias=bias,
362
+ dtype=params_dtype,
363
+ )
364
+
365
+ @staticmethod
366
+ def attention_mask_func(attention_scores, attention_mask):
367
+ attention_scores.masked_fill_(attention_mask, -10000.0)
368
+ return attention_scores
369
+
370
+ def split_tensor_along_last_dim(self, tensor, num_partitions,
371
+ contiguous_split_chunks=False):
372
+ """Split a tensor along its last dimension.
373
+ Arguments:
374
+ tensor: input tensor.
375
+ num_partitions: number of partitions to split the tensor
376
+ contiguous_split_chunks: If True, make each chunk contiguous
377
+ in memory.
378
+ """
379
+ # Get the size and dimension.
380
+ last_dim = tensor.dim() - 1
381
+ last_dim_size = tensor.size()[last_dim] // num_partitions
382
+ # Split.
383
+ tensor_list = torch.split(tensor, last_dim_size, dim=last_dim)
384
+ # Note: torch.split does not create contiguous tensors by default.
385
+ if contiguous_split_chunks:
386
+ return tuple(chunk.contiguous() for chunk in tensor_list)
387
+
388
+ return tensor_list
389
+
390
+ def forward(
391
+ self,
392
+ hidden_states: torch.Tensor,
393
+ position_ids,
394
+ attention_mask: torch.Tensor,
395
+ layer_id,
396
+ layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
397
+ use_cache: bool = False,
398
+ output_attentions: bool = False,
399
+ ):
400
+ """
401
+ hidden_states: [seq_len, batch, hidden_size]
402
+ attention_mask: [(1, 1), seq_len, seq_len]
403
+ """
404
+
405
+ # [seq_len, batch, 3 * hidden_size]
406
+ mixed_raw_layer = self.query_key_value(hidden_states)
407
+
408
+ # [seq_len, batch, 3 * hidden_size] --> [seq_len, batch, num_attention_heads, 3 * hidden_size_per_attention_head]
409
+ new_tensor_shape = mixed_raw_layer.size()[:-1] + (
410
+ self.num_attention_heads_per_partition,
411
+ 3 * self.hidden_size_per_attention_head,
412
+ )
413
+ mixed_raw_layer = mixed_raw_layer.view(*new_tensor_shape)
414
+
415
+ # [seq_len, batch, num_attention_heads, hidden_size_per_attention_head]
416
+ (query_layer, key_layer, value_layer) = self.split_tensor_along_last_dim(mixed_raw_layer, 3)
417
+
418
+ if self.position_encoding_2d:
419
+ q1, q2 = query_layer.chunk(2, dim=(query_layer.ndim - 1))
420
+ k1, k2 = key_layer.chunk(2, dim=(key_layer.ndim - 1))
421
+ cos, sin = self.rotary_emb(q1, seq_len=position_ids.max() + 1)
422
+ position_ids, block_position_ids = position_ids[:, 0, :].transpose(0, 1).contiguous(), \
423
+ position_ids[:, 1, :].transpose(0, 1).contiguous()
424
+ q1, k1 = apply_rotary_pos_emb_index(q1, k1, cos, sin, position_ids)
425
+ q2, k2 = apply_rotary_pos_emb_index(q2, k2, cos, sin, block_position_ids)
426
+ query_layer = torch.concat([q1, q2], dim=(q1.ndim - 1))
427
+ key_layer = torch.concat([k1, k2], dim=(k1.ndim - 1))
428
+ else:
429
+ position_ids = position_ids.transpose(0, 1)
430
+ cos, sin = self.rotary_emb(value_layer, seq_len=position_ids.max() + 1)
431
+ # [seq_len, batch, num_attention_heads, hidden_size_per_attention_head]
432
+ query_layer, key_layer = apply_rotary_pos_emb_index(query_layer, key_layer, cos, sin, position_ids)
433
+
434
+ # [seq_len, batch, hidden_size]
435
+ context_layer, present, attention_probs = attention_fn(
436
+ self=self,
437
+ query_layer=query_layer,
438
+ key_layer=key_layer,
439
+ value_layer=value_layer,
440
+ attention_mask=attention_mask,
441
+ hidden_size_per_partition=self.hidden_size_per_partition,
442
+ layer_id=layer_id,
443
+ layer_past=layer_past,
444
+ use_cache=use_cache
445
+ )
446
+
447
+ output = self.dense(context_layer)
448
+
449
+ outputs = (output, present)
450
+
451
+ if output_attentions:
452
+ outputs += (attention_probs,)
453
+
454
+ return outputs # output, present, attention_probs
455
+
456
+
457
+ class GEGLU(torch.nn.Module):
458
+ def __init__(self):
459
+ super().__init__()
460
+ self.activation_fn = F.gelu
461
+
462
+ def forward(self, x):
463
+ # dim=-1 breaks in jit for pt<1.10
464
+ x1, x2 = x.chunk(2, dim=(x.ndim - 1))
465
+ return x1 * self.activation_fn(x2)
466
+
467
+
468
+ class GLU(torch.nn.Module):
469
+ def __init__(self, hidden_size, inner_hidden_size=None,
470
+ layer_id=None, bias=True, activation_func=gelu, params_dtype=torch.float):
471
+ super(GLU, self).__init__()
472
+ self.layer_id = layer_id
473
+ self.activation_func = activation_func
474
+
475
+ # Project to 4h.
476
+ self.hidden_size = hidden_size
477
+ if inner_hidden_size is None:
478
+ inner_hidden_size = 4 * hidden_size
479
+ self.inner_hidden_size = inner_hidden_size
480
+ self.dense_h_to_4h = skip_init(
481
+ torch.nn.Linear,
482
+ self.hidden_size,
483
+ self.inner_hidden_size,
484
+ bias=bias,
485
+ dtype=params_dtype,
486
+ )
487
+ # Project back to h.
488
+ self.dense_4h_to_h = skip_init(
489
+ torch.nn.Linear,
490
+ self.inner_hidden_size,
491
+ self.hidden_size,
492
+ bias=bias,
493
+ dtype=params_dtype,
494
+ )
495
+
496
+ def forward(self, hidden_states):
497
+ """
498
+ hidden_states: [seq_len, batch, hidden_size]
499
+ """
500
+
501
+ # [seq_len, batch, inner_hidden_size]
502
+ intermediate_parallel = self.dense_h_to_4h(hidden_states)
503
+
504
+ intermediate_parallel = self.activation_func(intermediate_parallel)
505
+
506
+ output = self.dense_4h_to_h(intermediate_parallel)
507
+
508
+ return output
509
+
510
+
511
+ class GLMBlock(torch.nn.Module):
512
+ def __init__(
513
+ self,
514
+ hidden_size,
515
+ num_attention_heads,
516
+ layernorm_epsilon,
517
+ layer_id,
518
+ inner_hidden_size=None,
519
+ hidden_size_per_attention_head=None,
520
+ layernorm=LayerNorm,
521
+ use_bias=True,
522
+ params_dtype=torch.float,
523
+ num_layers=28,
524
+ position_encoding_2d=True
525
+ ):
526
+ super(GLMBlock, self).__init__()
527
+ # Set output layer initialization if not provided.
528
+
529
+ self.layer_id = layer_id
530
+
531
+ # Layernorm on the input data.
532
+ self.input_layernorm = layernorm(hidden_size, eps=layernorm_epsilon)
533
+
534
+ self.position_encoding_2d = position_encoding_2d
535
+
536
+ # Self attention.
537
+ self.attention = SelfAttention(
538
+ hidden_size,
539
+ num_attention_heads,
540
+ layer_id,
541
+ hidden_size_per_attention_head=hidden_size_per_attention_head,
542
+ bias=use_bias,
543
+ params_dtype=params_dtype,
544
+ position_encoding_2d=self.position_encoding_2d
545
+ )
546
+
547
+ # Layernorm on the input data.
548
+ self.post_attention_layernorm = layernorm(hidden_size, eps=layernorm_epsilon)
549
+
550
+ self.num_layers = num_layers
551
+
552
+ # GLU
553
+ self.mlp = GLU(
554
+ hidden_size,
555
+ inner_hidden_size=inner_hidden_size,
556
+ bias=use_bias,
557
+ layer_id=layer_id,
558
+ params_dtype=params_dtype,
559
+ )
560
+
561
+ def forward(
562
+ self,
563
+ hidden_states: torch.Tensor,
564
+ position_ids,
565
+ attention_mask: torch.Tensor,
566
+ layer_id,
567
+ layer_past: Optional[Tuple[torch.Tensor, torch.Tensor]] = None,
568
+ use_cache: bool = False,
569
+ output_attentions: bool = False,
570
+ ):
571
+ """
572
+ hidden_states: [seq_len, batch, hidden_size]
573
+ attention_mask: [(1, 1), seq_len, seq_len]
574
+ """
575
+
576
+ # Layer norm at the begining of the transformer layer.
577
+ # [seq_len, batch, hidden_size]
578
+ attention_input = self.input_layernorm(hidden_states)
579
+
580
+ # Self attention.
581
+ attention_outputs = self.attention(
582
+ attention_input,
583
+ position_ids,
584
+ attention_mask=attention_mask,
585
+ layer_id=layer_id,
586
+ layer_past=layer_past,
587
+ use_cache=use_cache,
588
+ output_attentions=output_attentions
589
+ )
590
+
591
+ attention_output = attention_outputs[0]
592
+
593
+ outputs = attention_outputs[1:]
594
+
595
+ # Residual connection.
596
+ alpha = (2 * self.num_layers) ** 0.5
597
+ hidden_states = attention_input * alpha + attention_output
598
+
599
+ mlp_input = self.post_attention_layernorm(hidden_states)
600
+
601
+ # MLP.
602
+ mlp_output = self.mlp(mlp_input)
603
+
604
+ # Second residual connection.
605
+ output = mlp_input * alpha + mlp_output
606
+
607
+ if use_cache:
608
+ outputs = (output,) + outputs
609
+ else:
610
+ outputs = (output,) + outputs[1:]
611
+
612
+ return outputs # hidden_states, present, attentions
613
+
614
+
615
+ class ChatGLMPreTrainedModel(PreTrainedModel):
616
+ """
617
+ An abstract class to handle weights initialization and
618
+ a simple interface for downloading and loading pretrained models.
619
+ """
620
+
621
+ is_parallelizable = False
622
+ supports_gradient_checkpointing = False
623
+ config_class = ChatGLMConfig
624
+ base_model_prefix = "transformer"
625
+ _no_split_modules = ["GLM6BBlock"]
626
+
627
+ def __init__(self, *inputs, **kwargs):
628
+ super().__init__(*inputs, **kwargs)
629
+
630
+ def _init_weights(self, module: nn.Module):
631
+ """Initialize the weights."""
632
+ return
633
+
634
+
635
+ CHATGLM_6B_START_DOCSTRING = r"""
636
+ This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) sub-class.
637
+ Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general
638
+ usage and behavior.
639
+
640
+ Parameters:
641
+ config ([`~ChatGLM6BConfig`]): Model configuration class with all the parameters of the model.
642
+ Initializing with a config file does not load the weights associated with the model, only the configuration.
643
+ Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
644
+ """
645
+
646
+ CHATGLM_6B_INPUTS_DOCSTRING = r"""
647
+ Args:
648
+ input_ids (`torch.LongTensor` of shape `({0})`):
649
+ Indices of input sequence tokens in the vocabulary.
650
+
651
+ Indices can be obtained using [`ChatGLM6BTokenizer`].
652
+ See [`PreTrainedTokenizer.encode`] and
653
+ [`PreTrainedTokenizer.__call__`] for details.
654
+
655
+ [What are input IDs?](../glossary#input-ids)
656
+ attention_mask (`torch.FloatTensor` of shape `({0})`, *optional*):
657
+ Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
658
+
659
+ - 1 for tokens that are **not masked**,
660
+ - 0 for tokens that are **masked**.
661
+
662
+ [What are attention masks?](../glossary#attention-mask)
663
+ token_type_ids (`torch.LongTensor` of shape `({0})`, *optional*):
664
+ Segment token indices to indicate first and second portions of the inputs. Indices are selected in `[0, 1]`:
665
+
666
+ - 0 corresponds to a *sentence A* token,
667
+ - 1 corresponds to a *sentence B* token.
668
+
669
+ [What are token type IDs?](../glossary#token-type-ids)
670
+ position_ids (`torch.LongTensor` of shape `({0})`, *optional*):
671
+ Indices of positions of each input sequence tokens in the position embeddings.
672
+ Selected in the range `[0, config.max_position_embeddings - 1]`.
673
+
674
+ [What are position IDs?](../glossary#position-ids)
675
+ head_mask (`torch.FloatTensor` of shape `(num_heads,)` or `(num_layers, num_heads)`, *optional*):
676
+ Mask to nullify selected heads of the self-attention modules. Mask values selected in `[0, 1]`:
677
+
678
+ - 1 indicates the head is **not masked**,
679
+ - 0 indicates the head is **masked**.
680
+
681
+ inputs_embeds (`torch.FloatTensor` of shape `({0}, hidden_size)`, *optional*):
682
+ Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation.
683
+ This is useful if you want more control over how to convert *input_ids* indices into associated vectors
684
+ than the model's internal embedding lookup matrix.
685
+ output_attentions (`bool`, *optional*):
686
+ Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
687
+ tensors for more detail.
688
+ output_hidden_states (`bool`, *optional*):
689
+ Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
690
+ more detail.
691
+ return_dict (`bool`, *optional*):
692
+ Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
693
+ """
694
+
695
+
696
+ @add_start_docstrings(
697
+ "The bare ChatGLM-6B Model transformer outputting raw hidden-states without any specific head on top.",
698
+ CHATGLM_6B_START_DOCSTRING,
699
+ )
700
+ class ChatGLMModel(ChatGLMPreTrainedModel):
701
+ """
702
+
703
+ The model can behave as an encoder (with only self-attention) as well
704
+ as a decoder, in which case a layer of cross-attention is added between
705
+ the self-attention layers, following the architecture described in [Attention is
706
+ all you need](https://arxiv.org/abs/1706.03762) by Ashish Vaswani,
707
+ Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz Kaiser and Illia Polosukhin.
708
+
709
+ To behave as an decoder the model needs to be initialized with the
710
+ `is_decoder` argument of the configuration set to `True`.
711
+ To be used in a Seq2Seq model, the model needs to initialized with both `is_decoder`
712
+ argument and `add_cross_attention` set to `True`; an
713
+ `encoder_hidden_states` is then expected as an input to the forward pass.
714
+ """
715
+
716
+ def __init__(self, config: ChatGLMConfig):
717
+ super().__init__(config)
718
+
719
+ # recording parameters
720
+ self.max_sequence_length = config.max_sequence_length
721
+ self.hidden_size = config.hidden_size
722
+ self.params_dtype = torch.half
723
+ self.num_attention_heads = config.num_attention_heads
724
+ self.vocab_size = config.vocab_size
725
+ self.num_layers = config.num_layers
726
+ self.layernorm_epsilon = config.layernorm_epsilon
727
+ self.inner_hidden_size = config.inner_hidden_size
728
+ self.hidden_size_per_attention_head = self.hidden_size // self.num_attention_heads
729
+ self.position_encoding_2d = config.position_encoding_2d
730
+
731
+ self.word_embeddings = skip_init(
732
+ torch.nn.Embedding,
733
+ num_embeddings=self.vocab_size, embedding_dim=self.hidden_size,
734
+ dtype=self.params_dtype
735
+ )
736
+
737
+ def get_layer(layer_id):
738
+ return GLMBlock(
739
+ self.hidden_size,
740
+ self.num_attention_heads,
741
+ self.layernorm_epsilon,
742
+ layer_id,
743
+ inner_hidden_size=self.inner_hidden_size,
744
+ hidden_size_per_attention_head=self.hidden_size_per_attention_head,
745
+ layernorm=LayerNorm,
746
+ use_bias=True,
747
+ params_dtype=self.params_dtype,
748
+ position_encoding_2d=self.position_encoding_2d,
749
+ )
750
+
751
+ self.layers = torch.nn.ModuleList(
752
+ [get_layer(layer_id) for layer_id in range(self.num_layers)]
753
+ )
754
+
755
+ # Final layer norm before output.
756
+ self.final_layernorm = LayerNorm(self.hidden_size, eps=self.layernorm_epsilon)
757
+
758
+ def get_input_embeddings(self):
759
+ return self.word_embeddings
760
+
761
+ def set_input_embeddings(self, new_embeddings: torch.Tensor):
762
+ self.word_embeddings = new_embeddings
763
+
764
+ def get_masks(self, seq, device):
765
+ context_length = seq.index(self.config.bos_token_id) + 1
766
+
767
+ attention_mask = torch.ones((1, len(seq), len(seq)), device=device)
768
+ attention_mask.tril_()
769
+ attention_mask[..., :context_length - 1] = 1
770
+ attention_mask.unsqueeze_(1)
771
+ attention_mask = (attention_mask < 0.5).bool()
772
+
773
+ return attention_mask
774
+
775
+ def get_position_ids(self, seq, mask_position, device, gmask=False):
776
+ context_length = seq.index(self.config.bos_token_id) + 1
777
+ if self.position_encoding_2d:
778
+ seq_length = seq.index(self.config.bos_token_id)
779
+ position_ids = torch.arange(context_length, dtype=torch.long, device=device)
780
+ if not gmask:
781
+ position_ids[seq_length:] = mask_position
782
+ block_position_ids = torch.cat((
783
+ torch.zeros(seq_length, dtype=torch.long, device=device),
784
+ torch.arange(context_length - seq_length, dtype=torch.long, device=device) + 1
785
+ ))
786
+ position_ids = torch.stack((position_ids, block_position_ids), dim=0)
787
+ else:
788
+ position_ids = torch.arange(context_length, dtype=torch.long, device=device)
789
+ if not gmask:
790
+ position_ids[context_length - 1:] = mask_position
791
+
792
+ position_ids = position_ids.unsqueeze(0)
793
+
794
+ return position_ids
795
+
796
+ @add_start_docstrings_to_model_forward(CHATGLM_6B_INPUTS_DOCSTRING.format("batch_size, sequence_length"))
797
+ @add_code_sample_docstrings(
798
+ checkpoint=_CHECKPOINT_FOR_DOC,
799
+ output_type=BaseModelOutputWithPastAndCrossAttentions,
800
+ config_class=_CONFIG_FOR_DOC,
801
+ )
802
+ def forward(
803
+ self,
804
+ input_ids: Optional[torch.LongTensor] = None,
805
+ position_ids: Optional[torch.LongTensor] = None,
806
+ attention_mask: Optional[torch.Tensor] = None,
807
+ past_key_values: Optional[Tuple[Tuple[torch.Tensor, torch.Tensor], ...]] = None,
808
+ inputs_embeds: Optional[torch.LongTensor] = None,
809
+ use_cache: Optional[bool] = None,
810
+ output_attentions: Optional[bool] = None,
811
+ output_hidden_states: Optional[bool] = None,
812
+ return_dict: Optional[bool] = None,
813
+ ) -> Union[Tuple[torch.Tensor, ...], BaseModelOutputWithPast]:
814
+
815
+ output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
816
+ output_hidden_states = (
817
+ output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
818
+ )
819
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
820
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
821
+
822
+ if input_ids is not None and inputs_embeds is not None:
823
+ raise ValueError("You cannot specify both input_ids and inputs_embeds at the same time")
824
+ elif input_ids is not None:
825
+ batch_size, seq_length = input_ids.shape[:2]
826
+ elif inputs_embeds is not None:
827
+ batch_size, seq_length, _ = inputs_embeds.shape[:2]
828
+ else:
829
+ raise ValueError("You have to specify either input_ids or inputs_embeds")
830
+
831
+ if past_key_values is None:
832
+ past_key_values = tuple([None] * len(self.layers))
833
+ seq = input_ids[0].tolist()
834
+
835
+ if attention_mask is None:
836
+ attention_mask = self.get_masks(
837
+ seq=seq,
838
+ device=input_ids.device
839
+ )
840
+
841
+ if position_ids is None:
842
+ MASK, gMASK = 150000, 150001
843
+ mask_token = MASK if MASK in input_ids else gMASK
844
+ use_gmask = False if MASK in input_ids else gMASK
845
+
846
+ mask_position = seq.index(mask_token)
847
+ position_ids = self.get_position_ids(
848
+ seq=seq,
849
+ mask_position=mask_position,
850
+ device=input_ids.device,
851
+ gmask=use_gmask
852
+ )
853
+
854
+ if inputs_embeds is None:
855
+ inputs_embeds = self.word_embeddings(input_ids)
856
+
857
+ # [seq_len, batch, hidden_size]
858
+ hidden_states = inputs_embeds.transpose(0, 1)
859
+
860
+ presents = () if use_cache else None
861
+ all_self_attentions = () if output_attentions else None
862
+ all_hidden_states = () if output_hidden_states else None
863
+
864
+ seq_length_with_past = seq_length
865
+ past_key_values_length = 0
866
+ if past_key_values[0] is not None:
867
+ past_key_values_length = past_key_values[0][0].shape[0]
868
+ seq_length_with_past = seq_length_with_past + past_key_values_length
869
+ if attention_mask is None:
870
+ attention_mask = torch.zeros(1, 1, device=input_ids.device).bool()
871
+
872
+ else:
873
+ attention_mask = attention_mask.to(input_ids.device)
874
+
875
+ for i, layer in enumerate(self.layers):
876
+
877
+ if output_hidden_states:
878
+ all_hidden_states = all_hidden_states + (hidden_states,)
879
+
880
+ layer_ret = layer(
881
+ hidden_states,
882
+ position_ids=position_ids,
883
+ attention_mask=attention_mask,
884
+ layer_id=torch.tensor(i),
885
+ layer_past=past_key_values[i],
886
+ use_cache=use_cache,
887
+ output_attentions=output_attentions
888
+ )
889
+
890
+ hidden_states = layer_ret[0]
891
+
892
+ if use_cache:
893
+ presents = presents + (layer_ret[1],)
894
+
895
+ if output_attentions:
896
+ all_self_attentions = all_self_attentions + (layer_ret[2 if use_cache else 1],)
897
+
898
+ # Final layer norm.
899
+ hidden_states = self.final_layernorm(hidden_states)
900
+
901
+ if output_hidden_states:
902
+ all_hidden_states = all_hidden_states + (hidden_states,)
903
+
904
+ if not return_dict:
905
+ return tuple(v for v in [hidden_states, presents, all_hidden_states, all_self_attentions] if v is not None)
906
+
907
+ return BaseModelOutputWithPast(
908
+ last_hidden_state=hidden_states,
909
+ past_key_values=presents,
910
+ hidden_states=all_hidden_states,
911
+ attentions=all_self_attentions,
912
+ )
913
+
914
+
915
+ class ChatGLMForConditionalGeneration(ChatGLMPreTrainedModel):
916
+ def __init__(self, config: ChatGLMConfig):
917
+ super().__init__(config)
918
+
919
+ # self.hidden_size = config.hidden_size
920
+ # self.params_dtype = torch.half
921
+ # self.vocab_size = config.vocab_size
922
+ self.max_sequence_length = config.max_sequence_length
923
+
924
+ self.position_encoding_2d = config.position_encoding_2d
925
+
926
+ self.transformer = ChatGLMModel(config)
927
+
928
+ self.lm_head = skip_init(
929
+ nn.Linear,
930
+ config.hidden_size,
931
+ config.vocab_size,
932
+ bias=False,
933
+ dtype=torch.half
934
+ )
935
+
936
+ self.config = config
937
+
938
+ self.quantized = False
939
+
940
+ if self.config.quantization_bit:
941
+ self.quantize(self.config.quantization_bit, self.config.quantization_embeddings, use_quantization_cache=True, empty_init=True)
942
+
943
+ def get_output_embeddings(self):
944
+ return self.lm_head
945
+
946
+ def set_output_embeddings(self, new_embeddings):
947
+ self.lm_head = new_embeddings
948
+
949
+ def get_masks_and_position_ids(self, seq, mask_position, context_length, device, gmask=False):
950
+ attention_mask = torch.ones((1, context_length, context_length), device=device)
951
+ attention_mask.tril_()
952
+ attention_mask[..., :context_length - 1] = 1
953
+ attention_mask.unsqueeze_(1)
954
+ attention_mask = (attention_mask < 0.5).bool()
955
+
956
+ if self.position_encoding_2d:
957
+ seq_length = seq.index(self.config.bos_token_id)
958
+ position_ids = torch.arange(context_length, dtype=torch.long, device=device)
959
+ if not gmask:
960
+ position_ids[seq_length:] = mask_position
961
+ block_position_ids = torch.cat((
962
+ torch.zeros(seq_length, dtype=torch.long, device=device),
963
+ torch.arange(context_length - seq_length, dtype=torch.long, device=device) + 1
964
+ ))
965
+ position_ids = torch.stack((position_ids, block_position_ids), dim=0)
966
+ else:
967
+ position_ids = torch.arange(context_length, dtype=torch.long, device=device)
968
+ if not gmask:
969
+ position_ids[context_length - 1:] = mask_position
970
+
971
+ position_ids = position_ids.unsqueeze(0)
972
+
973
+ return attention_mask, position_ids
974
+
975
+ def prepare_inputs_for_generation(
976
+ self,
977
+ input_ids: torch.LongTensor,
978
+ past: Optional[torch.Tensor] = None,
979
+ past_key_values: Optional[torch.Tensor] = None,
980
+ attention_mask: Optional[torch.Tensor] = None,
981
+ **kwargs
982
+ ) -> dict:
983
+
984
+ MASK, gMASK = 150000, 150001
985
+ mask_token = MASK if MASK in input_ids else gMASK
986
+ use_gmask = False if MASK in input_ids else gMASK
987
+ seq = input_ids[0].tolist()
988
+ mask_position = seq.index(mask_token)
989
+
990
+ if mask_token not in seq:
991
+ raise ValueError("You have to add either [MASK] or [gMASK] in your input")
992
+
993
+ # only last token for input_ids if past is not None
994
+ if past is not None or past_key_values is not None:
995
+ context_length = seq.index(self.config.bos_token_id)
996
+ last_token = input_ids[:, -1].unsqueeze(-1)
997
+ if self.position_encoding_2d:
998
+ position_ids = torch.tensor([[[mask_position], [len(seq) - context_length]]], dtype=torch.long,
999
+ device=input_ids.device)
1000
+ else:
1001
+ position_ids = torch.tensor([[mask_position]], dtype=torch.long, device=input_ids.device)
1002
+
1003
+ if past is None:
1004
+ past = past_key_values
1005
+ return {
1006
+ "input_ids": last_token,
1007
+ "past_key_values": past,
1008
+ "position_ids": position_ids,
1009
+ }
1010
+ else:
1011
+ attention_mask, position_ids = self.get_masks_and_position_ids(
1012
+ seq=seq,
1013
+ mask_position=mask_position,
1014
+ context_length=len(seq),
1015
+ device=input_ids.device,
1016
+ gmask=use_gmask
1017
+ )
1018
+
1019
+ return {
1020
+ "input_ids": input_ids,
1021
+ "past_key_values": past,
1022
+ "position_ids": position_ids,
1023
+ "attention_mask": attention_mask
1024
+ }
1025
+
1026
+ def forward(
1027
+ self,
1028
+ input_ids: Optional[torch.Tensor] = None,
1029
+ position_ids: Optional[torch.Tensor] = None,
1030
+ attention_mask: Optional[torch.Tensor] = None,
1031
+ past_key_values: Optional[Tuple[torch.FloatTensor]] = None,
1032
+ inputs_embeds: Optional[torch.Tensor] = None,
1033
+ labels: Optional[torch.Tensor] = None,
1034
+ use_cache: Optional[bool] = None,
1035
+ output_attentions: Optional[bool] = None,
1036
+ output_hidden_states: Optional[bool] = None,
1037
+ return_dict: Optional[bool] = None,
1038
+ ):
1039
+ use_cache = use_cache if use_cache is not None else self.config.use_cache
1040
+ return_dict = return_dict if return_dict is not None else self.config.use_return_dict
1041
+
1042
+ transformer_outputs = self.transformer(
1043
+ input_ids=input_ids,
1044
+ position_ids=position_ids,
1045
+ attention_mask=attention_mask,
1046
+ past_key_values=past_key_values,
1047
+ inputs_embeds=inputs_embeds,
1048
+ use_cache=use_cache,
1049
+ output_attentions=output_attentions,
1050
+ output_hidden_states=output_hidden_states,
1051
+ return_dict=return_dict,
1052
+ )
1053
+
1054
+ hidden_states = transformer_outputs[0]
1055
+
1056
+ lm_logits = self.lm_head(hidden_states).permute(1, 0, 2).contiguous()
1057
+
1058
+ loss = None
1059
+ if labels is not None:
1060
+ lm_logits = lm_logits.to(torch.float32)
1061
+
1062
+ # Shift so that tokens < n predict n
1063
+ shift_logits = lm_logits[..., :-1, :].contiguous()
1064
+ shift_labels = labels[..., 1:].contiguous()
1065
+ # Flatten the tokens
1066
+ loss_fct = CrossEntropyLoss()
1067
+ loss = loss_fct(shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1))
1068
+
1069
+ lm_logits = lm_logits.to(hidden_states.dtype)
1070
+ loss = loss.to(hidden_states.dtype)
1071
+
1072
+ if not return_dict:
1073
+ output = (lm_logits,) + transformer_outputs[1:]
1074
+ return ((loss,) + output) if loss is not None else output
1075
+
1076
+ return CausalLMOutputWithPast(
1077
+ loss=loss,
1078
+ logits=lm_logits,
1079
+ past_key_values=transformer_outputs.past_key_values,
1080
+ hidden_states=transformer_outputs.hidden_states,
1081
+ attentions=transformer_outputs.attentions,
1082
+ )
1083
+
1084
+ @staticmethod
1085
+ def _reorder_cache(
1086
+ past: Tuple[Tuple[torch.Tensor, torch.Tensor], ...], beam_idx: torch.LongTensor
1087
+ ) -> Tuple[Tuple[torch.Tensor, torch.Tensor], ...]:
1088
+ """
1089
+ This function is used to re-order the `past_key_values` cache if [`~PreTrainedModel.beam_search`] or
1090
+ [`~PreTrainedModel.beam_sample`] is called. This is required to match `past_key_values` with the correct
1091
+ beam_idx at every generation step.
1092
+
1093
+ Output shares the same memory storage as `past`.
1094
+ """
1095
+ return tuple(
1096
+ (
1097
+ layer_past[0].index_select(1, beam_idx.to(layer_past[0].device)),
1098
+ layer_past[1].index_select(1, beam_idx.to(layer_past[1].device)),
1099
+ )
1100
+ for layer_past in past
1101
+ )
1102
+
1103
+ def process_response(self, response):
1104
+ response = response.strip()
1105
+ response = response.replace("[[训练时间]]", "2023年")
1106
+ punkts = [
1107
+ [",", ","],
1108
+ ["!", "!"],
1109
+ [":", ":"],
1110
+ [";", ";"],
1111
+ ["\?", "?"],
1112
+ ]
1113
+ for item in punkts:
1114
+ response = re.sub(r"([\u4e00-\u9fff])%s" % item[0], r"\1%s" % item[1], response)
1115
+ response = re.sub(r"%s([\u4e00-\u9fff])" % item[0], r"%s\1" % item[1], response)
1116
+ return response
1117
+
1118
+ @torch.no_grad()
1119
+ def chat(self, tokenizer, query: str, history: List[Tuple[str, str]] = None, max_length: int = 2048, num_beams=1,
1120
+ do_sample=True, top_p=0.7, temperature=0.95, logits_processor=None, **kwargs):
1121
+ if history is None:
1122
+ history = []
1123
+ if logits_processor is None:
1124
+ logits_processor = LogitsProcessorList()
1125
+ logits_processor.append(InvalidScoreLogitsProcessor())
1126
+ gen_kwargs = {"max_length": max_length, "num_beams": num_beams, "do_sample": do_sample, "top_p": top_p,
1127
+ "temperature": temperature, "logits_processor": logits_processor, **kwargs}
1128
+ if not history:
1129
+ prompt = query
1130
+ else:
1131
+ prompt = ""
1132
+ for i, (old_query, response) in enumerate(history):
1133
+ prompt += "[Round {}]\n问:{}\n答:{}\n".format(i, old_query, response)
1134
+ prompt += "[Round {}]\n问:{}\n答:".format(len(history), query)
1135
+ input_ids = tokenizer([prompt], return_tensors="pt", padding=True)
1136
+ input_ids = input_ids.to(self.device)
1137
+ outputs = self.generate(**input_ids, **gen_kwargs)
1138
+ outputs = outputs.tolist()[0][len(input_ids["input_ids"][0]):]
1139
+ response = tokenizer.decode(outputs)
1140
+ response = self.process_response(response)
1141
+ history = history + [(query, response)]
1142
+ return response, history
1143
+
1144
+ @torch.no_grad()
1145
+ def stream_chat(self, tokenizer, query: str, history: List[Tuple[str, str]] = None, max_length: int = 2048,
1146
+ do_sample=True, top_p=0.7, temperature=0.95, logits_processor=None, **kwargs):
1147
+ if history is None:
1148
+ history = []
1149
+ if logits_processor is None:
1150
+ logits_processor = LogitsProcessorList()
1151
+ logits_processor.append(InvalidScoreLogitsProcessor())
1152
+ gen_kwargs = {"max_length": max_length, "do_sample": do_sample, "top_p": top_p,
1153
+ "temperature": temperature, "logits_processor": logits_processor, **kwargs}
1154
+ if not history:
1155
+ prompt = query
1156
+ else:
1157
+ prompt = ""
1158
+ for i, (old_query, response) in enumerate(history):
1159
+ prompt += "[Round {}]\n问:{}\n答:{}\n".format(i, old_query, response)
1160
+ prompt += "[Round {}]\n问:{}\n答:".format(len(history), query)
1161
+ input_ids = tokenizer([prompt], return_tensors="pt", padding=True)
1162
+ input_ids = input_ids.to(self.device)
1163
+ for outputs in self.stream_generate(**input_ids, **gen_kwargs):
1164
+ outputs = outputs.tolist()[0][len(input_ids["input_ids"][0]):]
1165
+ response = tokenizer.decode(outputs)
1166
+ response = self.process_response(response)
1167
+ new_history = history + [(query, response)]
1168
+ yield response, new_history
1169
+
1170
+ @torch.no_grad()
1171
+ def stream_generate(
1172
+ self,
1173
+ input_ids,
1174
+ generation_config: Optional[GenerationConfig] = None,
1175
+ logits_processor: Optional[LogitsProcessorList] = None,
1176
+ stopping_criteria: Optional[StoppingCriteriaList] = None,
1177
+ prefix_allowed_tokens_fn: Optional[Callable[[int, torch.Tensor], List[int]]] = None,
1178
+ **kwargs,
1179
+ ):
1180
+ batch_size, input_ids_seq_length = input_ids.shape[0], input_ids.shape[-1]
1181
+
1182
+ if generation_config is None:
1183
+ generation_config = self.generation_config
1184
+ generation_config = copy.deepcopy(generation_config)
1185
+ model_kwargs = generation_config.update(**kwargs)
1186
+ bos_token_id, eos_token_id = generation_config.bos_token_id, generation_config.eos_token_id
1187
+
1188
+ if isinstance(eos_token_id, int):
1189
+ eos_token_id = [eos_token_id]
1190
+
1191
+ has_default_max_length = kwargs.get("max_length") is None and generation_config.max_length is not None
1192
+ if has_default_max_length and generation_config.max_new_tokens is None:
1193
+ warnings.warn(
1194
+ f"Using `max_length`'s default ({generation_config.max_length}) to control the generation length. "
1195
+ "This behaviour is deprecated and will be removed from the config in v5 of Transformers -- we"
1196
+ " recommend using `max_new_tokens` to control the maximum length of the generation.",
1197
+ UserWarning,
1198
+ )
1199
+ elif generation_config.max_new_tokens is not None:
1200
+ generation_config.max_length = generation_config.max_new_tokens + input_ids_seq_length
1201
+ if not has_default_max_length:
1202
+ logger.warn(
1203
+ f"Both `max_new_tokens` (={generation_config.max_new_tokens}) and `max_length`(="
1204
+ f"{generation_config.max_length}) seem to have been set. `max_new_tokens` will take precedence. "
1205
+ "Please refer to the documentation for more information. "
1206
+ "(https://huggingface.co/docs/transformers/main/en/main_classes/text_generation)",
1207
+ UserWarning,
1208
+ )
1209
+
1210
+ if input_ids_seq_length >= generation_config.max_length:
1211
+ input_ids_string = "decoder_input_ids" if self.config.is_encoder_decoder else "input_ids"
1212
+ logger.warning(
1213
+ f"Input length of {input_ids_string} is {input_ids_seq_length}, but `max_length` is set to"
1214
+ f" {generation_config.max_length}. This can lead to unexpected behavior. You should consider"
1215
+ " increasing `max_new_tokens`."
1216
+ )
1217
+
1218
+ # 2. Set generation parameters if not already defined
1219
+ logits_processor = logits_processor if logits_processor is not None else LogitsProcessorList()
1220
+ stopping_criteria = stopping_criteria if stopping_criteria is not None else StoppingCriteriaList()
1221
+
1222
+ logits_processor = self._get_logits_processor(
1223
+ generation_config=generation_config,
1224
+ input_ids_seq_length=input_ids_seq_length,
1225
+ encoder_input_ids=input_ids,
1226
+ prefix_allowed_tokens_fn=prefix_allowed_tokens_fn,
1227
+ logits_processor=logits_processor,
1228
+ )
1229
+
1230
+ stopping_criteria = self._get_stopping_criteria(
1231
+ generation_config=generation_config, stopping_criteria=stopping_criteria
1232
+ )
1233
+ logits_warper = self._get_logits_warper(generation_config)
1234
+
1235
+ unfinished_sequences = input_ids.new(input_ids.shape[0]).fill_(1)
1236
+ scores = None
1237
+ while True:
1238
+ model_inputs = self.prepare_inputs_for_generation(input_ids, **model_kwargs)
1239
+ # forward pass to get next token
1240
+ outputs = self(
1241
+ **model_inputs,
1242
+ return_dict=True,
1243
+ output_attentions=False,
1244
+ output_hidden_states=False,
1245
+ )
1246
+
1247
+ next_token_logits = outputs.logits[:, -1, :]
1248
+
1249
+ # pre-process distribution
1250
+ next_token_scores = logits_processor(input_ids, next_token_logits)
1251
+ next_token_scores = logits_warper(input_ids, next_token_scores)
1252
+
1253
+ # sample
1254
+ probs = nn.functional.softmax(next_token_scores, dim=-1)
1255
+ if generation_config.do_sample:
1256
+ next_tokens = torch.multinomial(probs, num_samples=1).squeeze(1)
1257
+ else:
1258
+ next_tokens = torch.argmax(probs, dim=-1)
1259
+
1260
+ # update generated ids, model inputs, and length for next step
1261
+ input_ids = torch.cat([input_ids, next_tokens[:, None]], dim=-1)
1262
+ model_kwargs = self._update_model_kwargs_for_generation(
1263
+ outputs, model_kwargs, is_encoder_decoder=self.config.is_encoder_decoder
1264
+ )
1265
+ unfinished_sequences = unfinished_sequences.mul((sum(next_tokens != i for i in eos_token_id)).long())
1266
+
1267
+ # stop when each sentence is finished, or if we exceed the maximum length
1268
+ if unfinished_sequences.max() == 0 or stopping_criteria(input_ids, scores):
1269
+ break
1270
+ yield input_ids
1271
+
1272
+ def quantize(self, bits: int, quantize_embeddings=False, use_quantization_cache=False, empty_init=False, **kwargs):
1273
+ if bits == 0:
1274
+ return
1275
+
1276
+ from quantization import quantize, QuantizedEmbedding, QuantizedLinear, load_cpu_kernel
1277
+
1278
+ if self.quantized:
1279
+ if self.device == torch.device("cpu"):
1280
+ logger.info("Already quantized, reloading cpu kernel.")
1281
+ load_cpu_kernel(**kwargs)
1282
+ else:
1283
+ logger.info("Already quantized.")
1284
+ return self
1285
+
1286
+ self.quantized = True
1287
+
1288
+ self.config.quantization_bit = bits
1289
+ self.config.quantization_embeddings = quantize_embeddings
1290
+
1291
+ self.transformer = quantize(self.transformer, bits, use_quantization_cache=use_quantization_cache, empty_init=empty_init, **kwargs)
1292
+
1293
+ if quantize_embeddings:
1294
+ logger.info("Applying quantization to embeddings")
1295
+ self.transformer.word_embeddings = QuantizedEmbedding(
1296
+ weight_bit_width=bits,
1297
+ weight_tensor=self.transformer.word_embeddings.weight.to(self.device),
1298
+ num_embeddings=self.transformer.word_embeddings.num_embeddings,
1299
+ embedding_dim=self.transformer.word_embeddings.embedding_dim,
1300
+ dtype=torch.half,
1301
+ empty_init=True,
1302
+ device=self.transformer.word_embeddings.weight.device,
1303
+ )
1304
+ self.lm_head = QuantizedLinear(
1305
+ weight_bit_width=bits,
1306
+ weight_tensor=self.lm_head.weight.to(self.device),
1307
+ bias_tensor=None,
1308
+ in_features=self.lm_head.in_features,
1309
+ out_features=self.lm_head.out_features,
1310
+ bias=False,
1311
+ quantized_weight=self.transformer.word_embeddings.weight,
1312
+ quantized_weight_scale=self.transformer.word_embeddings.weight_scale,
1313
+ dtype=torch.half,
1314
+ empty_init=True,
1315
+ device=self.lm_head.weight.device,
1316
+ )
1317
+
1318
+ return self
quantization.py ADDED
@@ -0,0 +1,476 @@
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 import bz2
7
+ import torch
8
+ import base64
9
+ import ctypes
10
+
11
+ from typing import List
12
+ from functools import partial
13
+
14
+ try:
15
+ from cpm_kernels.kernels.base import LazyKernelCModule, KernelFunction, round_up
16
+
17
+ class Kernel:
18
+ def __init__(self, code: bytes, function_names: List[str]):
19
+ self.code = code
20
+ self._function_names = function_names
21
+ self._cmodule = LazyKernelCModule(self.code)
22
+
23
+ for name in self._function_names:
24
+ setattr(self, name, KernelFunction(self._cmodule, name))
25
+
26
+ quantization_code = "$QlpoOTFBWSZTWU9yuJUAQHN//////////f/n/8/n///n//bt4dTidcVx8X3V9FV/92/v4B7/AD5FBQFAAAChSgKpFCFAFVSigUAAAEKhSgUUqgFBKigqVREQAABQBQIANDTTIGI00BkZBkNGE0A0BkBkGQGRkaNAaAGQNBoGgDIAAYIGTI0DQAQAaGmmQMRpoDIyDIaMJoBoDIDIMgMjI0aA0AMgaDQNAGQAAwQMmRoGgAgA0NNMgYjTQGRkGQ0YTQDQGQGQZAZGRo0BoAZA0GgaAMgABggZMjQNABABoaaZAxGmgMjIMhowmgGgMgMgyAyMjRoDQAyBoNA0AZAADBAyZGgaAAmqU1NEgJqnptU/Sn4jRR6J6epk2pqb1Q/SgAPUGgyNNGjQ2SBpoAZAAGg0NB6mgDIAAAAA2oaApSREBNAARhGiYEaEwU8pvImlP0k2aam1GaGqbFNM1MHpTwmkepmyU9R6nqPKekHqNNPUxNGhp6n6p6QaZ6o9TG1GMqcoV9ly6nRanHlq6zPNbnGZNi6HSug+2nPiZ13XcnFYZW+45W11CumhzYhchOJ2GLLV1OBjBjGf4TptOddTSOcVxhqYZMYwZXZZY00zI1paX5X9J+b+f4e+x43RXSxXPOdquiGpduatGyXneN696M9t4HU2eR5XX/kPhP261NTx3JO1Ow7LyuDmeo9a7d351T1ZxnvnrvYnrXv/hXxPCeuYx2XsNmO003eg9J3Z6U7b23meJ4ri01OdzTk9BNO96brz+qT5nuvvH3ds/G+m/JcG/F2XYuhXlvO+jP7U3Xgrz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rxew929TR6bWnGL//F3JFOFCQT3K4lQ"
27
+
28
+ kernels = Kernel(
29
+ bz2.decompress(base64.b64decode(quantization_code)),
30
+ [
31
+ "int4WeightCompression",
32
+ "int4WeightExtractionFloat",
33
+ "int4WeightExtractionHalf",
34
+ "int8WeightExtractionFloat",
35
+ "int8WeightExtractionHalf",
36
+ ],
37
+ )
38
+ except Exception as exception:
39
+ kernels = None
40
+ print("Failed to load cpm_kernels:", exception)
41
+
42
+
43
+ class W8A16Linear(torch.autograd.Function):
44
+ @staticmethod
45
+ def forward(ctx, inp: torch.Tensor, quant_w: torch.Tensor, scale_w: torch.Tensor, weight_bit_width):
46
+ ctx.inp_shape = inp.size()
47
+ ctx.weight_shape = quant_w.size()
48
+ ctx.weight_bit_width = weight_bit_width
49
+ out_features = quant_w.size(0)
50
+ inp = inp.contiguous().view(-1, inp.size(-1))
51
+ weight = extract_weight_to_half(quant_w, scale_w, weight_bit_width)
52
+ output = inp.mm(weight.t())
53
+ ctx.save_for_backward(inp, quant_w, scale_w)
54
+ return output.view(*(ctx.inp_shape[:-1] + (out_features,)))
55
+
56
+ @staticmethod
57
+ def backward(ctx, grad_output: torch.Tensor):
58
+ inp, quant_w, scale_w = ctx.saved_tensors
59
+ weight = extract_weight_to_half(quant_w, scale_w, ctx.weight_bit_width)
60
+ grad_output = grad_output.contiguous().view(-1, weight.size(0))
61
+ grad_input = grad_output.mm(weight)
62
+ grad_weight = grad_output.t().mm(inp)
63
+ return grad_input.view(ctx.inp_shape), grad_weight.view(ctx.weight_shape), None
64
+
65
+
66
+ class W8A16LinearCPU(torch.autograd.Function):
67
+ @staticmethod
68
+ def forward(ctx, inp: torch.Tensor, quant_w: torch.Tensor, scale_w: torch.Tensor, weight_bit_width, quantization_cache=None):
69
+ ctx.inp_shape = inp.size()
70
+ ctx.weight_shape = quant_w.size()
71
+ ctx.weight_bit_width = weight_bit_width
72
+ out_features = quant_w.size(0)
73
+ inp = inp.contiguous().view(-1, inp.size(-1))
74
+ weight = extract_weight_to_float(quant_w, scale_w, weight_bit_width, quantization_cache=quantization_cache)
75
+ output = inp.mm(weight.t())
76
+ ctx.save_for_backward(inp, quant_w, scale_w)
77
+ return output.view(*(ctx.inp_shape[:-1] + (out_features,)))
78
+
79
+ @staticmethod
80
+ def backward(ctx, grad_output: torch.Tensor):
81
+ inp, quant_w, scale_w = ctx.saved_tensors
82
+ weight = extract_weight_to_float(quant_w, scale_w, ctx.weight_bit_width)
83
+ grad_output = grad_output.contiguous().view(-1, weight.size(0))
84
+ grad_input = grad_output.mm(weight)
85
+ grad_weight = grad_output.t().mm(inp)
86
+ return grad_input.view(ctx.inp_shape), grad_weight.view(ctx.weight_shape), None
87
+
88
+
89
+ default_cpu_kernel_code_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "quantization_kernels.c")
90
+ default_cpu_kernel_code = "QlpoOTFBWSZTWXLbSoQAAgzbgERwQXxmTwAAr/ff3kABt0Q2oRVT0hpo9RtEAAAAyBEiSQ9EGjQGQAAAwANGhowjJoNGmgMEUplMTNSMJ5TQaDJpsoMyRMj8P4mZzFSVVwqSXG8GG7MlVwiToYEQwVD7noBxMhNfkeZYtYFtbgOBUSIGtIQjhNHCEnPJsadhb3yBmRIOD3TeAtNLSaU5GgvKUBWSNuuOIHmVt0YhW6rsmDMDUjeUJGJ64R1Jm5lrh0Aa0tKjhFwPdWcGogxLDSXPWQUWTM8Sd3Qz1HMYNxx3HMeiNqNo4jeRDEfZ3gUSHIcU/heomq0vEzL1Msz5KKGxH8FrNOYw3KaxdqaEmNHYMxJFgQbR0DyRknL2L4kwUSxKRdhjRpEtUqilVfggFL1klaMS3PPRDfNqbBOPWO7m4JTVGhS9QTBDDJaEbLbrUQNB+IpJSKQbG5SZZ5gkwJEhJ3aYKJipZ/i7kinChIOW2lQg"
91
+ default_cpu_parallel_kernel_code_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "quantization_kernels_parallel.c")
92
+ default_cpu_parallel_kernel_code = "QlpoOTFBWSZTWZzWK2UAALXbgERwSX1mTwAAr/ff3kACNyXSbZYwBpoaNGIyAaADQwRRFT/UKDINANqAD1NABFQlPUzaaJHppGRmoAG01ARKKaaMp4gmgaNAaDQDIKVKfZ/g6v1Kem5ZsWZmZtSXS5ZwRAzKmjr1E1lKMEoQNCPkEYPACgcR5I9w/0k6JrJYHqFuHnChcD7N+DHeOQ0ajF83Tc40jgmQbOB5wt3TEHyTObDBLoxrJGBuJmNbxYZwAoKTjbIcI7GsbuVRERAR8wqwhXQjQOxiHQlgSnHjQjddXERojNmQYJJVoM2xxawMeI9asi6E1rfd7GO8S0S5vacCNGry4F1nyZbcTvSBXEMipuPfM7i0Y8kjirpbxb05jpIQjCGE8DYBNCAZyHz9EoOpDRST/I1aFCNpcjoXgyc3NjVsUvYIaYq7xopYJqcxg2g4qXofm7AaGNTzJSNguOQw4utKcEl0F1UOgI+T1hk5LusbGZ9udC1CiBeGwwFxR/QdbZDndehRPxyGt3Me1DBW45MXIY24ZD30aFNuSEUdu5LWx1sSJWLGgsmqUIFTgWhU0gfxXpzhghr2AYpV3hE06mGk1I2JyuZiFgkiz/i7kinChITmsVso"
93
+
94
+ cpu_kernels = None
95
+
96
+
97
+ class CPUKernel:
98
+ def __init__(self, kernel_file="", source_code=default_cpu_kernel_code_path, compile_parallel_kernel=None, parallel_num=None):
99
+ self.load =False
100
+ self.int8WeightExtractionFloat = None
101
+ self.int4WeightExtractionFloat = None
102
+ self.int4WeightCompression = None
103
+ self.SetNumThreads = None
104
+
105
+ try:
106
+ if not os.path.exists(default_cpu_kernel_code_path):
107
+ with open(default_cpu_kernel_code_path, "w", encoding="utf-8") as file:
108
+ code = default_cpu_kernel_code
109
+ cpu_quantization_code = bz2.decompress(base64.b64decode(code)).decode()
110
+ file.write(cpu_quantization_code)
111
+
112
+ if not os.path.exists(default_cpu_parallel_kernel_code_path):
113
+ with open(default_cpu_parallel_kernel_code_path, "w", encoding="utf-8") as file:
114
+ code = default_cpu_parallel_kernel_code
115
+ cpu_quantization_code = bz2.decompress(base64.b64decode(code)).decode()
116
+ file.write(cpu_quantization_code)
117
+
118
+ except Exception as ex:
119
+ print("Error when generating default cpu kernel code(can be ignored when using custom kernels).")
120
+
121
+ if compile_parallel_kernel is None:
122
+ compile_parallel_kernel = bool(int(os.cpu_count()) >= 4)
123
+
124
+ if compile_parallel_kernel and source_code == default_cpu_kernel_code_path:
125
+ source_code = default_cpu_parallel_kernel_code_path
126
+
127
+ if (not kernel_file) or (not os.path.exists(kernel_file)):
128
+ print("No compiled kernel found.")
129
+ try:
130
+ if os.path.exists(source_code):
131
+ print("Compiling kernels :", source_code)
132
+ kernel_file = source_code[:-2] + ".so"
133
+ if compile_parallel_kernel:
134
+ compile_command = "gcc -O3 -fPIC -pthread -fopenmp -std=c99 {} -shared -o {}".format(source_code, kernel_file)
135
+ print("Compiling", compile_command)
136
+ exit_state = os.system(compile_command)
137
+ if exit_state:
138
+ print("Compile failed, using default cpu kernel code.")
139
+ compile_parallel_kernel = False
140
+ source_code = default_cpu_kernel_code_path
141
+ kernel_file = source_code[:-2] + ".so"
142
+ compile_command = "gcc -O3 -fPIC -std=c99 {} -shared -o {}".format(source_code, kernel_file)
143
+ print("Compiling", compile_command)
144
+ else:
145
+ compile_command = "gcc -O3 -fPIC -std=c99 {} -shared -o {}".format(source_code, kernel_file)
146
+ print("Compiling", compile_command)
147
+ exit_state = os.system(compile_command)
148
+
149
+ print("Kernels compiled :", kernel_file)
150
+ else:
151
+ print("Kernel source code not found.")
152
+ return
153
+ except:
154
+ print("Failed to build kernel.")
155
+ return
156
+ if kernel_file:
157
+ kernels = ctypes.cdll.LoadLibrary(kernel_file)
158
+ self.int8WeightExtractionFloat = kernels.extract_int8_weight_to_float
159
+ self.int4WeightExtractionFloat = kernels.extract_int4_weight_to_float
160
+ self.int4WeightCompression = kernels.compress_int4_weight
161
+ if compile_parallel_kernel:
162
+ try:
163
+ self.SetNumThreads = kernels.set_num_threads
164
+ except:
165
+ print("No set_num_threads() found in kernel.")
166
+ self.SetNumThreads = lambda x: x
167
+ self.load = True
168
+ print("Load kernel :", kernel_file)
169
+ else:
170
+ print("Failed to load kernel.")
171
+
172
+ if compile_parallel_kernel:
173
+ if parallel_num is None:
174
+ parallel_num = max(os.cpu_count() // 2, 1)
175
+ print("Setting CPU quantization kernel threads to", parallel_num)
176
+ if parallel_num < 4:
177
+ print("Parallel kernel is not recommended when parallel num < 4.")
178
+ self.SetNumThreads(parallel_num)
179
+
180
+ self.parallel_num = parallel_num
181
+
182
+
183
+ def compress_int4_weight(weight: torch.Tensor): # (n, m)
184
+ """compress weight on cpu or cuda to int4"""
185
+ if weight.device == torch.device("cpu"):
186
+ assert isinstance(cpu_kernels, CPUKernel)
187
+ n, m = weight.size(0), weight.size(1)
188
+ assert m % 2 == 0
189
+ m = m // 2
190
+ out = torch.empty(n, m, dtype=torch.int8, device="cpu")
191
+ cpu_kernels.int4WeightCompression(
192
+ ctypes.c_void_p(weight.data_ptr()),
193
+ ctypes.c_void_p(out.data_ptr()),
194
+ ctypes.c_int32(n),
195
+ ctypes.c_int32(m)
196
+ )
197
+ return out
198
+ else:
199
+ with torch.cuda.device(weight.device):
200
+ n, m = weight.size(0), weight.size(1)
201
+ assert m % 2 == 0
202
+ m = m // 2
203
+ out = torch.empty(n, m, dtype=torch.int8, device="cuda")
204
+ stream = torch.cuda.current_stream()
205
+
206
+ gridDim = (n, 1, 1)
207
+ blockDim = (min(round_up(m, 32), 1024), 1, 1)
208
+
209
+ kernels.int4WeightCompression(
210
+ gridDim,
211
+ blockDim,
212
+ 0,
213
+ stream,
214
+ [ctypes.c_void_p(weight.data_ptr()), ctypes.c_void_p(out.data_ptr()), ctypes.c_int32(n), ctypes.c_int32(m)],
215
+ )
216
+ return out
217
+
218
+
219
+ def extract_weight_to_half(weight: torch.Tensor, scale_list: torch.Tensor, source_bit_width: int):
220
+ if source_bit_width == 8:
221
+ func = kernels.int8WeightExtractionHalf
222
+ elif source_bit_width == 4:
223
+ func = kernels.int4WeightExtractionHalf
224
+ else:
225
+ assert False, "Unsupported bit-width"
226
+
227
+ with torch.cuda.device(weight.device):
228
+ n, m = weight.size(0), weight.size(1)
229
+ out = torch.empty(n, m * (8 // source_bit_width), dtype=torch.half, device="cuda")
230
+ stream = torch.cuda.current_stream()
231
+
232
+ gridDim = (n, 1, 1)
233
+ blockDim = (min(round_up(m, 32), 1024), 1, 1)
234
+
235
+ func(
236
+ gridDim,
237
+ blockDim,
238
+ 0,
239
+ stream,
240
+ [
241
+ ctypes.c_void_p(weight.data_ptr()),
242
+ ctypes.c_void_p(scale_list.data_ptr()),
243
+ ctypes.c_void_p(out.data_ptr()),
244
+ ctypes.c_int32(n),
245
+ ctypes.c_int32(m),
246
+ ],
247
+ )
248
+ return out
249
+
250
+
251
+ def extract_weight_to_float(weight: torch.Tensor, scale_list: torch.Tensor, source_bit_width: int, quantization_cache=None):
252
+ """extract weight on cpu to float32"""
253
+ if source_bit_width == 8:
254
+ func = cpu_kernels.int8WeightExtractionFloat
255
+ elif source_bit_width == 4:
256
+ func = cpu_kernels.int4WeightExtractionFloat
257
+ else:
258
+ assert False, "Unsupported bit-width"
259
+
260
+ n, m = weight.size(0), weight.size(1)
261
+
262
+ if quantization_cache is not None:
263
+ out = quantization_cache
264
+ func(
265
+ ctypes.c_void_p(weight.data_ptr()),
266
+ ctypes.c_void_p(scale_list.data_ptr()),
267
+ ctypes.c_void_p(out.data_ptr()),
268
+ ctypes.c_int32(n),
269
+ ctypes.c_int32(m)
270
+ )
271
+ return out.tensor
272
+ else:
273
+ out = torch.empty(n, m * (8 // source_bit_width), dtype=torch.float, device="cpu")
274
+ func(
275
+ ctypes.c_void_p(weight.data_ptr()),
276
+ ctypes.c_void_p(scale_list.data_ptr()),
277
+ ctypes.c_void_p(out.data_ptr()),
278
+ ctypes.c_int32(n),
279
+ ctypes.c_int32(m)
280
+ )
281
+ return out
282
+
283
+
284
+ class CacheTensor():
285
+ def __init__(self, *args, **kwargs):
286
+ self.tensor = torch.empty(*args, **kwargs)
287
+
288
+ def to(self, *args, **kwargs):
289
+ self.tensor = self.tensor.to(*args, **kwargs)
290
+
291
+ def data_ptr(self):
292
+ return self.tensor.data_ptr()
293
+
294
+
295
+ class QuantizedLinear(Linear):
296
+ def __init__(self, weight_bit_width: int, weight_tensor=None, bias_tensor=None, quantized_weight=None, quantized_weight_scale=None, quantization_cache=None, empty_init=False, *args, **kwargs):
297
+ super(QuantizedLinear, self).__init__(*args, **kwargs)
298
+ self.weight_bit_width = weight_bit_width
299
+ self.quantization_cache = quantization_cache
300
+
301
+ if (quantized_weight is not None) and (quantized_weight_scale is not None):
302
+ del self.weight
303
+ self.weight = Parameter(quantized_weight.to(kwargs["device"]), requires_grad=False)
304
+ self.weight_scale = Parameter(quantized_weight_scale.to(kwargs["device"]), requires_grad=False)
305
+ else:
306
+ shape = self.weight.shape
307
+ del self.weight
308
+
309
+ if weight_tensor is None or empty_init:
310
+ self.weight = torch.empty(
311
+ shape[0], shape[1] * weight_bit_width // 8, dtype=torch.int8, device=kwargs["device"]
312
+ )
313
+ self.weight_scale = torch.empty(shape[0], dtype=kwargs["dtype"], device=kwargs["device"])
314
+ else:
315
+ self.weight_scale = (weight_tensor.abs().max(dim=-1).values / ((2 ** (weight_bit_width - 1)) - 1)).to(kwargs["dtype"])
316
+ self.weight = torch.round(weight_tensor / self.weight_scale[:, None]).to(torch.int8)
317
+ if weight_bit_width == 4:
318
+ self.weight = compress_int4_weight(self.weight)
319
+
320
+ self.weight = Parameter(self.weight.to(kwargs["device"]), requires_grad=False)
321
+ self.weight_scale = Parameter(self.weight_scale.to(kwargs["device"]), requires_grad=False)
322
+
323
+ if bias_tensor is not None:
324
+ self.bias = Parameter(bias_tensor.to(kwargs["device"]), requires_grad=False)
325
+ else:
326
+ self.bias = None
327
+
328
+ def reset_parameters(self):
329
+ """To accelerate initialization"""
330
+ pass
331
+
332
+ def forward(self, input):
333
+ if self.weight.device == torch.device("cpu"):
334
+ output = W8A16LinearCPU.apply(input, self.weight, self.weight_scale, self.weight_bit_width, self.quantization_cache)
335
+ else:
336
+ output = W8A16Linear.apply(input, self.weight, self.weight_scale, self.weight_bit_width)
337
+ if self.bias is not None:
338
+ output = output + self.bias
339
+ return output
340
+
341
+ def _apply(self, fn):
342
+ self_obj = super()._apply(fn)
343
+ if self.quantization_cache is not None:
344
+ self.quantization_cache.to(self_obj.weight.device)
345
+ self.quantization_cache.to(self_obj.weight_scale.dtype)
346
+ return self_obj
347
+
348
+
349
+ class QuantizedEmbedding(Embedding): # TODO: backward, check empty_init
350
+ def __init__(self, weight_bit_width: int, weight_tensor=None, quantized_weight=None, quantized_weight_scale=None, empty_init=False, *args, **kwargs):
351
+ super(QuantizedEmbedding, self).__init__(*args, **kwargs)
352
+ self.weight_bit_width = weight_bit_width
353
+
354
+ if (quantized_weight is not None) and (quantized_weight_scale is not None):
355
+ del self.weight
356
+ self.weight = Parameter(quantized_weight.to(kwargs["device"]), requires_grad=False)
357
+ self.weight_scale = Parameter(quantized_weight_scale.to(kwargs["device"]), requires_grad=False)
358
+ else:
359
+ shape = self.weight.shape
360
+ del self.weight
361
+
362
+ if weight_tensor is None or empty_init:
363
+ self.weight = torch.empty(
364
+ shape[0], shape[1] * weight_bit_width // 8, dtype=torch.int8, device=kwargs["device"]
365
+ )
366
+ self.weight_scale = torch.empty(shape[0], dtype=kwargs["dtype"], device=kwargs["device"])
367
+ else:
368
+ self.weight_scale = (weight_tensor.abs().max(dim=-1).values / ((2 ** (weight_bit_width - 1)) - 1)).half()
369
+ self.weight = torch.round(weight_tensor / self.weight_scale[:, None]).to(torch.int8)
370
+ if weight_bit_width == 4:
371
+ self.weight = compress_int4_weight(self.weight)
372
+
373
+ self.weight = Parameter(self.weight.to(kwargs["device"]), requires_grad=False)
374
+ self.weight_scale = Parameter(self.weight_scale.to(kwargs["device"]), requires_grad=False)
375
+
376
+ def forward(self, input):
377
+ if self.weight.device == torch.device("cpu"):
378
+ original_weight = extract_weight_to_float(weight=self.weight, scale_list=self.weight_scale, source_bit_width=self.weight_bit_width)
379
+ else:
380
+ original_weight = extract_weight_to_half(weight=self.weight, scale_list=self.weight_scale, source_bit_width=self.weight_bit_width)
381
+ output = F.embedding(
382
+ input, original_weight, self.padding_idx, self.max_norm,
383
+ self.norm_type, self.scale_grad_by_freq, self.sparse
384
+ )
385
+ return output
386
+
387
+
388
+ def load_cpu_kernel(**kwargs):
389
+ global cpu_kernels
390
+ cpu_kernels = CPUKernel(**kwargs)
391
+ assert cpu_kernels.load
392
+
393
+
394
+ def quantize(model, weight_bit_width, use_quantization_cache=False, empty_init=False, **kwargs):
395
+ """Replace fp16 linear with quantized linear"""
396
+
397
+ query_key_value_quantization_cache = None
398
+ dense_quantization_cache = None
399
+ dense_h_to_4h_quantization_cache = None
400
+ dense_4h_to_h_quantization_cache = None
401
+
402
+ try:
403
+ load_cpu_kernel(**kwargs)
404
+ except:
405
+ print("Cannot load cpu kernel, don't use quantized model on cpu.")
406
+ if kernels is None: # CUDA kernels failed
407
+ print("Cannot load cuda kernel, quantization failed.")
408
+ return model
409
+
410
+ current_device = model.device
411
+
412
+ if model.device == torch.device("cpu"):
413
+ dtype=torch.float32
414
+ else:
415
+ dtype = torch.half
416
+
417
+ QuantizedLinearWithPara = partial(
418
+ QuantizedLinear,
419
+ weight_bit_width=weight_bit_width,
420
+ bias=True,
421
+ dtype=dtype,
422
+ empty_init=empty_init
423
+ )
424
+
425
+ if use_quantization_cache:
426
+ print("Using quantization cache")
427
+ layer = model.layers[0]
428
+ weight = layer.attention.query_key_value.weight
429
+ n, m = weight.size(0), weight.size(1)
430
+ query_key_value_quantization_cache = CacheTensor(n, m, dtype=dtype, device=current_device, requires_grad=False)
431
+ weight = layer.attention.dense.weight
432
+ n, m = weight.size(0), weight.size(1)
433
+ dense_quantization_cache = CacheTensor(n, m, dtype=dtype, device=current_device, requires_grad=False)
434
+ weight = layer.mlp.dense_h_to_4h.weight
435
+ n, m = weight.size(0), weight.size(1)
436
+ dense_h_to_4h_quantization_cache = CacheTensor(n, m, dtype=dtype, device=current_device, requires_grad=False)
437
+ weight = layer.mlp.dense_4h_to_h.weight
438
+ n, m = weight.size(0), weight.size(1)
439
+ dense_4h_to_h_quantization_cache = CacheTensor(n, m, dtype=dtype, device=current_device, requires_grad=False)
440
+
441
+ print("Applying quantization to glm layers")
442
+
443
+ for layer in model.layers:
444
+ layer.attention.query_key_value = QuantizedLinearWithPara(
445
+ weight_tensor=layer.attention.query_key_value.weight.to(current_device),
446
+ bias_tensor=layer.attention.query_key_value.bias,
447
+ in_features=layer.attention.query_key_value.in_features,
448
+ out_features=layer.attention.query_key_value.out_features,
449
+ device=layer.attention.query_key_value.weight.device,
450
+ quantization_cache=query_key_value_quantization_cache
451
+ )
452
+ layer.attention.dense = QuantizedLinearWithPara(
453
+ weight_tensor=layer.attention.dense.weight.to(current_device),
454
+ bias_tensor=layer.attention.dense.bias,
455
+ in_features=layer.attention.dense.in_features,
456
+ out_features=layer.attention.dense.out_features,
457
+ device=layer.attention.dense.weight.device,
458
+ quantization_cache=dense_quantization_cache
459
+ )
460
+ layer.mlp.dense_h_to_4h = QuantizedLinearWithPara(
461
+ weight_tensor=layer.mlp.dense_h_to_4h.weight.to(current_device),
462
+ bias_tensor=layer.mlp.dense_h_to_4h.bias,
463
+ in_features=layer.mlp.dense_h_to_4h.in_features,
464
+ out_features=layer.mlp.dense_h_to_4h.out_features,
465
+ device=layer.mlp.dense_h_to_4h.weight.device,
466
+ quantization_cache=dense_h_to_4h_quantization_cache
467
+ )
468
+ layer.mlp.dense_4h_to_h = QuantizedLinearWithPara(
469
+ weight_tensor=layer.mlp.dense_4h_to_h.weight.to(current_device),
470
+ bias_tensor=layer.mlp.dense_4h_to_h.bias,
471
+ in_features=layer.mlp.dense_4h_to_h.in_features,
472
+ out_features=layer.mlp.dense_4h_to_h.out_features,
473
+ device=layer.mlp.dense_4h_to_h.weight.device,
474
+ quantization_cache=dense_4h_to_h_quantization_cache
475
+ )
476
+ return model
quantization_kernels.c ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ void compress_int4_weight(void *weight, void *out, int n, int m)
2
+ {
3
+ for(int i=0;i<n*m;i++)
4
+ {
5
+ (*(unsigned char*)(out)) = ((*(unsigned char*)(weight)) << 4);
6
+ weight += sizeof(char);
7
+ (*(unsigned char*)(out)) |= ((*(unsigned char*)(weight)) & 15);
8
+ weight += sizeof(char);
9
+ out += sizeof(char);
10
+ }
11
+ }
12
+
13
+ void extract_int8_weight_to_float(void *weight, void *scale_list, void *out, int n, int m)
14
+ {
15
+ for(int i=0;i<n;i++)
16
+ for(int j=0;j<m;j++)
17
+ (*(float*)(out + sizeof(float) * (i * m + j))) = (*(float*)(scale_list + sizeof(float) * i)) * (*(char*)(weight + sizeof(char) * (i * m + j)));
18
+ }
19
+
20
+ void extract_int4_weight_to_float(void *weight, void *scale_list, void *out, int n, int m)
21
+ {
22
+ for(int i=0;i<n;i++)
23
+ {
24
+ for(int j=0;j<m;j++)
25
+ {
26
+ (*(float*)(out)) = (*(float*)(scale_list)) * ((*(char*)(weight)) >> 4);
27
+ out += sizeof(float);
28
+ (*(float*)(out)) = (*(float*)(scale_list)) * (((char)((*(unsigned char*)(weight)) << 4))>> 4);
29
+ out += sizeof(float);
30
+ weight += sizeof(char);
31
+ }
32
+ scale_list += sizeof(float);
33
+ }
34
+ }
quantization_kernels_parallel.c ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <omp.h>
2
+
3
+ void set_num_threads(int n_threads)
4
+ {
5
+ omp_set_num_threads(n_threads);
6
+ }
7
+
8
+ int get_num_threads()
9
+ {
10
+ return omp_get_num_threads();
11
+ }
12
+
13
+ void compress_int4_weight(void *weight, void *out, int n, int m)
14
+ {
15
+ #pragma omp parallel for
16
+ for(int i=0;i<n;i++)
17
+ {
18
+ for(int j=0;j<m;j++)
19
+ {
20
+ (*(unsigned char*)(out + sizeof(unsigned char) * (i * m + j))) |= ((*(unsigned char*)(weight + sizeof(unsigned char) * (i * (m << 1) + (j << 1)))) << 4);
21
+ (*(unsigned char*)(out + sizeof(unsigned char) * (i * m + j))) |= (((*(unsigned char*)(weight + sizeof(unsigned char) * (i * (m << 1) + ((j << 1) | 1)))) & 15));
22
+ }
23
+ }
24
+ }
25
+
26
+ void extract_int8_weight_to_float(void *weight, void *scale_list, void *out, int n, int m)
27
+ {
28
+ #pragma omp parallel for
29
+ for(int i=0;i<n;i++)
30
+ {
31
+ for(int j=0;j<m;j++)
32
+ (*(float*)(out + sizeof(float) * (i * m + j))) = (*(float*)(scale_list + sizeof(float) * i)) * (*(char*)(weight + sizeof(char) * (i * m + j)));
33
+ }
34
+ }
35
+
36
+ void extract_int4_weight_to_float(void *weight, void *scale_list, void *out, int n, int m)
37
+ {
38
+ #pragma omp parallel for
39
+ for(int i=0;i<n;i++)
40
+ {
41
+ for(int j=0;j<m;j++)
42
+ {
43
+ (*(float*)(out + sizeof(float) * (i * (m << 1) + (j << 1)))) = (*(float*)(scale_list + sizeof(float) * i)) * ((*(char*)(weight + sizeof(char) * (i * m + j))) >> 4);
44
+ (*(float*)(out + sizeof(float) * (i * (m << 1) + ((j << 1) | 1)))) = (*(float*)(scale_list + sizeof(float) * i)) * (((char)((*(unsigned char*)(weight + sizeof(char) * (i * m + j))) << 4))>> 4);
45
+ }
46
+ }
47
+ }