/* 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. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/amp/fp16_type_traits.h" #include "paddle/fluid/operators/optimizers/lars_momentum_op.h" namespace paddle { namespace operators { template using MultiPrecisionType = typename details::MPTypeTrait::Type; template __global__ void MomentumLarsKernel( const T* p, const T* g, const MT* v, const MultiPrecisionType* learning_rate, const MT mu, const int64_t num, const MT lars_coeff, const MT lars_weight_decay, const MultiPrecisionType* p_norm, const MultiPrecisionType* g_norm, T* p_out, MT* v_out, const MT epsilon, const MT* master_p, MT* master_p_out, const MultiPrecisionType rescale_grad) { const MT lr = static_cast(learning_rate[0]); MT local_lr = lr; const MT p_n = static_cast(p_norm[0]); const MT g_n = static_cast(g_norm[0]); if (lars_weight_decay > static_cast(0) && p_n > static_cast(0) && g_n > static_cast(0)) { local_lr = lr * lars_coeff * p_n / (g_n + lars_weight_decay * p_n + epsilon); } CUDA_KERNEL_LOOP(i, num) { MT grad = static_cast(g[i]) * static_cast(rescale_grad); MT param = master_p ? master_p[i] : static_cast(p[i]); MT v_new = v[i] * mu + local_lr * (grad + lars_weight_decay * param); MT p_new = param - v_new; v_out[i] = v_new; p_out[i] = static_cast(p_new); if (master_p_out) master_p_out[i] = p_new; } } template class LarsMomentumOpCUDAKernel : public framework::OpKernel { using MPDType = MultiPrecisionType; public: void Compute(const framework::ExecutionContext& ctx) const override { const bool multi_precision = ctx.Attr("multi_precision"); if (multi_precision) { InnerCompute(ctx, multi_precision); } else { InnerCompute(ctx, multi_precision); } } private: template void InnerCompute(const framework::ExecutionContext& ctx, const bool multi_precision) const { auto param_out = ctx.Output("ParamOut"); auto velocity_out = ctx.Output("VelocityOut"); auto param = ctx.Input("Param"); auto velocity = ctx.Input("Velocity"); auto grad = ctx.Input("Grad"); auto learning_rate = ctx.Input("LearningRate"); const framework::Tensor* master_param = nullptr; framework::Tensor* master_param_out = nullptr; if (multi_precision) { bool has_master = ctx.HasInput("MasterParam") && ctx.HasOutput("MasterParamOut"); PADDLE_ENFORCE_EQ(has_master, true, platform::errors::InvalidArgument( "The Input(MasterParam) and Output(MasterParamOut) " "should not be null when " "the attr `multi_precision` is true")); master_param = ctx.Input("MasterParam"); master_param_out = ctx.Output("MasterParamOut"); } const MT* master_p = multi_precision ? master_param->data() : nullptr; MT* master_p_out = multi_precision ? master_param_out->mutable_data(ctx.GetPlace()) : nullptr; T* p_out = param_out->mutable_data(ctx.GetPlace()); MT* v_out = velocity_out->mutable_data(ctx.GetPlace()); MT mu = static_cast(ctx.Attr("mu")); MT lars_coeff = static_cast(ctx.Attr("lars_coeff")); MT lars_weight_decay = static_cast(ctx.Attr("lars_weight_decay")); MT epsilon = static_cast(ctx.Attr("epsilon")); MPDType rescale_grad = static_cast(ctx.Attr("rescale_grad")); auto* p = param->data(); auto* g = grad->data(); auto* v = velocity->data(); auto* lr = learning_rate->data(); int block = 512; int grid = (param->numel() + block - 1) / block; auto eigen_p = framework::EigenVector::Flatten(*param); auto eigen_g = framework::EigenVector::Flatten(*grad); // calculate norms using eigein and launch the kernel. framework::Tensor p_norm_t, g_norm_t; p_norm_t.Resize({1}); g_norm_t.Resize({1}); auto* p_norm_data = p_norm_t.mutable_data(ctx.GetPlace()); auto* g_norm_data = g_norm_t.mutable_data(ctx.GetPlace()); auto ep_norm = framework::EigenScalar::From(p_norm_t); auto eg_norm = framework::EigenScalar::From(g_norm_t); auto* place = ctx.template device_context().eigen_device(); // eigen unsupport fp16 l2-norm ep_norm.device(*place) = eigen_p.template cast().square().sum().sqrt(); eg_norm.device(*place) = (eigen_g.template cast() * rescale_grad).square().sum().sqrt(); MomentumLarsKernel< T, MT><<>>( p, g, v, lr, mu, param->numel(), lars_coeff, lars_weight_decay, p_norm_data, g_norm_data, p_out, v_out, epsilon, master_p, master_p_out, rescale_grad); } }; } // namespace operators } // namespace paddle namespace ops = paddle::operators; REGISTER_OP_CUDA_KERNEL( lars_momentum, ops::LarsMomentumOpCUDAKernel, ops::LarsMomentumOpCUDAKernel, ops::LarsMomentumOpCUDAKernel);