myownskyW7 commited on
Commit
7a51eae
·
1 Parent(s): 8974c5f

add meta instruct

Browse files
Files changed (1) hide show
  1. modeling_InternLM_XComposer.py +25 -10
modeling_InternLM_XComposer.py CHANGED
@@ -26,6 +26,13 @@ class InternLMXComposerForCausalLM(PreTrainedModel):
26
  config_class = InternLMXComposerConfig
27
  _auto_class = "AutoModelForCausalLM"
28
 
 
 
 
 
 
 
 
29
  gen_config = dict(
30
  num_beams=5,
31
  do_sample=False,
@@ -33,7 +40,7 @@ class InternLMXComposerForCausalLM(PreTrainedModel):
33
  repetition_penalty=1.5,
34
  length_penalty=1.0,
35
  temperature=1.0,
36
- max_new_tokens=200,
37
  )
38
 
39
  def __init__(self, config):
@@ -74,13 +81,14 @@ class InternLMXComposerForCausalLM(PreTrainedModel):
74
  # speed up init llm
75
  with torch.device('meta'):
76
  self.internlm_model = InternLMForCausalLM._from_config(config)
77
- self.internlm_model.to_empty(device=config.device).to(torch.float16)
 
78
  for n, m in self.internlm_model.named_modules():
79
  if 'lora' in n:
80
  m.float()
81
 
82
  self.internlm_proj = nn.Linear(self.Qformer.config.hidden_size,
83
- self.internlm_model.config.hidden_size)
84
  print('Done')
85
 
86
  self.vis_processor = transforms.Compose([
@@ -154,13 +162,14 @@ class InternLMXComposerForCausalLM(PreTrainedModel):
154
  encoder_attention_mask=image_atts,
155
  return_dict=True,
156
  )
157
- inputs_internlm = self.internlm_proj(query_output.last_hidden_state)
 
158
  inputs_internlm = torch.cat([
159
  self.flag_image_start.expand(inputs_internlm.shape[0], -1, -1),
160
  inputs_internlm,
161
  self.flag_image_end.expand(inputs_internlm.shape[0], -1, -1)
162
  ],
163
- dim=1)
164
  return inputs_internlm
165
 
166
  def encode_text(self, text, add_special_tokens=False):
@@ -195,8 +204,8 @@ class InternLMXComposerForCausalLM(PreTrainedModel):
195
  text_embeds = self.encode_text(text)
196
  img_embeds = self.encode_img(image)
197
  prompt_embeds = self.wrap_prompt(text_embeds, img_embeds)
198
- out_embeds = self.internlm_model.generate(inputs_embeds=prompt_embeds,
199
- **self.get_gen_args(**kwargs))
200
  out_text = self.decode_text(out_embeds)
201
  return out_text
202
 
@@ -206,8 +215,8 @@ class InternLMXComposerForCausalLM(PreTrainedModel):
206
  prompt_embeds = self.wrap_prompt(text_embeds,
207
  img_embeds,
208
  history=history)
209
- out_embeds = self.internlm_model.generate(inputs_embeds=prompt_embeds,
210
- **self.get_gen_args(**kwargs))
211
  out_text = self.decode_text(out_embeds)
212
 
213
  # trunc at eoh and eoa
@@ -231,7 +240,13 @@ class InternLMXComposerForCausalLM(PreTrainedModel):
231
  history=None,
232
  add_special=True):
233
  if add_special:
234
- prompt_segs = [' <|User|>:', f'\n{self.eoh} <|Bot|>:']
 
 
 
 
 
 
235
  else:
236
  prompt_segs = [' <|User|>:', ' <|Bot|>:'] # used in wrap history
237
  prompt_seg_embeds = []
 
26
  config_class = InternLMXComposerConfig
27
  _auto_class = "AutoModelForCausalLM"
28
 
29
+ meta_instruction = """meta instruction
30
+ You are an AI assistant whose name is 浦语.
31
+ - 浦语 is a conversational language model that is developed by Shanghai AI Laboratory (上海人工智能实验室). It is designed to be helpful, honest, and harmless.
32
+ - 浦语 can understand and communicate fluently in the language chosen by the user such as English and 中文.
33
+ conversation
34
+ """
35
+
36
  gen_config = dict(
37
  num_beams=5,
38
  do_sample=False,
 
40
  repetition_penalty=1.5,
41
  length_penalty=1.0,
42
  temperature=1.0,
43
+ max_new_tokens=500,
44
  )
45
 
46
  def __init__(self, config):
 
81
  # speed up init llm
82
  with torch.device('meta'):
83
  self.internlm_model = InternLMForCausalLM._from_config(config)
84
+ self.internlm_model.to_empty(device=config.device).to(
85
+ torch.float16)
86
  for n, m in self.internlm_model.named_modules():
87
  if 'lora' in n:
88
  m.float()
89
 
90
  self.internlm_proj = nn.Linear(self.Qformer.config.hidden_size,
91
+ self.internlm_model.config.hidden_size)
92
  print('Done')
93
 
94
  self.vis_processor = transforms.Compose([
 
162
  encoder_attention_mask=image_atts,
163
  return_dict=True,
164
  )
165
+ inputs_internlm = self.internlm_proj(
166
+ query_output.last_hidden_state)
167
  inputs_internlm = torch.cat([
168
  self.flag_image_start.expand(inputs_internlm.shape[0], -1, -1),
169
  inputs_internlm,
170
  self.flag_image_end.expand(inputs_internlm.shape[0], -1, -1)
171
  ],
172
+ dim=1)
173
  return inputs_internlm
174
 
175
  def encode_text(self, text, add_special_tokens=False):
 
204
  text_embeds = self.encode_text(text)
205
  img_embeds = self.encode_img(image)
206
  prompt_embeds = self.wrap_prompt(text_embeds, img_embeds)
207
+ out_embeds = self.internlm_model.generate(
208
+ inputs_embeds=prompt_embeds, **self.get_gen_args(**kwargs))
209
  out_text = self.decode_text(out_embeds)
210
  return out_text
211
 
 
215
  prompt_embeds = self.wrap_prompt(text_embeds,
216
  img_embeds,
217
  history=history)
218
+ out_embeds = self.internlm_model.generate(
219
+ inputs_embeds=prompt_embeds, **self.get_gen_args(**kwargs))
220
  out_text = self.decode_text(out_embeds)
221
 
222
  # trunc at eoh and eoa
 
240
  history=None,
241
  add_special=True):
242
  if add_special:
243
+ if history is None:
244
+ prompt_segs = [
245
+ self.meta_instruction + ' <|User|>:',
246
+ f'\n{self.eoh} <|Bot|>:'
247
+ ]
248
+ else:
249
+ prompt_segs = [' <|User|>:', f'\n{self.eoh} <|Bot|>:']
250
  else:
251
  prompt_segs = [' <|User|>:', ' <|Bot|>:'] # used in wrap history
252
  prompt_seg_embeds = []