Repository: huggingface/transformers

Issue:
ZeroDivisionError in NotebookProgressBar.update with small dataset
I don't know the specifics, but during training (details below) NotebookProgressBar's update function got called with `force_update` is true, while no progress had been made (i.e. `value == self.start_value`). This leads directly to a ZeroDivisionError on line 151 in src/transformers/util/notebook.py.

## Environment info
- `transformers` version: 4.6.1
- Platform: Linux-5.4.0-1051-azure-x86_64-with-glibc2.17
- Python version: 3.8.10
- PyTorch version (GPU?): 1.8.1+cu102 (False)
- Tensorflow version (GPU?): not installed (NA)
- Using GPU in script?: yes
- Using distributed or parallel set-up in script?: no

### Who can help
@sgugger based on Git blame.

## Information
Model I am using (Bert, XLNet ...): Roberta-Base

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)

## To reproduce
Managed to reproduce based on the token classification notebook ([here](https://github.com/huggingface/notebooks/blob/master/examples/token_classification.ipynb)).
Steps to reproduce the behavior from this notebook:
After loading the dataset, apply the following code to make small:
```python
n = 14
datasets["train"] = datasets["train"].filter(lambda x: int(x["id"]) < n)
datasets["validation"] = datasets["validation"].filter(lambda x: int(x["id"]) < n)
datasets["test"] = datasets["test"].filter(lambda x: int(x["id"]) < n)
```

Replace the TrainingArguments in "Fine-tuning the model" with:
```python
model_name = model_checkpoint.split("/")[-1]
args = TrainingArguments(
    output_dir='./deletepls',
    num_train_epochs=1,
    per_device_train_batch_size=8,
    per_device_eval_batch_size=4,
    warmup_steps=500,
    weight_decay=0.01,
    learning_rate=5e-4,
    logging_dir='./logs',
    logging_steps=7,
)
```
After executing `trainer.train()` I get the ZeroDivisionError.

## Expected behavior
Some more descriptive error related to my logging steps, dataset size, or batch size. I'm still not sure what exactly causes this error.



Discussion:

Resolving patch:
diff --git a/src/transformers/utils/notebook.py b/src/transformers/utils/notebook.py
--- a/src/transformers/utils/notebook.py
+++ b/src/transformers/utils/notebook.py
@@ -148,18 +148,25 @@ def update(self, value: int, force_update: bool = False, comment: str = None):
                 self.first_calls -= 1
             current_time = time.time()
             self.elapsed_time = current_time - self.start_time
-            self.average_time_per_item = self.elapsed_time / (value - self.start_value)
+            # We could have value = self.start_value if the update is called twixe with the same start value.
+            if value > self.start_value:
+                self.average_time_per_item = self.elapsed_time / (value - self.start_value)
+            else:
+                self.average_time_per_item = None
             if value >= self.total:
                 value = self.total
                 self.predicted_remaining = None
                 if not self.leave:
                     self.close()
-            else:
+            elif self.average_time_per_item is not None:
                 self.predicted_remaining = self.average_time_per_item * (self.total - value)
             self.update_bar(value)
             self.last_value = value
             self.last_time = current_time
-            self.wait_for = max(int(self.update_every / self.average_time_per_item), 1)
+            if self.average_time_per_item is None:
+                self.wait_for = 1
+            else:
+                self.wait_for = max(int(self.update_every / self.average_time_per_item), 1)
 
     def update_bar(self, value, comment=None):
         spaced_value = " " * (len(str(self.total)) - len(str(value))) + str(value)


Test patch: