phi_op_cvt_pass.cc 7.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// Copyright (c) 2021 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/infrt/dialect/phi/pass/phi_op_cvt_pass.h"

#include <glog/logging.h>
#include <llvm/ADT/SetVector.h>
#include <mlir/Analysis/SliceAnalysis.h>
#include <mlir/IR/Builders.h>
21 22
#include <mlir/IR/Operation.h>
#include <mlir/IR/OperationSupport.h>
23 24 25 26
#include <list>
#include <unordered_set>
#include <vector>

27
#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h"
28
#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h"
29 30 31 32
#include "paddle/infrt/dialect/phi/pass/kernel_op_desc.h"
#include "paddle/infrt/dialect/phi/pass/proto_arg_map_context.h"
#include "paddle/phi/core/compat/op_utils.h"
#include "paddle/phi/ops/compat/signatures.h"
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

namespace {
class phiOpCvtPass
    : public mlir::PassWrapper<phiOpCvtPass, mlir::FunctionPass> {
 public:
  ::llvm::StringRef getName() const override { return "phiOpCvtPass"; }
  void runOnFunction() override;
  explicit phiOpCvtPass(
      std::vector<infrt::Place> valid_places = std::vector<infrt::Place>())
      : valid_places_(valid_places) {}

 private:
  void convertStage();
  void diapatchStage();
  std::vector<infrt::Place> valid_places_;
};

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
// Implementation of the phiOpCvtPass.
void phiOpCvtPass::runOnFunction() {
  convertStage();
  diapatchStage();
}
void phiOpCvtPass::convertStage() {
  mlir::Block &body = getFunction().front();
  std::vector<mlir::Operation *> worklist;
  for (auto &op : body.without_terminator()) {
    worklist.push_back(&op);
  }
  mlir::OpBuilder builder(&body, body.begin());
  while (!worklist.empty()) {
    auto *op = worklist.back();
    worklist.pop_back();
    if (op == nullptr) continue;

    std::string op_name = op->getName().getIdentifier().str();

    // only convert op in pd dialect.
    if (op_name.substr(0, 3) != "pd.") continue;
    op_name = op_name.substr(3);
    if (pd_dialect_inputs_info_map_.find(op_name) ==
            pd_dialect_inputs_info_map_.end() ||
        pd_dialect_outputs_info_map_.find(op_name) ==
            pd_dialect_outputs_info_map_.end()) {
      // Todo: print log
      continue;
    }

80 81
    ::phi::KernelSignature kernel_sign =
        ::phi::OpUtilsMap::Instance().GetArgumentMappingFn(op_name)(
82
            infrt::ProtoArgumentMappingContext(op));
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    // resort input&output according to kernel_sign
    ::llvm::SmallVector<mlir::Value, 4> inputs, ori_output;
    ::llvm::SmallVector<mlir::Type, 4> output_types;
    for (const std::string &str : std::get<0>(kernel_sign.args)) {
      if (pd_dialect_inputs_info_map_.at(op_name).count(str) == 0) {
        // Todo: print error log
        return;
      }
      uint8_t index = pd_dialect_inputs_info_map_.at(op_name).at(str);
      inputs.push_back(op->getOperands()[index]);
    }

    for (const std::string &str : std::get<2>(kernel_sign.args)) {
      if (pd_dialect_outputs_info_map_.at(op_name).count(str) == 0) {
        // Todo: print error log
        return;
      }
      uint8_t index = pd_dialect_outputs_info_map_.at(op_name).at(str);
      output_types.push_back(op->getResultTypes()[index]);
      ori_output.push_back(op->getResult(index));
    }

    auto loc = getFunction().getLoc();
    builder.setInsertionPoint(op);
    auto kernel_op = builder.create<infrt::KernelOp>(
        loc, output_types, inputs, kernel_sign.name, op->getAttrDictionary());
    for (size_t index = 0; index < ori_output.size(); ++index) {
      ori_output[index].replaceAllUsesWith(kernel_op.getResult(index));
    }
    if (!op->use_empty()) {
      // Todo: print error log
      return;
    }
    op->erase();
  }
}
void phiOpCvtPass::diapatchStage() {
  std::vector<infrt::KernelOp> worklist;
  mlir::Block &block = getFunction().front();
  for (auto &op : block) {
    infrt::KernelOp kernel_op = ::llvm::dyn_cast_or_null<infrt::KernelOp>(&op);
    if (nullptr != kernel_op) worklist.push_back(kernel_op);
  }
126 127

  mlir::OpBuilder builder(&block, block.begin());
128
  std::map<infrt::TargetType, mlir::Value> phi_context;
129 130
  for (infrt::KernelOp kernel_op : worklist) {
    std::string kernel_name = kernel_op.name().str();
131
    std::vector<infrt::PhiKernelDesc> candidates =
132 133 134 135 136 137 138 139
        getCandidateKernels(kernel_name, valid_places_);
    if (candidates.empty()) {
      LOG(FATAL) << "No candidate kernels for op:" << kernel_name;
      continue;
    }
    builder.setInsertionPoint(kernel_op);

    // Todo: Implimentation the concrete pass pick strategy
140
    const infrt::PhiKernelDesc &phi_kernel_desc = candidates.front();
141

142 143 144 145 146
    kernel_name =
        infrt::getPhiTargetPrefix(phi_kernel_desc.kernelType.target) +
        kernel_name +
        infrt::getPhiPrecisionSuffix(phi_kernel_desc.kernelType.precision) +
        infrt::getPhiLayoutSuffix(phi_kernel_desc.kernelType.layout);
147 148 149 150 151 152 153

    mlir::OperationName operation_name(kernel_name, kernel_op.getContext());
    mlir::OperationState operation_state(kernel_op.getLoc(), operation_name);

    if (phi_context.find(phi_kernel_desc.kernelType.target) ==
        phi_context.end()) {
      switch (phi_kernel_desc.kernelType.target) {
154
        case infrt::TargetType::CPU: {
155 156
          auto context_value =
              builder
157
                  .create<infrt::phi::CreateCPUContextOp>(
158
                      kernel_op.getLoc(),
159 160
                      infrt::phi::ContextType::get(kernel_op.getContext(),
                                                   infrt::TargetType::CPU))
161
                  .output();
162
          phi_context[infrt::TargetType::CPU] = context_value;
163
        } break;
164 165
        case infrt::TargetType::GPU:
        case infrt::TargetType::UNK:
166 167 168 169 170 171 172 173 174
        default:
          LOG(FATAL) << "Unsupported TargetType";
          break;
      }
    }
    operation_state.addOperands(
        phi_context.at(phi_kernel_desc.kernelType.target));
    for (size_t index = 0; index < phi_kernel_desc.inputsType.size(); ++index) {
      mlir::Value input = kernel_op.getOperand(index);
175
      auto cvt_tensor_type_op = builder.create<infrt::CvtTensorOp>(
176
          kernel_op.getLoc(),
177 178 179 180 181
          infrt::DenseTensorType::get(
              kernel_op.getContext(),
              phi_kernel_desc.inputsType[index].target,
              phi_kernel_desc.inputsType[index].precision,
              phi_kernel_desc.inputsType[index].layout),
182 183 184 185 186
          input);
      operation_state.addOperands(cvt_tensor_type_op.output());
    }
    for (size_t index = 0; index < phi_kernel_desc.outputsType.size();
         ++index) {
187 188 189 190 191
      operation_state.addTypes(infrt::DenseTensorType::get(
          kernel_op.getContext(),
          phi_kernel_desc.outputsType[index].target,
          phi_kernel_desc.outputsType[index].precision,
          phi_kernel_desc.outputsType[index].layout));
192 193 194 195 196 197
    }
    operation_state.addAttributes(kernel_op.attrsAttr().getValue());
    mlir::Operation *phi_operation = builder.createOperation(operation_state);
    for (size_t index = 0; index < phi_kernel_desc.outputsType.size();
         ++index) {
      mlir::Value input = phi_operation->getResult(index);
198
      auto cvt_tensor_type_op = builder.create<infrt::CvtTensorOp>(
199 200 201 202 203
          kernel_op.getLoc(), kernel_op.getResultTypes()[index], input);
      kernel_op.getResult(index).replaceAllUsesWith(
          cvt_tensor_type_op.output());
    }
    kernel_op.erase();
204 205
  }
}
206 207 208 209 210 211 212

}  // namespace

std::unique_ptr<mlir::Pass> infrt::createPhiOpCvtPass(
    std::vector<Place> valid_places) {
  return std::make_unique<phiOpCvtPass>(valid_places);
}