add finGPT_merged

This commit is contained in:
mjchen6 2023-10-07 17:50:37 +08:00
parent 6fa776047d
commit 8ec6fa4eae
26 changed files with 2012 additions and 0 deletions

1
.gitattributes vendored Normal file
View File

@ -0,0 +1 @@
*.bin filter=lfs diff=lfs merge=lfs -text

44
config.json Normal file
View File

@ -0,0 +1,44 @@
{
"_name_or_path": "/home/sdk_models/chatglm2_6b/",
"add_bias_linear": false,
"add_qkv_bias": true,
"apply_query_key_layer_scaling": true,
"apply_residual_connection_post_layernorm": false,
"architectures": [
"ChatGLMForConditionalGeneration"
],
"attention_dropout": 0.0,
"attention_softmax_in_fp32": true,
"auto_map": {
"AutoConfig": "configuration_chatglm.ChatGLMConfig",
"AutoModel": "modeling_chatglm.ChatGLMForConditionalGeneration",
"AutoModelForSeq2SeqLM": "modeling_chatglm.ChatGLMForConditionalGeneration"
},
"bias_dropout_fusion": true,
"eos_token_id": 2,
"ffn_hidden_size": 13696,
"fp32_residual_connection": false,
"hidden_dropout": 0.0,
"hidden_size": 4096,
"kv_channels": 128,
"layernorm_epsilon": 1e-05,
"model_type": "chatglm",
"multi_query_attention": true,
"multi_query_group_num": 2,
"num_attention_heads": 32,
"num_layers": 28,
"original_rope": true,
"pad_token_id": 0,
"padded_vocab_size": 65024,
"post_layer_norm": true,
"pre_seq_len": null,
"prefix_projection": false,
"quantization_bit": 0,
"rmsnorm": true,
"seq_length": 32768,
"tie_word_embeddings": false,
"torch_dtype": "float16",
"transformers_version": "4.31.0",
"use_cache": true,
"vocab_size": 65024
}

60
configuration_chatglm.py Normal file
View File

@ -0,0 +1,60 @@
from transformers import PretrainedConfig
class ChatGLMConfig(PretrainedConfig):
model_type = "chatglm"
def __init__(
self,
num_layers=28,
padded_vocab_size=65024,
hidden_size=4096,
ffn_hidden_size=13696,
kv_channels=128,
num_attention_heads=32,
seq_length=2048,
hidden_dropout=0.0,
attention_dropout=0.0,
layernorm_epsilon=1e-5,
rmsnorm=True,
apply_residual_connection_post_layernorm=False,
post_layer_norm=True,
add_bias_linear=False,
add_qkv_bias=False,
interleaved_qkv=False,
bias_dropout_fusion=True,
multi_query_attention=False,
multi_query_group_num=1,
apply_query_key_layer_scaling=True,
attention_softmax_in_fp32=True,
fp32_residual_connection=False,
quantization_bit=0,
pre_seq_len=None,
prefix_projection=False,
**kwargs
):
self.num_layers = num_layers
self.vocab_size = padded_vocab_size
self.padded_vocab_size = padded_vocab_size
self.hidden_size = hidden_size
self.ffn_hidden_size = ffn_hidden_size
self.kv_channels = kv_channels
self.num_attention_heads = num_attention_heads
self.seq_length = seq_length
self.hidden_dropout = hidden_dropout
self.attention_dropout = attention_dropout
self.layernorm_epsilon = layernorm_epsilon
self.rmsnorm = rmsnorm
self.apply_residual_connection_post_layernorm = apply_residual_connection_post_layernorm
self.post_layer_norm = post_layer_norm
self.add_bias_linear = add_bias_linear
self.add_qkv_bias = add_qkv_bias
self.bias_dropout_fusion = bias_dropout_fusion
self.multi_query_attention = multi_query_attention
self.multi_query_group_num = multi_query_group_num
self.apply_query_key_layer_scaling = apply_query_key_layer_scaling
self.attention_softmax_in_fp32 = attention_softmax_in_fp32
self.fp32_residual_connection = fp32_residual_connection
self.quantization_bit = quantization_bit
self.pre_seq_len = pre_seq_len
self.prefix_projection = prefix_projection
super().__init__(**kwargs)

6
generation_config.json Normal file
View File

@ -0,0 +1,6 @@
{
"_from_model_config": true,
"eos_token_id": 2,
"pad_token_id": 0,
"transformers_version": "4.31.0"
}

1192
modeling_chatglm.py Normal file

File diff suppressed because it is too large Load Diff

BIN
pytorch_model-00001-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

BIN
pytorch_model-00002-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

BIN
pytorch_model-00003-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

BIN
pytorch_model-00004-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

