-
Notifications
You must be signed in to change notification settings - Fork 33.9k
blip support for training
#21021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
blip support for training
#21021
Changes from 1 commit
bd67bd0
61309e9
772bd06
01a4016
27c6b98
e6f4b17
896bd63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1125,14 +1125,27 @@ def __init__(self, config: BlipConfig): | |
| self.text_decoder = BlipTextLMHeadModel(config.text_config) | ||
|
|
||
| self.decoder_pad_token_id = config.text_config.pad_token_id | ||
| self.decoder_bos_token_id = config.text_config.bos_token_id | ||
| self.decoder_start_token_id = config.text_config.bos_token_id | ||
|
|
||
| # Initialize weights and apply final processing | ||
| self.post_init() | ||
|
|
||
| def get_input_embeddings(self) -> nn.Module: | ||
| return self.vision_model.embeddings.patch_embedding | ||
|
|
||
| # Adapted from transformers.models.t5.modeling_t5.T5PreTrainedModel._shift_right | ||
| def _shift_right(self, input_ids): | ||
| pad_token_id = self.decoder_pad_token_id | ||
|
|
||
| shifted_input_ids = input_ids.new_zeros(input_ids.shape) | ||
| shifted_input_ids[..., 1:] = input_ids[..., :-1].clone() | ||
| shifted_input_ids[..., 0] = self.decoder_start_token_id | ||
|
|
||
| # replace possible -100 values in labels by `pad_token_id` | ||
| shifted_input_ids.masked_fill_(shifted_input_ids == -100, pad_token_id) | ||
|
|
||
| return shifted_input_ids | ||
|
|
||
| @add_start_docstrings_to_model_forward(BLIP_VISION_INPUTS_DOCSTRING) | ||
| @replace_return_docstrings(output_type=BlipTextVisionModelOutput, config_class=BlipVisionConfig) | ||
| def forward( | ||
|
|
@@ -1191,10 +1204,17 @@ def forward( | |
|
|
||
| question_embeds = question_embeds[0] if not return_dict else question_embeds.last_hidden_state | ||
|
|
||
| if decoder_input_ids is None: | ||
| decoder_input_ids = torch.LongTensor([self.decoder_bos_token_id]).repeat((batch_size, 1)) | ||
| if labels is not None and decoder_input_ids is None: | ||
| # get decoder inputs from shifting lm labels to the right - this is used in training mode | ||
| decoder_input_ids = self._shift_right(labels) | ||
| # replace possible -100 values in labels by `pad_token_id` | ||
| labels = labels.masked_fill(labels == self.decoder_pad_token_id, -100) | ||
| elif decoder_input_ids is None: | ||
| # by default use BOS token as decoder_input_ids | ||
| decoder_input_ids = torch.LongTensor([self.decoder_start_token_id]).repeat((batch_size, 1)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure there's a need for this. This is handled by the See also BART and T5 who don't have these lines.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be removed indeed |
||
|
|
||
| if labels is None: | ||
| # labels is None, but decoder_input_ids is not None, this is used for inference | ||
|
younesbelkada marked this conversation as resolved.
Outdated
|
||
| labels = decoder_input_ids.masked_fill(decoder_input_ids == self.decoder_pad_token_id, -100) | ||
|
|
||
| answer_output = self.text_decoder( | ||
|
|
@@ -1288,7 +1308,7 @@ def generate( | |
| question_attention_mask = torch.ones(question_embeds.size()[:-1], dtype=torch.long).to(question_embeds.device) | ||
|
|
||
| bos_ids = torch.full( | ||
| (question_embeds.size(0), 1), fill_value=self.decoder_bos_token_id, device=question_embeds.device | ||
| (question_embeds.size(0), 1), fill_value=self.decoder_start_token_id, device=question_embeds.device | ||
| ) | ||
|
|
||
| outputs = self.text_decoder.generate( | ||
|
|
@@ -1330,8 +1350,16 @@ def __init__(self, config: BlipConfig): | |
| # image text matching head | ||
| self.itm_head = nn.Linear(config.text_config.hidden_size, 2) | ||
|
|
||
| self.decoder_pad_token_id = config.text_config.pad_token_id | ||
| self.decoder_bos_token_id = config.text_config.bos_token_id | ||
| self.decoder_pad_token_id = ( | ||
| config.text_config.pad_token_id | ||
| if not hasattr(config, "decoder_pad_token_id") | ||
| else config.decoder_pad_token_id | ||
| ) | ||
| self.decoder_start_token_id = ( | ||
| config.text_config.bos_token_id | ||
| if not hasattr(config, "decoder_start_token_id") | ||
| else config.decoder_start_token_id | ||
| ) | ||
|
|
||
| # Initialize weights and apply final processing | ||
| self.post_init() | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.