From 4aa8f6ad999f87151f16681c2b968b680bb7eb66 Mon Sep 17 00:00:00 2001 From: Stas Bekman Date: Tue, 5 Jan 2021 00:57:57 -0800 Subject: [PATCH] [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 --- src/transformers/utils/logging.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/transformers/utils/logging.py b/src/transformers/utils/logging.py index ad514f707a..9ac852a7e8 100644 --- a/src/transformers/utils/logging.py +++ b/src/transformers/utils/logging.py @@ -16,6 +16,7 @@ import logging import os +import sys import threading from logging import CRITICAL # 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. return _default_handler = logging.StreamHandler() # Set sys.stderr as stream. + _default_handler.flush = sys.stderr.flush # Apply our default configuration to the library root logger. library_root_logger = _get_library_root_logger()