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

Fix example logs repeating themselves #16669

Merged
merged 5 commits into from
Apr 11, 2022
Merged

Conversation

muellerzr
Copy link
Contributor

@muellerzr muellerzr commented Apr 8, 2022

Fix Loggers repeating themselves during the Examples tests

What does this add?

This PR refactores slighly how the stream_handler is made and passed to logger during all of the examples tests

Why is it needed?

When running the no_trainer tests, I was noticing that after each test was called the data would repeat itself. So after test 1 it was only logged once, test 2 twice, etc.

For example something like the following was printed:

***** Running training *****
***** Running training *****
***** Running training *****
  Num examples = 10
  Num examples = 10
  Num examples = 10

After further investigation, I found that this was due to the stream_handler being added at the start of each test. As discussed in this SO post I found, since the loggers are global we keep adding more handlers without actually removing them.

As a result we kept getting multitudes of sys.stdout loggers being added, leading to these multiple print statements each time.

The fix is before each test case is written, add in our handler (e.g.):

stream_handler = logging.StreamHandler(sys.stdout)
logger.addHandler(stream_handler)

class SomeTestCase(TestCasePlus):
  ...

Also unsure why this didn't quite happen during CircleCI, but this was what occurred locally for me

@muellerzr muellerzr added Examples Which is related to examples in general bug labels Apr 8, 2022
@muellerzr muellerzr requested a review from sgugger April 8, 2022 15:08
@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Apr 8, 2022

The documentation is not available anymore as the PR was closed or merged.

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.

LGTM, but also pinging the logging master @LysandreJik
Thanks for fixing!

@muellerzr muellerzr requested a review from LysandreJik April 11, 2022 20:09
Copy link
Member

@LysandreJik LysandreJik 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 @muellerzr!

@muellerzr muellerzr merged commit 69233cf into main Apr 11, 2022
@muellerzr muellerzr deleted the muellerzr-logging-bugfix branch April 11, 2022 20:25
elusenji pushed a commit to elusenji/transformers that referenced this pull request Jun 12, 2022
Move declaration of log streams to before tests, so that results won't get compounded on top of each other
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Examples Which is related to examples in general
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants