Fix tot update in trainer
Author: efsotr
Pull request: yes

# What does this PR do?

Fixed incorrect calculation of update steps per epoch and micro steps in the last step of an epoch when gradient_accumulation > 1, which was introduced by #34198.

Added tests to ensure that the number of micro batches during training matches the number of micro batches provided by the dataloader.

Fixes #37777 


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [x] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [x] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@ArthurZucker @muellerzr @SunMarc @Rocketknight1

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker
- vision models: @amyeroberts, @qubvel
- speech models: @eustlb
- graph models: @clefourrier

Library:

- flax: @gante and @Rocketknight1
- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Rocketknight1
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @zach-huggingface and @SunMarc
- chat templates: @Rocketknight1

Integrations:

- deepspeed: HF Trainer/Accelerate: @SunMarc @zach-huggingface
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc @MekkCyber

Documentation: @stevhliu

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @Rocketknight1
- PyTorch: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


Comments:
Hi 👋, thank you for opening this pull request! The pull request is converted to draft by default. The CI will be paused while the PR is in draft mode. When it is ready for review, please click the `Ready for review` button (at the bottom of the PR page). This will assign reviewers and trigger CI.

cc @sunmarc!

The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_37923). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

simulated code
```python
import math
def ceil_div(a, b):
    return int(math.ceil(a / b))

num_gpus = 2
num_train_epochs = 1
train_len = 123
per_device_train_batch_size = 4
gradient_accumulation_steps = 5

num_examples = train_len


def old_code():
    remainder = num_examples % gradient_accumulation_steps
    if remainder == 0:
        remainder = gradient_accumulation_steps
    total_updates = steps_in_epoch // gradient_accumulation_steps + 1
    if gradient_accumulation_steps == 1:
        total_updates -= 1
    return total_updates, remainder

def new_code():
    remainder = steps_in_epoch % gradient_accumulation_steps
    if remainder == 0:
        remainder = gradient_accumulation_steps
    total_updates = steps_in_epoch // gradient_accumulation_steps + int(
        remainder < gradient_accumulation_steps
    )    
    return total_updates, remainder

train_len = 123

for i in [123, 120]:
    train_len = i
    steps_in_epoch = ceil_div(ceil_div(train_len, per_device_train_batch_size), num_gpus)
    print(f"train_len {train_len}, per_device_train_batch_size {per_device_train_batch_size}, gradient_accumulation_steps {gradient_accumulation_steps}, steps_in_epoch {steps_in_epoch}")
    total_updates, remainder = old_code()
    print(f"old code, total_updates: {total_updates}, remainder: {remainder}, steps_in_epoch: {(total_updates - 1) * gradient_accumulation_steps + remainder}")
    total_updates, remainder = new_code()
    print(f"new code, total_updates: {total_updates}, remainder: {remainder}, steps_in_epoch: {(total_updates - 1) * gradient_accumulation_steps + remainder}")
    print()
```

stdout
```log
train_len 123, per_device_train_batch_size 4, gradient_accumulation_steps 5, steps_in_epoch 16
old code, total_updates: 4, remainder: 3, steps_in_epoch: 18
new code, total_updates: 4, remainder: 1, steps_in_epoch: 16

train_len 120, per_device_train_batch_size 4, gradient_accumulation_steps 5, steps_in_epoch 15
old code, total_updates: 4, remainder: 3, steps_in_epoch: 18
new code, total_updates: 3, remainder: 5, steps_in_epoch: 15
```