diff --git a/paddle/fluid/operators/dgc_op.h b/paddle/fluid/operators/dgc_op.h index 65aaf47472dd2a3d4c3d65eab6b4d269f7182286..1736fc36f647bd47969edb87bb471cb7aad7d9e1 100644 --- a/paddle/fluid/operators/dgc_op.h +++ b/paddle/fluid/operators/dgc_op.h @@ -28,7 +28,7 @@ inline float get_period_sparcity(const std::vector& sparsity, size_t idx = static_cast(cur_step * sparsity.size() / rampup_steps); if (idx >= sparsity.size()) { - return 0.999; + idx = sparsity.size() - 1; } PADDLE_ENFORCE_LT(idx, sparsity.size()); @@ -102,8 +102,9 @@ class DGCOpKernel : public framework::OpKernel { } float ratio = - 1 - get_period_sparcity(sparsity, static_cast(*current_step), - rampup_step); + 1 - get_period_sparcity( + sparsity, static_cast(*current_step - rampup_begin_step), + rampup_step); PADDLE_ENFORCE_GE(ratio, 0.0); PADDLE_ENFORCE_LT(ratio, 1.0); int k = static_cast(g->numel() * ratio); diff --git a/python/paddle/fluid/optimizer.py b/python/paddle/fluid/optimizer.py index 168afd4a52cd16f3ad68b4163f6786bed5747a37..8d66a810b0bf3a7b23b5bc768f064d8ea13bdeb0 100644 --- a/python/paddle/fluid/optimizer.py +++ b/python/paddle/fluid/optimizer.py @@ -947,6 +947,7 @@ class DGCMomentumOptimizer(Optimizer): self._momentum = momentum self._use_nesterov = bool(use_nesterov) + assert rampup_begin_step >= 0, "rampup_begin_step must >= 0" self._rampup_begin_step = rampup_begin_step self._rampup_step = rampup_step self._sparsity = sparsity @@ -963,8 +964,7 @@ class DGCMomentumOptimizer(Optimizer): self._local_grad_clip_norm = local_grad_clip_norm self._num_trainers = num_trainers - self._clip_norm = local_grad_clip_norm / (num_trainers * - num_trainers) + self._clip_norm = local_grad_clip_norm * (num_trainers**-0.5) self._get_dgc_regularization_param() diff --git a/python/paddle/fluid/tests/unittests/test_dgc_optimizer.py b/python/paddle/fluid/tests/unittests/test_dgc_optimizer.py index 996b6ae6ea4561922ae5c2acab4a9760dd73e88b..07dda4d594207f6c48d904b7e7aa7c2186449736 100644 --- a/python/paddle/fluid/tests/unittests/test_dgc_optimizer.py +++ b/python/paddle/fluid/tests/unittests/test_dgc_optimizer.py @@ -67,6 +67,8 @@ class TestDGCMomentumOptimizer(unittest.TestCase): learning_rate=learning_rate, momentum=0.2, rampup_begin_step=0, + local_grad_clip_norm=1.0, + num_trainers=2, regularization=regularization) mean_out = block.create_var( dtype="float32", shape=[1], lod_level=0, name="mean.out")