io_converter.h 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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

17
#include <string>
18 19 20 21 22 23 24 25 26 27 28
#include <unordered_map>
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/inference/utils/singleton.h"

namespace paddle {
namespace inference {
namespace tensorrt {

using framework::LoDTensor;

/*
29 30 31 32 33 34
 * Convert Input from Fluid to TensorRT Engine.
 * Convert Output from TensorRT Engine to Fluid.
 *
 * Note that TensorRT's ITensor follows row major, NCHW. Fluid is also row
 * major,
 * so in the default case just need to copy the data.
35
 */
36
class EngineIOConverter {
37
 public:
38
  EngineIOConverter() {}
39 40

  virtual void operator()(const LoDTensor& in, void* out, size_t max_size) {}
41
  virtual void operator()(const void* in, LoDTensor* out, size_t max_size) {}
42 43 44

  void SetStream(cudaStream_t* stream) { stream_ = stream; }

45 46
  static void ConvertInput(const std::string& op_type, const LoDTensor& in,
                           void* out, size_t max_size, cudaStream_t* stream) {
47
    PADDLE_ENFORCE(stream != nullptr);
48 49
    auto* converter = Registry<EngineIOConverter>::Lookup(
        op_type, "default" /* default_type */);
50 51 52 53 54
    PADDLE_ENFORCE_NOT_NULL(converter);
    converter->SetStream(stream);
    (*converter)(in, out, max_size);
  }

55 56 57 58 59 60 61 62 63 64 65 66
  static void ConvertOutput(const std::string& op_type, const void* in,
                            LoDTensor* out, size_t max_size,
                            cudaStream_t* stream) {
    PADDLE_ENFORCE(stream != nullptr);
    auto* converter = Registry<EngineIOConverter>::Lookup(
        op_type, "default" /* default_type */);
    PADDLE_ENFORCE_NOT_NULL(converter);
    converter->SetStream(stream);
    (*converter)(in, out, max_size);
  }

  virtual ~EngineIOConverter() {}
67 68 69 70 71

 protected:
  cudaStream_t* stream_{nullptr};
};

72 73 74 75 76 77 78 79
#define REGISTER_TENSORRT_IO_CONVERTER(op_type__, Converter__)        \
  struct trt_io_##op_type__##_converter {                             \
    trt_io_##op_type__##_converter() {                                \
      Registry<EngineIOConverter>::Register<Converter__>(#op_type__); \
    }                                                                 \
  };                                                                  \
  trt_io_##op_type__##_converter trt_io_##op_type__##_converter__;

80 81 82
}  // namespace tensorrt
}  // namespace inference
}  // namespace paddle