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

ValueError: The pre-trained model weights are not frozen. Despite proper setup. #540

Closed
davidobermann opened this issue May 2, 2023 · 4 comments
Labels
question Further information is requested Stale

Comments

@davidobermann
Copy link

davidobermann commented May 2, 2023

My training setup produces an Error:
ValueError: The pre-trained model weights are not frozen. For training adapters, please call the train_adapter() method

This occurs in the trainer.train() call.

I setup a BERT Finetuning with adapters and a PredictionHead.
In my setup I DO call the train_adapter method, I also tried to add the set_active_adapters, use the fields to set the active adapters and tried to follow every single guide I could find.
I even tried to freeze the model weights by calling the freeze method myself.
I am almost sure my setup is to blame maybe somebody already encountered this before.

Full Traceback:

Traceback (most recent call last):
File "/home/davido/dr/./star/train_adapters.py", line 267, in
main()
File "/home/davido/dr/./star/train_adapters.py", line 257, in main
trainer.train()
File "/home/davido/miniconda3/lib/python3.10/site-packages/transformers/trainer.py", line 1543, in train
return inner_training_loop(
File "/home/davido/miniconda3/lib/python3.10/site-packages/transformers/trainer.py", line 1720, in _inner_training_loop
self.control = self.callback_handler.on_train_begin(args, self.state, self.control)
File "/home/davido/miniconda3/lib/python3.10/site-packages/transformers/trainer_callback.py", line 353, in on_train_begin
return self.call_event("on_train_begin", args, state, control)
File "/home/davido/miniconda3/lib/python3.10/site-packages/transformers/trainer_callback.py", line 397, in call_event
result = getattr(callback, event)(
File "/home/davido/miniconda3/lib/python3.10/site-packages/transformers/adapters/trainer.py", line 253, in on_train_begin
raise ValueError(
ValueError: The pre-trained model weights are not frozen. For training adapters, please call the train_adapter() method

My Model Setup:

class AdapterBertDot(BaseModelDot, BertAdapterModel):
    def __init__(self, config, model_argobj=None):
        BaseModelDot.__init__(self, model_argobj)
        BertAdapterModel.__init__(self, config)
        if int(transformers.__version__[0]) == 4:
            config.return_dict = False
        self.bert = BertAdapterModel(config)
        if hasattr(config, "output_embedding_size"):
            self.output_embedding_size = config.output_embedding_size
        else:
            self.output_embedding_size = config.hidden_size
        print("output_embedding_size", self.output_embedding_size)

        self.task_name = 'dpr'

        self.bert.register_custom_head('dpr-head', DPRHead)
        self.bert.add_custom_head(head_type='dpr-head', head_name=self.task_name)
        self.bert.add_adapter(self.task_name, config='pfeiffer')
        self.bert.train_adapter([self.task_name])
        self.bert.set_active_adapters([self.task_name])
        #self.apply(self._init_weights)

    ....
@davidobermann davidobermann added the question Further information is requested label May 2, 2023
@hSterz
Copy link
Member

hSterz commented May 4, 2023

Hey @davidobermann, the setup looks good to me. You can remove the set_active_adaptersmethod call since this is already activated by the train_adapter method. But this should not change the validity of the setup. You can check whether the setup adds the adapter, activates it, and freezes the weights by checking that bert.active_adapters has the correct setup and requires_gradis False for the pre-trained model weights by iterating over bert.named_parameters().
Let me know whether that helps with your problem.

@davidobermann
Copy link
Author

Thanks @hSterz for your fast reply, So i did find a way to get it to run by calling freeze_model(True) on the model.
Although now only the head is kept active. When checking the named paramters the entire model is frozen inlcuding the adapter layers.

@adapter-hub-bert
Copy link
Member

This issue has been automatically marked as stale because it has been without activity for 90 days. This issue will be closed in 14 days unless you comment or remove the stale label.

@adapter-hub-bert
Copy link
Member

This issue was closed because it was stale for 14 days without any activity.

@adapter-hub-bert adapter-hub-bert closed this as not planned Won't fix, can't repro, duplicate, stale Aug 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested Stale
Projects
None yet
Development

No branches or pull requests

3 participants