From 21b7ed3e8d91ffb2fab3e28367c6eacfa292855b Mon Sep 17 00:00:00 2001 From: wuhuanzhou Date: Thu, 23 Dec 2021 17:19:39 +0800 Subject: [PATCH] add control/status API (#37885) * add control/status API, test=develop * fix import error, test=develop * add is_grad_enabled unittest, test=develop * add code comment for example code and API, test=develop * add checking for type, test=develop * add api description, test=develop * fix docs index_en, test=document_fix * fix doc of is_floating_point, test=document_fix --- python/paddle/__init__.py | 4 +++ python/paddle/autograd/__init__.py | 2 +- .../tests/unittests/test_imperative_basic.py | 7 ++++++ python/paddle/framework/__init__.py | 1 + python/paddle/framework/framework.py | 25 +++++++++++++++++++ python/paddle/tensor/__init__.py | 2 ++ python/paddle/tensor/attribute.py | 24 ++++++++++++++++++ 7 files changed, 64 insertions(+), 1 deletion(-) diff --git a/python/paddle/__init__.py b/python/paddle/__init__.py index 4e6fe5a686..24607319f3 100755 --- a/python/paddle/__init__.py +++ b/python/paddle/__init__.py @@ -72,6 +72,7 @@ from .tensor.attribute import rank # noqa: F401 from .tensor.attribute import shape # noqa: F401 from .tensor.attribute import real # noqa: F401 from .tensor.attribute import imag # noqa: F401 +from .tensor.attribute import is_floating_point # noqa: F401 from .tensor.creation import to_tensor # noqa: F401 from .tensor.creation import diag # noqa: F401 from .tensor.creation import diagflat # noqa: F401 @@ -285,6 +286,7 @@ from .framework import CUDAPinnedPlace # noqa: F401 from .autograd import grad # noqa: F401 from .autograd import no_grad # noqa: F401 from .autograd import set_grad_enabled # noqa: F401 +from .autograd import is_grad_enabled # noqa: F401 from .framework import save # noqa: F401 from .framework import load # noqa: F401 from .framework import DataParallel # noqa: F401 @@ -453,6 +455,7 @@ __all__ = [ # noqa 'shape', 'real', 'imag', + 'is_floating_point', 'complex', 'reciprocal', 'rand', @@ -468,6 +471,7 @@ __all__ = [ # noqa 'median', 'no_grad', 'set_grad_enabled', + 'is_grad_enabled', 'mod', 'abs', 'tril', diff --git a/python/paddle/autograd/__init__.py b/python/paddle/autograd/__init__.py index 661bc3485b..86500db659 100644 --- a/python/paddle/autograd/__init__.py +++ b/python/paddle/autograd/__init__.py @@ -16,7 +16,7 @@ from ..fluid.dygraph.base import grad # noqa: F401 from . import backward_mode # noqa: F401 from .backward_mode import backward # noqa: F401 from .py_layer import PyLayer, PyLayerContext # noqa: F401 -from ..framework import set_grad_enabled # noqa: F401 +from ..framework import set_grad_enabled, is_grad_enabled # noqa: F401 from ..fluid.dygraph.base import no_grad_ as no_grad # noqa: F401 from .functional import jacobian, hessian, batch_jacobian, batch_hessian # noqa: F401 from .functional import vjp, jvp, vhp # noqa: F401 diff --git a/python/paddle/fluid/tests/unittests/test_imperative_basic.py b/python/paddle/fluid/tests/unittests/test_imperative_basic.py index d6835069b9..8f8ecca105 100644 --- a/python/paddle/fluid/tests/unittests/test_imperative_basic.py +++ b/python/paddle/fluid/tests/unittests/test_imperative_basic.py @@ -317,6 +317,13 @@ class TestImperative(unittest.TestCase): self.assertTrue(tmp2._grad_ivar() is not None) self.assertTrue(l0.weight._grad_ivar() is not None) + def test_paddle_imperative_is_grad_enabled(self): + with fluid.dygraph.guard(): + with paddle.set_grad_enabled(False): + self.assertTrue(paddle.is_grad_enabled() is False) + with paddle.set_grad_enabled(True): + self.assertTrue(paddle.is_grad_enabled()) + def test_sum_op(self): x = np.ones([2, 2], np.float32) with fluid.dygraph.guard(): diff --git a/python/paddle/framework/__init__.py b/python/paddle/framework/__init__.py index 722003c034..a081da60c6 100644 --- a/python/paddle/framework/__init__.py +++ b/python/paddle/framework/__init__.py @@ -19,6 +19,7 @@ from .random import seed # noqa: F401 from .framework import get_default_dtype # noqa: F401 from .framework import set_default_dtype # noqa: F401 from .framework import set_grad_enabled # noqa: F401 +from .framework import is_grad_enabled # noqa: F401 from ..fluid.param_attr import ParamAttr # noqa: F401 from ..fluid.layers.tensor import create_parameter # noqa: F401 diff --git a/python/paddle/framework/framework.py b/python/paddle/framework/framework.py index e9d690c28d..e899d26728 100644 --- a/python/paddle/framework/framework.py +++ b/python/paddle/framework/framework.py @@ -116,3 +116,28 @@ def set_grad_enabled(mode): tracer._has_grad = prev_mode else: yield + + +def is_grad_enabled(): + """ + Returns whether current dygraph gradient calculation mode is enabled. + + Returns: + bool: True if current dygraph gradient calculation mode is enabled, otherwise false. + + Examples: + .. code-block:: python + + import paddle + + # Dygraph gradient calculation mode is enabled by default. + paddle.is_grad_enabled() # True + + with paddle.set_grad_enabled(False): + paddle.is_grad_enabled() # False + + paddle.enable_static() + paddle.is_grad_enabled() # False + """ + tracer = _dygraph_tracer() + return tracer._has_grad if tracer else False diff --git a/python/paddle/tensor/__init__.py b/python/paddle/tensor/__init__.py index ef9425f6db..fcb328ba27 100755 --- a/python/paddle/tensor/__init__.py +++ b/python/paddle/tensor/__init__.py @@ -18,6 +18,7 @@ from .attribute import rank # noqa: F401 from .attribute import shape # noqa: F401 from .attribute import real # noqa: F401 from .attribute import imag # noqa: F401 +from .attribute import is_floating_point # noqa: F401 from .creation import to_tensor # noqa: F401 from .creation import diag # noqa: F401 from .creation import diagflat # noqa: F401 @@ -418,6 +419,7 @@ tensor_method_func = [ #noqa 'shape', 'real', 'imag', + 'is_floating_point', 'digamma', 'diagonal', 'trunc', diff --git a/python/paddle/tensor/attribute.py b/python/paddle/tensor/attribute.py index aefe2e4749..ee84b43e13 100644 --- a/python/paddle/tensor/attribute.py +++ b/python/paddle/tensor/attribute.py @@ -81,6 +81,30 @@ def is_complex(x): def is_floating_point(x): + """ + Returns whether the dtype of `x` is one of paddle.float64, paddle.float32, paddle.float16, and paddle.bfloat16. + + Args: + x (Tensor): The input tensor. + + Returns: + bool: True if the dtype of `x` is floating type, otherwise false. + + Examples: + .. code-block:: python + + import paddle + + x = paddle.arange(1., 5., dtype='float32') + y = paddle.arange(1, 5, dtype='int32') + print(paddle.is_floating_point(x)) + # True + print(paddle.is_floating_point(y)) + # False + """ + if not isinstance(x, (paddle.Tensor, paddle.static.Variable)): + raise TypeError("Expected Tensor, but received type of x: {}".format( + type(x))) dtype = x.dtype is_fp_dtype = (dtype == core.VarDesc.VarType.FP32 or dtype == core.VarDesc.VarType.FP64 or -- GitLab