未验证 提交 9f236d37 编写于 作者: 张春乔 提交者: GitHub

【Hackathon No.89】 Remove circle import Part5 (#51881)

* test

* rollback

* move some abvious import into head part

* rollback

* reduce utils_helper.py

* MOVE INTO DEVELOP

* add from paddle.nn import Layer

* codestyle
上级 599388e3
...@@ -23,6 +23,8 @@ from typing import Any, List ...@@ -23,6 +23,8 @@ from typing import Any, List
import numpy import numpy
from paddle.nn import Layer
from .convert_operators import ( from .convert_operators import (
convert_enumerate, convert_enumerate,
convert_len, convert_len,
...@@ -302,8 +304,6 @@ def convert_call(func): ...@@ -302,8 +304,6 @@ def convert_call(func):
converted_call = None converted_call = None
elif hasattr(func, '__class__') and callable(func.__class__): elif hasattr(func, '__class__') and callable(func.__class__):
from paddle.nn import Layer
if hasattr(func, 'forward') and isinstance(func, Layer): if hasattr(func, 'forward') and isinstance(func, Layer):
try: try:
_, forward_func = unwrap_decorators(func.forward) _, forward_func = unwrap_decorators(func.forward)
......
...@@ -16,7 +16,10 @@ import re ...@@ -16,7 +16,10 @@ import re
import paddle import paddle
from paddle.fluid.data_feeder import convert_dtype 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.framework import Variable, core
from paddle.fluid.layers import Print, control_flow from paddle.fluid.layers import Print, control_flow
from paddle.fluid.layers.control_flow import while_loop from paddle.fluid.layers.control_flow import while_loop
...@@ -40,8 +43,6 @@ def convert_attr(x, attr): ...@@ -40,8 +43,6 @@ def convert_attr(x, attr):
def convert_load(x): 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): 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. TODO:(@xiongkun) may run convert_load in dygraph mode, which should be fixed.
......
...@@ -22,7 +22,11 @@ import weakref ...@@ -22,7 +22,11 @@ import weakref
from paddle.amp.auto_cast import _in_amp_guard from paddle.amp.auto_cast import _in_amp_guard
from paddle.fluid import _non_static_mode, core, framework from paddle.fluid import _non_static_mode, core, framework
from paddle.fluid.data_feeder import check_type 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.nn.layer import layers
from paddle.utils import flatten, gast from paddle.utils import flatten, gast
...@@ -989,8 +993,6 @@ class ConcreteProgram: ...@@ -989,8 +993,6 @@ class ConcreteProgram:
framework.default_startup_program().random_seed 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 framework.program_guard(main_program, startup_program):
with _switch_declarative_mode_guard_(is_declarative=True): with _switch_declarative_mode_guard_(is_declarative=True):
# 1. Adds `paddle.static.data` layers for input if needed # 1. Adds `paddle.static.data` layers for input if needed
......
...@@ -31,6 +31,7 @@ import astor ...@@ -31,6 +31,7 @@ import astor
import numpy as np import numpy as np
import paddle import paddle
from paddle import fluid # noqa: F401
from paddle.fluid import core, unique_name from paddle.fluid import core, unique_name
from paddle.fluid.data_feeder import convert_dtype from paddle.fluid.data_feeder import convert_dtype
from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.layer_helper import LayerHelper
...@@ -309,7 +310,6 @@ def is_paddle_func(func, ignore_white_list=True): ...@@ -309,7 +310,6 @@ def is_paddle_func(func, ignore_white_list=True):
def _delete_keywords_from(node): def _delete_keywords_from(node):
assert isinstance(node, gast.Call) assert isinstance(node, gast.Call)
func_src = astor.to_source(gast.gast_to_ast(node.func)) 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 = eval(f"inspect.getfullargspec({func_src})")
full_args_name = full_args[0] full_args_name = full_args[0]
...@@ -390,7 +390,6 @@ def update_args_of_func(node, dygraph_node, method_name): ...@@ -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)) class_src = astor.to_source(gast.gast_to_ast(dygraph_node.func))
from paddle import fluid # noqa: F401
if method_name == "__init__" or eval( if method_name == "__init__" or eval(
"issubclass({}, paddle.nn.Layer)".format(class_src) "issubclass({}, paddle.nn.Layer)".format(class_src)
......
...@@ -16,7 +16,13 @@ ...@@ -16,7 +16,13 @@
import inspect import inspect
import astor 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 paddle.utils import gast
from .ast_utils import ast_to_source_code from .ast_utils import ast_to_source_code
...@@ -60,13 +66,8 @@ def is_api_in_module(node, module_prefix): ...@@ -60,13 +66,8 @@ def is_api_in_module(node, module_prefix):
func_str = astor.to_source(gast.gast_to_ast(func_node)).strip() func_str = astor.to_source(gast.gast_to_ast(func_node)).strip()
try: try:
import paddle # noqa: F401
import paddle.jit.dy2static as _jst # 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 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( return eval(
"_is_api_in_module_helper({}, '{}')".format(func_str, module_prefix) "_is_api_in_module_helper({}, '{}')".format(func_str, module_prefix)
...@@ -85,8 +86,6 @@ def is_numpy_api(node): ...@@ -85,8 +86,6 @@ def is_numpy_api(node):
assert isinstance(node, gast.Call), "Input non-Call node for is_numpy_api" assert isinstance(node, gast.Call), "Input non-Call node for is_numpy_api"
func_str = astor.to_source(gast.gast_to_ast(node.func)) func_str = astor.to_source(gast.gast_to_ast(node.func))
try: try:
import numpy as np # noqa: F401
module_result = eval( module_result = eval(
"_is_api_in_module_helper({}, '{}')".format(func_str, "numpy") "_is_api_in_module_helper({}, '{}')".format(func_str, "numpy")
) )
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册