mul_op.cc 4.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#include "paddle/operators/mul_op.h"
16 17 18 19

namespace paddle {
namespace operators {

D
dongzhihong 已提交
20 21
using framework::Tensor;

D
dongzhihong 已提交
22
class MulOp : public framework::OperatorWithKernel {
Y
Yu Yang 已提交
23 24
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;
25

26
 protected:
D
dongzhihong 已提交
27
  void InferShape(const framework::InferShapeContext &ctx) const override {
F
WIP  
fengjiayi 已提交
28 29
    auto x_dim = ctx.Input<Tensor>("X")->dims();
    auto y_dim = ctx.Input<Tensor>("Y")->dims();
30 31
    int x_num_row_dims = GetAttr<int>("x_num_row_dims");
    int y_num_row_dims = GetAttr<int>("y_num_row_dims");
F
WIP  
fengjiayi 已提交
32 33 34

    PADDLE_ENFORCE(x_dim.size() > x_num_row_dims,
                   "The rank of input tensor X(%s) should be larger than "
35
                   "`mul_op`'s `x_num_row_dims`.",
36
                   ctx.op().Input("X"));
F
WIP  
fengjiayi 已提交
37 38
    PADDLE_ENFORCE(y_dim.size() > y_num_row_dims,
                   "The rank of input tensor Y(%s) should be larger than "
39
                   "`mul_op`'s `y_num_row_dims`.",
40
                   ctx.op().Input("Y"));
Y
Yan Chunwei 已提交
41
    PADDLE_ENFORCE_EQ(
F
WIP  
fengjiayi 已提交
42 43
        product(x_dim, x_dim.size() - x_num_row_dims, x_dim.size()),
        product(y_dim, 0, y_dim.size() - y_num_row_dims),
44
        "First matrix's width must be equal with second matrix's height.");
F
WIP  
fengjiayi 已提交
45
    ctx.Output<Tensor>("Out")->Resize(
46 47 48
        {static_cast<int>(product(x_dim, 0, x_dim.size() - x_num_row_dims)),
         static_cast<int>(
             product(y_dim, y_dim.size() - y_num_row_dims, y_dim.size()))});
49 50 51
  }
};

D
dongzhihong 已提交
52
class MulOpMaker : public framework::OpProtoAndCheckerMaker {
53
 public:
D
dongzhihong 已提交
54
  MulOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
55
      : OpProtoAndCheckerMaker(proto, op_checker) {
56 57 58
    AddInput("X", "The first input of mul op");
    AddInput("Y", "The second input of mul op");
    AddOutput("Out", "The output of mul op");
F
WIP  
fengjiayi 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    AddAttr<int>(
        "x_num_row_dims",
        "mul_op can take tensors with more than two dimensions as input `X`, "
        "in that case, tensors will be flattened to a matrix. The matrix's "
        "second dimension(row length) will be the product of tensor's last "
        "`num_row_dims` dimensions, and the matrix's first dimension(column "
        "length) will be the product of tensor's first `rank - num_row_dims` "
        "dimensions.")
        .SetDefault(1)
        .EqualLargerThan(1);
    AddAttr<int>(
        "y_num_row_dims",
        "mul_op can take tensors with more than two dimensions as input `Y`, "
        "in that case, tensors will be flattened to a matrix. Just like input "
        "`X`.")
        .SetDefault(1)
        .EqualLargerThan(1);
76 77 78 79 80 81 82 83
    AddComment(R"DOC(
Two Element Mul Operator.

The equation is: Out = X * Y
)DOC");
  }
};

D
dongzhihong 已提交
84
class MulOpGrad : public framework::OperatorWithKernel {
Y
Yu Yang 已提交
85 86 87
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

88
 protected:
D
dongzhihong 已提交
89 90 91 92 93
  void InferShape(const framework::InferShapeContext &ctx) const override {
    PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("X"), "Input(X) should not be null");
    PADDLE_ENFORCE_NOT_NULL(ctx.InputVar("Y"), "Input(Y) should not be null");
    PADDLE_ENFORCE_NOT_NULL(ctx.InputVar(framework::GradVarName("Out")),
                            "Input(Out@GRAD) should not be null");
D
dongzhihong 已提交
94 95 96
    auto x_dims = ctx.Input<Tensor>("X")->dims();
    auto y_dims = ctx.Input<Tensor>("Y")->dims();
    auto out_dims = ctx.Input<Tensor>(framework::GradVarName("Out"))->dims();
D
dongzhihong 已提交
97 98
    auto *x_grad = ctx.Output<Tensor>(framework::GradVarName("X"));
    auto *y_grad = ctx.Output<Tensor>(framework::GradVarName("Y"));
F
WIP  
fengjiayi 已提交
99
    PADDLE_ENFORCE(
100 101
        product(x_dims, 0, x_dims.size() - GetAttr<int>("x_num_row_dims")) ==
            out_dims[0],
F
WIP  
fengjiayi 已提交
102 103
        "The first dimension of Out@GRAD must equal to the first dimension of "
        "the first operand.");
104 105 106 107 108
    PADDLE_ENFORCE(
        product(y_dims, y_dims.size() - GetAttr<int>("y_num_row_dims"),
                y_dims.size()) == out_dims[1],
        "The second dimension of Out@GRAD must equal to the second "
        "dimension of the second operand.");
D
dongzhihong 已提交
109

110 111
    if (x_grad) x_grad->Resize(x_dims);
    if (y_grad) y_grad->Resize(y_dims);
D
dongzhihong 已提交
112 113 114
  }
};

115 116 117
}  // namespace operators
}  // namespace paddle

D
dongzhihong 已提交
118
namespace ops = paddle::operators;
119
REGISTER_OP(mul, ops::MulOp, ops::MulOpMaker, mul_grad, ops::MulOpGrad);
D
dongzhihong 已提交
120
REGISTER_OP_CPU_KERNEL(mul, ops::MulKernel<paddle::platform::CPUPlace, float>);
D
dongzhihong 已提交
121 122
REGISTER_OP_CPU_KERNEL(mul_grad,
                       ops::MulGradKernel<paddle::platform::CPUPlace, float>);