diff --git a/docs/source/main_classes/logging.rst b/docs/source/main_classes/logging.rst index 6e2441a349..9377bb0613 100644 --- a/docs/source/main_classes/logging.rst +++ b/docs/source/main_classes/logging.rst @@ -1,4 +1,4 @@ -.. +.. Copyright 2020 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with @@ -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 diff --git a/src/transformers/tokenization_utils_base.py b/src/transformers/tokenization_utils_base.py index d72ad37dcf..4ca518e8ad 100644 --- a/src/transformers/tokenization_utils_base.py +++ b/src/transformers/tokenization_utils_base.py @@ -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." ) diff --git a/src/transformers/utils/logging.py b/src/transformers/utils/logging.py index 4abb94aa4e..eab03c19a5 100644 --- a/src/transformers/utils/logging.py +++ b/src/transformers/utils/logging.py @@ -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 diff --git a/tests/test_logging.py b/tests/test_logging.py index d0633bfbe4..914a46a3c8 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -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")