multihead_matmul_op.cc 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.

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 <vector>
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/detail/safe_ref.h"
18
#include "paddle/fluid/platform/errors.h"
19 20 21 22

namespace paddle {
namespace operators {

23
class MultiHeadMatMulV2Op : public framework::OperatorWithKernel {
24 25 26 27 28
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext *context) const override {
29
    PADDLE_ENFORCE_EQ(
30
        context->HasInput("Input"), true,
31
        platform::errors::InvalidArgument(
32 33 34 35
            "Input(Input) of MultiHeadMatMul should not be null."));
    PADDLE_ENFORCE_EQ(context->HasInput("W"), true,
                      platform::errors::InvalidArgument(
                          "Input(W) of MultiHeadMatMul should not be null."));
36
    PADDLE_ENFORCE_EQ(
37
        context->HasInput("Bias"), true,
38
        platform::errors::InvalidArgument(
39
            "Input(Bias) of MultiHeadMatMul should not be null."));
40
    PADDLE_ENFORCE_EQ(
41
        context->HasInput("BiasQK"), true,
42
        platform::errors::InvalidArgument(
43 44 45 46 47
            "Input(BiasQK) of MultiHeadMatMul should not be null."));
    PADDLE_ENFORCE_EQ(
        context->HasOutput("Out"), true,
        platform::errors::InvalidArgument(
            "Output(Out) of MultiHeadMatMul should not be null."));
Z
zhaoyuchen2018 已提交
48

49 50 51 52 53 54 55
    auto dim_w = context->GetInputDim("W");
    PADDLE_ENFORCE_GT(
        dim_w.size(), 2,
        platform::errors::InvalidArgument(
            "Multihead input is expected at least a 3-D tensor, but "
            "it's %d-D tensor now.",
            dim_w.size()));
Z
zhaoyuchen2018 已提交
56

57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    auto dim_bias_q = context->GetInputDim("Bias");
    PADDLE_ENFORCE_GT(
        dim_bias_q.size(), 1,
        platform::errors::InvalidArgument(
            "Multihead input should be at least 2-D tensor, but it's "
            "%d-D tensor now.",
            dim_bias_q.size()));

    auto dim_bias_qk = context->GetInputDim("BiasQK");
    PADDLE_ENFORCE_GT(
        dim_bias_qk.size(), 3,
        platform::errors::InvalidArgument(
            "Multihead input bias qk should be at least 4-D tensor, "
            "but it's %d-D tensor now.",
            dim_bias_qk.size()));
72

73 74 75 76 77 78 79 80 81 82
    int head_number = context->Attrs().Get<int>("head_number");
    PADDLE_ENFORCE_GT(
        head_number, 1,
        platform::errors::InvalidArgument(
            "Multihead input head number should be at least 1, but it %d now.",
            head_number));
    // modify this
    auto dim_input = context->GetInputDim("Input");
    context->SetOutputDim("Out", dim_input);
    context->ShareLoD("Input", /*->*/ "Out");
83 84 85
  }
};

86
class MultiHeadMatMulV2OpMaker : public framework::OpProtoAndCheckerMaker {
87 88
 public:
  void Make() override {
89 90 91
    AddInput("Input", "The input of MultiHeadMatMul op");
    AddInput("W", "The weight input of MultiHeadMatMul op");
    AddInput("Bias", "The bias input of MultiHeadMatMul op");
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    AddInput("BiasQK", "The QK bias input of MultiHeadMatMul op");
    AddOutput("Out", "The output of MultiHeadMatMul op");
    AddAttr<bool>("transpose_Q",
                  R"DOC(If true, use the transpose of `Q`.
        )DOC")
        .SetDefault(false);
    AddAttr<bool>("transpose_K",
                  R"DOC(If true, use the transpose of `K`.
        )DOC")
        .SetDefault(true);
    AddAttr<bool>("transpose_V",
                  R"DOC(If true, use the transpose of `V`.
        )DOC")
        .SetDefault(false);
    AddAttr<float>("alpha", "The scale of Out").SetDefault(1.0f);
    AddAttr<int>("head_number", "The number of heads of the matrix")
        .SetDefault(1);
    AddComment(R"DOC(
MultiHeadMatMul Operator.

This op is used for optimize multi head calculation in ernie model.
Not suggest to use in other case except has same structure as ernie.

Z
zhaoyuchen2018 已提交
115
Example of matrix multiplication with head_number of B
116 117 118 119 120 121 122 123 124 125
- X: [B, M, K], Y: [B, K, N] => Out: [B, M, N]

)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
126 127
REGISTER_OP_WITHOUT_GRADIENT(multihead_matmul, ops::MultiHeadMatMulV2Op,
                             ops::MultiHeadMatMulV2OpMaker);