|
| 1 | +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 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/fluid/operators/average_accumulates_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +template <> |
| 21 | +void GetAccumulators<paddle::platform::CPUDeviceContext>( |
| 22 | + const framework::ExecutionContext& ctx, int64_t& num_updates_, |
| 23 | + int64_t& num_accumulates_, int64_t& old_num_accumulates_) { |
| 24 | + auto* in_old_num_accumulates = ctx.Input<Tensor>("in_old_num_accumulates"); |
| 25 | + auto* in_num_accumulates = ctx.Input<Tensor>("in_num_accumulates"); |
| 26 | + auto* in_num_updates = ctx.Input<Tensor>("in_num_updates"); |
| 27 | + |
| 28 | + old_num_accumulates_ = in_old_num_accumulates->data<int64_t>()[0]; |
| 29 | + num_accumulates_ = in_num_accumulates->data<int64_t>()[0]; |
| 30 | + num_updates_ = in_num_updates->data<int64_t>()[0]; |
| 31 | +} |
| 32 | + |
| 33 | +template <> |
| 34 | +void SetAccumulators<paddle::platform::CPUDeviceContext>( |
| 35 | + const framework::ExecutionContext& ctx, int64_t num_updates_, |
| 36 | + int64_t num_accumulates_, int64_t old_num_accumulates_) { |
| 37 | + auto* out_old_num_accumulates = ctx.Output<Tensor>("out_old_num_accumulates"); |
| 38 | + auto* out_num_accumulates = ctx.Output<Tensor>("out_num_accumulates"); |
| 39 | + auto* out_num_updates = ctx.Output<Tensor>("out_num_updates"); |
| 40 | + |
| 41 | + out_old_num_accumulates->data<int64_t>()[0] = old_num_accumulates_; |
| 42 | + out_num_accumulates->data<int64_t>()[0] = num_accumulates_; |
| 43 | + out_num_updates->data<int64_t>()[0] = num_updates_; |
| 44 | +} |
| 45 | + |
| 46 | +class AverageAccumulatesOp : public framework::OperatorWithKernel { |
| 47 | + public: |
| 48 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 49 | + |
| 50 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 51 | + PADDLE_ENFORCE( |
| 52 | + ctx->HasInput("param"), |
| 53 | + "Input (param) of average_accumulates op should not be null."); |
| 54 | + PADDLE_ENFORCE( |
| 55 | + ctx->HasInput("in_sum_1"), |
| 56 | + "Input (sum_1) of average_accumulates op should not be null."); |
| 57 | + PADDLE_ENFORCE( |
| 58 | + ctx->HasInput("in_sum_2"), |
| 59 | + "Input (sum_2) of average_accumulates op should not be null."); |
| 60 | + PADDLE_ENFORCE( |
| 61 | + ctx->HasInput("in_sum_3"), |
| 62 | + "Input (sum_3) of average_accumulates op should not be null."); |
| 63 | + PADDLE_ENFORCE( |
| 64 | + ctx->HasInput("in_num_accumulates"), |
| 65 | + "Input (in_num_accumulates) of average_accumulates op should " |
| 66 | + "not be null."); |
| 67 | + PADDLE_ENFORCE(ctx->HasInput("in_old_num_accumulates"), |
| 68 | + "Input (old_num_accumulates) of average_accumulates op " |
| 69 | + "should not be null."); |
| 70 | + PADDLE_ENFORCE( |
| 71 | + ctx->HasInput("in_num_updates"), |
| 72 | + "Input (num_updates) of average_accumulates op should not be null."); |
| 73 | + |
| 74 | + PADDLE_ENFORCE( |
| 75 | + ctx->HasOutput("out_sum_1"), |
| 76 | + "Output (sum_1) of average_accumulates op should not be null."); |
| 77 | + PADDLE_ENFORCE( |
| 78 | + ctx->HasOutput("out_sum_2"), |
| 79 | + "Output (sum_2) of average_accumulates op should not be null."); |
| 80 | + PADDLE_ENFORCE( |
| 81 | + ctx->HasOutput("out_sum_3"), |
| 82 | + "Output (sum_3) of average_accumulates op should not be null."); |
| 83 | + PADDLE_ENFORCE(ctx->HasOutput("out_num_accumulates"), |
| 84 | + "Output (num_accumulates) of average_accumulates op should " |
| 85 | + "not be null."); |
| 86 | + PADDLE_ENFORCE(ctx->HasOutput("out_old_num_accumulates"), |
| 87 | + "Output (old_num_accumulates) of average_accumulates op " |
| 88 | + "should not be null."); |
| 89 | + PADDLE_ENFORCE( |
| 90 | + ctx->HasOutput("out_num_updates"), |
| 91 | + "Output (num_updates) of average_accumulates op should not be null."); |
| 92 | + |
| 93 | + auto in_dim = ctx->GetInputDim("param"); |
| 94 | + |
| 95 | + ctx->SetOutputDim("out_sum_1", in_dim); |
| 96 | + ctx->SetOutputDim("out_sum_2", in_dim); |
| 97 | + ctx->SetOutputDim("out_sum_3", in_dim); |
| 98 | + ctx->SetOutputDim("out_num_accumulates", {1}); |
| 99 | + ctx->SetOutputDim("out_old_num_accumulates", {1}); |
| 100 | + ctx->SetOutputDim("out_num_updates", {1}); |
| 101 | + } |
| 102 | + |
| 103 | + protected: |
| 104 | + framework::OpKernelType GetExpectedKernelType( |
| 105 | + const framework::ExecutionContext& ctx) const override { |
| 106 | + return framework::OpKernelType( |
| 107 | + framework::ToDataType(ctx.Input<Tensor>("param")->type()), |
| 108 | + ctx.GetPlace()); |
| 109 | + } |
| 110 | +}; |
| 111 | + |
| 112 | +class AverageAccumulatesOpMaker : public framework::OpProtoAndCheckerMaker { |
| 113 | + public: |
| 114 | + AverageAccumulatesOpMaker(OpProto* proto, OpAttrChecker* op_checker) |
| 115 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 116 | + AddInput("param", "(Tensor), The parameter to be accumulated."); |
| 117 | + AddInput("in_sum_1", |
| 118 | + "(Tensor), A tensor used to store the parameter " |
| 119 | + "sums with the same shape as input(param)."); |
| 120 | + AddInput("in_sum_2", |
| 121 | + "(Tensor), A auxiliary tensor to help " |
| 122 | + "accumulating sums of parameter values with the same shape as " |
| 123 | + "input(param). It is used to avoid loss of precision due to too " |
| 124 | + "many sums."); |
| 125 | + AddInput("in_sum_3", |
| 126 | + "(Tensor), A auxiliary tensor to help " |
| 127 | + "accumulating sums of parameter values with the same shape as " |
| 128 | + "input(param)."); |
| 129 | + AddInput("in_num_accumulates", |
| 130 | + "(Tensor<int64_t>), The accumulating times of current window with " |
| 131 | + "shape [1]."); |
| 132 | + AddInput( |
| 133 | + "in_old_num_accumulates", |
| 134 | + "(Tensor<int64_t>), The accumulating times of previous window with " |
| 135 | + "shape [1]."); |
| 136 | + AddInput("in_num_updates", |
| 137 | + "(Tensor<int64_t>), The total number of batches used by trainning " |
| 138 | + "before this batch with shape [1]."); |
| 139 | + |
| 140 | + AddOutput("out_sum_1", |
| 141 | + "(Tensor), A tensor used to store the " |
| 142 | + "parameter sums with the same shape as input(param)."); |
| 143 | + AddOutput("out_sum_2", |
| 144 | + "(Tensor), A auxiliary tensor to help " |
| 145 | + "accumulating sums of parameter values with the same shape as " |
| 146 | + "input(param). It is used to avoid loss of precision due to too " |
| 147 | + "many sums."); |
| 148 | + AddOutput("out_sum_3", |
| 149 | + "(Tensor), A auxiliary tensor to help " |
| 150 | + "accumulating sums of parameter values with the same shape as " |
| 151 | + "input(param)."); |
| 152 | + AddOutput( |
| 153 | + "out_num_accumulates", |
| 154 | + "(Tensor<int64_t>), The accumulating times of current window with " |
| 155 | + "shape [1]."); |
| 156 | + AddOutput( |
| 157 | + "out_old_num_accumulates", |
| 158 | + "(Tensor<int64_t>) The accumulating times of previous window with " |
| 159 | + "shape [1]."); |
| 160 | + AddOutput( |
| 161 | + "out_num_updates", |
| 162 | + "(Tensor<int64_t>), The total number of batches used by trainning " |
| 163 | + "before this batch with shape [1]."); |
| 164 | + |
| 165 | + AddAttr<float>("average_window", |
| 166 | + "(float, default 0) " |
| 167 | + "The rate of average window size relative to num_updates.") |
| 168 | + .SetDefault(0); |
| 169 | + AddAttr<int64_t>("max_average_window", |
| 170 | + "(int64_t) " |
| 171 | + "Maximum size of average window. It suggests that the " |
| 172 | + "number of mini-batches " |
| 173 | + "in one pass is appropriate value to set."); |
| 174 | + AddAttr<int64_t>("min_average_window", |
| 175 | + "(int64_t, default 10000L) " |
| 176 | + "Minimu size of average window.") |
| 177 | + .SetDefault(10000L); |
| 178 | + |
| 179 | + AddComment(R"DOC( |
| 180 | +AverageAccumulates Operator. |
| 181 | +Accumulate the sum of parameter whtin sliding window. The size of sliding window is |
| 182 | +determined by 'average_window', 'max_average_window' and 'min_average_window'. |
| 183 | +Memory was shared by Input(in_sum_1) and Output(out_sum_1) which acts as an accumulator 'sum_1'. |
| 184 | +'sum_2', 'sum_3', 'num_accumulates', 'old_num_accumulates' and 'num_updates' were the same as 'sum_1'. |
| 185 | +
|
| 186 | +All the accumulators were inited to zero before training. |
| 187 | +
|
| 188 | +And for a mini-batch in training, accumulators were computed as below steps: |
| 189 | + num_updates += 1 |
| 190 | + num_accumulates += 1 |
| 191 | + sum_1 += param |
| 192 | + if num_updates % kMaxNumAccumulates == 0: |
| 193 | + sum_2 += sum_1 |
| 194 | + sum_1 = 0 |
| 195 | + if num_accumulates >= min_average_window && num_accumulates >= min(max_average_window, num_updates * average_window): |
| 196 | + sum_3 = sum_1 + sum_2 |
| 197 | + sum_1 = 0 |
| 198 | + sum_2 = 0 |
| 199 | + old_num_accumulates = num_accumulates |
| 200 | + num_accumulates = 0 |
| 201 | +
|
| 202 | +)DOC"); |
| 203 | + } |
| 204 | +}; |
| 205 | + |
| 206 | +} // namespace operators |
| 207 | +} // namespace paddle |
| 208 | + |
| 209 | +namespace ops = paddle::operators; |
| 210 | +REGISTER_OPERATOR(average_accumulates, ops::AverageAccumulatesOp, |
| 211 | + ops::AverageAccumulatesOpMaker, |
| 212 | + paddle::framework::EmptyGradOpMaker); |
| 213 | +REGISTER_OP_CPU_KERNEL( |
| 214 | + average_accumulates, |
| 215 | + ops::AverageAccumulatesKernel<paddle::platform::CPUDeviceContext, float>, |
| 216 | + ops::AverageAccumulatesKernel<paddle::platform::CPUDeviceContext, double>); |
0 commit comments