未验证 提交 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): ...@@ -1272,6 +1272,7 @@ def _get_no_grad_set_name(no_grad_set):
return no_grad_set_name return no_grad_set_name
@framework.static_only
def append_backward(loss, def append_backward(loss,
parameter_list=None, parameter_list=None,
no_grad_set=None, no_grad_set=None,
...@@ -1861,6 +1862,7 @@ def calc_gradient(targets, inputs, target_gradients=None, no_grad_set=None): ...@@ -1861,6 +1862,7 @@ def calc_gradient(targets, inputs, target_gradients=None, no_grad_set=None):
return grad_vars return grad_vars
@framework.static_only
def gradients(targets, inputs, target_gradients=None, no_grad_set=None): def gradients(targets, inputs, target_gradients=None, no_grad_set=None):
""" """
:api_attr: Static Graph :api_attr: Static Graph
......
...@@ -229,7 +229,7 @@ def _dygraph_only_(func): ...@@ -229,7 +229,7 @@ def _dygraph_only_(func):
def _static_only_(func): def _static_only_(func):
def __impl__(*args, **kwargs): def __impl__(*args, **kwargs):
assert not in_dygraph_mode( 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 func(*args, **kwargs)
return __impl__ return __impl__
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
from __future__ import print_function from __future__ import print_function
import warnings import warnings
from .framework import Variable, in_dygraph_mode from .framework import Variable, in_dygraph_mode, static_only
from .layer_helper import LayerHelper from .layer_helper import LayerHelper
from .data_feeder import check_variable_and_dtype, check_dtype from .data_feeder import check_variable_and_dtype, check_dtype
from ..utils import deprecated from ..utils import deprecated
...@@ -129,6 +129,7 @@ def one_hot(input, depth, allow_out_of_range=False): ...@@ -129,6 +129,7 @@ def one_hot(input, depth, allow_out_of_range=False):
return one_hot_out return one_hot_out
@static_only
@deprecated(since='2.0.0', update_to='paddle.nn.functional.embedding') @deprecated(since='2.0.0', update_to='paddle.nn.functional.embedding')
def embedding(input, def embedding(input,
size, size,
......
...@@ -30,7 +30,7 @@ from paddle.fluid import layers ...@@ -30,7 +30,7 @@ from paddle.fluid import layers
from paddle.fluid.executor import Executor, global_scope from paddle.fluid.executor import Executor, global_scope
from paddle.fluid.evaluator import Evaluator from paddle.fluid.evaluator import Evaluator
from paddle.fluid.framework import Program, Parameter, default_main_program, default_startup_program, Variable, \ 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, \ from paddle.reader import cache, map_readers, buffered, compose, chain, shuffle, \
ComposeNotAligned, firstn, xmap_readers, multiprocess_reader ComposeNotAligned, firstn, xmap_readers, multiprocess_reader
from .wrapped_decorator import signature_safe_contextmanager from .wrapped_decorator import signature_safe_contextmanager
...@@ -1710,7 +1710,7 @@ def _load_persistable_nodes(executor, dirname, graph): ...@@ -1710,7 +1710,7 @@ def _load_persistable_nodes(executor, dirname, graph):
load_vars(executor=executor, dirname=dirname, vars=var_list) load_vars(executor=executor, dirname=dirname, vars=var_list)
@dygraph_not_support @static_only
def save(program, model_path): def save(program, model_path):
""" """
:api_attr: Static Graph :api_attr: Static Graph
...@@ -1773,7 +1773,7 @@ def save(program, model_path): ...@@ -1773,7 +1773,7 @@ def save(program, model_path):
f.write(program.desc.serialize_to_string()) f.write(program.desc.serialize_to_string())
@dygraph_not_support @static_only
def load(program, model_path, executor=None, var_list=None): def load(program, model_path, executor=None, var_list=None):
""" """
:api_attr: Static Graph :api_attr: Static Graph
...@@ -2107,7 +2107,7 @@ def load_program_state(model_path, var_list=None): ...@@ -2107,7 +2107,7 @@ def load_program_state(model_path, var_list=None):
return para_dict return para_dict
@dygraph_not_support @static_only
def set_program_state(program, state_dict): def set_program_state(program, state_dict):
""" """
:api_attr: Static Graph :api_attr: Static Graph
......
...@@ -18,7 +18,7 @@ from ..wrapped_decorator import signature_safe_contextmanager ...@@ -18,7 +18,7 @@ from ..wrapped_decorator import signature_safe_contextmanager
from .layer_function_generator import autodoc, templatedoc from .layer_function_generator import autodoc, templatedoc
from .tensor import assign, cast, fill_constant from .tensor import assign, cast, fill_constant
from .. import core 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 ..layer_helper import LayerHelper, unique_name
from .nn import logical_and, logical_not, logical_or from .nn import logical_and, logical_not, logical_or
from .utils import assert_same_structure, map_structure, hold_mutable_vars, copy_mutable_vars 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): ...@@ -211,6 +211,7 @@ def merge_lod_tensor(in_true, in_false, x, mask, level=0):
return out return out
@static_only
def Print(input, def Print(input,
first_n=-1, first_n=-1,
message=None, message=None,
......
...@@ -20,7 +20,7 @@ from __future__ import print_function ...@@ -20,7 +20,7 @@ from __future__ import print_function
from .layer_function_generator import generate_layer_fn from .layer_function_generator import generate_layer_fn
from .layer_function_generator import autodoc, templatedoc from .layer_function_generator import autodoc, templatedoc
from ..layer_helper import LayerHelper 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 .. import core
from .loss import softmax_with_cross_entropy from .loss import softmax_with_cross_entropy
from . import tensor from . import tensor
...@@ -2099,6 +2099,7 @@ def density_prior_box(input, ...@@ -2099,6 +2099,7 @@ def density_prior_box(input,
return box, var return box, var
@static_only
def multi_box_head(inputs, def multi_box_head(inputs,
image, image,
base_size, base_size,
......
...@@ -20,7 +20,7 @@ from paddle.utils import deprecated ...@@ -20,7 +20,7 @@ from paddle.utils import deprecated
from . import nn from . import nn
from .layer_function_generator import templatedoc from .layer_function_generator import templatedoc
from ..layer_helper import LayerHelper 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 .. import core
from ..data_feeder import check_variable_and_dtype, check_type from ..data_feeder import check_variable_and_dtype, check_type
from ..param_attr import ParamAttr from ..param_attr import ParamAttr
...@@ -664,6 +664,7 @@ def warpctc(input, ...@@ -664,6 +664,7 @@ def warpctc(input,
# FIXME(wuyi): let docstring_checker.py understand @autodoc. # FIXME(wuyi): let docstring_checker.py understand @autodoc.
# For now, the comments in c++ use types like Tensor, but in python side # For now, the comments in c++ use types like Tensor, but in python side
# the type is often "Variable", and arguments may vary. # the type is often "Variable", and arguments may vary.
@static_only
@templatedoc(op_type="nce") @templatedoc(op_type="nce")
def nce(input, def nce(input,
label, label,
......
...@@ -26,7 +26,7 @@ import six ...@@ -26,7 +26,7 @@ import six
import paddle import paddle
from ..layer_helper import LayerHelper from ..layer_helper import LayerHelper
from ..initializer import Normal, Constant, NumpyArrayInitializer 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 .. import dygraph_utils
from ..param_attr import ParamAttr from ..param_attr import ParamAttr
from .layer_function_generator import autodoc, templatedoc, _generate_doc_string_ from .layer_function_generator import autodoc, templatedoc, _generate_doc_string_
...@@ -3216,6 +3216,7 @@ def instance_norm(input, ...@@ -3216,6 +3216,7 @@ def instance_norm(input,
return instance_norm_out return instance_norm_out
@static_only
def data_norm(input, def data_norm(input,
act=None, act=None,
epsilon=1e-05, epsilon=1e-05,
...@@ -13465,6 +13466,7 @@ class PyFuncRegistry(object): ...@@ -13465,6 +13466,7 @@ class PyFuncRegistry(object):
return tuple(ret) return tuple(ret)
@static_only
@templatedoc() @templatedoc()
def py_func(func, x, out, backward_func=None, skip_vars_in_backward_input=None): def py_func(func, x, out, backward_func=None, skip_vars_in_backward_input=None):
""" """
......
...@@ -19,7 +19,8 @@ __all__ = [ ...@@ -19,7 +19,8 @@ __all__ = [
'name_scope', 'ParallelExecutor', 'program_guard', 'WeightNormParamAttr', 'name_scope', 'ParallelExecutor', 'program_guard', 'WeightNormParamAttr',
'default_main_program', 'default_startup_program', 'Program', 'data', 'default_main_program', 'default_startup_program', 'Program', 'data',
'InputSpec', 'save', 'load', 'save_inference_model', 'load_inference_model', '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 . import nn
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册