split_selected_rows_op.cc 3.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yancey 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/split_selected_rows_op.h"
Y
Yancey 已提交
16

M
minqiyang 已提交
17 18
#include <memory>

Y
Yancey 已提交
19 20 21 22 23
namespace paddle {
namespace operators {

class SplitSelectedRowsOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
24
  void Make() override {
Y
Yancey 已提交
25
    AddInput("X", "The input SelectedRows.");
26
    AddOutput("Out", "The outputs of the input SelectedRows.").AsDuplicable();
T
tangwei12 已提交
27 28 29
    AddAttr<std::vector<int64_t>>("height_sections",
                                  "Height for each output SelectedRows.")
        .SetDefault(std::vector<int64_t>({}));
Y
Yancey 已提交
30 31 32 33 34 35 36

    AddComment(R"DOC(
Split a SelectedRows with a specified rows section.
height_sections is only needed when need to split the dims of the original tensor.

Example:
  Input:
37
    X.rows = {7, 5}
Y
Yancey 已提交
38 39
    X.height = 12
  Attr:
40
    height_sections = {4, 8}
Y
Yancey 已提交
41
  Out:
42 43 44 45 46
    out0.rows = {}
    out0.height = 4

    out1.rows = {5, 7}
    out2.height = 8
Y
Yancey 已提交
47 48 49 50 51 52 53 54 55 56

)DOC");
  }
};

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

  void InferShape(framework::InferShapeContext *ctx) const override {
Z
zhangchunle 已提交
57 58
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "SplitSelectedRowsOp must have input X.");
Y
Yancey 已提交
59
    PADDLE_ENFORCE(ctx->HasOutputs("Out"),
Z
zhangchunle 已提交
60
                   "SplitSelectedRowsOp must have output Out.");
Y
Yancey 已提交
61 62 63
  }
};

Y
Yancey1989 已提交
64 65
class SplitSelectedRowsOpInferVarType : public framework::VarTypeInference {
 public:
M
minqiyang 已提交
66 67 68
  void operator()(framework::InferVarTypeContext *ctx) const override {
    for (auto &out_var : ctx->Output("Out")) {
      ctx->SetType(out_var, framework::proto::VarType::SELECTED_ROWS);
Y
Yancey1989 已提交
69 70 71 72
    }
  }
};

H
hong 已提交
73 74
template <typename T>
class SplitSelectedRowsGradMaker : public framework::SingleGradOpMaker<T> {
Y
Yancey 已提交
75
 public:
H
hong 已提交
76
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
Y
Yancey 已提交
77 78

 protected:
79
  void Apply(GradOpPtr<T> grad_op) const override {
Y
Yancey 已提交
80
    grad_op->SetType("sum");
H
hong 已提交
81 82 83
    grad_op->SetInput("X", this->OutputGrad("Out"));
    grad_op->SetOutput("Out", this->InputGrad("X"));
    grad_op->SetAttrMap(this->Attrs());
Y
Yancey 已提交
84 85 86 87 88 89 90 91 92
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(split_selected_rows, ops::SplitSelectedRowsOp,
                  ops::SplitSelectedRowsOpMaker,
H
hong 已提交
93 94
                  ops::SplitSelectedRowsGradMaker<paddle::framework::OpDesc>,
                  ops::SplitSelectedRowsGradMaker<paddle::imperative::OpBase>,
Y
Yancey1989 已提交
95
                  ops::SplitSelectedRowsOpInferVarType);
Y
Yancey 已提交
96 97 98
REGISTER_OP_CPU_KERNEL(
    split_selected_rows,
    ops::SplitSelectedRowsOpKernel<paddle::platform::CPUPlace, float>);