lamb_op_xpu.cc 4.5 KB
Newer Older
G
Gradie 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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/operators/optimizers/lamb_op.h"
#include "gflags/gflags.h"
17
#include "paddle/fluid/platform/device/device_wrapper.h"
G
Gradie 已提交
18 19 20 21 22 23 24 25 26 27 28

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

#ifdef PADDLE_WITH_XPU
template <typename DeviceContext, typename T>
class LambOpXPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
29 30 31 32 33 34 35 36
    using paddle::framework::LoDTensor;
    const auto* param_var = ctx.InputVar("Param");
    PADDLE_ENFORCE_EQ(param_var->IsType<framework::LoDTensor>(), true,
                      platform::errors::InvalidArgument(
                          "The Var(%s)'s type should be LoDTensor, "
                          "but the received is %s",
                          ctx.InputNames("Param").front(),
                          framework::ToTypeName(param_var->Type())));
G
Gradie 已提交
37

38
    using paddle::framework::LoDTensor;
G
Gradie 已提交
39

40 41 42 43 44 45 46 47 48 49 50 51 52 53
    // inputs
    T epsilon = static_cast<T>(ctx.Attr<float>("epsilon"));
    T weight_decay = static_cast<T>(ctx.Attr<float>("weight_decay"));
    T beta1 = static_cast<T>(ctx.Attr<float>("beta1"));
    T beta2 = static_cast<T>(ctx.Attr<float>("beta2"));
    auto& param = GET_DATA_SAFELY(ctx.Input<LoDTensor>("Param"), "Input",
                                  "Param", "Lamb");
    auto* grad_var = ctx.InputVar("Grad");
    auto& mom1 = GET_DATA_SAFELY(ctx.Input<LoDTensor>("Moment1"), "Input",
                                 "Moment1", "Lamb");
    auto& mom2 = GET_DATA_SAFELY(ctx.Input<LoDTensor>("Moment2"), "Input",
                                 "Moment2", "Lamb");
    auto& lr = GET_DATA_SAFELY(ctx.Input<LoDTensor>("LearningRate"), "Input",
                               "LearningRate", "Lamb");
G
Gradie 已提交
54

55 56 57 58
    auto& beta1_pow = GET_DATA_SAFELY(ctx.Input<LoDTensor>("Beta1Pow"), "Input",
                                      "Beta1Pow", "Lamb");
    auto& beta2_pow = GET_DATA_SAFELY(ctx.Input<LoDTensor>("Beta2Pow"), "Input",
                                      "Beta2Pow", "Lamb");
G
Gradie 已提交
59

60 61 62 63 64 65 66 67 68 69 70
    auto& param_out = GET_DATA_SAFELY(ctx.Output<LoDTensor>("ParamOut"),
                                      "Output", "ParamOut", "Lamb");
    auto& mom1_out = GET_DATA_SAFELY(ctx.Output<LoDTensor>("Moment1Out"),
                                     "Output", "Moment1Out", "Lamb");
    auto& mom2_out = GET_DATA_SAFELY(ctx.Output<LoDTensor>("Moment2Out"),
                                     "Output", "Moment2Out", "Lamb");
    auto& beta1_pow_out = GET_DATA_SAFELY(ctx.Output<LoDTensor>("Beta1PowOut"),
                                          "Output", "Beta1PowOut", "Lamb");
    auto& beta2_pow_out = GET_DATA_SAFELY(ctx.Output<LoDTensor>("Beta2PowOut"),
                                          "Output", "Beta2PowOut", "Lamb");
    auto& dev_ctx = ctx.template device_context<DeviceContext>();
G
Gradie 已提交
71

72 73 74 75 76 77 78 79 80 81 82 83
    if (grad_var->IsType<framework::LoDTensor>()) {
      auto& grad = *ctx.Input<LoDTensor>("Grad");
      int r = xpu::lamb(
          dev_ctx.x_context(), grad.template data<T>(), mom1.template data<T>(),
          mom2.template data<T>(), param.template data<T>(),
          beta1_pow.template data<T>(), beta2_pow.template data<T>(),
          mom1_out.template mutable_data<T>(ctx.GetPlace()),
          mom2_out.template mutable_data<T>(ctx.GetPlace()),
          param_out.template mutable_data<T>(ctx.GetPlace()),
          beta1_pow_out.template mutable_data<T>(ctx.GetPlace()),
          beta2_pow_out.template mutable_data<T>(ctx.GetPlace()), beta1, beta2,
          epsilon, weight_decay, lr.template data<T>(), param.numel());
G
Gradie 已提交
84

85 86 87 88 89 90 91
      PADDLE_ENFORCE_XDNN_SUCCESS(r, "lamb");
    } else {
      PADDLE_THROW(platform::errors::InvalidArgument(
          "Variable type not supported by lamb_op. Expect LoDTensor, "
          "but got %s",
          framework::ToTypeName(param_var->Type())));
    }
G
Gradie 已提交
92 93 94 95 96
  }
};
}  // namespace operators
}  // namespace paddle

97 98 99
namespace ops = paddle::operators;
REGISTER_OP_XPU_KERNEL(
    lamb, ops::LambOpXPUKernel<paddle::platform::XPUDeviceContext, float>);
G
Gradie 已提交
100
#endif