delete_adapter skips adapter layers whose module name contains the tuner prefix (e.g. lora_foobar)
System Info
peft main @ b8674c8 (0.21.1.dev0), torch 2.14.0 (CPU), Python 3.12, macOS.
Who can help?
@BenjaminBossan @githubnemo
Reproduction
The module-level delete_adapter helper in src/peft/tuners/tuners_utils.py (used by BaseTuner.delete_adapter, i.e. by every tuner, and exposed as peft.functional.delete_adapter) skips modules by an unanchored substring test on the module name:
for key, target in model.named_modules():
if prefix in key: # prefix == "lora_"
continue
if isinstance(target, layer_cls):
target.delete_adapter(adapter_name)
A base-model module whose name happens to contain the prefix (e.g. lora_foobar) and that is itself a target of the adapter is therefore never visited. The adapter is removed from peft_config and from the other layers, but not from that one:
import torch
from torch import nn
from peft import LoraConfig, get_peft_model
from peft.tuners.tuners_utils import BaseTunerLayer
class MyModel(nn.Module):
def __init__(self):
super().__init__()
self.lin0 = nn.Linear(10, 10)
self.lora_foobar = nn.Linear(10, 10) # ordinary module, name contains "lora_"
def forward(self, x):
return self.lora_foobar(self.lin0(x))
config = LoraConfig(target_modules=["lin0", "lora_foobar"], init_lora_weights=False)
model = get_peft_model(MyModel(), config)
model.add_adapter("other", LoraConfig(target_modules=["lin0", "lora_foobar"], init_lora_weights=False))
model.set_adapter("other")
model.delete_adapter("other")
print(list(model.peft_config)) # ['default']
for name, module in model.named_modules():
if isinstance(module, BaseTunerLayer):
print(name, sorted(module._get_available_adapters()), module.active_adapters)
# base_model.model.lin0 ['default'] ['default']
# base_model.model.lora_foobar ['default', 'other'] ['other'] <-- deleted adapter still present and still active
print([k for k in model.state_dict() if ".other." in k])
# ['base_model.model.lora_foobar.lora_A.other.weight', 'base_model.model.lora_foobar.lora_B.other.weight']
So after delete_adapter("other") the lora_foobar layer keeps the weights of the deleted adapter (they are also still in state_dict()), and because other was the active adapter, that layer keeps applying it in forward, while lin0 uses default. This is the inconsistent state that BaseTunerLayer.delete_adapter's docstring warns about.
The same substring pitfall was already fixed for the state dict in #3490 (see the lora_foo remark in the PR description and test_prefixes_module_named_like_peft_prefix_not_included) and _mark_only_adapters_as_trainable / _unload_and_optionally_merge identify tuner layers structurally; delete_adapter still uses the name check. There are a few more self.prefix in key filters of the same kind (lora/model.py add_weighted_adapter, ia3/model.py and ln_tuning/model.py _unload_and_optionally_merge, mixed/model.py), which I have not changed.
Expected behavior
delete_adapter removes the adapter from every adapter layer regardless of the module name, so that _get_available_adapters(), active_adapters and state_dict() no longer reference it.
Proposed fix: iterate over model.modules() and rely on isinstance(target, layer_cls) only (the adapter sub-modules such as lora_A are not BaseTunerLayers, so nothing needs to be excluded by name); keep the prefix argument for backwards compatibility since the function is part of peft.functional. With that change the new regression test passes and pytest tests/ -k delete gives the same results as main.
I intend to open a PR for this if approved.
Disclosure: found, reproduced and the draft fix tested locally with the help of an AI coding agent (Claude Code); reviewed before filing.
just pushed a patch for this in #3846.
good catch. dropped the unanchored substring filter entirely and switched it over to model.modules() since the isinstance(target, layer_cls) check is already watertight and enough to skip sub-modules like lora_A.
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.
I'd like to fix this if it's welcome. I reproduced it on main with your snippet: after delete_adapter("other"), lora_foobar still holds ['default', 'other'] with other active, and its lora_A/B.other weights are still in the state dict. The if prefix in key check skips any base module whose name contains lora_.
My plan is to make the module-level delete_adapter identify tuner layers structurally, the same way _mark_only_adapters_as_trainable does. I'll add a regression test in tests/test_custom_models.py based on your example. I'd keep the other self.prefix in key sites you listed out of this PR unless you'd like them included.
Could a maintainer add /peft-triage approved if this looks right? I'll be using AI assistance (Claude Code) to help with the change, and I'll review and test it myself before opening anything.
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.