Skip to content

Commit 9f236d3

Browse files
authored
【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
1 parent 599388e commit 9f236d3

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

python/paddle/jit/dy2static/convert_call_func.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import numpy
2525

26+
from paddle.nn import Layer
27+
2628
from .convert_operators import (
2729
convert_enumerate,
2830
convert_len,
@@ -302,8 +304,6 @@ def dyfunc(x):
302304
converted_call = None
303305

304306
elif hasattr(func, '__class__') and callable(func.__class__):
305-
from paddle.nn import Layer
306-
307307
if hasattr(func, 'forward') and isinstance(func, Layer):
308308
try:
309309
_, forward_func = unwrap_decorators(func.forward)

python/paddle/jit/dy2static/convert_operators.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616

1717
import paddle
1818
from paddle.fluid.data_feeder import convert_dtype
19-
from paddle.fluid.dygraph.base import _convert_into_variable
19+
from paddle.fluid.dygraph.base import (
20+
_convert_into_variable,
21+
in_declarative_mode,
22+
)
2023
from paddle.fluid.framework import Variable, core
2124
from paddle.fluid.layers import Print, control_flow
2225
from paddle.fluid.layers.control_flow import while_loop
@@ -40,8 +43,6 @@ def convert_attr(x, attr):
4043

4144

4245
def convert_load(x):
43-
from paddle.fluid.dygraph.base import in_declarative_mode
44-
4546
if in_declarative_mode() and isinstance(x, paddle.fluid.core.eager.Tensor):
4647
"""
4748
TODO:(@xiongkun) may run convert_load in dygraph mode, which should be fixed.

python/paddle/jit/dy2static/program_translator.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@
2222
from paddle.amp.auto_cast import _in_amp_guard
2323
from paddle.fluid import _non_static_mode, core, framework
2424
from paddle.fluid.data_feeder import check_type
25-
from paddle.fluid.dygraph.base import param_guard, switch_to_static_graph
25+
from paddle.fluid.dygraph.base import (
26+
_switch_declarative_mode_guard_,
27+
param_guard,
28+
switch_to_static_graph,
29+
)
2630
from paddle.nn.layer import layers
2731
from paddle.utils import flatten, gast
2832

@@ -989,8 +993,6 @@ def from_func_spec(
989993
framework.default_startup_program().random_seed
990994
)
991995

992-
from paddle.fluid.dygraph.base import _switch_declarative_mode_guard_
993-
994996
with framework.program_guard(main_program, startup_program):
995997
with _switch_declarative_mode_guard_(is_declarative=True):
996998
# 1. Adds `paddle.static.data` layers for input if needed

python/paddle/jit/dy2static/utils.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import numpy as np
3232

3333
import paddle
34+
from paddle import fluid # noqa: F401
3435
from paddle.fluid import core, unique_name
3536
from paddle.fluid.data_feeder import convert_dtype
3637
from paddle.fluid.layer_helper import LayerHelper
@@ -309,7 +310,6 @@ def in_white_list(module, func_name):
309310
def _delete_keywords_from(node):
310311
assert isinstance(node, gast.Call)
311312
func_src = astor.to_source(gast.gast_to_ast(node.func))
312-
from paddle import fluid # noqa: F401
313313

314314
full_args = eval(f"inspect.getfullargspec({func_src})")
315315
full_args_name = full_args[0]
@@ -390,7 +390,6 @@ def update_args_of_func(node, dygraph_node, method_name):
390390
)
391391

392392
class_src = astor.to_source(gast.gast_to_ast(dygraph_node.func))
393-
from paddle import fluid # noqa: F401
394393

395394
if method_name == "__init__" or eval(
396395
"issubclass({}, paddle.nn.Layer)".format(class_src)

python/paddle/jit/dy2static/utils_helper.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
import inspect
1717

1818
import astor
19+
import numpy as np # noqa: F401
1920

21+
import paddle # noqa: F401
22+
from paddle import fluid # noqa: F401
23+
from paddle.fluid import dygraph # noqa: F401
24+
from paddle.fluid import layers # noqa: F401
25+
from paddle.fluid.dygraph import to_variable # noqa: F401
2026
from paddle.utils import gast
2127

2228
from .ast_utils import ast_to_source_code
@@ -60,13 +66,8 @@ def is_api_in_module(node, module_prefix):
6066

6167
func_str = astor.to_source(gast.gast_to_ast(func_node)).strip()
6268
try:
63-
import paddle # noqa: F401
6469
import paddle.jit.dy2static as _jst # noqa: F401
65-
from paddle import fluid # noqa: F401
6670
from paddle import to_tensor # noqa: F401
67-
from paddle.fluid import dygraph # noqa: F401
68-
from paddle.fluid import layers # noqa: F401
69-
from paddle.fluid.dygraph import to_variable # noqa: F401
7071

7172
return eval(
7273
"_is_api_in_module_helper({}, '{}')".format(func_str, module_prefix)
@@ -85,8 +86,6 @@ def is_numpy_api(node):
8586
assert isinstance(node, gast.Call), "Input non-Call node for is_numpy_api"
8687
func_str = astor.to_source(gast.gast_to_ast(node.func))
8788
try:
88-
import numpy as np # noqa: F401
89-
9089
module_result = eval(
9190
"_is_api_in_module_helper({}, '{}')".format(func_str, "numpy")
9291
)

0 commit comments

Comments
 (0)