merged_adam_op.h 5.4 KB
Newer Older
Z
zhangbo9674 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2021 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 "paddle/fluid/framework/op_registry.h"
14 15
#include "paddle/fluid/operators/math/selected_rows_functor.h"
#include "paddle/phi/kernels/funcs/adam_functors.h"
Z
zhangbo9674 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28

namespace paddle {
namespace operators {

namespace scatter = paddle::operators::math::scatter;

template <typename DeviceContext, typename T>
class MergedAdamOpKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    auto param = ctx.MultiInput<framework::Tensor>("Param");
    size_t n = param.size();
    auto grad = ctx.MultiInput<framework::Tensor>("Grad");
29 30
    PADDLE_ENFORCE_EQ(n,
                      grad.size(),
Z
zhangbo9674 已提交
31 32 33 34
                      platform::errors::InvalidArgument(
                          "The size of Input(Grad) must be equal to "
                          "Input(Param), but got the size of Input(Grad) "
                          "is %d, the size of Input(Param) is %d.",
35 36
                          grad.size(),
                          n));
Z
zhangbo9674 已提交
37 38
    auto lr = ctx.MultiInput<framework::Tensor>("LearningRate");
    PADDLE_ENFORCE_EQ(
39 40
        n,
        lr.size(),
Z
zhangbo9674 已提交
41 42 43 44
        platform::errors::InvalidArgument(
            "The size of Input(LearningRate) must be equal to "
            "Input(Param), but got the size of Input(LearningRate) "
            "is %d, the size of Input(Param) is %d.",
45 46
            lr.size(),
            n));
Z
zhangbo9674 已提交
47
    auto mom1 = ctx.MultiInput<framework::Tensor>("Moment1");
48 49
    PADDLE_ENFORCE_EQ(n,
                      mom1.size(),
Z
zhangbo9674 已提交
50 51 52 53
                      platform::errors::InvalidArgument(
                          "The size of Input(Moment1) must be equal to "
                          "Input(Param), but got the size of Input(Moment1) "
                          "is %d, the size of Input(Param) is %d.",
54 55
                          mom1.size(),
                          n));
Z
zhangbo9674 已提交
56
    auto mom2 = ctx.MultiInput<framework::Tensor>("Moment2");
57 58
    PADDLE_ENFORCE_EQ(n,
                      mom2.size(),
Z
zhangbo9674 已提交
59 60 61 62
                      platform::errors::InvalidArgument(
                          "The size of Input(Moment2) must be equal to "
                          "Input(Param), but got the size of Input(Moment2) "
                          "is %d, the size of Input(Param) is %d.",
63 64
                          mom2.size(),
                          n));
Z
zhangbo9674 已提交
65
    auto beta1_pow = ctx.MultiInput<framework::Tensor>("Beta1Pow");
66 67
    PADDLE_ENFORCE_EQ(n,
                      beta1_pow.size(),
Z
zhangbo9674 已提交
68 69 70 71
                      platform::errors::InvalidArgument(
                          "The size of Input(Beta1Pow) must be equal to "
                          "Input(Param), but got the size of Input(Beta1Pow) "
                          "is %d, the size of Input(Param) is %d.",
72 73
                          beta1_pow.size(),
                          n));
Z
zhangbo9674 已提交
74
    auto beta2_pow = ctx.MultiInput<framework::Tensor>("Beta2Pow");
75 76
    PADDLE_ENFORCE_EQ(n,
                      beta2_pow.size(),
Z
zhangbo9674 已提交
77 78 79 80
                      platform::errors::InvalidArgument(
                          "The size of Input(Beta2Pow) must be equal to "
                          "Input(Param), but got the size of Input(Beta2Pow) "
                          "is %d, the size of Input(Param) is %d.",
81 82
                          beta2_pow.size(),
                          n));
Z
zhangbo9674 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

    auto param_out = ctx.MultiOutput<framework::Tensor>("ParamOut");
    auto mom1_out = ctx.MultiOutput<framework::Tensor>("Moment1Out");
    auto mom2_out = ctx.MultiOutput<framework::Tensor>("Moment2Out");
    auto beta1_pow_out = ctx.MultiOutput<framework::Tensor>("Beta1PowOut");
    auto beta2_pow_out = ctx.MultiOutput<framework::Tensor>("Beta2PowOut");

    T beta1 = static_cast<T>(ctx.Attr<float>("beta1"));
    T beta2 = static_cast<T>(ctx.Attr<float>("beta2"));
    T epsilon = static_cast<T>(ctx.Attr<float>("epsilon"));
    bool use_global_beta_pow = ctx.Attr<bool>("use_global_beta_pow");
    VLOG(4) << "use_global_beta_pow:" << use_global_beta_pow;

    size_t param_num = param.size();
    for (size_t idx = 0; idx < param_num; idx++) {
98
      phi::funcs::AdamFunctor<T, phi::funcs::CPUAdam> functor(
99 100 101 102 103 104 105 106 107 108 109 110
          beta1,
          beta2,
          epsilon,
          beta1_pow[idx]->data<T>(),
          beta2_pow[idx]->data<T>(),
          mom1[idx]->data<T>(),
          mom1_out[idx]->mutable_data<T>(ctx.GetPlace()),
          mom2[idx]->data<T>(),
          mom2_out[idx]->mutable_data<T>(ctx.GetPlace()),
          lr[idx]->data<T>(),
          grad[idx]->data<T>(),
          param[idx]->data<T>(),
Z
zhangbo9674 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124
          param_out[idx]->mutable_data<T>(ctx.GetPlace()));
      functor(param[idx]->numel());
      if (!use_global_beta_pow) {
        beta1_pow_out[idx]->mutable_data<T>(ctx.GetPlace())[0] =
            beta1 * beta1_pow[idx]->data<T>()[0];
        beta2_pow_out[idx]->mutable_data<T>(ctx.GetPlace())[0] =
            beta2 * beta2_pow[idx]->data<T>()[0];
      }
    }
  }
};

}  // namespace operators
}  // namespace paddle