Skip to content

Commit c4ebd98

Browse files
authored
fix cells to modules in mindnlp.peft (#1875)
1 parent a717062 commit c4ebd98

File tree

24 files changed

+189
-189
lines changed

24 files changed

+189
-189
lines changed

mindnlp/peft/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
# ============================================================================
15-
"""Parameter effcient fine tuning cells, like huggingface peft."""
15+
"""Parameter effcient fine tuning modules, like huggingface peft."""
1616
from .mapping import (
1717
MODEL_TYPE_TO_PEFT_MODEL_MAPPING,
1818
PEFT_TYPE_TO_CONFIG_MAPPING,

mindnlp/peft/config.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class PromptLearningConfig(PeftConfig):
166166
Args:
167167
num_virtual_tokens (`int`): The number of virtual tokens to use.
168168
token_dim (`int`): The hidd-en embedding dimension of the base transformer model.
169-
num_transformer_submodules (`int`): The number of transformer subcells in the base transformer model.
169+
num_transformer_submodules (`int`): The number of transformer submodules in the base transformer model.
170170
num_attention_heads (`int`): The number of attention heads in the base transformer model.
171171
num_layers (`int`): The number of layers in the base transformer model.
172172
"""
@@ -175,7 +175,7 @@ class PromptLearningConfig(PeftConfig):
175175
default=None, metadata={"help": "The hidden embedding dimension of the base transformer model"}
176176
)
177177
num_transformer_submodules: Optional[int] = field(
178-
default=None, metadata={"help": "Number of transformer subcells"}
178+
default=None, metadata={"help": "Number of transformer submodules"}
179179
)
180180
num_attention_heads: Optional[int] = field(default=None, metadata={"help": "Number of attention heads"})
181181
num_layers: Optional[int] = field(default=None, metadata={"help": "Number of transformer layers"})

mindnlp/peft/mapping.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def inject_adapter_in_model(
141141

142142
tuner_cls = PEFT_TYPE_TO_TUNER_MAPPING[peft_config.peft_type]
143143

144-
# By instantiating a peft model we are injecting randomly initialized LoRA layers into the model's cells.
144+
# By instantiating a peft model we are injecting randomly initialized LoRA layers into the model's modules.
145145
peft_model = tuner_cls(model, peft_config, adapter_name=adapter_name)
146146

147147
return peft_model.model

mindnlp/peft/peft_model.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def __init__(self, model, peft_config: PeftConfig, adapter_name="default"):
114114
self.base_model = PEFT_TYPE_TO_MODEL_MAPPING[peft_config.peft_type](
115115
self.base_model, self.peft_config, adapter_name
116116
)
117-
self.set_additional_trainable_cells(peft_config, adapter_name)
117+
self.set_additional_trainable_modules(peft_config, adapter_name)
118118
else:
119119
self.add_adapter(adapter_name, peft_config)
120120

@@ -247,7 +247,7 @@ def load_adapter(self, model_id: str, adapter_name: str, is_trainable: bool = Fa
247247
load_result = set_peft_model_state_dict(self, adapters_weights, adapter_name=adapter_name)
248248
# TODO: add parallel logic & offload logic & device map logic(dispatch_model)
249249

250-
# Set model in evaluation mode to deactivate Dropout cells by default
250+
# Set model in evaluation mode to deactivate Dropout modules by default
251251
if not is_trainable:
252252
self.set_train(False)
253253

@@ -425,10 +425,10 @@ def add_adapter(self, adapter_name: str, peft_config: PeftConfig):
425425
del self.peft_config[adapter_name]
426426
raise
427427

428-
self.set_additional_trainable_cells(peft_config, adapter_name)
428+
self.set_additional_trainable_modules(peft_config, adapter_name)
429429

430-
def set_additional_trainable_cells(self, peft_config, adapter_name):
431-
"""set additional trainable cells"""
430+
def set_additional_trainable_modules(self, peft_config, adapter_name):
431+
"""set additional trainable modules"""
432432
if getattr(peft_config, "modules_to_save", None) is not None:
433433
if self.modules_to_save is None:
434434
self.modules_to_save = set(peft_config.modules_to_save)
@@ -473,7 +473,7 @@ def __init__(self, model, peft_config: PeftConfig, adapter_name="default"):
473473
else:
474474
self.modules_to_save.update({"classifier", "score"})
475475

476-
for name, _ in self.base_model.cells_and_names():
476+
for name, _ in self.base_model.modules_and_names():
477477
if any(module_name in name for module_name in self.modules_to_save):
478478
self.cls_layer_name = name
479479
break
@@ -955,7 +955,7 @@ def __init__(self, model, peft_config: PeftConfig = None, adapter_name="default"
955955
else:
956956
self.modules_to_save.update({"classifier", "score"})
957957

958-
for name, _ in self.base_model.cells_and_names():
958+
for name, _ in self.base_model.modules_and_names():
959959
if any(module_name in name for module_name in self.modules_to_save):
960960
self.cls_layer_name = name
961961
break
@@ -1079,15 +1079,15 @@ def _prefix_tuning_forward(
10791079
if "past_key_values" in fwd_params:
10801080
return self.base_model(labels=labels, **kwargs)
10811081
else:
1082-
transformer_backbone_name = self.base_model.get_subcell(self.transformer_backbone_name)
1082+
transformer_backbone_name = self.base_model.get_submodule(self.transformer_backbone_name)
10831083
fwd_params = list(inspect.signature(transformer_backbone_name.forward).parameters.keys())
10841084
if "past_key_values" not in fwd_params:
10851085
raise ValueError("Model does not support past key values which are required for prefix tuning.")
10861086
outputs = transformer_backbone_name(**kwargs)
10871087
sequence_output = outputs[0]
1088-
if "dropout" in [name for name, _ in list(self.base_model.cells_and_names())]:
1088+
if "dropout" in [name for name, _ in list(self.base_model.modules_and_names())]:
10891089
sequence_output = self.base_model.dropout(sequence_output)
1090-
logits = self.base_model.get_subcell(self.cls_layer_name)(sequence_output)
1090+
logits = self.base_model.get_submodule(self.cls_layer_name)(sequence_output)
10911091

10921092
loss = None
10931093
if labels is not None:

mindnlp/peft/tuners/adalora/model.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from mindnlp.peft.utils import (
2424
TRANSFORMERS_MODELS_TO_ADALORA_TARGET_MODULES_MAPPING,
2525
_freeze_adapter,
26-
_get_subcells,
26+
_get_submodules,
2727
)
2828

2929
from ..tuners_utils import BaseTunerLayer
@@ -145,7 +145,7 @@ def _mark_only_adapters_as_trainable(self, model: nn.Module) -> None:
145145
if "bias" in n:
146146
p.requires_grad = True
147147
elif bias == "lora_only":
148-
for m in model.cells():
148+
for m in model.modules():
149149
if isinstance(m, AdaLoraLayer) and hasattr(m, "bias") and m.bias is not None:
150150
m.bias.requires_grad = True
151151
else:
@@ -331,7 +331,7 @@ def _prepare_adapter_config(peft_config, model_config):
331331
This method '_prepare_adapter_config' in the class 'AdaLoraModel' prepares the adapter configuration based on the provided 'peft_config' and 'model_config' parameters.
332332
333333
Args:
334-
- peft_config (dict): A dictionary containing the configuration details for the adapter. It should include information about the target cells. If 'target_modules' is not specified, it is inferred based
334+
- peft_config (dict): A dictionary containing the configuration details for the adapter. It should include information about the target modules. If 'target_modules' is not specified, it is inferred based
335335
on the 'model_type' from the 'model_config' parameter.
336336
- model_config (dict): A dictionary containing the configuration details specific to the model. It is used to determine the 'model_type' which is then used to infer the 'target_modules' if not explicitly
337337
provided in 'peft_config'.
@@ -385,8 +385,8 @@ def forward(self, *args, **kwargs):
385385
outputs.loss += orth_reg_weight * regu_loss
386386
return outputs
387387

388-
def resize_cells_by_rank_pattern(self, rank_pattern, adapter_name):
389-
"resize the cells by rank pattern"
388+
def resize_modules_by_rank_pattern(self, rank_pattern, adapter_name):
389+
"resize the modules by rank pattern"
390390
lora_config = self.peft_config[adapter_name]
391391
for name, rank_idx in rank_pattern.items():
392392
if isinstance(rank_idx, list):
@@ -398,7 +398,7 @@ def resize_cells_by_rank_pattern(self, rank_pattern, adapter_name):
398398
else:
399399
raise ValueError("Unexpected type of rank_idx")
400400
key = ".".join(name.split(".")[0:-2]) if adapter_name in name else ".".join(name.split(".")[0:-1])
401-
_, target, _ = _get_subcells(self.model, key)
401+
_, target, _ = _get_submodules(self.model, key)
402402
lora_E_weights = target.lora_E[adapter_name][rank_idx]
403403
lora_A_weights = target.lora_A[adapter_name][rank_idx]
404404
lora_B_weights = target.lora_B[adapter_name][:, rank_idx]

mindnlp/peft/tuners/adaption_prompt/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
class AdaptionPromptConfig(PeftConfig):
2525
"""Stores the configuration of an [`AdaptionPromptModel`]."""
2626
target_modules: str = field(
27-
default=None, metadata={"help": "Name of the attention subcells to insert adaption prompts into."}
27+
default=None, metadata={"help": "Name of the attention submodules to insert adaption prompts into."}
2828
)
2929
adapter_len: int = field(default=None, metadata={"help": "Number of adapter tokens to insert"})
3030
adapter_layers: int = field(default=None, metadata={"help": "Number of adapter layers (from the top)"})

mindnlp/peft/tuners/adaption_prompt/model.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from mindnlp.core import nn
1818

19-
from mindnlp.peft.utils import _freeze_adapter, _get_subcells
19+
from mindnlp.peft.utils import _freeze_adapter, _get_submodules
2020

2121
from .config import AdaptionPromptConfig, prepare_config
2222
from .layer import AdaptedAttention
@@ -27,17 +27,17 @@ class AdaptionPromptModel(nn.Module):
2727
"""
2828
Implements adaption prompts as described in https://arxiv.org/pdf/2303.16199.pdf.
2929
30-
The top L attention cells are replaced with AdaptedAttention cells that wrap the original ones, but insert
30+
The top L attention modules are replaced with AdaptedAttention modules that wrap the original ones, but insert
3131
trainable prompts with gates (for zero init).
3232
3333
Notes on the multi-adapter pattern:
34-
- We store the states of different adapters by keeping a dictionary of AdaptedAttention cells indexed by adapter
34+
- We store the states of different adapters by keeping a dictionary of AdaptedAttention modules indexed by adapter
3535
name.
36-
- Every time we switch adapters, we remove the cells of the currently active adapter from the model, store them
37-
in the dictionary, and replace them with the cells of the new adapter.
36+
- Every time we switch adapters, we remove the modules of the currently active adapter from the model, store them
37+
in the dictionary, and replace them with the modules of the new adapter.
3838
- To avoid duplicated and potentially inconsistent state, the currently active adapter is always removed from the
3939
dictionary.
40-
- Disabling the adapter would also result in the cells being removed from the model.
40+
- Disabling the adapter would also result in the modules being removed from the model.
4141
"""
4242
def __init__(self, model, configs: Dict, adapter_name: str):
4343
r"""
@@ -80,11 +80,11 @@ def add_adapter(self, adapter_name: str, config: AdaptionPromptConfig) -> None:
8080

8181
parents = []
8282
# 获取模型的所有子模块及其名称
83-
for name, subcell in self.model.name_cells().items():
83+
for name, submodule in self.model.named_modules().items():
8484
if name.endswith(config.target_modules):
85-
# 对每个符合条件的子模块调用 _get_subcells 函数
86-
parent, target, target_name = _get_subcells(self.model, name)
87-
if target == subcell:
85+
# 对每个符合条件的子模块调用 _get_submodules 函数
86+
parent, target, target_name = _get_submodules(self.model, name)
87+
if target == submodule:
8888
parents.append(parent)
8989

9090
if len(parents) < config.adapter_layers:
@@ -118,17 +118,17 @@ def set_adapter(self, adapter_name: str) -> None:
118118
self._active_adapter = adapter_name
119119

120120
def enable_adapter_layers(self):
121-
"""Enable adapter layers by swapping in cached AdaptedAttention cells."""
121+
"""Enable adapter layers by swapping in cached AdaptedAttention modules."""
122122
self._enabled = True
123123
self._set_adapted_attentions(self._active_adapter)
124124

125125
def disable_adapter_layers(self):
126-
"""Disable adapter layers by swapping out AdaptedAttention cells."""
126+
"""Disable adapter layers by swapping out AdaptedAttention modules."""
127127
self._enabled = False
128128
self._remove_adapted_attentions(self._active_adapter)
129129

130130
def _create_adapted_attentions(self, config: AdaptionPromptConfig, parents: List[nn.Module]) -> None:
131-
"""Wrap LlamaAttention cells with newly created AdaptedAttention cells."""
131+
"""Wrap LlamaAttention modules with newly created AdaptedAttention modules."""
132132
for par in parents:
133133
attn = AdaptedAttention(
134134
model_type=self.model.config.model_type,
@@ -138,15 +138,15 @@ def _create_adapted_attentions(self, config: AdaptionPromptConfig, parents: List
138138
setattr(par, config.target_modules, attn)
139139

140140
def _set_adapted_attentions(self, adapter_name: str) -> None:
141-
"""Replace LlamaAttention cells with cached AdaptedAttention cells."""
141+
"""Replace LlamaAttention modules with cached AdaptedAttention modules."""
142142
cached = self._cached_adapters[adapter_name]
143143
del self._cached_adapters[adapter_name]
144144
config = self.peft_config[adapter_name]
145145
for i, par in enumerate(self._parents[adapter_name]):
146146
setattr(par, config.target_modules, cached[i])
147147

148148
def _remove_adapted_attentions(self, adapter_name: str) -> None:
149-
"""Remove AdaptedAttention cells from the model and store them in the cache."""
149+
"""Remove AdaptedAttention modules from the model and store them in the cache."""
150150
config = self.peft_config[adapter_name]
151151
adapted_attentions = []
152152
for par in self._parents[adapter_name]:

0 commit comments

Comments
 (0)