|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 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 "paddle/operators/adam_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +class AdamOp : public framework::OperatorWithKernel { |
| 21 | + public: |
| 22 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 23 | + |
| 24 | + protected: |
| 25 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 26 | + PADDLE_ENFORCE(ctx->HasInput("Param"), |
| 27 | + "Input(Param) of AdamOp should not be null."); |
| 28 | + PADDLE_ENFORCE(ctx->HasInput("Grad"), |
| 29 | + "Input(Grad) of AdamOp should not be null."); |
| 30 | + PADDLE_ENFORCE(ctx->HasInput("Moment1"), |
| 31 | + "Input(Moment1) of AdamOp should not be null."); |
| 32 | + PADDLE_ENFORCE(ctx->HasInput("Moment2"), |
| 33 | + "Input(Moment2) of AdamOp should not be null."); |
| 34 | + PADDLE_ENFORCE(ctx->HasInput("LearningRate"), |
| 35 | + "Input(LearningRate) of AdamOp should not be null."); |
| 36 | + PADDLE_ENFORCE(ctx->HasInput("Beta1Pow"), |
| 37 | + "Input(Beta1Pow) of AdamOp should not be null."); |
| 38 | + PADDLE_ENFORCE(ctx->HasInput("Beta2Pow"), |
| 39 | + "Input(Beta2Pow) of AdamOp should not be null."); |
| 40 | + |
| 41 | + PADDLE_ENFORCE(ctx->HasOutput("ParamOut"), |
| 42 | + "Output(ParamOut) of AdamOp should not be null."); |
| 43 | + PADDLE_ENFORCE(ctx->HasOutput("Moment1Out"), |
| 44 | + "Output(Moment1Out) of AdamOp should not be null."); |
| 45 | + PADDLE_ENFORCE(ctx->HasOutput("Moment2Out"), |
| 46 | + "Output(Moment2Out) of AdamOp should not be null."); |
| 47 | + PADDLE_ENFORCE(ctx->HasOutput("Beta1PowOut"), |
| 48 | + "Output(Beta1PowOut) of AdamOp should not be null."); |
| 49 | + PADDLE_ENFORCE(ctx->HasOutput("Beta2PowOut"), |
| 50 | + "Output(Beta2PowOut) of AdamOp should not be null."); |
| 51 | + |
| 52 | + auto lr_dims = ctx->GetInputDim("LearningRate"); |
| 53 | + PADDLE_ENFORCE_EQ(framework::product(lr_dims), 1, |
| 54 | + "Learning rate should have 1 dimension"); |
| 55 | + auto beta1_pow_dims = ctx->GetInputDim("Beta1Pow"); |
| 56 | + PADDLE_ENFORCE_EQ(framework::product(beta1_pow_dims), 1, |
| 57 | + "Beta1 power accumulator should have 1 dimension"); |
| 58 | + auto beta2_pow_dims = ctx->GetInputDim("Beta2Pow"); |
| 59 | + PADDLE_ENFORCE_EQ(framework::product(beta1_pow_dims), 1, |
| 60 | + "Beta1 power accumulator should have 1 dimension"); |
| 61 | + |
| 62 | + auto param_dims = ctx->GetInputDim("Param"); |
| 63 | + PADDLE_ENFORCE_EQ( |
| 64 | + param_dims, ctx->GetInputDim("Grad"), |
| 65 | + "Param and Grad input of AdamOp should have same dimension"); |
| 66 | + PADDLE_ENFORCE_EQ( |
| 67 | + param_dims, ctx->GetInputDim("Moment1"), |
| 68 | + "Param and Moment input of AdamOp should have same dimension"); |
| 69 | + PADDLE_ENFORCE_EQ( |
| 70 | + param_dims, ctx->GetInputDim("Moment2"), |
| 71 | + "Param and InfNorm input of AdamOp should have same dimension"); |
| 72 | + |
| 73 | + ctx->SetOutputDim("ParamOut", param_dims); |
| 74 | + ctx->SetOutputDim("Moment1Out", param_dims); |
| 75 | + ctx->SetOutputDim("Moment2Out", param_dims); |
| 76 | + ctx->SetOutputDim("Beta1PowOut", beta1_pow_dims); |
| 77 | + ctx->SetOutputDim("Beta2PowOut", beta2_pow_dims); |
| 78 | + } |
| 79 | +}; |
| 80 | + |
| 81 | +class AdamOpMaker : public framework::OpProtoAndCheckerMaker { |
| 82 | + public: |
| 83 | + AdamOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker) |
| 84 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 85 | + AddInput("Param", "(Tensor) Input parameter"); |
| 86 | + AddInput("Grad", "(Tensor) Input gradient"); |
| 87 | + AddInput("LearningRate", "(Tensor) Learning rate"); |
| 88 | + AddInput("Moment1", "(Tensor) Input first moment"); |
| 89 | + AddInput("Moment2", "(Tensor) Input second moment"); |
| 90 | + AddInput("Beta1Pow", "(Tensor) Input beta1 power accumulator"); |
| 91 | + AddInput("Beta2Pow", "(Tensor) Input beta2 power accumulator"); |
| 92 | + |
| 93 | + AddOutput("ParamOut", "(Tensor) Output parameter"); |
| 94 | + AddOutput("Moment1Out", "(Tensor) Output first moment"); |
| 95 | + AddOutput("Moment2Out", "(Tensor) Output second moment"); |
| 96 | + AddOutput("Beta1PowOut", "(Tensor) Output beta1 power accumulator"); |
| 97 | + AddOutput("Beta2PowOut", "(Tensor) Output beta2 power accumulator"); |
| 98 | + |
| 99 | + AddAttr<float>("beta1", |
| 100 | + "(float, default 0.9) " |
| 101 | + "Exponential decay rate for the " |
| 102 | + "first moment estimates.") |
| 103 | + .SetDefault(0.9f); |
| 104 | + AddAttr<float>("beta2", |
| 105 | + "(float, default 0.999) " |
| 106 | + "exponential decay rate for the " |
| 107 | + "second moment estimates.") |
| 108 | + .SetDefault(0.999f); |
| 109 | + AddAttr<float>("epsilon", |
| 110 | + "(float, default 1.0e-8) " |
| 111 | + "Constant for numerical stability") |
| 112 | + .SetDefault(1.0e-8f); |
| 113 | + |
| 114 | + AddComment(R"DOC( |
| 115 | +Adam Updates Operator. |
| 116 | +
|
| 117 | +This implements the Adam optimizer from Section 2 of the Adam |
| 118 | +paper[1]. Adam is a first-order gradient-based optimization |
| 119 | +method based on adaptive estimates of lower-order moments. |
| 120 | +
|
| 121 | +Adam updates: |
| 122 | +
|
| 123 | +moment1_out = beta1 * moment1 + (1 − beta1) * grad |
| 124 | +moment2_out = beta2 * moment2 + (1 − beta2) * grad * grad |
| 125 | +beta1_pow_out = beta1_pow * beta1 |
| 126 | +beta2_pow_out = beta2_pow * beta2 |
| 127 | +learning_rate_t = learning_rate_t * |
| 128 | + sqrt(1 - beta2_pow_out) / (1 - beta1_pow_out) |
| 129 | +param_out = param - learning_rate_t * moment1/ (sqrt(moment2) + epsilon) |
| 130 | +
|
| 131 | +References: |
| 132 | + [1] Adam: A Method for Stochastic Optimization |
| 133 | + (https://arxiv.org/abs/1412.6980) |
| 134 | +
|
| 135 | +)DOC"); |
| 136 | + } |
| 137 | +}; |
| 138 | +} // namespace operators |
| 139 | +} // namespace paddle |
| 140 | + |
| 141 | +namespace ops = paddle::operators; |
| 142 | +REGISTER_OP_WITHOUT_GRADIENT(adam, ops::AdamOp, ops::AdamOpMaker); |
| 143 | +REGISTER_OP_CPU_KERNEL(adam, |
| 144 | + ops::AdamOpKernel<paddle::platform::CPUPlace, float>); |
0 commit comments