From 9f236d375695a7f11d60c713c23dcb7c41d5f9f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=98=A5=E4=B9=94?= <83450930+Liyulingyue@users.noreply.github.com> Date: Wed, 29 Mar 2023 14:04:17 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90Hackathon=20No.89=E3=80=91=20Remove=20?= =?UTF-8?q?circle=20import=20Part5=20(#51881)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test * rollback * move some abvious import into head part * rollback * reduce utils_helper.py * MOVE INTO DEVELOP * add from paddle.nn import Layer * codestyle --- python/paddle/jit/dy2static/convert_call_func.py | 4 ++-- python/paddle/jit/dy2static/convert_operators.py | 7 ++++--- python/paddle/jit/dy2static/program_translator.py | 8 +++++--- python/paddle/jit/dy2static/utils.py | 3 +-- python/paddle/jit/dy2static/utils_helper.py | 13 ++++++------- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/python/paddle/jit/dy2static/convert_call_func.py b/python/paddle/jit/dy2static/convert_call_func.py index 75daa4ba13a..077f208fb2c 100644 --- a/python/paddle/jit/dy2static/convert_call_func.py +++ b/python/paddle/jit/dy2static/convert_call_func.py @@ -23,6 +23,8 @@ from typing import Any, List import numpy +from paddle.nn import Layer + from .convert_operators import ( convert_enumerate, convert_len, @@ -302,8 +304,6 @@ def convert_call(func): converted_call = None elif hasattr(func, '__class__') and callable(func.__class__): - from paddle.nn import Layer - if hasattr(func, 'forward') and isinstance(func, Layer): try: _, forward_func = unwrap_decorators(func.forward) diff --git a/python/paddle/jit/dy2static/convert_operators.py b/python/paddle/jit/dy2static/convert_operators.py index 6d85fa7f97d..f69403cb75b 100644 --- a/python/paddle/jit/dy2static/convert_operators.py +++ b/python/paddle/jit/dy2static/convert_operators.py @@ -16,7 +16,10 @@ import re import paddle from paddle.fluid.data_feeder import convert_dtype -from paddle.fluid.dygraph.base import _convert_into_variable +from paddle.fluid.dygraph.base import ( + _convert_into_variable, + in_declarative_mode, +) from paddle.fluid.framework import Variable, core from paddle.fluid.layers import Print, control_flow from paddle.fluid.layers.control_flow import while_loop @@ -40,8 +43,6 @@ def convert_attr(x, attr): def convert_load(x): - from paddle.fluid.dygraph.base import in_declarative_mode - if in_declarative_mode() and isinstance(x, paddle.fluid.core.eager.Tensor): """ TODO:(@xiongkun) may run convert_load in dygraph mode, which should be fixed. diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index 52488a4d2d6..942f1323c62 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -22,7 +22,11 @@ import weakref from paddle.amp.auto_cast import _in_amp_guard from paddle.fluid import _non_static_mode, core, framework from paddle.fluid.data_feeder import check_type -from paddle.fluid.dygraph.base import param_guard, switch_to_static_graph +from paddle.fluid.dygraph.base import ( + _switch_declarative_mode_guard_, + param_guard, + switch_to_static_graph, +) from paddle.nn.layer import layers from paddle.utils import flatten, gast @@ -989,8 +993,6 @@ class ConcreteProgram: framework.default_startup_program().random_seed ) - from paddle.fluid.dygraph.base import _switch_declarative_mode_guard_ - with framework.program_guard(main_program, startup_program): with _switch_declarative_mode_guard_(is_declarative=True): # 1. Adds `paddle.static.data` layers for input if needed diff --git a/python/paddle/jit/dy2static/utils.py b/python/paddle/jit/dy2static/utils.py index c60c654ab7d..a1fd7376b86 100644 --- a/python/paddle/jit/dy2static/utils.py +++ b/python/paddle/jit/dy2static/utils.py @@ -31,6 +31,7 @@ import astor import numpy as np import paddle +from paddle import fluid # noqa: F401 from paddle.fluid import core, unique_name from paddle.fluid.data_feeder import convert_dtype from paddle.fluid.layer_helper import LayerHelper @@ -309,7 +310,6 @@ def is_paddle_func(func, ignore_white_list=True): def _delete_keywords_from(node): assert isinstance(node, gast.Call) func_src = astor.to_source(gast.gast_to_ast(node.func)) - from paddle import fluid # noqa: F401 full_args = eval(f"inspect.getfullargspec({func_src})") full_args_name = full_args[0] @@ -390,7 +390,6 @@ def update_args_of_func(node, dygraph_node, method_name): ) class_src = astor.to_source(gast.gast_to_ast(dygraph_node.func)) - from paddle import fluid # noqa: F401 if method_name == "__init__" or eval( "issubclass({}, paddle.nn.Layer)".format(class_src) diff --git a/python/paddle/jit/dy2static/utils_helper.py b/python/paddle/jit/dy2static/utils_helper.py index 78687603f71..67dc7c49885 100644 --- a/python/paddle/jit/dy2static/utils_helper.py +++ b/python/paddle/jit/dy2static/utils_helper.py @@ -16,7 +16,13 @@ import inspect import astor +import numpy as np # noqa: F401 +import paddle # noqa: F401 +from paddle import fluid # noqa: F401 +from paddle.fluid import dygraph # noqa: F401 +from paddle.fluid import layers # noqa: F401 +from paddle.fluid.dygraph import to_variable # noqa: F401 from paddle.utils import gast from .ast_utils import ast_to_source_code @@ -60,13 +66,8 @@ def is_api_in_module(node, module_prefix): func_str = astor.to_source(gast.gast_to_ast(func_node)).strip() try: - import paddle # noqa: F401 import paddle.jit.dy2static as _jst # noqa: F401 - from paddle import fluid # noqa: F401 from paddle import to_tensor # noqa: F401 - from paddle.fluid import dygraph # noqa: F401 - from paddle.fluid import layers # noqa: F401 - from paddle.fluid.dygraph import to_variable # noqa: F401 return eval( "_is_api_in_module_helper({}, '{}')".format(func_str, module_prefix) @@ -85,8 +86,6 @@ def is_numpy_api(node): assert isinstance(node, gast.Call), "Input non-Call node for is_numpy_api" func_str = astor.to_source(gast.gast_to_ast(node.func)) try: - import numpy as np # noqa: F401 - module_result = eval( "_is_api_in_module_helper({}, '{}')".format(func_str, "numpy") ) -- GitLab