bilinear_tensor_product_op.cc 6.5 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 68 69 70
  }
};

class BilinearTensorProductOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  BilinearTensorProductOpMaker(framework::OpProto* proto,
                               framework::OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
P
peterzhang2029 已提交
71 72 73 74 75
    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 已提交
76
        .AsDispensable();
P
peterzhang2029 已提交
77
    AddOutput("Out", "The output of bilinear_tensor_product operator.");
P
peterzhang2029 已提交
78 79
    AddComment(R"DOC(
Bilinear Tensor Product operator.
P
peterzhang2029 已提交
80 81
Given input X and Y, a 3D tensor weight, and bias. Each column of the
output is computed by one slice i = 1, . . . , k of the tensor:
P
peterzhang2029 已提交
82

P
peterzhang2029 已提交
83 84
    M =  (X W_i) \cdot Y
    Out_i = \sum_i {M_i} + Bias_i
P
peterzhang2029 已提交
85 86 87 88 89 90 91 92 93 94 95

)DOC");
  }
};

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

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

    if (ctx->HasInput("Bias")) {
P
peterzhang2029 已提交
119
      auto bias_dims = ctx->GetInputDim("Bias");
P
peterzhang2029 已提交
120 121 122 123
      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 已提交
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
      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,
P
peterzhang2029 已提交
154 155
    ops::BilinearTensorProductKernel<paddle::platform::CPUPlace, float>,
    ops::BilinearTensorProductKernel<paddle::platform::CPUPlace, double>);
P
peterzhang2029 已提交
156 157
REGISTER_OP_CPU_KERNEL(
    bilinear_tensor_product_grad,
P
peterzhang2029 已提交
158 159
    ops::BilinearTensorProductGradKernel<paddle::platform::CPUPlace, float>,
    ops::BilinearTensorProductGradKernel<paddle::platform::CPUPlace, double>);