From b1eb28d74b72cce629744cde0b3e17bf6e713489 Mon Sep 17 00:00:00 2001 From: liym27 <33742067+liym27@users.noreply.github.com> Date: Tue, 27 Oct 2020 15:20:51 +0800 Subject: [PATCH] [Dy2Stat-log] Call warnings.warn() to display the warning-message only once when calling StaticFunc.__call__ or ProgramTranslator().get_output (#28260) --- .../dygraph/dygraph_to_static/program_translator.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/program_translator.py b/python/paddle/fluid/dygraph/dygraph_to_static/program_translator.py index 6d9bfc909a..9c3f572eb9 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/program_translator.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/program_translator.py @@ -20,6 +20,7 @@ import inspect import six import textwrap import threading +import warnings import weakref from paddle.fluid import framework @@ -298,7 +299,11 @@ class StaticFunction(object): # 1. call dygraph function directly if not enable `declarative` if not self._program_trans.enable_to_static: - logging_utils.warn( + # NOTE(liym27): + # Here calls `warnings.warn` but not `logging_utils.warn` because by default warnings.warn(message) + # will show up **only once**. StaticFunction.__call__ will run many times, it is appropriate to + # display this warning message only once. + warnings.warn( "The decorator '@paddle.jit.to_static' does NOT work when setting ProgramTranslator.enable to False. " "We will just return dygraph output. If you would like to get static graph output, please call API " "ProgramTranslator.enable(True)") @@ -831,7 +836,9 @@ class ProgramTranslator(object): ), "Input dygraph_func is not a callable in ProgramTranslator.get_output" if not self.enable_to_static: - logging_utils.warn( + # Here calls `warnings.warn` but not `logging_utils.warn` because by default warnings.warn(message) + # will show up **only once**. + warnings.warn( "The ProgramTranslator.get_output doesn't work when setting ProgramTranslator.enable to False. " "We will just return dygraph output. " "Please call ProgramTranslator.enable(True) if you would like to get static output."