Add PreLN to fsmt module
Author: jinmang2
Pull request: yes

# 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 -->

Add pre-layer normalization module to FSMT module to match fairseq transformers.


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] 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?
@stas00

<!-- 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: @LysandreJik

Library:

- benchmarks: @patrickvonplaten
- deepspeed: @stas00
- ray/raytune: @richardliaw, @amogkam
- text generation: @patrickvonplaten
- tokenizers: @n1t0, @LysandreJik
- trainer: @sgugger
- pipelines: @LysandreJik

Documentation: @sgugger

HF projects:

- datasets: [different repo](https://github.com/huggingface/datasets)
- 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

 -->


Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_15747). All of your documentation changes will be reflected on that endpoint.

cc: @patil-suraj - if you'd like to have another pair of eyes on this PR and also fyi as this will now create a bit of a diversion from your unmerged fsmt PR from long time ago. Not sure what you would want to do about it. It has kind of fallen between cracks due to the performance regression.

This is typically the type of changes we usually don't accept inside one model architecture as it doesn't pass the test for new models: can an existing checkpoint for FSMT be used with this new config argument and give sensible results? -> No

I think this should warrant a new model.

This is how BART was initially designed and was refactored to align with the general philosophy of the library to have self-contained model files. cf #9343. So this should be a new model. 

Also there are other fairseq models in the library that use `PreLN` for ex mBART and M2M100. Could you maybe check if you can load your checkpoints with these models ?





> This is typically the type of changes we usually don't accept inside one model architecture as it doesn't pass the test for new models: can an existing checkpoint for FSMT be used with this new config argument and give sensible results? -> No

Not arguing whether it should be a new model or not, but unless I'm missing something, why the answer to :

>  can an existing checkpoint for FSMT be used with this new config argument and give sensible results?

is `No`? I don't see any reason why it shouldn't work and not give identical results with this PR? It won't have any of the new model config entries and will work as intended.

First of all, thank you for leaving a comment about my PR!

I wanted to port pororo's translation and wsd model to transformers. When I tested it myself, I got the same result as fairseq's reasoning result when I modified it with the corresponding PR.
- https://github.com/kakaobrain/pororo

I will check the following items mentioned above and mention them.
- mBART
- Test results

> Not arguing whether it should be a new model or not, but unless I'm missing something, why the answer to *can an existing checkpoint for FSMT be used with this new config argument and give sensible results?* is No? I don't see any reason why it shouldn't work and not give identical results with this PR? It won't have any of the new model config entries and will work as intended.

When using an existing pretrained checkpoint with the config option `encoder_normalize_before=True` or `decoder_normalize_before=True`, the pretrained model will load properly (as all weights match) but a user won't get decent results with the model.

We can write a perfect modular toolbox that work with a current model implementation without breaking backward compatibility and have identical results, with just adding many new options in the configuration. That does not mean it conforms to the philosophy or pass the test of "this new option can be used with existing checkpoints".

> When using an existing pretrained checkpoint with the config option `encoder_normalize_before=True` or `decoder_normalize_before=True`, the pretrained model will load properly (as all weights match) but a user won't get decent results with the model.

If you're talking about the existing checkpoints - they won't have these config flags set in their config and as long as the default value for the new config options is `False` they will get identical results, as essentially they will be running the same modeling code as before this PR.

If the defaults for the new config options are set to True, then indeed it'd break.



So talked some more with Sylvain and we aren't using the same test, hence the different pictures. He was using defaults `True` for new configs, I was using defaults `False` - hence the difference in the outcome.

And for me the idea of when using a new arch or not comes down to:

1. the config is for minor settings, like beam width, activation checkpointing, etc. which sometimes may lead to slightly less than optimal outcome but as close as possible to the best result.
2. any new config that changes the control flow (enables/disables code/changes order) which would lead to a different numerical outcome on the modeling level requires a new arch.

In other words no new architectural features can be added once a given modeling code is unleashed into the world and at least one checkpoint started using it, and a new architecture needs to be created.


I did a force-pushed on my local branch 30 minutes ago because of my commit mistake. If this behavior is a problem, please let me know! I will close the PR and commit a new one.

> This is how BART was initially designed and was refactored to align with the general philosophy of the library to have self-contained model files. cf https://github.com/huggingface/transformers/pull/9343. So this should be a new model.

> Also there are other fairseq models in the library that use PreLN for ex mBART and M2M100. Could you maybe check if you can load your checkpoints with these models ?

@patil-suraj Because there is no `SinusoidalPositionalEmbedding` class in mBART script, missing key occurs separately from the convert script. Therefore, porting with the script seems impossible.

> Overall looks good. I left a few suggestions.

@stas00 I checked all your suggestions and fixed them all in 2982403 commit!

> The next step is to add a functional test, by building upon one of the existing fsmt tests.

Does the functional test need to modify `test_modeling_fsmt.py`? Or can I provide the test result to the PR to see if the `pre_layernorm` option works normally?

> Also if there are pre-trained models we could port that use preLN - it'd be great for then doing a quality test, which would be tricky to do with a dummy random data.

Among the models of the [PORORO library](https://github.com/kakaobrain/pororo) developed by OSLO developer hyunwoong ko, some models such as translation and word sense disambiguation use the `normalize_before` option. Is it okay to test the result and reflect it in the convert script?

> > This is how BART was initially designed and was refactored to align with the general philosophy of the library to have self-contained model files. cf #9343. So this should be a new model.
> 
> > Also there are other fairseq models in the library that use PreLN for ex mBART and M2M100. Could you maybe check if you can load your checkpoints with these models ?
> 
> @patil-suraj Because there is no `SinusoidalPositionalEmbedding` class in mBART script, missing key occurs separately from the convert script. Therefore, porting with the script seems impossible.

I see. Could you try with `M2M100` then ? M2M100 also uses `PreLN` and `SinusoidalPositionalEmbedding`.

> > Overall looks good. I left a few suggestions.
> 
> @stas00 I checked all your suggestions and fixed them all in [2982403](https://github.com/huggingface/transformers/commit/298240385f570cae0616cf502b36b8bd909336d7) commit!

Thank you.

> > The next step is to add a functional test, by building upon one of the existing fsmt tests.
> 
> Does the functional test need to modify `test_modeling_fsmt.py`? Or can I provide the test result to the PR to see if the `pre_layernorm` option works normally?

I'm not sure I understand your question. What I meant is this new code can't be merged w/o tests exercising it.

And, of course, I'm not trying to contradict the maintainers who requested a new architecture. But it'd need a new test and it'd be something new and not `test_modeling_fsmt.py`, but you can of course model it after. Probably `test_modeling_fsmt_preln.py` or something of the sorts - based on the new arch name that you will choose.
 
> > Also if there are pre-trained models we could port that use preLN - it'd be great for then doing a quality test, which would be tricky to do with a dummy random data.
> 
> Among the models of the [PORORO library](https://github.com/kakaobrain/pororo) developed by OSLO developer hyunwoong ko, some models such as translation and word sense disambiguation use the `normalize_before` option. Is it okay to test the result and reflect it in the convert script?

if these models fit the architecture by all means that would be perfect. Except we would want the model to be on the https://huggingface.co/models hub first, so that the test suite could run reliably.