-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Add unload_ip_adapter method #6192
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
Merged
patrickvonplaten
merged 7 commits into
huggingface:main
from
fabiorigano:unloadipadapter
Jan 2, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a28a6e5
Add unload_ip_adapter method
fabiorigano a760578
Merge branch 'main' into unloadipadapter
sayakpaul ce9b12a
Update attn_processors with original layers
fabiorigano 4122918
Merge branch 'main' into unloadipadapter
sayakpaul a11bb5c
Merge branch 'main' into unloadipadapter
fabiorigano a5fcc6a
Add test
fabiorigano 412194f
Use set_default_attn_processor
fabiorigano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,7 +132,7 @@ def load_ip_adapter( | |
| if keys != ["image_proj", "ip_adapter"]: | ||
| raise ValueError("Required keys are (`image_proj` and `ip_adapter`) missing from the state dict.") | ||
|
|
||
| # load CLIP image encoer here if it has not been registered to the pipeline yet | ||
| # load CLIP image encoder here if it has not been registered to the pipeline yet | ||
| if hasattr(self, "image_encoder") and getattr(self, "image_encoder", None) is None: | ||
| if not isinstance(pretrained_model_name_or_path_or_dict, dict): | ||
| logger.info(f"loading image_encoder from {pretrained_model_name_or_path_or_dict}") | ||
|
|
@@ -141,12 +141,14 @@ def load_ip_adapter( | |
| subfolder=os.path.join(subfolder, "image_encoder"), | ||
| ).to(self.device, dtype=self.dtype) | ||
| self.image_encoder = image_encoder | ||
| self.register_to_config(image_encoder=["transformers", "CLIPVisionModelWithProjection"]) | ||
| else: | ||
| raise ValueError("`image_encoder` cannot be None when using IP Adapters.") | ||
|
|
||
| # create feature extractor if it has not been registered to the pipeline yet | ||
| if hasattr(self, "feature_extractor") and getattr(self, "feature_extractor", None) is None: | ||
| self.feature_extractor = CLIPImageProcessor() | ||
| self.register_to_config(feature_extractor=["transformers", "CLIPImageProcessor"]) | ||
|
|
||
| # load ip-adapter into unet | ||
| self.unet._load_ip_adapter_weights(state_dict) | ||
|
|
@@ -155,3 +157,32 @@ def set_ip_adapter_scale(self, scale): | |
| for attn_processor in self.unet.attn_processors.values(): | ||
| if isinstance(attn_processor, (IPAdapterAttnProcessor, IPAdapterAttnProcessor2_0)): | ||
| attn_processor.scale = scale | ||
|
|
||
| def unload_ip_adapter(self): | ||
|
Member
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. Works for me! |
||
| """ | ||
| Unloads the IP Adapter weights | ||
|
|
||
| Examples: | ||
|
|
||
| ```python | ||
| >>> # Assuming `pipeline` is already loaded with the IP Adapter weights. | ||
| >>> pipeline.unload_ip_adapter() | ||
| >>> ... | ||
| ``` | ||
| """ | ||
| # remove CLIP image encoder | ||
| if hasattr(self, "image_encoder") and getattr(self, "image_encoder", None) is not None: | ||
| self.image_encoder = None | ||
| self.register_to_config(image_encoder=[None, None]) | ||
|
|
||
| # remove feature extractor | ||
| if hasattr(self, "feature_extractor") and getattr(self, "feature_extractor", None) is not None: | ||
| self.feature_extractor = None | ||
| self.register_to_config(feature_extractor=[None, None]) | ||
|
|
||
| # remove hidden encoder | ||
| self.unet.encoder_hid_proj = None | ||
| self.config.encoder_hid_dim_type = None | ||
|
|
||
| # restore original Unet attention processors layers | ||
| self.unet.set_default_attn_processor() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.