|
| 1 | +# usage: |
| 2 | +# cd build/ |
| 3 | +# cmake -S ../ -B ./ -DCMAKE_BUILD_TYPE=Debug -DORT_HOME=/path/to/ort_package/onnxruntime-win-x64-gpu-1.23.0 -DCMAKE_CUDA_ARCHITECTURES=80 -DCMAKE_CUDA_COMPILER=/usr/local/cuda/bin/nvcc -DTENSORRT_HOME=/path/to/tensorrt/TensorRT-10.3.0.26 -DCMAKE_POSITION_INDEPENDENT_CODE=ON (see the result of "nvidia-smi --query-gpu=compute_cap --format=csv,noheader,nounits") |
| 4 | +# cmake --build ./ --config Debug |
| 5 | +cmake_minimum_required(VERSION 3.26) |
| 6 | +project(TensorRTEp VERSION 1.0) |
| 7 | +set(CMAKE_CXX_STANDARD 17) |
| 8 | + |
| 9 | +enable_language(CUDA) # via nvcc to get the CUDA tool kit |
| 10 | +file(TO_CMAKE_PATH "/usr/local/cuda" CUDAToolkit_ROOT) |
| 11 | +find_package(CUDAToolkit REQUIRED) |
| 12 | + |
| 13 | +# CMake config to force dynamic debug CRT or dynamic release CRT globally for all dependencies. |
| 14 | +# This is to address the issue of: |
| 15 | +# libprotobufd.lib(common.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in unary_elementwise_ops_impl.obj |
| 16 | +if (WIN32) |
| 17 | + if(CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 18 | + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDebugDLL" CACHE STRING "" FORCE) # /MDd |
| 19 | + set(BUILD_SHARED_LIBS OFF) # Build protobuf as static .lib, but using dynamic runtime |
| 20 | + endif() |
| 21 | + |
| 22 | + if(CMAKE_BUILD_TYPE STREQUAL "Release") |
| 23 | + set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL" CACHE STRING "" FORCE) |
| 24 | + set(BUILD_SHARED_LIBS OFF) # Build protobuf as static .lib, but using dynamic runtime |
| 25 | + endif() |
| 26 | +endif() |
| 27 | + |
| 28 | +add_definitions(-DONNX_NAMESPACE=onnx) |
| 29 | +add_definitions(-DONNX_ML) |
| 30 | +add_definitions(-DNOMINMAX) |
| 31 | +file(GLOB tensorrt_src "./*.cc" "./utils/*.cc" "./cuda/unary_elementwise_ops_impl.cu" "./*.h") |
| 32 | +add_library(TensorRTEp SHARED ${tensorrt_src}) |
| 33 | + |
| 34 | +if (NOT ORT_HOME) |
| 35 | + message(FATAL_ERROR "Please specify ORT_HOME, e.g. -DORT_HOME=/path/to/ort/") |
| 36 | +endif() |
| 37 | + |
| 38 | +if (NOT TENSORRT_HOME) |
| 39 | + message(FATAL_ERROR "Please specify TENSORRT_HOME, e.g. -DTENSORRT_HOME=/path/to/trt/") |
| 40 | +endif() |
| 41 | + |
| 42 | +# Use release mode if not specified |
| 43 | +if (NOT CMAKE_BUILD_TYPE) |
| 44 | + set(CMAKE_BUILD_TYPE "Release") |
| 45 | +endif() |
| 46 | + |
| 47 | +# Add dependencies |
| 48 | +include(FetchContent) |
| 49 | + |
| 50 | +# Add protobuf |
| 51 | +FetchContent_Declare( |
| 52 | + protobuf |
| 53 | + GIT_REPOSITORY https://github.com/protocolbuffers/protobuf.git |
| 54 | + GIT_TAG v21.12 # Use a specific tag or commit |
| 55 | +) |
| 56 | + |
| 57 | +if (WIN32) |
| 58 | + # Sometimes, protobuf ignores CMAKE_MSVC_RUNTIME_LIBRARY. To ensure it works: |
| 59 | + set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "" FORCE) |
| 60 | +endif() |
| 61 | + |
| 62 | +FetchContent_MakeAvailable(protobuf) |
| 63 | + |
| 64 | +# Add ONNX |
| 65 | +FetchContent_Declare( |
| 66 | + onnx |
| 67 | + GIT_REPOSITORY https://github.com/onnx/onnx.git |
| 68 | + GIT_TAG v1.18.0 # Use a specific tag or commit |
| 69 | +) |
| 70 | + |
| 71 | +FetchContent_MakeAvailable(onnx) |
| 72 | + |
| 73 | +# Add GSL |
| 74 | +FetchContent_Declare( |
| 75 | + gsl |
| 76 | + GIT_REPOSITORY https://github.com/microsoft/GSL.git |
| 77 | + GIT_TAG v4.0.0 # Use a specific tag or commit |
| 78 | +) |
| 79 | + |
| 80 | +FetchContent_MakeAvailable(gsl) |
| 81 | + |
| 82 | +# Add flatbuffers |
| 83 | +FetchContent_Declare( |
| 84 | + flatbuffers |
| 85 | + GIT_REPOSITORY https://github.com/google/flatbuffers.git |
| 86 | + GIT_TAG v23.5.26 # Use a specific tag or commit |
| 87 | +) |
| 88 | + |
| 89 | +FetchContent_MakeAvailable(flatbuffers) |
| 90 | + |
| 91 | +set(DEPS_PATH "${CMAKE_BINARY_DIR}/_deps") |
| 92 | + |
| 93 | +if (WIN32) # Windows |
| 94 | + set(ORT_LIB "${ORT_HOME}/lib/onnxruntime.lib") |
| 95 | + set(TRT_LIBS "${TENSORRT_HOME}/lib/nvinfer_10.lib" |
| 96 | + "${TENSORRT_HOME}/lib/nvinfer_plugin_10.lib" |
| 97 | + "${TENSORRT_HOME}/lib/nvonnxparser_10.lib") |
| 98 | + |
| 99 | + if(CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 100 | + set(DEPS_LIBS ${DEPS_LIBS} |
| 101 | + "${DEPS_PATH}/protobuf-build/${CMAKE_BUILD_TYPE}/libprotobufd.lib" |
| 102 | + "${DEPS_PATH}/protobuf-build/${CMAKE_BUILD_TYPE}/libprotocd.lib") |
| 103 | + else() |
| 104 | + set(DEPS_LIBS ${DEPS_LIBS} |
| 105 | + "${DEPS_PATH}/protobuf-build/${CMAKE_BUILD_TYPE}/libprotobuf.lib" |
| 106 | + "${DEPS_PATH}/protobuf-build/${CMAKE_BUILD_TYPE}/libprotoc.lib") |
| 107 | + endif() |
| 108 | + |
| 109 | + set(DEPS_LIBS "${DEPS_PATH}/flatbuffers-build/${CMAKE_BUILD_TYPE}/flatbuffers.lib" |
| 110 | + "${DEPS_PATH}/onnx-build/${CMAKE_BUILD_TYPE}/onnx.lib" |
| 111 | + "${DEPS_PATH}/onnx-build/${CMAKE_BUILD_TYPE}/onnx_proto.lib") |
| 112 | + |
| 113 | + set(TRT_EP_LIB_LINK_FLAG |
| 114 | + "-DEF:${CMAKE_SOURCE_DIR}/tensorrt_execution_provider.def") |
| 115 | + |
| 116 | +else() # Linux |
| 117 | + set(ORT_LIB "${ORT_HOME}/lib/libonnxruntime.so") |
| 118 | + set(TRT_LIBS "${TENSORRT_HOME}/lib/libnvinfer.so" |
| 119 | + "${TENSORRT_HOME}/lib/libnvinfer_plugin.so" |
| 120 | + "${TENSORRT_HOME}/lib/libnvonnxparser.so") |
| 121 | + set(DEPS_LIBS "${DEPS_PATH}/flatbuffers-build/libflatbuffers.a" |
| 122 | + "${DEPS_PATH}/onnx-build/libonnx.a" |
| 123 | + "${DEPS_PATH}/onnx-build/libonnx_proto.a") |
| 124 | + |
| 125 | + if(CMAKE_BUILD_TYPE STREQUAL "Debug") |
| 126 | + set(DEPS_LIBS ${DEPS_LIBS} |
| 127 | + "${DEPS_PATH}/protobuf-build/libprotobufd.a" |
| 128 | + "${DEPS_PATH}/protobuf-build/libprotocd.a") |
| 129 | + else() |
| 130 | + set(DEPS_LIBS ${DEPS_LIBS} |
| 131 | + "${DEPS_PATH}/protobuf-build/libprotobuf.a" |
| 132 | + "${DEPS_PATH}/protobuf-build/libprotoc.a") |
| 133 | + endif() |
| 134 | +endif() |
| 135 | + |
| 136 | +MESSAGE(STATUS "Looking for following dependencies ...") |
| 137 | +MESSAGE(STATUS "ORT lib : ${ORT_LIB}") |
| 138 | +MESSAGE(STATUS "TRT libs : ${TRT_LIBS}") |
| 139 | +MESSAGE(STATUS "Deps libs: ${DEPS_LIBS}") |
| 140 | + |
| 141 | +set_property(TARGET TensorRTEp APPEND_STRING PROPERTY LINK_FLAGS |
| 142 | + ${TRT_EP_LIB_LINK_FLAG}) |
| 143 | + |
| 144 | +target_include_directories(TensorRTEp PUBLIC "${ORT_HOME}/include" |
| 145 | + "./utils" |
| 146 | + "/usr/local/cuda/include" |
| 147 | + "${TENSORRT_HOME}/include" |
| 148 | + "${DEPS_PATH}/flatbuffers-src/include" |
| 149 | + "${DEPS_PATH}/gsl-src/include" # GSL is header-only |
| 150 | + "${DEPS_PATH}/onnx-src" |
| 151 | + "${DEPS_PATH}/onnx-build" |
| 152 | + "${DEPS_PATH}/protobuf-src/src" |
| 153 | +) |
| 154 | + |
| 155 | +target_link_libraries(TensorRTEp PUBLIC #${DEPS_LIBS} |
| 156 | + protobuf::libprotobuf onnx flatbuffers |
| 157 | + ${ORT_LIB} |
| 158 | + ${TRT_LIBS} |
| 159 | + CUDA::cudart |
| 160 | +) |
0 commit comments