bilinear_tensor_product_op.cc 6.6 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 39
    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.");
    PADDLE_ENFORCE_EQ(weight_dims.size(), 3UL,
P
peterzhang2029 已提交
40
                      "The input Weight must be a 3D tensor.");
P
peterzhang2029 已提交
41 42 43 44 45 46
    PADDLE_ENFORCE(weight_dims[0],
                   "The first dimension of Weight must be larger than 0.");
    PADDLE_ENFORCE(weight_dims[1],
                   "The second dimension of Weight must be larger than 0.");
    PADDLE_ENFORCE(weight_dims[2],
                   "The third dimension of Weight must be larger than 0.");
P
peterzhang2029 已提交
47 48
    PADDLE_ENFORCE_EQ(x_dims[0], y_dims[0],
                      "The first dimension(batch_size) of X must be "
P
peterzhang2029 已提交
49
                      "equal to the first dimension of the Y.");
P
peterzhang2029 已提交
50
    PADDLE_ENFORCE_EQ(x_dims[1], weight_dims[1],
P
peterzhang2029 已提交
51
                      "The second dimension of X must be equal to the second "
P
peterzhang2029 已提交
52
                      "dimension of the Weight.");
P
peterzhang2029 已提交
53
    PADDLE_ENFORCE_EQ(y_dims[1], weight_dims[2],
P
peterzhang2029 已提交
54
                      "The second dimension of Y must be equal to the third "
P
peterzhang2029 已提交
55 56
                      "dimension of the Weight.");

P
peterzhang2029 已提交
57
    if (ctx->HasInput("Bias")) {
P
peterzhang2029 已提交
58
      auto bias_dims = ctx->GetInputDim("Bias");
P
peterzhang2029 已提交
59
      PADDLE_ENFORCE_EQ(bias_dims.size(), 2UL,
P
peterzhang2029 已提交
60
                        "The input Bias must have 2 dimensions.");
P
peterzhang2029 已提交
61
      PADDLE_ENFORCE_EQ(bias_dims[0], 1UL,
P
peterzhang2029 已提交
62 63
                        "The first dimention of input Bias must be 1.");
      PADDLE_ENFORCE_EQ(bias_dims[1], weight_dims[0],
P
peterzhang2029 已提交
64
                        "The second dimension of Bias must be equal to the  "
P
peterzhang2029 已提交
65
                        "first dimension of the Weight.");
P
peterzhang2029 已提交
66 67
    }

P
peterzhang2029 已提交
68 69
    ctx->SetOutputDim("Out", {x_dims[0], weight_dims[0]});
    ctx->ShareLoD("X", /*->*/ "Out");
P
peterzhang2029 已提交
70 71 72 73 74 75 76 77
  }
};

class BilinearTensorProductOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  BilinearTensorProductOpMaker(framework::OpProto* proto,
                               framework::OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
P
peterzhang2029 已提交
78 79 80 81
    AddInput("X", "The first input of BilinearTensorProduct op.");
    AddInput("Y", "The second input of BilinearTensorProduct op.");
    AddInput("Weight", "The input weight of BilinearTensorProduct op.");
    AddInput("Bias", "The input bias of BilinearTensorProduct op.")
P
peterzhang2029 已提交
82
        .AsDispensable();
P
peterzhang2029 已提交
83
    AddOutput("Out", "The output of BilinearTensorProduct op.");
P
peterzhang2029 已提交
84 85
    AddComment(R"DOC(
Bilinear Tensor Product operator.
P
peterzhang2029 已提交
86 87
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 已提交
88

P
peterzhang2029 已提交
89 90
    M =  (X W_i) \cdot Y
    Out_i = \sum_i {M_i} + Bias_i
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 114
    PADDLE_ENFORCE_EQ(out_dims.size(), 2UL,
                      "The 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
        "The first dimension(batch_size) of Out@GRAD must be equal to "
        "the first dimension of the Input(X).");
P
peterzhang2029 已提交
119
    PADDLE_ENFORCE_EQ(weight_dims[0], out_dims[1],
P
peterzhang2029 已提交
120 121
                      "The second dimension of Out@GRAD must be equal to "
                      "the third dimension of the Input(Weight).");
P
peterzhang2029 已提交
122 123

    if (ctx->HasInput("Bias")) {
P
peterzhang2029 已提交
124
      auto bias_dims = ctx->GetInputDim("Bias");
P
peterzhang2029 已提交
125
      PADDLE_ENFORCE_EQ(bias_dims[1], out_dims[1],
P
peterzhang2029 已提交
126 127
                        "The second dimension of Out@GRAD must be equal to "
                        "the second dimension of the Input(Bias).");
P
peterzhang2029 已提交
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 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,
    ops::BilinearTensorProductKernel<paddle::platform::CPUPlace, float>);
REGISTER_OP_CPU_KERNEL(
    bilinear_tensor_product_grad,
161
    ops::BilinearTensorProductGradKernel<paddle::platform::CPUPlace, float>);