Workaround for #27758 to avoid ZeroDivisionError
Author: tleyden
Pull request: yes

# What does this PR do?

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

It can speed up devloops to test with very small datasets which end up being a single batch.  However, that can trigger the error described in #27758.

This PR works around it by changing the division by zero to division by a very small number.  The loss metric will already be meaningless if `self.state.global_step == 0`.  This PR won't change that, however it will prevent the unhelpful `ZeroDivisionError`

I have not written any tests yet, but would be happy to if the reviewers agree with the overall approach.

<!-- Remove if not applicable -->

Fixes #27758


## Before submitting
- [x] 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#start-contributing-pull-requests),
      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.
- [x] 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).
- [ ] Did you write any new necessary tests?


## Who can review?

@ArthurZucker @pacman100 

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

@ArthurZucker sounds good, I'll look into adding a test.  If you know of any existing tests that I should look at, LMK!

this one seems relevant https://github.com/huggingface/transformers/blob/main/tests/trainer/test_trainer.py#L835

Hey! If you can rebase we can merge as is it's alright

Yes will do

On Thu, Feb 29, 2024 at 11:12 AM Arthur ***@***.***> wrote:

> Hey! If you can rebase we can merge as is it's alright
>
> —
> Reply to this email directly, view it on GitHub
> <https://github.com/huggingface/transformers/pull/28756#issuecomment-1970790622>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AACIPLEOAPOMV3TF3GUSWHLYV37J5AVCNFSM6AAAAABCPO6Z5KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSNZQG44TANRSGI>
> .
> You are receiving this because you authored the thread.Message ID:
> ***@***.***>
>


@ArthurZucker I just rebased - can you approve the workflow?

@ArthurZucker @pacman100 PTAL at approving workflow

Sure np @pacman100, but I would call it more of a workaround.  At some point it's worth finding out why `self.state.global_step` can end up as `0` in the first place

@pacman100 The github merge automation seems to be stuck .. or does it just take a while to kick in?

Sorry for the delay just merged!