Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Workaround for #27758 to avoid ZeroDivisionError #28756

Merged

Conversation

tleyden
Copy link
Contributor

@tleyden tleyden commented Jan 29, 2024

What does this PR do?

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.

Fixes #27758

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? 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, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

@ArthurZucker @pacman100

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks for opening the PR 🤗

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just missing a small test imo to make sure this does not break anything! 🤗

@tleyden
Copy link
Contributor Author

tleyden commented Jan 30, 2024

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

@ArthurZucker
Copy link
Collaborator

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

@huggingface huggingface deleted a comment from github-actions bot Feb 29, 2024
@ArthurZucker
Copy link
Collaborator

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

@tleyden
Copy link
Contributor Author

tleyden commented Feb 29, 2024 via email

@tleyden tleyden force-pushed the issue_27758_zero_divide_error_take_2 branch from 5fd8ca8 to 0f6a59f Compare February 29, 2024 11:43
@tleyden
Copy link
Contributor Author

tleyden commented Feb 29, 2024

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

@tleyden
Copy link
Contributor Author

tleyden commented Mar 1, 2024

@ArthurZucker @pacman100 PTAL at approving workflow

Copy link
Contributor

@pacman100 pacman100 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @tleyden for the fix!

@tleyden
Copy link
Contributor Author

tleyden commented Mar 1, 2024

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

@tleyden
Copy link
Contributor Author

tleyden commented Mar 1, 2024

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

@ArthurZucker ArthurZucker merged commit c38a122 into huggingface:main Mar 4, 2024
20 checks passed
@ArthurZucker
Copy link
Collaborator

Sorry for the delay just merged!

damithsenanayake pushed a commit to damithsenanayake/transformers that referenced this pull request Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ZeroDivisionError when training on a single batch of data
4 participants