Skip to content
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

FEAT: Adding exclude modules param(#2044) #2102

Merged
merged 20 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/peft/tuners/adalora/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ def __post_init__(self):
self.target_modules = (
set(self.target_modules) if isinstance(self.target_modules, list) else self.target_modules
)
self.exclude_modules = (
set(self.exclude_modules) if isinstance(self.exclude_modules, list) else self.exclude_modules
)
# if target_modules is a regex expression, then layers_to_transform should be None
if isinstance(self.target_modules, str) and self.layers_to_transform is not None:
raise ValueError("`layers_to_transform` cannot be used when `target_modules` is a str.")
Expand Down
13 changes: 13 additions & 0 deletions src/peft/tuners/boft/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class BOFTConfig(PeftConfig):
boft_block_num (`int`): Number of BOFT blocks per injected layer.
boft_n_butterfly_factor (`int`): Number of butterfly factors across different layers.
target_modules (`Union[List[str],str]`): The names of the modules to apply the adapter to.
exclude_modules (`Optional[Union[List[str], str]]`):
The names of the modules to not apply the adapter. 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.
boft_dropout (`float`): The multiplicative dropout probability for BOFT layers.
fan_in_fan_out (`bool`): Set this to True if the layer to replace stores weight like (fan_in, fan_out).
For example, gpt-2 uses `Conv1D` which stores weights like (fan_in, fan_out) and hence this should be set
Expand Down Expand Up @@ -81,6 +85,12 @@ class BOFTConfig(PeftConfig):
"example": "For example, ['q', 'v'] or '.*decoder.*(SelfAttention|EncDecAttention).*(q|v)$' ",
},
)
exclude_modules: Optional[Union[list[str], str]] = field(
default=None,
metadata={
"help": "List of module names or regex expression of the module names to exclude from BOFT."
},
)
boft_dropout: float = field(default=0.0, metadata={"help": "BOFT multiplicative dropout"})
fan_in_fan_out: bool = field(
default=False,
Expand Down Expand Up @@ -124,6 +134,9 @@ def __post_init__(self):
self.target_modules = (
set(self.target_modules) if isinstance(self.target_modules, list) else self.target_modules
)
self.exclude_modules = (
set(self.exclude_modules) if isinstance(self.exclude_modules, list) else self.exclude_modules
)
if self.boft_block_size == 0 and self.boft_block_num == 0:
raise ValueError("You must specify either boft_block_size or boft_block_num.")
if not (self.boft_block_size != 0) ^ (self.boft_block_num != 0):
Expand Down
13 changes: 13 additions & 0 deletions src/peft/tuners/fourierft/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ class FourierFTConfig(PeftConfig):
target_modules (`Union[list[str],str]`):
List of module names or regex expression of the module names to replace with FourierFT. For example, ['q',
'v'] or '.*decoder.*(SelfAttention|EncDecAttention).*(q|v)$'. Only linear layers are supported.
exclude_modules (`Optional[Union[List[str], str]]`):
The names of the modules to not apply the adapter. 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.
fan_in_fan_out (`bool`):
Set this to True if the layer to replace stores weight like (fan_in, fan_out).
bias (`str`):
Expand Down Expand Up @@ -123,6 +127,12 @@ class FourierFTConfig(PeftConfig):
)
},
)
exclude_modules: Optional[Union[list[str], str]] = field(
default=None,
metadata={
"help": "List of module names or regex expression of the module names to exclude from fourierft."
},
)
bias: str = field(
default="none", metadata={"help": "Bias type for FourierFT. Can be 'none', 'all' or 'fourier_only'."}
)
Expand Down Expand Up @@ -179,6 +189,9 @@ def __post_init__(self):
self.target_modules = (
set(self.target_modules) if isinstance(self.target_modules, list) else self.target_modules
)
self.exclude_modules = (
set(self.exclude_modules) if isinstance(self.exclude_modules, list) else self.exclude_modules
)
# if target_modules is a regex expression, then layers_to_transform should be None
if isinstance(self.target_modules, str) and self.layers_to_transform is not None:
raise ValueError("`layers_to_transform` cannot be used when `target_modules` is a str.")
Expand Down
13 changes: 13 additions & 0 deletions src/peft/tuners/hra/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ class HRAConfig(PeftConfig):
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.
exclude_modules (`Optional[Union[List[str], str]]`):
The names of the modules to not apply the adapter. 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.
init_weights (`bool`):
Whether to perform initialization of HRA weights.
layers_to_transform (`Union[List[int], int]`):
Expand Down Expand Up @@ -71,6 +75,12 @@ class HRAConfig(PeftConfig):
"example": "For example, ['q', 'v'] or '.*decoder.*(SelfAttention|EncDecAttention).*(q|v)$' ",
},
)
exclude_modules: Optional[Union[list[str], str]] = field(
default=None,
metadata={
"help": "List of module names or regex expression of the module names to exclude from HRA."
},
)
init_weights: bool = field(
default=True,
metadata={
Expand Down Expand Up @@ -107,6 +117,9 @@ def __post_init__(self):
self.target_modules = (
set(self.target_modules) if isinstance(self.target_modules, list) else self.target_modules
)
self.exclude_modules = (
set(self.exclude_modules) if isinstance(self.exclude_modules, list) else self.exclude_modules
)
# if target_modules is a regex expression, then layers_to_transform should be None
if isinstance(self.target_modules, str) and self.layers_to_transform is not None:
raise ValueError("`layers_to_transform` cannot be used when `target_modules` is a str.")
Expand Down
13 changes: 13 additions & 0 deletions src/peft/tuners/ia3/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class IA3Config(PeftConfig):
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.
exclude_modules (`Optional[Union[List[str], str]]`):
The names of the modules to not apply the adapter. 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.
feedforward_modules (`Optional[Union[List[str], str]]`):
The names of the modules to be treated as feedforward modules, as in the original paper. These modules will
have (IA)³ vectors multiplied to the input, instead of the output. `feedforward_modules` must be a name or
Expand All @@ -59,6 +63,12 @@ class IA3Config(PeftConfig):
),
},
)
exclude_modules: Optional[Union[list[str], str]] = field(
default=None,
metadata={
"help": "List of module names or regex expression of the module names to exclude from (IA)³."
},
)
feedforward_modules: Optional[Union[List[str], str]] = field(
default=None,
metadata={
Expand Down Expand Up @@ -88,6 +98,9 @@ def __post_init__(self):
self.target_modules = (
set(self.target_modules) if isinstance(self.target_modules, list) else self.target_modules
)
self.exclude_modules = (
set(self.exclude_modules) if isinstance(self.exclude_modules, list) else self.exclude_modules
)
self.feedforward_modules = (
set(self.feedforward_modules) if isinstance(self.feedforward_modules, list) else self.feedforward_modules
)
Expand Down
10 changes: 10 additions & 0 deletions src/peft/tuners/ln_tuning/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class LNTuningConfig(PeftConfig):
'.*decoder.*' or '.*encoder.*'. 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.
exclude_modules (`Optional[Union[List[str], str]]`):
The names of the modules to not apply the adapter. 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.
modules_to_save (`Optional[Union[List[str], str]]`):
List of modules 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
Expand All @@ -48,6 +52,12 @@ class LNTuningConfig(PeftConfig):
),
},
)
exclude_modules: Optional[Union[list[str], str]] = field(
default=None,
metadata={
"help": "List of module names or regex expression of the module names to exclude from LNTuning."
},
)
modules_to_save: Optional[Union[list[str], str]] = field(
default=None,
metadata={
Expand Down
13 changes: 13 additions & 0 deletions src/peft/tuners/loha/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class LoHaConfig(LycorisConfig):
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.
exclude_modules (`Optional[Union[List[str], str]]`):
The names of the modules to not apply the adapter. 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.
init_weights (`bool`):
Whether to perform initialization of adapter weights. This defaults to `True`, passing `False` is
discouraged.
Expand Down Expand Up @@ -84,6 +88,12 @@ class LoHaConfig(LycorisConfig):
"This can also be a wildcard 'all-linear' which matches all linear/Conv1D layers except the output layer."
},
)
exclude_modules: Optional[Union[list[str], str]] = field(
default=None,
metadata={
"help": "List of module names or regex expression of the module names to exclude from LoHa."
},
)
init_weights: bool = field(
default=True,
metadata={
Expand Down Expand Up @@ -119,3 +129,6 @@ def __post_init__(self):
self.target_modules = (
set(self.target_modules) if isinstance(self.target_modules, list) else self.target_modules
)
self.exclude_modules = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conversion to set is missing in the LoKrConfig.

set(self.exclude_modules) if isinstance(self.exclude_modules, list) else self.exclude_modules
)
10 changes: 10 additions & 0 deletions src/peft/tuners/lokr/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ class LoKrConfig(LycorisConfig):
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.
exclude_modules (`Optional[Union[List[str], str]]`):
The names of the modules to not apply the adapter. 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.
init_weights (`bool`):
Whether to perform initialization of adapter weights. This defaults to `True`, passing `False` is
discouraged.
Expand Down Expand Up @@ -93,6 +97,12 @@ class LoKrConfig(LycorisConfig):
"This can also be a wildcard 'all-linear' which matches all linear/Conv1D layers except the output layer."
},
)
exclude_modules: Optional[Union[list[str], str]] = field(
default=None,
metadata={
"help": "List of module names or regex expression of the module names to exclude from LoKr."
},
)
init_weights: bool = field(
default=True,
metadata={
Expand Down
13 changes: 13 additions & 0 deletions src/peft/tuners/lora/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ class LoraConfig(PeftConfig):
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.
exclude_modules (`Optional[Union[List[str], str]]`):
The names of the modules to not apply the adapter. 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.
lora_alpha (`int`):
The alpha parameter for Lora scaling.
lora_dropout (`float`):
Expand Down Expand Up @@ -166,6 +170,12 @@ class LoraConfig(PeftConfig):
),
},
)
exclude_modules: Optional[Union[list[str], str]] = field(
default=None,
metadata={
"help": "List of module names or regex expression of the module names to exclude from Lora."
},
)
lora_alpha: int = field(default=8, metadata={"help": "Lora alpha"})
lora_dropout: float = field(default=0.0, metadata={"help": "Lora dropout"})
fan_in_fan_out: bool = field(
Expand Down Expand Up @@ -327,6 +337,9 @@ def __post_init__(self):
self.target_modules = (
set(self.target_modules) if isinstance(self.target_modules, list) else self.target_modules
)
self.exclude_modules = (
set(self.exclude_modules) if isinstance(self.exclude_modules, list) else self.exclude_modules
)
# if target_modules is a regex expression, then layers_to_transform should be None
if isinstance(self.target_modules, str) and self.layers_to_transform is not None:
raise ValueError("`layers_to_transform` cannot be used when `target_modules` is a str.")
Expand Down
13 changes: 13 additions & 0 deletions src/peft/tuners/oft/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class OFTConfig(LycorisConfig):
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.
exclude_modules (`Optional[Union[List[str], str]]`):
The names of the modules to not apply the adapter. 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.
init_weights (`bool`):
Whether to perform initialization of OFT weights.
layers_to_transform (`Union[List[int], int]`):
Expand Down Expand Up @@ -68,6 +72,12 @@ class OFTConfig(LycorisConfig):
"This can also be a wildcard 'all-linear' which matches all linear/Conv1D layers except the output layer."
},
)
exclude_modules: Optional[Union[list[str], str]] = field(
default=None,
metadata={
"help": "List of module names or regex expression of the module names to exclude from OFT."
},
)
init_weights: bool = field(
default=True,
metadata={
Expand Down Expand Up @@ -117,3 +127,6 @@ def __post_init__(self):
self.target_modules = (
set(self.target_modules) if isinstance(self.target_modules, list) else self.target_modules
)
self.exclude_modules = (
set(self.exclude_modules) if isinstance(self.exclude_modules, list) else self.exclude_modules
)
13 changes: 13 additions & 0 deletions src/peft/tuners/poly/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class PolyConfig(PeftConfig):
Args:
r (`int`): Attention dimension of each Lora in Poly.
target_modules (`Union[List[str],str]`): The names of the modules to apply Poly to.
exclude_modules (`Optional[Union[List[str], str]]`):
The names of the modules to not apply the adapter. 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.
modules_to_save (`List[str]`): List of modules apart from Poly layers to be set as trainable
and saved in the final checkpoint.
init_weights (bool): Whether to perform initialization of Poly weights.
Expand All @@ -48,6 +52,12 @@ class PolyConfig(PeftConfig):
"For example, ['q', 'v'] or '.*decoder.*(SelfAttention|EncDecAttention).*(q|v)$' "
},
)
exclude_modules: Optional[Union[list[str], str]] = field(
default=None,
metadata={
"help": "List of module names or regex expression of the module names to exclude from Poly."
},
)
modules_to_save: Optional[List[str]] = field(
default=None,
metadata={
Expand Down Expand Up @@ -87,3 +97,6 @@ def __post_init__(self):
self.target_modules = (
set(self.target_modules) if isinstance(self.target_modules, list) else self.target_modules
)
self.exclude_modules = (
set(self.exclude_modules) if isinstance(self.exclude_modules, list) else self.exclude_modules
)
Loading