-
Notifications
You must be signed in to change notification settings - Fork 28.2k
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
Centralize logging #6434
Centralize logging #6434
Conversation
src/transformers/hf_logging.py
Outdated
def get_verbosity() -> int: | ||
_configure_library_logger() | ||
return _get_library_logger().getEffectiveLevel() | ||
|
||
|
||
def set_verbosity(verbosity: int) -> None: | ||
_configure_library_logger() | ||
_get_library_logger().setLevel(verbosity) | ||
|
||
|
||
def get_logger(): | ||
_configure_library_logger() | ||
return _get_library_logger() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Three helper methods
if tqdm_enabled is None: | ||
tqdm_enabled = hf_logging.get_verbosity() <= hf_logging.INFO | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tqdm can be enabled/disabled like so.
Codecov Report
@@ Coverage Diff @@
## master #6434 +/- ##
==========================================
+ Coverage 79.63% 80.09% +0.46%
==========================================
Files 156 157 +1
Lines 28420 28471 +51
==========================================
+ Hits 22631 22805 +174
+ Misses 5789 5666 -123
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm all for centralization. This looks like a nice improvement for the user, great work!
Didn't read the full diff, just a bit, since there is POC in the title :-) Ping me when it's the right time to do so.
Looks good to me as well! I'm thinking that it might be a good idea to create one helper function for each verbosity level:
These functions might be easier to remember...what do you think @LysandreJik ? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it but I think we should follow a bit more the example of optuna and have a 'root logger' (they have a MIT licence so feel free borrow (and credit!) their logging code).
Also we can add a few utilities to easily disable tqdm bar I think.
src/transformers/file_utils.py
Outdated
|
||
|
||
logger = logging.getLogger(__name__) # pylint: disable=invalid-name | ||
logger = hf_logging.get_logger() # pylint: disable=invalid-name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to keep some detail of the filename in which the log happened (cc @lhoestq).
Can we keep __name__
as an argument here?
src/transformers/file_utils.py
Outdated
@@ -733,7 +732,7 @@ def http_get(url, temp_file, proxies=None, resume_size=0, user_agent: Union[Dict | |||
total=total, | |||
initial=resume_size, | |||
desc="Downloading", | |||
disable=bool(logger.getEffectiveLevel() == logging.NOTSET), | |||
disable=bool(logger.getEffectiveLevel() == hf_logging.NOTSET), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit mouthful in my opinion.
Following @patrickvonplaten suggestion to had
hf_logger.set_verbosity_info()
hf_logger.set_verbosity_warning()
hf_logger.set_verbosity_debug()
hf_logger.set_verbosity_error()
Could we maybe expose utilities like:
hf_logger.is_verbosity_at_least_info()
hf_logger.is_verbosity_at_least_warning()
hf_logger.is_verbosity_at_least_debug()
hf_logger.is_verbosity_at_least_error()
?
src/transformers/hf_logging.py
Outdated
_default_handler: Optional[logging.Handler] = None | ||
|
||
|
||
def _get_library_logger() -> logging.Logger: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As mentioned above, can we still accept a name
argument here and set the logger formatting to display it in addition to the name transformers
?
I think we will probably need to follow more the approach of optuna (have a root logger etc)
For a simpler completion, I would rather call these:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!! This new feature is more than welcome!
863af96
to
a931df8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following @thomwolf and @patrickvonplaten's reviews, added some helper methods and updated to be more similar to Optuna.
There is now a root logger with separate loggers for each file.
logging.basicConfig(level=logging.INFO) | ||
logger = logging.getLogger(__name__) | ||
hf_logging.set_verbosity_info() | ||
logger = hf_logging.get_logger(__name__) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example of helper method
This comment was marked as spam.
This comment was marked as spam.
src/transformers/file_utils.py
Outdated
@@ -757,7 +756,7 @@ def http_get(url, temp_file, proxies=None, resume_size=0, user_agent: Union[Dict | |||
total=total, | |||
initial=resume_size, | |||
desc="Downloading", | |||
disable=bool(logger.getEffectiveLevel() == logging.NOTSET), | |||
disable=hf_logging.get_verbosity() == hf_logging.NOTSET, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
disable=bool(hf_logging.get_verbosity() > hf_logging.INFO
)?
src/transformers/hf_logging.py
Outdated
def is_verbosity_at_least_info(): | ||
return get_verbosity() <= INFO | ||
|
||
|
||
def is_verbosity_at_least_warning(): | ||
return get_verbosity() <= WARNING | ||
|
||
|
||
def is_verbosity_at_least_debug(): | ||
return get_verbosity() <= DEBUG | ||
|
||
|
||
def is_verbosity_at_least_error(): | ||
return get_verbosity() <= ERROR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok actually I think these ones will be not very useful (cf the tqdm in file_utils
). Sorry for proposing them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
src/transformers/training_args.py
Outdated
@@ -247,7 +247,7 @@ class TrainingArguments: | |||
|
|||
def __post_init__(self): | |||
if self.disable_tqdm is None: | |||
self.disable_tqdm = logger.getEffectiveLevel() > logging.WARN | |||
self.disable_tqdm = logging.get_verbosity() <= logging.INFO |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand this change. Is the order different from logging
? Also I'm pretty sure we want the progress bars at the info level.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, this was a mistake. Reverting.
Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
* Logging * Style * hf_logging > utils.logging * Address @thomwolf's comments * Update test * Update src/transformers/benchmark/benchmark_utils.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> * Revert bad change Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com>
The goal of this PR is to offer a better way to manage logging to the HuggingFace/transformers users. It's a very simple proposal: implement a single logger that is shared across all files, and implement three helper methods that can be used across the library and by users:
Users can use these methods as such:
The noteworthy additions/changes are shown below.