diff --git a/python/paddle/fluid/compiler.py b/python/paddle/fluid/compiler.py index 81af46c468adbd7b9f1f9bc948f33ce2870da4e7..f763e0f1d8838ccde33f711b64586622ac84fdc4 100644 --- a/python/paddle/fluid/compiler.py +++ b/python/paddle/fluid/compiler.py @@ -696,7 +696,7 @@ class IpuDynamicPatcher: ProgramCache, MAX_TRACED_PROGRAM_COUNT, ) - from ..fluid.dygraph.dygraph_to_static import logging_utils + from paddle.jit.dy2static import logging_utils from paddle.jit.dy2static.partial_program import ( partial_program_from, ) diff --git a/python/paddle/fluid/dygraph/__init__.py b/python/paddle/fluid/dygraph/__init__.py index 76c75b974e3a42ec2845c6216047e8868d2c1ded..551561428da72f932e34baad8ea36aa4722c6b21 100644 --- a/python/paddle/fluid/dygraph/__init__.py +++ b/python/paddle/fluid/dygraph/__init__.py @@ -43,8 +43,6 @@ from .io import * from . import static_runner from .static_runner import StaticModelRunner -from . import dygraph_to_static - from . import rnn from .rnn import * diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/__init__.py b/python/paddle/fluid/dygraph/dygraph_to_static/__init__.py deleted file mode 100644 index 15013fb36d806fe009afc565e2eeeefc2cb6b36f..0000000000000000000000000000000000000000 --- a/python/paddle/fluid/dygraph/dygraph_to_static/__init__.py +++ /dev/null @@ -1,28 +0,0 @@ -# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from . import static_analysis -from .static_analysis import * - -from . import variable_trans_func -from .variable_trans_func import * - - -from . import logging_utils -from .logging_utils import * - -__all__ = [] -__all__ += static_analysis.__all__ -__all__ += variable_trans_func.__all__ -__all__ += logging_utils.__all__ diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/variable_trans_func.py b/python/paddle/fluid/dygraph/dygraph_to_static/variable_trans_func.py deleted file mode 100644 index f28f1993621cc43eb8e4dfb1ddf6f2b07b99c432..0000000000000000000000000000000000000000 --- a/python/paddle/fluid/dygraph/dygraph_to_static/variable_trans_func.py +++ /dev/null @@ -1,95 +0,0 @@ -# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -import paddle -import textwrap -from paddle.utils import gast -from paddle.fluid import unique_name -from paddle.fluid.framework import Variable -from paddle.fluid.dygraph.dygraph_to_static.utils import ( - UndefinedVar, - create_undefined_variable, -) -from paddle.fluid.layers.utils import map_structure, is_sequence - -__all__ = [ - 'create_bool_as_type', - 'create_fill_constant_node', - 'to_static_variable', - 'create_undefined_var', -] - - -def create_undefined_var(name): - func_code = "{} = _jst.UndefinedVar('{}')".format(name, name) - return gast.parse(func_code).body[0] - - -def create_fill_constant_node(name, value=0): - func_code = "{} = paddle.full(shape=[1], ".format(name) - if isinstance(value, bool): - func_code += "dtype='bool', fill_value={}, name='{}')".format( - value, name - ) - return gast.parse(func_code).body[0] - if isinstance(value, float): - func_code += "dtype='float64', fill_value={}, name='{}')".format( - value, name - ) - return gast.parse(func_code).body[0] - - if isinstance(value, int): - func_code += "dtype='int64', fill_value={}, name='{}')".format( - value, name - ) - return gast.parse(func_code).body[0] - - -def to_static_variable(x): - ''' - Translate a Python Tensor to PaddlePaddle static graph Tensor - ''' - if isinstance(x, bool): - return paddle.full(shape=[1], dtype='bool', fill_value=x) - if isinstance(x, float): - return paddle.full(shape=[1], dtype='float64', fill_value=x) - if isinstance(x, int): - return paddle.full(shape=[1], dtype='int64', fill_value=x) - if isinstance(x, UndefinedVar) or x is None: - """ - for early return case, we need a variable to represent None, current we use data_layer_not_check. - """ - return create_undefined_variable() - if is_sequence(x): - return map_structure(to_static_variable, x) - return x - - -def create_bool_as_type(x, value=True): - ''' - Create a bool variable, which type is the same as x. - ''' - if isinstance(x, Variable): - return paddle.full(shape=[1], fill_value=value, dtype="bool") - else: - return value - - -def create_bool_node(name, value): - ''' - Create a assign stmt for name = value . - ''' - assert isinstance(value, bool) - node = "{} = {}".format(name, value) - return gast.parse(node).body[0] diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py index c47e9babea7fdb549c3647aca96a15a7e6383cce..7a6079ad62c18ea8faec88b433225227e16289e3 100755 --- a/python/paddle/fluid/layers/control_flow.py +++ b/python/paddle/fluid/layers/control_flow.py @@ -159,10 +159,10 @@ def select_input(inputs, mask): def select_input_with_buildin_type(inputs, mask, name): - from paddle.fluid.dygraph.dygraph_to_static.variable_trans_func import ( + from paddle.jit.dy2static.variable_trans_func import ( to_static_variable, ) - from paddle.fluid.dygraph.dygraph_to_static.utils import UndefinedVar + from paddle.jit.dy2static.utils import UndefinedVar false_var, true_var = inputs @@ -1484,7 +1484,7 @@ def _deal_with_undefined_var(output_vars, loop_vars): 3. UndefinedVar = List(int) # create a list of variable 4. UndefinedVar = value # create a variable """ - from paddle.fluid.dygraph.dygraph_to_static.utils import ( + from paddle.jit.dy2static.utils import ( UndefinedVar, create_undefined_variable, ) @@ -2552,7 +2552,7 @@ def cond(pred, true_fn=None, false_fn=None, name=None, return_names=None): def change_none_to_undefinedvar(nest1, nest2): - from paddle.fluid.dygraph.dygraph_to_static.utils import UndefinedVar + from paddle.jit.dy2static.utils import UndefinedVar def map_fn(x): if x is None: @@ -2588,7 +2588,7 @@ def expand_undefined_var(nest1, nest2, names): nest2: Var2, ([1,2,3,4], UndefinedVar) In this case, we should not expand recursively. """ - from paddle.fluid.dygraph.dygraph_to_static.utils import UndefinedVar + from paddle.jit.dy2static.utils import UndefinedVar from paddle.jit.dy2static.return_transformer import ( RETURN_VALUE_PREFIX, ) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ast_util.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ast_util.py index b81ed5f0b4360ad2989ceec6d9234510f25198f6..b417cd5fd95af2c532e1a6410b3cfdfe9849ffb9 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ast_util.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ast_util.py @@ -25,7 +25,7 @@ from ifelse_simple_func import ( import paddle import paddle.fluid as fluid -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_func +from paddle.jit.dy2static.utils import ast_to_func from paddle.utils import gast diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_basic_api_transformation.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_basic_api_transformation.py index 34a65913c5ae56cbf11d4273d2d4f8a87db86bf5..3733977c5dbcfa2e53624e8d0517ddc4d4f67ad3 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_basic_api_transformation.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_basic_api_transformation.py @@ -22,8 +22,8 @@ import paddle.fluid as fluid import paddle.fluid.dygraph as dygraph from paddle import to_tensor from paddle.fluid.dygraph import to_variable -from paddle.fluid.dygraph.dygraph_to_static.utils import is_dygraph_api from paddle.jit.api import dygraph_to_static_func +from paddle.jit.dy2static.utils import is_dygraph_api from paddle.utils import gast SEED = 2020 diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_break_continue.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_break_continue.py index b3d1b5b2cb4f63898ffa39dcecfe1e2fbdc48e10..f81c2d53640f55e931d66ac7c909250abd367a0a 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_break_continue.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_break_continue.py @@ -18,9 +18,9 @@ import numpy as np import paddle import paddle.fluid as fluid -from paddle.fluid.dygraph.dygraph_to_static.utils import Dygraph2StaticException from paddle.jit.api import declarative from paddle.jit.dy2static.program_translator import ProgramTranslator +from paddle.jit.dy2static.utils import Dygraph2StaticException SEED = 2020 np.random.seed(SEED) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_closure_analysis.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_closure_analysis.py index b0156699161e7fdd5ae68d9cbd9bea4c0b470bda..bed90ccbe47fe090938f67aa24a1c0324f62dcdf 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_closure_analysis.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_closure_analysis.py @@ -18,9 +18,7 @@ import unittest from numpy import append import paddle -from paddle.fluid.dygraph.dygraph_to_static.utils import ( - FunctionNameLivenessAnalysis, -) +from paddle.jit.dy2static.utils import FunctionNameLivenessAnalysis from paddle.utils import gast global_a = [] diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py index 6faed1a61e809d06f168a9c2f3c8f19a1ee3e64b..805970814425dabfe71dda6a121df17e3414f432 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_error.py @@ -18,8 +18,8 @@ import unittest import numpy as np import paddle import paddle.fluid as fluid -from paddle.fluid.dygraph.dygraph_to_static import error -from paddle.fluid.dygraph.dygraph_to_static.origin_info import unwrap +from paddle.jit.dy2static import error +from paddle.jit.dy2static.origin_info import unwrap def inner_func(): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_function_spec.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_function_spec.py index b7a26169f3a2d800437137b2a95f4b5d1c74ed49..ac9f6362cb42c06c381dc72d45d8990aadf9650d 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_function_spec.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_function_spec.py @@ -17,7 +17,7 @@ import unittest from test_declarative import foo_func import paddle -from paddle.fluid.dygraph.dygraph_to_static.function_spec import FunctionSpec +from paddle.jit.dy2static.function_spec import FunctionSpec from paddle.static import InputSpec paddle.enable_static() diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse.py index 89b9e871564f9fb2209ffad1bd7b6c25eefccc93..c17bfd2508b3de369f5d6b6acb30d4bc77a0d5d2 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_ifelse.py @@ -43,9 +43,9 @@ from ifelse_simple_func import ( import paddle import paddle.fluid.core as core -from paddle.fluid.dygraph.dygraph_to_static.utils import Dygraph2StaticException from paddle.jit.api import declarative from paddle.jit.dy2static.program_translator import ProgramTranslator +from paddle.jit.dy2static.utils import Dygraph2StaticException np.random.seed(1) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_logging_utils.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_logging_utils.py index 93e5fae00d410f75ad416407e6c0eb257aaaa4b1..fa34869e922055cb42af6a576e5707a384af56aa 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_logging_utils.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_logging_utils.py @@ -20,7 +20,7 @@ import unittest from unittest import mock import paddle -from paddle.fluid.dygraph.dygraph_to_static import logging_utils +from paddle.jit.dy2static import logging_utils from paddle.utils import gast diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py index 65c8fb4a1fd7044fdb71cb68fccc831ada579c8c..540ad3c13bb4b8f56add81338840cab96feaa640 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_origin_info.py @@ -15,7 +15,9 @@ import sys import unittest -from paddle.fluid.dygraph.dygraph_to_static.origin_info import ( +from paddle.jit.api import declarative +from paddle.jit.dy2static import DygraphToStaticAst +from paddle.jit.dy2static.origin_info import ( ORIGI_INFO, Location, OriginInfo, @@ -25,9 +27,7 @@ from paddle.fluid.dygraph.dygraph_to_static.origin_info import ( inspect, unwrap, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_func -from paddle.jit.api import declarative -from paddle.jit.dy2static import DygraphToStaticAst +from paddle.jit.dy2static.utils import ast_to_func def simple_func(x): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py index c61fdcccf015ac682177e3bc7babe5b085b29650..aee91f6de1729fabfb8a490c005736b0f59253a5 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_program_translator.py @@ -27,9 +27,9 @@ from ifelse_simple_func import ( import paddle import paddle.fluid as fluid import paddle.jit.dy2static as _jst -from paddle.fluid.dygraph.dygraph_to_static.utils import func_to_source_code from paddle.jit import ProgramTranslator from paddle.jit.api import declarative +from paddle.jit.dy2static.utils import func_to_source_code from paddle.utils import gast np.random.seed(0) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_return.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_return.py index 76e3de2a5ec870aea702947b244a54e40f13d0b4..b4ec4fb8fd5afea62670299561b0cafb7702ab56 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_return.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_return.py @@ -20,8 +20,8 @@ from ifelse_simple_func import dyfunc_with_if_else import paddle import paddle.fluid as fluid import paddle.fluid.core as core -from paddle.fluid.dygraph.dygraph_to_static.utils import Dygraph2StaticException from paddle.jit import ProgramTranslator, to_static +from paddle.jit.dy2static.utils import Dygraph2StaticException SEED = 2020 np.random.seed(SEED) diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_rollback.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_rollback.py index e7dabd9af31ea42bd4243250d97b516aeb508be5..c418a850d5aafb40ac2a3b96e6c8f9a2538757c2 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_rollback.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_rollback.py @@ -17,8 +17,8 @@ import unittest import numpy as np import paddle -from paddle.fluid.dygraph.dygraph_to_static.utils import func_to_source_code from paddle.jit.dy2static.program_translator import StaticFunction +from paddle.jit.dy2static.utils import func_to_source_code class Net(paddle.nn.Layer): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_setter_helper.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_setter_helper.py index 725dc032d5e2015592c05bf804def8a8cd496c9e..eab182b6f419dfe04cac5b403833c9785956cd4d 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_setter_helper.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_setter_helper.py @@ -14,7 +14,7 @@ import unittest -from paddle.fluid.dygraph.dygraph_to_static.utils import GetterSetterHelper +from paddle.jit.dy2static.utils import GetterSetterHelper vars = [1, 2, 3, 4, 5] diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_static_analysis.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_static_analysis.py index 22598ede71b74fdb2e3c809af6173a7dfcbd185d..23eb6964c3e3d7b3c311de80188d33193e0d4837 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_static_analysis.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_static_analysis.py @@ -19,10 +19,7 @@ import numpy as np import paddle import paddle.fluid as fluid -from paddle.fluid.dygraph.dygraph_to_static import ( - NodeVarType, - StaticAnalysisVisitor, -) +from paddle.jit.dy2static import NodeVarType, StaticAnalysisVisitor from paddle.utils import gast diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_utils.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_utils.py index a310ab65307c587f9bcf986411a53bc80c3c34dd..3361a866feb5408daae0b205177f4590ed6a2000 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_utils.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_utils.py @@ -15,10 +15,7 @@ import types import unittest -from paddle.fluid.dygraph.dygraph_to_static.utils import ( - index_in_list, - is_paddle_func, -) +from paddle.jit.dy2static.utils import index_in_list, is_paddle_func class TestIndexInList(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_variable_trans_func.py b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_variable_trans_func.py index 353a5e8b79738afce5438a7cf2f19bcc81e3cdac..f2395fa517793d284ba0ec60e6de5cbcb3166177 100644 --- a/python/paddle/fluid/tests/unittests/dygraph_to_static/test_variable_trans_func.py +++ b/python/paddle/fluid/tests/unittests/dygraph_to_static/test_variable_trans_func.py @@ -14,10 +14,8 @@ import unittest -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_source_code -from paddle.fluid.dygraph.dygraph_to_static.variable_trans_func import ( - create_fill_constant_node, -) +from paddle.jit.dy2static.utils import ast_to_source_code +from paddle.jit.dy2static.variable_trans_func import create_fill_constant_node class TestVariableTransFunc(unittest.TestCase): diff --git a/python/paddle/fluid/tests/unittests/op_test.py b/python/paddle/fluid/tests/unittests/op_test.py index fd43a80c176185e8cf59f76f92a91d9215acd064..9728edf5d1c04ff95575ae62b04f2c5128cc0064 100644 --- a/python/paddle/fluid/tests/unittests/op_test.py +++ b/python/paddle/fluid/tests/unittests/op_test.py @@ -29,7 +29,6 @@ import paddle.fluid as fluid import paddle.fluid.core as core from paddle.fluid import unique_name from paddle.fluid.backward import append_backward -from paddle.fluid.dygraph.dygraph_to_static.utils import parse_arg_and_kwargs from paddle.fluid.executor import Executor from paddle.fluid.framework import ( OpProtoHolder, @@ -43,6 +42,7 @@ from paddle.fluid.framework import ( _test_eager_guard, ) from paddle.fluid.op import Operator +from paddle.jit.dy2static.utils import parse_arg_and_kwargs sys.path.append(os.path.abspath(os.path.dirname(__file__))) from testsuite import append_input_output, append_loss_ops, create_op, set_input diff --git a/python/paddle/fluid/tests/unittests/test_input_spec.py b/python/paddle/fluid/tests/unittests/test_input_spec.py index 3a623f463c1813d5c3b5d7d16270c62d44a8ec78..07a2e4da449ba24174d86d9ec2a5e3e3f43a8087 100644 --- a/python/paddle/fluid/tests/unittests/test_input_spec.py +++ b/python/paddle/fluid/tests/unittests/test_input_spec.py @@ -20,10 +20,8 @@ import numpy as np import paddle import paddle.fluid as fluid -from paddle.fluid.dygraph.dygraph_to_static.utils import ( - _compatible_non_tensor_spec, -) from paddle.fluid.framework import convert_np_dtype_to_dtype_ +from paddle.jit.dy2static.utils import _compatible_non_tensor_spec from paddle.static import InputSpec diff --git a/python/paddle/fluid/variable_index.py b/python/paddle/fluid/variable_index.py index cf298501a29f8b4b64ffd44ebc9ee249d3abbb15..31d587269db71c9fd1f80c6b02ad206873fea630 100644 --- a/python/paddle/fluid/variable_index.py +++ b/python/paddle/fluid/variable_index.py @@ -622,7 +622,7 @@ def _setitem_for_tensor_array(var, item, value): not _non_static_mode() ), "setitem for tensor_array must be called in static graph mode." if isinstance(item, (Variable, int)): - from paddle.fluid.dygraph.dygraph_to_static.variable_trans_func import ( + from paddle.jit.dy2static.variable_trans_func import ( to_static_variable, ) from paddle import cast diff --git a/python/paddle/jit/api.py b/python/paddle/jit/api.py index 0387460c26a5d7a3e9fd5d4cc8a5142b294fcc39..95b07a989a7a7325d20e29ba583c02f3803460cd 100644 --- a/python/paddle/jit/api.py +++ b/python/paddle/jit/api.py @@ -34,16 +34,16 @@ from paddle.fluid.dygraph.base import ( program_desc_tracing_guard, switch_to_static_graph, ) -from paddle.fluid.dygraph.dygraph_to_static import logging_utils -from paddle.jit.dy2static.convert_call_func import ( +from .dy2static import logging_utils +from .dy2static.convert_call_func import ( ConversionOptions, CONVERSION_OPTIONS, ) -from paddle.fluid.dygraph.dygraph_to_static.logging_utils import ( +from .dy2static.logging_utils import ( set_code_level, set_verbosity, ) -from paddle.jit.dy2static.program_translator import ( +from .dy2static.program_translator import ( ProgramTranslator, StaticFunction, unwrap_decorators, diff --git a/python/paddle/jit/dy2static/__init__.py b/python/paddle/jit/dy2static/__init__.py index 605c3ea93bf48882abc596ad8d8ddb49cf7b6371..c42116c21065de30100a1838e18ae15bf6300f1e 100644 --- a/python/paddle/jit/dy2static/__init__.py +++ b/python/paddle/jit/dy2static/__init__.py @@ -12,8 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. -from .base import saw -from .base import UndefinedVar +from .utils import ( + saw, + UndefinedVar, +) from .convert_operators import convert_logical_and as And # noqa: F401 from .convert_operators import convert_var_dtype as AsDtype # noqa: F401 from .convert_operators import convert_assert as Assert # noqa: F401 @@ -35,5 +37,6 @@ from .convert_operators import convert_shape_compare # noqa: F401 from .assert_transformer import AssertTransformer from .ast_transformer import DygraphToStaticAst from .program_translator import convert_to_static +from .static_analysis import * # noqa: F403 __all__ = [] diff --git a/python/paddle/jit/dy2static/assert_transformer.py b/python/paddle/jit/dy2static/assert_transformer.py index 3a8a8b01aecc8b9caf4f14b5c5cffd7eeaa7cfa4..81bad1111b19205ac0f8dcd55855c9bacddba1c5 100644 --- a/python/paddle/jit/dy2static/assert_transformer.py +++ b/python/paddle/jit/dy2static/assert_transformer.py @@ -14,10 +14,10 @@ from paddle.utils import gast -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( +from paddle.jit.dy2static.static_analysis import ( AstNodeWrapper, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_source_code +from paddle.jit.dy2static.utils import ast_to_source_code from .base_transformer import ( BaseTransformer, ) diff --git a/python/paddle/jit/dy2static/ast_transformer.py b/python/paddle/jit/dy2static/ast_transformer.py index b23a6dc368ad3762b7d90ee64fcbc65770f5ad2a..2e244d6f341833c53111d83890972dec6bff9d11 100644 --- a/python/paddle/jit/dy2static/ast_transformer.py +++ b/python/paddle/jit/dy2static/ast_transformer.py @@ -61,7 +61,7 @@ from .return_transformer import ( from .create_variable_transformer import ( CreateVariableTransformer, ) -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( +from .static_analysis import ( StaticAnalysisVisitor, ) from .tensor_shape_transformer import ( @@ -71,8 +71,8 @@ from .decorator_transformer import ( DecoratorTransformer, ) -from paddle.fluid.dygraph.dygraph_to_static import logging_utils -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_source_code +from . import logging_utils +from .utils import ast_to_source_code __all__ = ['DygraphToStaticAst'] diff --git a/python/paddle/jit/dy2static/base.py b/python/paddle/jit/dy2static/base.py deleted file mode 100644 index 5d1bb01cf5c0be5b0c6d62248123eee836396d91..0000000000000000000000000000000000000000 --- a/python/paddle/jit/dy2static/base.py +++ /dev/null @@ -1,18 +0,0 @@ -# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -from ...fluid.dygraph.dygraph_to_static.utils import saw # noqa: F401 -from ...fluid.dygraph.dygraph_to_static.utils import UndefinedVar # noqa: F401 - -__all__ = [] diff --git a/python/paddle/jit/dy2static/base_transformer.py b/python/paddle/jit/dy2static/base_transformer.py index 518805250de93a0c3a293aa061613f09ac326aeb..166753d05f57a41201d04202e840e73a31a42df1 100644 --- a/python/paddle/jit/dy2static/base_transformer.py +++ b/python/paddle/jit/dy2static/base_transformer.py @@ -14,7 +14,7 @@ from paddle.utils import gast from paddle.fluid import unique_name -from paddle.fluid.dygraph.dygraph_to_static.utils import ( +from paddle.jit.dy2static.utils import ( ORIGI_INFO, FOR_ITER_INDEX_PREFIX, FOR_ITER_VAR_LEN_PREFIX, diff --git a/python/paddle/jit/dy2static/basic_api_transformer.py b/python/paddle/jit/dy2static/basic_api_transformer.py index 8a80fc4e6ed64102575802923ec858e6e9faea73..89fa0738b9a190413fcd26be8e3263f55f3859b9 100644 --- a/python/paddle/jit/dy2static/basic_api_transformer.py +++ b/python/paddle/jit/dy2static/basic_api_transformer.py @@ -15,14 +15,13 @@ import astor from paddle.utils import gast -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( +from .static_analysis import ( AstNodeWrapper, ) -from paddle.fluid.dygraph.dygraph_to_static import utils +from . import utils from .base_transformer import ( BaseTransformer, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_source_code class BasicApiTransformer(BaseTransformer): @@ -166,7 +165,7 @@ class AttributeJstTransformer(BaseTransformer): node = ( gast.parse( "_jst.Attr({}, \"{}\")".format( - ast_to_source_code(value).strip(), attr + utils.ast_to_source_code(value).strip(), attr ) ) .body[0] diff --git a/python/paddle/jit/dy2static/break_continue_transformer.py b/python/paddle/jit/dy2static/break_continue_transformer.py index 23576fdf9849ec7c49d140477d202ae0a0aa443f..670cc842f349b5f52b276a998244d56086a25f8d 100644 --- a/python/paddle/jit/dy2static/break_continue_transformer.py +++ b/python/paddle/jit/dy2static/break_continue_transformer.py @@ -15,9 +15,9 @@ from paddle.utils import gast from paddle.fluid import unique_name -from paddle.fluid.dygraph.dygraph_to_static.utils import index_in_list -from paddle.fluid.dygraph.dygraph_to_static.utils import BaseNodeVisitor -from paddle.fluid.dygraph.dygraph_to_static.variable_trans_func import ( +from paddle.jit.dy2static.utils import index_in_list +from paddle.jit.dy2static.utils import BaseNodeVisitor +from paddle.jit.dy2static.variable_trans_func import ( create_bool_node, ) from .base_transformer import ( diff --git a/python/paddle/jit/dy2static/call_transformer.py b/python/paddle/jit/dy2static/call_transformer.py index 7380934d47e16aca5da901c6b5a1e3afaba622ec..11f0f6624e8fede00dcfb74e07d847b2cd6763fa 100644 --- a/python/paddle/jit/dy2static/call_transformer.py +++ b/python/paddle/jit/dy2static/call_transformer.py @@ -14,11 +14,11 @@ from paddle.utils import gast -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( +from paddle.jit.dy2static.static_analysis import ( AstNodeWrapper, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_source_code -from paddle.fluid.dygraph.dygraph_to_static.utils import is_paddle_api +from paddle.jit.dy2static.utils import ast_to_source_code +from paddle.jit.dy2static.utils import is_paddle_api from .base_transformer import ( BaseTransformer, ) diff --git a/python/paddle/jit/dy2static/cast_transformer.py b/python/paddle/jit/dy2static/cast_transformer.py index ca1bf11c43897f844532a66169437d00eece8d2c..96a504d70cdb582fd3f5ff92d97775e9000a9103 100644 --- a/python/paddle/jit/dy2static/cast_transformer.py +++ b/python/paddle/jit/dy2static/cast_transformer.py @@ -14,10 +14,10 @@ from paddle.utils import gast -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( +from paddle.jit.dy2static.static_analysis import ( AstNodeWrapper, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_source_code +from paddle.jit.dy2static.utils import ast_to_source_code from .base_transformer import ( BaseTransformer, ) diff --git a/python/paddle/jit/dy2static/convert_call_func.py b/python/paddle/jit/dy2static/convert_call_func.py index 325ffb206011fda1de841abf9236a847bcf80437..e0f393028cfac4c10df008465fdf83970161c58e 100644 --- a/python/paddle/jit/dy2static/convert_call_func.py +++ b/python/paddle/jit/dy2static/convert_call_func.py @@ -32,11 +32,11 @@ from .convert_operators import ( convert_enumerate, ) -from paddle.fluid.dygraph.dygraph_to_static.logging_utils import ( +from paddle.jit.dy2static.logging_utils import ( TranslatorLogger, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import is_paddle_func, unwrap +from paddle.jit.dy2static.utils import is_paddle_func, unwrap from paddle.fluid.dygraph.layers import Layer __all__ = ["convert_call"] diff --git a/python/paddle/jit/dy2static/convert_operators.py b/python/paddle/jit/dy2static/convert_operators.py index e5b35d0b4c0d11fb5c8d721e20f06c8235a0926d..3643da7591f4f4cff462848f23551ceb06e3c6bb 100644 --- a/python/paddle/jit/dy2static/convert_operators.py +++ b/python/paddle/jit/dy2static/convert_operators.py @@ -15,7 +15,7 @@ import re import paddle from paddle.fluid.data_feeder import convert_dtype -from paddle.fluid.dygraph.dygraph_to_static.variable_trans_func import ( +from paddle.jit.dy2static.variable_trans_func import ( to_static_variable, ) from paddle.fluid.framework import core, Variable @@ -46,11 +46,11 @@ from paddle.fluid.layers.control_flow import ( from .return_transformer import ( RETURN_NO_VALUE_VAR_NAME, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ( +from paddle.jit.dy2static.utils import ( UndefinedVar, Dygraph2StaticException, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import GetterSetterHelper +from paddle.jit.dy2static.utils import GetterSetterHelper from paddle.fluid.layers.utils import copy_mutable_vars diff --git a/python/paddle/jit/dy2static/create_variable_transformer.py b/python/paddle/jit/dy2static/create_variable_transformer.py index feccbfe594e137972dca940b2bd1c60b6464cdcf..808a047c3a283802aecb1d8ad479555e947253ee 100644 --- a/python/paddle/jit/dy2static/create_variable_transformer.py +++ b/python/paddle/jit/dy2static/create_variable_transformer.py @@ -12,13 +12,13 @@ # See the License for the specific language governing permissions and # limitations under the License. -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( +from paddle.jit.dy2static.static_analysis import ( AstNodeWrapper, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ( +from paddle.jit.dy2static.utils import ( FunctionNameLivenessAnalysis, ) -from paddle.fluid.dygraph.dygraph_to_static.variable_trans_func import ( +from paddle.jit.dy2static.variable_trans_func import ( create_undefined_var, ) from .base_transformer import ( diff --git a/python/paddle/jit/dy2static/decorator_transformer.py b/python/paddle/jit/dy2static/decorator_transformer.py index f802db72de3bc730c1b14156ad38e264b0eae79d..cd33f8625a3b07813d5cc09973470c5a05667e69 100644 --- a/python/paddle/jit/dy2static/decorator_transformer.py +++ b/python/paddle/jit/dy2static/decorator_transformer.py @@ -14,13 +14,13 @@ # limitations under the License. from paddle.utils import gast -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( +from paddle.jit.dy2static.static_analysis import ( AstNodeWrapper, ) from .base_transformer import ( BaseTransformer, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ( +from paddle.jit.dy2static.utils import ( RE_PYNAME, RE_PYMODULE, ast_to_source_code, diff --git a/python/paddle/jit/dy2static/early_return_transformer.py b/python/paddle/jit/dy2static/early_return_transformer.py index 53bb2394e80373af240423b2ed48e9bb2ca8fe5b..72076fb3cd8839bbb49f41e874ade96e27b99780 100644 --- a/python/paddle/jit/dy2static/early_return_transformer.py +++ b/python/paddle/jit/dy2static/early_return_transformer.py @@ -13,7 +13,7 @@ # limitations under the License. from paddle.utils import gast -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( +from paddle.jit.dy2static.static_analysis import ( AstNodeWrapper, ) from .base_transformer import ( diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/error.py b/python/paddle/jit/dy2static/error.py similarity index 98% rename from python/paddle/fluid/dygraph/dygraph_to_static/error.py rename to python/paddle/jit/dy2static/error.py index f4a66100807fda87f46084c6d2b63e7f9ae41906..40f5b16def610e2aa3fbbd415135514d31383e2c 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/error.py +++ b/python/paddle/jit/dy2static/error.py @@ -17,17 +17,18 @@ import sys import traceback import linecache import re -import numpy as np +import numpy as np # noqa: F401 -from paddle.fluid.dygraph.dygraph_to_static.origin_info import ( +from .origin_info import ( Location, OriginInfo, global_origin_info_map, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ( - _is_api_in_module_helper, - RE_PYMODULE, -) +from .utils import _is_api_in_module_helper # noqa: F401 +from .utils import RE_PYMODULE + + +__all__ = [] ERROR_DATA = "Error data about original source code information and traceback." diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/function_spec.py b/python/paddle/jit/dy2static/function_spec.py similarity index 97% rename from python/paddle/fluid/dygraph/dygraph_to_static/function_spec.py rename to python/paddle/jit/dy2static/function_spec.py index cc77e05dad1174f43ee59af9f8d010d117d893be..370fb36bcfb91f41bbda1dd3382b227141d439d6 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/function_spec.py +++ b/python/paddle/jit/dy2static/function_spec.py @@ -22,13 +22,16 @@ from paddle.fluid.dygraph import layers from paddle.fluid.layers.utils import flatten from paddle.fluid.layers.utils import pack_sequence_as from paddle.fluid.dygraph.base import switch_to_static_graph -from paddle.fluid.dygraph.dygraph_to_static import logging_utils -from paddle.fluid.dygraph.dygraph_to_static.utils import parse_arg_and_kwargs -from paddle.fluid.dygraph.dygraph_to_static.utils import parse_varargs_name -from paddle.fluid.dygraph.dygraph_to_static.utils import type_name -from paddle.fluid.dygraph.dygraph_to_static.utils import func_to_source_code from paddle.fluid.dygraph.io import TranslatedLayer +from . import logging_utils +from .utils import ( + parse_arg_and_kwargs, + parse_varargs_name, + type_name, + func_to_source_code, +) + class FunctionSpec: """ diff --git a/python/paddle/jit/dy2static/ifelse_transformer.py b/python/paddle/jit/dy2static/ifelse_transformer.py index 8bae82c11a16fd61b7309537d28e8b1194107014..8759e742cef47be4385beb21c4128e6eeefcae4f 100644 --- a/python/paddle/jit/dy2static/ifelse_transformer.py +++ b/python/paddle/jit/dy2static/ifelse_transformer.py @@ -22,27 +22,27 @@ from collections import defaultdict from paddle.utils import gast from paddle.fluid import unique_name -from paddle.fluid.dygraph.dygraph_to_static.utils import ( +from paddle.jit.dy2static.utils import ( create_funcDef_node, ast_to_source_code, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ( +from paddle.jit.dy2static.utils import ( FunctionNameLivenessAnalysis, ) -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( +from paddle.jit.dy2static.static_analysis import ( AstNodeWrapper, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ( +from paddle.jit.dy2static.utils import ( create_nonlocal_stmt_nodes, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ( +from paddle.jit.dy2static.utils import ( create_get_args_node, create_set_args_node, ) from .base_transformer import ( BaseTransformer, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ( +from paddle.jit.dy2static.utils import ( FOR_ITER_INDEX_PREFIX, FOR_ITER_TUPLE_PREFIX, FOR_ITER_TUPLE_INDEX_PREFIX, @@ -52,7 +52,7 @@ from paddle.fluid.dygraph.dygraph_to_static.utils import ( FOR_ITER_TARGET_PREFIX, FOR_ITER_ITERATOR_PREFIX, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ( +from paddle.jit.dy2static.utils import ( GetterSetterHelper, create_name_str, ) diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/logging_utils.py b/python/paddle/jit/dy2static/logging_utils.py similarity index 99% rename from python/paddle/fluid/dygraph/dygraph_to_static/logging_utils.py rename to python/paddle/jit/dy2static/logging_utils.py index 7b004964a472ddf65e13b3fb3e06044202c2a9a5..ca36cb0b35aa5e3d49ce9649af828ab200273797 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/logging_utils.py +++ b/python/paddle/jit/dy2static/logging_utils.py @@ -16,7 +16,7 @@ import os import threading from paddle.fluid import log_helper -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_source_code +from .utils import ast_to_source_code __all__ = ["TranslatorLogger", "set_verbosity", "set_code_level"] diff --git a/python/paddle/jit/dy2static/logical_transformer.py b/python/paddle/jit/dy2static/logical_transformer.py index 1ab4491d8d9412f79e167095800e7937d1caabd6..cd4d27ac73e57919f2d474432e7edb9e0824d042 100644 --- a/python/paddle/jit/dy2static/logical_transformer.py +++ b/python/paddle/jit/dy2static/logical_transformer.py @@ -13,7 +13,7 @@ # limitations under the License. from paddle.utils import gast -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_source_code +from paddle.jit.dy2static.utils import ast_to_source_code from .base_transformer import ( BaseTransformer, ) diff --git a/python/paddle/jit/dy2static/loop_transformer.py b/python/paddle/jit/dy2static/loop_transformer.py index 7d42638b9e0f6bf9fbc67f232ed045e169a4a744..4bb99e830b35016d57fcafb0a1bc23bdede017c8 100644 --- a/python/paddle/jit/dy2static/loop_transformer.py +++ b/python/paddle/jit/dy2static/loop_transformer.py @@ -17,21 +17,21 @@ from paddle.utils import gast from collections import defaultdict from paddle.fluid import unique_name -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( +from paddle.jit.dy2static.static_analysis import ( AstNodeWrapper, ) -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import NodeVarType -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( +from paddle.jit.dy2static.static_analysis import NodeVarType +from paddle.jit.dy2static.static_analysis import ( StaticAnalysisVisitor, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_source_code -from paddle.fluid.dygraph.dygraph_to_static.utils import get_attribute_full_name -from paddle.fluid.dygraph.dygraph_to_static.utils import ( +from paddle.jit.dy2static.utils import ast_to_source_code +from paddle.jit.dy2static.utils import get_attribute_full_name +from paddle.jit.dy2static.utils import ( create_nonlocal_stmt_nodes, create_get_args_node, create_set_args_node, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ( +from paddle.jit.dy2static.utils import ( FunctionNameLivenessAnalysis, ) from .ifelse_transformer import ARGS_NAME @@ -41,7 +41,7 @@ from .base_transformer import ( ForNodeVisitor, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ( +from paddle.jit.dy2static.utils import ( GetterSetterHelper, create_name_str, ) diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/origin_info.py b/python/paddle/jit/dy2static/origin_info.py similarity index 98% rename from python/paddle/fluid/dygraph/dygraph_to_static/origin_info.py rename to python/paddle/jit/dy2static/origin_info.py index 7eb9da1206439d7d86c6b0f62942ff81e38d6d06..7f3c7f719a53959a612fcf27d25c6d3b0da25b87 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/origin_info.py +++ b/python/paddle/jit/dy2static/origin_info.py @@ -12,13 +12,14 @@ # See the License for the specific language governing permissions and # limitations under the License. -import collections import inspect from paddle.utils import gast from paddle.fluid import core -from paddle.fluid.dygraph.dygraph_to_static.utils import unwrap -from paddle.fluid.dygraph.dygraph_to_static.utils import ORIGI_INFO +from .utils import ( + unwrap, + ORIGI_INFO, +) from paddle.fluid.framework import Program from collections.abc import Sequence diff --git a/python/paddle/jit/dy2static/partial_program.py b/python/paddle/jit/dy2static/partial_program.py index ad5afaff7cdd51542d4664ff5db6907fd13a6f11..d1ebdbe5ccc57feb9a147e3dcc408bace103e96f 100644 --- a/python/paddle/jit/dy2static/partial_program.py +++ b/python/paddle/jit/dy2static/partial_program.py @@ -22,7 +22,7 @@ from paddle.fluid.executor import ( ) from paddle.fluid.dygraph import layers from paddle.fluid.dygraph.base import switch_to_static_graph -from paddle.fluid.dygraph.dygraph_to_static import logging_utils +from . import logging_utils from .return_transformer import ( RETURN_NO_VALUE_MAGIC_NUM, ) diff --git a/python/paddle/jit/dy2static/print_transformer.py b/python/paddle/jit/dy2static/print_transformer.py index cd4f13f019bc8ff704e75e8abb6d005dad5ef474..aa4bc2c219bbcec120346e0e0c78c656e3a68aa0 100644 --- a/python/paddle/jit/dy2static/print_transformer.py +++ b/python/paddle/jit/dy2static/print_transformer.py @@ -14,7 +14,7 @@ from paddle.utils import gast -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( +from paddle.jit.dy2static.static_analysis import ( AstNodeWrapper, StaticAnalysisVisitor, ) diff --git a/python/paddle/jit/dy2static/program_translator.py b/python/paddle/jit/dy2static/program_translator.py index 0e912954ae6950f9e2ba3a4edddb82c79fccb181..37a85fc078bc9ab0291cba374a31c0e099df7449 100644 --- a/python/paddle/jit/dy2static/program_translator.py +++ b/python/paddle/jit/dy2static/program_translator.py @@ -26,39 +26,35 @@ from paddle.fluid.data_feeder import check_type from paddle.fluid.layers.utils import flatten from paddle.fluid.dygraph.base import param_guard from paddle.fluid.dygraph.base import switch_to_static_graph -from paddle.fluid.dygraph.dygraph_to_static import error -from paddle.fluid.dygraph.dygraph_to_static import logging_utils -from paddle.fluid.dygraph.dygraph_to_static.origin_info import ( +from . import error +from . import logging_utils +from .origin_info import ( attach_origin_info, -) -from paddle.fluid.dygraph.dygraph_to_static.origin_info import ( create_and_update_origin_info_map, -) -from paddle.fluid.dygraph.dygraph_to_static.origin_info import ( update_op_callstack_with_origin_info, ) + from .partial_program import ( partial_program_from, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_func -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_source_code -from paddle.fluid.dygraph.dygraph_to_static.utils import func_to_source_code -from paddle.fluid.dygraph.dygraph_to_static.utils import input_specs_compatible -from paddle.fluid.dygraph.dygraph_to_static.utils import type_name -from paddle.fluid.dygraph.dygraph_to_static.utils import unwrap -from paddle.fluid.dygraph.dygraph_to_static.utils import ( +from .utils import ( + ast_to_func, + ast_to_source_code, + func_to_source_code, + input_specs_compatible, + type_name, + unwrap, make_hashable, ALREADY_D2S, ) -from paddle.fluid.dygraph.dygraph_to_static.function_spec import ( +from .function_spec import ( FunctionSpec, _hash_spec_names, -) -from paddle.fluid.dygraph.dygraph_to_static.function_spec import ( get_buffers, get_parameters, ) + from .ast_transformer import DygraphToStaticAst __all__ = ['ProgramTranslator', 'convert_to_static'] diff --git a/python/paddle/jit/dy2static/return_transformer.py b/python/paddle/jit/dy2static/return_transformer.py index c8114e078d592e1365dc75dd0d1754a83101e1ec..8aa96b615784686355eb740a7b957a60ad5f78e0 100644 --- a/python/paddle/jit/dy2static/return_transformer.py +++ b/python/paddle/jit/dy2static/return_transformer.py @@ -15,16 +15,16 @@ from paddle.utils import gast from paddle.fluid import unique_name -from paddle.fluid.dygraph.dygraph_to_static.utils import index_in_list +from paddle.jit.dy2static.utils import index_in_list from .break_continue_transformer import ( ForToWhileTransformer, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_source_code +from paddle.jit.dy2static.utils import ast_to_source_code from .base_transformer import ( BaseTransformer, ) -from paddle.fluid.dygraph.dygraph_to_static.utils import Dygraph2StaticException -from paddle.fluid.dygraph.dygraph_to_static.utils import ORIGI_INFO +from paddle.jit.dy2static.utils import Dygraph2StaticException +from paddle.jit.dy2static.utils import ORIGI_INFO __all__ = [ 'RETURN_NO_VALUE_MAGIC_NUM', diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/static_analysis.py b/python/paddle/jit/dy2static/static_analysis.py similarity index 100% rename from python/paddle/fluid/dygraph/dygraph_to_static/static_analysis.py rename to python/paddle/jit/dy2static/static_analysis.py diff --git a/python/paddle/jit/dy2static/tensor_shape_transformer.py b/python/paddle/jit/dy2static/tensor_shape_transformer.py index 9dae08b123eebc02da599602b948c4a8db93b743..ffdba7e790a77cdd9048b4bd250fe25f29d15f5b 100644 --- a/python/paddle/jit/dy2static/tensor_shape_transformer.py +++ b/python/paddle/jit/dy2static/tensor_shape_transformer.py @@ -14,8 +14,8 @@ from paddle.utils import gast -from paddle.fluid.dygraph.dygraph_to_static.utils import ast_to_source_code -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( +from paddle.jit.dy2static.utils import ast_to_source_code +from paddle.jit.dy2static.static_analysis import ( AstNodeWrapper, ) from .base_transformer import ( diff --git a/python/paddle/jit/dy2static/typehint_transformer.py b/python/paddle/jit/dy2static/typehint_transformer.py index dd272f76477f47c57ea41cac6991909d90f0c7ef..d5c23d1d7d73c1800430e951d5de21902f5b9012 100644 --- a/python/paddle/jit/dy2static/typehint_transformer.py +++ b/python/paddle/jit/dy2static/typehint_transformer.py @@ -13,7 +13,7 @@ # limitations under the License. -from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( +from paddle.jit.dy2static.static_analysis import ( AstNodeWrapper, ) from .base_transformer import ( diff --git a/python/paddle/fluid/dygraph/dygraph_to_static/utils.py b/python/paddle/jit/dy2static/utils.py similarity index 98% rename from python/paddle/fluid/dygraph/dygraph_to_static/utils.py rename to python/paddle/jit/dy2static/utils.py index 23579720f3804c9dd2fa35b893618a0f058dea29..a57134411ffec3dcabdfafab31a2243ea0b6695e 100644 --- a/python/paddle/fluid/dygraph/dygraph_to_static/utils.py +++ b/python/paddle/jit/dy2static/utils.py @@ -16,7 +16,6 @@ import ast import astor import atexit import copy -import collections from paddle.utils import gast import inspect import os @@ -32,15 +31,17 @@ from paddle.fluid.data_feeder import convert_dtype from paddle.fluid import core from paddle.fluid.layer_helper import LayerHelper from paddle.fluid.layers import assign -import collections from functools import reduce import warnings + +__all__ = [] + # Note(Aurelius): Do not forget the dot `.` to distinguish other # module such as paddlenlp. PADDLE_MODULE_PREFIX = 'paddle.' DYGRAPH_MODULE_PREFIX = 'paddle.fluid.dygraph' -DYGRAPH_TO_STATIC_MODULE_PREFIX = 'paddle.fluid.dygraph.dygraph_to_static' +DYGRAPH_TO_STATIC_MODULE_PREFIX = 'paddle.jit.dy2static' GET_ARGS_FUNC_PREFIX = 'get_args' SET_ARGS_FUNC_PREFIX = 'set_args' ALREADY_D2S = '__already_d2s' @@ -258,19 +259,13 @@ def is_api_in_module(node, module_prefix): func_str = astor.to_source(gast.gast_to_ast(func_node)).strip() try: - # TODO(liym27): - # Consider a better to import modules like: - # source_file = inspect.getfile(dyfunc) - # import_statements = ImportVisitor(source_file).transform() - # import_str = "".join(import_statements) - import paddle - import paddle.fluid as fluid - import paddle.fluid.dygraph as dygraph - import paddle.fluid.layers as layers - import paddle.jit.dy2static as _jst - - from paddle.fluid.dygraph import to_variable - from paddle import to_tensor + import paddle # noqa: F401 + import paddle.fluid as fluid # noqa: F401 + import paddle.fluid.dygraph as dygraph # noqa: F401 + import paddle.fluid.layers as layers # noqa: F401 + import paddle.jit.dy2static as _jst # noqa: F401 + from paddle.fluid.dygraph import to_variable # noqa: F401 + from paddle import to_tensor # noqa: F401 return eval( "_is_api_in_module_helper({}, '{}')".format(func_str, module_prefix) @@ -304,7 +299,7 @@ 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 + import numpy as np # noqa: F401 module_result = eval( "_is_api_in_module_helper({}, '{}')".format(func_str, "numpy") @@ -321,7 +316,7 @@ def is_numpy_api(node): def _delete_keywords_from(node): assert isinstance(node, gast.Call) func_src = astor.to_source(gast.gast_to_ast(node.func)) - import paddle.fluid as fluid + import paddle.fluid as fluid # noqa: F401 full_args = eval(f"inspect.getfullargspec({func_src})") full_args_name = full_args[0] @@ -402,7 +397,7 @@ def update_args_of_func(node, dygraph_node, method_name): ) class_src = astor.to_source(gast.gast_to_ast(dygraph_node.func)) - import paddle.fluid as fluid + import paddle.fluid as fluid # noqa: F401 if method_name == "__init__" or eval( "issubclass({}, fluid.dygraph.Layer)".format(class_src) @@ -894,7 +889,7 @@ class IsControlFlowVisitor(gast.NodeVisitor): return node def _is_node_with_tensor(self, node, name_id): - from paddle.fluid.dygraph.dygraph_to_static.static_analysis import ( + from paddle.jit.dy2static.static_analysis import ( NodeVarType, ) @@ -1213,7 +1208,6 @@ class FunctionNameLivenessAnalysis(gast.NodeVisitor): because we do ifelse_transformer after loop_transformer. Loops will changed into functioons. but we know this function will be called in if. so we add w_vars to father function scope. """ from paddle.jit.dy2static.loop_transformer import ( - WHILE_CONDITION_PREFIX, WHILE_BODY_PREFIX, FOR_CONDITION_PREFIX, FOR_BODY_PREFIX, diff --git a/python/paddle/jit/dy2static/variable_trans_func.py b/python/paddle/jit/dy2static/variable_trans_func.py index 88f8cd3c2c464f7356f6f50c9ab554a49b810ba5..c98823242b40a091b917b5426e473df8080bf8e0 100644 --- a/python/paddle/jit/dy2static/variable_trans_func.py +++ b/python/paddle/jit/dy2static/variable_trans_func.py @@ -12,9 +12,82 @@ # See the License for the specific language governing permissions and # limitations under the License. -from ...fluid.dygraph.dygraph_to_static.variable_trans_func import ( # noqa: F401 - create_bool_as_type, - to_static_variable, +import paddle +from paddle.utils import gast +from paddle.fluid.framework import Variable +from paddle.jit.dy2static.utils import ( + UndefinedVar, + create_undefined_variable, ) +from paddle.fluid.layers.utils import map_structure, is_sequence -__all__ = [] +__all__ = [ + 'create_bool_as_type', + 'create_fill_constant_node', + 'to_static_variable', + 'create_undefined_var', +] + + +def create_undefined_var(name): + func_code = "{} = _jst.UndefinedVar('{}')".format(name, name) + return gast.parse(func_code).body[0] + + +def create_fill_constant_node(name, value=0): + func_code = "{} = paddle.full(shape=[1], ".format(name) + if isinstance(value, bool): + func_code += "dtype='bool', fill_value={}, name='{}')".format( + value, name + ) + return gast.parse(func_code).body[0] + if isinstance(value, float): + func_code += "dtype='float64', fill_value={}, name='{}')".format( + value, name + ) + return gast.parse(func_code).body[0] + + if isinstance(value, int): + func_code += "dtype='int64', fill_value={}, name='{}')".format( + value, name + ) + return gast.parse(func_code).body[0] + + +def to_static_variable(x): + ''' + Translate a Python Tensor to PaddlePaddle static graph Tensor + ''' + if isinstance(x, bool): + return paddle.full(shape=[1], dtype='bool', fill_value=x) + if isinstance(x, float): + return paddle.full(shape=[1], dtype='float64', fill_value=x) + if isinstance(x, int): + return paddle.full(shape=[1], dtype='int64', fill_value=x) + if isinstance(x, UndefinedVar) or x is None: + """ + for early return case, we need a variable to represent None, current we use data_layer_not_check. + """ + return create_undefined_variable() + if is_sequence(x): + return map_structure(to_static_variable, x) + return x + + +def create_bool_as_type(x, value=True): + ''' + Create a bool variable, which type is the same as x. + ''' + if isinstance(x, Variable): + return paddle.full(shape=[1], fill_value=value, dtype="bool") + else: + return value + + +def create_bool_node(name, value): + ''' + Create a assign stmt for name = value . + ''' + assert isinstance(value, bool) + node = "{} = {}".format(name, value) + return gast.parse(node).body[0] diff --git a/python/setup.py.in b/python/setup.py.in index cab42d8f3613baf4970382e3bae86283f48d2775..faa994d744747a91b105947301e15c69c625094a 100755 --- a/python/setup.py.in +++ b/python/setup.py.in @@ -331,7 +331,6 @@ packages=['paddle', 'paddle.inference.contrib.utils', 'paddle.fluid', 'paddle.fluid.dygraph', - 'paddle.fluid.dygraph.dygraph_to_static', 'paddle.fluid.dygraph.amp', 'paddle.fluid.proto', 'paddle.fluid.proto.profiler',