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

RuntimeError in compute_loss function: Tensor with 2 elements cannot be converted to Scalar #1

Open
lenijwp opened this issue Jul 28, 2024 · 0 comments

Comments

@lenijwp
Copy link

lenijwp commented Jul 28, 2024

When executing the code, an error occurs in the compute_loss function in hfutil/hf_trainers.py. The error message is as follows:

Error executing job with overrides: ['data=harry', 'data.dataset.split=None', 'model=llama-2-7b', 'model_mode=uld', 'model_mode.num_layer=8', 'unlearn_loss=remember+uniform', 'trainer.strategy=ddp', 'OUTPUTMODELDIR=../Model/unlearned_model/uld']
Traceback (most recent call last):
  File "/home/lenijwp/codes/testUnlearning/unlearning_methods/ULD/scripts/hf_forget_train.py", line 193, in main
    trainer.train()
  File "/home/lenijwp/.conda/envs/uld/lib/python3.10/site-packages/transformers/trainer.py", line 1624, in train
    return inner_training_loop(
  File "/home/lenijwp/.conda/envs/uld/lib/python3.10/site-packages/transformers/trainer.py", line 1961, in innertraining_loop
    tr_loss_step = self.training_step(model, inputs)
  File "/home/lenijwp/.conda/envs/uld/lib/python3.10/site-packages/transformers/trainer.py", line 2902, in training_step
    loss = self.compute_loss(model, inputs)
  File "/home/lenijwp/codes/testUnlearning/unlearning_methods/ULD/uld/hfutil/hf_trainers.py", line 93, in compute_loss
    'trainloss/loss': loss.item(),
RuntimeError: a Tensor with 2 elements cannot be converted to Scalar

The problematic code seems:

logitems = {
    'trainloss/loss': loss.item(),
    'trainloss/forgetloss': forgetloss.item(),
    'trainloss/retainloss': retainloss.item()
}

To resolve the issue, I needed to replace it with:

logitems = {
    'trainloss/loss': loss.sum().item(),
    'trainloss/forgetloss': forgetloss.sum().item(),
    'trainloss/retainloss': retainloss.sum().item()
}
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