#3804·peft

Adapter loading never warns about unexpected keys, and `strict=True` is silently ignored

Author: hivazeCreated Sep 23, 2026Updated Sep 29, 2026

System Info

peft 0.21.0 and main (116a979), transformers 5.16.1, torch 2.11.0, Python 3.12, Linux (CPU)

Who can help?

@BenjaminBossan @githubnemo

Reproduction

Following up on whitecircle/halo#35. When an adapter checkpoint doesn't match the model, PeftModel.from_pretrained warns about missing adapter keys but drops unexpected ones, load_adapter warns about neither, and both accept strict=True without effect.

python
import os, tempfile, warnings

import torch
from safetensors.torch import load_file, save_file
from transformers import LlamaConfig, LlamaForCausalLM

import peft
from peft import LoraConfig, PeftModel, get_peft_model


def base(layers=1):
    torch.manual_seed(0)
    cfg = LlamaConfig(vocab_size=32, hidden_size=16, intermediate_size=32, num_hidden_layers=layers,
                      num_attention_heads=2, num_key_value_heads=2)
    return LlamaForCausalLM(cfg).eval()


def save_adapter(layers=1, rename=None):
    model = get_peft_model(base(layers), LoraConfig(r=4, target_modules=["q_proj"]))
    for name, param in model.named_parameters():
        if "lora_B" in name:
            torch.nn.init.normal_(param)  # stands in for training
    path = tempfile.mkdtemp()
    model.save_pretrained(path)
    if rename:
        f = os.path.join(path, "adapter_model.safetensors")
        save_file({k.replace(*rename): v for k, v in load_file(f).items()}, f)
    return path


x = torch.arange(8)[None]
base_logits = base()(x).logits
cases = {
    "keys under another module path": save_adapter(rename=(".model.layers.", ".model.language_model.layers.")),
    "2-layer adapter on a 1-layer base": save_adapter(layers=2),
}
print("peft", peft.__version__)
for case, path in cases.items():
    for api in ("from_pretrained", "load_adapter"):
        with warnings.catch_warnings(record=True) as caught:
            warnings.simplefilter("always")
            if api == "from_pretrained":
                model, result = PeftModel.from_pretrained(base(), path, strict=True), None
            else:
                model = get_peft_model(base(), LoraConfig(r=4, target_modules=["q_proj"]))
                result = model.load_adapter(path, adapter_name="loaded", strict=True)
                model.set_adapter("loaded")
        key_warnings = [str(w.message).split(":")[0] for w in caught if "keys" in str(w.message)]
        returned = f"missing={len(result.missing_keys)} unexpected={len(result.unexpected_keys)}" if result else "model only"
        print(f"[{case}] {api}(strict=True): no error | returned: {returned} | warnings: {key_warnings} | "
              f"output == base model: {torch.equal(model(x).logits, base_logits)}")

python repro.py prints the same on 0.21.0 and main:

peft 0.21.0
[keys under another module path] from_pretrained(strict=True): no error | returned: model only | warnings: ['Found missing adapter keys while loading the checkpoint'] | output == base model: True
[keys under another module path] load_adapter(strict=True): no error | returned: missing=2 unexpected=2 | warnings: [] | output == base model: True
[2-layer adapter on a 1-layer base] from_pretrained(strict=True): no error | returned: model only | warnings: [] | output == base model: False
[2-layer adapter on a 1-layer base] load_adapter(strict=True): no error | returned: missing=0 unexpected=2 | warnings: [] | output == base model: False

Case 1 is an adapter whose keys sit under another module path, e.g. one trained on a VLM whose decoder lives under model.language_model, loaded onto the text-only model. load_adapter leaves the new adapter at its init, so lora_B is zero and the output equals the base model, with no warning. Only the returned keys show it. from_pretrained does warn here, because the adapter's own keys are missing.

Case 2 is an adapter trained on a deeper model of the same width (a depth-pruned variant, for instance). Layer 0 loads, layer 1 is dropped, and neither API warns. from_pretrained doesn't return the load result, so the caller can't see the unexpected keys at all.

strict=True is not a parameter of either method. It lands in **kwargs, where load_adapter splits off the Hub download arguments (peft_model.py:1484, line numbers at 116a979) and reads only a few known keys from the rest (e.g. peft_model.py:1518), so it is dropped like any misspelled keyword.

Per #2118, load_adapter doesn't warn on purpose because it returns the result, and warning on unexpected keys came up in that PR's review but was left for later to avoid false positives. A strict option would still help: with from_pretrained there is no way to fail on unexpected keys, and a bare strict=True currently looks like it works. For comparison, transformers' own PreTrainedModel.load_adapter (5.16.1) prints a load report that lists the two layer-1 keys of case 2 as UNEXPECTED.

Expected behavior

from_pretrained warns about unexpected keys the same way it warns about missing ones, and strict=True raises on missing or unexpected adapter keys instead of being ignored.

One way to do it: add strict: bool = False to load_adapter, raise right after the missing-key filtering (peft_model.py:1552-1553) when either list is non-empty, and forward it from from_pretrained. In a save/load round trip over about 20 tuner types, correct checkpoints came back with empty key lists except in two cases that would need filtering first. Prompt learning methods always report prompt_embeddings as unexpected, because it is loaded through the prompt encoder after load_state_dict (save_and_load.py:651-654). VB-LoRA's shared vblora_vector_bank shows up in load_adapter's missing keys, while from_pretrained filters it out with is_shared_parameter (peft_model.py:629).