[Generation] Fix max_new_tokens
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 #13880

@Narsil - IMO we can deprecate the MaxNewTokensStoppingCriteria - don't really see why one would use this instead of MaxLengthsStoppingCriteria no?


## 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 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 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:

- 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:
@patil-suraj - it would be great if you could do a second review and if you agree with the current code, feel free to merge. If you change your opinion having read @Narsil's comments - then let's maybe have a quick call about it all three of us

Documenting a longer internal discussion we had about the PR here: 

> Ok, I've thought about it again a bit - and I really think the following is the right approach:
> Have `max_length` and `max_new_tokens` in generate(...) . Think we all agree that this is the correct way to go and that we should document the difference well (maybe also add some examples with `max_new_tokens` ).
> Only have `MaxLengthCriteria` and deprecate MaxNewTokensCriteria  -> this decision is not as straight forward and has the following pro's and con's where IMO the pro's are more important:
>       **Pro's:**
> -  Less error prone - stopping criteria cannot interfere with each other in sample , greedy , ... we prevent weird use cases that would currently break beam scorer's length penalty etc...
> -  More readable code & easier to understand code. The objective of sample  , greedy was to be very readable.
> - MaxNewTokensCriteria is not very useful from a user-experience - a user has to re-instantiate MaxNewTokensCriteria at every call of sample or `generate()` . It's more natural to define stopping criteria's and logits processors once and then leave them as is when calling` sample(...)` . I think almost all logits processors and stopping criteria are not dependent from a input variable at init - MaxNewTokensCriteria is. E.g., writing:
```python
for input_ids in list:
      stop_criteria = MaxNewTokensCriteria(20, input_ids.shape[-1])
      model.sample(input_ids, stop_criteria)
```
>              is in general not very clean IMO
> It's difficult to correctly initialize MaxNewTokensCriteria in an encoder-decoder setting. Here people might also just do `stop_criteria = MaxNewTokensCriteria(20, input_ids.shape[-1])` which would be wrong as the current init_length would be 1 (the BOS token the decoder starts with)
>        **Con's:**
> - Just having `MaxLengthCriteria` might lead to silent errors as people think it's the max number of generated tokens and not the total number of tokens. This is a valid concern and we already know that this is the case for people using generate(...) . A remedy here is that 99% of people don't manually pass StoppingCriteria but will just use generate(...) . Also IMO this problem can be resolved by providing good docs.
> - We have max_new_tokens but no `MaxNewTokensCriteria` . Don't think that this is a strong argument though as we have many generate inputs that have no corresponding logits processor, e.g. length_penalty
> => So I'll merge the PR now as is and document the discussion we had here