#3818·peft

Overlapping rank_pattern / alpha_pattern keys resolve by dict order, and save_pretrained(sort_keys=True) changes that order on reload

Author: jayzuccarelliCreated Sep 25, 2026Updated Sep 29, 2026

System Info

  • peft 0.21.0 (PyPI) and main 0.21.1.dev0 @ b8674c861, same result on both
  • torch 2.14.0+cpu, transformers 5.17.0, accelerate 1.15.0
  • Python 3.12.13, Linux (Ubuntu 22.04)

Who can help?

@BenjaminBossan

Reproduction

get_pattern_key (src/peft/utils/other.py:1532-1540) returns the first key in rank_pattern / alpha_pattern that matches a module, in dict order. PeftConfigMixin.save_pretrained writes adapter_config.json with json.dumps(..., sort_keys=True) (src/peft/config.py:163), which also sorts the nested pattern dicts. So when two keys overlap (a general q and a specific layers.0.q), the key that wins during training can be different from the one that wins after reload.

  • alpha_pattern: loads with no error or warning, but with a different scaling, so outputs change.
  • rank_pattern: the reloaded model builds the layer at a different rank and from_pretrained fails with a size mismatch.

The unmatched-key warning from #3588 doesn't catch it since both keys match something.

python
import torch, torch.nn as nn, tempfile
from peft import LoraConfig, get_peft_model, PeftModel

class Block(nn.Module):
    def __init__(s): super().__init__(); s.q = nn.Linear(8, 8)
    def forward(s, x): return s.q(x)

class M(nn.Module):
    def __init__(s): super().__init__(); s.layers = nn.ModuleList([Block() for _ in range(2)])
    def forward(s, x):
        for b in s.layers: x = b(x)
        return x

torch.manual_seed(0); base = M(); sd = base.state_dict()
m = get_peft_model(base, LoraConfig(target_modules=["q"], init_lora_weights=False,
                                    alpha_pattern={"q": 8, "layers.0.q": 64}))
x = torch.randn(2, 8); m.eval(); y1 = m(x)
with tempfile.TemporaryDirectory() as d:
    m.save_pretrained(d); b2 = M(); b2.load_state_dict(sd)
    m2 = PeftModel.from_pretrained(b2, d); m2.eval(); y2 = m2(x)
print((y1 - y2).abs().max())  # tensor(4.8710), no warning

# rank variant: rank_pattern={"q": 4, "layers.0.q": 16}
# saved rank_pattern: {"layers.0.q": 16, "q": 4}
# PeftModel.from_pretrained -> RuntimeError: Error(s) in loading state_dict for PeftModel:
#   size mismatch for base_model.model.layers.0.q.lora_A.default.weight: copying a param with shape
#   torch.Size([4, 8]) from checkpoint, the shape in current model is torch.Size([16, 8]).

Observed scaling: before save {layers.0.q: 1.0, layers.1.q: 1.0}, after reload {layers.0.q: 8.0, layers.1.q: 1.0}. Saved alpha_pattern is {"layers.0.q": 64, "q": 8}.

Same lookup is used by LoHa (tuners/loha/model.py:106) and DeLoRA (tuners/delora/model.py:75).

Expected behavior

A saved adapter reloads with the same ranks and alphas it was trained with. Separately, it's arguably surprising that {"q": 8, "layers.0.q": 64} gives layer 0 an alpha of 8 during training: the specific key reads like an override, and the reload result (64/rank = 8.0 scaling) is probably what the user meant.

Two options, happy to go with whichever you prefer:

  1. Minimal round-trip fix: in save_pretrained, sort only the top-level keys (dict(sorted(output_dict.items())), then json.dumps(..., indent=2)) so pattern-dict order survives save/load. Training behavior unchanged.
  2. Make get_pattern_key pick the most specific match (or warn when several keys match one module), which removes the order dependence entirely.

Either way, a save/reload test with overlapping rank_pattern and alpha_pattern asserting equal ranks, scaling, and outputs.

Happy to open a PR with a regression test if this approach works for you.