Skip to content

Commit

Permalink
DOC Improve docs for layers_pattern argument (huggingface#2157)
Browse files Browse the repository at this point in the history
Addresses part of huggingface#2155.

Also fix type annotations where appropriate.
  • Loading branch information
BenjaminBossan authored and sirluk committed Oct 19, 2024
1 parent 9af2058 commit c0f1722
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 40 deletions.
10 changes: 6 additions & 4 deletions src/peft/tuners/boft/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ class BOFTConfig(PeftConfig):
The layer indexes to transform, if this argument is specified, it will apply the BOFT transformations on
the layer indexes that are specified in this list. If a single integer is passed, it will apply the BOFT
transformations on the layer at this index.
layers_pattern (`str`):
layers_pattern (`Optional[Union[List[str], str]]`):
The layer pattern name, used only if `layers_to_transform` is different from `None` and if the layer
pattern is not in the common layers pattern.
pattern is not in the common layers pattern. This should target the `nn.ModuleList` of the model, which is
often called `'layers'` or `'h'`.
"""

boft_block_size: int = field(
Expand Down Expand Up @@ -129,10 +130,11 @@ class BOFTConfig(PeftConfig):
"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(
layers_pattern: Optional[Union[list[str], 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."
"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. "
"This should target the `nn.ModuleList` of the model, which is often called `'layers'` or `'h'`."
},
)

Expand Down
10 changes: 6 additions & 4 deletions src/peft/tuners/fourierft/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ class FourierFTConfig(PeftConfig):
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 (`str`):
layers_pattern (`Optional[Union[List[str], str]]`):
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.
not in the common layers pattern. This should target the `nn.ModuleList` of the model, which is often
called `'layers'` or `'h'`.
n_frequency_pattern (`dict`):
The mapping from layer names or regexp expression to n_frequency which are different from the default
specified. For example, `{model.decoder.layers.0.encoder_attn.k_proj: 1000`}.
Expand Down Expand Up @@ -154,12 +155,13 @@ class FourierFTConfig(PeftConfig):
)
},
)
layers_pattern: Optional[str] = field(
layers_pattern: Optional[Union[list[str], 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."
" pattern is not in the common layers pattern. This should target the `nn.ModuleList` of the "
"model, which is often called `'layers'` or `'h'`."
)
},
)
Expand Down
10 changes: 6 additions & 4 deletions src/peft/tuners/hra/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ class HRAConfig(PeftConfig):
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`.
layers_pattern (`Optional[Union[List[str], str]]`):
The layer pattern name, used only if `layers_to_transform` is different from `None`. This should target the
`nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.
rank_pattern (`dict`):
The mapping from layer names or regexp expression to ranks which are different from the default rank
specified by `r`.
Expand Down Expand Up @@ -96,10 +97,11 @@ class HRAConfig(PeftConfig):
"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(
layers_pattern: Optional[Union[list[str], 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."
"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. "
"This should target the `nn.ModuleList` of the model, which is often called `'layers'` or `'h'`."
},
)
bias: str = field(default="none", metadata={"help": "Bias type for HRA. Can be 'none', 'all' or 'hra_only'"})
Expand Down
10 changes: 6 additions & 4 deletions src/peft/tuners/loha/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ class LoHaConfig(LycorisConfig):
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`.
layers_pattern (`Optional[Union[List[str], str]]`):
The layer pattern name, used only if `layers_to_transform` is different from `None`. This should target the
`nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.
rank_pattern (`dict`):
The mapping from layer names or regexp expression to ranks which are different from the default rank
specified by `r`.
Expand Down Expand Up @@ -108,10 +109,11 @@ class LoHaConfig(LycorisConfig):
"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(
layers_pattern: Optional[Union[list[str], 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."
"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. "
"This should target the `nn.ModuleList` of the model, which is often called `'layers'` or `'h'`."
},
)
modules_to_save: Optional[list[str]] = field(
Expand Down
10 changes: 6 additions & 4 deletions src/peft/tuners/lokr/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ class LoKrConfig(LycorisConfig):
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`.
layers_pattern (`Optional[Union[List[str], str]]`):
The layer pattern name, used only if `layers_to_transform` is different from `None`. This should target the
`nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.
rank_pattern (`dict`):
The mapping from layer names or regexp expression to ranks which are different from the default rank
specified by `r`.
Expand Down Expand Up @@ -117,10 +118,11 @@ class LoKrConfig(LycorisConfig):
"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(
layers_pattern: Optional[Union[list[str], 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."
"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. "
"This should target the `nn.ModuleList` of the model, which is often called `'layers'` or `'h'`."
},
)
modules_to_save: Optional[list[str]] = field(
Expand Down
8 changes: 5 additions & 3 deletions src/peft/tuners/lora/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,9 @@ class LoraConfig(PeftConfig):
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`.
layers_pattern (`Optional[Union[List[str], str]]`):
The layer pattern name, used only if `layers_to_transform` is different from `None`. This should target the
`nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.
rank_pattern (`dict`):
The mapping from layer names or regexp expression to ranks which are different from the default rank
specified by `r`.
Expand Down Expand Up @@ -259,7 +260,8 @@ class LoraConfig(PeftConfig):
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."
"This only works when target_modules is a list of str."
"This only works when target_modules is a list of str. This should target the `nn.ModuleList` of the "
"model, which is often called `'layers'` or `'h'`."
},
)
rank_pattern: Optional[dict] = field(
Expand Down
10 changes: 6 additions & 4 deletions src/peft/tuners/oft/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ class OFTConfig(PeftConfig):
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`.
layers_pattern (`Optional[Union[List[str], str]]`):
The layer pattern name, used only if `layers_to_transform` is different from `None`. This should target the
`nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.
rank_pattern (`dict`):
The mapping from layer names or regexp expression to ranks which are different from the default rank
specified by `r`.
Expand Down Expand Up @@ -117,10 +118,11 @@ class OFTConfig(PeftConfig):
"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(
layers_pattern: Optional[Union[list[str], 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."
"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. "
"This should target the `nn.ModuleList` of the model, which is often called `'layers'` or `'h'`."
},
)
modules_to_save: Optional[list[str]] = field(
Expand Down
8 changes: 5 additions & 3 deletions src/peft/tuners/vblora/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ class VBLoRAConfig(PeftConfig):
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`.
layers_pattern (`Optional[Union[List[str], str]]`):
The layer pattern name, used only if `layers_to_transform` is different from `None`. This should target the
`nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.
"""

r: int = field(default=4, metadata={"help": "The rank of incremental matrices."})
Expand Down Expand Up @@ -169,7 +170,8 @@ class VBLoRAConfig(PeftConfig):
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. "
"This only works when target_modules is a list of str."
"This only works when target_modules is a list of str. This should target the `nn.ModuleList` of the "
"model, which is often called `'layers'` or `'h'`."
},
)
layers_pattern: Optional[Union[list[str], str]] = field(
Expand Down
22 changes: 12 additions & 10 deletions src/peft/tuners/vera/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
# 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 __future__ import annotations

import warnings
from dataclasses import dataclass, field
from typing import List, Optional, Union
from typing import Optional, Union

from peft.config import PeftConfig
from peft.utils import PeftType
Expand Down Expand Up @@ -61,14 +62,14 @@ class VeraConfig(PeftConfig):
The layer indexes to transform, if this argument is specified, it will apply the Vera transformations on
the layer indexes that are specified in this list. If a single integer is passed, it will apply the Vera
transformations on the layer at this index.
layers_pattern (`str`):
The layer pattern name, used only if `layers_to_transform` is different from `None` and if the layer
pattern is not in the common layers pattern.
layers_pattern (`Optional[Union[List[str], str]]`):
The layer pattern name, used only if `layers_to_transform` is different from `None`. This should target the
`nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.
"""

r: int = field(default=256, metadata={"help": "Vera attention dimension"})

target_modules: Optional[Union[List[str], str]] = field(
target_modules: Optional[Union[list[str], str]] = field(
default=None,
metadata={
"help": (
Expand Down Expand Up @@ -104,7 +105,7 @@ class VeraConfig(PeftConfig):
metadata={"help": "Set this to True if the layer to replace stores weight like (fan_in, fan_out)"},
)
bias: str = field(default="none", metadata={"help": "Bias type for Vera. Can be 'none', 'all' or 'vera_only'"})
modules_to_save: Optional[List[str]] = field(
modules_to_save: Optional[list[str]] = field(
default=None,
metadata={
"help": (
Expand All @@ -123,7 +124,7 @@ class VeraConfig(PeftConfig):
),
},
)
layers_to_transform: Optional[Union[List[int], int]] = field(
layers_to_transform: Optional[Union[list[int], int]] = field(
default=None,
metadata={
"help": (
Expand All @@ -133,12 +134,13 @@ class VeraConfig(PeftConfig):
)
},
)
layers_pattern: Optional[str] = field(
layers_pattern: Optional[Union[list[str], 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."
"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. This should target the `nn.ModuleList` of the "
"model, which is often called `'layers'` or `'h'`."
)
},
)
Expand Down

0 comments on commit c0f1722

Please sign in to comment.