-
Notifications
You must be signed in to change notification settings - Fork 34.1k
[Blip2] Fix Blip-2 multi gpu
#21707
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
[Blip2] Fix Blip-2 multi gpu
#21707
Changes from 4 commits
357c7d0
e0104f0
e9d1a30
0383c02
535c6c1
11b9a8e
10413a4
f2c409f
9fdbacc
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 |
|---|---|---|
|
|
@@ -283,6 +283,7 @@ class Blip2PreTrainedModel(PreTrainedModel): | |
| r"position_ids", | ||
| r"language_model.encoder.embed_tokens.weight", | ||
| r"language_model.decoder.embed_tokens.weight", | ||
| r"language_model.lm_head.weight", | ||
| ] | ||
| _no_split_modules = ["Blip2Attention", "T5Block", "OPTDecoderLayer"] | ||
| _keep_in_fp32_modules = ["wo"] | ||
|
|
@@ -1203,8 +1204,45 @@ def __init__(self, config: Blip2Config): | |
| # Initialize weights and apply final processing | ||
| self.post_init() | ||
|
|
||
| def get_input_embeddings(self) -> nn.Module: | ||
| return self.vision_model.embeddings.patch_embedding | ||
| 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" | ||
| ) | ||
|
younesbelkada marked this conversation as resolved.
|
||
|
|
||
| if hasattr(self.language_model, "_hf_hook"): | ||
| self.language_model._hf_hook.io_same_device = True # For `generate` compatibility | ||
|
|
||
| @add_start_docstrings_to_model_forward(BLIP_2_INPUTS_DOCSTRING) | ||
| @replace_return_docstrings(output_type=Blip2ForConditionalGenerationModelOutput, config_class=Blip2VisionConfig) | ||
|
|
@@ -1311,7 +1349,7 @@ def forward( | |
| language_model_inputs.size()[:-1], dtype=torch.long, device=language_model_inputs.device | ||
| ) | ||
| inputs_embeds = self.language_model.get_input_embeddings()(input_ids) | ||
| inputs_embeds = torch.cat([language_model_inputs, inputs_embeds], dim=1) | ||
| inputs_embeds = torch.cat([language_model_inputs, inputs_embeds.to(language_model_inputs.device)], dim=1) | ||
|
|
||
| if attention_mask is None: | ||
| attention_mask = torch.ones_like(input_ids) | ||
|
|
@@ -1387,6 +1425,9 @@ def generate( | |
| Returns: | ||
| captions (list): A list of strings of length batch_size * num_captions. | ||
| """ | ||
| # preprocess for `accelerate` | ||
| self._preprocess_accelerate() | ||
|
|
||
| batch_size = pixel_values.shape[0] | ||
| 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) | ||
|
|
@@ -1415,8 +1456,8 @@ def generate( | |
| attention_mask = torch.cat([language_attention_mask, attention_mask], dim=1) | ||
|
Contributor
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. @younesbelkada Hi, thanks a lot for the effort for unblocking this issue! Could you double-confirm on your environment if this is the case too? |
||
|
|
||
| # concatenate query embeddings with prompt embeddings | ||
| inputs_embeds = self.language_model.get_input_embeddings()(input_ids) | ||
| inputs_embeds = torch.cat([language_model_inputs, inputs_embeds], dim=1) | ||
| inputs_embeds = self.get_input_embeddings()(input_ids) | ||
| inputs_embeds = torch.cat([language_model_inputs, inputs_embeds.to(language_model_inputs.device)], dim=1) | ||
|
|
||
| outputs = self.language_model.generate( | ||
| inputs_embeds=inputs_embeds, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should never happen as Accelerate will error if you pass along a device_map that is incomplete.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, what I meant here is to have a device map that contains the exact key
language_model, happy to edit the error message a bit if you wantUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@younesbelkada
Can we make this warning more scalable towards larger models? e.g., when trying to use
Salesforce/blip2-flan-t5-xxl, the wholelanguage_modelmay not fit into consumer-level GPUs (e.g., on my 16GB V100).I believe adding the link to https://github.com/huggingface/blog/blob/main/accelerate-large-models.md and guide it to make it work on larger models would be quite helpful.
FYI, to scale it to larger models, I follow what is instructed in https://github.com/huggingface/blog/blob/main/accelerate-large-models.md i.e.,
For OPT-* models,
For Flan-T5-* models,
Again, thanks a lot for this PR!