-
Notifications
You must be signed in to change notification settings - Fork 6.7k
[Core] introduce PeftAdapterMixin module.
#6416
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
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
4f67ac2
introduce integrations module.
sayakpaul 8bbdb66
remove duplicate methods.
sayakpaul 9e461c1
better imports.
sayakpaul cf1cd0c
Merge branch 'main' into introduce-intrgrations
sayakpaul 2427724
Merge branch 'main' into introduce-intrgrations
sayakpaul 33844a5
Merge branch 'main' into introduce-intrgrations
sayakpaul c80d5d1
Merge branch 'main' into introduce-intrgrations
sayakpaul 763f282
move to loaders.py
sayakpaul 1240a58
remove peftadaptermixin from modelmixin.
sayakpaul 488463b
add: peftadaptermixin selectively.
sayakpaul b6e7465
add: entry to _toctree
sayakpaul 0107460
Empty-Commit
sayakpaul 84b1fb1
Merge branch 'main' into introduce-intrgrations
sayakpaul c011038
Merge branch 'main' into introduce-intrgrations
sayakpaul de5f402
Merge branch 'main' into introduce-intrgrations
sayakpaul 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
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 |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <!--Copyright 2023 The HuggingFace Team. All rights reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
| the License. You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
| an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
| specific language governing permissions and limitations under the License. | ||
| --> | ||
|
|
||
| # PEFT | ||
|
|
||
| Diffusers supports working with adapters (such as [LoRA](../../using-diffusers/loading_adapters)) via the [`peft` library](https://huggingface.co/docs/peft/index). We provide a `PeftAdapterMixin` class to handle this for modeling classes in Diffusers (such as [`UNet2DConditionModel`]). | ||
|
|
||
| <Tip> | ||
|
|
||
| Refer to [this doc](../../tutorials/using_peft_for_inference.md) to get an overview of how to work with `peft` in Diffusers for inference. | ||
|
|
||
| </Tip> | ||
|
|
||
| ## PeftAdapterMixin | ||
|
|
||
| [[autodoc]] loaders.peft.PeftAdapterMixin |
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 |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from ..utils import DIFFUSERS_SLOW_IMPORT, _LazyModule, deprecate | ||
| from ..utils.import_utils import is_torch_available, is_transformers_available | ||
| from ..utils.import_utils import is_peft_available, is_torch_available, is_transformers_available | ||
|
|
||
|
|
||
| def text_encoder_lora_state_dict(text_encoder): | ||
|
|
@@ -64,6 +64,8 @@ def text_encoder_attn_modules(text_encoder): | |
| _import_structure["textual_inversion"] = ["TextualInversionLoaderMixin"] | ||
| _import_structure["ip_adapter"] = ["IPAdapterMixin"] | ||
|
|
||
| _import_structure["peft"] = ["PeftAdapterMixin"] | ||
|
|
||
|
|
||
| if TYPE_CHECKING or DIFFUSERS_SLOW_IMPORT: | ||
| if is_torch_available(): | ||
|
|
@@ -76,6 +78,8 @@ def text_encoder_attn_modules(text_encoder): | |
| from .lora import LoraLoaderMixin, StableDiffusionXLLoraLoaderMixin | ||
| from .single_file import FromSingleFileMixin | ||
| from .textual_inversion import TextualInversionLoaderMixin | ||
|
|
||
| from .peft import PeftAdapterMixin | ||
|
Member
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. |
||
| else: | ||
| import sys | ||
|
|
||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| # coding=utf-8 | ||
| # Copyright 2023 The HuggingFace Inc. team. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from typing import List, Union | ||
|
|
||
| from ..utils import MIN_PEFT_VERSION, check_peft_version, is_peft_available | ||
|
|
||
|
|
||
| class PeftAdapterMixin: | ||
| """ | ||
| A class containing all functions for loading and using adapters weights that are supported in PEFT library. For | ||
| more details about adapters and injecting them on a transformer-based model, check out the documentation of PEFT | ||
| library: https://huggingface.co/docs/peft/index. | ||
|
|
||
|
|
||
| With this mixin, if the correct PEFT version is installed, it is possible to: | ||
|
|
||
| - Attach new adapters in the model. | ||
| - Attach multiple adapters and iteratively activate / deactivate them. | ||
| - Activate / deactivate all adapters from the model. | ||
| - Get a list of the active adapters. | ||
| """ | ||
|
|
||
| _hf_peft_config_loaded = False | ||
|
|
||
| def add_adapter(self, adapter_config, adapter_name: str = "default") -> None: | ||
| r""" | ||
| Adds a new adapter to the current model for training. If no adapter name is passed, a default name is assigned | ||
| to the adapter to follow the convention of the PEFT library. | ||
|
|
||
| If you are not familiar with adapters and PEFT methods, we invite you to read more about them in the PEFT | ||
| [documentation](https://huggingface.co/docs/peft). | ||
|
|
||
| Args: | ||
| adapter_config (`[~peft.PeftConfig]`): | ||
| The configuration of the adapter to add; supported adapters are non-prefix tuning and adaption prompt | ||
| methods. | ||
| adapter_name (`str`, *optional*, defaults to `"default"`): | ||
| The name of the adapter to add. If no name is passed, a default name is assigned to the adapter. | ||
| """ | ||
| check_peft_version(min_version=MIN_PEFT_VERSION) | ||
|
|
||
| if not is_peft_available(): | ||
| raise ImportError("PEFT is not available. Please install PEFT to use this function: `pip install peft`.") | ||
|
|
||
| from peft import PeftConfig, inject_adapter_in_model | ||
|
|
||
| if not self._hf_peft_config_loaded: | ||
| self._hf_peft_config_loaded = True | ||
| elif adapter_name in self.peft_config: | ||
| raise ValueError(f"Adapter with name {adapter_name} already exists. Please use a different name.") | ||
|
|
||
| if not isinstance(adapter_config, PeftConfig): | ||
| raise ValueError( | ||
| f"adapter_config should be an instance of PeftConfig. Got {type(adapter_config)} instead." | ||
| ) | ||
|
|
||
| # Unlike transformers, here we don't need to retrieve the name_or_path of the unet as the loading logic is | ||
| # handled by the `load_lora_layers` or `LoraLoaderMixin`. Therefore we set it to `None` here. | ||
| adapter_config.base_model_name_or_path = None | ||
| inject_adapter_in_model(adapter_config, self, adapter_name) | ||
| self.set_adapter(adapter_name) | ||
|
|
||
| def set_adapter(self, adapter_name: Union[str, List[str]]) -> None: | ||
| """ | ||
| Sets a specific adapter by forcing the model to only use that adapter and disables the other adapters. | ||
|
|
||
| If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT | ||
| official documentation: https://huggingface.co/docs/peft | ||
|
|
||
| Args: | ||
| adapter_name (Union[str, List[str]])): | ||
| The list of adapters to set or the adapter name in case of single adapter. | ||
| """ | ||
| check_peft_version(min_version=MIN_PEFT_VERSION) | ||
|
|
||
| if not self._hf_peft_config_loaded: | ||
| raise ValueError("No adapter loaded. Please load an adapter first.") | ||
|
|
||
| if isinstance(adapter_name, str): | ||
| adapter_name = [adapter_name] | ||
|
|
||
| missing = set(adapter_name) - set(self.peft_config) | ||
| if len(missing) > 0: | ||
| raise ValueError( | ||
| f"Following adapter(s) could not be found: {', '.join(missing)}. Make sure you are passing the correct adapter name(s)." | ||
| f" current loaded adapters are: {list(self.peft_config.keys())}" | ||
| ) | ||
|
|
||
| from peft.tuners.tuners_utils import BaseTunerLayer | ||
|
|
||
| _adapters_has_been_set = False | ||
|
|
||
| for _, module in self.named_modules(): | ||
| if isinstance(module, BaseTunerLayer): | ||
| if hasattr(module, "set_adapter"): | ||
| module.set_adapter(adapter_name) | ||
| # Previous versions of PEFT does not support multi-adapter inference | ||
| elif not hasattr(module, "set_adapter") and len(adapter_name) != 1: | ||
| raise ValueError( | ||
| "You are trying to set multiple adapters and you have a PEFT version that does not support multi-adapter inference. Please upgrade to the latest version of PEFT." | ||
| " `pip install -U peft` or `pip install -U git+https://github.com/huggingface/peft.git`" | ||
| ) | ||
| else: | ||
| module.active_adapter = adapter_name | ||
| _adapters_has_been_set = True | ||
|
|
||
| if not _adapters_has_been_set: | ||
| raise ValueError( | ||
| "Did not succeeded in setting the adapter. Please make sure you are using a model that supports adapters." | ||
| ) | ||
|
|
||
| def disable_adapters(self) -> None: | ||
| r""" | ||
| Disable all adapters attached to the model and fallback to inference with the base model only. | ||
|
|
||
| If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT | ||
| official documentation: https://huggingface.co/docs/peft | ||
| """ | ||
| check_peft_version(min_version=MIN_PEFT_VERSION) | ||
|
|
||
| if not self._hf_peft_config_loaded: | ||
| raise ValueError("No adapter loaded. Please load an adapter first.") | ||
|
|
||
| from peft.tuners.tuners_utils import BaseTunerLayer | ||
|
|
||
| for _, module in self.named_modules(): | ||
| if isinstance(module, BaseTunerLayer): | ||
| if hasattr(module, "enable_adapters"): | ||
| module.enable_adapters(enabled=False) | ||
| else: | ||
| # support for older PEFT versions | ||
| module.disable_adapters = True | ||
|
|
||
| def enable_adapters(self) -> None: | ||
| """ | ||
| Enable adapters that are attached to the model. The model will use `self.active_adapters()` to retrieve the | ||
| list of adapters to enable. | ||
|
|
||
| If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT | ||
| official documentation: https://huggingface.co/docs/peft | ||
| """ | ||
| check_peft_version(min_version=MIN_PEFT_VERSION) | ||
|
|
||
| if not self._hf_peft_config_loaded: | ||
| raise ValueError("No adapter loaded. Please load an adapter first.") | ||
|
|
||
| from peft.tuners.tuners_utils import BaseTunerLayer | ||
|
|
||
| for _, module in self.named_modules(): | ||
| if isinstance(module, BaseTunerLayer): | ||
| if hasattr(module, "enable_adapters"): | ||
| module.enable_adapters(enabled=True) | ||
| else: | ||
| # support for older PEFT versions | ||
| module.disable_adapters = False | ||
|
|
||
| def active_adapters(self) -> List[str]: | ||
| """ | ||
| Gets the current list of active adapters of the model. | ||
|
|
||
| If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT | ||
| official documentation: https://huggingface.co/docs/peft | ||
| """ | ||
| check_peft_version(min_version=MIN_PEFT_VERSION) | ||
|
|
||
| if not is_peft_available(): | ||
| raise ImportError("PEFT is not available. Please install PEFT to use this function: `pip install peft`.") | ||
|
|
||
| if not self._hf_peft_config_loaded: | ||
| raise ValueError("No adapter loaded. Please load an adapter first.") | ||
|
|
||
| from peft.tuners.tuners_utils import BaseTunerLayer | ||
|
|
||
| for _, module in self.named_modules(): | ||
| if isinstance(module, BaseTunerLayer): | ||
| return module.active_adapter |
Oops, something went wrong.
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.
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 is out of the condition because:
peft. So, if we add the above underis_peft_available(), we will run into import errors.Can move it under
is_torch_available(), though.@DN6 let me know if you have a better way of handling this.