From d0129fcd88b6c6805590f13102223e4df730dde8 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Thu, 26 Nov 2020 15:34:24 +0800 Subject: [PATCH] Add static_only decorator for static apis (#29015) * add static_only for static api * addd static_only for class init * remove static_only for default_main_program * remove creater_parameter & startup_program * remove failed apis * revert py_func import * remove global scope * remove some api * remove cuda pinned place --- python/paddle/fluid/backward.py | 2 ++ python/paddle/fluid/framework.py | 2 +- python/paddle/fluid/input.py | 3 ++- python/paddle/fluid/io.py | 8 ++++---- python/paddle/fluid/layers/control_flow.py | 3 ++- python/paddle/fluid/layers/detection.py | 3 ++- python/paddle/fluid/layers/loss.py | 3 ++- python/paddle/fluid/layers/nn.py | 4 +++- python/paddle/fluid/parallel_executor.py | 2 +- python/paddle/static/__init__.py | 7 ++++--- 10 files changed, 23 insertions(+), 14 deletions(-) diff --git a/python/paddle/fluid/backward.py b/python/paddle/fluid/backward.py index c40b8db6948..0dbf840b990 100644 --- a/python/paddle/fluid/backward.py +++ b/python/paddle/fluid/backward.py @@ -1272,6 +1272,7 @@ def _get_no_grad_set_name(no_grad_set): return no_grad_set_name +@framework.static_only def append_backward(loss, parameter_list=None, no_grad_set=None, @@ -1861,6 +1862,7 @@ def calc_gradient(targets, inputs, target_gradients=None, no_grad_set=None): return grad_vars +@framework.static_only def gradients(targets, inputs, target_gradients=None, no_grad_set=None): """ :api_attr: Static Graph diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 28891871777..3a2d99085b3 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -229,7 +229,7 @@ def _dygraph_only_(func): def _static_only_(func): def __impl__(*args, **kwargs): assert not in_dygraph_mode( - ), "We only support '%s()' in static graph mode, please call 'paddle.enable_static()' to enter static graph mode." % func.__name__ + ), "In PaddlePaddle 2.x, we turn on dynamic graph mode by default, and '%s()' is only supported in static graph mode. So if you want to use this api, please call 'paddle.enable_static()' before this api to enter static graph mode." % func.__name__ return func(*args, **kwargs) return __impl__ diff --git a/python/paddle/fluid/input.py b/python/paddle/fluid/input.py index e56d1876e3f..2c4a9272648 100644 --- a/python/paddle/fluid/input.py +++ b/python/paddle/fluid/input.py @@ -14,7 +14,7 @@ from __future__ import print_function import warnings -from .framework import Variable, in_dygraph_mode +from .framework import Variable, in_dygraph_mode, static_only from .layer_helper import LayerHelper from .data_feeder import check_variable_and_dtype, check_dtype from ..utils import deprecated @@ -129,6 +129,7 @@ def one_hot(input, depth, allow_out_of_range=False): return one_hot_out +@static_only @deprecated(since='2.0.0', update_to='paddle.nn.functional.embedding') def embedding(input, size, diff --git a/python/paddle/fluid/io.py b/python/paddle/fluid/io.py index ebaa145d400..e65210331a1 100644 --- a/python/paddle/fluid/io.py +++ b/python/paddle/fluid/io.py @@ -30,7 +30,7 @@ from paddle.fluid import layers from paddle.fluid.executor import Executor, global_scope from paddle.fluid.evaluator import Evaluator from paddle.fluid.framework import Program, Parameter, default_main_program, default_startup_program, Variable, \ - program_guard, dygraph_not_support + program_guard, dygraph_not_support, static_only from paddle.reader import cache, map_readers, buffered, compose, chain, shuffle, \ ComposeNotAligned, firstn, xmap_readers, multiprocess_reader from .wrapped_decorator import signature_safe_contextmanager @@ -1710,7 +1710,7 @@ def _load_persistable_nodes(executor, dirname, graph): load_vars(executor=executor, dirname=dirname, vars=var_list) -@dygraph_not_support +@static_only def save(program, model_path): """ :api_attr: Static Graph @@ -1773,7 +1773,7 @@ def save(program, model_path): f.write(program.desc.serialize_to_string()) -@dygraph_not_support +@static_only def load(program, model_path, executor=None, var_list=None): """ :api_attr: Static Graph @@ -2107,7 +2107,7 @@ def load_program_state(model_path, var_list=None): return para_dict -@dygraph_not_support +@static_only def set_program_state(program, state_dict): """ :api_attr: Static Graph diff --git a/python/paddle/fluid/layers/control_flow.py b/python/paddle/fluid/layers/control_flow.py index b5f66a1308e..82c79d3b2f6 100755 --- a/python/paddle/fluid/layers/control_flow.py +++ b/python/paddle/fluid/layers/control_flow.py @@ -18,7 +18,7 @@ from ..wrapped_decorator import signature_safe_contextmanager from .layer_function_generator import autodoc, templatedoc from .tensor import assign, cast, fill_constant from .. import core -from ..framework import Program, Variable, Operator, in_dygraph_mode +from ..framework import Program, Variable, Operator, in_dygraph_mode, static_only from ..layer_helper import LayerHelper, unique_name from .nn import logical_and, logical_not, logical_or from .utils import assert_same_structure, map_structure, hold_mutable_vars, copy_mutable_vars @@ -211,6 +211,7 @@ def merge_lod_tensor(in_true, in_false, x, mask, level=0): return out +@static_only def Print(input, first_n=-1, message=None, diff --git a/python/paddle/fluid/layers/detection.py b/python/paddle/fluid/layers/detection.py index ce29b64ce43..de74902212c 100644 --- a/python/paddle/fluid/layers/detection.py +++ b/python/paddle/fluid/layers/detection.py @@ -20,7 +20,7 @@ from __future__ import print_function from .layer_function_generator import generate_layer_fn from .layer_function_generator import autodoc, templatedoc from ..layer_helper import LayerHelper -from ..framework import Variable, in_dygraph_mode +from ..framework import Variable, in_dygraph_mode, static_only from .. import core from .loss import softmax_with_cross_entropy from . import tensor @@ -2099,6 +2099,7 @@ def density_prior_box(input, return box, var +@static_only def multi_box_head(inputs, image, base_size, diff --git a/python/paddle/fluid/layers/loss.py b/python/paddle/fluid/layers/loss.py index 45f3de2d99a..5a15d4865a1 100644 --- a/python/paddle/fluid/layers/loss.py +++ b/python/paddle/fluid/layers/loss.py @@ -20,7 +20,7 @@ from paddle.utils import deprecated from . import nn from .layer_function_generator import templatedoc from ..layer_helper import LayerHelper -from ..framework import Variable, in_dygraph_mode +from ..framework import Variable, in_dygraph_mode, static_only from .. import core from ..data_feeder import check_variable_and_dtype, check_type from ..param_attr import ParamAttr @@ -664,6 +664,7 @@ def warpctc(input, # FIXME(wuyi): let docstring_checker.py understand @autodoc. # For now, the comments in c++ use types like Tensor, but in python side # the type is often "Variable", and arguments may vary. +@static_only @templatedoc(op_type="nce") def nce(input, label, diff --git a/python/paddle/fluid/layers/nn.py b/python/paddle/fluid/layers/nn.py index 97dea27f3b7..121ec47d947 100755 --- a/python/paddle/fluid/layers/nn.py +++ b/python/paddle/fluid/layers/nn.py @@ -26,7 +26,7 @@ import six import paddle from ..layer_helper import LayerHelper from ..initializer import Normal, Constant, NumpyArrayInitializer -from ..framework import Variable, OpProtoHolder, in_dygraph_mode, dygraph_only, _dygraph_tracer, default_main_program, _varbase_creator +from ..framework import Variable, OpProtoHolder, in_dygraph_mode, dygraph_only, _dygraph_tracer, default_main_program, _varbase_creator, static_only from .. import dygraph_utils from ..param_attr import ParamAttr from .layer_function_generator import autodoc, templatedoc, _generate_doc_string_ @@ -3216,6 +3216,7 @@ def instance_norm(input, return instance_norm_out +@static_only def data_norm(input, act=None, epsilon=1e-05, @@ -13465,6 +13466,7 @@ class PyFuncRegistry(object): return tuple(ret) +@static_only @templatedoc() def py_func(func, x, out, backward_func=None, skip_vars_in_backward_input=None): """ diff --git a/python/paddle/fluid/parallel_executor.py b/python/paddle/fluid/parallel_executor.py index a9904d6f982..e63270c1697 100644 --- a/python/paddle/fluid/parallel_executor.py +++ b/python/paddle/fluid/parallel_executor.py @@ -28,7 +28,7 @@ BuildStrategy = core.ParallelExecutor.BuildStrategy class ParallelExecutor(object): """ - :api_attr: Static Graph + :api_attr: Static Graph The ParallelExecutor is an upgraded version of :code:`paddle.static.Executor` that supports multi-node model training and testing based on the data-parallel mode. In data-parallel mode, diff --git a/python/paddle/static/__init__.py b/python/paddle/static/__init__.py index 7c9c034e8f9..6778149e2bf 100644 --- a/python/paddle/static/__init__.py +++ b/python/paddle/static/__init__.py @@ -19,12 +19,13 @@ __all__ = [ 'name_scope', 'ParallelExecutor', 'program_guard', 'WeightNormParamAttr', 'default_main_program', 'default_startup_program', 'Program', 'data', 'InputSpec', 'save', 'load', 'save_inference_model', 'load_inference_model', - 'load_program_state', 'set_program_state', 'cpu_places', 'cuda_places', 'Variable' + 'load_program_state', 'set_program_state', 'cpu_places', 'cuda_places', + 'Variable' ] from . import nn -from .io import save_inference_model #DEFINE_ALIAS -from .io import load_inference_model #DEFINE_ALIAS +from .io import save_inference_model #DEFINE_ALIAS +from .io import load_inference_model #DEFINE_ALIAS from ..fluid import Scope #DEFINE_ALIAS from .input import data #DEFINE_ALIAS from .input import InputSpec #DEFINE_ALIAS -- GitLab