[Generation] better error message
Author: patrickvonplaten
Pull request: yes

If `cur_len` of input context is as long or longer than `max_length` a nice error message should be shown.

Comments:
# [Codecov](https://codecov.io/gh/huggingface/transformers/pull/5497?src=pr&el=h1) Report
> Merging [#5497](https://codecov.io/gh/huggingface/transformers/pull/5497?src=pr&el=desc) into [master](https://codecov.io/gh/huggingface/transformers/commit/49281ac9390e19f30c30a914b11aa55b561973d1&el=desc) will **decrease** coverage by `0.23%`.
> The diff coverage is `100.00%`.

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

```diff
@@            Coverage Diff             @@
##           master    #5497      +/-   ##
==========================================
- Coverage   77.82%   77.59%   -0.24%     
==========================================
  Files         141      141              
  Lines       24617    24619       +2     
==========================================
- Hits        19159    19103      -56     
- Misses       5458     5516      +58     
```


| [Impacted Files](https://codecov.io/gh/huggingface/transformers/pull/5497?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/transformers/generation\_tf\_utils.py](https://codecov.io/gh/huggingface/transformers/pull/5497/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9nZW5lcmF0aW9uX3RmX3V0aWxzLnB5) | `84.71% <100.00%> (-1.47%)` | :arrow_down: |
| [src/transformers/generation\_utils.py](https://codecov.io/gh/huggingface/transformers/pull/5497/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9nZW5lcmF0aW9uX3V0aWxzLnB5) | `96.82% <100.00%> (+<0.01%)` | :arrow_up: |
| [src/transformers/tokenization\_xlm\_roberta.py](https://codecov.io/gh/huggingface/transformers/pull/5497/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy90b2tlbml6YXRpb25feGxtX3JvYmVydGEucHk=) | `61.90% <0.00%> (-33.34%)` | :arrow_down: |
| [src/transformers/modeling\_tf\_gpt2.py](https://codecov.io/gh/huggingface/transformers/pull/5497/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9tb2RlbGluZ190Zl9ncHQyLnB5) | `65.69% <0.00%> (-29.45%)` | :arrow_down: |
| [src/transformers/tokenization\_transfo\_xl.py](https://codecov.io/gh/huggingface/transformers/pull/5497/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy90b2tlbml6YXRpb25fdHJhbnNmb194bC5weQ==) | `33.56% <0.00%> (-8.93%)` | :arrow_down: |
| [src/transformers/modeling\_tf\_utils.py](https://codecov.io/gh/huggingface/transformers/pull/5497/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9tb2RlbGluZ190Zl91dGlscy5weQ==) | `84.01% <0.00%> (-5.11%)` | :arrow_down: |
| [src/transformers/tokenization\_auto.py](https://codecov.io/gh/huggingface/transformers/pull/5497/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy90b2tlbml6YXRpb25fYXV0by5weQ==) | `95.55% <0.00%> (-2.23%)` | :arrow_down: |
| [src/transformers/tokenization\_utils\_base.py](https://codecov.io/gh/huggingface/transformers/pull/5497/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy90b2tlbml6YXRpb25fdXRpbHNfYmFzZS5weQ==) | `92.89% <0.00%> (-1.33%)` | :arrow_down: |
| [src/transformers/modeling\_tf\_pytorch\_utils.py](https://codecov.io/gh/huggingface/transformers/pull/5497/diff?src=pr&el=tree#diff-c3JjL3RyYW5zZm9ybWVycy9tb2RlbGluZ190Zl9weXRvcmNoX3V0aWxzLnB5) | `88.05% <0.00%> (-1.26%)` | :arrow_down: |
| ... and [2 more](https://codecov.io/gh/huggingface/transformers/pull/5497/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/huggingface/transformers/pull/5497?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/5497?src=pr&el=footer). Last update [49281ac...3e5929c](https://codecov.io/gh/huggingface/transformers/pull/5497?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Should be trivial to merge this IMO. Is there any case why one would now receive an error if `max_length` < `cur_len` @sshleifer @yjernite ?

Is max_length still meant to include the length of input_ids?

yes! Would you change it to be added to the input?

I don't understand your response exactly. My preference is that `max_length` is completely independent of the ids sent to the encoder. That is how the current code works. For example, if we are summarizing a news article of length `X`, and max_length is 20, we should be allowed to generate 20 tokens regardless of `X`. 
For the text generation use case, I care less, but I have the same opinion. `X.shape[1]` should not matter.
I don't have an opinion on whether `decoder_start_token_id` counts.
When does this assert get hit?

The assert hits at the moment only for text-generation of "encoder" only models (GPT2, XLNET, ...) if  `input_ids.shape[1] >= max_length`. 
In the case of all "conditional" generation (using an encoder + decoder, like Bart / T5) `max_length` is independent of the input to the encoder because auto-regressive generation is only done for the decoder => which is expected.

The question is whether for text generation of encoder only models like gpt2 `max_length` should be changed to something like `max_tokens_to_generate` in which case the limit would be `input_ids.shape[1] + max_tokens_to_generate` (independent of `input_ids`). Not sure whether it's more intuitive to define the number of max length tokens **to be generated** or better the max length of the complete text, also given the name `max_length`. 

I think `max_length` should refer to the maximum number of tokens that can be generated.
For example, if you wanted to make a next word suggester, it would be much simpler to have max_length=1, than it would be check how many input_ids != pad_token_id for each entry in your batch and then add 1 to that.

I don't think there are as many use cases where you need N tokens regardless of the length of the input.