[WIP] T5v1.1 & MT5
Author: patrickvonplaten
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 -->

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 the 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.

 albert, bert, XLM: @LysandreJik
 GPT2: @LysandreJik, @patrickvonplaten
 tokenizers: @mfuntowicz
 Trainer: @sgugger
 Benchmarks: @patrickvonplaten
 Model Cards: @julien-c
 examples/distillation: @VictorSanh
 nlp datasets: [different repo](https://github.com/huggingface/nlp)
 rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)
 Text Generation: @patrickvonplaten, @TevenLeScao
 Blenderbot, Bart, Marian, Pegasus: @patrickvonplaten
 T5: @patrickvonplaten
 Rag: @patrickvonplaten, @lhoestq
 EncoderDecoder: @patrickvonplaten
 Longformer, Reformer: @patrickvonplaten
 TransfoXL, XLNet: @TevenLeScao, @patrickvonplaten
 examples/seq2seq: @patil-suraj
 examples/bert-loses-patience: @JetRunner
 tensorflow: @jplu
 examples/token-classification: @stefan-it
 documentation: @sgugger
 FSTM: @stas00
 -->


Comments:
Maybe wrong model config for T5.1.1. For instance, T5.1.1.small should have num_layers=8 and num_heads=6.

See https://github.com/google-research/text-to-text-transfer-transformer/blob/master/t5/models/gin/models/t5.1.1.small.gin

> Maybe wrong model config for T5.1.1. For instance, T5.1.1.small should have num_layers=8 and num_heads=6.
> 
> See https://github.com/google-research/text-to-text-transfer-transformer/blob/master/t5/models/gin/models/t5.1.1.small.gin

Thanks yeah, I implemented that. 

The new model structure is now equal to mesh t5 v1.1. 

If you download the t5v1.1 `t5-small` checkpoint and replace the corresponding path in `check_t5_against_hf.py` you can see that the models are equal. 

There is still quite some work to do: write more tests, lots of cleaning and better design, and check if mT5 works with it.

> If you download the t5v1.1 `t5-small` checkpoint and replace the corresponding path in `check_t5_against_hf.py` you can see that the models are equal.

Hi, `check_t5_against_hf.py` still fails if I use a longer input text instead of `Hello there`, like `Hello there. Let's put more words in more languages than I originally thought.`


> > If you download the t5v1.1 `t5-small` checkpoint and replace the corresponding path in `check_t5_against_hf.py` you can see that the models are equal.
> 
> Hi, `check_t5_against_hf.py` still fails if I use a longer input text instead of `Hello there`, like `Hello there. Let's put more words in more languages than I originally thought.`

Hmm, it works for me - do you experience that for T5v1.1 or mT5?

> > > If you download the t5v1.1 `t5-small` checkpoint and replace the corresponding path in `check_t5_against_hf.py` you can see that the models are equal.
> > 
> > 
> > Hi, `check_t5_against_hf.py` still fails if I use a longer input text instead of `Hello there`, like `Hello there. Let's put more words in more languages than I originally thought.`
> 
> Hmm, it works for me - do you experience that for T5v1.1 or mT5?

Aha, the checking is OK now. Yesterday I made a mistake that when I changed the test input sentence in the check script, I didn't update the input length for MTF model from 4 to a longer value like 128. So actually the MTF model and PyTorch model received different inputs, and of course got different results.

Besides, if I add the z-loss to the CE loss at last, it differs from MTF score again. I just found MTF ignores z-loss when not training ([code](https://github.com/tensorflow/mesh/blob/4f82ba1275e4c335348019fee7974d11ac0c9649/mesh_tensorflow/transformer/transformer.py#L781)). So I think MTF model score does not include z-loss, but its training does, which is absent from HF T5 training. Well, this is absolutely not a blocking issue now.

Appreciate your great work :) 

closing in favor of https://github.com/huggingface/transformers/pull/8552.