|
| 1 | +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | + |
| 17 | +#include <unordered_map> |
| 18 | +#include "paddle/fluid/framework/lod_tensor.h" |
| 19 | +#include "paddle/fluid/inference/utils/singleton.h" |
| 20 | + |
| 21 | +namespace paddle { |
| 22 | +namespace inference { |
| 23 | +namespace tensorrt { |
| 24 | + |
| 25 | +using framework::LoDTensor; |
| 26 | + |
| 27 | +/* |
| 28 | + * Convert Input from Fluid to an Engine. |
| 29 | + * TensorRT's ITensor follows row major, NCHW. Fluid is also row major, so in |
| 30 | + * most cases just need to copy the data. |
| 31 | + */ |
| 32 | +class EngineInputConverter { |
| 33 | + public: |
| 34 | + EngineInputConverter() {} |
| 35 | + |
| 36 | + virtual void operator()(const LoDTensor& in, void* out, size_t max_size) {} |
| 37 | + |
| 38 | + void SetStream(cudaStream_t* stream) { stream_ = stream; } |
| 39 | + |
| 40 | + static void Run(const std::string& in_op_type, const LoDTensor& in, void* out, |
| 41 | + size_t max_size, cudaStream_t* stream) { |
| 42 | + PADDLE_ENFORCE(stream != nullptr); |
| 43 | + auto* converter = Registry<EngineInputConverter>::Lookup(in_op_type); |
| 44 | + PADDLE_ENFORCE_NOT_NULL(converter); |
| 45 | + converter->SetStream(stream); |
| 46 | + (*converter)(in, out, max_size); |
| 47 | + } |
| 48 | + |
| 49 | + virtual ~EngineInputConverter() {} |
| 50 | + |
| 51 | + protected: |
| 52 | + cudaStream_t* stream_{nullptr}; |
| 53 | +}; |
| 54 | + |
| 55 | +} // namespace tensorrt |
| 56 | +} // namespace inference |
| 57 | +} // namespace paddle |
| 58 | + |
| 59 | +#define REGISTER_TENSORRT_INPUT_CONVERTER(in_op_type__, Converter__) \ |
| 60 | + struct trt_input_##in_op_type__##_converter { \ |
| 61 | + trt_input_##in_op_type__##_converter() { \ |
| 62 | + ::paddle::inference::Registry<EngineInputConverter>::Register< \ |
| 63 | + Converter__>(#in_op_type__); \ |
| 64 | + } \ |
| 65 | + }; \ |
| 66 | + trt_input_##in_op_type__##_converter trt_input_##in_op_type__##_converter__; |
0 commit comments