|
| 1 | +# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Redistribution and use in source and binary forms, with or without |
| 4 | +# modification, are permitted provided that the following conditions |
| 5 | +# are met: |
| 6 | +# * Redistributions of source code must retain the above copyright |
| 7 | +# notice, this list of conditions and the following disclaimer. |
| 8 | +# * Redistributions in binary form must reproduce the above copyright |
| 9 | +# notice, this list of conditions and the following disclaimer in the |
| 10 | +# documentation and/or other materials provided with the distribution. |
| 11 | +# * Neither the name of NVIDIA CORPORATION nor the names of its |
| 12 | +# contributors may be used to endorse or promote products derived |
| 13 | +# from this software without specific prior written permission. |
| 14 | +# |
| 15 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY |
| 16 | +# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 18 | +# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 19 | +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | +# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | +# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | +# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 23 | +# OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + |
| 27 | +cmake_minimum_required(VERSION 3.17) |
| 28 | + |
| 29 | +project(tritontensorrtbackend LANGUAGES C CXX) |
| 30 | + |
| 31 | +# |
| 32 | +# Options |
| 33 | +# |
| 34 | +option(TRITON_ENABLE_GPU "Enable GPU support in backend." ON) |
| 35 | +option(TRITON_ENABLE_STATS "Include statistics collections in backend." ON) |
| 36 | +set(TRITON_TENSORRT_LIB_PATHS "" CACHE PATH "Paths to TensorRT libraries. Multiple paths may be specified by separating them with a semicolon.") |
| 37 | +set(TRITON_TENSORRT_INCLUDE_PATHS "" CACHE PATH "Paths to TensorRT includes. Multiple paths may be specified by separating them with a semicolon.") |
| 38 | + |
| 39 | +set(TRITON_BACKEND_REPO_TAG "main" CACHE STRING "Tag for triton-inference-server/backend repo.") |
| 40 | +set(TRITON_CORE_REPO_TAG "main" CACHE STRING "Tag for triton-inference-server/core repo.") |
| 41 | +set(TRITON_COMMON_REPO_TAG "main" CACHE STRING "Tag for triton-inference-server/common repo.") |
| 42 | + |
| 43 | +if(NOT CMAKE_BUILD_TYPE) |
| 44 | + set(CMAKE_BUILD_TYPE Release) |
| 45 | +endif() |
| 46 | + |
| 47 | +set(TRITON_TENSORRT_BACKEND_LIBNAME triton_tensorrt) |
| 48 | +set(TRITON_TENSORRT_BACKEND_INSTALLDIR ${CMAKE_INSTALL_PREFIX}/backends/tensorrt) |
| 49 | + |
| 50 | +# |
| 51 | +# Dependencies |
| 52 | +# |
| 53 | +# FetchContent's composibility isn't very good. We must include the |
| 54 | +# transitive closure of all repos so that we can override the tag. |
| 55 | +# |
| 56 | +include(FetchContent) |
| 57 | + |
| 58 | +FetchContent_Declare( |
| 59 | + repo-common |
| 60 | + GIT_REPOSITORY https://github.com/triton-inference-server/common.git |
| 61 | + GIT_TAG ${TRITON_COMMON_REPO_TAG} |
| 62 | + GIT_SHALLOW ON |
| 63 | +) |
| 64 | +FetchContent_Declare( |
| 65 | + repo-core |
| 66 | + GIT_REPOSITORY https://github.com/triton-inference-server/core.git |
| 67 | + GIT_TAG ${TRITON_CORE_REPO_TAG} |
| 68 | + GIT_SHALLOW ON |
| 69 | +) |
| 70 | +FetchContent_Declare( |
| 71 | + repo-backend |
| 72 | + GIT_REPOSITORY https://github.com/triton-inference-server/backend.git |
| 73 | + GIT_TAG ${TRITON_BACKEND_REPO_TAG} |
| 74 | + GIT_SHALLOW ON |
| 75 | +) |
| 76 | +FetchContent_MakeAvailable(repo-common repo-core repo-backend) |
| 77 | + |
| 78 | +# |
| 79 | +# CUDA |
| 80 | +# |
| 81 | +if(${TRITON_ENABLE_GPU}) |
| 82 | + find_package(CUDAToolkit REQUIRED) |
| 83 | + message(STATUS "Using CUDA ${CUDA_VERSION}") |
| 84 | + set(CUDA_NVCC_FLAGS -std=c++11) |
| 85 | + |
| 86 | + if(CUDA_VERSION VERSION_GREATER "10.1" OR CUDA_VERSION VERSION_EQUAL "10.1") |
| 87 | + add_definitions(-DTRITON_ENABLE_CUDA_GRAPH=1) |
| 88 | + else() |
| 89 | + message(WARNING "CUDA ${CUDA_VERSION} does not support CUDA graphs.") |
| 90 | + endif() |
| 91 | +else() |
| 92 | + message( FATAL_ERROR "TensorRT backend requires TRITON_ENABLE_GPU=1, CMake will exit." ) |
| 93 | +endif() # TRITON_ENABLE_GPU |
| 94 | + |
| 95 | +# |
| 96 | +# Shared library implementing the Triton Backend API |
| 97 | +# |
| 98 | +configure_file(src/libtriton_tensorrt.ldscript libtriton_tensorrt.ldscript COPYONLY) |
| 99 | + |
| 100 | +add_library( |
| 101 | + triton-tensorrt-backend SHARED |
| 102 | + src/tensorrt.cc |
| 103 | + src/tensorrt_model.cc |
| 104 | + src/tensorrt_model.h |
| 105 | + src/tensorrt_model_instance.cc |
| 106 | + src/tensorrt_model_instance.h |
| 107 | + src/tensorrt_utils.cc |
| 108 | + src/tensorrt_utils.h |
| 109 | + src/loader.cc |
| 110 | + src/loader.h |
| 111 | + src/logging.cc |
| 112 | + src/logging.h |
| 113 | +) |
| 114 | + |
| 115 | +add_library( |
| 116 | + TritonTensorRTBackend::triton-tensorrt-backend ALIAS triton-tensorrt-backend |
| 117 | +) |
| 118 | + |
| 119 | +target_include_directories( |
| 120 | + triton-tensorrt-backend |
| 121 | + PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src |
| 122 | +) |
| 123 | + |
| 124 | +target_include_directories( |
| 125 | + triton-tensorrt-backend |
| 126 | + PRIVATE ${TRITON_TENSORRT_INCLUDE_PATHS} |
| 127 | + ) |
| 128 | + |
| 129 | +target_compile_features(triton-tensorrt-backend PRIVATE cxx_std_11) |
| 130 | +target_compile_options( |
| 131 | + triton-tensorrt-backend PRIVATE |
| 132 | + $<$<OR:$<CXX_COMPILER_ID:Clang>,$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:GNU>>: |
| 133 | + -Wall -Wextra -Wno-unused-parameter -Wno-type-limits -Werror> |
| 134 | +) |
| 135 | + |
| 136 | +# C/C++ defines that are used directly by this backend. |
| 137 | +target_compile_definitions( |
| 138 | + triton-tensorrt-backend |
| 139 | + PRIVATE TRITON_ENABLE_GPU=1 |
| 140 | +) |
| 141 | + |
| 142 | +set_target_properties( |
| 143 | + triton-tensorrt-backend |
| 144 | + PROPERTIES |
| 145 | + POSITION_INDEPENDENT_CODE ON |
| 146 | + OUTPUT_NAME ${TRITON_TENSORRT_BACKEND_LIBNAME} |
| 147 | + SKIP_BUILD_RPATH TRUE |
| 148 | + BUILD_WITH_INSTALL_RPATH TRUE |
| 149 | + INSTALL_RPATH_USE_LINK_PATH FALSE |
| 150 | + INSTALL_RPATH "$\{ORIGIN\}" |
| 151 | + LINK_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/libtriton_tensorrt.ldscript |
| 152 | + LINK_FLAGS "-Wl,--version-script libtriton_tensorrt.ldscript" |
| 153 | +) |
| 154 | + |
| 155 | +FOREACH(p ${TRITON_TENSORRT_LIB_PATHS}) |
| 156 | + set(TRITON_TENSORRT_LDFLAGS ${TRITON_TENSORRT_LDFLAGS} "-L${p}") |
| 157 | +ENDFOREACH(p) |
| 158 | + |
| 159 | + |
| 160 | +target_link_libraries( |
| 161 | + triton-tensorrt-backend |
| 162 | + PRIVATE |
| 163 | + triton-core-serverapi # from repo-core |
| 164 | + triton-core-serverstub # from repo-core |
| 165 | + triton-backend-utils # from repo-backend |
| 166 | + -lpthread |
| 167 | +) |
| 168 | + |
| 169 | +target_link_libraries( |
| 170 | + triton-tensorrt-backend |
| 171 | + PRIVATE ${TRITON_TENSORRT_LDFLAGS} |
| 172 | +) |
| 173 | + |
| 174 | +target_link_libraries( |
| 175 | + triton-tensorrt-backend |
| 176 | + PRIVATE |
| 177 | + CUDA::cudart |
| 178 | +) |
| 179 | + |
| 180 | + |
| 181 | +# |
| 182 | +# Install |
| 183 | +# |
| 184 | +include(GNUInstallDirs) |
| 185 | +set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/TritonTensorRTBackend) |
| 186 | + |
| 187 | +install( |
| 188 | + TARGETS |
| 189 | + triton-tensorrt-backend |
| 190 | + EXPORT |
| 191 | + triton-tensorrt-backend-targets |
| 192 | + LIBRARY DESTINATION ${TRITON_TENSORRT_BACKEND_INSTALLDIR} |
| 193 | + ARCHIVE DESTINATION ${TRITON_TENSORRT_BACKEND_INSTALLDIR} |
| 194 | +) |
| 195 | + |
| 196 | +install( |
| 197 | + EXPORT |
| 198 | + triton-tensorrt-backend-targets |
| 199 | + FILE |
| 200 | + TritonTensorRTBackendTargets.cmake |
| 201 | + NAMESPACE |
| 202 | + TritonTensorRTBackend:: |
| 203 | + DESTINATION |
| 204 | + ${INSTALL_CONFIGDIR} |
| 205 | +) |
| 206 | + |
| 207 | +include(CMakePackageConfigHelpers) |
| 208 | +configure_package_config_file( |
| 209 | + ${CMAKE_CURRENT_LIST_DIR}/cmake/TritonTensorRTBackendConfig.cmake.in |
| 210 | + ${CMAKE_CURRENT_BINARY_DIR}/TritonTensorRTBackendConfig.cmake |
| 211 | + INSTALL_DESTINATION ${INSTALL_CONFIGDIR} |
| 212 | +) |
| 213 | + |
| 214 | +install( |
| 215 | + FILES |
| 216 | + ${CMAKE_CURRENT_BINARY_DIR}/TritonTensorRTBackendConfig.cmake |
| 217 | + DESTINATION ${INSTALL_CONFIGDIR} |
| 218 | +) |
| 219 | + |
| 220 | +# |
| 221 | +# Export from build tree |
| 222 | +# |
| 223 | +export( |
| 224 | + EXPORT triton-tensorrt-backend-targets |
| 225 | + FILE ${CMAKE_CURRENT_BINARY_DIR}/TritonTensorRTBackendTargets.cmake |
| 226 | + NAMESPACE TritonTensorRTBackend:: |
| 227 | +) |
| 228 | + |
| 229 | +export(PACKAGE TritonTensorRTBackend) |
0 commit comments