Open MabelQi opened 3 weeks ago
I took a look at your example but since you're using custom models and datasets, I cannot reproduce it. Instead, I tried to simplify the example as much as I could, resulting in the code below. It uses the codebert model from MS and some dummy data:
import torch
from torch.optim import AdamW
from peft import (
get_peft_model,
PeftType,
PrefixTuningConfig,
)
from transformers import RobertaForSequenceClassification
model_name_or_path = "microsoft/codebert-base" # BB
model = RobertaForSequenceClassification.from_pretrained(model_name_or_path, return_dict=True, num_labels=2) # BB
batch_size = 1
gradient_accumulation_steps = 4
num_epochs = 5
lr = 3e-2
max_input_length = 512
peft_config = PrefixTuningConfig(
task_type='SEQ_CLS',
num_virtual_tokens=1 # to be changed
)
model = get_peft_model(model, peft_config)
model.print_trainable_parameters()
optimizer = AdamW(model.parameters(), lr=lr)
model.train()
for epoch in range(num_epochs):
outputs = model(torch.arange(10).view(-1, 1), torch.ones(10).view(-1, 1), labels=torch.zeros(20).view(10, 2))
loss = outputs.loss
loss.backward()
optimizer.step()
optimizer.zero_grad()
print(epoch, loss.item())
print("finished successfully")
When I run it locally, it passes. Therefore, it's unclear to me what goes wrong with your script. If possible, I would advise to run this code on CPU with a debugger turned on and check what exactly is going wrong, as it looks like you're indexing outside of the tensor dimensions. If you do that, you should also be able to provide the full stack trace of the error, which could also help investigating.
@BenjaminBossan Thank you for your quick reply! ☺️ I've fixed this issue but now new issue appears: I am trying to use PEFT methods to fine tune Codebert for multi-label classification, which means I set num_labels=5. Doese PEFT library support fine tuning models for multi-label classification tasks? I doubt it because all the examples are setting num_label=2
Yes, it should totally work for 5 labels. Did you try it and encounter an error?
@BenjaminBossan Yes I tried it, it did not report any errors. However, compared to extracting part of my dataset turning it into a binary classification task, the result is extremely poor when I set num_labels=5 (a multi-label classification).
Well, predicting 5 classes is harder than 2, so it's hard to tell if there is something wrong or if the output is expected. Could you please show me the repr
of your model (i.e. what is shown when calling print(model)
) so that I can double-check if it looks correct?
System Info
linux
Who can help?
@pacman100 @younesbelkada @BenjaminBossan
When I used prefix tuning to fine-tune codebert for sequence classification, it showed the following errors:
I used RobertaForSequenceClassification to load codebert. Did the error occure for the reason that PEFT does not support codebert?
Information
Tasks
examples
folderReproduction
Expected behavior
finetune the codebert model