varbase_patch_methods.py 10.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
import inspect
16 17 18
import numpy as np

import paddle
19 20
from .. import framework
from .. import core
21 22
from ..framework import Variable, Parameter, ParamBase
from .base import switch_to_static_graph
23
from .math_op_patch import monkey_patch_math_varbase
24
from .parallel import scale_loss
25 26 27


def monkey_patch_varbase():
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    @switch_to_static_graph
    def _to_static_var(self, to_parameter=False, **kwargs):
        """
        **Notes**:
            **This API is ONLY available in Dygraph mode**

        Transform a VarBase into static Variable with same attributes. It's a low level interface used
        in dy2static and shall not be called directly.

        Args:
            to_parameter (bool): It takes effect only if the input a VarBase. If set True,
                                 the VarBase will be converted into framework.Parameters. Otherwise, it will
                                 be converted into framework.Variable. Default False.

        Examples:
            .. code-block:: python

                import paddle.fluid as fluid
                from paddle.fluid.dygraph.base import to_variable
                import numpy as np

                data = np.ones([3, 1024], dtype='float32')
                with fluid.dygraph.guard():
                    var_base = to_variable(data)
                    static_var = var_base._to_static_var()

        """
55 56 57 58

        # Note: getattr(self, attr, None) will call x.grad=x.gradient(), but gradient() only available in dygraph. 
        # It will fail. So, for propery in dygraph only, should not let it getattr(self, attr, None).
        attr_not_need_keys = ['grad']
59 60 61
        if isinstance(self, ParamBase):
            attr_kwargs = self.__dict__.copy()
        else:
62 63 64 65 66 67
            attr_names = []
            for name in dir(self):
                if name not in attr_not_need_keys and not (
                        inspect.ismethod(getattr(self, name)) or
                        name.startswith('_')):
                    attr_names.append(name)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
            attr_kwargs = {name: getattr(self, name) for name in attr_names}

        attr_keys = ['block', 'shape', 'dtype', 'type', 'name', 'persistable']
        for attr in attr_keys:
            attr_kwargs[attr] = getattr(self, attr, None)

        attr_kwargs.update(kwargs)

        if to_parameter or isinstance(self, ParamBase):
            del attr_kwargs['persistable']
            static_var = Parameter(**attr_kwargs)
        else:
            static_var = Variable(**attr_kwargs)
        return static_var

83 84 85 86 87
    # TODO(jiabin): move this to cplusplus end if we find some performance issue on it
    @framework.dygraph_only
    def set_value(self, value):
        """
        **Notes**:
T
tianshuo78520a 已提交
88
            **This API is ONLY available in Dygraph mode**
89 90 91 92 93 94 95 96 97 98 99

        Set a new value for this Variable.

        Args:
            value (Variable|np.ndarray): the new value.

        Examples:
            .. code-block:: python

                import paddle.fluid as fluid
                from paddle.fluid.dygraph.base import to_variable
100
                from paddle.fluid.dygraph import Linear
101 102
                import numpy as np

103
                data = np.ones([3, 1024], dtype='float32')
104
                with fluid.dygraph.guard():
105
                    linear = fluid.dygraph.Linear(1024, 4)
106
                    t = to_variable(data)
107
                    linear(t)  # call with default weight
108
                    custom_weight = np.random.randn(1024, 4).astype("float32")
109 110
                    linear.weight.set_value(custom_weight)  # change existing weight
                    out = linear(t)  # call with different weight
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

        """
        assert isinstance(value, (np.ndarray, core.VarBase)), \
            "Variable set_value function, arguments type only support Variable, numpy, VarBase"

        value_np = value
        if isinstance(value, core.VarBase):
            value_np = value.numpy()

        self_tensor_np = self.numpy()

        assert self_tensor_np.shape == value_np.shape, \
            "Variable Shape not match, Variable [ {} ] need tensor with shape {} but load set tensor with shape {}".format(
                self.name, self_tensor_np.shape, value_np.shape)

        assert self_tensor_np.dtype == value_np.dtype, \
            "Variable dtype not match, Variable [ {} ] need tensor with dtype {}  but load tensor with dtype {}".format(
                self.name, self_tensor_np.dtype, value_np.dtype)

        self.value().get_tensor().set(value_np,
                                      framework._current_expected_place())

    @framework.dygraph_only
134
    def backward(self, retain_graph=False):
