[logging] autoflush (#9385)
This PR proposes to:
* auto-flush `transformers` logging
When using logging for tracing signals from different parts of the code and which could be mixed with print debug this aids to get all the logging events synchronized.
I don't think this change will introduce any performance impacts.
If it helps someone here is the code I used to sync `transformers` logging with various other debug prints.
I was porting bart to MP and I needed to trace that the device switching happens correctly and I added a bunch of logger.info calls inside `modeling_bart.py` and also had some other helpers `print` debug messages which weren't logger based:
```
# auto flush std streams
from sys import stdout, stderr
def stdout_write_flush(args, w=stderr.write): w(args); stderr.flush()
def stderr_write_flush(args, w=stderr.write): w(args); stderr.flush()
stdout.write = stdout_write_flush
stderr.write = stderr_write_flush
from transformers import BartTokenizer, BartForConditionalGeneration, BartConfig
import logging
import transformers.utils.logging
import transformers.models.bart.modeling_bart
# I wanted a shorter simpler format
handlers = transformers.utils.logging._get_library_root_logger().handlers
for handler in handlers:
formatter = logging.Formatter("[%(funcName)s] %(message)s")
handler.setFormatter(formatter)
transformers.models.bart.modeling_bart.logger.setLevel(transformers.logging.INFO)
```
@LysandreJik, @sgugger, @patrickvonplaten
This commit is contained in:
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
import threading
|
import threading
|
||||||
from logging import CRITICAL # NOQA
|
from logging import CRITICAL # NOQA
|
||||||
from logging import DEBUG # NOQA
|
from logging import DEBUG # NOQA
|
||||||
@@ -78,6 +79,7 @@ def _configure_library_root_logger() -> None:
|
|||||||
# This library has already configured the library root logger.
|
# This library has already configured the library root logger.
|
||||||
return
|
return
|
||||||
_default_handler = logging.StreamHandler() # Set sys.stderr as stream.
|
_default_handler = logging.StreamHandler() # Set sys.stderr as stream.
|
||||||
|
_default_handler.flush = sys.stderr.flush
|
||||||
|
|
||||||
# Apply our default configuration to the library root logger.
|
# Apply our default configuration to the library root logger.
|
||||||
library_root_logger = _get_library_root_logger()
|
library_root_logger = _get_library_root_logger()
|
||||||
|
|||||||
Reference in New Issue
Block a user