# Retraining a peft model after loading

**URL:** <https://discuss.huggingface.co/t/retraining-a-peft-model-after-loading/140893>\
**Category:** Beginners\
**Created:** [February 14, 2025, 5:29pm UTC](https://discuss.huggingface.co/t/retraining-a-peft-model-after-loading/140893 "2025-02-14T17:29:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sourabhY](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/sourabhy/32/40447_2.png) [@sourabhY](https://discuss.huggingface.co/u/sourabhY)\
**Post date:** [February 14, 2025, 5:29pm UTC](https://discuss.huggingface.co/t/retraining-a-peft-model-after-loading/140893/1 "2025-02-14T17:29:23Z")

</div>

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 = 224_28_28  
max\_pixels = 224_28_28  
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}")

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [February 15, 2025, 7:47am UTC](https://discuss.huggingface.co/t/retraining-a-peft-model-after-loading/140893/2 "2025-02-15T07:47:40Z")

</div>

This might fix it in simple cases.

> <https://github.com/huggingface/peft/issues/137>
>
> Hi!
> 
> I tried to use peft model with Seq2SeqTrainer, got the next error:
> \`\`\`
> …Variable.\_execution\_engine.run\_backward( # Calls into the C++ engine to run the backward pass
> RuntimeError: element 0 of tensors does not require grad and does not have a grad\_fn
> \`\`\`
> 
> Code:
> 
> \`\`\`
> tokenizer = AutoTokenizer.from\_pretrained(args.model\_id)
> model = AutoModelForSeq2SeqLM.from\_pretrained(
> "google/flan-t5-large",
> use\_cache=False if args.gradient\_checkpointing else True, 
> )
> 
> lora\_config = LoraConfig(
> r=16,
> lora\_alpha=32,
> target\_modules=\["q", "v"\],
> lora\_dropout=0.05,
> bias="none",
> task\_type="SEQ\_2\_SEQ\_LM"
> )
> 
> model = get\_peft\_model(model, lora\_config)
> 
> 
> trainer = Seq2SeqTrainer(
> model=model,
> args=training\_args,
> train\_dataset=train\_dataset,
> eval\_dataset=eval\_dataset,
> )
> \`\`\`

> 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…

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](https://discuss.huggingface.co/u/John6666)\
**Post date:** [February 15, 2025, 7:56am UTC](https://discuss.huggingface.co/t/retraining-a-peft-model-after-loading/140893/3 "2025-02-15T07:56:47Z")

</div>

> load\_in\_8bit=True, # Load in 8-bit precision

Maybe it’s because of the quantization.

> [@How to fix RuntimeError: element 0 of tensors does not require grad and does not have a grad\_fn](https://discuss.huggingface.co/t/how-to-fix-runtimeerror-element-0-of-tensors-does-not-require-grad-and-does-not-have-a-grad-fn/91217):
>
> I am trying to finetune a model: TheBloke/Mistral-7B-Instruct-v0.1-GPTQ. However, I am getting RuntimeError: element 0 of tensors does not require grad and does not have a grad\_fn error. How can I fix this error? Here is the code that is throwing the error: import transformers from datetime import datetime project = "Mixtral-alpaca-finance-finetune" base\_model\_name = "mixtral" run\_name = base\_model\_name + "-" + project output\_dir = "./" + run\_name tokenizer.pad\_token = tokenizer.eos\_token …
