momentum_op.h 4.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
S
sidgoyal78 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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
S
sneaxiy 已提交
16
#include <memory>
D
dzhwinter 已提交
17
#include <string>
18

Y
Yi Wang 已提交
19 20
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
21
#include "paddle/fluid/operators/amp/fp16_type_traits.h"
22
#include "paddle/fluid/platform/float16.h"
D
dzhwinter 已提交
23
#include "paddle/fluid/platform/for_range.h"
24
#include "paddle/phi/kernels/funcs/algorithm.h"
S
sidgoyal78 已提交
25 26 27 28

namespace paddle {
namespace operators {

29 30 31 32 33
class MomentumOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override;
};

34 35 36 37 38 39
class MomentumOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {
40 41
    PADDLE_ENFORCE_EQ(ctx->HasInput("Param"),
                      true,
C
Chengmo 已提交
42 43
                      platform::errors::NotFound(
                          "Input(param) of Momentum should not be null."));
44 45
    PADDLE_ENFORCE_EQ(ctx->HasInput("Grad"),
                      true,
C
Chengmo 已提交
46 47
                      platform::errors::NotFound(
                          "Input(grad) of Momentum should not be null."));
48 49
    PADDLE_ENFORCE_EQ(ctx->HasInput("Velocity"),
                      true,
C
Chengmo 已提交
50 51 52
                      platform::errors::NotFound(
                          "Input(velocity) of Momentum should not be null."));
    PADDLE_ENFORCE_EQ(
53 54
        ctx->HasInput("LearningRate"),
        true,
C
Chengmo 已提交
55 56 57 58 59 60 61 62 63
        platform::errors::NotFound(
            "Input(LearningRate) of Momentum should not be null."));
    PADDLE_ENFORCE_EQ(
        ctx->GetInputsVarType("Param").front(),
        framework::proto::VarType::LOD_TENSOR,
        platform::errors::InvalidArgument(
            "The input var's type should be LoDTensor, but the received is %s",
            ctx->GetInputsVarType("Param").front()));

64 65
    PADDLE_ENFORCE_EQ(ctx->HasOutput("ParamOut"),
                      true,
C
Chengmo 已提交
66 67 68
                      platform::errors::NotFound(
                          "Output(ParamOut) of Momentum should not be null."));
    PADDLE_ENFORCE_EQ(
69 70
        ctx->HasOutput("VelocityOut"),
        true,
C
Chengmo 已提交
71 72
        platform::errors::NotFound(
            "Output(VelocityOut) of Momentum should not be null."));
73

74
    auto lr_dims = ctx->GetInputDim("LearningRate");
75 76
    PADDLE_ENFORCE_NE(phi::product(lr_dims),
                      0,
C
Chengmo 已提交
77 78 79 80 81
                      platform::errors::InvalidArgument(
                          "Maybe the Input variable LearningRate has not "
                          "been initialized. You may need to confirm "
                          "if you put exe.run(startup_program) "
                          "after optimizer.minimize function."));
82 83
    PADDLE_ENFORCE_EQ(phi::product(lr_dims),
                      1,
C
Chengmo 已提交
84 85 86
                      platform::errors::InvalidArgument(
                          "Learning_rate should be a scalar. But Received "
                          "LearningRate's dim [%s]",
87
                          phi::product(lr_dims)));
88

89 90 91 92
    auto param_dim = ctx->GetInputDim("Param");
    if (ctx->GetInputsVarType("Grad")[0] ==
        framework::proto::VarType::LOD_TENSOR) {
      PADDLE_ENFORCE_EQ(
93 94
          param_dim,
          ctx->GetInputDim("Grad"),
C
Chengmo 已提交
95 96 97
          platform::errors::InvalidArgument(
              "Param and Grad input of MomentumOp should have the same "
              "dimension. But received Param's dim [%s] and Grad's dim [%s].",
98 99
              param_dim,
              ctx->GetInputDim("Grad")));
100
      PADDLE_ENFORCE_EQ(
101 102
          param_dim,
          ctx->GetInputDim("Velocity"),
C
Chengmo 已提交
103 104 105
          platform::errors::InvalidArgument(
              "Param and Velocity of MomentumOp should have the same "
              "dimension. But received Param's dim [%s] and Velocity [%s].",
106 107
              param_dim,
              ctx->GetInputDim("Velocity")));
108 109 110 111
    }

    ctx->SetOutputDim("ParamOut", param_dim);
    ctx->SetOutputDim("VelocityOut", param_dim);
112 113 114
    if (ctx->HasOutput("MasterParamOut")) {
      ctx->SetOutputDim("MasterParamOut", param_dim);
    }
115
  }
S
sneaxiy 已提交
116

117 118
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
119 120
    auto input_data_type =
        OperatorWithKernel::IndicateVarDataType(ctx, "Param");
121 122 123 124
    return framework::OpKernelType(input_data_type, ctx.GetPlace());
  }
};

S
sidgoyal78 已提交
125 126
}  // namespace operators
}  // namespace paddle