From 01bdea7c31d30cd25d6db0e519270acd750a7ec1 Mon Sep 17 00:00:00 2001 From: liym27 <33742067+liym27@users.noreply.github.com> Date: Sat, 28 Nov 2020 17:29:55 +0800 Subject: [PATCH] [Dy2Stat] Don't conver the function from third library logging (#29161) --- .../dygraph_to_static/convert_call_func.py | 5 ++++- .../dygraph_to_static/test_recursive_call.py | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/convert_call_func.py b/python/paddle/fluid/dygraph/dygraph_to_static/convert_call_func.py index bd7f51d89b..a6b207bb99 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/convert_call_func.py +++ b/python/paddle/fluid/dygraph/dygraph_to_static/convert_call_func.py @@ -19,6 +19,7 @@ __all__ = ['convert_call'] import collections import copy import functools +import logging import inspect import pdb import re @@ -35,7 +36,9 @@ from paddle.fluid.dygraph.dygraph_to_static.program_translator import unwrap_dec from paddle.fluid.dygraph.layers import Layer # TODO(liym27): A better way to do this. -BUILTIN_LIKELY_MODULES = [collections, pdb, copy, inspect, re, six, numpy] +BUILTIN_LIKELY_MODULES = [ + collections, pdb, copy, inspect, re, six, numpy, logging +] translator_logger = TranslatorLogger() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_recursive_call.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_recursive_call.py index 18645efad2..ab524b1c32 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_recursive_call.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_recursive_call.py @@ -16,6 +16,7 @@ from __future__ import print_function import unittest +import logging import numpy as np import paddle.fluid as fluid @@ -49,6 +50,16 @@ def nested_func(x_v): return res +@declarative +def dyfunc_with_third_library_logging(x_v): + logging.info('test dyfunc_with_third_library_logging') + if fluid.layers.mean(x_v).numpy()[0] > 5: + x_v = x_v - 1 + else: + x_v = x_v + 1 + return x_v + + class TestRecursiveCall1(unittest.TestCase): def setUp(self): self.input = np.random.random([10, 16]).astype('float32') @@ -163,5 +174,16 @@ class TestRecursiveCall2(unittest.TestCase): static_res)) +class TestThirdPartyLibrary(TestRecursiveCall2): + def _run(self): + with fluid.dygraph.guard(): + self.dygraph_func = dyfunc_with_third_library_logging + fluid.default_startup_program.random_seed = SEED + fluid.default_main_program.random_seed = SEED + data = fluid.dygraph.to_variable(self.input) + res = self.dygraph_func(data) + return res.numpy() + + if __name__ == '__main__': unittest.main() -- GitLab