mul_op.cc 5.3 KB
Newer Older
1 2
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

3 4 5
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
6

7
    http://www.apache.org/licenses/LICENSE-2.0
8

9 10 11 12 13
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. */
14

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

namespace paddle {
namespace operators {

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

22
class MulOpShapeInference : public framework::InferShapeBase {
Y
Yu Yang 已提交
23
 public:
24
  void operator()(framework::InferShapeContext* ctx) const override {
Q
Qiao Longfei 已提交
25 26 27 28 29 30 31 32 33
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) of MulOp should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Y"), "Input(Y) of MulOp should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   "Output(Out) of MulOp should not be null.");

    auto x_dims = ctx->GetInputDim("X");
    auto y_dims = ctx->GetInputDim("Y");
    int x_num_col_dims = ctx->Attrs().Get<int>("x_num_col_dims");
    int y_num_col_dims = ctx->Attrs().Get<int>("y_num_col_dims");
F
WIP  
fengjiayi 已提交
34

35 36 37 38 39 40 41 42
    PADDLE_ENFORCE_GT(
        x_dims.size(), x_num_col_dims,
        "The input tensor X's rank of MulOp should be larger than "
        "x_num_col_dims.");
    PADDLE_ENFORCE_GT(
        y_dims.size(), y_num_col_dims,
        "The input tensor Y's rank of MulOp should be larger than "
        "y_num_col_dims.");
43

F
fengjiayi 已提交
44 45
    auto x_mat_dims = framework::flatten_to_2d(x_dims, x_num_col_dims);
    auto y_mat_dims = framework::flatten_to_2d(y_dims, y_num_col_dims);
46

Y
Yan Chunwei 已提交
47
    PADDLE_ENFORCE_EQ(
48
        x_mat_dims[1], y_mat_dims[0],
49
        "First matrix's width must be equal with second matrix's height.");
Y
Yu Yang 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62
    std::vector<int64_t> output_dims;
    output_dims.reserve(
        static_cast<size_t>(x_num_col_dims + y_dims.size() - y_num_col_dims));

    for (int i = 0; i < x_num_col_dims; ++i) {
      output_dims.push_back(x_dims[i]);
    }

    for (int i = y_num_col_dims; i < y_dims.size(); ++i) {
      output_dims.push_back(y_dims[i]);
    }

    ctx->SetOutputDim("Out", framework::make_ddim(output_dims));
Q
Qiao Longfei 已提交
63
    ctx->ShareLoD("X", /*->*/ "Out");
64 65 66
  }
};

D
dongzhihong 已提交
67
class MulOpMaker : public framework::OpProtoAndCheckerMaker {
68
 public:
Q
Qiao Longfei 已提交
69
  MulOpMaker(framework::OpProto* proto, framework::OpAttrChecker* op_checker)
70
      : OpProtoAndCheckerMaker(proto, op_checker) {
71 72 73
    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 已提交
74
    AddAttr<int>(
F
fengjiayi 已提交
75
        "x_num_col_dims",
Q
Qiao Longfei 已提交
76 77 78
        R"DOC(mul_op can take tensors with more than two dimensions as input `X`,
            in that case, tensors will be reshaped to a matrix. The matrix's first
            dimension(column length) will be the product of tensor's last
F
fengjiayi 已提交
79 80 81
            `num_col_dims` dimensions, and the matrix's second dimension(row length)
            will be the product of tensor's first `rank - num_col_dims` dimensions.
        )DOC")
F
WIP  
fengjiayi 已提交
82
        .SetDefault(1)
F
fengjiayi 已提交
83
        .EqualGreaterThan(1);
F
WIP  
fengjiayi 已提交
84
    AddAttr<int>(
F
fengjiayi 已提交
85
        "y_num_col_dims",
F
fengjiayi 已提交
86 87 88
        R"DOC(mul_op can take tensors with more than two dimensions as input `Y`,
             in that case, tensors will be reshaped to a matrix. Just like input `X`.
        )DOC")
F
WIP  
fengjiayi 已提交
89
        .SetDefault(1)
F
fengjiayi 已提交
90
        .EqualGreaterThan(1);
91
    AddComment(R"DOC(
92
Mul operator is used to perform matrix multiplication for input X and Y.
93

94 95 96 97 98 99
The equation is:

    Out = X * Y

Both the input `X` and `Y` can carry the LoD (Level of Details) information,
or not. But the output only shares the LoD with input `X`.
100 101 102 103
)DOC");
  }
};

D
dongzhihong 已提交
104
class MulOpGrad : public framework::OperatorWithKernel {
Y
Yu Yang 已提交
105 106 107
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

108
  void InferShape(framework::InferShapeContext* ctx) const override {
Q
Qiao Longfei 已提交
109 110 111 112 113 114 115
    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(framework::GradVarName("Out")),
                   "Input(Out@GRAD) should not be null");
    auto x_dims = ctx->GetInputDim("X");
    auto y_dims = ctx->GetInputDim("Y");
    auto out_dims = ctx->GetInputDim(framework::GradVarName("Out"));
116

Q
Qiao Longfei 已提交
117 118 119 120
    auto x_mat_dims = framework::flatten_to_2d(
        x_dims, ctx->Attrs().Get<int>("x_num_col_dims"));
    auto y_mat_dims = framework::flatten_to_2d(
        y_dims, ctx->Attrs().Get<int>("y_num_col_dims"));
121

Q
Qiao Longfei 已提交
122 123 124 125 126 127 128 129 130
    auto x_grad_name = framework::GradVarName("X");
    auto y_grad_name = framework::GradVarName("Y");

    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);
    }
D
dongzhihong 已提交
131 132 133
  }
};

134 135 136
}  // namespace operators
}  // namespace paddle

D
dongzhihong 已提交
137
namespace ops = paddle::operators;
138 139 140 141
REGISTER_OPERATOR(mul, paddle::framework::OperatorWithKernel, ops::MulOpMaker,
                  ops::MulOpShapeInference,
                  paddle::framework::DefaultGradOpDescMaker<true>);
REGISTER_OPERATOR(mul_grad, ops::MulOpGrad);
D
dongzhihong 已提交
142
REGISTER_OP_CPU_KERNEL(mul, ops::MulKernel<paddle::platform::CPUPlace, float>);
D
dongzhihong 已提交
143 144
REGISTER_OP_CPU_KERNEL(mul_grad,
                       ops::MulGradKernel<paddle::platform::CPUPlace, float>);