BIN
pytorch_model-00005-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

BIN
pytorch_model-00006-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

BIN
pytorch_model-00007-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

BIN
pytorch_model-00008-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

BIN
pytorch_model-00009-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

BIN
pytorch_model-00010-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

BIN
pytorch_model-00011-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

BIN
pytorch_model-00012-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

BIN
pytorch_model-00013-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

BIN
pytorch_model-00014-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

BIN
pytorch_model-00015-of-00015.bin (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -0,0 +1,208 @@
{
"metadata": {
"total_size": 12487168064
},
"weight_map": {
"lm_head.weight": "pytorch_model-00015-of-00015.bin",
"transformer.embedding.word_embeddings.weight": "pytorch_model-00001-of-00015.bin",
"transformer.encoder.final_layernorm.weight": "pytorch_model-00015-of-00015.bin",
"transformer.encoder.layers.0.input_layernorm.weight": "pytorch_model-00001-of-00015.bin",
"transformer.encoder.layers.0.mlp.dense_4h_to_h.weight": "pytorch_model-00001-of-00015.bin",
"transformer.encoder.layers.0.mlp.dense_h_to_4h.weight": "pytorch_model-00001-of-00015.bin",
"transformer.encoder.layers.0.post_attention_layernorm.weight": "pytorch_model-00001-of-00015.bin",
"transformer.encoder.layers.0.self_attention.dense.weight": "pytorch_model-00001-of-00015.bin",
"transformer.encoder.layers.0.self_attention.query_key_value.bias": "pytorch_model-00001-of-00015.bin",
"transformer.encoder.layers.0.self_attention.query_key_value.weight": "pytorch_model-00001-of-00015.bin",
"transformer.encoder.layers.1.input_layernorm.weight": "pytorch_model-00001-of-00015.bin",
"transformer.encoder.layers.1.mlp.dense_4h_to_h.weight": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.1.mlp.dense_h_to_4h.weight": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.1.post_attention_layernorm.weight": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.1.self_attention.dense.weight": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.1.self_attention.query_key_value.bias": "pytorch_model-00001-of-00015.bin",
"transformer.encoder.layers.1.self_attention.query_key_value.weight": "pytorch_model-00001-of-00015.bin",
"transformer.encoder.layers.10.input_layernorm.weight": "pytorch_model-00006-of-00015.bin",
"transformer.encoder.layers.10.mlp.dense_4h_to_h.weight": "pytorch_model-00006-of-00015.bin",
"transformer.encoder.layers.10.mlp.dense_h_to_4h.weight": "pytorch_model-00006-of-00015.bin",
"transformer.encoder.layers.10.post_attention_layernorm.weight": "pytorch_model-00006-of-00015.bin",
"transformer.encoder.layers.10.self_attention.dense.weight": "pytorch_model-00006-of-00015.bin",
"transformer.encoder.layers.10.self_attention.query_key_value.bias": "pytorch_model-00006-of-00015.bin",
"transformer.encoder.layers.10.self_attention.query_key_value.weight": "pytorch_model-00006-of-00015.bin",
"transformer.encoder.layers.11.input_layernorm.weight": "pytorch_model-00006-of-00015.bin",
"transformer.encoder.layers.11.mlp.dense_4h_to_h.weight": "pytorch_model-00007-of-00015.bin",
"transformer.encoder.layers.11.mlp.dense_h_to_4h.weight": "pytorch_model-00007-of-00015.bin",
"transformer.encoder.layers.11.post_attention_layernorm.weight": "pytorch_model-00006-of-00015.bin",
"transformer.encoder.layers.11.self_attention.dense.weight": "pytorch_model-00006-of-00015.bin",
"transformer.encoder.layers.11.self_attention.query_key_value.bias": "pytorch_model-00006-of-00015.bin",
"transformer.encoder.layers.11.self_attention.query_key_value.weight": "pytorch_model-00006-of-00015.bin",
"transformer.encoder.layers.12.input_layernorm.weight": "pytorch_model-00007-of-00015.bin",
"transformer.encoder.layers.12.mlp.dense_4h_to_h.weight": "pytorch_model-00007-of-00015.bin",
"transformer.encoder.layers.12.mlp.dense_h_to_4h.weight": "pytorch_model-00007-of-00015.bin",
"transformer.encoder.layers.12.post_attention_layernorm.weight": "pytorch_model-00007-of-00015.bin",
"transformer.encoder.layers.12.self_attention.dense.weight": "pytorch_model-00007-of-00015.bin",
"transformer.encoder.layers.12.self_attention.query_key_value.bias": "pytorch_model-00007-of-00015.bin",
"transformer.encoder.layers.12.self_attention.query_key_value.weight": "pytorch_model-00007-of-00015.bin",
"transformer.encoder.layers.13.input_layernorm.weight": "pytorch_model-00007-of-00015.bin",
"transformer.encoder.layers.13.mlp.dense_4h_to_h.weight": "pytorch_model-00008-of-00015.bin",
"transformer.encoder.layers.13.mlp.dense_h_to_4h.weight": "pytorch_model-00008-of-00015.bin",
"transformer.encoder.layers.13.post_attention_layernorm.weight": "pytorch_model-00007-of-00015.bin",
"transformer.encoder.layers.13.self_attention.dense.weight": "pytorch_model-00007-of-00015.bin",
"transformer.encoder.layers.13.self_attention.query_key_value.bias": "pytorch_model-00007-of-00015.bin",
"transformer.encoder.layers.13.self_attention.query_key_value.weight": "pytorch_model-00007-of-00015.bin",
"transformer.encoder.layers.14.input_layernorm.weight": "pytorch_model-00008-of-00015.bin",
"transformer.encoder.layers.14.mlp.dense_4h_to_h.weight": "pytorch_model-00008-of-00015.bin",
"transformer.encoder.layers.14.mlp.dense_h_to_4h.weight": "pytorch_model-00008-of-00015.bin",
"transformer.encoder.layers.14.post_attention_layernorm.weight": "pytorch_model-00008-of-00015.bin",
"transformer.encoder.layers.14.self_attention.dense.weight": "pytorch_model-00008-of-00015.bin",
"transformer.encoder.layers.14.self_attention.query_key_value.bias": "pytorch_model-00008-of-00015.bin",
"transformer.encoder.layers.14.self_attention.query_key_value.weight": "pytorch_model-00008-of-00015.bin",
"transformer.encoder.layers.15.input_layernorm.weight": "pytorch_model-00008-of-00015.bin",
"transformer.encoder.layers.15.mlp.dense_4h_to_h.weight": "pytorch_model-00009-of-00015.bin",
"transformer.encoder.layers.15.mlp.dense_h_to_4h.weight": "pytorch_model-00009-of-00015.bin",
"transformer.encoder.layers.15.post_attention_layernorm.weight": "pytorch_model-00008-of-00015.bin",
"transformer.encoder.layers.15.self_attention.dense.weight": "pytorch_model-00008-of-00015.bin",
"transformer.encoder.layers.15.self_attention.query_key_value.bias": "pytorch_model-00008-of-00015.bin",
"transformer.encoder.layers.15.self_attention.query_key_value.weight": "pytorch_model-00008-of-00015.bin",
"transformer.encoder.layers.16.input_layernorm.weight": "pytorch_model-00009-of-00015.bin",
"transformer.encoder.layers.16.mlp.dense_4h_to_h.weight": "pytorch_model-00009-of-00015.bin",
"transformer.encoder.layers.16.mlp.dense_h_to_4h.weight": "pytorch_model-00009-of-00015.bin",
"transformer.encoder.layers.16.post_attention_layernorm.weight": "pytorch_model-00009-of-00015.bin",
"transformer.encoder.layers.16.self_attention.dense.weight": "pytorch_model-00009-of-00015.bin",
"transformer.encoder.layers.16.self_attention.query_key_value.bias": "pytorch_model-00009-of-00015.bin",
"transformer.encoder.layers.16.self_attention.query_key_value.weight": "pytorch_model-00009-of-00015.bin",
"transformer.encoder.layers.17.input_layernorm.weight": "pytorch_model-00009-of-00015.bin",
"transformer.encoder.layers.17.mlp.dense_4h_to_h.weight": "pytorch_model-00010-of-00015.bin",
"transformer.encoder.layers.17.mlp.dense_h_to_4h.weight": "pytorch_model-00010-of-00015.bin",
"transformer.encoder.layers.17.post_attention_layernorm.weight": "pytorch_model-00009-of-00015.bin",
"transformer.encoder.layers.17.self_attention.dense.weight": "pytorch_model-00009-of-00015.bin",
"transformer.encoder.layers.17.self_attention.query_key_value.bias": "pytorch_model-00009-of-00015.bin",
"transformer.encoder.layers.17.self_attention.query_key_value.weight": "pytorch_model-00009-of-00015.bin",
"transformer.encoder.layers.18.input_layernorm.weight": "pytorch_model-00010-of-00015.bin",
"transformer.encoder.layers.18.mlp.dense_4h_to_h.weight": "pytorch_model-00010-of-00015.bin",
"transformer.encoder.layers.18.mlp.dense_h_to_4h.weight": "pytorch_model-00010-of-00015.bin",
"transformer.encoder.layers.18.post_attention_layernorm.weight": "pytorch_model-00010-of-00015.bin",
"transformer.encoder.layers.18.self_attention.dense.weight": "pytorch_model-00010-of-00015.bin",
"transformer.encoder.layers.18.self_attention.query_key_value.bias": "pytorch_model-00010-of-00015.bin",
"transformer.encoder.layers.18.self_attention.query_key_value.weight": "pytorch_model-00010-of-00015.bin",
"transformer.encoder.layers.19.input_layernorm.weight": "pytorch_model-00010-of-00015.bin",
"transformer.encoder.layers.19.mlp.dense_4h_to_h.weight": "pytorch_model-00011-of-00015.bin",
"transformer.encoder.layers.19.mlp.dense_h_to_4h.weight": "pytorch_model-00011-of-00015.bin",
"transformer.encoder.layers.19.post_attention_layernorm.weight": "pytorch_model-00010-of-00015.bin",
"transformer.encoder.layers.19.self_attention.dense.weight": "pytorch_model-00010-of-00015.bin",
"transformer.encoder.layers.19.self_attention.query_key_value.bias": "pytorch_model-00010-of-00015.bin",
"transformer.encoder.layers.19.self_attention.query_key_value.weight": "pytorch_model-00010-of-00015.bin",
"transformer.encoder.layers.2.input_layernorm.weight": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.2.mlp.dense_4h_to_h.weight": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.2.mlp.dense_h_to_4h.weight": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.2.post_attention_layernorm.weight": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.2.self_attention.dense.weight": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.2.self_attention.query_key_value.bias": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.2.self_attention.query_key_value.weight": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.20.input_layernorm.weight": "pytorch_model-00011-of-00015.bin",
"transformer.encoder.layers.20.mlp.dense_4h_to_h.weight": "pytorch_model-00011-of-00015.bin",
"transformer.encoder.layers.20.mlp.dense_h_to_4h.weight": "pytorch_model-00011-of-00015.bin",
"transformer.encoder.layers.20.post_attention_layernorm.weight": "pytorch_model-00011-of-00015.bin",
"transformer.encoder.layers.20.self_attention.dense.weight": "pytorch_model-00011-of-00015.bin",
"transformer.encoder.layers.20.self_attention.query_key_value.bias": "pytorch_model-00011-of-00015.bin",
"transformer.encoder.layers.20.self_attention.query_key_value.weight": "pytorch_model-00011-of-00015.bin",
"transformer.encoder.layers.21.input_layernorm.weight": "pytorch_model-00011-of-00015.bin",
"transformer.encoder.layers.21.mlp.dense_4h_to_h.weight": "pytorch_model-00012-of-00015.bin",
"transformer.encoder.layers.21.mlp.dense_h_to_4h.weight": "pytorch_model-00012-of-00015.bin",
"transformer.encoder.layers.21.post_attention_layernorm.weight": "pytorch_model-00011-of-00015.bin",
"transformer.encoder.layers.21.self_attention.dense.weight": "pytorch_model-00011-of-00015.bin",
"transformer.encoder.layers.21.self_attention.query_key_value.bias": "pytorch_model-00011-of-00015.bin",
"transformer.encoder.layers.21.self_attention.query_key_value.weight": "pytorch_model-00011-of-00015.bin",
"transformer.encoder.layers.22.input_layernorm.weight": "pytorch_model-00012-of-00015.bin",
"transformer.encoder.layers.22.mlp.dense_4h_to_h.weight": "pytorch_model-00012-of-00015.bin",
"transformer.encoder.layers.22.mlp.dense_h_to_4h.weight": "pytorch_model-00012-of-00015.bin",
"transformer.encoder.layers.22.post_attention_layernorm.weight": "pytorch_model-00012-of-00015.bin",
"transformer.encoder.layers.22.self_attention.dense.weight": "pytorch_model-00012-of-00015.bin",
"transformer.encoder.layers.22.self_attention.query_key_value.bias": "pytorch_model-00012-of-00015.bin",
"transformer.encoder.layers.22.self_attention.query_key_value.weight": "pytorch_model-00012-of-00015.bin",
"transformer.encoder.layers.23.input_layernorm.weight": "pytorch_model-00012-of-00015.bin",
"transformer.encoder.layers.23.mlp.dense_4h_to_h.weight": "pytorch_model-00013-of-00015.bin",
"transformer.encoder.layers.23.mlp.dense_h_to_4h.weight": "pytorch_model-00013-of-00015.bin",
"transformer.encoder.layers.23.post_attention_layernorm.weight": "pytorch_model-00012-of-00015.bin",
"transformer.encoder.layers.23.self_attention.dense.weight": "pytorch_model-00012-of-00015.bin",
"transformer.encoder.layers.23.self_attention.query_key_value.bias": "pytorch_model-00012-of-00015.bin",
"transformer.encoder.layers.23.self_attention.query_key_value.weight": "pytorch_model-00012-of-00015.bin",
"transformer.encoder.layers.24.input_layernorm.weight": "pytorch_model-00013-of-00015.bin",
"transformer.encoder.layers.24.mlp.dense_4h_to_h.weight": "pytorch_model-00013-of-00015.bin",
"transformer.encoder.layers.24.mlp.dense_h_to_4h.weight": "pytorch_model-00013-of-00015.bin",
"transformer.encoder.layers.24.post_attention_layernorm.weight": "pytorch_model-00013-of-00015.bin",
"transformer.encoder.layers.24.self_attention.dense.weight": "pytorch_model-00013-of-00015.bin",
"transformer.encoder.layers.24.self_attention.query_key_value.bias": "pytorch_model-00013-of-00015.bin",
"transformer.encoder.layers.24.self_attention.query_key_value.weight": "pytorch_model-00013-of-00015.bin",
"transformer.encoder.layers.25.input_layernorm.weight": "pytorch_model-00013-of-00015.bin",
"transformer.encoder.layers.25.mlp.dense_4h_to_h.weight": "pytorch_model-00014-of-00015.bin",
"transformer.encoder.layers.25.mlp.dense_h_to_4h.weight": "pytorch_model-00014-of-00015.bin",
"transformer.encoder.layers.25.post_attention_layernorm.weight": "pytorch_model-00013-of-00015.bin",
"transformer.encoder.layers.25.self_attention.dense.weight": "pytorch_model-00013-of-00015.bin",
"transformer.encoder.layers.25.self_attention.query_key_value.bias": "pytorch_model-00013-of-00015.bin",
"transformer.encoder.layers.25.self_attention.query_key_value.weight": "pytorch_model-00013-of-00015.bin",
"transformer.encoder.layers.26.input_layernorm.weight": "pytorch_model-00014-of-00015.bin",
"transformer.encoder.layers.26.mlp.dense_4h_to_h.weight": "pytorch_model-00014-of-00015.bin",
"transformer.encoder.layers.26.mlp.dense_h_to_4h.weight": "pytorch_model-00014-of-00015.bin",
"transformer.encoder.layers.26.post_attention_layernorm.weight": "pytorch_model-00014-of-00015.bin",
"transformer.encoder.layers.26.self_attention.dense.weight": "pytorch_model-00014-of-00015.bin",
"transformer.encoder.layers.26.self_attention.query_key_value.bias": "pytorch_model-00014-of-00015.bin",
"transformer.encoder.layers.26.self_attention.query_key_value.weight": "pytorch_model-00014-of-00015.bin",
"transformer.encoder.layers.27.input_layernorm.weight": "pytorch_model-00014-of-00015.bin",
"transformer.encoder.layers.27.mlp.dense_4h_to_h.weight": "pytorch_model-00015-of-00015.bin",
"transformer.encoder.layers.27.mlp.dense_h_to_4h.weight": "pytorch_model-00015-of-00015.bin",
"transformer.encoder.layers.27.post_attention_layernorm.weight": "pytorch_model-00014-of-00015.bin",
"transformer.encoder.layers.27.self_attention.dense.weight": "pytorch_model-00014-of-00015.bin",
"transformer.encoder.layers.27.self_attention.query_key_value.bias": "pytorch_model-00014-of-00015.bin",
"transformer.encoder.layers.27.self_attention.query_key_value.weight": "pytorch_model-00014-of-00015.bin",
"transformer.encoder.layers.3.input_layernorm.weight": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.3.mlp.dense_4h_to_h.weight": "pytorch_model-00003-of-00015.bin",
"transformer.encoder.layers.3.mlp.dense_h_to_4h.weight": "pytorch_model-00003-of-00015.bin",
"transformer.encoder.layers.3.post_attention_layernorm.weight": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.3.self_attention.dense.weight": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.3.self_attention.query_key_value.bias": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.3.self_attention.query_key_value.weight": "pytorch_model-00002-of-00015.bin",
"transformer.encoder.layers.4.input_layernorm.weight": "pytorch_model-00003-of-00015.bin",
"transformer.encoder.layers.4.mlp.dense_4h_to_h.weight": "pytorch_model-00003-of-00015.bin",
"transformer.encoder.layers.4.mlp.dense_h_to_4h.weight": "pytorch_model-00003-of-00015.bin",
"transformer.encoder.layers.4.post_attention_layernorm.weight": "pytorch_model-00003-of-00015.bin",
"transformer.encoder.layers.4.self_attention.dense.weight": "pytorch_model-00003-of-00015.bin",
"transformer.encoder.layers.4.self_attention.query_key_value.bias": "pytorch_model-00003-of-00015.bin",
"transformer.encoder.layers.4.self_attention.query_key_value.weight": "pytorch_model-00003-of-00015.bin",
"transformer.encoder.layers.5.input_layernorm.weight": "pytorch_model-00003-of-00015.bin",
"transformer.encoder.layers.5.mlp.dense_4h_to_h.weight": "pytorch_model-00004-of-00015.bin",
"transformer.encoder.layers.5.mlp.dense_h_to_4h.weight": "pytorch_model-00004-of-00015.bin",
"transformer.encoder.layers.5.post_attention_layernorm.weight": "pytorch_model-00003-of-00015.bin",
"transformer.encoder.layers.5.self_attention.dense.weight": "pytorch_model-00003-of-00015.bin",
"transformer.encoder.layers.5.self_attention.query_key_value.bias": "pytorch_model-00003-of-00015.bin",
"transformer.encoder.layers.5.self_attention.query_key_value.weight": "pytorch_model-00003-of-00015.bin",
"transformer.encoder.layers.6.input_layernorm.weight": "pytorch_model-00004-of-00015.bin",
"transformer.encoder.layers.6.mlp.dense_4h_to_h.weight": "pytorch_model-00004-of-00015.bin",
"transformer.encoder.layers.6.mlp.dense_h_to_4h.weight": "pytorch_model-00004-of-00015.bin",
"transformer.encoder.layers.6.post_attention_layernorm.weight": "pytorch_model-00004-of-00015.bin",
"transformer.encoder.layers.6.self_attention.dense.weight": "pytorch_model-00004-of-00015.bin",
"transformer.encoder.layers.6.self_attention.query_key_value.bias": "pytorch_model-00004-of-00015.bin",
"transformer.encoder.layers.6.self_attention.query_key_value.weight": "pytorch_model-00004-of-00015.bin",
"transformer.encoder.layers.7.input_layernorm.weight": "pytorch_model-00004-of-00015.bin",
"transformer.encoder.layers.7.mlp.dense_4h_to_h.weight": "pytorch_model-00005-of-00015.bin",
"transformer.encoder.layers.7.mlp.dense_h_to_4h.weight": "pytorch_model-00005-of-00015.bin",
"transformer.encoder.layers.7.post_attention_layernorm.weight": "pytorch_model-00004-of-00015.bin",
"transformer.encoder.layers.7.self_attention.dense.weight": "pytorch_model-00004-of-00015.bin",
"transformer.encoder.layers.7.self_attention.query_key_value.bias": "pytorch_model-00004-of-00015.bin",
"transformer.encoder.layers.7.self_attention.query_key_value.weight": "pytorch_model-00004-of-00015.bin",
"transformer.encoder.layers.8.input_layernorm.weight": "pytorch_model-00005-of-00015.bin",
"transformer.encoder.layers.8.mlp.dense_4h_to_h.weight": "pytorch_model-00005-of-00015.bin",
"transformer.encoder.layers.8.mlp.dense_h_to_4h.weight": "pytorch_model-00005-of-00015.bin",
"transformer.encoder.layers.8.post_attention_layernorm.weight": "pytorch_model-00005-of-00015.bin",
"transformer.encoder.layers.8.self_attention.dense.weight": "pytorch_model-00005-of-00015.bin",
"transformer.encoder.layers.8.self_attention.query_key_value.bias": "pytorch_model-00005-of-00015.bin",
"transformer.encoder.layers.8.self_attention.query_key_value.weight": "pytorch_model-00005-of-00015.bin",
"transformer.encoder.layers.9.input_layernorm.weight": "pytorch_model-00005-of-00015.bin",
"transformer.encoder.layers.9.mlp.dense_4h_to_h.weight": "pytorch_model-00006-of-00015.bin",
"transformer.encoder.layers.9.mlp.dense_h_to_4h.weight": "pytorch_model-00006-of-00015.bin",
"transformer.encoder.layers.9.post_attention_layernorm.weight": "pytorch_model-00005-of-00015.bin",
"transformer.encoder.layers.9.self_attention.dense.weight": "pytorch_model-00005-of-00015.bin",
"transformer.encoder.layers.9.self_attention.query_key_value.bias": "pytorch_model-00005-of-00015.bin",
"transformer.encoder.layers.9.self_attention.query_key_value.weight": "pytorch_model-00005-of-00015.bin",
"transformer.output_layer.weight": "pytorch_model-00015-of-00015.bin",
"transformer.rotary_pos_emb.inv_freq": "pytorch_model-00001-of-00015.bin"
}
}

188
quantization.py Normal file

File diff suppressed because one or more lines are too long

1
special_tokens_map.json Normal file
View File

@ -0,0 +1 @@
{}

253
tokenization_chatglm.py Normal file
View File

@ -0,0 +1,253 @@
import os
import torch
from typing import List, Optional, Union, Dict
from sentencepiece import SentencePieceProcessor
from transformers import PreTrainedTokenizer
from transformers.utils import logging, PaddingStrategy
from transformers.tokenization_utils_base import EncodedInput, BatchEncoding
class SPTokenizer:
def __init__(self, model_path: str):
# reload tokenizer
assert os.path.isfile(model_path), model_path
self.sp_model = SentencePieceProcessor(model_file=model_path)
# BOS / EOS token IDs
self.n_words: int = self.sp_model.vocab_size()
self.bos_id: int = self.sp_model.bos_id()
self.eos_id: int = self.sp_model.eos_id()
self.pad_id: int = self.sp_model.unk_id()
assert self.sp_model.vocab_size() == self.sp_model.get_piece_size()
special_tokens = ["[MASK]", "[gMASK]", "[sMASK]", "sop", "eop"]
self.special_tokens = {}
self.index_special_tokens = {}
for token in special_tokens:
self.special_tokens[token] = self.n_words
self.index_special_tokens[self.n_words] = token
self.n_words += 1
def tokenize(self, s: str):
return self.sp_model.EncodeAsPieces(s)
def encode(self, s: str, bos: bool = False, eos: bool = False) -> List[int]:
assert type(s) is str
t = self.sp_model.encode(s)
if bos:
t = [self.bos_id] + t
if eos:
t = t + [self.eos_id]
return t
def decode(self, t: List[int]) -> str:
return self.sp_model.decode(t)
def decode_tokens(self, tokens: List[str]) -> str:
text = self.sp_model.DecodePieces(tokens)
return text
def convert_token_to_id(self, token):
""" Converts a token (str) in an id using the vocab. """
if token in self.special_tokens:
return self.special_tokens[token]
return self.sp_model.PieceToId(token)
def convert_id_to_token(self, index):
"""Converts an index (integer) in a token (str) using the vocab."""
if index in self.index_special_tokens or index in [self.eos_id, self.bos_id, self.pad_id] or index < 0:
return ""
return self.sp_model.IdToPiece(index)
class ChatGLMTokenizer(PreTrainedTokenizer):
vocab_files_names = {"vocab_file": "tokenizer.model"}
model_input_names = ["input_ids", "attention_mask", "position_ids"]
def __init__(self, vocab_file, padding_side="left", **kwargs):
super().__init__(padding_side=padding_side, **kwargs)
self.name = "GLMTokenizer"
self.vocab_file = vocab_file
self.tokenizer = SPTokenizer(vocab_file)
self.special_tokens = {
"<bos>": self.tokenizer.bos_id,
"<eos>": self.tokenizer.eos_id,
"<pad>": self.tokenizer.pad_id
}
def get_command(self, token):
if token in self.special_tokens:
return self.special_tokens[token]
assert token in self.tokenizer.special_tokens, f"{token} is not a special token for {self.name}"
return self.tokenizer.special_tokens[token]
@property
def pad_token(self) -> str:
return "<unk>"
@property
def pad_token_id(self):
return self.get_command("<pad>")
@property
def eos_token(self) -> str:
return "</s>"
@property
def eos_token_id(self):
return self.get_command("<eos>")
@property
def vocab_size(self):
return self.tokenizer.n_words
def get_vocab(self):
""" Returns vocab as a dict """
vocab = {self._convert_id_to_token(i): i for i in range(self.vocab_size)}
vocab.update(self.added_tokens_encoder)
return vocab
def _tokenize(self, text, **kwargs):
return self.tokenizer.tokenize(text)
def _convert_token_to_id(self, token):
""" Converts a token (str) in an id using the vocab. """
return self.tokenizer.convert_token_to_id(token)
def _convert_id_to_token(self, index):
"""Converts an index (integer) in a token (str) using the vocab."""
return self.tokenizer.convert_id_to_token(index)
def convert_tokens_to_string(self, tokens: List[str]) -> str:
return self.tokenizer.decode_tokens(tokens)
def save_vocabulary(self, save_directory, filename_prefix=None):
"""
Save the vocabulary and special tokens file to a directory.
Args:
save_directory (`str`):
The directory in which to save the vocabulary.
filename_prefix (`str`, *optional*):
An optional prefix to add to the named of the saved files.
Returns:
`Tuple(str)`: Paths to the files saved.
"""
if os.path.isdir(save_directory):
vocab_file = os.path.join(
save_directory, self.vocab_files_names["vocab_file"]
)
else:
vocab_file = save_directory
with open(self.vocab_file, 'rb') as fin:
proto_str = fin.read()
with open(vocab_file, "wb") as writer:
writer.write(proto_str)
return (vocab_file,)
def get_prefix_tokens(self):
prefix_tokens = [self.get_command("[gMASK]"), self.get_command("sop")]
return prefix_tokens
def build_prompt(self, query, history=None):
if history is None:
history = []
prompt = ""
for i, (old_query, response) in enumerate(history):
prompt += "[Round {}]\n\n问:{}\n\n答:{}\n\n".format(i + 1, old_query, response)
prompt += "[Round {}]\n\n问:{}\n\n答:".format(len(history) + 1, query)
return prompt
def build_inputs_with_special_tokens(
self, token_ids_0: List[int], token_ids_1: Optional[List[int]] = None
) -> List[int]:
"""
Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and
adding special tokens. A BERT sequence has the following format:
- single sequence: `[CLS] X [SEP]`
- pair of sequences: `[CLS] A [SEP] B [SEP]`
Args:
token_ids_0 (`List[int]`):
List of IDs to which the special tokens will be added.
token_ids_1 (`List[int]`, *optional*):
Optional second list of IDs for sequence pairs.
Returns:
`List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens.
"""
prefix_tokens = self.get_prefix_tokens()
token_ids_0 = prefix_tokens + token_ids_0
if token_ids_1 is not None:
token_ids_0 = token_ids_0 + token_ids_1 + [self.get_command("<eos>")]
return token_ids_0
def _pad(
self,
encoded_inputs: Union[Dict[str, EncodedInput], BatchEncoding],
max_length: Optional[int] = None,
padding_strategy: PaddingStrategy = PaddingStrategy.DO_NOT_PAD,
pad_to_multiple_of: Optional[int] = None,
return_attention_mask: Optional[bool] = None,
) -> dict:
"""
Pad encoded inputs (on left/right and up to predefined length or max length in the batch)
Args:
encoded_inputs:
Dictionary of tokenized inputs (`List[int]`) or batch of tokenized inputs (`List[List[int]]`).
max_length: maximum length of the returned list and optionally padding length (see below).
Will truncate by taking into account the special tokens.
padding_strategy: PaddingStrategy to use for padding.
- PaddingStrategy.LONGEST Pad to the longest sequence in the batch
- PaddingStrategy.MAX_LENGTH: Pad to the max length (default)
- PaddingStrategy.DO_NOT_PAD: Do not pad
The tokenizer padding sides are defined in self.padding_side:
- 'left': pads on the left of the sequences
- 'right': pads on the right of the sequences
pad_to_multiple_of: (optional) Integer if set will pad the sequence to a multiple of the provided value.
This is especially useful to enable the use of Tensor Core on NVIDIA hardware with compute capability
`>= 7.5` (Volta).
return_attention_mask:
(optional) Set to False to avoid returning attention mask (default: set to model specifics)
"""
# Load from model defaults
assert self.padding_side == "left"
required_input = encoded_inputs[self.model_input_names[0]]
seq_length = len(required_input)
if padding_strategy == PaddingStrategy.LONGEST:
max_length = len(required_input)
if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
max_length = ((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of
needs_to_be_padded = padding_strategy != PaddingStrategy.DO_NOT_PAD and len(required_input) != max_length
# Initialize attention mask if not present.
if "attention_mask" not in encoded_inputs:
encoded_inputs["attention_mask"] = [1] * seq_length
if "position_ids" not in encoded_inputs:
encoded_inputs["position_ids"] = list(range(seq_length))
if needs_to_be_padded:
difference = max_length - len(required_input)
if "attention_mask" in encoded_inputs:
encoded_inputs["attention_mask"] = [0] * difference + encoded_inputs["attention_mask"]
if "position_ids" in encoded_inputs:
encoded_inputs["position_ids"] = [0] * difference + encoded_inputs["position_ids"]
encoded_inputs[self.model_input_names[0]] = [self.pad_token_id] * difference + required_input
return encoded_inputs

BIN
tokenizer.model Normal file

Binary file not shown.

14
tokenizer_config.json Normal file
View File

@ -0,0 +1,14 @@
{
"auto_map": {
"AutoTokenizer": [
"tokenization_chatglm.ChatGLMTokenizer",
null
]
},
"clean_up_tokenization_spaces": true,
"do_lower_case": false,
"model_max_length": 1000000000000000019884624838656,
"padding_side": "left",
"remove_space": false,
"tokenizer_class": "ChatGLMTokenizer"
}