fusion_group_op.cc 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* 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 "paddle/fluid/operators/fused/fusion_group_op.h"

namespace paddle {
namespace operators {

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

24
  void InferShape(framework::InferShapeContext* ctx) const override {
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    const size_t num_ins = ctx->Inputs("Inputs").size();
    const size_t num_outs = ctx->Outputs("Outs").size();

    PADDLE_ENFORCE_GE(
        num_ins, 1UL,
        platform::errors::InvalidArgument(
            "Expected the number of inputs >= 1. Received %d.", num_ins));
    PADDLE_ENFORCE_GE(
        num_outs, 1UL,
        platform::errors::InvalidArgument(
            "Expected the number of outputs >= 1. Recived %d.", num_outs));

    int type = ctx->Attrs().Get<int>("type");
    PADDLE_ENFORCE_EQ(type, 0UL,
                      platform::errors::InvalidArgument(
                          "Only support fusion of elementwise operations."));

    std::vector<framework::DDim> x_dims = ctx->GetInputsDim("Inputs");
    if (type == 0) {
      for (size_t i = 1; i < num_ins; ++i) {
        PADDLE_ENFORCE_EQ(x_dims[0], x_dims[i],
                          platform::errors::InvalidArgument(
                              "All the inputs' dims should be the same."));
      }
      std::vector<framework::DDim> out_dims;
      for (size_t j = 0; j < num_outs; ++j) {
        out_dims.push_back(x_dims[0]);
      }
      ctx->SetOutputsDim("Outs", out_dims);
    }

    // Only lod of Inputs[0] would be shared with Outs.
    for (size_t j = 0; j < num_outs; ++j) {
      ctx->ShareLoD("Inputs", /*->*/ "Outs", 0, j);
    }
  }
61 62 63 64 65 66 67

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(framework::proto::VarType::FP32,
                                   platform::CUDAPlace(0));
  };
68 69 70 71 72 73 74 75 76 77 78
};

class FusionGroupOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("Inputs",
             "(std::vector<LoDTensor>) The inputs of fusion_group op.")
        .AsDuplicable();
    AddOutput("Outs",
              "(std::vector<LoDTensor>) The outputs of fusion_group op.")
        .AsDuplicable();
79 80 81 82 83 84
    AddAttr<std::vector<std::string>>(
        "outs_data_type", "The data type of Outputs in fusion_group op.")
        .SetDefault({});
    AddAttr<std::vector<std::string>>(
        "inputs_data_type", "The data type of Inputs in fusion_group op.")
        .SetDefault({});
85 86 87 88 89 90 91
    AddAttr<int>("type", "Fusion type.").SetDefault(0);
    AddAttr<std::string>("func_name", "Name of the generated functions.")
        .SetDefault("");
    AddComment(R"DOC(
fusion_group Operator.

It is used to execute a generated CUDA kernel which fuse the computation of
T
tianshuo78520a 已提交
92
multiple operators into one. It supports several types:
93 94 95 96 97 98 99 100 101 102 103
0, fused computation of elementwise operations in which all the dims of inputs
    and outputs should be exactly the same.
)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(fusion_group, ops::FusionGroupOp, ops::FusionGroupOpMaker);