Hi everyone,
I’m having trouble loading my LoRA adapters for inference after fine-tuning Llama 3.1 8B. When I try to load the adapter files in a new session, I get a warning about missing adapter keys:
/usr/local/lib/python3.11/dist-packages/peft/peft_model.py:599: UserWarning: Found missing adapter keys while loading the checkpoint: ['base_model.model.model.layers.0.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.layers.0.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.layers.0.self_attn.k_proj.lora_A.default.weight',...
The warning continues with many similar missing keys. The model behaves as if it wasn’t fine-tuned at all.
My setup:
transformers 4.43.1
peft 0.14.0
bitsandbytes 0.45.4
torch 2.6.0+cu124
accelerate 0.31.0
After training, I saved the model and got these files:
adapter_model.safetensors
adapter_config.json
training_args.bin
Here’s how I loaded the model and adapters:
# Load base model
base_model_id = "meta-llama/Llama-3.1-8B"
lora_weights = "path to weights"
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16
)
base_model = AutoModelForCausalLM.from_pretrained(
pretrained_model_name_or_path=base_model_id,
quantization_config=bnb_config,
trust_remote_code=True,
token=True
)
# Set up tokenizer
eval_tokenizer = AutoTokenizer.from_pretrained(
base_model_id,
add_bos_token=True,
trust_remote_code=True,
use_fast=True
)
eval_tokenizer.pad_token = eval_tokenizer.eos_token
# Load LoRA adapters
ft_model = PeftModel.from_pretrained(base_model, lora_weights, output_loading_info=True)
Any ideas on what might be causing this issue or how to fix it?
Thanks in advance!
2 Likes
I think this is probably a warning related to this specification.
opened 02:05PM - 03 Dec 24 UTC
closed 10:52AM - 06 Jan 25 UTC
### System Info
- peft=0.13.3.dev0
- transformers=4.46.3
- Python=3.12.6
…
### Who can help?
@BenjaminBossan
@stevhliu
### Information
- [ ] The official example scripts
- [X] My own modified scripts
### Tasks
- [ ] An officially supported task in the `examples` folder
- [X] My own task or dataset (give details below)
### Reproduction
```python
import peft
import torch
from pathlib import Path
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import LoraConfig, TaskType
from tempfile import TemporaryDirectory
def get_adapter_state_dict(model, adapter_name):
adapter_state_dict = model.state_dict()
adapter_weights = {key: value for key, value in adapter_state_dict.items() if adapter_name in key}
return adapter_weights
def test_adapter_name():
MODEL_ID = "openai-community/gpt2"
ADAPTER_NAME = "lora"
# Load the base model
base_model = AutoModelForCausalLM.from_pretrained(MODEL_ID)
# Define LoraConfig without base_model_name_or_path
peft_config = LoraConfig(
task_type=TaskType.CAUSAL_LM,
inference_mode=False,
r=8
)
# Get the PEFT model with the adapter name
peft_model = peft.get_peft_model(base_model, peft_config, adapter_name=ADAPTER_NAME)
# Saving and loading the model
with TemporaryDirectory() as temp_dir:
temp_dir = Path(temp_dir)
adapter_weights = get_adapter_state_dict(peft_model, ADAPTER_NAME)
peft_model.save_pretrained(temp_dir)
loaded_peft_model = peft.PeftModel.from_pretrained(model=base_model,
model_id= temp_dir / ADAPTER_NAME,
adapter_name=ADAPTER_NAME)
loaded_adapter_weights = get_adapter_state_dict(loaded_peft_model, ADAPTER_NAME)
# Assertion fails due to adapter weights been newly intialized
for key in adapter_weights:
assert torch.isclose(adapter_weights[key], loaded_adapter_weights[key]).all()
```
### Expected behavior
A clear error or warning message indicating that the adapter_name and tuner_prefix should not be the same.
I want to load state_dict export bypeft.get_peft_model_state_dict into a new model with same lora config, but I find that the keys of state_dict export by get_peft_model_state_dict doesn’t contain lora name. For example:
model_name_or_path = "bert-base-cased"
bert: BertModel = AutoModel.from_pretrained(model_name_or_path)
lora_bert = get_peft_model(bert, lora_config)
state_dict = get_peft_model_state_dict(lora_bert, save_embedding_layers=False)
for key in state_dict.keys():
print(key)
"""
f…
opened 12:47PM - 13 Apr 23 UTC
closed 01:16AM - 19 Apr 23 UTC
**I trained Alpaca-LoRA model with params:**
base_model: /usr/local/dbbd/model/… llama-7b-hf
data_path: alpaca_data.json
output_dir: ./lora-alpaca
batch_size: 128
micro_batch_size: 4
num_epochs: 2
learning_rate: 0.0001
cutoff_len: 512
val_set_size: 2000
lora_r: 8
lora_alpha: 16
lora_dropout: 0.05
lora_target_modules: ['q_proj', 'v_proj']
train_on_inputs: True
group_by_length: True
wandb_project:
wandb_run_name:
wandb_watch:
wandb_log_model:
resume_from_checkpoint: False
prompt template: alpaca
**But I get a wrong adapter_model.bin which is only 443 bytes, and some output like:**
**There were missing keys in the checkpoint model loaded**: ['base_model.model.model.embed_tokens.weight', 'base_model.model.model.layers.0.self_attn.q_proj.weight', 'base_model.model.model.layers.0.self_attn.q_proj.lora_A.default.weight', '.model.layers.0.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.layers.0.self_attn.k_proj.weight', 'base_model.model.model.layers.0.self_attn.v_proj.weight', 'base_model.model.model.layers.0.self_attn.v_proj.lora_A.defaue_model.model.model.layers.0.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.layers.0.self_attn.o_proj.weight', 'base_model.model.model.layers.0.self_attn.rotary_emb.inv_freq', 'base_model.model.model.layers.0.mlp.gate_pse_model.model.model.layers.0.mlp.down_proj.weight', 'base_model.model.model.layers.0.mlp.up_proj.weight', 'base_model.model.model.layers.0.input_layernorm.weight', 'base_model.model.model.layers.0.post_attention_layernorm.weight', 'baseel.layers.1.self_attn.q_proj.weight', 'base_model.model.model.layers.1.self_attn.q_proj.lora_A.default.weight', 'base_model.model.model.layers.1.self_attn.q_proj.lora_B.default.weight', 'base_model.model.model.layers.1.self_attn.k_proj.wdel.model.model.layers.1.self_attn.v_proj.weight', 'base_model.model.model.layers.1.self_attn.v_proj.lora_A.default.weight', 'base_model.model.model.layers.1.self_attn.v_proj.lora_B.default.weight', 'base_model.model.model.layers.1.self_ht', 'base_model.model.model.layers.1.self_attn.rotary_emb.inv_freq', 'base_model.model.model.layers.1.mlp.gate_proj.weight', 'base_model.model.model.layers.1.mlp.down_proj.weight', 'base_model.model.model.layers.1.mlp.up_proj.weight',
Does anyone know why this is
How did you save the checkpoint? In some circumstances, it can happen that values are lost if not careful, e.g. in distributed training.
Regarding the checkpoint, could you try loading it directly, e.g.
from safetensors.torch import load_file
sd = load_file(...)
print(sd.keys())
It would be interesting to know which keys are present.
1 Like