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

[Trainer] Add nan/inf logging filter #13619

Conversation

patrickvonplaten
Copy link
Contributor

@patrickvonplaten patrickvonplaten commented Sep 17, 2021

What does this PR do?

Some losses sometimes produce inf losses which however doesn't necessarily mean that the training went bad. When using the CTC loss for speech recognition - see: #13620 this is often the case. The problem is that as soon as one single loss step is inf or nan the rest of the training logs will display inf or nan. In this PR a flag is added that allows the user to filter out nan and inf values for training. It defaults to False and will be set to True in all CTC training scripts.

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?

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.

Copy link
Collaborator

@sgugger sgugger left a comment

Choose a reason for hiding this comment

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

Thanks for adding this! I wonder if the default of the new feature shouldn't be True? It's a little bi breaking, but only for the logging, not the actual training, and it makes more sense to ignore thoses NaN/infs (which are ignored by default by the optimizer since there is some gradient clipping).

Comment on lines 475 to 476
"Filter nan and inf losses for logging. "
"Note this flag only affects the logging output and not the optimization step."
Copy link
Collaborator

Choose a reason for hiding this comment

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

No need to be this long here, but there should be a proper documentation above that can be longer!

@patrickvonplaten
Copy link
Contributor Author

Happy to let it default to True!

@@ -468,6 +478,7 @@ class TrainingArguments:
)
logging_first_step: bool = field(default=False, metadata={"help": "Log the first global_step"})
logging_steps: int = field(default=500, metadata={"help": "Log every X updates steps."})
logging_nan_inf_filter: str = field(default=True, metadata={"help": "Filter nan and inf losses for logging."})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The default is now True

@patrickvonplaten patrickvonplaten merged commit 1f9dcfc into huggingface:master Sep 17, 2021
@patrickvonplaten patrickvonplaten deleted the add_inf_nan_filter_to_trainer branch September 17, 2021 14:22
Albertobegue pushed a commit to Albertobegue/transformers that referenced this pull request Jan 13, 2022
* finish

* add test

* push

* remove unnecessary code

* up

* correct test

* Update src/transformers/training_args.py
Albertobegue pushed a commit to Albertobegue/transformers that referenced this pull request Jan 27, 2022
* finish

* add test

* push

* remove unnecessary code

* up

* correct test

* Update src/transformers/training_args.py
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.

2 participants