Repository: huggingface/transformers

Issue:
`max_steps` would not override `num_train_epochs` when training with IterableDataset
## Environment info

- `transformers` version: 4.8.2
- Platform: Linux-5.4.0-1047-azure-x86_64-with-glibc2.10
- Python version: 3.8.10
- PyTorch version (GPU?): 1.9.0+cu102 (True)
- Tensorflow version (GPU?): not installed (NA)
- Flax version (CPU?/GPU?/TPU?): not installed (NA)
- Jax version: not installed
- JaxLib version: not installed
- Using GPU in script?: N/A
- Using distributed or parallel set-up in script?: No

### Who can help

Library/trainer & Documentation: @sgugger

## Information

Model I am using (Bert, XLNet ...): N/A

The problem arises when using:
* [ ] the official example scripts: (give details below)
* [x] my own modified scripts: (give details below)

The tasks I am working on is:
* [ ] an official GLUE/SQUaD task: (give the name)
* [x] my own task or dataset: (give details below)

In [the documentation of Trainer](https://huggingface.co/transformers/_modules/transformers/trainer.html), it says that the `max_steps` argument in `transformers.TrainingArguments`:

> If set to a positive number, the total number of training steps to perform. Overrides `num_train_epochs`.

However, the override is not true when the dataset is an `IterableDataset`. In 

<https://github.com/huggingface/transformers/blob/2df63282e010ac518683252d8ddba21e58d2faf3/src/transformers/trainer.py#L1091-L1111>

when the dataset is not an instance of `collections.abc.Sized` (aka, it does not implement `__len__()`), the `num_train_epochs` is independent with `max_steps`.

And the default value of `num_train_epochs` is set to 3.0:

<https://github.com/huggingface/transformers/blob/2df63282e010ac518683252d8ddba21e58d2faf3/src/transformers/training_args.py#L410>

It brings unexpected behavior when training models with iterable dataset and `num_train_epochs` not set (model is only trained for 3 epochs). I hope it could be clarified in documentation.

## To reproduce

Steps to reproduce the behavior:

1. Use an `IterableDatset` as trainset. We assume that it has 1024 items and use 128 as batch size.
2. Set `max_steps` to 80 (= 1024 / 128 * 10) and do not set `num_train_epochs` when instancing `TrainingArguments`, and then set trainer. 
3. `trainer.train()`

<!-- 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.-->

MWE:

```python
from transformers import Trainer, TrainingArguments, AutoModelForSequenceClassification
import torch

class Dataset(torch.utils.data.IterableDataset):
    def __init__(self):
      super(Dataset).__init__()

    def __iter__(self):
      for i in range(1024):
        yield {
          'labels': [1],
          'input_ids': [100, 200, 300, 400]
        }

def main():
  epochs = 10
  batch = 128
  data_line_count = 1024
  steps = int(data_line_count / batch * epochs)

  model = AutoModelForSequenceClassification.from_pretrained("bert-base-cased-finetuned-mrpc")

  dataset = Dataset()
  training_args = TrainingArguments(
    output_dir='/tmp',          # output directory
    max_steps=steps,
    per_device_train_batch_size=batch,  # batch size per device during training
    logging_dir='./logs',            # directory for storing logs
  )
  trainer = Trainer(
    args=training_args,
    model=model,
    train_dataset=dataset,
  )
  trainer.train()

if __name__ == "__main__":
  main()
```

## Expected behavior

<!-- A clear and concise description of what you would expect to happen. -->
Expected that this model is trained for 10 epochs (80 steps), but it actually been trained for 3 epochs (24 steps).


Discussion:

Resolving patch:
diff --git a/src/transformers/trainer.py b/src/transformers/trainer.py
--- a/src/transformers/trainer.py
+++ b/src/transformers/trainer.py
@@ -1110,7 +1110,8 @@ def train(
         else:
             # see __init__. max_steps is set when the dataset has no __len__
             max_steps = args.max_steps
-            num_train_epochs = int(args.num_train_epochs)
+            # Setting a very large number of epochs so we go as many times as necessary over the iterator.
+            num_train_epochs = sys.maxsize
             num_update_steps_per_epoch = max_steps
             num_train_samples = args.max_steps * total_train_batch_size
 


Test patch: