Revert "add device attr for regularizer, test=develop (#24981)" (#25375)

This reverts commit ab5a1fb853.
fix_copy_if_different
lilong12 5 years ago committed by GitHub
parent 2d9dbd31ad
commit 8a68d2c213
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -791,8 +791,8 @@ class Optimizer(object):
params_grads = append_gradient_clip_ops(params_grads) params_grads = append_gradient_clip_ops(params_grads)
# Add regularization if any # Add regularization if any
params_grads = append_regularization_ops( params_grads = append_regularization_ops(params_grads,
params_grads, self.regularization, self._param_device_map) self.regularization)
optimize_ops = self._create_optimization_pass(params_grads) optimize_ops = self._create_optimization_pass(params_grads)
return optimize_ops return optimize_ops

@ -16,7 +16,7 @@ from __future__ import print_function
import logging import logging
from . import framework from . import framework
from .framework import in_dygraph_mode, _varbase_creator, device_guard from .framework import in_dygraph_mode, _varbase_creator
from . import core from . import core
__all__ = ['L1Decay', 'L2Decay', 'L1DecayRegularizer', 'L2DecayRegularizer'] __all__ = ['L1Decay', 'L2Decay', 'L1DecayRegularizer', 'L2DecayRegularizer']
@ -62,9 +62,7 @@ def _create_regularization_of_grad(param, grad, regularization=None):
return new_grad return new_grad
def append_regularization_ops(parameters_and_grads, def append_regularization_ops(parameters_and_grads, regularization=None):
regularization=None,
param_device_map=None):
"""Create and add backward regularization Operators """Create and add backward regularization Operators
Creates and adds backward regularization operators in the BlockDesc. Creates and adds backward regularization operators in the BlockDesc.
@ -95,18 +93,15 @@ def append_regularization_ops(parameters_and_grads,
repeate_regularizer = False repeate_regularizer = False
with framework.name_scope('regularization'): with framework.name_scope('regularization'):
for param, grad in parameters_and_grads: for param, grad in parameters_and_grads:
device = param_device_map[
param.name] if param_device_map else None
if not repeate_regularizer and param.regularizer is not None and regularization is not None: if not repeate_regularizer and param.regularizer is not None and regularization is not None:
repeate_regularizer = True repeate_regularizer = True
logging.info( logging.info(
"If regularizer of a Parameter has been set by 'fluid.ParamAttr' or 'fluid.WeightNormParamAttr' already. " "If regularizer of a Parameter has been set by 'fluid.ParamAttr' or 'fluid.WeightNormParamAttr' already. "
"The Regularization[%s] in Optimizer will not take effect, and it will only be applied to other Parameters!" "The Regularization[%s] in Optimizer will not take effect, and it will only be applied to other Parameters!"
% regularization.__str__()) % regularization.__str__())
with device_guard(device):
with param.block.program._optimized_guard([param, grad]): with param.block.program._optimized_guard([param, grad]):
new_grad = _create_regularization_of_grad( new_grad = _create_regularization_of_grad(param, grad,
param, grad, regularization) regularization)
params_and_grads.append((param, new_grad)) params_and_grads.append((param, new_grad))
return params_and_grads return params_and_grads

Loading…
Cancel
Save