#3835·peft

delete_adapter skips adapter layers whose module name contains the tuner prefix (e.g. lora_foobar)

Author: JoeyTan21Created Sep 28, 2026Updated Sep 29, 2026

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:

python
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:

python
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.