|
| 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/adamax_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +class AdamaxOp : public framework::OperatorWithKernel { |
| 21 | + public: |
| 22 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 23 | + |
| 24 | + protected: |
| 25 | + void InferShape(framework::InferShapeContextBase *ctx) const override { |
| 26 | + PADDLE_ENFORCE(ctx->HasInput("Param"), |
| 27 | + "Input(Param) of AdamaxOp should not be null."); |
| 28 | + PADDLE_ENFORCE(ctx->HasInput("Grad"), |
| 29 | + "Input(Grad) of AdamaxOp should not be null."); |
| 30 | + PADDLE_ENFORCE(ctx->HasInput("Moment"), |
| 31 | + "Input(Moment) of AdamaxOp should not be null."); |
| 32 | + PADDLE_ENFORCE(ctx->HasInput("InfNorm"), |
| 33 | + "Input(InfNorm) of AdamaxOp should not be null."); |
| 34 | + PADDLE_ENFORCE(ctx->HasInput("LearningRate"), |
| 35 | + "Input(LearningRate) of AdamaxOp should not be null."); |
| 36 | + PADDLE_ENFORCE(ctx->HasInput("Beta1Pow"), |
| 37 | + "Input(Beta1Pow) of AdamaxOp should not be null."); |
| 38 | + |
| 39 | + PADDLE_ENFORCE(ctx->HasOutput("ParamOut"), |
| 40 | + "Output(ParamOut) of AdamaxOp should not be null."); |
| 41 | + PADDLE_ENFORCE(ctx->HasOutput("MomentOut"), |
| 42 | + "Output(MomentOut) of AdamaxOp should not be null."); |
| 43 | + PADDLE_ENFORCE(ctx->HasOutput("InfNormOut"), |
| 44 | + "Output(InfNormOut) of AdamaxOp should not be null."); |
| 45 | + PADDLE_ENFORCE(ctx->HasOutput("Beta1PowOut"), |
| 46 | + "Output(Beta1PowOut) of AdamaxOp should not be null."); |
| 47 | + |
| 48 | + auto lr_dims = ctx->GetInputDim("LearningRate"); |
| 49 | + PADDLE_ENFORCE_EQ(framework::product(lr_dims), 1, |
| 50 | + "Learning rate should have 1 dimension"); |
| 51 | + auto beta1_pow_dims = ctx->GetInputDim("Beta1Pow"); |
| 52 | + PADDLE_ENFORCE_EQ(framework::product(beta1_pow_dims), 1, |
| 53 | + "Beta1 power accumulator should have 1 dimension"); |
| 54 | + auto param_dims = ctx->GetInputDim("Param"); |
| 55 | + PADDLE_ENFORCE_EQ( |
| 56 | + param_dims, ctx->GetInputDim("Grad"), |
| 57 | + "Param and Grad input of AdamaxOp should have same dimension"); |
| 58 | + PADDLE_ENFORCE_EQ( |
| 59 | + param_dims, ctx->GetInputDim("Moment"), |
| 60 | + "Param and Moment input of AdamaxOp should have same dimension"); |
| 61 | + PADDLE_ENFORCE_EQ( |
| 62 | + param_dims, ctx->GetInputDim("InfNorm"), |
| 63 | + "Param and InfNorm input of AdamaxOp should have same dimension"); |
| 64 | + |
| 65 | + ctx->SetOutputDim("ParamOut", param_dims); |
| 66 | + ctx->SetOutputDim("MomentOut", param_dims); |
| 67 | + ctx->SetOutputDim("InfNormOut", param_dims); |
| 68 | + ctx->SetOutputDim("Beta1PowOut", beta1_pow_dims); |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +class AdamaxOpMaker : public framework::OpProtoAndCheckerMaker { |
| 73 | + public: |
| 74 | + AdamaxOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker) |
| 75 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 76 | + AddInput("Param", "(Tensor) Input parameter"); |
| 77 | + AddInput("Grad", "(Tensor) Input gradient"); |
| 78 | + AddInput("LearningRate", "(Tensor) Learning rate"); |
| 79 | + AddInput("Moment", "(Tensor) First moment"); |
| 80 | + AddInput("InfNorm", |
| 81 | + "(Tensor) " |
| 82 | + "Input exponentially weighted infinity norm"); |
| 83 | + AddInput("Beta1Pow", "(Tensor) Input beta1 power accumulator"); |
| 84 | + |
| 85 | + AddOutput("ParamOut", "(Tensor) Output parameter"); |
| 86 | + AddOutput("MomentOut", "(Tensor) Output first moment"); |
| 87 | + AddOutput("InfNormOut", |
| 88 | + "(Tensor) " |
| 89 | + "Output exponentially weighted infinity norm"); |
| 90 | + AddOutput("Beta1PowOut", "(Tensor) Output beta1 power accumulator"); |
| 91 | + |
| 92 | + AddAttr<float>("beta1", |
| 93 | + "(float, default 0.9) " |
| 94 | + "Exponential decay rate for the " |
| 95 | + "1st moment estimates.") |
| 96 | + .SetDefault(0.9f); |
| 97 | + AddAttr<float>("beta2", |
| 98 | + "(float, default 0.999) " |
| 99 | + "exponential decay rate for the weighted " |
| 100 | + "infinity norm estimates.") |
| 101 | + .SetDefault(0.999f); |
| 102 | + AddAttr<float>("epsilon", |
| 103 | + "(float, default 1.0e-8) " |
| 104 | + "Constant for numerical stability") |
| 105 | + .SetDefault(1.0e-8f); |
| 106 | + AddComment(R"DOC( |
| 107 | +Adamax Updates Operator. |
| 108 | +
|
| 109 | +This implements the Adamax optimizer from Section 7 of the Adam |
| 110 | +paper[1]. Adamax is a variant of the |
| 111 | +Adam algorithm based on the infinity norm. |
| 112 | +
|
| 113 | +Adamax updates: |
| 114 | +
|
| 115 | +moment_out = beta1 * moment + (1 - beta1) * grad |
| 116 | +inf_norm_out = max(beta2 * inf_norm + epsilon, abs(grad)) |
| 117 | +beta1_pow_out = beta1_pow * beta1 |
| 118 | +learning_rate_t = learning_rate/(1 - beta1_pow_out) |
| 119 | +param_out = param - learning_rate_t * moment_out/inf_norm_out |
| 120 | +
|
| 121 | +The original paper does not have an epsilon attribute. |
| 122 | +However, it is added here for numerical stability |
| 123 | +by preventing divide by 0. |
| 124 | +
|
| 125 | +References: |
| 126 | + [1] Adam: A Method for Stochastic Optimization |
| 127 | + (https://arxiv.org/abs/1412.6980) |
| 128 | +
|
| 129 | +)DOC"); |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +} // namespace operators |
| 134 | +} // namespace paddle |
| 135 | + |
| 136 | +namespace ops = paddle::operators; |
| 137 | +REGISTER_OP_WITHOUT_GRADIENT(adamax, ops::AdamaxOp, ops::AdamaxOpMaker); |
| 138 | +REGISTER_OP_CPU_KERNEL(adamax, |
| 139 | + ops::AdamaxOpKernel<paddle::platform::CPUPlace, float>); |
0 commit comments