silver commited on
Commit
4ef674a
1 Parent(s): 437fc94

remove unused image token

Browse files
README.md CHANGED
@@ -7,8 +7,13 @@ tags:
7
  - chatglm
8
  - thudm
9
  ---
10
- # ChatGLM-6B
11
  ## 介绍
 
 
 
 
 
12
  ChatGLM-6B 是一个开源的、支持中英双语问答的对话语言模型,基于 [General Language Model (GLM)](https://github.com/THUDM/GLM) 架构,具有 62 亿参数。结合模型量化技术,用户可以在消费级的显卡上进行本地部署(INT4 量化级别下最低只需 6GB 显存)。ChatGLM-6B 使用了和 [ChatGLM](https://chatglm.cn) 相同的技术,针对中文问答和对话进行了优化。经过约 1T 标识符的中英双语训练,辅以监督微调、反馈自助、人类反馈强化学习等技术的加持,62 亿参数的 ChatGLM-6B 已经能生成相当符合人类偏好的回答。
13
 
14
  ChatGLM-6B-INT4-QE 是 ChatGLM-6B 量化后的模型权重。具体的,ChatGLM-6B-INT4-QE 对 ChatGLM-6B 中的 28 个 GLM Block 、 Embedding 和 LM Head 进行了 INT4 量化。量化后的模型权重文件仅为 3G ,理论上 6G 显存(使用 CPU 即 6G 内存)即可推理,具有在嵌入式设备(如树莓派)上运行的可能。
@@ -27,8 +32,8 @@ pip install protobuf==3.20.0 transformers==4.26.1 icetk cpm_kernels
27
 
28
  ```ipython
29
  >>> from transformers import AutoTokenizer, AutoModel
30
- >>> tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b-int4-qe", trust_remote_code=True)
31
- >>> model = AutoModel.from_pretrained("THUDM/chatglm-6b-int4-qe", trust_remote_code=True).half().cuda()
32
  >>> response, history = model.chat(tokenizer, "你好", history=[])
33
  >>> print(response)
34
  你好👋!我是人工智能助手 ChatGLM-6B,很高兴见到你,欢迎问我任何问题。
 
7
  - chatglm
8
  - thudm
9
  ---
10
+ # ChatGLM-6B-INT4-QE-Slim: 低显存版ChatGLM-6B-INT4-QE
11
  ## 介绍
12
+
13
+ ChatGLM-6B-INT4-QE-Slim是在ChatGLM-6B-INT4-QE的基础上通过裁剪词表构建的。因为ChatGLM-6B使用了icetk,在其词表中,前20000个token是预留给图片的,在文本模型中没有用到这些图片token,但是在infer和微调的时候,这些token对应的embedding依然需要被加载,并且在解码每一个token的时候需要多计算20K个logits,会占用不少显存。因此将这一部分token裁剪掉以节省显存。
14
+
15
+ 除了词表外,ChatGLM-6B-INT4-QE-Slim的其他结构与ChatGLM-6B-INT4-QE完全一致,性能也完全一样,可以认为是ChatGLM-6B-INT4的一个低显存版等价平替。
16
+
17
  ChatGLM-6B 是一个开源的、支持中英双语问答的对话语言模型,基于 [General Language Model (GLM)](https://github.com/THUDM/GLM) 架构,具有 62 亿参数。结合模型量化技术,用户可以在消费级的显卡上进行本地部署(INT4 量化级别下最低只需 6GB 显存)。ChatGLM-6B 使用了和 [ChatGLM](https://chatglm.cn) 相同的技术,针对中文问答和对话进行了优化。经过约 1T 标识符的中英双语训练,辅以监督微调、反馈自助、人类反馈强化学习等技术的加持,62 亿参数的 ChatGLM-6B 已经能生成相当符合人类偏好的回答。
18
 
19
  ChatGLM-6B-INT4-QE 是 ChatGLM-6B 量化后的模型权重。具体的,ChatGLM-6B-INT4-QE 对 ChatGLM-6B 中的 28 个 GLM Block 、 Embedding 和 LM Head 进行了 INT4 量化。量化后的模型权重文件仅为 3G ,理论上 6G 显存(使用 CPU 即 6G 内存)即可推理,具有在嵌入式设备(如树莓派)上运行的可能。
 
32
 
33
  ```ipython
34
  >>> from transformers import AutoTokenizer, AutoModel
35
+ >>> tokenizer = AutoTokenizer.from_pretrained("silver/chatglm-6b-int4-qe-slim", trust_remote_code=True)
36
+ >>> model = AutoModel.from_pretrained("silver/chatglm-6b-int4-qe-slim", trust_remote_code=True).half().cuda()
37
  >>> response, history = model.chat(tokenizer, "你好", history=[])
38
  >>> print(response)
39
  你好👋!我是人工智能助手 ChatGLM-6B,很高兴见到你,欢迎问我任何问题。
config.json CHANGED
@@ -8,8 +8,8 @@
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,
@@ -24,5 +24,5 @@
24
  "torch_dtype": "float16",
25
  "transformers_version": "4.26.1",
26
  "use_cache": true,
27
- "vocab_size": 150528
28
  }
 
8
  "AutoModel": "modeling_chatglm.ChatGLMForConditionalGeneration",
9
  "AutoModelForSeq2SeqLM": "modeling_chatglm.ChatGLMForConditionalGeneration"
