/* 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 "gflags/gflags.h" #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/operators/optimizers/adam_op_functor.h" namespace paddle { namespace operators { using Tensor = framework::Tensor; #ifdef PADDLE_WITH_XPU template class AdamOpXPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { const auto* param_var = ctx.InputVar("Param"); PADDLE_ENFORCE_EQ(param_var->IsType(), true, platform::errors::InvalidArgument( "Tensor holds the wrong type,Expected Var(%s)'s " "type is LoDTensor, " "but the received is %s", ctx.InputNames("Param").front(), framework::ToTypeName(param_var->Type()))); using paddle::framework::LoDTensor; auto& param = GET_DATA_SAFELY(ctx.Input("Param"), "Input", "Param", "Adam"); // auto& grad = Ref(ctx.Input("Grad"), "Must set Grad"); auto* grad_var = ctx.InputVar("Grad"); auto& mom1 = GET_DATA_SAFELY(ctx.Input("Moment1"), "Input", "Moment1", "Adam"); auto& mom2 = GET_DATA_SAFELY(ctx.Input("Moment2"), "Input", "Moment2", "Adam"); auto& lr = GET_DATA_SAFELY(ctx.Input("LearningRate"), "Input", "LearningRate", "Adam"); auto& beta1_pow = GET_DATA_SAFELY(ctx.Input("Beta1Pow"), "Input", "Beta1Pow", "Adam"); auto& beta2_pow = GET_DATA_SAFELY(ctx.Input("Beta2Pow"), "Input", "Beta2Pow", "Adam"); auto& param_out = GET_DATA_SAFELY(ctx.Output("ParamOut"), "Output", "ParamOut", "Adam"); auto& mom1_out = GET_DATA_SAFELY(ctx.Output("Moment1Out"), "Output", "Moment1Out", "Adam"); auto& mom2_out = GET_DATA_SAFELY(ctx.Output("Moment2Out"), "Output", "Moment2Out", "Adam"); auto* beta1_pow_out = ctx.Output("Beta1PowOut"); auto* beta2_pow_out = ctx.Output("Beta2PowOut"); bool skip_update = false; if (ctx.HasInput("SkipUpdate")) { auto* skip_update_tensor = ctx.Input("SkipUpdate"); PADDLE_ENFORCE_EQ(skip_update_tensor->numel(), 1, platform::errors::InvalidArgument( "Input(SkipUpdate) size must be 1, but get %d", skip_update_tensor->numel())); std::vector skip_update_vec; paddle::framework::TensorToVector(*skip_update_tensor, ctx.device_context(), &skip_update_vec); skip_update = skip_update_vec[0]; } // skip_update=true, just copy input to output, and TensorCopy will call // mutable_data if (skip_update) { VLOG(4) << "Adam skip update"; framework::TensorCopy( param, ctx.GetPlace(), ctx.template device_context(), ¶m_out); framework::TensorCopy( mom1, ctx.GetPlace(), ctx.template device_context(), &mom1_out); framework::TensorCopy( mom2, ctx.GetPlace(), ctx.template device_context(), &mom2_out); framework::TensorCopy( beta1_pow, beta1_pow.place(), ctx.template device_context(), beta1_pow_out); framework::TensorCopy( beta2_pow, beta2_pow.place(), ctx.template device_context(), beta2_pow_out); return; } PADDLE_ENFORCE_EQ(beta1_pow_out->numel(), 1, platform::errors::InvalidArgument( "Tensor holds the wrong size, Expected beta1 pow " "output size is 1, but received " "value is:%d.", beta1_pow_out->numel())); PADDLE_ENFORCE_EQ(beta2_pow_out->numel(), 1, platform::errors::InvalidArgument( "Tensor holds the wrong size, Expected beta2 pow " "output size is 1, but received " "value is:%d.", beta2_pow_out->numel())); bool use_global_beta_pow = ctx.Attr("use_global_beta_pow"); VLOG(4) << "use_global_beta_pow:" << use_global_beta_pow; float beta1 = static_cast(ctx.Attr("beta1")); if (ctx.HasInput("Beta1Tensor")) { auto* beta1_tensor = ctx.Input("Beta1Tensor"); beta1 = static_cast(GetAttrFromTensor(beta1_tensor)); } float beta2 = static_cast(ctx.Attr("beta2")); if (ctx.HasInput("Beta2Tensor")) { auto* beta2_tensor = ctx.Input("Beta2Tensor"); beta2 = static_cast(GetAttrFromTensor(beta2_tensor)); } float epsilon = static_cast(ctx.Attr("epsilon")); if (ctx.HasInput("EpsilonTensor")) { auto* epsilon_tensor = ctx.Input("EpsilonTensor"); epsilon = static_cast(GetAttrFromTensor(epsilon_tensor)); } if (grad_var->IsType()) { auto& grad = GET_DATA_SAFELY(ctx.Input("Grad"), "Input", "Grad", "Adam"); auto& dev_ctx = ctx.template device_context(); const float* beta1_pow_ptr = beta1_pow.template data(); const float* beta2_pow_ptr = beta2_pow.template data(); Tensor xpu_beta1_pow; Tensor xpu_beta2_pow; if (beta1_pow.place() == platform::CPUPlace() && beta2_pow.place() == platform::CPUPlace()) { paddle::framework::TensorCopy(beta1_pow, ctx.GetPlace(), dev_ctx, &xpu_beta1_pow); paddle::framework::TensorCopy(beta2_pow, ctx.GetPlace(), dev_ctx, &xpu_beta2_pow); dev_ctx.Wait(); beta1_pow_ptr = xpu_beta1_pow.template data(); beta2_pow_ptr = xpu_beta2_pow.template data(); } int r = xpu::adam(dev_ctx.x_context(), grad.template data(), mom1.template data(), mom2.template data(), param.template data(), beta1_pow_ptr, beta2_pow_ptr, lr.template data(), mom1_out.template mutable_data(ctx.GetPlace()), mom2_out.template mutable_data(ctx.GetPlace()), param_out.template mutable_data(ctx.GetPlace()), beta1, beta2, epsilon, param.numel()); xpu_wait(dev_ctx.x_context()->xpu_stream); PADDLE_ENFORCE_EQ( r == xpu::Error_t::SUCCESS, true, platform::errors::External("XPU API return wrong value[%d],", r)); if (!use_global_beta_pow) { // update in cpu and then copy to xpu if (beta1_pow.place() == platform::CPUPlace() && beta2_pow.place() == platform::CPUPlace()) { const float* beta1_pow_p = beta1_pow.template data(); beta1_pow_out->mutable_data(platform::CPUPlace())[0] = beta1 * beta1_pow_p[0]; const float* beta2_pow_p = beta2_pow.template data(); beta2_pow_out->mutable_data(platform::CPUPlace())[0] = beta2 * beta2_pow_p[0]; } else { float* beta1_pow_out_p = beta1_pow_out->mutable_data(ctx.GetPlace()); float* beta2_pow_out_p = beta2_pow_out->mutable_data(ctx.GetPlace()); int r = xpu::scale(dev_ctx.x_context(), beta1_pow_ptr, beta1_pow_out_p, beta1_pow.numel(), false, beta1, 0.0f); PADDLE_ENFORCE_EQ( r, xpu::SUCCESS, platform::errors::External( "XPU kernel scale occur error in adam error code ", r, XPUAPIErrorMsg[r])); r = xpu::scale(dev_ctx.x_context(), beta2_pow_ptr, beta2_pow_out_p, beta2_pow.numel(), false, beta2, 0.0f); PADDLE_ENFORCE_EQ( r, xpu::SUCCESS, platform::errors::External( "XPU kernel scale occur error in adam error code ", r, XPUAPIErrorMsg[r])); xpu_wait(dev_ctx.x_context()->xpu_stream); } } } else if (grad_var->IsType()) { auto* grad = ctx.Input("Grad"); auto& dev_ctx = ctx.template device_context(); if (grad->rows().size() == 0) { VLOG(3) << "grad row size is 0!!"; return; } std::vector cpu_rows(grad->rows().begin(), grad->rows().end()); bool is_strict_sorted = true; for (size_t i = 1; i < cpu_rows.size(); ++i) { if (cpu_rows[i - 1] >= cpu_rows[i]) { is_strict_sorted = false; break; } } phi::SelectedRows tmp_grad_merge; const phi::SelectedRows* grad_merge_ptr; if (is_strict_sorted) { grad_merge_ptr = grad; } else { scatter::MergeAdd merge_func; merge_func(ctx.template device_context(), *grad, &tmp_grad_merge, true); xpu_wait(dev_ctx.x_context()->xpu_stream); grad_merge_ptr = &tmp_grad_merge; } const T* beta1_pow_ptr = beta1_pow.template data(); const T* beta2_pow_ptr = beta2_pow.template data(); Tensor xpu_beta1_pow; Tensor xpu_beta2_pow; if (beta1_pow.place() == platform::CPUPlace() && beta2_pow.place() == platform::CPUPlace()) { paddle::framework::TensorCopy(beta1_pow, ctx.GetPlace(), dev_ctx, &xpu_beta1_pow); paddle::framework::TensorCopy(beta2_pow, ctx.GetPlace(), dev_ctx, &xpu_beta2_pow); dev_ctx.Wait(); beta1_pow_ptr = xpu_beta1_pow.template data(); beta2_pow_ptr = xpu_beta2_pow.template data(); } auto& grad_merge = *grad_merge_ptr; auto& grad_tensor = grad_merge.value(); const T* grad_data = grad_tensor.template data(); int row_count = grad_merge.rows().size(); std::vector rows(row_count); xpu::ctx_guard RAII_GUARD(dev_ctx.x_context()); int* xpu_rows = RAII_GUARD.alloc_l3_or_gm(row_count); std::vector merge_rows(grad_merge.rows().begin(), grad_merge.rows().end()); for (size_t i = 0; i < grad_merge.rows().size(); ++i) { rows[i] = static_cast(merge_rows[i]); } xpu_wait(dev_ctx.x_context()->xpu_stream); memory::Copy(ctx.GetPlace(), xpu_rows, platform::CPUPlace(), rows.data(), row_count * sizeof(int)); auto row_numel = grad_tensor.numel() / grad_merge.rows().size(); auto ori_rows = param.numel() / row_numel; int lazy_mode = static_cast(ctx.Attr("lazy_mode")); int r = xpu::sparse_adam( dev_ctx.x_context(), grad_data, mom1.template data(), mom2.template data(), param.template data(), beta1_pow_ptr, beta2_pow_ptr, lr.template data(), mom1_out.template mutable_data(ctx.GetPlace()), mom2_out.template mutable_data(ctx.GetPlace()), param_out.template mutable_data(ctx.GetPlace()), beta1, beta2, epsilon, ori_rows, xpu_rows, row_numel, grad_merge.rows().size(), lazy_mode); PADDLE_ENFORCE_EQ( r == xpu::Error_t::SUCCESS, true, platform::errors::External("XPU API return wrong value[%d],", r)); if (!use_global_beta_pow) { // update in cpu and then copy to xpu if (beta1_pow.place() == platform::CPUPlace() && beta2_pow.place() == platform::CPUPlace()) { const float* beta1_pow_p = beta1_pow.template data(); beta1_pow_out->mutable_data(platform::CPUPlace())[0] = beta1 * beta1_pow_p[0]; const float* beta2_pow_p = beta2_pow.template data(); beta2_pow_out->mutable_data(platform::CPUPlace())[0] = beta2 * beta2_pow_p[0]; } else { float* beta1_pow_out_p = beta1_pow_out->mutable_data(ctx.GetPlace()); float* beta2_pow_out_p = beta2_pow_out->mutable_data(ctx.GetPlace()); int r = xpu::scale(dev_ctx.x_context(), beta1_pow_ptr, beta1_pow_out_p, beta1_pow.numel(), false, beta1, 0.0f); PADDLE_ENFORCE_EQ( r, xpu::SUCCESS, platform::errors::External( "XPU kernel scale occur error in adam error code ", r, XPUAPIErrorMsg[r])); r = xpu::scale(dev_ctx.x_context(), beta2_pow_ptr, beta2_pow_out_p, beta2_pow.numel(), false, beta2, 0.0f); PADDLE_ENFORCE_EQ( r, xpu::SUCCESS, platform::errors::External( "XPU kernel scale occur error in adam error code ", r, XPUAPIErrorMsg[r])); } } xpu_wait(dev_ctx.x_context()->xpu_stream); } else { PADDLE_ENFORCE_EQ(1, 2, platform::errors::InvalidArgument( "Variable type not supported by adam_op")); } } }; #endif } // namespace operators } // namespace paddle namespace ops = paddle::operators; #ifdef PADDLE_WITH_XPU REGISTER_OP_XPU_KERNEL( adam, ops::AdamOpXPUKernel); #endif