average_accumulates_op.cc 7.9 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 21 22 23

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 <>
void getAccumulators<paddle::platform::CPUDeviceContext>(
    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 31 32 33 34 35 36

  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];
}

template <>
void setAccumulators<paddle::platform::CPUDeviceContext>(
    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 44 45 46 47 48 49 50 51

  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_;
}

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

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(
W
wanghaoshuang 已提交
52 53
        ctx->HasInput("param"),
        "Input (param) of average_accumulates op should not be null.");
54
    PADDLE_ENFORCE(
W
wanghaoshuang 已提交
55
        ctx->HasInput("in_sum_1"),
56 57
        "Input (sum_1) of average_accumulates op should not be null.");
    PADDLE_ENFORCE(
W
wanghaoshuang 已提交
58
        ctx->HasInput("in_sum_2"),
59 60
        "Input (sum_2) of average_accumulates op should not be null.");
    PADDLE_ENFORCE(
W
wanghaoshuang 已提交
61
        ctx->HasInput("in_sum_3"),
62
        "Input (sum_3) of average_accumulates op should not be null.");
W
wanghaoshuang 已提交
63 64 65 66 67
    PADDLE_ENFORCE(
        ctx->HasInput("in_num_accumulates"),
        "Input (in_num_accumulates) of average_accumulates op should "
        "not be null.");
    PADDLE_ENFORCE(ctx->HasInput("in_old_num_accumulates"),
68 69 70
                   "Input (old_num_accumulates) of average_accumulates op "
                   "should not be null.");
    PADDLE_ENFORCE(
W
wanghaoshuang 已提交
71
        ctx->HasInput("in_num_updates"),
72 73 74
        "Input (num_updates) of average_accumulates op should not be null.");

    PADDLE_ENFORCE(
W
wanghaoshuang 已提交
75
        ctx->HasOutput("out_sum_1"),
76 77
        "Output (sum_1) of average_accumulates op should not be null.");
    PADDLE_ENFORCE(
W
wanghaoshuang 已提交
78
        ctx->HasOutput("out_sum_2"),
79 80
        "Output (sum_2) of average_accumulates op should not be null.");
    PADDLE_ENFORCE(
W
wanghaoshuang 已提交
81
        ctx->HasOutput("out_sum_3"),
82
        "Output (sum_3) of average_accumulates op should not be null.");
W
wanghaoshuang 已提交
83
    PADDLE_ENFORCE(ctx->HasOutput("out_num_accumulates"),
84 85
                   "Output (num_accumulates) of average_accumulates op should "
                   "not be null.");
W
wanghaoshuang 已提交
86
    PADDLE_ENFORCE(ctx->HasOutput("out_old_num_accumulates"),
87 88 89
                   "Output (old_num_accumulates) of average_accumulates op "
                   "should not be null.");
    PADDLE_ENFORCE(
W
wanghaoshuang 已提交
90
        ctx->HasOutput("out_num_updates"),
91 92
        "Output (num_updates) of average_accumulates op should not be null.");

W
wanghaoshuang 已提交
93
    auto in_dim = ctx->GetInputDim("param");
94

W
wanghaoshuang 已提交
95 96 97 98 99 100
    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});
101 102 103 104 105 106
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
W
wanghaoshuang 已提交
107
        framework::ToDataType(ctx.Input<Tensor>("param")->type()),
108 109 110 111 112 113 114 115
        ctx.GetPlace());
  }
};

class AverageAccumulatesOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  AverageAccumulatesOpMaker(OpProto* proto, OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
W
wanghaoshuang 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    AddInput("param",
             "Input(Tensor or LoDTensor): The parameter to be accumulated.");
    AddInput("in_sum_1",
             "Input(Tensor or LoDTensor): A tensor used to store the parameter "
             "sums with the same shape as input(param).");
    AddInput("in_sum_2",
             "Input(Tensor or LoDTensor): A auxiliary tensor to help "
             "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",
             "Input(Tensor or LoDTensor): A auxiliary tensor to help "
             "accumulating sums of parameter values with the same shape as "
             "input(param).");
    AddInput("in_num_accumulates",
             "Input(Tensor): The accumulating times of current window with "
             "shape [1].");
    AddInput("in_old_num_accumulates",
             "Input(Tensor): The accumulating times of previous window with "
             "shape [1].");
    AddInput("in_num_updates",
             "Input(Tensor): The total number of batches used by trainning "
             "before this batch with shape [1].");

    AddOutput("out_sum_1",
              "Output(Tensor or LoDTensor): A tensor used to store the "
              "parameter sums with the same shape as input(param).");
    AddOutput("out_sum_2",
              "Output(Tensor or LoDTensor): A auxiliary tensor to help "
              "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",
              "Output(Tensor or LoDTensor): A auxiliary tensor to help "
              "accumulating sums of parameter values with the same shape as "
              "input(param).");
    AddOutput("out_num_accumulates",
              "Output(Tensor): The accumulating times of current window with "
              "shape [1].");
    AddOutput("out_old_num_accumulates",
              "Output(Tensor): The accumulating times of previous window with "
              "shape [1].");
    AddOutput("out_num_updates",
              "Output(Tensor): The total number of batches used by trainning "
              "before this batch with shape [1].");

    AddAttr<float>("average_window",
                   "The rate of average window size relative to num_updates.");
    AddAttr<int64_t>("max_average_window", "Maximum size of average window.");
    AddAttr<int64_t>("min_average_window", "Minimu size of average window.");
166 167 168

    AddComment(R"DOC(
AverageAccumulates Operator.
W
wanghaoshuang 已提交
169
Accumulate the sum of parameter whtin sliding window. The size of sliding window is determined by 'average_window', 'max_average_window' and 'min_average_window'.
170 171 172 173 174 175 176 177
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
W
wanghaoshuang 已提交
178
REGISTER_OPERATOR(average_accumulates, ops::AverageAccumulatesOp,
179 180 181
                  ops::AverageAccumulatesOpMaker,
                  paddle::framework::EmptyGradOpMaker);
REGISTER_OP_CPU_KERNEL(
W
wanghaoshuang 已提交
182
    average_accumulates,
183 184
    ops::AverageAccumulatesKernel<paddle::platform::CPUDeviceContext, float>,
    ops::AverageAccumulatesKernel<paddle::platform::CPUDeviceContext, double>);