alpaca-lora icon indicating copy to clipboard operation
alpaca-lora copied to clipboard

Cannot backpropagate on the loss

Open ajsanjoaquin opened this issue 8 months ago • 0 comments

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.

ajsanjoaquin avatar Oct 26 '23 09:10 ajsanjoaquin