multiplex_op.cc 3.9 KB
Newer Older
Y
Yibing Liu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

   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/operators/multiplex_op.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;

class MultiplexOp : public framework::OperatorWithKernel {
 public:
24
  using framework::OperatorWithKernel::OperatorWithKernel;
Y
Yibing Liu 已提交
25 26 27

 protected:
  void InferShape(const framework::InferShapeContext &ctx) const override {
28
    PADDLE_ENFORCE(!ctx.MultiInputVar("X").empty(),
Y
Yibing Liu 已提交
29
                   "Input(X) should not be null.");
30 31
    PADDLE_ENFORCE_NOT_NULL(ctx.OutputVar("Out"),
                            "Output(Out) shouldn't be null.");
Y
Yibing Liu 已提交
32
    auto ins = ctx.MultiInput<Tensor>("X");
Y
Yibing Liu 已提交
33
    auto *out = ctx.Output<Tensor>("Out");
Y
Yibing Liu 已提交
34 35 36 37 38 39 40 41 42
    auto num_ins = ins.size();
    PADDLE_ENFORCE(num_ins > 2,
                   "multiplex operator should have more than 2 inputs.");
    PADDLE_ENFORCE_EQ(ins[0]->dims().size(), 1,
                      "The first input must be a index vector.");
    auto in_dim = ins[1]->dims();

    for (size_t i = 2; i < num_ins; i++) {
      auto dim = ins[i]->dims();
Y
Yibing Liu 已提交
43 44 45
      PADDLE_ENFORCE(in_dim == dim,
                     "All the input tensors except the first one must have the "
                     "same size.");
Y
Yibing Liu 已提交
46 47 48 49 50 51 52 53 54 55
    }
    out->Resize(in_dim);
  }
};

class MultiplexOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  MultiplexOpMaker(framework::OpProto *proto,
                   framework::OpAttrChecker *op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
56
    AddInput("X", "The input tensors of multiplex operator.").AsDuplicable();
Y
Yibing Liu 已提交
57 58 59 60 61 62
    AddOutput("Out", "The output tensor of multiplex operator.");
    AddComment(R"DOC(Multiplex operator

Multiplex multiple tensors according to the index provided by the first
input tensor.

63 64
ins[0]: the index tensor.
ins[1:N]: the candidate output tensors.
Y
Yibing Liu 已提交
65 66 67
For each index i from 0 to batchSize - 1, the output is the i-th row of the
the (index[i] + 1)-th tensor.

68
For i-th row of the output tensor:
Y
Yibing Liu 已提交
69 70 71

y[i][j] = x_{k}[i][j], j = 0,1, ... , (x_{1}.width - 1)

72
where y is the output tensor. `x_{k}` is the k-th input tensor
Y
Yibing Liu 已提交
73 74 75 76 77 78 79 80
and `k = x{0}[i] + 1`.

)DOC");
  }
};

class MultiplexGradOp : public framework::OperatorWithKernel {
 public:
81
  using framework::OperatorWithKernel::OperatorWithKernel;
Y
Yibing Liu 已提交
82 83 84

 protected:
  void InferShape(const framework::InferShapeContext &ctx) const override {
85
    PADDLE_ENFORCE(!ctx.MultiInputVar("X").empty(),
Y
Yibing Liu 已提交
86
                   "Input(X) should not be null.");
87
    PADDLE_ENFORCE(!ctx.MultiOutputVar(framework::GradVarName("X")).empty(),
Y
Yibing Liu 已提交
88
                   "Output(X@Grad) should not be null.");
Y
Yibing Liu 已提交
89 90
    PADDLE_ENFORCE_NOT_NULL(ctx.InputVar(framework::GradVarName("Out")),
                            "Input(Out@GRAD) shouldn't be null.");
Y
Yibing Liu 已提交
91
    auto d_ins = ctx.MultiOutput<Tensor>(framework::GradVarName("X"));
Y
Yibing Liu 已提交
92
    auto ins = ctx.MultiInput<Tensor>("X");
93
    // don't compute gradient for index (ins[0])
94 95 96 97
    for (size_t i = 1; i < ins.size(); i++) {
      if (d_ins[i]) {
        d_ins[i]->Resize(ins[i]->dims());
      }
Y
Yibing Liu 已提交
98 99 100 101 102 103 104 105 106 107
    }
  }
};

}  // namespace operators
}  // namespace paddle
namespace ops = paddle::operators;

REGISTER_OP(multiplex, ops::MultiplexOp, ops::MultiplexOpMaker, multiplex_grad,
            ops::MultiplexGradOp);
Y
Yibing Liu 已提交
108 109
REGISTER_OP_CPU_KERNEL(
    multiplex, ops::MultiplexCPUKernel<paddle::platform::CPUPlace, float>);
110 111
REGISTER_OP_CPU_KERNEL(
    multiplex_grad,
Y
Yibing Liu 已提交
112
    ops::MultiplexGradCPUKernel<paddle::platform::CPUPlace, float>);