135 136
        """
        **Notes**:
T
tianshuo78520a 已提交
137
            **This API is ONLY available in Dygraph mode**
138

139
        Run backward of current Graph which starts from current Tensor.
140 141

        Args:
142
            retain_graph(bool, optional): If False, the graph used to compute grads will be freed. If you would
143 144 145
                like to add more ops to the built graph after calling this method( :code:`backward` ), set the parameter
                :code:`retain_graph` to True, then the grads will be retained. Thus, seting it to False is much more memory-efficient.
                Defaults to False.
146 147 148 149 150 151 152 153

        Returns:
            NoneType: None

        Examples:
            .. code-block:: python

                import numpy as np
154 155
                import paddle
                paddle.disable_static()
156 157

                x = np.ones([2, 2], np.float32)
158 159 160 161 162 163 164 165 166 167
                inputs = []
                for _ in range(10):
                    tmp = paddle.to_tensor(x)
                    # if we don't set tmp's stop_gradient as False then, all path to loss will has no gradient since
                    # there is no one need gradient on it.
                    tmp.stop_gradient=False
                    inputs.append(tmp)
                ret = paddle.sums(inputs)
                loss = paddle.reduce_sum(ret)
                loss.backward()
168 169 170

        """
        if framework.in_dygraph_mode():
171 172 173 174 175 176
            if paddle.distributed.get_world_size() > 1:
                scaled_loss = scale_loss(self)
                scaled_loss._run_backward(framework._dygraph_tracer(),
                                          retain_graph)
            else:
                self._run_backward(framework._dygraph_tracer(), retain_graph)
177 178
        else:
            raise ValueError(
T
tianshuo78520a 已提交
179
                "Variable.backward() is only available in DyGraph mode")
180 181 182 183 184

    @framework.dygraph_only
    def gradient(self):
        """
        **Notes**:
T
tianshuo78520a 已提交
185
            **This API is ONLY available in Dygraph mode**
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206

        Get the Gradient of Current Variable

        Returns:
            ndarray: Numpy value of the gradient of current Variable

        Examples:
            .. code-block:: python

                import paddle.fluid as fluid
                import numpy as np

                x = np.ones([2, 2], np.float32)
                with fluid.dygraph.guard():
                    inputs2 = []
                    for _ in range(10):
                        tmp = fluid.dygraph.base.to_variable(x)
                        tmp.stop_gradient=False
                        inputs2.append(tmp)
                    ret2 = fluid.layers.sums(inputs2)
                    loss2 = fluid.layers.reduce_sum(ret2)
207
                    loss2.backward()
208 209 210 211
                    print(loss2.gradient())

        """
        if self._grad_ivar() is None:
212 213
            return None

214 215 216 217 218 219 220
        new_ivar = self._grad_ivar()._copy_to(core.CPUPlace(), True)
        if self._grad_ivar().type == core.VarDesc.VarType.SELECTED_ROWS:
            return (np.array(new_ivar.value().get_selected_rows().get_tensor()),
                    np.array(new_ivar.value().get_selected_rows().rows()))
        else:
            return np.array(new_ivar.value().get_tensor())

221 222 223 224 225 226 227 228
    @property
    def grad(self):
        """
        The alias of gradient().
        """

        return self.gradient()

229 230
    def __str__(self):
        """
231
        Convert a VarBase object to a readable string.
232

233
        Returns(str): A readable string.
234 235 236 237

        Examples:
            .. code-block:: python

238
                import paddle
239
                x = paddle.rand([2, 5])
240
                print(x)
241 242 243 244
                
                # Tensor(shape=[2, 5], dtype=float32, place=CPUPlace,
                #        [[0.30574632, 0.55739117, 0.30902600, 0.39413780, 0.44830436],
                #         [0.79010487, 0.53972793, 0.09495186, 0.44267157, 0.72112119]])
245
        """
246 247
        from paddle.tensor.to_string import to_string
        return to_string(self)
248 249 250 251

    @property
    def block(self):
        return framework.default_main_program().global_block()
252

253 254 255 256 257 258 259 260 261 262 263 264
    def __nonzero__(self):
        numel = np.prod(self.shape)
        assert numel == 1, "When Variable is used as the condition of if/while , Variable can only contain one element."
        tensor = self.value().get_tensor()
        assert tensor._is_initialized(), "tensor not initialized"
        return bool(np.all(tensor.__array__() > 0))

    def __bool__(self):
        return self.__nonzero__()

    for method_name, method in (
        ("__bool__", __bool__), ("__nonzero__", __nonzero__),
265
        ("_to_static_var", _to_static_var), ("set_value", set_value),
266 267 268
        ("block", block), ("backward", backward), ("grad", grad),
        ("gradient", gradient), ("__str__", __str__), ("__repr__", __str__),
        ("__module__", "paddle"), ("__name__", "Tensor")):
269
        setattr(core.VarBase, method_name, method)
270 271 272

    # patch math methods for varbase
    monkey_patch_math_varbase()