Fix v2 model loading issue
# What does this PR do?
Fix few issues with loading deberta v2 models and deberta mnli fine-tuned model
<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

<!-- Remove if not applicable -->

Fixes # (issue)


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/master/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/master/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors which may be interested in your PR.

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- albert, bert, xlm: @LysandreJik
- blenderbot, bart, marian, pegasus, encoderdecoder,  t5: @patrickvonplaten, @patil-suraj
- longformer, reformer, transfoxl, xlnet: @patrickvonplaten
- fsmt: @stas00
- funnel: @sgugger
- gpt2: @patrickvonplaten, @LysandreJik
- rag: @patrickvonplaten, @lhoestq
- tensorflow: @jplu

Library:

- benchmarks: @patrickvonplaten
- deepspeed: @stas00
- ray/raytune: @richardliaw, @amogkam
- text generation: @patrickvonplaten
- tokenizers: @n1t0, @LysandreJik
- trainer: @sgugger
- pipelines: @LysandreJik

Documentation: @sgugger

HF projects:

- nlp datasets: [different repo](https://github.com/huggingface/nlp)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Examples:

- maintained examples (not research project or legacy): @sgugger, @patil-suraj
- research_projects/bert-loses-patience: @JetRunner
- research_projects/distillation: @VictorSanh

 -->

Hi @BigBird01, thanks a lot for fixing these issues! I'd like to prevent as much as possible from having the `pre_load_hooks` in the code. When do you expect mismatched head dimensions?<|||||>This is the case that if we want to fine-tune a task based on mnli models, e.g. MRPC, SST, QNLI. If we want to avoid this method, we need to fix the error reporting when load pretrained models.

Get Outlook for iOS<https://aka.ms/o0ukef>
________________________________
From: Lysandre Debut <notifications@github.com>
Sent: Saturday, February 13, 2021 5:23:53 AM
To: huggingface/transformers <transformers@noreply.github.com>
Cc: Pengcheng He <biglm100@gmail.com>; Mention <mention@noreply.github.com>
Subject: Re: [huggingface/transformers] Fix v2 model loading issue (#10129)


Hi @BigBird01<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2FBigBird01&data=04%7C01%7CPengcheng.H%40microsoft.com%7C301190f89ac34907b53408d8d0229db7%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637488194400010761%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=9%2BnuKLc3nPym5xSUhe%2Flko3Fd3jxus0WRwj3W%2Bks%2FuM%3D&reserved=0>, thanks a lot for fixing these issues! I'd like to prevent as much as possible from having the pre_load_hooks in the code. When do you expect mismatched head dimensions?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fhuggingface%2Ftransformers%2Fpull%2F10129%23issuecomment-778618852&data=04%7C01%7CPengcheng.H%40microsoft.com%7C301190f89ac34907b53408d8d0229db7%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637488194400010761%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=ka8Oo2xj%2Ba0u2X6Fobb77H27KO94ePzlbCeZ7a797ww%3D&reserved=0>, or unsubscribe<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAJDNDRT5T5MFRSCQH3MXIEDS6Z4OTANCNFSM4XOC2W6A&data=04%7C01%7CPengcheng.H%40microsoft.com%7C301190f89ac34907b53408d8d0229db7%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637488194400020711%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=kdOpjyJfnJyaSLKNbPjNIMs3p2KbiceAVHHS4WCfVEA%3D&reserved=0>.
<|||||>Usually we recommend to load through the base model to lose the head:

```py
from transformers import DebertaV2Model, DebertaV2ForSequenceClassification

seq_model = DebertaV2ForSequenceClassification.from_pretrained("xxx", num_labels=4)
seq_model.save_pretrained(directory)

base = DebertaV2Model.from_pretrained(directory)  # Lose the head
base.save_pretrained(directory)

seq_model = DebertaV2ForSequenceClassification.from_pretrained(directory, num_labels=8)
```

Does that work in your case? I agree you're touching to something that has a bad API, and this should be handled in the `from_pretrained` method. I don't think we should handle it model-wise, however. I'll look into it soon.<|||||>yes. but this looks a little bit tricky. And need to modify existing text classification code to benefit from mnli fine-tuned models. How about we keep current hook method, and finish current PR? After we work out a systematic solution for such scenario, we can drop the hook method.

Get Outlook for iOS<https://aka.ms/o0ukef>
________________________________
From: Lysandre Debut <notifications@github.com>
Sent: Saturday, February 13, 2021 6:28:34 AM
To: huggingface/transformers <transformers@noreply.github.com>
Cc: Pengcheng He <biglm100@gmail.com>; Mention <mention@noreply.github.com>
Subject: Re: [huggingface/transformers] Fix v2 model loading issue (#10129)


Usually we recommend to load through the base model to lose the head:

from transformers import DebertaV2Model, DebertaV2ForSequenceClassification

seq_model = DebertaV2ForSequenceClassification.from_pretrained("xxx", num_labels=4)
seq_model.save_pretrained(directory)

base = DebertaV2Model.from_pretrained(directory)  # Lose the head
base.save_pretrained(directory)

seq_model = DebertaV2ForSequenceClassification.from_pretrained(directory, num_labels=8)

Does that work in your case? I agree you're touching to something that has a bad API, and this should be handled in the from_pretrained method. I don't think we should handle it model-wise, however. I'll look into it soon.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fhuggingface%2Ftransformers%2Fpull%2F10129%23issuecomment-778627044&data=04%7C01%7CPengcheng.H%40microsoft.com%7C4e6ebb9592e84114db8908d8d02ba69b%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637488233183179214%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=6B7Tw%2BulDyc50hCWtt40Y3Eu%2F9pDhycqsFcGGlbEWJA%3D&reserved=0>, or unsubscribe<https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAJDNDRSALWZ5T6MA5DFF7VDS62EBFANCNFSM4XOC2W6A&data=04%7C01%7CPengcheng.H%40microsoft.com%7C4e6ebb9592e84114db8908d8d02ba69b%7C72f988bf86f141af91ab2d7cd011db47%7C1%7C0%7C637488233183179214%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=DUoGLBFe%2Ff2Ms580JCNn5okYXrYWH9vqTIhAMve0tqE%3D&reserved=0>.
<|||||>Ok, will merge like that and we'll discuss with other team members for the main PR. Thanks!