`compute_metrics` show better results than `generate` because target data leaks
Author: pperle
Pull request: no

## Environment info
<!-- You can run the command `transformers-cli env` and copy-and-paste its output below.
     Don't forget to fill out the missing fields in that output! -->

- `transformers` version: 4.3.2
- Platform: Linux-5.8.18-050818-generic-x86_64-with-glibc2.10
- Python version: 3.8.5
- PyTorch version (GPU?): 1.7.1 (True)
- Tensorflow version (GPU?): not installed (NA)
- Using GPU in script?: yes
- Using distributed or parallel set-up in script?: no

### Who can help
<!-- Your issue 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

 -->

## Information

Model I am using:
- t5

The problem arises when using:
- my own modified scripts: (give details below)

The tasks I am working on is:
- my own task or dataset: (give details below)

## To reproduce

Steps to reproduce the behavior:

1. train model with `compute_metrics` function to monitor metrics
2. use `generate` to predict text with trained model

<!-- If you have code snippets, error messages, stack traces please provide them here as well.
     Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting
     Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code.-->

## Expected behavior

I expect the metrics of `compute_metrics` to be equal to my generated text.

<!-- A clear and concise description of what you would expect to happen. -->

## More information

While training, I used `compute_metrics` to calculate the metric on my validation set every X steps. I was surprised to see that after training my model did not perform as expected using the `generate` function provided by huggingface.

After some digging through the code I think I understand what the problem is. [`compute_metrics`](https://github.com/huggingface/transformers/blob/cd48078ce59a195473729759c76d88ae612b0f7a/src/transformers/trainer.py#L1665) takes as input `preds`, which is [a collection of `logits`](https://github.com/huggingface/transformers/blob/cd48078ce59a195473729759c76d88ae612b0f7a/src/transformers/trainer.py#L1635) from [`prediction_step`](https://github.com/huggingface/transformers/blob/cd48078ce59a195473729759c76d88ae612b0f7a/src/transformers/trainer.py#L1630) which internally [calls `model` with the inputs and targets of the model](https://github.com/huggingface/transformers/blob/cd48078ce59a195473729759c76d88ae612b0f7a/src/transformers/trainer.py#L1733).
This means that the target text leaks into `preds.predictions` because `mode.forward` used the targets as [input for the decoder](https://github.com/huggingface/transformers/blob/1cd16512dc8060aa8c2419664f9cb83813ade4d5/src/transformers/models/t5/modeling_t5.py#L1331). This makes the metrics of `compute_metrics` seem much better than they really are.

In my opinion the target data should not be used to create `preds.predictions`. Maybe the `generate` function is a better fit.


Comments:
Pinging @patrickvonplaten and @sgugger 

Did you use the flag `--predict_with_generate`? It's there just for this: predicting using the `generate` method and the labels are then not passed (except to compute the loss).

Thank you for the hint. I followed this tutorial [this example](https://huggingface.co/transformers/custom_datasets.html#fine-tuning-with-trainer) and used [`TrainingArguments`](https://huggingface.co/transformers/main_classes/trainer.html#trainingarguments), which to not have the `predict_with_generate` option, instead of [`Seq2SeqTrainingArguments`](https://huggingface.co/transformers/main_classes/trainer.html#transformers.Seq2SeqTrainingArguments).

Maybe it's just me, but I think the `predict_with_generate` option should be described more visibly. I found it only in [`Seq2SeqTrainingArguments`](https://huggingface.co/transformers/main_classes/trainer.html#transformers.Seq2SeqTrainingArguments) and in none of the examples. Also, none of the examples in the documentation use `Seq2SeqTrainingArguments`.

If you don't think the documentation should be updated you can close this issue since my confusion has been resolved. Thank you.


Yes the documentation is missing a seq2seq example. This is because we have been working on the design recently. The most up-to-date example you should use as reference is the [run_seq2seq script](https://github.com/huggingface/transformers/blob/master/examples/seq2seq/run_seq2seq.py).

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/master/CONTRIBUTING.md) are likely to be ignored.