Skip to content

Commit e8259ff

Browse files
DOC Improve docs for layers_pattern argument (#2157)
Addresses part of #2155. Also fix type annotations where appropriate.
1 parent 57a452a commit e8259ff

File tree

9 files changed

+58
-40
lines changed

9 files changed

+58
-40
lines changed

src/peft/tuners/boft/config.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ class BOFTConfig(PeftConfig):
5353
The layer indexes to transform, if this argument is specified, it will apply the BOFT transformations on
5454
the layer indexes that are specified in this list. If a single integer is passed, it will apply the BOFT
5555
transformations on the layer at this index.
56-
layers_pattern (`str`):
56+
layers_pattern (`Optional[Union[List[str], str]]`):
5757
The layer pattern name, used only if `layers_to_transform` is different from `None` and if the layer
58-
pattern is not in the common layers pattern.
58+
pattern is not in the common layers pattern. This should target the `nn.ModuleList` of the model, which is
59+
often called `'layers'` or `'h'`.
5960
"""
6061

6162
boft_block_size: int = field(
@@ -129,10 +130,11 @@ class BOFTConfig(PeftConfig):
129130
"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."
130131
},
131132
)
132-
layers_pattern: Optional[str] = field(
133+
layers_pattern: Optional[Union[list[str], str]] = field(
133134
default=None,
134135
metadata={
135-
"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."
136+
"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. "
137+
"This should target the `nn.ModuleList` of the model, which is often called `'layers'` or `'h'`."
136138
},
137139
)
138140

src/peft/tuners/fourierft/config.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ class FourierFTConfig(PeftConfig):
6868
The layer indexes to transform, is this argument is specified, PEFT will transform only the layers indexes
6969
that are specified inside this list. If a single integer is passed, PEFT will transform only the layer at
7070
this index.
71-
layers_pattern (`str`):
71+
layers_pattern (`Optional[Union[List[str], str]]`):
7272
The layer pattern name, used only if `layers_to_transform` is different to None and if the layer pattern is
73-
not in the common layers pattern.
73+
not in the common layers pattern. This should target the `nn.ModuleList` of the model, which is often
74+
called `'layers'` or `'h'`.
7475
n_frequency_pattern (`dict`):
7576
The mapping from layer names or regexp expression to n_frequency which are different from the default
7677
specified. For example, `{model.decoder.layers.0.encoder_attn.k_proj: 1000`}.
@@ -154,12 +155,13 @@ class FourierFTConfig(PeftConfig):
154155
)
155156
},
156157
)
157-
layers_pattern: Optional[str] = field(
158+
layers_pattern: Optional[Union[list[str], str]] = field(
158159
default=None,
159160
metadata={
160161
"help": (
161162
"The layer pattern name, used only if `layers_to_transform` is different to None and if the layer"
162-
" pattern is not in the common layers pattern."
163+
" pattern is not in the common layers pattern. This should target the `nn.ModuleList` of the "
164+
"model, which is often called `'layers'` or `'h'`."
163165
)
164166
},
165167
)

src/peft/tuners/hra/config.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ class HRAConfig(PeftConfig):
5050
The layer indices to transform. If a list of ints is passed, it will apply the adapter to the layer indices
5151
that are specified in this list. If a single integer is passed, it will apply the transformations on the
5252
layer at this index.
53-
layers_pattern (`str`):
54-
The layer pattern name, used only if `layers_to_transform` is different from `None`.
53+
layers_pattern (`Optional[Union[List[str], str]]`):
54+
The layer pattern name, used only if `layers_to_transform` is different from `None`. This should target the
55+
`nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.
5556
rank_pattern (`dict`):
5657
The mapping from layer names or regexp expression to ranks which are different from the default rank
5758
specified by `r`.
@@ -96,10 +97,11 @@ class HRAConfig(PeftConfig):
9697
"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."
9798
},
9899
)
99-
layers_pattern: Optional[str] = field(
100+
layers_pattern: Optional[Union[list[str], str]] = field(
100101
default=None,
101102
metadata={
102-
"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."
103+
"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. "
104+
"This should target the `nn.ModuleList` of the model, which is often called `'layers'` or `'h'`."
103105
},
104106
)
105107
bias: str = field(default="none", metadata={"help": "Bias type for HRA. Can be 'none', 'all' or 'hra_only'"})

src/peft/tuners/loha/config.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,9 @@ class LoHaConfig(LycorisConfig):
5555
The layer indices to transform. If a list of ints is passed, it will apply the adapter to the layer indices
5656
that are specified in this list. If a single integer is passed, it will apply the transformations on the
5757
layer at this index.
58-
layers_pattern (`str`):
59-
The layer pattern name, used only if `layers_to_transform` is different from `None`.
58+
layers_pattern (`Optional[Union[List[str], str]]`):
59+
The layer pattern name, used only if `layers_to_transform` is different from `None`. This should target the
60+
`nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.
6061
rank_pattern (`dict`):
6162
The mapping from layer names or regexp expression to ranks which are different from the default rank
6263
specified by `r`.
@@ -108,10 +109,11 @@ class LoHaConfig(LycorisConfig):
108109
"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."
109110
},
110111
)
111-
layers_pattern: Optional[str] = field(
112+
layers_pattern: Optional[Union[list[str], str]] = field(
112113
default=None,
113114
metadata={
114-
"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."
115+
"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. "
116+
"This should target the `nn.ModuleList` of the model, which is often called `'layers'` or `'h'`."
115117
},
116118
)
117119
modules_to_save: Optional[list[str]] = field(

src/peft/tuners/lokr/config.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ class LoKrConfig(LycorisConfig):
5959
The layer indices to transform. If a list of ints is passed, it will apply the adapter to the layer indices
6060
that are specified in this list. If a single integer is passed, it will apply the transformations on the
6161
layer at this index.
62-
layers_pattern (`str`):
63-
The layer pattern name, used only if `layers_to_transform` is different from `None`.
62+
layers_pattern (`Optional[Union[List[str], str]]`):
63+
The layer pattern name, used only if `layers_to_transform` is different from `None`. This should target the
64+
`nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.
6465
rank_pattern (`dict`):
6566
The mapping from layer names or regexp expression to ranks which are different from the default rank
6667
specified by `r`.
@@ -117,10 +118,11 @@ class LoKrConfig(LycorisConfig):
117118
"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."
118119
},
119120
)
120-
layers_pattern: Optional[str] = field(
121+
layers_pattern: Optional[Union[list[str], str]] = field(
121122
default=None,
122123
metadata={
123-
"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."
124+
"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. "
125+
"This should target the `nn.ModuleList` of the model, which is often called `'layers'` or `'h'`."
124126
},
125127
)
126128
modules_to_save: Optional[list[str]] = field(

src/peft/tuners/lora/config.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@ class LoraConfig(PeftConfig):
123123
The layer indices to transform. If a list of ints is passed, it will apply the adapter to the layer indices
124124
that are specified in this list. If a single integer is passed, it will apply the transformations on the
125125
layer at this index.
126-
layers_pattern (`str`):
127-
The layer pattern name, used only if `layers_to_transform` is different from `None`.
126+
layers_pattern (`Optional[Union[List[str], str]]`):
127+
The layer pattern name, used only if `layers_to_transform` is different from `None`. This should target the
128+
`nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.
128129
rank_pattern (`dict`):
129130
The mapping from layer names or regexp expression to ranks which are different from the default rank
130131
specified by `r`.
@@ -229,7 +230,8 @@ class LoraConfig(PeftConfig):
229230
default=None,
230231
metadata={
231232
"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."
232-
"This only works when target_modules is a list of str."
233+
"This only works when target_modules is a list of str. This should target the `nn.ModuleList` of the "
234+
"model, which is often called `'layers'` or `'h'`."
233235
},
234236
)
235237
rank_pattern: Optional[dict] = field(

src/peft/tuners/oft/config.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ class OFTConfig(PeftConfig):
5454
The layer indices to transform. If a list of ints is passed, it will apply the adapter to the layer indices
5555
that are specified in this list. If a single integer is passed, it will apply the transformations on the
5656
layer at this index.
57-
layers_pattern (`str`):
58-
The layer pattern name, used only if `layers_to_transform` is different from `None`.
57+
layers_pattern (`Optional[Union[List[str], str]]`):
58+
The layer pattern name, used only if `layers_to_transform` is different from `None`. This should target the
59+
`nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.
5960
rank_pattern (`dict`):
6061
The mapping from layer names or regexp expression to ranks which are different from the default rank
6162
specified by `r`.
@@ -117,10 +118,11 @@ class OFTConfig(PeftConfig):
117118
"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."
118119
},
119120
)
120-
layers_pattern: Optional[str] = field(
121+
layers_pattern: Optional[Union[list[str], str]] = field(
121122
default=None,
122123
metadata={
123-
"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."
124+
"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. "
125+
"This should target the `nn.ModuleList` of the model, which is often called `'layers'` or `'h'`."
124126
},
125127
)
126128
modules_to_save: Optional[list[str]] = field(

src/peft/tuners/vblora/config.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ class VBLoRAConfig(PeftConfig):
7979
The layer indices to transform. If a list of ints is passed, it will apply the adapter to the layer indices
8080
that are specified in this list. If a single integer is passed, it will apply the transformations on the
8181
layer at this index.
82-
layers_pattern (`str`):
83-
The layer pattern name, used only if `layers_to_transform` is different from `None`.
82+
layers_pattern (`Optional[Union[List[str], str]]`):
83+
The layer pattern name, used only if `layers_to_transform` is different from `None`. This should target the
84+
`nn.ModuleList` of the model, which is often called `'layers'` or `'h'`.
8485
"""
8586

8687
r: int = field(default=4, metadata={"help": "The rank of incremental matrices."})
@@ -169,7 +170,8 @@ class VBLoRAConfig(PeftConfig):
169170
default=None,
170171
metadata={
171172
"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. "
172-
"This only works when target_modules is a list of str."
173+
"This only works when target_modules is a list of str. This should target the `nn.ModuleList` of the "
174+
"model, which is often called `'layers'` or `'h'`."
173175
},
174176
)
175177
layers_pattern: Optional[Union[list[str], str]] = field(

src/peft/tuners/vera/config.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from __future__ import annotations
1415

1516
import warnings
1617
from dataclasses import dataclass, field
17-
from typing import List, Optional, Union
18+
from typing import Optional, Union
1819

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

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

71-
target_modules: Optional[Union[List[str], str]] = field(
72+
target_modules: Optional[Union[list[str], str]] = field(
7273
default=None,
7374
metadata={
7475
"help": (
@@ -104,7 +105,7 @@ class VeraConfig(PeftConfig):
104105
metadata={"help": "Set this to True if the layer to replace stores weight like (fan_in, fan_out)"},
105106
)
106107
bias: str = field(default="none", metadata={"help": "Bias type for Vera. Can be 'none', 'all' or 'vera_only'"})
107-
modules_to_save: Optional[List[str]] = field(
108+
modules_to_save: Optional[list[str]] = field(
108109
default=None,
109110
metadata={
110111
"help": (
@@ -123,7 +124,7 @@ class VeraConfig(PeftConfig):
123124
),
124125
},
125126
)
126-
layers_to_transform: Optional[Union[List[int], int]] = field(
127+
layers_to_transform: Optional[Union[list[int], int]] = field(
127128
default=None,
128129
metadata={
129130
"help": (
@@ -133,12 +134,13 @@ class VeraConfig(PeftConfig):
133134
)
134135
},
135136
)
136-
layers_pattern: Optional[str] = field(
137+
layers_pattern: Optional[Union[list[str], str]] = field(
137138
default=None,
138139
metadata={
139140
"help": (
140-
"The layer pattern name, used only if `layers_to_transform` is different to None and if the layer"
141-
" pattern is not in the common layers pattern."
141+
"The layer pattern name, used only if `layers_to_transform` is different to None and if the layer "
142+
"pattern is not in the common layers pattern. This should target the `nn.ModuleList` of the "
143+
"model, which is often called `'layers'` or `'h'`."
142144
)
143145
},
144146
)

0 commit comments

Comments
 (0)