题意:
Size mismatch for embed_out.weight: copying a param with shape torch.Size([0]) from checkpoint - Huggingface PyTorch
这个错误信息 "Size mismatch for embed_out.weight: copying a param with shape torch.Size([0]) from checkpoint - Huggingface PyTorch" 通常出现在使用 Hugging Face 的 Transformers 库加载预训练模型时,模型的某些参数与预训练模型检查点(checkpoint)中的参数形状不匹配。
问题背景:
I want to finetune an LLM. I am able to successfully finetune LLM. But when reload the model after save, gets error. Below is the code
import argparse
import numpy as np
import torch
from datasets import load_dataset
from transformers import AutoTokenizer, AutoModelForCausalLM
from trl import DPOTrainer, DPOConfig
def preprocess_data(item):
return {
'prompt': 'Instruct: ' + item['prompt'] + '\n',
'chosen': 'Output: ' + item['chosen'],
'rejected': 'Output: ' + item['rejected']
}
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--epochs", type=int, default=1)
parser.add_argument("--beta", type=float, default=0.1)
parser.add_argument("--batch_size", type=int, default=4)
parser.add_argument("--lr", type=float, default=1e-6)
parser.add_argument("--seed", type=int, default=2003)
parser.add_argument("--model_name", type=str, default="EleutherAI/pythia-14m")
parser.add_argument("--dataset_name", type=str, default="jondurbin/truthy-dpo-v0.1")
parser.add_argument("--local_rank", type=int, default=0)
args = parser.parse_args()
# Determine device based on local_rank
device = torch.device("cuda", args.local_rank) if torch.cuda.is_available() else torch.device("cpu")
tokenizer = AutoTokenizer.from_pretrained(args.model_name)
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(args.model_name).to(device)
ref_model = AutoModelForCausalLM.from_pretrained(args.model_name).to(device)
dataset = load_dataset(args.dataset_name, split="train")
dataset = dataset.map(preprocess_data)
# Split the dataset into training and validation sets
dataset = dataset.train_test_split(test_size=0.1, seed=args.seed)
train_dataset = dataset['train']
val_dataset = dataset['test']
training_args = DPOConfig(
learning_rate=args.lr,
num_train_epochs=args.epochs,
per_device_train_batch_size=args.batch_size,
logging_steps=10,
remove_unused_columns=False,
max_length=1024,
max_prompt_length=512,
fp16=True
)
# Verify and print embedding dimensions before finetuning
print("Base model embedding dimension:", model.config.hidden_size)
model.train()
ref_model.eval()
dpo_trainer = DPOTrainer(
model,
ref_model,
beta=args.beta,
train_dataset=train_dataset,
eval_dataset=val_dataset,
tokenizer=tokenizer,
args=training_args,
)
dpo_trainer.train()
# Evaluate
evaluation_results = dpo_trainer.evaluate()
print("Evaluation Results:", evaluation_results)
save_model_name = 'finetuned_model'
model.save_pretrained(save_model_name)
if __name__ == "__main__":
main()
Error I was getting as below
return model_class.from_pretrained(
File "/.local/lib/python3.10/site-packages/transformers/modeling_utils.py", line 3838, in from_pretrained
) = cls._load_pretrained_model(
File "/.local/lib/python3.10/site-packages/transformers/modeling_utils.py", line 4349, in _load_pretrained_model
raise RuntimeError(f"Error(s) in loading state_dict for {model.__class__.__name__}:\n\t{error_msg}")
RuntimeError: Error(s) in loading state_dict for GPTNeoXForCausalLM:
size mismatch for gpt_neox.embed_in.weight: copying a param with shape torch.Size([0]) from checkpoint, the shape in current model is torch.Size([50304, 128]).
size mismatch for embed_out.weight: copying a param with shape torch.Size([0]) from checkpoint, the shape in current model is torch.Size([50304, 128]).
You may consider adding `ignore_mismatched_sizes=True` in the model `from_pretrained` method.
After finetuning, model works perfectly. But after reloading the saved trained model its not working. Any idea why gets this error when reloading the model ?
问题解决:
Instead of
model.save_pretrained(save_model_name)
try this
dpo_trainer.save_model(save_model_name)