average_accumulates_op.cc 8.5 KB
Newer Older
W
wanghaoshuang 已提交
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

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. */

#include "paddle/fluid/operators/average_accumulates_op.h"

namespace paddle {
namespace operators {

template <>
W
wanghaoshuang 已提交
21
void GetAccumulators<paddle::platform::CPUDeviceContext>(
22 23
    const framework::ExecutionContext& ctx, int64_t* num_updates,
    int64_t* num_accumulates, int64_t* old_num_accumulates) {
W
wanghaoshuang 已提交
24 25 26
  auto* in_old_num_accumulates = ctx.Input<Tensor>("in_old_num_accumulates");
  auto* in_num_accumulates = ctx.Input<Tensor>("in_num_accumulates");
  auto* in_num_updates = ctx.Input<Tensor>("in_num_updates");
27

28 29 30
  *old_num_accumulates = in_old_num_accumulates->data<int64_t>()[0];
  *num_accumulates = in_num_accumulates->data<int64_t>()[0];
  *num_updates = in_num_updates->data<int64_t>()[0];
31 32 33
}

template <>
W
wanghaoshuang 已提交
34
void SetAccumulators<paddle::platform::CPUDeviceContext>(
35 36
    const framework::ExecutionContext& ctx, int64_t num_updates,
    int64_t num_accumulates, int64_t old_num_accumulates) {
W
wanghaoshuang 已提交
37 38 39
  auto* out_old_num_accumulates = ctx.Output<Tensor>("out_old_num_accumulates");
  auto* out_num_accumulates = ctx.Output<Tensor>("out_num_accumulates");
  auto* out_num_updates = ctx.Output<Tensor>("out_num_updates");
40

41 42 43
  out_old_num_accumulates->data<int64_t>()[0] = old_num_accumulates;
  out_num_accumulates->data<int64_t>()[0] = num_accumulates;
  out_num_updates->data<int64_t>()[0] = num_updates;
44 45 46 47 48 49 50
}

class AverageAccumulatesOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    OP_INOUT_CHECK(ctx->HasInput("param"), "Input", "param",
                   "AverageAccumulates");
    OP_INOUT_CHECK(ctx->HasInput("in_sum_1"), "Input", "in_sum_1",
                   "AverageAccumulates");
    OP_INOUT_CHECK(ctx->HasInput("in_sum_2"), "Input", "in_sum_2",
                   "AverageAccumulates");
    OP_INOUT_CHECK(ctx->HasInput("in_sum_3"), "Input", "in_sum_3",
                   "AverageAccumulates");
    OP_INOUT_CHECK(ctx->HasInput("in_num_accumulates"), "Input",
                   "in_num_accumulates", "AverageAccumulates");
    OP_INOUT_CHECK(ctx->HasInput("in_old_num_accumulates"), "Input",
                   "in_old_num_accumulates", "AverageAccumulates");
    OP_INOUT_CHECK(ctx->HasInput("in_num_updates"), "Input", "in_num_updates",
                   "AverageAccumulates");

    OP_INOUT_CHECK(ctx->HasOutput("out_sum_1"), "Output", "out_sum_1",
                   "AverageAccumulates");
    OP_INOUT_CHECK(ctx->HasOutput("out_sum_2"), "Output", "out_sum_2",
                   "AverageAccumulates");
    OP_INOUT_CHECK(ctx->HasOutput("out_sum_3"), "Output", "out_sum_3",
                   "AverageAccumulates");
    OP_INOUT_CHECK(ctx->HasOutput("out_num_accumulates"), "Output",
                   "out_num_accumulates", "AverageAccumulates");
    OP_INOUT_CHECK(ctx->HasOutput("out_old_num_accumulates"), "Output",
                   "out_old_num_accumulates", "AverageAccumulates");
    OP_INOUT_CHECK(ctx->HasOutput("out_num_updates"), "Output",
                   "out_num_updates", "AverageAccumulates");
W
wanghaoshuang 已提交
78
    auto in_dim = ctx->GetInputDim("param");
79

W
wanghaoshuang 已提交
80 81 82 83 84 85
    ctx->SetOutputDim("out_sum_1", in_dim);
    ctx->SetOutputDim("out_sum_2", in_dim);
    ctx->SetOutputDim("out_sum_3", in_dim);
    ctx->SetOutputDim("out_num_accumulates", {1});
    ctx->SetOutputDim("out_old_num_accumulates", {1});
    ctx->SetOutputDim("out_num_updates", {1});
86 87 88 89 90
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
91 92
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "param"), ctx.GetPlace());
93 94 95 96 97
  }
};

class AverageAccumulatesOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
98
  void Make() override {
W
wanghaoshuang 已提交
99
    AddInput("param", "(Tensor), The parameter to be accumulated.");
W
wanghaoshuang 已提交
100
    AddInput("in_sum_1",
W
wanghaoshuang 已提交
101
             "(Tensor), A tensor used to store the parameter "
W
wanghaoshuang 已提交
102 103
             "sums with the same shape as input(param).");
    AddInput("in_sum_2",
W
wanghaoshuang 已提交
104
             "(Tensor), A auxiliary tensor to help "
W
wanghaoshuang 已提交
105 106 107 108
             "accumulating sums of parameter values with the same shape as "
             "input(param). It is used to avoid loss of precision due to too "
             "many sums.");
    AddInput("in_sum_3",
W
wanghaoshuang 已提交
109
             "(Tensor), A auxiliary tensor to help "
W
wanghaoshuang 已提交
110 111 112
             "accumulating sums of parameter values with the same shape as "
             "input(param).");
    AddInput("in_num_accumulates",
W
wanghaoshuang 已提交
113
             "(Tensor<int64_t>), The accumulating times of current window with "
W
wanghaoshuang 已提交
114
             "shape [1].");
W
wanghaoshuang 已提交
115 116 117 118
    AddInput(
        "in_old_num_accumulates",
        "(Tensor<int64_t>), The accumulating times of previous window with "
        "shape [1].");
W
wanghaoshuang 已提交
119
    AddInput("in_num_updates",
T
tianshuo78520a 已提交
120
             "(Tensor<int64_t>), The total number of batches used by training "
W
wanghaoshuang 已提交
121 122 123
             "before this batch with shape [1].");

    AddOutput("out_sum_1",
W
wanghaoshuang 已提交
124
              "(Tensor), A tensor used to store the "
W
wanghaoshuang 已提交
125 126
              "parameter sums with the same shape as input(param).");
    AddOutput("out_sum_2",
W
wanghaoshuang 已提交
127
              "(Tensor), A auxiliary tensor to help "
W
wanghaoshuang 已提交
128 129 130 131
              "accumulating sums of parameter values with the same shape as "
              "input(param). It is used to avoid loss of precision due to too "
              "many sums.");
    AddOutput("out_sum_3",
W
wanghaoshuang 已提交
132
              "(Tensor), A auxiliary tensor to help "
W
wanghaoshuang 已提交
133 134
              "accumulating sums of parameter values with the same shape as "
              "input(param).");
W
wanghaoshuang 已提交
135 136 137 138 139 140 141 142
    AddOutput(
        "out_num_accumulates",
        "(Tensor<int64_t>), The accumulating times of current window with "
        "shape [1].");
    AddOutput(
        "out_old_num_accumulates",
        "(Tensor<int64_t>) The accumulating times of previous window with "
        "shape [1].");
T
tianshuo78520a 已提交
143 144 145
    AddOutput("out_num_updates",
              "(Tensor<int64_t>), The total number of batches used by training "
              "before this batch with shape [1].");
W
wanghaoshuang 已提交
146 147

    AddAttr<float>("average_window",
W
wanghaoshuang 已提交
148 149 150 151 152 153 154 155 156 157 158 159
                   "(float, default 0) "
                   "The rate of average window size relative to num_updates.")
        .SetDefault(0);
    AddAttr<int64_t>("max_average_window",
                     "(int64_t) "
                     "Maximum size of average window. It suggests that the "
                     "number of mini-batches "
                     "in one pass is appropriate value to set.");
    AddAttr<int64_t>("min_average_window",
                     "(int64_t, default 10000L) "
                     "Minimu size of average window.")
        .SetDefault(10000L);
160 161 162

    AddComment(R"DOC(
AverageAccumulates Operator.
163
Accumulate the sum of parameter within sliding window. The size of sliding window is
W
wanghaoshuang 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
determined by 'average_window', 'max_average_window' and 'min_average_window'.
Memory was shared by Input(in_sum_1) and Output(out_sum_1) which acts as an accumulator 'sum_1'.
'sum_2', 'sum_3', 'num_accumulates', 'old_num_accumulates' and 'num_updates' were the same as 'sum_1'.

All the accumulators were inited to zero before training.

And for a mini-batch in training, accumulators were computed as below steps:
    num_updates += 1
    num_accumulates += 1
    sum_1 += param
    if num_updates % kMaxNumAccumulates == 0:
        sum_2 += sum_1
        sum_1 = 0
    if num_accumulates >= min_average_window && num_accumulates >= min(max_average_window, num_updates * average_window):
        sum_3 = sum_1 + sum_2
        sum_1 = 0
        sum_2 = 0
        old_num_accumulates = num_accumulates
        num_accumulates = 0

184 185 186 187 188 189 190 191
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
H
hong 已提交
192 193 194 195 196
REGISTER_OPERATOR(
    average_accumulates, ops::AverageAccumulatesOp,
    ops::AverageAccumulatesOpMaker,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
197
REGISTER_OP_CPU_KERNEL(
W
wanghaoshuang 已提交
198
    average_accumulates,
199 200
    ops::AverageAccumulatesKernel<paddle::platform::CPUDeviceContext, float>,
    ops::AverageAccumulatesKernel<paddle::platform::CPUDeviceContext, double>);