File size: 11,900 Bytes
52b6907
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8b5be2
52b6907
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from typing import Optional, Tuple, Union, List

import torch
import torch.utils.checkpoint
from torch import nn
from transformers.utils import (
    logging,
)
from transformers.models.blip_2.configuration_blip_2 import Blip2Config
from transformers.models.blip_2.modeling_blip_2 import Blip2ForConditionalGenerationModelOutput
from transformers import (
    Blip2PreTrainedModel,
    Blip2VisionModel,
    Blip2QFormerModel,
    PreTrainedTokenizer,
    PreTrainedModel,
)


logger = logging.get_logger(__name__)


class ZiyaBlip2ForCausalLM(Blip2PreTrainedModel):
    config_class = Blip2Config
    main_input_name = "pixel_values"
    _keys_to_ignore_on_load_missing = [
        r"language_model",
    ]
    def __init__(self, config: Blip2Config, language_model: PreTrainedModel = None):
        super().__init__(config)

        self.vision_model = Blip2VisionModel(config.vision_config)

        self.query_tokens = nn.Parameter(torch.zeros(
            1, config.num_query_tokens, config.qformer_config.hidden_size))
        self.qformer = Blip2QFormerModel(config.qformer_config)

        self.language_projection = nn.Linear(
            config.qformer_config.hidden_size, config.text_config.hidden_size)
        self.language_model = language_model

        # Initialize weights and apply final processing
        self.post_init()

    def get_input_embeddings(self):
        return self.language_model.get_input_embeddings()

    def set_input_embeddings(self, value):
        self.language_model.set_input_embeddings(value)

    def set_output_embeddings(self, new_embeddings):
        self.language_model.set_output_embeddings(new_embeddings)

    def get_output_embeddings(self) -> nn.Module:
        return self.language_model.get_output_embeddings()

    def get_encoder(self):
        return self.language_model.get_encoder()

    def get_decoder(self):
        return self.language_model.get_decoder()

    def _tie_weights(self):
        if not self.config.use_decoder_only_language_model:
            self.language_model.encoder.embed_tokens = self.language_model.shared
            self.language_model.decoder.embed_tokens = self.language_model.shared

    def _preprocess_accelerate(self):
        r"""
        Some pre-processing hacks to make the model `accelerate` compatible. Check
        https://github.com/huggingface/transformers/pull/21707 for more details.
        """
        hf_device_map = self.hf_device_map

        if len(hf_device_map) > 1 and "language_model" not in hf_device_map and torch.cuda.device_count() > 1:
            # warn users about unexpected behavior when using multi-GPU + BLIP-2 + `accelerate`.
            logger.warning(
                "The `language_model` is not in the `hf_device_map` dictionary and you are running your script"
                " in a multi-GPU environment. this may lead to unexpected behavior when using `accelerate`."
                " Please pass a `device_map` that contains `language_model` to remove this warning."
                " Please refer to https://github.com/huggingface/blog/blob/main/accelerate-large-models.md for",
                " more details on creating a `device_map` for large models.",
            )

        if hasattr(self.language_model, "_hf_hook"):
            self.language_model._hf_hook.io_same_device = True  # For `generate` compatibility

    def forward(
        self,
        pixel_values: torch.FloatTensor,
        input_ids_before_image: torch.FloatTensor,
        input_ids_after_image: torch.FloatTensor,
        labels_after_image: torch.FloatTensor,
        # 因为label不会出现在image之前,所以这里不需要labels_before_image, 按照input_ids_before_image补-100就可以了
        output_attentions: Optional[bool] = None,
        output_hidden_states: Optional[bool] = None,
        return_dict: Optional[bool] = None,
    ) -> Union[Tuple, Blip2ForConditionalGenerationModelOutput]:
        return_dict = return_dict if return_dict is not None else self.config.use_return_dict

        # step 1: forward the images through the vision encoder,
        # to get image embeddings of shape (batch_size, seq_len, hidden_size)
        vision_outputs = self.vision_model(
            pixel_values=pixel_values,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )
        image_embeds = vision_outputs[0]

        # step 2: forward the query tokens through the QFormer, using the image embeddings for cross-attention
        image_attention_mask = torch.ones(
            image_embeds.size()[:-1], dtype=torch.long, device=image_embeds.device)

        query_tokens = self.query_tokens.expand(image_embeds.shape[0], -1, -1)
        query_outputs = self.qformer(
            query_embeds=query_tokens,
            encoder_hidden_states=image_embeds,
            encoder_attention_mask=image_attention_mask,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )
        query_output = query_outputs[0]

        # step 2.5 generate the lm input by prompt and output
        language_model_inputs = self.language_projection(query_output)
        language_model_attention_mask = torch.ones(
            language_model_inputs.size()[:-1], dtype=torch.long, device=language_model_inputs.device
        )
        # 确保language_model_inputs的batch
        assert language_model_inputs.shape[0] == input_ids_after_image.shape[0]
        inputs_embeds_before_image = self.language_model.get_input_embeddings()(input_ids_before_image)
        inputs_embeds_after_image = self.language_model.get_input_embeddings()(input_ids_after_image)
        inputs_embeds = torch.cat(
            [
                inputs_embeds_before_image.to(language_model_inputs.device),
                language_model_inputs,
                inputs_embeds_after_image.to(language_model_inputs.device)
            ], dim=1)

        attention_mask_before = torch.ones_like(input_ids_before_image)
        attention_mask_after = torch.ones_like(input_ids_after_image)
        attention_mask = torch.cat(
            [
                attention_mask_before.to(language_model_attention_mask.device),
                language_model_attention_mask,
                attention_mask_after.to(language_model_attention_mask.device)
            ], dim=1
        )
        # labels也需要对应的处理,把前面空缺的-100加进去
        labels = torch.cat(
            [
                torch.tensor(
                    [-100]).expand_as(input_ids_before_image).to(language_model_inputs.device),
                torch.tensor([-100]).expand(query_tokens.shape[:-1]
                                            ).to(language_model_inputs.device),
                labels_after_image,
            ], dim=1
        )

        # step 3: use the language model

        if self.config.use_decoder_only_language_model:
            outputs = self.language_model(
                inputs_embeds=inputs_embeds,
                attention_mask=attention_mask,
                output_attentions=output_attentions,
                output_hidden_states=output_hidden_states,
                return_dict=return_dict,
                labels=labels,
            )
            loss = outputs.loss if return_dict else outputs[0]
            logits = outputs.logits if return_dict else outputs[1]

        else:
            raise Exception("not impl")

        if not return_dict:
            output = (logits, vision_outputs, query_outputs, outputs)
            return ((loss,) + output) if loss is not None else output

        return Blip2ForConditionalGenerationModelOutput(
            loss=loss,
            logits=logits,
            vision_outputs=vision_outputs,
            qformer_outputs=query_outputs,
            language_model_outputs=outputs,
        )

    def prepare_inputs_for_chat(
        self,
        tokenizer: PreTrainedTokenizer,
        query: str,
        pixel_values: torch.Tensor,
        previous_querys: List[str],
        previous_outputs: List[str],
        max_length: int,
    ):
        # 1. process input_ids
        assert len(previous_querys) == len(previous_outputs)
        device = self.device
        prefix = self.config.prompt_prefix
        human_name = self.config.human_name
        assistant_name = self.config.assistant_name
        input_ids_before_image = tokenizer(
            prefix, return_tensors="pt").input_ids.to(device)
        inputs_ids_after_image = []
        for (p, o) in zip(previous_querys, previous_outputs):
            # {pormpt}\n[答]: {output}\n[问]:
            inputs_ids_after_image += tokenizer(f"{human_name}: {p}\n", add_special_tokens=False).input_ids + \
                tokenizer(f"{assistant_name}: {o}\n", add_special_tokens=False).input_ids

        inputs_ids_after_image += tokenizer(f"{human_name}: {query}\n",
                                            add_special_tokens=False).input_ids + tokenizer(f"{assistant_name} :",
                                            add_special_tokens=False).input_ids
        inputs_ids_after_image = torch.IntTensor([inputs_ids_after_image]).to(device)
        # 2. Prepare embeddings
        pixel_values.to(device)
        image_embeds = self.vision_model(pixel_values, return_dict=True).last_hidden_state
        image_attention_mask = torch.ones(
            image_embeds.size()[:-1], dtype=torch.long, device=image_embeds.device)
        query_tokens = self.query_tokens.expand(image_embeds.shape[0], -1, -1)
        query_outputs = self.qformer(
            query_embeds=query_tokens,
            encoder_hidden_states=image_embeds,
            encoder_attention_mask=image_attention_mask,
            return_dict=True,
        )
        query_output = query_outputs.last_hidden_state
        language_model_inputs = self.language_projection(query_output)

        # concatenate query embeddings with prompt embeddings
        prefix_inputs_embeds = self.get_input_embeddings()(input_ids_before_image)
        prompt_inputs_embeds = self.get_input_embeddings()(inputs_ids_after_image)
        inputs_embeds = torch.cat([
            prefix_inputs_embeds.to(language_model_inputs.device),
            language_model_inputs,
            prompt_inputs_embeds.to(language_model_inputs.device)], dim=1)

        if inputs_embeds.shape[1] > max_length:
            inputs_embeds = inputs_embeds[:, -max_length:, :]

        input_ids = torch.concat([
            input_ids_before_image,
            torch.tensor([tokenizer.eos_token_id]).expand(
                query_tokens.shape[:-1]).to(language_model_inputs.device),
            inputs_ids_after_image,
        ], dim=1)

        return input_ids, inputs_embeds

    def chat(self,
             tokenizer,
             query: str,
             pixel_values: torch.Tensor,
             previous_querys: List[str],
             previous_outputs: List[str],
             **generate_kwargs,):
        """
        use for generate text by chat-style
        Args:
            tokenizer (PretrainedTokenizer): llama tokenizer
            query (str): current input query
            pixel_values (torch.Tensor): image after image_processor
            prompts (List[str]): chat history
            outputs (List[str]): chat history

        Returns:
            text: generate text
        """
        input_ids, inputs_embeds = self.prepare_inputs_for_chat(
            tokenizer, query, pixel_values, previous_querys, previous_outputs, 2048
        )
        response = self.language_model.generate(
            inputs_embeds=inputs_embeds,
            attention_mask=torch.ones_like(input_ids),
            **generate_kwargs,
        )
        response = tokenizer.decode(response[0], skip_special_tokens=True)
        return response