Commit
·
eea2f56
1
Parent(s):
047819a
Upload modeling_InternLM_XComposer.py
Browse files- modeling_InternLM_XComposer.py +30 -24
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=
|
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(
|
|
|
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 |
-
|
84 |
print('Done')
|
85 |
|
86 |
self.vis_processor = transforms.Compose([
|
@@ -95,15 +103,13 @@ class InternLMXComposerForCausalLM(PreTrainedModel):
|
|
95 |
|
96 |
@property
|
97 |
def eoh(self):
|
98 |
-
|
99 |
-
|
100 |
-
return '<TOKENS_UNUSED_0>'
|
101 |
|
102 |
@property
|
103 |
def eoa(self):
|
104 |
-
|
105 |
-
|
106 |
-
return '<TOKENS_UNUSED_1>'
|
107 |
|
108 |
def maybe_autocast(self, dtype=torch.float16):
|
109 |
# if on cpu, don't use autocast
|
@@ -121,16 +127,12 @@ class InternLMXComposerForCausalLM(PreTrainedModel):
|
|
121 |
vision_width,
|
122 |
cross_attention_freq=2,
|
123 |
pretrain=True):
|
124 |
-
encoder_config = BertConfig
|
125 |
encoder_config.encoder_width = vision_width
|
126 |
# insert cross-attention layer every other block
|
127 |
encoder_config.add_cross_attention = True
|
128 |
encoder_config.cross_attention_freq = cross_attention_freq
|
129 |
encoder_config.query_length = num_query_token
|
130 |
-
# if pretrain:
|
131 |
-
# Qformer = BertLMHeadModel.from_pretrained("bert-base-uncased",
|
132 |
-
# config=encoder_config)
|
133 |
-
# else:
|
134 |
Qformer = BertLMHeadModel(config=encoder_config)
|
135 |
query_tokens = nn.Parameter(
|
136 |
torch.zeros(1, num_query_token, encoder_config.hidden_size))
|
@@ -160,13 +162,14 @@ class InternLMXComposerForCausalLM(PreTrainedModel):
|
|
160 |
encoder_attention_mask=image_atts,
|
161 |
return_dict=True,
|
162 |
)
|
163 |
-
inputs_internlm = self.internlm_proj(
|
|
|
164 |
inputs_internlm = torch.cat([
|
165 |
self.flag_image_start.expand(inputs_internlm.shape[0], -1, -1),
|
166 |
inputs_internlm,
|
167 |
self.flag_image_end.expand(inputs_internlm.shape[0], -1, -1)
|
168 |
],
|
169 |
-
|
170 |
return inputs_internlm
|
171 |
|
172 |
def encode_text(self, text, add_special_tokens=False):
|
@@ -196,16 +199,13 @@ class InternLMXComposerForCausalLM(PreTrainedModel):
|
|
196 |
new_kargs = copy.deepcopy(self.gen_config)
|
197 |
new_kargs.update(kwargs)
|
198 |
return new_kargs
|
199 |
-
|
200 |
-
def forward(self, **kwargs):
|
201 |
-
return self.internlm_model(**kwargs)
|
202 |
|
203 |
def generate(self, text, image=None, **kwargs):
|
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 |
-
|
209 |
out_text = self.decode_text(out_embeds)
|
210 |
return out_text
|
211 |
|
@@ -215,8 +215,8 @@ class InternLMXComposerForCausalLM(PreTrainedModel):
|
|
215 |
prompt_embeds = self.wrap_prompt(text_embeds,
|
216 |
img_embeds,
|
217 |
history=history)
|
218 |
-
out_embeds = self.internlm_model.generate(
|
219 |
-
|
220 |
out_text = self.decode_text(out_embeds)
|
221 |
|
222 |
# trunc at eoh and eoa
|
@@ -240,7 +240,13 @@ class InternLMXComposerForCausalLM(PreTrainedModel):
|
|
240 |
history=None,
|
241 |
add_special=True):
|
242 |
if add_special:
|
243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
244 |
else:
|
245 |
prompt_segs = [' <|User|>:', ' <|Bot|>:'] # used in wrap history
|
246 |
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([
|
|
|
103 |
|
104 |
@property
|
105 |
def eoh(self):
|
106 |
+
return self.tokenizer.decode(torch.Tensor([103027]),
|
107 |
+
skip_special_tokens=True)
|
|
|
108 |
|
109 |
@property
|
110 |
def eoa(self):
|
111 |
+
return self.tokenizer.decode(torch.Tensor([103028]),
|
112 |
+
skip_special_tokens=True)
|
|
|
113 |
|
114 |
def maybe_autocast(self, dtype=torch.float16):
|
115 |
# if on cpu, don't use autocast
|
|
|
127 |
vision_width,
|
128 |
cross_attention_freq=2,
|
129 |
pretrain=True):
|
130 |
+
encoder_config = BertConfig()
|
131 |
encoder_config.encoder_width = vision_width
|
132 |
# insert cross-attention layer every other block
|
133 |
encoder_config.add_cross_attention = True
|
134 |
encoder_config.cross_attention_freq = cross_attention_freq
|
135 |
encoder_config.query_length = num_query_token
|
|
|
|
|
|
|
|
|
136 |
Qformer = BertLMHeadModel(config=encoder_config)
|
137 |
query_tokens = nn.Parameter(
|
138 |
torch.zeros(1, num_query_token, encoder_config.hidden_size))
|
|
|
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):
|
|
|
199 |
new_kargs = copy.deepcopy(self.gen_config)
|
200 |
new_kargs.update(kwargs)
|
201 |
return new_kargs
|
|
|
|
|
|
|
202 |
|
203 |
def generate(self, text, image=None, **kwargs):
|
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 = []
|