Retraining a peft model after loading

pretty much the title. I am low on resources so I am training Peft adapters then saving them and loading them to retrain them on next data. but when I load adapters after training I get error also when I check the trainable parameters it prints the number of adapter correctly.

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

device = “cuda” if torch.cuda.is_available() else “cpu”
model_id = “Qwen/Qwen2.5-VL-3B-Instruct”

base_model = AutoModelForImageTextToText.from_pretrained(
model_id,
torch_dtype=torch.float16,
load_in_8bit=True, # Load in 8-bit precision
device_map=“auto”
)

min_pixels = 2242828
max_pixels = 2242828
processor = AutoProcessor.from_pretrained(model_id, min_pixels=min_pixels, max_pixels=max_pixels)
processor.tokenizer.padding_side = “right”

model = PeftModel.from_pretrained(base_model, “./docmat/iter-1”, is_trainable=True)

optimizer = AdamW(model.parameters(), lr=2e-6)

training_args = TrainingArguments(
output_dir=“./results”,
learning_rate=2e-6,
per_device_train_batch_size=6,
per_device_eval_batch_size=4,
num_train_epochs=1,
weight_decay=0.01,
logging_dir=“./logs”,
logging_steps=50,
save_steps=1500,
gradient_checkpointing=True,
gradient_accumulation_steps=6,
max_grad_norm= 1,
remove_unused_columns= False,
no_cuda=False
)

def batch_collate_fn(examples):
texts = [processor.apply_chat_template(example, tokenize=False) for example in examples]
image_inputs = [example[1][“content”][0][“image”] for example in examples]
inputs = processor(
text=texts,
images=image_inputs,
return_tensors=“pt”,
padding=True
)
inputs[“labels”] = inputs[“input_ids”].clone()
return inputs

trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
optimizers=(optimizer, None),
data_collator=batch_collate_fn
)

print(“Starting training…”)
trainer.train()
print(“-”*30)

print(“Final evaluation…”)
metric = trainer.evaluate()
print(f"Final Evaluation metrics: {metric}")

1 Like

This might fix it in simple cases.

Hello, is_trainable=True is required to load the pretrained adapter and have them in trainable mode.
Please pass PeftModel.from_pretrained(model, peft_model_id, is_trainable=True).to(device) and let us know if that solves the issue

But in your case, it’s set to True…

load_in_8bit=True, # Load in 8-bit precision

Maybe it’s because of the quantization.