bilinear_tensor_product_op.cc 6.9 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
P
peterzhang2029 已提交
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/bilinear_tensor_product_op.h"
P
peterzhang2029 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

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:
Y
Yu Yang 已提交
68
  void Make() override {
P
peterzhang2029 已提交
69 70 71 72 73
    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 已提交
74
        .AsDispensable();
P
peterzhang2029 已提交
75
    AddOutput("Out", "The output of bilinear_tensor_product operator.");
P
peterzhang2029 已提交
76 77
    AddComment(R"DOC(
Bilinear Tensor Product operator.
78
Given input X and Y, a 3D tensor Weight and a Bias. Each column of the
P
peterzhang2029 已提交
79
Output is computed by one slice $i = 1, . . . , k$ of the tensor:
80 81 82 83 84 85

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

P
peterzhang2029 已提交
86 87 88 89 90
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 已提交
91 92 93 94 95 96 97 98 99 100 101

)DOC");
  }
};

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

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {
P
peterzhang2029 已提交
102 103 104 105
    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 已提交
106
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
P
peterzhang2029 已提交
107
                   "Input(Out@GRAD) should not be null.");
P
peterzhang2029 已提交
108 109 110 111 112
    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 已提交
113
    PADDLE_ENFORCE_EQ(out_dims.size(), 2UL,
P
peterzhang2029 已提交
114
                      "The input(Out@GRAD) must be a 2D Tensor.");
P
peterzhang2029 已提交
115
    PADDLE_ENFORCE_EQ(
P
peterzhang2029 已提交
116
        x_dims[0], out_dims[0],
P
peterzhang2029 已提交
117 118 119 120 121 122
        "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 已提交
123 124

    if (ctx->HasInput("Bias")) {
P
peterzhang2029 已提交
125
      auto bias_dims = ctx->GetInputDim("Bias");
P
peterzhang2029 已提交
126 127 128 129
      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 已提交
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
      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;
Y
Yang Yang 已提交
155 156
REGISTER_OPERATOR(bilinear_tensor_product, ops::BilinearTensorProductOp,
                  ops::BilinearTensorProductOpMaker,
157
                  paddle::framework::DefaultGradOpDescMaker<true>);
Y
Yang Yang 已提交
158
REGISTER_OPERATOR(bilinear_tensor_product_grad,
159
                  ops::BilinearTensorProductOpGrad);
P
peterzhang2029 已提交
160 161
REGISTER_OP_CPU_KERNEL(
    bilinear_tensor_product,
Q
QI JUN 已提交
162 163 164
    ops::BilinearTensorProductKernel<paddle::platform::CPUDeviceContext, float>,
    ops::BilinearTensorProductKernel<paddle::platform::CPUDeviceContext,
                                     double>);
P
peterzhang2029 已提交
165 166
REGISTER_OP_CPU_KERNEL(
    bilinear_tensor_product_grad,
Q
QI JUN 已提交
167 168 169 170
    ops::BilinearTensorProductGradKernel<paddle::platform::CPUDeviceContext,
                                         float>,
    ops::BilinearTensorProductGradKernel<paddle::platform::CPUDeviceContext,
                                         double>);