tloen / alpaca-lora

Instruct-tune LLaMA on consumer hardware
Apache License 2.0
18.66k stars 2.22k forks source link

Cannot backpropagate on the loss #595

Open ajsanjoaquin opened 1 year ago

ajsanjoaquin commented 1 year ago

When passing a tensor to the model, collecting the loss, and calling loss.backward(), i'm getting the error: Element 0 of tensors does not require grad and does not have a grad_fn

Here's a minimum reproduceable code:

from peft import PeftModel
from transformers import LLaMAForCausalLM

model = LLaMAForCausalLM.from_pretrained(
    "decapoda-research/llama-7b-hf",
    load_in_8bit=True,
    device_map="auto",)
model = PeftModel.from_pretrained(model, "tloen/alpaca-lora-7b")

model(torch.ones(2, 2).type(torch.LongTensor), labels = torch.ones(2,2).type(torch.LongTensor)).loss
>>> tensor(50.9062, dtype=torch.float16)

where I expect the loss tensor to have a grad_fcn, and / or requires_grad=True.

like

from transformers import BertForSequenceClassification
>>> model = BertForSequenceClassification.from_pretrained("bert-base-uncased", return_dict=True)
>>> model(torch.ones(2,34).long(), labels = torch.ones(2,2)).loss
tensor(0.8236, grad_fn=<BinaryCrossEntropyWithLogitsBackward0>)

from here

Please help me.

ahmadmobeen commented 5 days ago

Did you solve the problem? I am also facing the same issue.