Skip to content

Commit

Permalink
[API Clean]Clean __all__ to avoid exposing usless API (#48713)
Browse files Browse the repository at this point in the history
* [API Clean]Clean __all__ to avoid exposing usless API

* fix import

* fix typo

* remove tracedLayer unittest
  • Loading branch information
Aurelius84 committed Dec 8, 2022
1 parent b91bbd3 commit ea9e408
Show file tree
Hide file tree
Showing 33 changed files with 75 additions and 152 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ def test_new_directory(self):
'paddle.distributed.ParallelEnv',
'paddle.DataParallel',
'paddle.jit',
'paddle.jit.TracedLayer',
'paddle.jit.to_static',
'paddle.jit.ProgramTranslator',
'paddle.jit.TranslatedLayer',
Expand Down
16 changes: 2 additions & 14 deletions python/paddle/fluid/tests/unittests/test_imperative_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
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

Expand Down Expand Up @@ -153,19 +153,7 @@ def func_test_mnist_float32(self):
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])
Expand Down
24 changes: 3 additions & 21 deletions python/paddle/fluid/tests/unittests/test_imperative_ptb_rnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@

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
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):
Expand Down Expand Up @@ -298,25 +297,8 @@ def ptb_rnn_cpu_float32(self, is_sparse):
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

Expand Down
20 changes: 3 additions & 17 deletions python/paddle/fluid/tests/unittests/test_imperative_resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@

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

Expand Down Expand Up @@ -301,20 +300,7 @@ def func_test_resnet_float32(self):
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
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
Expand Down Expand Up @@ -1171,27 +1170,7 @@ def run_dygraph():

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

Expand Down
16 changes: 0 additions & 16 deletions python/paddle/fluid/tests/unittests/test_op_function_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
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):
Expand Down Expand Up @@ -93,20 +91,6 @@ def test_trace_backward(self):
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()
2 changes: 0 additions & 2 deletions python/paddle/jit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,5 +33,4 @@
'set_code_level',
'set_verbosity',
'not_to_static',
'TracedLayer',
]
2 changes: 0 additions & 2 deletions python/paddle/jit/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@
from paddle.fluid.wrapped_decorator import wrap_decorator

__all__ = [
'TracedLayer',
'declarative',
'dygraph_to_static_func',
'set_code_level',
'set_verbosity',
'save',
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/jit/dy2static/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
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__ = []
2 changes: 1 addition & 1 deletion python/paddle/jit/dy2static/assert_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
BaseTransformer,
)

__all__ = ['AssertTransformer']
__all__ = []


class AssertTransformer(BaseTransformer):
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/jit/dy2static/ast_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
from . import logging_utils
from .utils import ast_to_source_code

__all__ = ['DygraphToStaticAst']
__all__ = []


def apply_optimization(transformers):
Expand Down
2 changes: 2 additions & 0 deletions python/paddle/jit/dy2static/base_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
get_attribute_full_name,
)

__all__ = []


class BaseTransformer(gast.NodeTransformer):
def visit(self, node):
Expand Down
2 changes: 2 additions & 0 deletions python/paddle/jit/dy2static/basic_api_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
BaseTransformer,
)

__all__ = []


class BasicApiTransformer(BaseTransformer):
"""
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/jit/dy2static/break_continue_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
ForNodeVisitor,
)

__all__ = ['BreakContinueTransformer']
__all__ = []

BREAK_NAME_PREFIX = '__break'
CONTINUE_NAME_PREFIX = '__continue'
Expand Down
2 changes: 2 additions & 0 deletions python/paddle/jit/dy2static/call_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

PDB_SET = "pdb.set_trace"

__all__ = []


class CallTransformer(BaseTransformer):
"""
Expand Down
2 changes: 2 additions & 0 deletions python/paddle/jit/dy2static/cast_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
BaseTransformer,
)

__all__ = []


class CastTransformer(BaseTransformer):
"""
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/jit/dy2static/convert_call_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
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
Expand Down
7 changes: 5 additions & 2 deletions python/paddle/jit/dy2static/convert_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,10 +43,13 @@
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":
Expand Down
9 changes: 6 additions & 3 deletions python/paddle/jit/dy2static/create_variable_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@
# 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 (
BaseTransformer,
)


__all__ = []


class CreateVariableTransformer(BaseTransformer):
""" """

Expand Down
6 changes: 4 additions & 2 deletions python/paddle/jit/dy2static/decorator_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,6 +29,8 @@

import re

__all__ = []

IGNORE_NAMES = [
'declarative',
'to_static',
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/jit/dy2static/early_return_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading

0 comments on commit ea9e408

Please sign in to comment.