10
  },
11
+ "bos_token_id": 130004,
12
+ "eos_token_id": 130005,
13
  "hidden_size": 4096,
14
  "inner_hidden_size": 16384,
15
  "layernorm_epsilon": 1e-05,
 
24
  "torch_dtype": "float16",
25
  "transformers_version": "4.26.1",
26
  "use_cache": true,
27
+ "vocab_size": 130528
28
  }
configuration_chatglm.py CHANGED
@@ -58,14 +58,14 @@ class ChatGLMConfig(PretrainedConfig):
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,
 
58
 
59
  def __init__(
60
  self,
61
+ vocab_size=130528,
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=130004,
68
+ eos_token_id=130005,
69
  pad_token_id=0,
70
  max_sequence_length=2048,
71
  inner_hidden_size=16384,
modeling_chatglm.py CHANGED
@@ -53,7 +53,7 @@ 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
 
@@ -839,7 +839,7 @@ class ChatGLMModel(ChatGLMPreTrainedModel):
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
 
@@ -981,7 +981,7 @@ class ChatGLMForConditionalGeneration(ChatGLMPreTrainedModel):
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()
 
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[..., 5] = 5e4
57
  return scores
58
 
59
 
 
839
  )
840
 
841
  if position_ids is None:
842
+ MASK, gMASK = 130000, 130001
843
  mask_token = MASK if MASK in input_ids else gMASK
844
  use_gmask = False if MASK in input_ids else gMASK
845
 
 
981
  **kwargs
982
  ) -> dict:
983
 
984
+ MASK, gMASK = 130000, 130001
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()
pytorch_model.bin CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:9be2d85ff9384dd93e11f724e495a2b0835d985a073cfa2a8ccf02016854378f
3
- size 3132384013
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a7236a772df5ef4c2f62ad733adb7ad2c5dd17cde8d8c7a31ba7848e17d8affd
3
+ size 3132377293
tokenization_chatglm.py CHANGED
@@ -85,17 +85,13 @@ class SPTokenizer:
85
  def get_tab_token():
86
  return f"<|tab|>"
87
 
88
- @property
89
- def num_image_tokens(self):
90
- return 20000
91
-
92
  @property
93
  def num_text_tokens(self):
94
  return self.text_tokenizer.num_tokens
95
 
96
  @property
97
  def num_tokens(self):
98
- return self.num_image_tokens + self.num_text_tokens
99
 
100
  @staticmethod
101
  def _encode_whitespaces(text: str, max_len: int = 80):
@@ -125,11 +121,11 @@ class SPTokenizer:
125
  if not add_dummy_prefix:
126
  text = "<n>" + text
127
  tmp = self._get_text_tokenizer(encode_special_tokens=special_tokens).encode(text)
128
- tokens = [x + self.num_image_tokens for x in tmp]
129
  return tokens if add_dummy_prefix else tokens[2:]
130
 
131
  def decode(self, text_ids: List[int], special_tokens=False) -> str:
132
- ids = [int(_id) - self.num_image_tokens for _id in text_ids]
133
  ids = [_id for _id in ids if _id >= 0]
134
  text = self._get_text_tokenizer(encode_special_tokens=special_tokens).decode(ids)
135
  text = text.replace("<n>", "\n")
@@ -156,15 +152,9 @@ class SPTokenizer:
156
 
157
  def __getitem__(self, x: Union[int, str]):
158
  if isinstance(x, int):
159
- if x < self.num_image_tokens:
160
- return "<image_{}>".format(x)
161
- else:
162
- return self.text_tokenizer.convert_id_to_token(x - self.num_image_tokens)
163
  elif isinstance(x, str):
164
- if x.startswith("<image_") and x.endswith(">") and x[7:-1].isdigit():
165
- return int(x[7:-1])
166
- else:
167
- return self.text_tokenizer.convert_token_to_id(x) + self.num_image_tokens
168
  else:
169
  raise ValueError("The key should be str or int.")
170
 
 
85
  def get_tab_token():
86
  return f"<|tab|>"
87
 
 
 
 
 
88
  @property
89
  def num_text_tokens(self):
90
  return self.text_tokenizer.num_tokens
91
 
92
  @property
93
  def num_tokens(self):
94
+ return self.num_text_tokens
95
 
96
  @staticmethod
97
  def _encode_whitespaces(text: str, max_len: int = 80):
 
121
  if not add_dummy_prefix:
122
  text = "<n>" + text
123
  tmp = self._get_text_tokenizer(encode_special_tokens=special_tokens).encode(text)
124
+ tokens = [x for x in tmp]
125
  return tokens if add_dummy_prefix else tokens[2:]
126
 
127
  def decode(self, text_ids: List[int], special_tokens=False) -> str:
128
+ ids = [int(_id) for _id in text_ids]
129
  ids = [_id for _id in ids if _id >= 0]
130
  text = self._get_text_tokenizer(encode_special_tokens=special_tokens).decode(ids)
131
  text = text.replace("<n>", "\n")
 
152
 
153
  def __getitem__(self, x: Union[int, str]):
154
  if isinstance(x, int):
155
+ return self.text_tokenizer.convert_id_to_token(x)
 
 
 
156
  elif isinstance(x, str):
157
+ return self.text_tokenizer.convert_token_to_id(x)
 
 
 
158
  else:
159
  raise ValueError("The key should be str or int.")
160