adapters icon indicating copy to clipboard operation
adapters copied to clipboard

Error when trying to do torch.save

Open SSamDav opened this issue 2 years ago • 2 comments

When running this code

import adapters
import torch
from transformers import AutoConfig, AutoModelForSequenceClassification

source_model = "FacebookAI/xlm-roberta-base"
num_labels =  100
config = AutoConfig.from_pretrained(
    source_model, num_labels=num_labels
)
model = AutoModelForSequenceClassification.from_pretrained(
    source_model, config=config
)

adapters.init(model)
torch.save(model, "test.pth")

I got the following error:

Can't pickle <class 'abc.XLMRobertaForSequenceClassification'>: attribute lookup XLMRobertaForSequenceClassification on abc failed

I'm using the following setup:

adapters==0.1.1
torch==1.13.1
transformers==4.35.2

SSamDav avatar Feb 12 '24 17:02 SSamDav

It seems the problem is because the code creates a class in "execution time" using

model_class = type(
    model_class_name,
    (EmbeddingAdaptersWrapperMixin, ModelWithHeadsAdaptersMixin, model.__class__),
    {},
)

There is any way I could do torch.save and preserving the class?

SSamDav avatar Feb 12 '24 18:02 SSamDav

I found that the model-saving operations was achieved by model.save_adapter(). In your case, after adding adapters into models, It may not support torch save anymore.

conglei2XU avatar Mar 15 '24 09:03 conglei2XU