Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot backpropagate on the loss #595

Open
ajsanjoaquin opened this issue Oct 26, 2023 · 0 comments
Open

Cannot backpropagate on the loss #595

ajsanjoaquin opened this issue Oct 26, 2023 · 0 comments

Comments

@ajsanjoaquin
Copy link

ajsanjoaquin commented Oct 26, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant