add_weighted_adapter silently produces a wrong adapter for use_rslora, lora_bias, DoRA and KaSA
System Info
Combining a single adapter with weight 1.0 has to give back that same adapter. It does for plain LoRA. It does not for use_rslora, lora_bias, use_dora or kasa_config, and none of them raise. On the tiny OPT model below the relative difference in the logits is 4.2e-01 for use_rslora, 1.0e+00 for lora_bias, 4.2e-01 for use_dora and 1.3e+00 for kasa_config, against 0.0 for plain LoRA. Only LoraModel is affected, since AdaLoraModel and MixedModel raise on add_weighted_adapter and the IA3 version has neither scaling nor variants.
peft main da2a84da, transformers 5.15.0, torch 2.14.0+cpu, Python 3.12, CPU only.
Who can help?
@BenjaminBossan @githubnemo
Reproduction
import torch
from transformers import AutoModelForCausalLM
from peft import LoraConfig, get_peft_model
from peft.tuners.lora.config import KasaConfig
MODEL_ID = "peft-internal-testing/tiny-random-OPTForCausalLM"
X = torch.arange(8).reshape(1, 8)
cases = [
("plain LoRA", {}),
("use_rslora", {"use_rslora": True}),
("lora_bias", {"lora_bias": True}),
("use_dora", {"use_dora": True}),
("kasa_config", {"kasa_config": KasaConfig(), "r": 4}),
]
for label, kwargs in cases:
torch.manual_seed(42)
base = AutoModelForCausalLM.from_pretrained(MODEL_ID)
config = LoraConfig(target_modules=["q_proj", "v_proj"], init_lora_weights=False, **kwargs)
model = get_peft_model(base, config, adapter_name="source").eval()
model.set_adapter("source")
with torch.no_grad():
out_source = model(X).logits
model.add_weighted_adapter(["source"], [1.0], "combined", combination_type="cat")
model.set_adapter("combined")
with torch.no_grad():
out_combined = model(X).logits
rel = ((out_combined - out_source).norm() / out_source.norm()).item()
print(f"{label:12} relative difference {rel:.3e}")Output:
plain LoRA relative difference 0.000e+00
use_rslora relative difference 4.226e-01
lora_bias relative difference 1.019e+00
use_dora relative difference 4.231e-01
kasa_config relative difference 1.285e+00The same happens with svd and with the linear family, and when more than one adapter is combined.
Cause
There are four of them, all in add_weighted_adapter.
The combined config is built with r=new_rank, lora_alpha=new_rank and inherits use_rslora from adapters[0] (lora/model.py:828). With rsLoRA the scaling is lora_alpha / sqrt(r) (lora/layer.py:279), so the combined adapter ends up with sqrt(new_rank) while the combination code writes a delta that assumes 1. For r of 4, 8, 16 and 32 the scaling comes out as 2.0, 2.8284, 4.0 and 5.6569.
lora_B is an nn.Linear and its bias is part of lora_B(lora_A(dropout(x))) * scaling (lora/layer.py:1102), with merge applying the same factor (lora/layer.py:995 and :1017). The bias is never combined, so the new adapter keeps whatever bias it was initialized with.
lora_magnitude_vector does not appear in add_weighted_adapter at all. The new adapter's vector is set during inject_adapter and never touched again, while lora_A and lora_B are overwritten afterwards, so the vector normalizes against weights that are no longer there.
lora_diag is learned and goes into the delta as (lora_B * lora_diag) @ lora_A, scaled (lora/variants.py:1506), but the combined adapter keeps the torch.randn(r) it got from init (lora/variants.py:1482).
mica, velora and monteclora pass the same check. alora is inconclusive, because its forward returns the base result unless alora_offsets is passed as a keyword argument (lora/variants.py:627), so it was a no-op here and the check says nothing about it either way.
Expected behavior
use_rslora and lora_bias have an exact answer. The combined adapter needs a scaling of 1, and the bias combines as sum(w_i * scaling_i * bias_i), which holds for every combination type. DoRA and KaSA carry learned state that has no defined combination, so add_weighted_adapter should raise for them the way it already does for target_parameters. If you would rather see them supported properly, say so and I will do that instead.
This is not #3737, which is about rank_pattern and the saved config, and I am not touching it. It is also not #3741, which is the bias scaling in the bnb merge path rather than in add_weighted_adapter.
One thing off to the side. test_add_weighted_adapter_cat_with_rank_pattern parametrizes over seven config classes but never uses config_cls in the body, so it runs the same LoRA case seven times. Happy to fold that in separately if you want it.
I have the fix and the tests ready and will open the PR once you confirm.
AI assistance was used. I reviewed every changed line and ran the tests myself.
Source: huggingface/peft