未验证 提交 d0129fcd 编写于 作者: C Chen Weihang 提交者: GitHub

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
上级 e931c7ba
......@@ -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
......
......@@ -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__
......
......@@ -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,
......
......@@ -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
......
......@@ -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,
......
......@@ -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,
......
......@@ -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,
......
......@@ -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):
"""
......
......@@ -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,
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册