bilinear_tensor_product_op.cc 6.9 KB
Newer Older
P
peterzhang2029 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/* 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/bilinear_tensor_product_op.h"

namespace paddle {
namespace operators {

using framework::Tensor;

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

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Y"), "Input(Y) should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Weight"),
                   "Input(Weight) should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Out"), "Output(Out) should not be null.");
    auto x_dims = ctx->GetInputDim("X");
    auto y_dims = ctx->GetInputDim("Y");
    auto weight_dims = ctx->GetInputDim("Weight");

P
peterzhang2029 已提交
37 38
    PADDLE_ENFORCE_EQ(x_dims.size(), 2UL, "The input(X) must be a 2D Tensor.");
    PADDLE_ENFORCE_EQ(y_dims.size(), 2UL, "The input(Y) must be a 2D Tensor.");
P
peterzhang2029 已提交
39
    PADDLE_ENFORCE_EQ(weight_dims.size(), 3UL,
P
peterzhang2029 已提交
40
                      "The input(Weight) must be a 3D tensor.");
P
peterzhang2029 已提交
41
    PADDLE_ENFORCE_EQ(x_dims[0], y_dims[0],
P
peterzhang2029 已提交
42 43
                      "The first dimension(batch_size) of input(X) must be "
                      "equal to the first dimension of the input(Y).");
P
peterzhang2029 已提交
44
    PADDLE_ENFORCE_EQ(x_dims[1], weight_dims[1],
P
peterzhang2029 已提交
45 46
                      "The second dimension of input(X) must be equal to "
                      "the second dimension of the input(Weight).");
P
peterzhang2029 已提交
47
    PADDLE_ENFORCE_EQ(y_dims[1], weight_dims[2],
P
peterzhang2029 已提交
48 49
                      "The second dimension of input(Y) must be equal to "
                      "the third dimension of the input(Weight).");
P
peterzhang2029 已提交
50

P
peterzhang2029 已提交
51
    if (ctx->HasInput("Bias")) {
P
peterzhang2029 已提交
52
      auto bias_dims = ctx->GetInputDim("Bias");
P
peterzhang2029 已提交
53 54 55
      PADDLE_ENFORCE(bias_dims.size() == 2UL && bias_dims[0] == 1UL,
                     "The Input(Bias) must be a 2-D tensor with "
                     "the 2nd dimension fixed to 1 (a row vector).");
P
peterzhang2029 已提交
56
      PADDLE_ENFORCE_EQ(bias_dims[1], weight_dims[0],
P
peterzhang2029 已提交
57 58
                        "The second dimension of input(Bias) must be equal "
                        "to the first dimension of the input(Weight).");
P
peterzhang2029 已提交
59 60
    }

P
peterzhang2029 已提交
61 62
    ctx->SetOutputDim("Out", {x_dims[0], weight_dims[0]});
    ctx->ShareLoD("X", /*->*/ "Out");
P
peterzhang2029 已提交
63 64 65 66 67
  }
};

class BilinearTensorProductOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
68
  BilinearTensorProductOpMaker(OpProto* proto, OpAttrChecker* op_checker)
P
peterzhang2029 已提交
69
      : OpProtoAndCheckerMaker(proto, op_checker) {
P
peterzhang2029 已提交
70 71 72 73 74
    AddInput("X", "The first input of bilinear_tensor_product operator.");
    AddInput("Y", "The second input of bilinear_tensor_product operator.");
    AddInput("Weight",
             "The learnable parameters of bilinear_tensor_product operator.");
    AddInput("Bias", "The learnable bias of bilinear_tensor_product operator.")
P
peterzhang2029 已提交
75
        .AsDispensable();
P
peterzhang2029 已提交
76
    AddOutput("Out", "The output of bilinear_tensor_product operator.");
P
peterzhang2029 已提交
77 78
    AddComment(R"DOC(
Bilinear Tensor Product operator.
79
Given input X and Y, a 3D tensor Weight and a Bias. Each column of the
P
peterzhang2029 已提交
80
Output is computed by one slice $i = 1, . . . , k$ of the tensor:
81 82 83 84 85 86

$$
M =  (X W_i) * Y \\
Out_i = \sum_j {M_j} + Bias_i
$$

P
peterzhang2029 已提交
87 88 89 90 91
Where $W_i$ is the $i$-th slice of Input(Weight);
      $M_j$ is the $j$-th column of $M$;
      $Out_i$ is the $i$-th column of Output(Out);
      $Bias_i$ is a column vector, each element of it is equal to
        the $i$-th element of $Bias$;
P
peterzhang2029 已提交
92 93 94 95 96 97 98 99 100 101 102

)DOC");
  }
};

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

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {
P
peterzhang2029 已提交
103 104 105 106
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Y"), "Input(Y) should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Weight"),
                   "Input(Weight) should not be null.");
