Tensorflow improvements
Author: jplu
Pull request: yes

Hello,

Here a quite big PR that propose the following updates:

- Loss computation is now attached to their respective class, such as PyTorch.
- Remove useless `mode` and `loss_name` parameters for the TF Trainer.
- Add missing task models to different Transformers
- Bugfix on T5 keras serialization + tests
- Add tests for TF Flaubert and XLM-Roberta
- Bugfix in TF Trainer for Tensorflow 2.2

Reviews are welcome :)

/cc @julien-c @LysandreJik @thomwolf 

Comments:
# [Codecov](https://codecov.io/gh/huggingface/transformers/pull/4530?src=pr&el=h1) Report
> Merging [#4530](https://codecov.io/gh/huggingface/transformers/pull/4530?src=pr&el=desc) into [master](https://codecov.io/gh/huggingface/transformers/commit/d976ef262e0b2c52363d201b2e14e5ecc42abbb3&el=desc) will **increase** coverage by `0.38%`.
> The diff coverage is `41.45%`.

[![Impacted file tree graph](https://codecov.io/gh/huggingface/transformers/pull/4530/graphs/tree.svg?width=650&height=150&src=pr&token=9qOlN6Hb1c)](https://codecov.io/gh/huggingface/transformers/pull/4530?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master    #4530      +/-   ##
==========================================
+ Coverage   75.63%   76.01%   +0.38%     
==========================================
  Files         128      128              
  Lines       20979    21417     +438     
==========================================
+ Hits        15867    16280     +413     
- Misses       5112     5137      +25     
```


| [Impacted Files](https://codecov.io/gh/huggingface/transformers/pull/4530?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/transformers/data/processors/squad.py](https://codecov.io/gh/huggingface/transformers/pull/4530/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9kYXRhL3Byb2Nlc3NvcnMvc3F1YWQucHk=) | `28.66% <ø> (ø)` | |
| [src/transformers/training\_args\_tf.py](https://codecov.io/gh/huggingface/transformers/pull/4530/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy90cmFpbmluZ19hcmdzX3RmLnB5) | `51.16% <ø> (-4.16%)` | :arrow_down: |
| [src/transformers/trainer\_tf.py](https://codecov.io/gh/huggingface/transformers/pull/4530/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy90cmFpbmVyX3RmLnB5) | `18.86% <17.94%> (+0.94%)` | :arrow_up: |
| [src/transformers/modeling\_tf\_xlm.py](https://codecov.io/gh/huggingface/transformers/pull/4530/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9tb2RlbGluZ190Zl94bG0ucHk=) | `76.10% <27.47%> (-14.30%)` | :arrow_down: |
| [src/transformers/modeling\_tf\_xlnet.py](https://codecov.io/gh/huggingface/transformers/pull/4530/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9tb2RlbGluZ190Zl94bG5ldC5weQ==) | `80.53% <27.50%> (-9.80%)` | :arrow_down: |
| [src/transformers/modeling\_tf\_distilbert.py](https://codecov.io/gh/huggingface/transformers/pull/4530/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9tb2RlbGluZ190Zl9kaXN0aWxiZXJ0LnB5) | `82.88% <32.00%> (-12.24%)` | :arrow_down: |
| [src/transformers/modeling\_tf\_roberta.py](https://codecov.io/gh/huggingface/transformers/pull/4530/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9tb2RlbGluZ190Zl9yb2JlcnRhLnB5) | `74.74% <34.21%> (-25.26%)` | :arrow_down: |
| [src/transformers/modeling\_tf\_electra.py](https://codecov.io/gh/huggingface/transformers/pull/4530/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9tb2RlbGluZ190Zl9lbGVjdHJhLnB5) | `91.17% <38.70%> (-7.89%)` | :arrow_down: |
| [src/transformers/modeling\_tf\_albert.py](https://codecov.io/gh/huggingface/transformers/pull/4530/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9tb2RlbGluZ190Zl9hbGJlcnQucHk=) | `75.39% <45.45%> (-3.30%)` | :arrow_down: |
| [src/transformers/modeling\_tf\_utils.py](https://codecov.io/gh/huggingface/transformers/pull/4530/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9tb2RlbGluZ190Zl91dGlscy5weQ==) | `87.20% <50.00%> (-1.60%)` | :arrow_down: |
| ... and [22 more](https://codecov.io/gh/huggingface/transformers/pull/4530/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/huggingface/transformers/pull/4530?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/huggingface/transformers/pull/4530?src=pr&el=footer). Last update [d976ef2...5b456e2](https://codecov.io/gh/huggingface/transformers/pull/4530?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Some commits are missing... I think it is due to the high number of error rate from Github.

Thanks @LysandreJik for your constructive comments!

For the second point, before to answer in order to be sure, you mean that it would be more convenient that the output of the `call(...)` methods in the TF tasks model returns the same tuple `(loss), logits, (hidden_states), (attentions)` than the `forward(...)` methods in PT tasks model? 

Yes, that's what I mean. I think having this to be the same as the PyTorch API would make sense. It wouldn't be a breaking change either, as it would require the `labels` to be passed to the model.

I think doing this could still leverage Mixins, by calling a `self._compute_loss` or `self.compute_loss` if we want to expose this method as well. I have no strong opinion on that last item.

Ok, indeed makes sense and I don't think it is a problem to do that way, I will work on this today to see if there is any issue that would not allow us to do that.

I agree with @LysandreJik's 2nd point – maybe we can even take advantage of this to implement named tuples for TF models output, like @thomwolf and @patrickvonplaten intend to do for PyTorch (as it's going to be a breaking change in TF models anyways, maybe we can do this at the same time?)

Since my last commit, now the TF models return the loss such as the PT ones if the labels are given. 

About the named tuples, looks to be a good idea indeed, but I think we should implement this in another PR in order to release this in same time than for PT. No?

> About the named tuples [...] we should implement this in another PR in order to release this in same time than for PT. No?

Yes, makes sense!

Ok, looks good to me, I have tested the new models with different examples that use the trainer and they all work, tests looks to be ok as well except the quality one that I don't know how to fix :smile: 

A more general question regarding training in TensorFlow (I'm not super familiar with TF 2.0 training, so I'm asking primarily to learn a bit :-) ): 
I remember that when TF 2.0 was not out, most people used Keras to train a model with 
`model.fit(x_train, y_train)` => is this still the case?
or are people more and more switching to the TF 2.0 training style as shown here: https://www.tensorflow.org/tutorials/quickstart/advanced and which basically consists of using 
`optimizer.apply_gradients(zip(gradients, model.trainable_variables))`. This is also what we do in the TF trainer right? 

Was it possible and recommended to train transformer models with keras' `model.train()` before TF Trainer and is it still possible now?

This is a good question! Short answer: yes it is still possible but witthout any gradient accumulation, that's mostly why the trainer uses the advanced training of TensorFlow.

I'm currently preparing a next PR that will integrate the new `Model.train_step` feature added in [TF 2.2](https://github.com/tensorflow/tensorflow/releases/tag/v2.2.0). Basically this update allows you to create your own train step, and then integrate the missing gradient accumulation but this new PR will be only for TF >= 2.2.

@patrickvonplaten It was possible and we definitely aim to keep compatibility with keras' `fit` method. We don't have many tutorials that cover it, though, having some would probably make it easier for new users coming from Keras to use our lib.

@julien-c, we've had the offline approval from @thomwolf, feel free to merge when you want. Glad to welcome this in the library!

Just tweaked the training_args.logging_dir to keep the same default as pytorch (I like that it creates a new subfolder each time you relaunch a training)

Great job @jplu, thank you 💪