Skip to content

Commit 0436df3

Browse files
committed
support for tensorrt
1 parent 89acb2e commit 0436df3

File tree

8 files changed

+105
-1
lines changed

8 files changed

+105
-1
lines changed

CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ find_package(CUDA QUIET)
1717

1818
if(CUDA_FOUND AND USE_GPU)
1919
add_definitions(-DENABLE_GPU=1)
20+
include(tensorrt)
21+
if (TENSORRT_FOUND)
22+
add_definitions(-DENABLE_TENSORRT=1)
23+
endif()
2024
else()
2125
add_definitions(-DENABLE_GPU=0)
26+
add_definitions(-DENABLE_TENSORRT=0)
2227
endif()
2328

2429

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ Hope that they both are helpful for your work.
2323
## TODO
2424

2525
- [x] Support inference of multi-inputs, multi-outputs
26-
- [x] Examples for famous models, like yolov3, mask-rcnn, [ultra-light-weight face detector](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB), [yolox](https://github.com/Megvii-BaseDetection/YOLOX), [PaddleSeg](https://github.com/PaddlePaddle/PaddleSeg/tree/release/2.3), [SuperPoint](https://github.com/magicleap/SuperPointPretrainedNetwork), [SuperGlue](https://github.com/magicleap/SuperGluePretrainedNetwork/tree/ddcf11f42e7e0732a0c4607648f9448ea8d73590). Might consider supporting more if requested
26+
- [x] Examples for famous models, like yolov3, mask-rcnn, [ultra-light-weight face detector](https://github.com/Linzaer/Ultra-Light-Fast-Generic-Face-Detector-1MB), [yolox](https://github.com/Megvii-BaseDetection/YOLOX), [PaddleSeg](https://github.com/PaddlePaddle/PaddleSeg/tree/release/2.3), [SuperPoint](https://github.com/magicleap/SuperPointPretrainedNetwork), [SuperGlue](https://github.com/magicleap/SuperGluePretrainedNetwork/tree/ddcf11f42e7e0732a0c4607648f9448ea8d73590). Might consider supporting more if requested.
27+
- [x] (Minimal^^) Support for TensorRT backend
2728
- [ ] Batch-inference
2829

2930
## Installation
@@ -96,6 +97,10 @@ docker build -f ./dockerfiles/ubuntu2004_gpu.dockerfile -t onnx_runtime_gpu .
9697
docker run -it --rm --gpus all -v `pwd`:/workspace onnx_runtime_gpu
9798
```
9899

100+
- Onnxruntime will be built with TensorRT support if the environment has TensorRT. Check [this memo](./docs/onnxruntime_tensorrt.md) for useful URLs related to building with TensorRT.
101+
- Be careful to choose TensorRT version compatible with onnxruntime. A good guess can be inferred from [HERE](https://github.com/microsoft/onnxruntime/blob/main/dockerfiles/Dockerfile.tensorrt).
102+
- Also it is not possible to use models whose input shapes are dynamic with TensorRT backend, according to [this](https://onnxruntime.ai/docs/execution-providers/TensorRT-ExecutionProvider.html#shape-inference-for-tensorrt-subgraphs)
103+
99104
</details>
100105

101106
## How to test apps

cmake/tensorrt.cmake

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Ref: //github.com/PRBonn/rangenet_lib/blob/master/cmake/tensorrt-config.cmake
2+
3+
find_package(CUDA)
4+
find_library(NVINFER NAMES nvinfer)
5+
find_library(NVINFERPLUGIN NAMES nvinfer_plugin)
6+
find_library(NVPARSERS NAMES nvparsers)
7+
find_library(NVONNXPARSER NAMES nvonnxparser)
8+
9+
# newer tensorrt does not have nvonnxparser_runtime
10+
# find_library(NVONNXPARSERRUNTIME NAMES nvonnxparser_runtime)
11+
12+
# If it is ALL there, export libraries as a single package
13+
if(CUDA_FOUND AND NVINFER AND NVINFERPLUGIN AND NVPARSERS AND NVONNXPARSER)
14+
message("TensorRT available!")
15+
message("CUDA Libs: ${CUDA_LIBRARIES}")
16+
message("CUDA Headers: ${CUDA_INCLUDE_DIRS}")
17+
message("NVINFER: ${NVINFER}")
18+
message("NVINFERPLUGIN: ${NVINFERPLUGIN}")
19+
message("NVPARSERS: ${NVPARSERS}")
20+
message("NVONNXPARSER: ${NVONNXPARSER}")
21+
list(APPEND TENSORRT_LIBRARIES ${CUDA_LIBRARIES} nvinfer nvinfer_plugin nvparsers nvonnxparser)
22+
message("All togheter now (libs): ${TENSORRT_LIBRARIES}")
23+
list(APPEND TENSORRT_INCLUDE_DIRS ${CUDA_INCLUDE_DIRS})
24+
message("All togheter now (inc): ${TENSORRT_INCLUDE_DIRS}")
25+
set(TENSORRT_FOUND ON)
26+
else()
27+
message("TensorRT NOT Available")
28+
set(TENSORRT_FOUND OFF)
29+
endif()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM nvcr.io/nvidia/tensorrt:21.07-py3
2+
3+
ENV DEBIAN_FRONTEND=noninteractive
4+
5+
WORKDIR /build
6+
7+
RUN apt-get update && \
8+
apt-get install -y --no-install-recommends \
9+
sudo \
10+
gnupg2 \
11+
lsb-release \
12+
build-essential \
13+
software-properties-common \
14+
cmake \
15+
git \
16+
tmux && \
17+
bash install_latest_cmake.bash && \
18+
bash install_onnx_runtime.bash && \
19+
bash install_apps_dependencies.bash && \
20+
rm -rf /build && \
21+
apt-get clean && \
22+
rm -rf /var/lib/apt/lists/*
23+
24+
WORKDIR /workspace
25+
26+
ENTRYPOINT ["/bin/bash"]

docs/onnxruntime_tensorrt.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 📝 memo on how to use tensorrt onnxruntime
2+
3+
---
4+
5+
## :running: How to Run
6+
7+
---
8+
9+
Sample docker with tensorrt environment is provided [HERE](../dockerfiles/ubuntu2004_tensorrt.dockerfile)
10+
11+
## :gem: References
12+
13+
---
14+
15+
- [tensorrt tags](https://catalog.ngc.nvidia.com/orgs/nvidia/containers/tensorrt/tags)
16+
- [overview of tensorrt docker](https://docs.nvidia.com/deeplearning/tensorrt/container-release-notes/overview.html)
17+
- [tensorrt introduction](https://developer.nvidia.com/tensorrt)
18+
- [build instruction](https://onnxruntime.ai/docs/build/eps.html)
19+
- [sample dockerfile provided by onnxruntime repo](https://github.com/microsoft/onnxruntime/blob/v1.10.0/dockerfiles/Dockerfile.tensorrt): _need to choose suitable tensorrt version that matches onnxruntime version_
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env bash
2+
3+
export TENSORRT_HOME="$(dirname $(whereis libnvinfer | awk '{print $2}'))"

scripts/install_onnx_runtime.bash

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ source $CURRENT_DIR/get_cuda_environment_variables.bash
2424
if [ ! -z "$CUDA_HOME" -a ! -z "$CUDA_VERSION" -a ! -z "$CUDNN_HOME" ]; then
2525
BUILDARGS="${BUILDARGS} --use_cuda --cuda_version=${CUDA_VERSION} --cuda_home=${CUDA_HOME} --cudnn_home=${CUDNN_HOME}"
2626
fi
27+
28+
source $CURRENT_DIR/get_tensorrt_environment_variables.bash
29+
if [ ! -z "$TENSORRT_HOME" ]; then
30+
# onnxruntime v1.10.0 is compatible with tensorrt 8
31+
BUILDARGS="${BUILDARGS} --use_tensorrt --tensorrt_home=${TENSORRT_HOME}"
32+
fi
33+
2734
./build.sh ${BUILDARGS}
2835
cd ./build/Linux/${BUILDTYPE}
2936
sudo make install

src/OrtSessionHandler.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
*/
77

88
#include <onnxruntime/core/session/onnxruntime_cxx_api.h>
9+
10+
#if ENABLE_TENSORRT
11+
#include <onnxruntime/core/providers/tensorrt/tensorrt_provider_factory.h>
12+
#endif
13+
914
#include <ort_utility/ort_utility.hpp>
1015

1116
#include <algorithm>
@@ -207,10 +212,15 @@ void OrtSessionHandler::OrtSessionHandlerIml::initSession()
207212
Ort::SessionOptions sessionOptions;
208213

209214
sessionOptions.SetIntraOpNumThreads(1);
215+
// tensorrt options can be customized into sessionOptions
216+
// https://onnxruntime.ai/docs/execution-providers/TensorRT-ExecutionProvider.html
210217

211218
#if ENABLE_GPU
212219
if (m_gpuIdx.has_value()) {
213220
Ort::ThrowOnError(OrtSessionOptionsAppendExecutionProvider_CUDA(sessionOptions, m_gpuIdx.value()));
221+
#if ENABLE_TENSORRT
222+
Ort::ThrowOnError(OrtSessionOptionsAppendExecutionProvider_Tensorrt(sessionOptions, m_gpuIdx.value()));
223+
#endif
214224
}
215225
#endif
216226

0 commit comments

Comments
 (0)