Bug with max_seq_length argument in training scripts
Author: novice03
Pull request: no

Hello, I am trying to train Nystromformer for Masked Language Modeling with a sequence length of 1024 using the [run_mlm.py](https://github.com/huggingface/transformers/blob/master/examples/pytorch/language-modeling/run_mlm.py) script. As, I understand it, the `max_seq_length` argument of the script controls the maximum number of input tokens passed to the model. And this should be, less than or equal to the model's `max_position_embeddings`. So, I set `--max_seq_length 1024`, however I get: 

`01/17/2022 08:58:11 - WARNING - __main__ - The max_seq_length passed (1024) is larger than the maximum length for themodel (512). Using max_seq_length=512.`

This implies that sequences greater than 512 tokens are not being passed to the model, although the model can handle sequences of upto 1024 tokens (I double-checked that the model has  `max_position_embeddings=1024`).

### To reproduce

1. Clone the transformers repository
2. In the examples/pytorch/language-modeling directory, run `python run_mlm.py --model_type nystromformer --dataset_name wikitext --dataset_config_name wikitext-2-raw-v1 --per_device_train_batch_size 6 --num_train_epochs 1 --do_train --do_eval --tokenizer_name albert-base-v1 --output_dir ./nystrom --max_seq_length 1024 --config_overrides="max_position_embeddings=1024"`

### Potential fix

The `max_seq_length` variable in the script controls the size of the input sequences. [Here](https://github.com/huggingface/transformers/blob/9a2dabae7002258e41419491c73dd43ad61b5de7/examples/pytorch/language-modeling/run_mlm.py#L385), it is set to `min(data_args.max_seq_length, tokenizer.model_max_length)`. However, this will always evaluate to `tokenizer.model_max_length` (if `max_seq_length` is greater than the tokenizer's default `model_max_length`). So, although `--max_seq_length 1024` is passed to the script, the input sequences are truncated to 512 (the default `model_max_length` for tokenizers such as BERT, RoBERTa and ALBERT). 

We could fix this by replacing `tokenizer.model_max_length` with `config.max_position_embeddings` in this [block](https://github.com/huggingface/transformers/blob/0167edc8549b8c8b01fcc669d0da7ea41903b244/examples/pytorch/language-modeling/run_mlm.py#L379-L385). This way, the user is warned if the model cannot support the provided `max_seq_length`, and the sequences are truncated to `min(data_args.max_seq_length, config.max_position_embeddings)`.

I've implemented this change in the pull request below. 

### Who can help
@NielsRogge @LysandreJik @sgugger 


Comments:
No, the tokenizer used should be manually fixed if its maximum length is incorrect, but we shouldn't change that part of the script. This is not a bug, but a desired feature, as the corresponding pretrained model will not work with a greater maximum length.

You can always change the example script (which are just examples) if you want a different behavior.

I would argue that it would be beneficial (especially for training models from scratch) if `max_seq_length` is set to `min(data_args.max_seq_length, config.max_position_embeddings)`. Consider two cases:

1. The script is being used with a pre-trained model. As an example, consider a pre-trained Nystromformer model with maximum sequence length of 512. Here, I must truncate the inputs to 512 (since this is the maximum length handled by the model). Even if I pass `--max_seq_length 1024` with a pre-trained model with maximum length 512, the modified script will warn the user and set `max_seq_length = min(data_args.max_seq_length, config.max_position_embeddings)`. In this case, `max_seq_length` is set to `config.max_position_embeddings` (512), which is correct.
2. On the other hand, if I wish to train a model from scratch, I would want the tokenizer to produce sequences that are not greater than the model's `max_position_embeddings`. For example, if I want to train Nystromformer for a maximum length of 1024 (which is what I was trying to do originally) from scratch, I would also want the tokenizer to generate input sequences that are at most 1024 in length. However, with the current training script, I am unable to do so. The current script sets the maximum length of the tokenizer to `min(data_args.max_seq_length, tokenizer.model_max_length)`. In this case, it is set to 512, although I want to train my model on sequences of length 1024. The current script does not allow the tokenizer to produce large input sequences even if the model is compatible with them. In this case, if we `max_seq_length` is set to `min(data_args.max_seq_length, config.max_position_embeddings)`, the tokenizer will instead truncate the input sequences to the maximum length allowed by the model (1024).