P
peterzhang2029 已提交
107
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
P
peterzhang2029 已提交
108
                   "Input(Out@GRAD) should not be null.");
P
peterzhang2029 已提交
109 110 111 112 113
    auto x_dims = ctx->GetInputDim("X");
    auto y_dims = ctx->GetInputDim("Y");
    auto weight_dims = ctx->GetInputDim("Weight");
    auto out_dims = ctx->GetInputDim(framework::GradVarName("Out"));

P
peterzhang2029 已提交
114
    PADDLE_ENFORCE_EQ(out_dims.size(), 2UL,
P
peterzhang2029 已提交
115
                      "The input(Out@GRAD) must be a 2D Tensor.");
P
peterzhang2029 已提交
116
    PADDLE_ENFORCE_EQ(
P
peterzhang2029 已提交
117
        x_dims[0], out_dims[0],
P
peterzhang2029 已提交
118 119 120 121 122 123
        "The first dimension(batch_size) of input(Out@GRAD) must be "
        "equal to the first dimension of the Input(X).");
    PADDLE_ENFORCE_EQ(
        weight_dims[0], out_dims[1],
        "The second dimension of input(Out@GRAD) must be equal to "
        "the third dimension of the Input(Weight).");
P
peterzhang2029 已提交
124 125

    if (ctx->HasInput("Bias")) {
P
peterzhang2029 已提交
126
      auto bias_dims = ctx->GetInputDim("Bias");
P
peterzhang2029 已提交
127 128 129 130
      PADDLE_ENFORCE_EQ(
          bias_dims[1], out_dims[1],
          "The second dimension of input(Out@GRAD) must be equal to "
          "the second dimension of the Input(Bias).");
P
peterzhang2029 已提交
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
      auto bias_grad_name = framework::GradVarName("Bias");
      if (ctx->HasOutput(bias_grad_name))
        ctx->SetOutputDim(bias_grad_name, bias_dims);
    }

    auto x_grad_name = framework::GradVarName("X");
    auto y_grad_name = framework::GradVarName("Y");
    auto weight_grad_name = framework::GradVarName("Weight");

    if (ctx->HasOutput(x_grad_name)) {
      ctx->SetOutputDim(x_grad_name, x_dims);
    }
    if (ctx->HasOutput(y_grad_name)) {
      ctx->SetOutputDim(y_grad_name, y_dims);
    }
    if (ctx->HasOutput(weight_grad_name)) {
      ctx->SetOutputDim(weight_grad_name, weight_dims);
    }
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP(bilinear_tensor_product, ops::BilinearTensorProductOp,
            ops::BilinearTensorProductOpMaker, bilinear_tensor_product_grad,
            ops::BilinearTensorProductOpGrad);
REGISTER_OP_CPU_KERNEL(
    bilinear_tensor_product,
Q
QI JUN 已提交
161 162 163
    ops::BilinearTensorProductKernel<paddle::platform::CPUDeviceContext, float>,
    ops::BilinearTensorProductKernel<paddle::platform::CPUDeviceContext,
                                     double>);
P
peterzhang2029 已提交
164 165
REGISTER_OP_CPU_KERNEL(
    bilinear_tensor_product_grad,
Q
QI JUN 已提交
166 167 168 169
    ops::BilinearTensorProductGradKernel<paddle::platform::CPUDeviceContext,
                                         float>,
    ops::BilinearTensorProductGradKernel<paddle::platform::CPUDeviceContext,
                                         double>);