|
|
|
@ -12,9 +12,9 @@
|
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
|
# limitations under the License.
|
|
|
|
|
from ..wrapped_decorator import signature_safe_contextmanager, wrap_decorator
|
|
|
|
|
import inspect
|
|
|
|
|
import decorator
|
|
|
|
|
import contextlib
|
|
|
|
|
import functools
|
|
|
|
|
import sys
|
|
|
|
|
import numpy as np
|
|
|
|
|
from paddle.fluid import core
|
|
|
|
@ -172,28 +172,15 @@ def disable_dygraph():
|
|
|
|
|
_functional_dygraph_context_manager = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@signature_safe_contextmanager
|
|
|
|
|
def _switch_tracer_mode_guard_(is_train=True):
|
|
|
|
|
tracer = framework._dygraph_tracer()
|
|
|
|
|
if tracer:
|
|
|
|
|
mode = tracer._train_mode
|
|
|
|
|
tracer._train_mode = is_train
|
|
|
|
|
try:
|
|
|
|
|
yield
|
|
|
|
|
finally:
|
|
|
|
|
tracer._train_mode = mode
|
|
|
|
|
else:
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def no_grad(func=None):
|
|
|
|
|
class no_grad:
|
|
|
|
|
"""
|
|
|
|
|
:api_attr: imperative
|
|
|
|
|
|
|
|
|
|
Create a context which disables dygraph gradient calculation.
|
|
|
|
|
In this mode, the result of every computation will have `stop_gradient=True`.
|
|
|
|
|
In this mode, the result of every computation will have `stop_gradient` set
|
|
|
|
|
to `True`.
|
|
|
|
|
|
|
|
|
|
Also functions as a decorator. (Make sure to instantiate without parenthesis.)
|
|
|
|
|
Also functions as a decorator. (Make sure to use an instance.)
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
|
|
|
|
@ -202,47 +189,65 @@ def no_grad(func=None):
|
|
|
|
|
import numpy as np
|
|
|
|
|
import paddle.fluid as fluid
|
|
|
|
|
|
|
|
|
|
paddle.enable_imperative()
|
|
|
|
|
|
|
|
|
|
# use as generator
|
|
|
|
|
|
|
|
|
|
data = np.array([[2, 3], [4, 5]]).astype('float32')
|
|
|
|
|
with fluid.dygraph.guard():
|
|
|
|
|
l0 = fluid.Linear(2, 2) # l0.weight.gradient() is None
|
|
|
|
|
l1 = fluid.Linear(2, 2)
|
|
|
|
|
with fluid.dygraph.no_grad():
|
|
|
|
|
# l1.weight.stop_gradient is False
|
|
|
|
|
tmp = l1.weight * 2 # tmp.stop_gradient is True
|
|
|
|
|
x = fluid.dygraph.to_variable(data)
|
|
|
|
|
y = l0(x) + tmp
|
|
|
|
|
o = l1(y)
|
|
|
|
|
o.backward()
|
|
|
|
|
print(tmp.gradient() is None) # True
|
|
|
|
|
print(l0.weight.gradient() is None) # False
|
|
|
|
|
l0 = fluid.Linear(2, 2) # l0.weight.gradient() is None
|
|
|
|
|
l1 = fluid.Linear(2, 2)
|
|
|
|
|
with fluid.no_grad():
|
|
|
|
|
# l1.weight.stop_gradient is False
|
|
|
|
|
tmp = l1.weight * 2 # tmp.stop_gradient is True
|
|
|
|
|
x = fluid.dygraph.to_variable(data)
|
|
|
|
|
y = l0(x) + tmp
|
|
|
|
|
o = l1(y)
|
|
|
|
|
o.backward()
|
|
|
|
|
print(tmp.gradient() is None) # True
|
|
|
|
|
print(l0.weight.gradient() is None) # False
|
|
|
|
|
|
|
|
|
|
# use as decorator
|
|
|
|
|
|
|
|
|
|
@fluid.dygraph.no_grad
|
|
|
|
|
@fluid.no_grad()
|
|
|
|
|
def test_layer():
|
|
|
|
|
with fluid.dygraph.guard():
|
|
|
|
|
inp = np.ones([3, 1024], dtype='float32')
|
|
|
|
|
t = fluid.dygraph.base.to_variable(inp)
|
|
|
|
|
linear1 = fluid.Linear(1024, 4, bias_attr=False)
|
|
|
|
|
linear2 = fluid.Linear(4, 4)
|
|
|
|
|
ret = linear1(t)
|
|
|
|
|
dy_ret = linear2(ret)
|
|
|
|
|
inp = np.ones([3, 1024], dtype='float32')
|
|
|
|
|
t = fluid.dygraph.base.to_variable(inp)
|
|
|
|
|
linear1 = fluid.Linear(1024, 4, bias_attr=False)
|
|
|
|
|
linear2 = fluid.Linear(4, 4)
|
|
|
|
|
ret = linear1(t)
|
|
|
|
|
dy_ret = linear2(ret)
|
|
|
|
|
|
|
|
|
|
test_layer()
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
if func is None:
|
|
|
|
|
return _switch_tracer_mode_guard_(is_train=False)
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
def __call__(self, func):
|
|
|
|
|
@decorator.decorator
|
|
|
|
|
def __impl__(func, *args, **kwargs):
|
|
|
|
|
with _switch_tracer_mode_guard_(is_train=False):
|
|
|
|
|
def _decorate_function(func, *args, **kwargs):
|
|
|
|
|
with self:
|
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
return __impl__(func)
|
|
|
|
|
@decorator.decorator
|
|
|
|
|
def _decorate_generator(func, *args, **kwargs):
|
|
|
|
|
gen = func(*args, **kwargs)
|
|
|
|
|
with self:
|
|
|
|
|
for x in gen:
|
|
|
|
|
yield x
|
|
|
|
|
|
|
|
|
|
if inspect.isgeneratorfunction(func):
|
|
|
|
|
return _decorate_generator(func)
|
|
|
|
|
else:
|
|
|
|
|
return _decorate_function(func)
|
|
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
|
tracer = framework._dygraph_tracer()
|
|
|
|
|
if tracer:
|
|
|
|
|
self.orig = tracer._train_mode
|
|
|
|
|
tracer._train_mode = False
|
|
|
|
|
|
|
|
|
|
def __exit__(self, *args):
|
|
|
|
|
tracer = framework._dygraph_tracer()
|
|
|
|
|
if tracer:
|
|
|
|
|
tracer._train_mode = self.orig
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@signature_safe_contextmanager
|
|
|
|
|