FIX Correctly determine word embeddings on Deberta #2257
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
After a recent change in transformers, PEFT could no longer determine the word embeddings from Deberta. This PR provides a very minimal fix that correctly determines the word embeddings again.
Failing CI
To reproduce, run:
pytest tests/test_feature_extraction_models.py -k "prompt_tuning and deberta" -v
Details
Previously, the word embeddings were determined in the following manner:
transformers_backbone
by checking the base model's children forPreTrainedModel
instances(code)
Before the mentioned transformers PR, 1. did not find anything, so 2. was applied. After the PR, however, the
DebertaEncoder
is now an instance ofPreTrainedModel
(asked internally, this is intended). Therefore, the encoder is now considered the transformer backbone. But the encoder does not have the word embeddings attribute, therefore step 3. fails.The fix of this PR is to first explicitly check for
model.embeddings.word_embeddings
and if this attribute is found, use it as the word embeddings. Only when it's not found do we use the other method described above. This way, we can successfully determine the word embeddings on models like Deberta.This whole code is a bit messy and could probably be improved. However, changing the logic too much could inadvertently break for some existing model architectures that are not included in the tests. Therefore, I chose this method which leaves the existing logic mostly intact.
For reviewers: Note that the previous logic has not been changed, just moved into an
if
block. The actual diff is thus much smaller than it appears at first glance.