/* Copyright (c) 2016 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. */ #pragma once #include #include "dgc/dgc.h" #include "paddle/fluid/framework/eigen.h" #include "paddle/fluid/memory/malloc.h" #include "paddle/fluid/operators/elementwise/elementwise_add_op.h" namespace paddle { namespace operators { inline float get_period_sparcity(const std::vector& sparsity, float cur_step, float rampup_steps) { PADDLE_ENFORCE_GE(static_cast(cur_step), 0, platform::errors::InvalidArgument( "DGC current step=%d, but it must >= 0, " "please submit issue in github", static_cast(cur_step))); size_t idx = static_cast(cur_step * sparsity.size() / rampup_steps); if (idx >= sparsity.size()) { idx = sparsity.size() - 1; } PADDLE_ENFORCE_LT( idx, sparsity.size(), platform::errors::OutOfRange( "sparsity index out of bounds. idx=%d >= sparsity.size=%d", idx, sparsity.size())); return sparsity[idx]; } template class DGCOpKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { auto u = ctx.Input("U"); auto v = ctx.Input("V"); auto g = ctx.Input("Grad"); auto grad_out = ctx.Output("Grad_out"); // attrs float m = ctx.Attr("m"); bool use_nesterov = ctx.Attr("use_nesterov"); auto sparsity = ctx.Attr>("sparsity"); auto rampup_begin_step = ctx.Attr("rampup_begin_step"); auto rampup_step = ctx.Attr("rampup_step"); // nranks auto nranks_tensor = ctx.Input("nranks"); const int nranks = static_cast(*nranks_tensor->data()); PADDLE_ENFORCE_GT(nranks, 1, platform::errors::PreconditionNotMet( "DGC is not useful when num_trainers <= 1. Please " "use multi card or multi machine GPU")); // regularization auto p = ctx.Input("Param"); float regular_coeff = ctx.Attr("regular_coeff"); int regular_type = ctx.Attr("regular_type"); auto p_e = framework::EigenVector::Flatten(*p); auto g_e = framework::EigenVector::Flatten(*g); auto grad_out_e = framework::EigenVector::Flatten(*grad_out); auto& dev_ctx = ctx.template device_context(); auto& eigen_ctx = *dev_ctx.eigen_device(); // NOTE. In paddle, loss has divided by nranks. Because dgc_op is before // allreduce, so local regular_coeff need div nranks too. But now we // multi grad with nranks in dgc_op, in that case regular_coeff don't // need to /nranks, can prevent precision loss. For coeff often equal // with 1e-4, if nranks=32, coeff/nranks will be 3.125e-6, the numerical // accuracy of coeff/nranks will be too low. PADDLE_ENFORCE_EQ(regular_type >= 0 && regular_type <= 2, true, platform::errors::InvalidArgument( "DGC only support one of None|L1Decay|L2Decay " "Regularization for now.")); if (regular_type == 0) { grad_out_e.device(eigen_ctx) = (1.0 * nranks) * g_e; } else if (regular_type == 1) { // L1Decay. grad = grad + coeff * sign(param) grad_out_e.device(eigen_ctx) = (1.0 * nranks) * g_e + regular_coeff * p_e.sign(); } else if (regular_type == 2) { // L2Decay. grad = grad + coeff * param grad_out_e.device(eigen_ctx) = (1.0 * nranks) * g_e + regular_coeff * p_e; } // current step auto current_step_tensor = ctx.Input("current_step"); const float* current_step = current_step_tensor->data(); if (static_cast(*current_step) < static_cast(rampup_begin_step)) { VLOG(10) << "current_step:" << *current_step << " < rampup_begin_step:" << rampup_begin_step << " so does't use dgc"; return; } float ratio = 1 - get_period_sparcity( sparsity, static_cast(*current_step - rampup_begin_step), rampup_step); PADDLE_ENFORCE_GE(ratio, 0.0, platform::errors::InvalidArgument( "DGC sparsity ratio must >= 0")); PADDLE_ENFORCE_LT(ratio, 1.0, platform::errors::InvalidArgument( "DGC sparsity ratio must < 1")); int k = static_cast(g->numel() * ratio); VLOG(10) << "m:" << m << ", use_nesterov:" << use_nesterov << ", rampup_begin_step:" << rampup_begin_step << ", rampup_step:" << rampup_step << ", current_step:" << *current_step << ", ratio:" << ratio << ", k:" << k << ", nranks:" << nranks; auto k_out = ctx.Output("k"); T* k_out_data = k_out->data(); *k_out_data = k; auto u_out = ctx.Output("U_out"); auto v_out = ctx.Output("V_out"); auto encode_grad_out = ctx.Output("EncodeGrad"); auto gather_buff = ctx.Output("GatherBuff"); // FIXME(gongwb): use cublas. auto u_out_e = framework::EigenVector::Flatten(*u_out); auto u_e = framework::EigenVector::Flatten(*u); // calc local momentum from global momentum // NOTE. If grad not multi nranks, need add below code. // if (static_cast(*current_step) == // static_cast(rampup_begin_step)) { // u_out_e.device(eigen_ctx) = (1.0 / nranks) * u_e; // } if (use_nesterov) { // u = m * (u + g) u_out_e.device(eigen_ctx) = m * (u_e + grad_out_e); // v = u + v + g ElementwiseComputeEx, DeviceContext, T>( ctx, u, v, 0, AddFunctor(), v_out); ElementwiseComputeEx, DeviceContext, T>( ctx, g, v, 0, AddFunctor(), v_out); } else { // u = m * u + g u_out_e.device(eigen_ctx) = m * u_e + grad_out_e; // v = u + v ElementwiseComputeEx, DeviceContext, T>( ctx, u, v, 0, AddFunctor(), v_out); } T* v_out_data = v_out->mutable_data(ctx.GetPlace()); T* u_out_data = u_out->mutable_data(ctx.GetPlace()); T* encode_grad_out_data = encode_grad_out->mutable_data( framework::DDim{2 * k}, ctx.GetPlace()); gather_buff->mutable_data(framework::DDim{2 * k * nranks}, ctx.GetPlace()); int buf_size = paddle::communication::dgc::get_buffer_size(k); auto tmp_ious_data = memory::Alloc(dev_ctx, buf_size); void* buf = reinterpret_cast(tmp_ious_data->ptr()); if (!paddle::communication::dgc::k_select( static_cast(encode_grad_out_data), k, v_out_data, static_cast(v_out->numel()), buf, dev_ctx.stream(), u_out_data)) { // TODO(weihang): owner should polish this error message PADDLE_THROW(platform::errors::InvalidArgument( "V_out numel error, V_out numel is %d.", v_out->numel())); } math::SetConstant tset; tset(dev_ctx, grad_out, static_cast(0)); } }; } // namespace operators } // namespace paddle