[logging] implement warning_advice / TRANSFORMERS_NO_ADVISORY_WARNINGS (#14669)
* [logging] implement warning_advice / TRANSFORMERS_NO_ADVISORY_WARNINGS * reword
This commit is contained in:
@@ -32,6 +32,15 @@ to one of the following: ``debug``, ``info``, ``warning``, ``error``, ``critical
|
||||
|
||||
TRANSFORMERS_VERBOSITY=error ./myprogram.py
|
||||
|
||||
Additionally, some ``warnings`` can be disabled by setting the environment variable
|
||||
``TRANSFORMERS_NO_ADVISORY_WARNINGS`` to a true value, like `1`. This will disable any warning that is logged using
|
||||
:meth:`logger.warning_advice`. For example:
|
||||
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
TRANSFORMERS_NO_ADVISORY_WARNINGS=1 ./myprogram.py
|
||||
|
||||
All the methods of this logging module are documented below, the main ones are
|
||||
:func:`transformers.logging.get_verbosity` to get the current level of verbosity in the logger and
|
||||
:func:`transformers.logging.set_verbosity` to set the verbosity to the level of your choice. In order (from the least
|
||||
|
||||
@@ -1937,7 +1937,7 @@ class PreTrainedTokenizerBase(SpecialTokensMixin, PushToHubMixin):
|
||||
# Check all our special tokens are registered as "no split" token (we don't cut them) and are in the vocab
|
||||
added_tokens = tokenizer.sanitize_special_tokens()
|
||||
if added_tokens:
|
||||
logger.warning(
|
||||
logger.warning_advice(
|
||||
"Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained."
|
||||
)
|
||||
|
||||
|
||||
@@ -263,3 +263,17 @@ def reset_format() -> None:
|
||||
|
||||
for handler in handlers:
|
||||
handler.setFormatter(None)
|
||||
|
||||
|
||||
def warning_advice(self, *args, **kwargs):
|
||||
"""
|
||||
This method is identical to `logger.warning()`, but if env var TRANSFORMERS_NO_ADVISORY_WARNINGS=1 is set, this
|
||||
warning will not be printed
|
||||
"""
|
||||
no_advisory_warnings = os.getenv("TRANSFORMERS_NO_ADVISORY_WARNINGS", False)
|
||||
if no_advisory_warnings:
|
||||
return
|
||||
self.warning(*args, **kwargs)
|
||||
|
||||
|
||||
logging.Logger.warning_advice = warning_advice
|
||||
|
||||
@@ -17,7 +17,7 @@ import unittest
|
||||
|
||||
import transformers.models.bart.tokenization_bart
|
||||
from transformers import logging
|
||||
from transformers.testing_utils import CaptureLogger, mockenv
|
||||
from transformers.testing_utils import CaptureLogger, mockenv, mockenv_context
|
||||
|
||||
|
||||
class HfArgumentParserTest(unittest.TestCase):
|
||||
@@ -103,3 +103,21 @@ class HfArgumentParserTest(unittest.TestCase):
|
||||
self.assertIn("Unknown option TRANSFORMERS_VERBOSITY=super-error", cl.out)
|
||||
|
||||
# no need to restore as nothing was changed
|
||||
|
||||
def test_advisory_warnings(self):
|
||||
# testing `logger.warning_advice()`
|
||||
|
||||
logger = logging.get_logger("transformers.models.bart.tokenization_bart")
|
||||
msg = "Testing 1, 2, 3"
|
||||
|
||||
with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS="1"):
|
||||
# nothing should be logged as env var disables this method
|
||||
with CaptureLogger(logger) as cl:
|
||||
logger.warning_advice(msg)
|
||||
self.assertEqual(cl.out, "")
|
||||
|
||||
with mockenv_context(TRANSFORMERS_NO_ADVISORY_WARNINGS=""):
|
||||
# should log normally as TRANSFORMERS_NO_ADVISORY_WARNINGS is unset
|
||||
with CaptureLogger(logger) as cl:
|
||||
logger.warning_advice(msg)
|
||||
self.assertEqual(cl.out, msg + "\n")
|
||||
|
||||
Reference in New Issue
Block a user