未验证 提交 ea9e4085 编写于 作者: A Aurelius84 提交者: GitHub

[API Clean]Clean __all__ to avoid exposing usless API (#48713)

* [API Clean]Clean __all__ to avoid exposing usless API

* fix import

* fix typo

* remove tracedLayer unittest
上级 b91bbd32
......@@ -48,7 +48,6 @@ class TestDirectory(unittest.TestCase):
'paddle.distributed.ParallelEnv',
'paddle.DataParallel',
'paddle.jit',
'paddle.jit.TracedLayer',
'paddle.jit.to_static',
'paddle.jit.ProgramTranslator',
'paddle.jit.TranslatedLayer',
......
......@@ -21,7 +21,7 @@ from utils import DyGraphProgramDescTracerTestHelper
import paddle
import paddle.fluid as fluid
from paddle.fluid import core
from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard
from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.optimizer import SGDOptimizer
from paddle.nn import Linear
......@@ -153,19 +153,7 @@ class TestImperativeMnist(unittest.TestCase):
dy_x_data = img.numpy()
label = data[1]
label.stop_gradient = True
if batch_id % 10 == 0 and _in_legacy_dygraph():
cost, traced_layer = paddle.jit.TracedLayer.trace(
mnist, inputs=img
)
if program is not None:
self.assertTrue(program, traced_layer.program)
program = traced_layer.program
traced_layer.save_inference_model(
'./infer_imperative_mnist'
)
else:
cost = mnist(img)
cost = mnist(img)
if traced_layer is not None:
cost_static = traced_layer([img])
......
......@@ -16,7 +16,7 @@ import unittest
import numpy as np
from test_imperative_base import new_program_scope
from utils import DyGraphProgramDescTracerTestHelper, is_equal_program
from utils import DyGraphProgramDescTracerTestHelper
import paddle
import paddle.fluid as fluid
......@@ -24,9 +24,8 @@ import paddle.fluid.core as core
import paddle.fluid.framework as framework
from paddle.fluid.dygraph.base import to_variable
from paddle.fluid.dygraph.nn import Embedding
from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard
from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.optimizer import SGDOptimizer
from paddle.jit import TracedLayer
class SimpleLSTMRNN(fluid.Layer):
......@@ -298,25 +297,8 @@ class TestDygraphPtbRnn(unittest.TestCase):
y = to_variable(y_data)
init_hidden = to_variable(init_hidden_data)
init_cell = to_variable(init_cell_data)
if i % 5 == 0 and _in_legacy_dygraph():
outs, traced_layer = TracedLayer.trace(
ptb_model, [x, y, init_hidden, init_cell]
)
outs_static = traced_layer([x, y, init_hidden, init_cell])
helper.assertEachVar(outs, outs_static)
if program is not None:
self.assertTrue(
is_equal_program(traced_layer.program, program)
)
program = traced_layer.program
traced_layer.save_inference_model(
'./infe_imperative_ptb_rnn', feed=list(range(4))
)
else:
outs = ptb_model(x, y, init_hidden, init_cell)
outs = ptb_model(x, y, init_hidden, init_cell)
dy_loss, last_hidden, last_cell = outs
......
......@@ -16,15 +16,14 @@ import unittest
import numpy as np
from test_imperative_base import new_program_scope
from utils import DyGraphProgramDescTracerTestHelper, is_equal_program
from utils import DyGraphProgramDescTracerTestHelper
import paddle
import paddle.fluid as fluid
from paddle.fluid import BatchNorm, core
from paddle.fluid.dygraph.base import to_variable
from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard
from paddle.fluid.framework import _test_eager_guard
from paddle.fluid.layer_helper import LayerHelper
from paddle.jit import TracedLayer
# NOTE(zhiqiu): run with FLAGS_cudnn_deterministic=1
......@@ -301,20 +300,7 @@ class TestDygraphResnet(unittest.TestCase):
label.stop_gradient = True
out = None
if batch_id % 5 == 0 and _in_legacy_dygraph():
out, traced_layer = TracedLayer.trace(resnet, img)
if program is not None:
self.assertTrue(
is_equal_program(program, traced_layer.program)
)
traced_layer.save_inference_model(
'./infer_imperative_resnet'
)
program = traced_layer.program
else:
out = resnet(img)
out = resnet(img)
if traced_layer is not None:
resnet.eval()
......
......@@ -23,12 +23,11 @@ import paddle.nn.functional as F
from paddle.fluid import Embedding, Layer, core
from paddle.fluid.dygraph import guard, to_variable
from paddle.fluid.framework import _in_legacy_dygraph, _test_eager_guard
from paddle.jit import TracedLayer
from paddle.nn import Linear
np.set_printoptions(suppress=True)
from utils import DyGraphProgramDescTracerTestHelper, is_equal_program
from utils import DyGraphProgramDescTracerTestHelper
# Copy from models
......@@ -1171,27 +1170,7 @@ class TestDygraphTransformerSortGradient(unittest.TestCase):
for i in range(batch_num):
enc_inputs, dec_inputs, label, weights = create_data()
if False:
outs, traced_layer = TracedLayer.trace(
transformer, [enc_inputs, dec_inputs, label, weights]
)
ins_static = enc_inputs + dec_inputs + [label, weights]
outs_static = traced_layer(ins_static)
helper.assertEachVar(outs, outs_static)
if program is not None:
self.assertTrue(
is_equal_program(program, traced_layer.program)
)
program = traced_layer.program
traced_layer.save_inference_model(
'./infer_imperative_transformer',
feed=list(range(len(ins_static))),
fetch=list(range(len(outs_static))),
)
else:
outs = transformer(enc_inputs, dec_inputs, label, weights)
outs = transformer(enc_inputs, dec_inputs, label, weights)
dy_sum_cost, dy_avg_cost, dy_predict, dy_token_num = outs
......
......@@ -20,8 +20,6 @@ import paddle
import paddle.fluid as fluid
import paddle.fluid.layers as layers
from paddle import _legacy_C_ops
from paddle.fluid.framework import in_dygraph_mode
from paddle.jit.api import TracedLayer
class TestTracedLayer(fluid.dygraph.Layer):
......@@ -93,20 +91,6 @@ class TestVariable(unittest.TestCase):
np.testing.assert_array_equal(y_grad, loss.gradient() * a)
fluid.set_flags({"FLAGS_retain_grad_for_all_tensor": False})
def test_traced_layer(self):
if in_dygraph_mode():
return
with fluid.dygraph.guard():
layer = TestTracedLayer("test_traced_layer")
a = np.random.uniform(-1, 1, self.shape).astype(self.dtype)
x = fluid.dygraph.to_variable(a)
res_dygraph, static_layer = TracedLayer.trace(
layer, inputs=x
) # dygraph out
res_static_graph = static_layer([x])[0]
np.testing.assert_array_equal(res_dygraph.numpy(), res_static_graph)
if __name__ == '__main__':
unittest.main()
......@@ -15,7 +15,6 @@
from .api import save
from .api import load
from .api import TracedLayer
from .api import set_code_level
from .api import set_verbosity
from .api import declarative as to_static
......@@ -34,5 +33,4 @@ __all__ = [ # noqa
'set_code_level',
'set_verbosity',
'not_to_static',
'TracedLayer',
]
......@@ -74,9 +74,7 @@ from paddle.fluid.framework import dygraph_only, _non_static_mode
from paddle.fluid.wrapped_decorator import wrap_decorator
__all__ = [
'TracedLayer',
'declarative',
'dygraph_to_static_func',
'set_code_level',
'set_verbosity',
'save',
......
......@@ -36,6 +36,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
from .static_analysis import AstNodeWrapper, NodeVarType, StaticAnalysisVisitor
__all__ = []
......@@ -22,7 +22,7 @@ from .base_transformer import (
BaseTransformer,
)
__all__ = ['AssertTransformer']
__all__ = []
class AssertTransformer(BaseTransformer):
......
......@@ -71,7 +71,7 @@ from .decorator_transformer import (
from . import logging_utils
from .utils import ast_to_source_code
__all__ = ['DygraphToStaticAst']
__all__ = []
def apply_optimization(transformers):
......
......@@ -27,6 +27,8 @@ from paddle.jit.dy2static.utils import (
get_attribute_full_name,
)
__all__ = []
class BaseTransformer(gast.NodeTransformer):
def visit(self, node):
......
......@@ -23,6 +23,8 @@ from .base_transformer import (
BaseTransformer,
)
__all__ = []
class BasicApiTransformer(BaseTransformer):
"""
......
......@@ -27,7 +27,7 @@ from .base_transformer import (
ForNodeVisitor,
)
__all__ = ['BreakContinueTransformer']
__all__ = []
BREAK_NAME_PREFIX = '__break'
CONTINUE_NAME_PREFIX = '__continue'
......
......@@ -25,6 +25,8 @@ from .base_transformer import (
PDB_SET = "pdb.set_trace"
__all__ = []
class CallTransformer(BaseTransformer):
"""
......
......@@ -22,6 +22,8 @@ from .base_transformer import (
BaseTransformer,
)
__all__ = []
class CastTransformer(BaseTransformer):
"""
......
......@@ -40,7 +40,7 @@ from paddle.jit.dy2static.logging_utils import (
from paddle.jit.dy2static.utils import is_paddle_func, unwrap
from paddle.fluid.dygraph.layers import Layer
__all__ = ["convert_call"]
__all__ = []
# The api(s) should be considered as plain function and convert
......
......@@ -15,7 +15,7 @@
import re
import paddle
from paddle.fluid.data_feeder import convert_dtype
from paddle.jit.dy2static.variable_trans_func import (
from .variable_trans_func import (
to_static_variable,
)
from paddle.fluid.framework import core, Variable
......@@ -43,10 +43,13 @@ from .return_transformer import (
from paddle.jit.dy2static.utils import (
UndefinedVar,
Dygraph2StaticException,
GetterSetterHelper,
)
from paddle.jit.dy2static.utils import GetterSetterHelper
from paddle.fluid.layers.utils import copy_mutable_vars
__all__ = []
def convert_attr(x, attr):
if isinstance(x, Variable) and attr == "size":
......
......@@ -12,13 +12,13 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from paddle.jit.dy2static.static_analysis import (
from .static_analysis import (
AstNodeWrapper,
)
from paddle.jit.dy2static.utils import (
from .utils import (
FunctionNameLivenessAnalysis,
)
from paddle.jit.dy2static.variable_trans_func import (
from .variable_trans_func import (
create_undefined_var,
)
from .base_transformer import (
......@@ -26,6 +26,9 @@ from .base_transformer import (
)
__all__ = []
class CreateVariableTransformer(BaseTransformer):
""" """
......
......@@ -14,13 +14,13 @@
# limitations under the License.
from paddle.utils import gast
from paddle.jit.dy2static.static_analysis import (
from .static_analysis import (
AstNodeWrapper,
)
from .base_transformer import (
BaseTransformer,
)
from paddle.jit.dy2static.utils import (
from .utils import (
RE_PYNAME,
RE_PYMODULE,
ast_to_source_code,
......@@ -29,6 +29,8 @@ import warnings
import re
__all__ = []
IGNORE_NAMES = [
'declarative',
'to_static',
......
......@@ -13,14 +13,14 @@
# limitations under the License.
from paddle.utils import gast
from paddle.jit.dy2static.static_analysis import (
from .static_analysis import (
AstNodeWrapper,
)
from .base_transformer import (
BaseTransformer,
)
__all__ = ['EarlyReturnTransformer']
__all__ = []
class EarlyReturnTransformer(BaseTransformer):
......
......@@ -32,6 +32,8 @@ from .utils import (
func_to_source_code,
)
__all__ = []
class FunctionSpec:
"""
......
......@@ -18,7 +18,7 @@ import threading
from paddle.fluid import log_helper
from .utils import ast_to_source_code
__all__ = ["TranslatorLogger", "set_verbosity", "set_code_level"]
__all__ = []
VERBOSITY_ENV_NAME = 'TRANSLATOR_VERBOSITY'
CODE_LEVEL_ENV_NAME = 'TRANSLATOR_CODE_LEVEL'
......
......@@ -13,11 +13,13 @@
# limitations under the License.
from paddle.utils import gast
from paddle.jit.dy2static.utils import ast_to_source_code
from .utils import ast_to_source_code
from .base_transformer import (
BaseTransformer,
)
__all__ = []
cmpop_type_to_str = {
gast.Eq: "==",
gast.NotEq: "!=",
......
......@@ -17,22 +17,16 @@ from paddle.utils import gast
from collections import defaultdict
from paddle.fluid import unique_name
from paddle.jit.dy2static.static_analysis import (
AstNodeWrapper,
)
from paddle.jit.dy2static.static_analysis import NodeVarType
from paddle.jit.dy2static.static_analysis import (
StaticAnalysisVisitor,
)
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 (
from .static_analysis import AstNodeWrapper, NodeVarType, StaticAnalysisVisitor
from .utils import (
ast_to_source_code,
get_attribute_full_name,
create_nonlocal_stmt_nodes,
create_get_args_node,
create_set_args_node,
)
from paddle.jit.dy2static.utils import (
FunctionNameLivenessAnalysis,
GetterSetterHelper,
create_name_str,
)
from .ifelse_transformer import ARGS_NAME
from .base_transformer import (
......@@ -41,12 +35,8 @@ from .base_transformer import (
ForNodeVisitor,
)
from paddle.jit.dy2static.utils import (
GetterSetterHelper,
create_name_str,
)
__all__ = ['LoopTransformer', 'NameVisitor']
__all__ = []
WHILE_CONDITION_PREFIX = 'while_condition'
WHILE_BODY_PREFIX = 'while_body'
......
......@@ -24,6 +24,8 @@ from paddle.fluid.framework import Program
from collections.abc import Sequence
__all__ = []
class Location:
"""
......
......@@ -44,6 +44,8 @@ from paddle.fluid.dygraph.amp.auto_cast import (
)
from paddle import _legacy_C_ops
__all__ = []
class NestSequence:
"""
......
......@@ -57,7 +57,7 @@ from .function_spec import (
from .ast_transformer import DygraphToStaticAst
__all__ = ['ProgramTranslator', 'convert_to_static']
__all__ = []
# For each traced function, we set `max_traced_program_count` = 10 to consider caching performance.
# Once exceeding the threshold, we will raise warning to users to make sure the conversion is as expected.
......
......@@ -15,22 +15,18 @@
from paddle.utils import gast
from paddle.fluid import unique_name
from paddle.jit.dy2static.utils import index_in_list
from .break_continue_transformer import (
ForToWhileTransformer,
from .utils import (
index_in_list,
ast_to_source_code,
Dygraph2StaticException,
ORIGI_INFO,
)
from paddle.jit.dy2static.utils import ast_to_source_code
from .break_continue_transformer import ForToWhileTransformer
from .base_transformer import (
BaseTransformer,
)
from paddle.jit.dy2static.utils import Dygraph2StaticException
from paddle.jit.dy2static.utils import ORIGI_INFO
__all__ = [
'RETURN_NO_VALUE_MAGIC_NUM',
'RETURN_NO_VALUE_VAR_NAME',
'ReturnTransformer',
]
__all__ = []
# Constant for the name of the variable which stores the boolean state that we
# should return
......
......@@ -22,7 +22,7 @@ from .utils import (
ast_to_source_code,
)
__all__ = ['AstNodeWrapper', 'NodeVarType', 'StaticAnalysisVisitor']
__all__ = []
class NodeVarType:
......
......@@ -14,14 +14,16 @@
from paddle.utils import gast
from paddle.jit.dy2static.utils import ast_to_source_code
from paddle.jit.dy2static.static_analysis import (
from .utils import ast_to_source_code
from .static_analysis import (
AstNodeWrapper,
)
from .base_transformer import (
BaseTransformer,
)
__all__ = []
class TensorShapeTransformer(BaseTransformer):
"""
......
......@@ -20,6 +20,8 @@ from .base_transformer import (
BaseTransformer,
)
__all__ = []
class TypeHintTransformer(BaseTransformer):
"""
......
......@@ -15,18 +15,13 @@
import paddle
from paddle.utils import gast
from paddle.fluid.framework import Variable
from paddle.jit.dy2static.utils import (
from .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',
]
__all__ = []
def create_undefined_var(name):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册