Error (also in original) model, scaling only q matrix not qk.T dot product (qk.T/sqrt(dim_per_head))
Author: BenoitDalFerro
Pull request: yes

As per Vaswani et al, 2017 p.4
Is torch.matmul(q, k.transpose(2, 3)) / math.sqrt(dim_per_head) not q / math.sqrt(dim_per_head) https://arxiv.org/pdf/1912.05372.pdf

Error was in original FlauBERT repo and effectively scales queries but not keys cf. https://github.com/getalp/Flaubert/pull/45/commits/6d176880ca3a1a8dfa2b76c97030bb51c5e917b8

# What does this PR do?

<!--
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/main/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/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/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 who 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:

- text models: @ArthurZucker and @younesbelkada
- vision models: @amyeroberts and @NielsRogge
- speech models: @sanchit-gandhi
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @gante
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @sgugger

Integrations:

- deepspeed: HF Trainer: @stas00, Accelerate: @pacman100
- ray/raytune: @richardliaw, @amogkam

Documentation: @sgugger, @stevhliu and @MKhalusova

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @sanchit-gandhi
- PyTorch: @sgugger
- TensorFlow: @Rocketknight1

 -->


Comments:
cc @ArthurZucker and @younesbelkada 

Also note that the same change would need to be applied to XLM.

_The documentation is not available anymore as the PR was closed or merged._

Hi @younesbelkada ok I'm on it thanks

@younesbelkada curious `make fixup` executed fine and passing `make repo_consistency` locally but not remotely any clue ?

This comment might help you: https://github.com/huggingface/transformers/pull/20939#issuecomment-1423974311

Looking closer at your code, maybe a bad rebase happened on the `xlm` file, can you revert the changes there, and just modify the line as you did for flaubert, then run `make fix-copies` ?

@younesbelkada on a side note, the sqrt could be computed only once at init as self.sqrt_d and also with torch.sqrt() which is about 29x faster than math.sqrt() cf. benchmark https://twitter.com/k_saifullaah/status/1430510295257030658/photo/1

This is interesting, thanks for sharing! 
Feel free to add this change inside [`MultiHeadAttention`](https://github.com/huggingface/transformers/blob/d3b1adf59fff726f1c8f324728e562237f080ce6/src/transformers/models/xlm/modeling_xlm.py#L104) and then apply `make fix-copies` so that users can benefit from interesting speedups as you are suggesting
Otherwise, this can be addressed in a follow up PR too

Will do next in new PR to ensure consistency with above title