`max_steps` would not override `num_train_epochs` when training with IterableDataset
Author: taoky
Pull request: no

## 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).

Comments:
Thanks for reporting! Should be fixed by the PR mentioned above.

Hi, @sgugger I took a look at the issue you mentioned above and saw that [this PR](https://github.com/huggingface/transformers/pull/14413) disables the ability to loop over the iterable dataset in an infinite fashion (for example, how it is done in the [CodeParrot example](https://github.com/huggingface/transformers/blob/master/examples/research_projects/codeparrot/scripts/codeparrot_training.py)). I am not able to have my iterable dataset "restart" when it is empty. Was that intended? 

To loop several times on the iterable dataset, you have to use `num_epochs` instead of `max_steps`.

Hmm ok, however, if I set `max_steps=-1` I get the error:
```
ValueError: train_dataset does not implement __len__, max_steps has to be specified
```

But if I set max steps, then the value of `num_train_epochs` gets ignored and the training stops when the dataset has run out of samples.

Is there a way to set the length inside of the dataset but still load in streaming mode?

You can just implement the length of the dataset if you have it.

Oh ok, would it be easiest to way to force add the length to the dataset? Seems from looking at the code that it would potentially be easiest to subclass the `IterableDataset` and add in the length. But it seems somewhat unclear to me still if that is better than just using a torch iterable dataset. 




> Hmm ok, however, if I set `max_steps=-1` I get the error:
> 
> ```
> ValueError: train_dataset does not implement __len__, max_steps has to be specified
> ```
> 
> But if I set max steps, then the value of `num_train_epochs` gets ignored and the training stops when the dataset has run out of samples.
> 
> Is there a way to set the length inside of the dataset but still load in streaming mode?

hi @gabeorlanski can you solve this problem. if you hava reslove this problem by IterableDataset ,could you give me a sample