op_converter.h 3.3 KB
Newer Older
L
Luo Tao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (c) 2018 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. */

#pragma once

#include <string>
#include <unordered_map>
#include "paddle/fluid/framework/block_desc.h"
#include "paddle/fluid/framework/scope.h"
#include "paddle/fluid/inference/tensorrt/engine.h"
L
Luo Tao 已提交
22
#include "paddle/fluid/inference/utils/singleton.h"
L
Luo Tao 已提交
23 24 25 26 27 28 29 30 31 32 33

namespace paddle {
namespace inference {
namespace tensorrt {

/*
 * Convert Op from Fluid to TensorRT Engine.
 */
class OpConverter {
 public:
  OpConverter() {}
L
Luo Tao 已提交
34

35 36 37 38 39 40 41 42 43 44 45
  // Converter logic for an op.
  virtual void operator()(const framework::proto::OpDesc& op,
                          const framework::Scope& scope) {}

  // Convert a single fluid operaotr and add the corresponding layer to TRT.
  void ConvertOp(const framework::proto::OpDesc& op,
                 const std::unordered_set<std::string>& parameters,
                 const framework::Scope& scope, TensorRTEngine* engine) {
    framework::OpDesc op_desc(op, nullptr, nullptr);

    OpConverter* it{nullptr};
L
Luo Tao 已提交
46

47 48 49 50 51 52 53 54 55 56 57 58 59 60
    if (op_desc.Type() == "mul") {
      PADDLE_ENFORCE_EQ(op_desc.Input("Y").size(), 1UL);
      std::string Y = op_desc.Input("Y")[0];
      if (parameters.count(Y)) {
        it = Registry<OpConverter>::Lookup("fc");
      }
    }
    if (!it) {
      it = Registry<OpConverter>::Lookup(op_desc.Type());
    }
    PADDLE_ENFORCE_NOT_NULL(it, "no OpConverter for optype [%s]",
                            op_desc.Type());
    it->SetEngine(engine);
    (*it)(op, scope);
L
Luo Tao 已提交
61 62 63
  }

  // convert fluid block to tensorrt network
64
  void ConvertBlock(const framework::proto::BlockDesc& block,
65 66
                    const std::unordered_set<std::string>& parameters,
                    const framework::Scope& scope, TensorRTEngine* engine) {
K
Kexin Zhao 已提交
67
    for (int i = 0; i < block.ops_size(); i++) {
68
      const auto& op = block.ops(i);
69
      ConvertOp(op, parameters, scope, engine);
L
Luo Tao 已提交
70 71 72 73 74
    }
  }

  void SetEngine(TensorRTEngine* engine) { engine_ = engine; }

L
Luo Tao 已提交
75 76
  virtual ~OpConverter() {}

L
Luo Tao 已提交
77 78 79
  // TensorRT engine
  TensorRTEngine* engine_{nullptr};

L
Luo Tao 已提交
80 81 82 83 84
 private:
  // registered op converter map, whose key is the fluid op type, and value is
  // the pointer position of corresponding OpConverter class.
  std::unordered_map<std::string, OpConverter*> converters_;
  // fluid inference scope
L
Luo Tao 已提交
85
  framework::Scope* scope_{nullptr};
L
Luo Tao 已提交
86 87
};

L
Luo Tao 已提交
88 89 90 91 92 93
#define REGISTER_TRT_OP_CONVERTER(op_type__, Converter__)       \
  struct trt_##op_type__##_converter {                          \
    trt_##op_type__##_converter() {                             \
      Registry<OpConverter>::Register<Converter__>(#op_type__); \
    }                                                           \
  };                                                            \
L
Luo Tao 已提交
94 95 96 97 98
  trt_##op_type__##_converter trt_##op_type__##_converter__;

}  // namespace tensorrt
}  // namespace inference
}  // namespace paddle