|
| 1 | +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include <memory> |
| 16 | +#include <string> |
| 17 | + |
| 18 | +#include "paddle/fluid/operators/npu_op_runner.h" |
| 19 | +#include "paddle/fluid/operators/optimizers/adam_op.h" |
| 20 | + |
| 21 | +namespace paddle { |
| 22 | +namespace operators { |
| 23 | + |
| 24 | +using Tensor = framework::Tensor; |
| 25 | +using LoDTensor = framework::LoDTensor; |
| 26 | + |
| 27 | +template <typename DeviceContext, typename T> |
| 28 | +class AdamNPUKernel : public framework::OpKernel<T> { |
| 29 | + public: |
| 30 | + void Compute(const framework::ExecutionContext& ctx) const override { |
| 31 | + const auto* param_var = ctx.InputVar("Param"); |
| 32 | + PADDLE_ENFORCE_EQ(param_var->IsType<framework::LoDTensor>(), true, |
| 33 | + platform::errors::InvalidArgument( |
| 34 | + "The Var(%s)'s type should be LoDTensor, " |
| 35 | + "but the received is %s", |
| 36 | + ctx.InputNames("Param").front(), |
| 37 | + framework::ToTypeName(param_var->Type()))); |
| 38 | + T epsilon = static_cast<T>(ctx.Attr<float>("epsilon")); |
| 39 | + auto* param = ctx.Input<LoDTensor>("Param"); |
| 40 | + auto* grad_var = ctx.InputVar("Grad"); |
| 41 | + PADDLE_ENFORCE_EQ(grad_var->IsType<framework::LoDTensor>(), true, |
| 42 | + platform::errors::InvalidArgument( |
| 43 | + "The Grad(%s)'s type should be LoDTensor, " |
| 44 | + "but the received is %s", |
| 45 | + ctx.InputNames("Grad").front(), |
| 46 | + framework::ToTypeName(param_var->Type()))); |
| 47 | + auto* grad = ctx.Input<LoDTensor>("Grad"); |
| 48 | + auto* mom1 = ctx.Input<LoDTensor>("Moment1"); |
| 49 | + auto* mom2 = ctx.Input<LoDTensor>("Moment2"); |
| 50 | + auto* lr = ctx.Input<LoDTensor>("LearningRate"); |
| 51 | + |
| 52 | + auto* beta1_pow = ctx.Input<LoDTensor>("Beta1Pow"); |
| 53 | + auto* beta2_pow = ctx.Input<LoDTensor>("Beta2Pow"); |
| 54 | + |
| 55 | + auto* param_out = ctx.Output<LoDTensor>("ParamOut"); |
| 56 | + auto* mom1_out = ctx.Output<LoDTensor>("Moment1Out"); |
| 57 | + auto* mom2_out = ctx.Output<LoDTensor>("Moment2Out"); |
| 58 | + auto* beta1_pow_out = ctx.Output<LoDTensor>("Beta1PowOut"); |
| 59 | + auto* beta2_pow_out = ctx.Output<LoDTensor>("Beta2PowOut"); |
| 60 | + |
| 61 | + param_out->mutable_data<T>(ctx.GetPlace()); |
| 62 | + mom1_out->mutable_data<T>(ctx.GetPlace()); |
| 63 | + mom2_out->mutable_data<T>(ctx.GetPlace()); |
| 64 | + beta1_pow_out->mutable_data<T>(ctx.GetPlace()); |
| 65 | + beta2_pow_out->mutable_data<T>(ctx.GetPlace()); |
| 66 | + |
| 67 | + T beta1 = static_cast<T>(ctx.Attr<float>("beta1")); |
| 68 | + if (ctx.HasInput("Beta1Tensor")) { |
| 69 | + auto* beta1_tensor = ctx.Input<framework::Tensor>("Beta1Tensor"); |
| 70 | + PADDLE_ENFORCE_EQ(beta1_tensor->numel(), 1, |
| 71 | + platform::errors::InvalidArgument( |
| 72 | + "Input(Beta1Tensor) size must be 1, but get %d", |
| 73 | + beta1_tensor->numel())); |
| 74 | + beta1 = static_cast<T>(GetAttrFromTensor(beta1_tensor)); |
| 75 | + } |
| 76 | + T beta2 = static_cast<T>(ctx.Attr<float>("beta2")); |
| 77 | + if (ctx.HasInput("Beta2Tensor")) { |
| 78 | + auto* beta2_tensor = ctx.Input<framework::Tensor>("Beta2Tensor"); |
| 79 | + PADDLE_ENFORCE_EQ(beta2_tensor->numel(), 1, |
| 80 | + platform::errors::InvalidArgument( |
| 81 | + "Input(Beta2Tensor) size must be 1, but get %d", |
| 82 | + beta2_tensor->numel())); |
| 83 | + beta2 = static_cast<T>(GetAttrFromTensor(beta2_tensor)); |
| 84 | + } |
| 85 | + VLOG(3) << "beta1_pow.numel() : " << beta1_pow->numel() |
| 86 | + << "beta2_pow.numel() : " << beta2_pow->numel(); |
| 87 | + VLOG(3) << "param.numel(): " << param->numel(); |
| 88 | + |
| 89 | + PADDLE_ENFORCE_EQ(beta1_pow_out->numel(), 1, |
| 90 | + platform::errors::InvalidArgument( |
| 91 | + "beta1 pow output size should be 1, but received " |
| 92 | + "value is:%d.", |
| 93 | + beta1_pow_out->numel())); |
| 94 | + |
| 95 | + PADDLE_ENFORCE_EQ(beta2_pow_out->numel(), 1, |
| 96 | + platform::errors::InvalidArgument( |
| 97 | + "beta2 pow output size should be 1, but received " |
| 98 | + "value is:%d.", |
| 99 | + beta2_pow_out->numel())); |
| 100 | + |
| 101 | + // reshape |
| 102 | + Tensor beta1_tensor(framework::proto::VarType::FP32); |
| 103 | + beta1_tensor.mutable_data<float>({1}, ctx.GetPlace()); |
| 104 | + TensorFromVector(std::vector<T>{beta1}, ctx.device_context(), |
| 105 | + &beta1_tensor); |
| 106 | + Tensor beta2_tensor(framework::proto::VarType::FP32); |
| 107 | + beta2_tensor.mutable_data<float>({1}, ctx.GetPlace()); |
| 108 | + TensorFromVector(std::vector<T>{beta2}, ctx.device_context(), |
| 109 | + &beta2_tensor); |
| 110 | + |
| 111 | + Tensor epsilon_tensor(framework::proto::VarType::FP32); |
| 112 | + epsilon_tensor.mutable_data<T>({1}, ctx.GetPlace()); |
| 113 | + TensorFromVector(std::vector<T>{epsilon}, ctx.device_context(), |
| 114 | + &epsilon_tensor); |
| 115 | + auto stream = |
| 116 | + ctx.template device_context<paddle::platform::NPUDeviceContext>() |
| 117 | + .stream(); |
| 118 | + auto runner = |
| 119 | + NpuOpRunner("ApplyAdamD", |
| 120 | + { |
| 121 | + *param, *mom1, *mom2, *beta1_pow, *beta2_pow, *lr, |
| 122 | + beta1_tensor, beta2_tensor, epsilon_tensor, *grad, |
| 123 | + }, |
| 124 | + { |
| 125 | + *param_out, *mom1_out, *mom2_out, |
| 126 | + }, |
| 127 | + {}); |
| 128 | + runner.Run(stream); |
| 129 | + |
| 130 | + // NOTE(zhiqiu): ApplyAdamD updates params inplace, so |
| 131 | + // if param and param_out is not same, we need to do copy. |
| 132 | + if (param_out->data<T>() != param->data<T>()) { |
| 133 | + ctx.template device_context<paddle::platform::NPUDeviceContext>().Wait(); |
| 134 | + framework::TensorCopySync(*param, ctx.GetPlace(), param_out); |
| 135 | + } |
| 136 | + if (mom1_out->data<T>() != mom1->data<T>()) { |
| 137 | + ctx.template device_context<paddle::platform::NPUDeviceContext>().Wait(); |
| 138 | + framework::TensorCopySync(*mom1, ctx.GetPlace(), mom1_out); |
| 139 | + } |
| 140 | + if (mom2_out->data<T>() != mom2->data<T>()) { |
| 141 | + ctx.template device_context<paddle::platform::NPUDeviceContext>().Wait(); |
| 142 | + framework::TensorCopySync(*mom2, ctx.GetPlace(), mom2_out); |
| 143 | + } |
| 144 | + auto runner_m1 = |
| 145 | + NpuOpRunner("Mul", {*beta1_pow, beta1_tensor}, {*beta1_pow_out}, {}); |
| 146 | + runner_m1.Run(stream); |
| 147 | + auto runner_m2 = |
| 148 | + NpuOpRunner("Mul", {*beta2_pow, beta2_tensor}, {*beta2_pow_out}, {}); |
| 149 | + runner_m2.Run(stream); |
| 150 | + } |
| 151 | +}; |
| 152 | + |
| 153 | +} // namespace operators |
| 154 | +} // namespace paddle |
| 155 | + |
| 156 | +namespace ops = paddle::operators; |
| 157 | + |
| 158 | +REGISTER_OP_NPU_KERNEL( |
| 159 | + adam, ops::AdamNPUKernel<paddle::platform::NPUDeviceContext, float>, |
| 160 | + ops::AdamNPUKernel<paddle::platform::NPUDeviceContext, |
| 161 | + paddle::platform::float16>); |
0 commit comments