-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
support HRA #1864
Merged
Merged
support HRA #1864
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a66737e
support HRA
DaShenZi721 9593be3
fix bugs and add tests
DaShenZi721 96c11db
update
DaShenZi721 3597331
update ruff and make style
DaShenZi721 2098f87
fix bug
DaShenZi721 49c5dd1
make style
DaShenZi721 e0d4229
modify parameter names
DaShenZi721 628ad7c
update year
DaShenZi721 18e3e3f
update year
DaShenZi721 38bb3f3
add tests
DaShenZi721 c7bcfc6
make style
DaShenZi721 987e847
update and add tests
DaShenZi721 c679908
update hra_dreambooth
DaShenZi721 cd667f0
update
DaShenZi721 3ebdd73
Merge branch 'main' into hra
DaShenZi721 2ee4e93
fix bug and update hra_example
DaShenZi721 c4b667e
Merge branch 'hra' of github.com:DaShenZi721/peft into hra
DaShenZi721 10fb707
delete local path
DaShenZi721 d61e219
Merge branch 'main' into hra
DaShenZi721 6eb0008
make style
DaShenZi721 e258353
make style
DaShenZi721 6cab325
update
DaShenZi721 0a3d22c
make style
DaShenZi721 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 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 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 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 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 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,20 @@ | ||
# Copyright 2023-present 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 .config import HRAConfig | ||
from .layer import Conv2d, Linear, HRALayer | ||
from .model import HRAModel | ||
|
||
|
||
__all__ = ["HRAConfig", "HRAModel", "Conv2d", "Linear", "HRALayer"] |
This file contains 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,111 @@ | ||
# Copyright 2023-present 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 dataclasses import dataclass, field | ||
from typing import List, Optional, Union | ||
|
||
from peft.tuners.lycoris_utils import LycorisConfig | ||
from peft.utils import PeftType | ||
|
||
|
||
@dataclass | ||
class HRAConfig(LycorisConfig): | ||
""" | ||
This is the configuration class to store the configuration of a [`HRAModel`]. | ||
|
||
Args: | ||
r (`int`): HRA rank. | ||
target_modules (`Optional[Union[List[str], str]]`): | ||
The names of the modules to apply the adapter to. If this is specified, only the modules with the specified | ||
names will be replaced. When passing a string, a regex match will be performed. When passing a list of | ||
strings, either an exact match will be performed or it is checked if the name of the module ends with any | ||
of the passed strings. If this is specified as 'all-linear', then all linear modules are chosen, excluding | ||
the output layer. If this is not specified, modules will be chosen according to the model architecture. If | ||
the architecture is not known, an error will be raised -- in this case, you should specify the target | ||
modules manually. | ||
init_weights (`bool`): | ||
Whether to perform initialization of HRA weights. | ||
symmetric_init_weights (`bool`): | ||
Whether to perform symmetric initialization of HRA weights. | ||
layers_to_transform (`Union[List[int], int]`): | ||
The layer indices to transform. If a list of ints is passed, it will apply the adapter to the layer indices | ||
that are specified in this list. If a single integer is passed, it will apply the transformations on the | ||
layer at this index. | ||
layers_pattern (`str`): | ||
The layer pattern name, used only if `layers_to_transform` is different from `None`. | ||
rank_pattern (`dict`): | ||
The mapping from layer names or regexp expression to ranks which are different from the default rank | ||
specified by `r`. | ||
modules_to_save (`List[str]`): | ||
List of modules apart from adapter layers to be set as trainable and saved in the final checkpoint. | ||
apply_GS (`bool`): | ||
Whether to apply Gram-Schmidt orthogonalization. | ||
""" | ||
|
||
r: int = field(default=8, metadata={"help": "HRA rank"}) | ||
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. IIUC, 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. Done. |
||
target_modules: Optional[Union[List[str], str]] = field( | ||
default=None, | ||
metadata={ | ||
"help": "List of module names or regex expression of the module names to replace with HRA." | ||
"For example, ['q', 'v'] or '.*decoder.*(SelfAttention|EncDecAttention).*(q|v)$' " | ||
"This can also be a wildcard 'all-linear' which matches all linear/Conv1D layers except the output layer." | ||
}, | ||
) | ||
init_weights: bool = field( | ||
default=True, | ||
metadata={ | ||
"help": ( | ||
"Whether to initialize the weights of the HRA layers with their default initialization. Don't change " | ||
"this setting, except if you know exactly what you're doing." | ||
BenjaminBossan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), | ||
}, | ||
) | ||
symmetric_init_weights: bool = field( | ||
default=True, | ||
metadata={ | ||
"help": ( | ||
"Whether to perform symmetric initialization of the weights of the HRA layers. If init_weights is set to False or r is odd, this will default to False." | ||
), | ||
}, | ||
) | ||
layers_to_transform: Optional[Union[List[int], int]] = field( | ||
default=None, | ||
metadata={ | ||
"help": "The layer indexes to transform, is this argument is specified, PEFT will transform only the layers indexes that are specified inside this list. If a single integer is passed, PEFT will transform only the layer at this index." | ||
}, | ||
) | ||
layers_pattern: Optional[str] = field( | ||
default=None, | ||
metadata={ | ||
"help": "The layer pattern name, used only if `layers_to_transform` is different to None and if the layer pattern is not in the common layers pattern." | ||
}, | ||
) | ||
modules_to_save: Optional[List[str]] = field( | ||
default=None, | ||
metadata={ | ||
"help": "List of modules apart from HRA layers to be set as trainable and saved in the final checkpoint. " | ||
"For example, in Sequence Classification or Token Classification tasks, " | ||
"the final layer `classifier/score` are randomly initialized and as such need to be trainable and saved." | ||
}, | ||
) | ||
apply_GS: bool = field( | ||
default=False, | ||
metadata={"help": "Whether to apply Gram-Schmidt orthogonalization or not."}, | ||
) | ||
|
||
def __post_init__(self): | ||
self.peft_type = PeftType.HRA | ||
self.target_modules = ( | ||
set(self.target_modules) if isinstance(self.target_modules, list) else self.target_modules | ||
) |
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.
Please change all years to 2024.
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.
Done.