From 279c993483e8ff924e10688ce85de663aaa97739 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 25 Jul 2022 08:59:53 +0800 Subject: [PATCH] Polish cmake files and runtime apis (#36) * Add custom operator for onnxruntime ans fix paddle backend * Polish cmake files and runtime apis * Remove copy libraries * fix some issue * fix bug * fix bug --- CMakeLists.txt | 111 ++++++++++--------- FastDeploy.cmake.in | 21 ++-- copy_directory.py | 32 ++++++ fastdeploy/backends/ort/ort_backend.cc | 8 +- fastdeploy/backends/tensorrt/trt_backend.cc | 111 ++++++++++++------- fastdeploy/backends/tensorrt/trt_backend.h | 1 - fastdeploy/fastdeploy_model.cc | 6 +- fastdeploy/fastdeploy_runtime.cc | 114 ++++++++++++++++++-- fastdeploy/fastdeploy_runtime.h | 53 ++++++++- fastdeploy/fastdeploy_runtime.py | 24 +++-- fastdeploy/pybind/fastdeploy_runtime.cc | 83 ++++++++------ fastdeploy/utils/utils.cc | 2 +- fastdeploy/utils/utils.h | 14 ++- fastdeploy/vision/common/processors/cast.cc | 14 ++- model_zoo/vision/ppyoloe/cpp/CMakeLists.txt | 2 +- model_zoo/vision/ppyoloe/cpp/ppyoloe.cc | 6 +- model_zoo/vision/yolov5/cpp/CMakeLists.txt | 3 +- model_zoo/vision/yolox/cpp/CMakeLists.txt | 2 +- model_zoo/vision/yolox/yolox.py | 3 +- setup.py | 29 ++--- 20 files changed, 446 insertions(+), 193 deletions(-) create mode 100644 copy_directory.py diff --git a/CMakeLists.txt b/CMakeLists.txt index fa2d421a1..141c2d1c5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,8 +15,20 @@ PROJECT(fastdeploy C CXX) CMAKE_MINIMUM_REQUIRED (VERSION 3.16) +option(CSRCS_DIR_NAME "Name of source code directory") +option(LIBRARY_NAME "Name of build library name") +option(PY_LIBRARY_NAME "Name of build python library name") +if(NOT CSRCS_DIR_NAME) + set(CSRCS_DIR_NAME "./") +endif() +if(NOT LIBRARY_NAME) + set(LIBRARY_NAME "fastdeploy") +endif() +if(NOT PY_LIBRARY_NAME) + set(PY_LIBRARY_NAME "fastdeploy_main") +endif() include(ExternalProject) -add_subdirectory(fastdeploy) +add_subdirectory(${CSRCS_DIR_NAME}/fastdeploy) include(external/utils.cmake) # Set C++11 as standard for the whole project @@ -51,7 +63,8 @@ endif() option(BUILD_FASTDEPLOY_PYTHON "if build python lib for fastdeploy." OFF) -include_directories(${PROJECT_SOURCE_DIR}) +set(HEAD_DIR "${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}") +include_directories(${HEAD_DIR}) include_directories(${CMAKE_CURRENT_BINARY_DIR}) if (WITH_VISION_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples) @@ -62,12 +75,12 @@ if (WITH_VISION_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples) endif() add_definitions(-DFASTDEPLOY_LIB) -file(GLOB_RECURSE ALL_DEPLOY_SRCS ${PROJECT_SOURCE_DIR}/fastdeploy/*.cc) -file(GLOB_RECURSE DEPLOY_ORT_SRCS ${PROJECT_SOURCE_DIR}/fastdeploy/backends/ort/*.cc) -file(GLOB_RECURSE DEPLOY_PADDLE_SRCS ${PROJECT_SOURCE_DIR}/fastdeploy/backends/paddle/*.cc) -file(GLOB_RECURSE DEPLOY_TRT_SRCS ${PROJECT_SOURCE_DIR}/fastdeploy/backends/tensorrt/*.cc ${PROJECT_SOURCE_DIR}/fastdeploy/backends/tensorrt/*.cpp) -file(GLOB_RECURSE DEPLOY_VISION_SRCS ${PROJECT_SOURCE_DIR}/fastdeploy/vision/*.cc) -file(GLOB_RECURSE DEPLOY_PYBIND_SRCS ${PROJECT_SOURCE_DIR}/fastdeploy/pybind/*.cc ${PROJECT_SOURCE_DIR}/fastdeploy/*_pybind.cc) +file(GLOB_RECURSE ALL_DEPLOY_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/*.cc) +file(GLOB_RECURSE DEPLOY_ORT_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/backends/ort/*.cc) +file(GLOB_RECURSE DEPLOY_PADDLE_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/backends/paddle/*.cc) +file(GLOB_RECURSE DEPLOY_TRT_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/backends/tensorrt/*.cc ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/backends/tensorrt/*.cpp) +file(GLOB_RECURSE DEPLOY_VISION_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/vision/*.cc) +file(GLOB_RECURSE DEPLOY_PYBIND_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/pybind/*.cc ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/*_pybind.cc) list(REMOVE_ITEM ALL_DEPLOY_SRCS ${DEPLOY_ORT_SRCS} ${DEPLOY_PADDLE_SRCS} ${DEPLOY_TRT_SRCS} ${DEPLOY_VISION_SRCS}) set(DEPEND_LIBS "") @@ -117,7 +130,7 @@ if(ENABLE_TRT_BACKEND) endif() add_definitions(-DENABLE_TRT_BACKEND) include_directories(${TRT_DIRECTORY}/include) - include_directories(${PROJECT_SOURCE_DIR}/fastdeploy/backends/tensorrt/common) + include_directories(${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/backends/tensorrt/common) list(APPEND ALL_DEPLOY_SRCS ${DEPLOY_TRT_SRCS}) find_library(TRT_INFER_LIB nvinfer ${TRT_DIRECTORY}/lib) find_library(TRT_ONNX_LIB nvonnxparser ${TRT_DIRECTORY}/lib) @@ -125,12 +138,16 @@ if(ENABLE_TRT_BACKEND) find_library(TRT_PLUGIN_LIB nvinfer_plugin ${TRT_DIRECTORY}/lib) list(APPEND DEPEND_LIBS ${TRT_INFER_LIB} ${TRT_ONNX_LIB} ${TRT_CAFFE_LIB} ${TRT_PLUGIN_LIB}) - # copy tensorrt libraries to third lib -# if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/third_libs/install/tensorrt") -# file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/third_libs/install/tensorrt/lib") -# endif() -# file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/third_libs/install/tensorrt/lib") -# file(COPY ${TRT_INFER_LIB} ${TRT_ONNX_LIB} ${TRT_CAFFE_LIB} ${TRT_PLUGIN_LIB} DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/third_libs/install/tensorrt/lib" FOLLOW_SYMLINK_CHAIN) + if(NOT EXISTS "${CMAKE_CURRENT_BINARY_DIR}/third_libs/install/tensorrt") + file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/third_libs/install/tensorrt") + endif() + if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/third_libs/install/tensorrt/lib") + file(REMOVE_RECURSE "${CMAKE_CURRENT_BINARY_DIR}/third_libs/install/tensorrt/lib") + endif() + find_package(Python COMPONENTS Interpreter Development REQUIRED) + message(STATUS "Copying ${TRT_DIRECTORY}/lib to ${CMAKE_CURRENT_BINARY_DIR}/third_libs/install/tensorrt/lib ...") + execute_process(COMMAND ${Python_EXECUTABLE} ${PROJECT_SOURCE_DIR}/copy_directory.py ${TRT_DIRECTORY}/lib ${CMAKE_CURRENT_BINARY_DIR}/third_libs/install/tensorrt/lib) + endif() if(ENABLE_VISION) @@ -157,37 +174,37 @@ else() endif() endif() -configure_file(${PROJECT_SOURCE_DIR}/fastdeploy/core/config.h.in ${PROJECT_SOURCE_DIR}/fastdeploy/core/config.h) +configure_file(${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/core/config.h.in ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/core/config.h) configure_file(${PROJECT_SOURCE_DIR}/FastDeploy.cmake.in ${PROJECT_SOURCE_DIR}/FastDeploy.cmake @ONLY) list(REMOVE_ITEM ALL_DEPLOY_SRCS ${DEPLOY_PYBIND_SRCS}) -add_library(fastdeploy SHARED ${ALL_DEPLOY_SRCS}) -redefine_file_macro(fastdeploy) -set_target_properties(fastdeploy PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") +add_library(${LIBRARY_NAME} SHARED ${ALL_DEPLOY_SRCS}) +redefine_file_macro(${LIBRARY_NAME}) +set_target_properties(${LIBRARY_NAME} PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") if(NOT APPLE) - set_target_properties(fastdeploy PROPERTIES LINK_FLAGS "-Wl,--start-group,--exclude-libs,ALL") + set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS "-Wl,--start-group,--exclude-libs,ALL") endif() -set_target_properties(fastdeploy PROPERTIES LINK_FLAGS_RELEASE -s) +set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS_RELEASE -s) file(READ "${PROJECT_SOURCE_DIR}/VERSION_NUMBER" FASTDEPLOY_VERSION) string(STRIP "${FASTDEPLOY_VERSION}" FASTDEPLOY_VERSION) if (APPLE) # set_target_properties(fastdeploy PROPERTIES LINK_FLAGS "-undefined dynamic_lookup") - set_target_properties(fastdeploy PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") + set_target_properties(${LIBRARY_NAME} PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") elseif(MSVC) else() - set_target_properties(fastdeploy PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") - set_target_properties(fastdeploy PROPERTIES LINK_FLAGS "-Wl,--exclude-libs,ALL") - set_target_properties(fastdeploy PROPERTIES LINK_FLAGS_RELEASE -s) + set_target_properties(${LIBRARY_NAME} PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") + set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS "-Wl,--exclude-libs,ALL") + set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS_RELEASE -s) endif() find_package(OpenMP) if(OpenMP_CXX_FOUND) list(APPEND DEPEND_LIBS OpenMP::OpenMP_CXX) endif() -set_target_properties(fastdeploy PROPERTIES VERSION ${FASTDEPLOY_VERSION}) -target_link_libraries(fastdeploy ${DEPEND_LIBS}) +set_target_properties(${LIBRARY_NAME} PROPERTIES VERSION ${FASTDEPLOY_VERSION}) +target_link_libraries(${LIBRARY_NAME} ${DEPEND_LIBS}) # add examples after prepare include paths for third-parties if (WITH_VISION_EXAMPLES AND EXISTS ${PROJECT_SOURCE_DIR}/examples) @@ -200,15 +217,15 @@ include(external/summary.cmake) fastdeploy_summary() install( - TARGETS fastdeploy + TARGETS ${LIBRARY_NAME} LIBRARY DESTINATION lib ) install( - DIRECTORY ${PROJECT_SOURCE_DIR}/fastdeploy + DIRECTORY ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy DESTINATION ${CMAKE_INSTALL_PREFIX}/include FILES_MATCHING PATTERN "*.h" - PATTERN "${PROJECT_SOURCE_DIR}/fastdeploy/backends/*/*.h" + PATTERN "${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/backends/*/*.h" ) install( DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/third_libs/install @@ -243,40 +260,34 @@ if(BUILD_FASTDEPLOY_PYTHON) endif() if(NOT ENABLE_VISION) - file(GLOB_RECURSE VISION_PYBIND_SRCS ${PROJECT_SOURCE_DIR}/fastdeploy/vision/*_pybind.cc) + file(GLOB_RECURSE VISION_PYBIND_SRCS ${PROJECT_SOURCE_DIR}/${CSRCS_DIR_NAME}/fastdeploy/vision/*_pybind.cc) list(REMOVE_ITEM DEPLOY_PYBIND_SRCS ${VISION_PYBIND_SRCS}) endif() - add_library(fastdeploy_main MODULE ${DEPLOY_PYBIND_SRCS}) - redefine_file_macro(fastdeploy_main) - set_target_properties(fastdeploy_main PROPERTIES PREFIX "") - set_target_properties(fastdeploy_main + add_library(${PY_LIBRARY_NAME} MODULE ${DEPLOY_PYBIND_SRCS}) + redefine_file_macro(${PY_LIBRARY_NAME}) + set_target_properties(${PY_LIBRARY_NAME} PROPERTIES PREFIX "") + set_target_properties(${PY_LIBRARY_NAME} PROPERTIES COMPILE_FLAGS "-fvisibility=hidden") - set_target_properties(fastdeploy_main PROPERTIES SUFFIX ${PY_EXT_SUFFIX}) - set_target_properties(fastdeploy_main + set_target_properties(${PY_LIBRARY_NAME} PROPERTIES SUFFIX ${PY_EXT_SUFFIX}) + set_target_properties(${PY_LIBRARY_NAME} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) - target_include_directories(fastdeploy_main PRIVATE + target_include_directories(${PY_LIBRARY_NAME} PRIVATE $ $ ${PYTHON_INCLUDE_DIR}) - target_include_directories(fastdeploy_main PUBLIC ${PROJECT_SOURCE_DIR}/third_party/pybind11/include) + target_include_directories(${PY_LIBRARY_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/third_party/pybind11/include) if(APPLE) - set_target_properties(fastdeploy_main + set_target_properties(${PY_LIBRARY_NAME} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup") endif() - if(APPLE) - target_link_libraries(fastdeploy_main PUBLIC fastdeploy) - elseif(WIN32) - target_link_libraries(fastdeploy_main PUBLIC fastdeploy) - else() - target_link_libraries(fastdeploy_main PUBLIC fastdeploy) - endif() + target_link_libraries(${PY_LIBRARY_NAME} PUBLIC ${LIBRARY_NAME}) if(MSVC) - target_link_libraries(fastdeploy_main PRIVATE ${PYTHON_LIBRARIES}) - target_compile_options(fastdeploy_main + target_link_libraries(${PY_LIBRARY_NAME} PRIVATE ${PYTHON_LIBRARIES}) + target_compile_options(${PY_LIBRARY_NAME} PRIVATE /MP /wd4244 # 'argument': conversion from 'google:: # protobuf::uint64' to 'int', possible @@ -285,7 +296,7 @@ if(BUILD_FASTDEPLOY_PYTHON) # possible loss of data /wd4996 # The second parameter is ignored. ${EXTRA_FLAGS}) - target_compile_options(fastdeploy_main PRIVATE $<$>:/MT> $<$:/MTd>) + target_compile_options(${PY_LIBRARY_NAME} PRIVATE $<$>:/MT> $<$:/MTd>) endif() endif(BUILD_FASTDEPLOY_PYTHON) diff --git a/FastDeploy.cmake.in b/FastDeploy.cmake.in index e8c0bb3d5..ccf2a574b 100644 --- a/FastDeploy.cmake.in +++ b/FastDeploy.cmake.in @@ -8,6 +8,7 @@ set(ENABLE_TRT_BACKEND @ENABLE_TRT_BACKEND@) set(ENABLE_PADDLE_FRONTEND @ENABLE_PADDLE_FRONTEND@) set(ENABLE_VISION @ENABLE_VISION@) set(ENABLE_OPENCV_CUDA @ENABLE_OPENCV_CUDA@) +set(LIBRARY_NAME @LIBRARY_NAME@) set(FASTDEPLOY_LIBS "") set(FASTDEPLOY_INCS "") @@ -17,7 +18,7 @@ if(NOT CMAKE_CXX_STANDARD) set(CMAKE_CXX_STANDARD 11) endif() -find_library(FDLIB fastdeploy ${CMAKE_CURRENT_LIST_DIR}/lib) +find_library(FDLIB ${LIBRARY_NAME} ${CMAKE_CURRENT_LIST_DIR}/lib) list(APPEND FASTDEPLOY_LIBS ${FDLIB}) if(ENABLE_ORT_BACKEND) @@ -51,13 +52,17 @@ if(WITH_GPU) list(APPEND FASTDEPLOY_LIBS ${CUDA_LIB}) if (ENABLE_TRT_BACKEND) - if (NOT TRT_DIRECTORY) - message(FATAL_ERROR "[FastDeploy] Please define TRT_DIRECTORY, e.g -DTRT_DIRECTORY=/usr/downloads/TensorRT-8.4.1.0") - endif() - find_library(TRT_INFER_LIB nvinfer ${TRT_DIRECTORY}/lib) - find_library(TRT_ONNX_LIB nvonnxparser ${TRT_DIRECTORY}/lib) - find_library(TRT_CAFFE_LIB nvcaffe_parser ${TRT_DIRECTORY}/lib) - find_library(TRT_PLUGIN_LIB nvinfer_plugin ${TRT_DIRECTORY}/lib) +# if (NOT TRT_DIRECTORY) +# message(FATAL_ERROR "[FastDeploy] Please define TRT_DIRECTORY, e.g -DTRT_DIRECTORY=/usr/downloads/TensorRT-8.4.1.0") +# endif() +# find_library(TRT_INFER_LIB nvinfer ${TRT_DIRECTORY}/lib) +# find_library(TRT_ONNX_LIB nvonnxparser ${TRT_DIRECTORY}/lib) +# find_library(TRT_CAFFE_LIB nvcaffe_parser ${TRT_DIRECTORY}/lib) +# find_library(TRT_PLUGIN_LIB nvinfer_plugin ${TRT_DIRECTORY}/lib) + find_library(TRT_INFER_LIB nvinfer ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tensorrt/lib) + find_library(TRT_ONNX_LIB nvonnxparser ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tensorrt/lib) + find_library(TRT_CAFFE_LIB nvcaffe_parser ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tensorrt/lib) + find_library(TRT_PLUGIN_LIB nvinfer_plugin ${CMAKE_CURRENT_LIST_DIR}/third_libs/install/tensorrt/lib) list(APPEND FASTDEPLOY_LIBS ${TRT_INFER_LIB} ${TRT_ONNX_LIB} ${TRT_CAFFE_LIB} ${TRT_PLUGIN_LIB}) endif() endif() diff --git a/copy_directory.py b/copy_directory.py new file mode 100644 index 000000000..f0313db3c --- /dev/null +++ b/copy_directory.py @@ -0,0 +1,32 @@ +# Copyright (c) 2020 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. + +import shutil +import os +import sys + + +def copy_directory(src, dst): + if os.path.exists(dst): + raise Exception("Destination {} is already exist.".format(dst)) + if not os.path.exists(src): + raise Exception("Source {} is not exist.".format(src)) + try: + shutil.copytree(src, dst, symlinks=True) + except: + raise Exception("Copy {} to {} failed.".format(src, dst)) + + +if __name__ == "__main__": + copy_directory(sys.argv[1], sys.argv[2]) diff --git a/fastdeploy/backends/ort/ort_backend.cc b/fastdeploy/backends/ort/ort_backend.cc index 909b5f287..f5d0bfdd9 100644 --- a/fastdeploy/backends/ort/ort_backend.cc +++ b/fastdeploy/backends/ort/ort_backend.cc @@ -81,10 +81,10 @@ void OrtBackend::BuildOption(const OrtBackendOption& option) { } } if (!support_cuda) { - FDLogger() << "[WARN] Compiled fastdeploy with onnxruntime doesn't " - "support GPU, the available providers are " - << providers_msg << "will fallback to CPUExecutionProvider." - << std::endl; + FDWARNING << "Compiled fastdeploy with onnxruntime doesn't " + "support GPU, the available providers are " + << providers_msg << "will fallback to CPUExecutionProvider." + << std::endl; option_.use_gpu = false; } else { FDASSERT(option.gpu_id == 0, "Requires gpu_id == 0, but now gpu_id = " + diff --git a/fastdeploy/backends/tensorrt/trt_backend.cc b/fastdeploy/backends/tensorrt/trt_backend.cc index a29af6e9a..d050cc9f2 100644 --- a/fastdeploy/backends/tensorrt/trt_backend.cc +++ b/fastdeploy/backends/tensorrt/trt_backend.cc @@ -52,6 +52,61 @@ std::vector toVec(const nvinfer1::Dims& dim) { return out; } +bool CheckDynamicShapeConfig(const paddle2onnx::OnnxReader& reader, + const TrtBackendOption& option) { + paddle2onnx::ModelTensorInfo inputs[reader.NumInputs()]; + std::string input_shapes[reader.NumInputs()]; + for (int i = 0; i < reader.NumInputs(); ++i) { + reader.GetInputInfo(i, &inputs[i]); + + // change 0 to -1, when input_dim is a string, onnx will make it to zero + for (int j = 0; j < inputs[i].rank; ++j) { + if (inputs[i].shape[j] <= 0) { + inputs[i].shape[j] = -1; + } + } + + input_shapes[i] = ""; + for (int j = 0; j < inputs[i].rank; ++j) { + if (j != inputs[i].rank - 1) { + input_shapes[i] += (std::to_string(inputs[i].shape[j]) + ", "); + } else { + input_shapes[i] += std::to_string(inputs[i].shape[j]); + } + } + } + + bool all_check_passed = true; + for (int i = 0; i < reader.NumInputs(); ++i) { + bool contain_unknown_dim = false; + for (int j = 0; j < inputs[i].rank; ++j) { + if (inputs[i].shape[j] < 0) { + contain_unknown_dim = true; + } + } + + std::string name(inputs[i].name, strlen(inputs[i].name)); + FDINFO << "The loaded model's input tensor:" << name + << " has shape [" + input_shapes[i] << "]." << std::endl; + if (contain_unknown_dim) { + auto iter1 = option.min_shape.find(name); + auto iter2 = option.max_shape.find(name); + auto iter3 = option.opt_shape.find(name); + if (iter1 == option.min_shape.end() || iter2 == option.max_shape.end() || + iter3 == option.opt_shape.end()) { + FDERROR << "The loaded model's input tensor:" << name + << " has dynamic shape [" + input_shapes[i] + + "], but didn't configure it's shape for tensorrt with " + "SetTrtInputShape correctly." + << std::endl; + all_check_passed = false; + } + } + } + + return all_check_passed; +} + bool TrtBackend::InitFromTrt(const std::string& trt_engine_file, const TrtBackendOption& option) { if (initialized_) { @@ -167,13 +222,17 @@ bool TrtBackend::InitFromOnnx(const std::string& model_file, onnx_reader.output_names[i] + strlen(onnx_reader.output_names[i])); outputs_order_[name] = i; } + if (!CheckDynamicShapeConfig(onnx_reader, option)) { + FDERROR << "TrtBackend::CheckDynamicShapeConfig failed." << std::endl; + return false; + } if (option.serialize_file != "") { std::ifstream fin(option.serialize_file, std::ios::binary | std::ios::in); if (fin) { - FDLogger() << "Detect serialized TensorRT Engine file in " - << option.serialize_file << ", will load it directly." - << std::endl; + FDINFO << "Detect serialized TensorRT Engine file in " + << option.serialize_file << ", will load it directly." + << std::endl; fin.close(); return InitFromTrt(option.serialize_file); } @@ -311,9 +370,9 @@ bool TrtBackend::CreateTrtEngine(const std::string& onnx_model, if (option.enable_fp16) { if (!builder->platformHasFastFp16()) { - FDLogger() << "[WARN] Detected FP16 is not supported in the current GPU, " - "will use FP32 instead." - << std::endl; + FDWARNING << "Detected FP16 is not supported in the current GPU, " + "will use FP32 instead." + << std::endl; } else { config->setFlag(nvinfer1::BuilderFlag::kFP16); } @@ -330,33 +389,13 @@ bool TrtBackend::CreateTrtEngine(const std::string& onnx_model, return false; } - FDLogger() << "Start to building TensorRT Engine..." << std::endl; + FDINFO << "Start to building TensorRT Engine..." << std::endl; bool fp16 = builder->platformHasFastFp16(); builder->setMaxBatchSize(option.max_batch_size); config->setMaxWorkspaceSize(option.max_workspace_size); - if (option.fixed_shape.size() > 0) { - auto profile = builder->createOptimizationProfile(); - for (auto& item : option.fixed_shape) { - FDASSERT(profile->setDimensions(item.first.c_str(), - nvinfer1::OptProfileSelector::kMIN, - sample::toDims(item.second)), - "[TrtBackend] Failed to set min_shape for input: " + item.first + - " in TrtBackend."); - FDASSERT(profile->setDimensions(item.first.c_str(), - nvinfer1::OptProfileSelector::kOPT, - sample::toDims(item.second)), - "[TrtBackend] Failed to set min_shape for input: " + item.first + - " in TrtBackend."); - FDASSERT(profile->setDimensions(item.first.c_str(), - nvinfer1::OptProfileSelector::kMAX, - sample::toDims(item.second)), - "[TrtBackend] Failed to set min_shape for input: " + item.first + - " in TrtBackend."); - } - config->addOptimizationProfile(profile); - } else if (option.max_shape.size() > 0) { + if (option.max_shape.size() > 0) { auto profile = builder->createOptimizationProfile(); FDASSERT(option.max_shape.size() == option.min_shape.size() && option.min_shape.size() == option.opt_shape.size(), @@ -416,10 +455,10 @@ bool TrtBackend::CreateTrtEngine(const std::string& onnx_model, return false; } - FDLogger() << "TensorRT Engine is built succussfully." << std::endl; + FDINFO << "TensorRT Engine is built succussfully." << std::endl; if (option.serialize_file != "") { - FDLogger() << "Serialize TensorRTEngine to local file " - << option.serialize_file << "." << std::endl; + FDINFO << "Serialize TensorRTEngine to local file " << option.serialize_file + << "." << std::endl; std::ofstream engine_file(option.serialize_file.c_str()); if (!engine_file) { FDERROR << "Failed to open " << option.serialize_file << " to write." @@ -428,11 +467,11 @@ bool TrtBackend::CreateTrtEngine(const std::string& onnx_model, } engine_file.write(static_cast(plan->data()), plan->size()); engine_file.close(); - FDLogger() << "TensorRTEngine is serialized to local file " - << option.serialize_file - << ", we can load this model from the seralized engine " - "directly next time." - << std::endl; + FDINFO << "TensorRTEngine is serialized to local file " + << option.serialize_file + << ", we can load this model from the seralized engine " + "directly next time." + << std::endl; } return true; } diff --git a/fastdeploy/backends/tensorrt/trt_backend.h b/fastdeploy/backends/tensorrt/trt_backend.h index 1da7f1471..27e6e552b 100644 --- a/fastdeploy/backends/tensorrt/trt_backend.h +++ b/fastdeploy/backends/tensorrt/trt_backend.h @@ -46,7 +46,6 @@ struct TrtBackendOption { bool enable_int8 = false; size_t max_batch_size = 32; size_t max_workspace_size = 1 << 30; - std::map> fixed_shape; std::map> max_shape; std::map> min_shape; std::map> opt_shape; diff --git a/fastdeploy/fastdeploy_model.cc b/fastdeploy/fastdeploy_model.cc index c61eea7cb..e434e19fa 100644 --- a/fastdeploy/fastdeploy_model.cc +++ b/fastdeploy/fastdeploy_model.cc @@ -132,9 +132,9 @@ void FastDeployModel::EnableDebug() { #ifdef FASTDEPLOY_DEBUG debug_ = true; #else - FDLogger() << "The compile FastDeploy is not with -DENABLE_DEBUG=ON, so " - "cannot enable debug mode." - << std::endl; + FDWARNING << "The compile FastDeploy is not with -DENABLE_DEBUG=ON, so " + "cannot enable debug mode." + << std::endl; debug_ = false; #endif } diff --git a/fastdeploy/fastdeploy_runtime.cc b/fastdeploy/fastdeploy_runtime.cc index e353c6416..05af6e14e 100644 --- a/fastdeploy/fastdeploy_runtime.cc +++ b/fastdeploy/fastdeploy_runtime.cc @@ -77,23 +77,23 @@ bool CheckModelFormat(const std::string& model_file, if (model_format == Frontend::PADDLE) { if (model_file.size() < 8 || model_file.substr(model_file.size() - 8, 8) != ".pdmodel") { - FDLogger() << "With model format of Frontend::PADDLE, the model file " - "should ends with `.pdmodel`, but now it's " - << model_file << std::endl; + FDERROR << "With model format of Frontend::PADDLE, the model file " + "should ends with `.pdmodel`, but now it's " + << model_file << std::endl; return false; } } else if (model_format == Frontend::ONNX) { if (model_file.size() < 5 || model_file.substr(model_file.size() - 5, 5) != ".onnx") { - FDLogger() << "With model format of Frontend::ONNX, the model file " - "should ends with `.onnx`, but now it's " - << model_file << std::endl; + FDERROR << "With model format of Frontend::ONNX, the model file " + "should ends with `.onnx`, but now it's " + << model_file << std::endl; return false; } } else { - FDLogger() << "Only support model format with frontend Frontend::PADDLE / " - "Frontend::ONNX." - << std::endl; + FDERROR << "Only support model format with frontend Frontend::PADDLE / " + "Frontend::ONNX." + << std::endl; return false; } return true; @@ -116,6 +116,101 @@ Frontend GuessModelFormat(const std::string& model_file) { return Frontend::PADDLE; } +void RuntimeOption::SetModelPath(const std::string& model_path, + const std::string& params_path, + const std::string& _model_format) { + if (_model_format == "paddle") { + model_file = model_path; + params_file = params_path; + model_format = Frontend::PADDLE; + } else if (_model_format == "onnx") { + model_file = model_path; + model_format = Frontend::ONNX; + } else { + FDASSERT(false, "The model format only can be 'paddle' or 'onnx'."); + } +} + +void RuntimeOption::UseGpu(int gpu_id) { +#ifdef WITH_GPU + device = Device::GPU; + device_id = gpu_id; +#else + FDWARNING << "The FastDeploy didn't compile with GPU, will force to use CPU." + << std::endl; + device = Device::CPU; +#endif +} + +void RuntimeOption::UseCpu() { device = Device::CPU; } + +void RuntimeOption::SetCpuThreadNum(int thread_num) { + FDASSERT(thread_num > 0, "The thread_num must be greater than 0."); + cpu_thread_num = thread_num; +} + +// use paddle inference backend +void RuntimeOption::UsePaddleBackend() { +#ifdef ENABLE_PADDLE_BACKEND + backend = Backend::PDINFER; +#else + FDASSERT(false, "The FastDeploy didn't compile with Paddle Inference."); +#endif +} + +// use onnxruntime backend +void RuntimeOption::UseOrtBackend() { +#ifdef ENABLE_ORT_BACKEND + backend = Backend::ORT; +#else + FDASSERT(false, "The FastDeploy didn't compile with OrtBackend."); +#endif +} + +void RuntimeOption::UseTrtBackend() { +#ifdef ENABLE_TRT_BACKEND + backend = Backend::TRT; +#else + FDASSERT(false, "The FastDeploy didn't compile with TrtBackend."); +#endif +} + +void RuntimeOption::EnablePaddleMKLDNN() { pd_enable_mkldnn = true; } + +void RuntimeOption::DisablePaddleMKLDNN() { pd_enable_mkldnn = false; } + +void RuntimeOption::SetPaddleMKLDNNCacheSize(int size) { + FDASSERT(size > 0, "Parameter size must greater than 0."); + pd_mkldnn_cache_size = size; +} + +void RuntimeOption::SetTrtInputShape(const std::string& input_name, + const std::vector& min_shape, + const std::vector& opt_shape, + const std::vector& max_shape) { + trt_min_shape[input_name].clear(); + trt_max_shape[input_name].clear(); + trt_opt_shape[input_name].clear(); + trt_min_shape[input_name].assign(min_shape.begin(), min_shape.end()); + if (opt_shape.size() == 0) { + trt_opt_shape[input_name].assign(min_shape.begin(), min_shape.end()); + } else { + trt_opt_shape[input_name].assign(opt_shape.begin(), opt_shape.end()); + } + if (max_shape.size() == 0) { + trt_max_shape[input_name].assign(min_shape.begin(), min_shape.end()); + } else { + trt_max_shape[input_name].assign(max_shape.begin(), max_shape.end()); + } + FDINFO << trt_min_shape[input_name].size() << " " + << trt_opt_shape[input_name].size() << " " + << trt_max_shape[input_name].size() << std::endl; +} + +void RuntimeOption::EnableTrtFP16() { trt_enable_fp16 = true; } + +void RuntimeOption::DisableTrtFP16() { trt_enable_fp16 = false; } + bool Runtime::Init(const RuntimeOption& _option) { option = _option; if (option.model_format == Frontend::AUTOREC) { @@ -229,7 +324,6 @@ void Runtime::CreateTrtBackend() { trt_option.enable_int8 = option.trt_enable_int8; trt_option.max_batch_size = option.trt_max_batch_size; trt_option.max_workspace_size = option.trt_max_workspace_size; - trt_option.fixed_shape = option.trt_fixed_shape; trt_option.max_shape = option.trt_max_shape; trt_option.min_shape = option.trt_min_shape; trt_option.opt_shape = option.trt_opt_shape; diff --git a/fastdeploy/fastdeploy_runtime.h b/fastdeploy/fastdeploy_runtime.h index 7ec08e9d9..d0f01069f 100644 --- a/fastdeploy/fastdeploy_runtime.h +++ b/fastdeploy/fastdeploy_runtime.h @@ -36,8 +36,58 @@ bool CheckModelFormat(const std::string& model_file, Frontend GuessModelFormat(const std::string& model_file); struct FASTDEPLOY_DECL RuntimeOption { - Backend backend = Backend::UNKNOWN; + // set path of model file and params file + // for onnx, only need to define model_file, but also need to + // define model_format + // model_format support 'paddle' / 'onnx' now. + void SetModelPath(const std::string& model_path, + const std::string& params_path = "", + const std::string& _model_format = "paddle"); + // set model inference in GPU + void UseCpu(); + + // set model inference in CPU + void UseGpu(int gpu_id = 0); + + // set number of thread while inference in CPU + void SetCpuThreadNum(int thread_num); + + // use paddle inference backend + void UsePaddleBackend(); + + // use onnxruntime backend + void UseOrtBackend(); + + // use tensorrt backend + void UseTrtBackend(); + + // enable mkldnn while use paddle inference in CPU + void EnablePaddleMKLDNN(); + // disable mkldnn while use paddle inference in CPU + void DisablePaddleMKLDNN(); + + // set size of cached shape while enable mkldnn with paddle inference backend + void SetPaddleMKLDNNCacheSize(int size); + + // set tensorrt shape while the inputs of model contain dynamic shape + // min_shape: the minimum shape + // opt_shape: the most common shape while inference, default be empty + // max_shape: the maximum shape, default be empty + + // if opt_shape, max_shape are empty, they will keep same with the min_shape + // which means the shape will be fixed as min_shape while inference + void SetTrtInputShape( + const std::string& input_name, const std::vector& min_shape, + const std::vector& opt_shape = std::vector(), + const std::vector& max_shape = std::vector()); + + // enable half precision while use tensorrt backend + void EnableTrtFP16(); + // disable half precision, change to full precision(float32) + void DisableTrtFP16(); + + Backend backend = Backend::UNKNOWN; // for cpu inference and preprocess int cpu_thread_num = 8; int device_id = 0; @@ -62,7 +112,6 @@ struct FASTDEPLOY_DECL RuntimeOption { int pd_mkldnn_cache_size = 1; // ======Only for Trt Backend======= - std::map> trt_fixed_shape; std::map> trt_max_shape; std::map> trt_min_shape; std::map> trt_opt_shape; diff --git a/fastdeploy/fastdeploy_runtime.py b/fastdeploy/fastdeploy_runtime.py index 592d1d295..b23879b36 100644 --- a/fastdeploy/fastdeploy_runtime.py +++ b/fastdeploy/fastdeploy_runtime.py @@ -55,27 +55,33 @@ class FastDeployModel: return self._model.initialized() -class FastDeployRuntime: +class Runtime: def __init__(self, runtime_option): - self._runtime = C.Runtime(); - assert self._runtime.init(runtime_option), "Initialize FastDeployRuntime Failed!" + self._runtime = C.Runtime() + assert self._runtime.init(runtime_option), "Initialize Runtime Failed!" def infer(self, data): assert isinstance(data, dict), "The input data should be type of dict." return self._runtime.infer(data) def num_inputs(self): - return self._runtime.num_inputs(); + return self._runtime.num_inputs() def num_outputs(self): - return self._runtime.num_outputs(); + return self._runtime.num_outputs() def get_input_info(self, index): - assert isinstance(index, int), "The input parameter index should be type of int." - assert index < self.num_inputs(), "The input parameter index:{} should less than number of inputs:{}.".format(index, self.num_inputs) + assert isinstance( + index, int), "The input parameter index should be type of int." + assert index < self.num_inputs( + ), "The input parameter index:{} should less than number of inputs:{}.".format( + index, self.num_inputs) return self._runtime.get_input_info(index) def get_output_info(self, index): - assert isinstance(index, int), "The input parameter index should be type of int." - assert index < self.num_outputs(), "The input parameter index:{} should less than number of outputs:{}.".format(index, self.num_outputs) + assert isinstance( + index, int), "The input parameter index should be type of int." + assert index < self.num_outputs( + ), "The input parameter index:{} should less than number of outputs:{}.".format( + index, self.num_outputs) return self._runtime.get_output_info(index) diff --git a/fastdeploy/pybind/fastdeploy_runtime.cc b/fastdeploy/pybind/fastdeploy_runtime.cc index e3c6dd19a..5f27509ca 100644 --- a/fastdeploy/pybind/fastdeploy_runtime.cc +++ b/fastdeploy/pybind/fastdeploy_runtime.cc @@ -19,6 +19,20 @@ namespace fastdeploy { void BindRuntime(pybind11::module& m) { pybind11::class_(m, "RuntimeOption") .def(pybind11::init()) + .def("set_model_path", &RuntimeOption::SetModelPath) + .def("use_gpu", &RuntimeOption::UseGpu) + .def("use_cpu", &RuntimeOption::UseCpu) + .def("set_cpu_thread_num", &RuntimeOption::SetCpuThreadNum) + .def("use_paddle_backend", &RuntimeOption::UsePaddleBackend) + .def("use_ort_backend", &RuntimeOption::UseOrtBackend) + .def("use_trt_backend", &RuntimeOption::UseTrtBackend) + .def("enable_paddle_mkldnn", &RuntimeOption::EnablePaddleMKLDNN) + .def("disable_paddle_mkldnn", &RuntimeOption::DisablePaddleMKLDNN) + .def("set_paddle_mkldnn_cache_size", + &RuntimeOption::SetPaddleMKLDNNCacheSize) + .def("set_trt_input_shape", &RuntimeOption::SetTrtInputShape) + .def("enable_trt_fp16", &RuntimeOption::EnableTrtFP16) + .def("disable_trt_fp16", &RuntimeOption::DisableTrtFP16) .def_readwrite("model_file", &RuntimeOption::model_file) .def_readwrite("params_file", &RuntimeOption::params_file) .def_readwrite("model_format", &RuntimeOption::model_format) @@ -30,7 +44,6 @@ void BindRuntime(pybind11::module& m) { .def_readwrite("ort_inter_op_num_threads", &RuntimeOption::ort_inter_op_num_threads) .def_readwrite("ort_execution_mode", &RuntimeOption::ort_execution_mode) - .def_readwrite("trt_fixed_shape", &RuntimeOption::trt_fixed_shape) .def_readwrite("trt_max_shape", &RuntimeOption::trt_max_shape) .def_readwrite("trt_opt_shape", &RuntimeOption::trt_opt_shape) .def_readwrite("trt_min_shape", &RuntimeOption::trt_min_shape) @@ -49,41 +62,43 @@ void BindRuntime(pybind11::module& m) { pybind11::class_(m, "Runtime") .def(pybind11::init()) .def("init", &Runtime::Init) - .def("infer", [](Runtime& self, - std::map& data) { - std::vector inputs(data.size()); - int index = 0; - for (auto iter = data.begin(); iter != data.end(); ++iter) { - inputs[index].dtype = NumpyDataTypeToFDDataType(iter->second.dtype()); - inputs[index].shape.insert( - inputs[index].shape.begin(), iter->second.shape(), - iter->second.shape() + iter->second.ndim()); - // TODO(jiangjiajun) Maybe skip memory copy is a better choice - // use SetExternalData - inputs[index].data.resize(iter->second.nbytes()); - memcpy(inputs[index].data.data(), iter->second.mutable_data(), - iter->second.nbytes()); - inputs[index].name = iter->first; - } + .def("infer", + [](Runtime& self, std::map& data) { + std::vector inputs(data.size()); + int index = 0; + for (auto iter = data.begin(); iter != data.end(); ++iter) { + inputs[index].dtype = + NumpyDataTypeToFDDataType(iter->second.dtype()); + inputs[index].shape.insert( + inputs[index].shape.begin(), iter->second.shape(), + iter->second.shape() + iter->second.ndim()); + // TODO(jiangjiajun) Maybe skip memory copy is a better choice + // use SetExternalData + inputs[index].data.resize(iter->second.nbytes()); + memcpy(inputs[index].data.data(), iter->second.mutable_data(), + iter->second.nbytes()); + inputs[index].name = iter->first; + } - std::vector outputs(self.NumOutputs()); - self.Infer(inputs, &outputs); + std::vector outputs(self.NumOutputs()); + self.Infer(inputs, &outputs); - std::vector results; - results.reserve(outputs.size()); - for (size_t i = 0; i < outputs.size(); ++i) { - auto numpy_dtype = FDDataTypeToNumpyDataType(outputs[i].dtype); - results.emplace_back(pybind11::array(numpy_dtype, outputs[i].shape)); - memcpy(results[i].mutable_data(), outputs[i].data.data(), - outputs[i].Numel() * FDDataTypeSize(outputs[i].dtype)); - } - return results; - }) - .def("num_inputs", &Runtime::NumInputs) - .def("num_outputs", &Runtime::NumOutputs) - .def("get_input_info", &Runtime::GetInputInfo) - .def("get_output_info", &Runtime::GetOutputInfo) - .def_readonly("option", &Runtime::option); + std::vector results; + results.reserve(outputs.size()); + for (size_t i = 0; i < outputs.size(); ++i) { + auto numpy_dtype = FDDataTypeToNumpyDataType(outputs[i].dtype); + results.emplace_back( + pybind11::array(numpy_dtype, outputs[i].shape)); + memcpy(results[i].mutable_data(), outputs[i].data.data(), + outputs[i].Numel() * FDDataTypeSize(outputs[i].dtype)); + } + return results; + }) + .def("num_inputs", &Runtime::NumInputs) + .def("num_outputs", &Runtime::NumOutputs) + .def("get_input_info", &Runtime::GetInputInfo) + .def("get_output_info", &Runtime::GetOutputInfo) + .def_readonly("option", &Runtime::option); pybind11::enum_(m, "Backend", pybind11::arithmetic(), "Backend for inference.") diff --git a/fastdeploy/utils/utils.cc b/fastdeploy/utils/utils.cc index e4e5d1472..dfe5326d1 100644 --- a/fastdeploy/utils/utils.cc +++ b/fastdeploy/utils/utils.cc @@ -31,4 +31,4 @@ FDLogger& FDLogger::operator<<(std::ostream& (*os)(std::ostream&)) { return *this; } -} // namespace fastdeploy +} // namespace fastdeploy diff --git a/fastdeploy/utils/utils.h b/fastdeploy/utils/utils.h index b57a27f80..f427cd7a3 100644 --- a/fastdeploy/utils/utils.h +++ b/fastdeploy/utils/utils.h @@ -69,13 +69,17 @@ class FASTDEPLOY_DECL FDLogger { #define __REL_FILE__ __FILE__ #endif -#define FDERROR \ - FDLogger(true, "[ERROR]") \ +#define FDERROR \ + FDLogger(true, "[ERROR]") \ << __REL_FILE__ << "(" << __LINE__ << ")::" << __FUNCTION__ << "\t" -#define FDERROR \ - FDLogger(true, "[ERROR]") << __REL_FILE__ << "(" << __LINE__ \ - << ")::" << __FUNCTION__ << "\t" +#define FDWARNING \ + FDLogger(true, "[WARNING]") \ + << __REL_FILE__ << "(" << __LINE__ << ")::" << __FUNCTION__ << "\t" + +#define FDINFO \ + FDLogger(true, "[INFO]") << __REL_FILE__ << "(" << __LINE__ \ + << ")::" << __FUNCTION__ << "\t" #define FDASSERT(condition, message) \ if (!(condition)) { \ diff --git a/fastdeploy/vision/common/processors/cast.cc b/fastdeploy/vision/common/processors/cast.cc index 77a1b249a..b9a757f14 100644 --- a/fastdeploy/vision/common/processors/cast.cc +++ b/fastdeploy/vision/common/processors/cast.cc @@ -29,9 +29,8 @@ bool Cast::CpuRun(Mat* mat) { im->convertTo(*im, CV_64FC(c)); } } else { - FDLogger() << "[WARN] Cast not support for " << dtype_ - << " now! will skip this operation." - << std::endl; + FDWARNING << "Cast not support for " << dtype_ + << " now! will skip this operation." << std::endl; } return true; } @@ -49,9 +48,8 @@ bool Cast::GpuRun(Mat* mat) { im->convertTo(*im, CV_64FC(c)); } } else { - FDLogger() << "[WARN] Cast not support for " << dtype_ - << " now! will skip this operation." - << std::endl; + FDWARNING << "Cast not support for " << dtype_ + << " now! will skip this operation." << std::endl; } return true; } @@ -62,5 +60,5 @@ bool Cast::Run(Mat* mat, const std::string& dtype, ProcLib lib) { return c(mat, lib); } -} // namespace vision -} // namespace fastdeploy +} // namespace vision +} // namespace fastdeploy diff --git a/model_zoo/vision/ppyoloe/cpp/CMakeLists.txt b/model_zoo/vision/ppyoloe/cpp/CMakeLists.txt index e68156651..42ae43610 100644 --- a/model_zoo/vision/ppyoloe/cpp/CMakeLists.txt +++ b/model_zoo/vision/ppyoloe/cpp/CMakeLists.txt @@ -5,7 +5,7 @@ CMAKE_MINIMUM_REQUIRED (VERSION 3.16) # add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0) # 指定下载解压后的fastdeploy库路径 -set(FASTDEPLOY_INSTALL_DIR ${PROJECT_SOURCE_DIR}/fastdeploy-linux-x64-0.3.0/) +set(FASTDEPLOY_INSTALL_DIR /fastdeploy/CustomOp/FastDeploy/build1/fastdeploy-linux-x64-gpu-0.3.0) include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) diff --git a/model_zoo/vision/ppyoloe/cpp/ppyoloe.cc b/model_zoo/vision/ppyoloe/cpp/ppyoloe.cc index 30765f075..e63f29e62 100644 --- a/model_zoo/vision/ppyoloe/cpp/ppyoloe.cc +++ b/model_zoo/vision/ppyoloe/cpp/ppyoloe.cc @@ -23,11 +23,7 @@ int main() { std::string img_path = "000000014439_640x640.jpg"; std::string vis_path = "vis.jpeg"; - auto option = fastdeploy::RuntimeOption(); - option.device = fastdeploy::Device::CPU; - option.backend = fastdeploy::Backend::PDINFER; - auto model = - vis::ppdet::PPYOLOE(model_file, params_file, config_file, option); + auto model = vis::ppdet::PPYOLOE(model_file, params_file, config_file); if (!model.Initialized()) { std::cerr << "Init Failed." << std::endl; return -1; diff --git a/model_zoo/vision/yolov5/cpp/CMakeLists.txt b/model_zoo/vision/yolov5/cpp/CMakeLists.txt index 13ddc9d21..c1f82a6fe 100644 --- a/model_zoo/vision/yolov5/cpp/CMakeLists.txt +++ b/model_zoo/vision/yolov5/cpp/CMakeLists.txt @@ -5,7 +5,8 @@ CMAKE_MINIMUM_REQUIRED (VERSION 3.16) # add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0) # 指定下载解压后的fastdeploy库路径 -set(FASTDEPLOY_INSTALL_DIR ${PROJECT_SOURCE_DIR}/fastdeploy-linux-x64-0.3.0/) +set(FASTDEPLOY_INSTALL_DIR /fastdeploy/CustomOp/FastDeploy/build1/fastdeploy-linux-x64-gpu-0.3.0) + include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) diff --git a/model_zoo/vision/yolox/cpp/CMakeLists.txt b/model_zoo/vision/yolox/cpp/CMakeLists.txt index fe9668f6a..67bf0f2da 100644 --- a/model_zoo/vision/yolox/cpp/CMakeLists.txt +++ b/model_zoo/vision/yolox/cpp/CMakeLists.txt @@ -5,7 +5,7 @@ CMAKE_MINIMUM_REQUIRED (VERSION 3.16) # add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0) # 指定下载解压后的fastdeploy库路径 -set(FASTDEPLOY_INSTALL_DIR ${PROJECT_SOURCE_DIR}/fastdeploy-linux-x64-0.0.3/) +set(FASTDEPLOY_INSTALL_DIR /fastdeploy/CustomOp/FastDeploy/build1/fastdeploy-linux-x64-gpu-0.3.0) include(${FASTDEPLOY_INSTALL_DIR}/FastDeploy.cmake) diff --git a/model_zoo/vision/yolox/yolox.py b/model_zoo/vision/yolox/yolox.py index 8fd1a8a02..b63675049 100644 --- a/model_zoo/vision/yolox/yolox.py +++ b/model_zoo/vision/yolox/yolox.py @@ -1,5 +1,5 @@ import fastdeploy as fd -import cv2 +import cv2 # 下载模型和测试图片 model_url = "https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_s.onnx" @@ -20,4 +20,3 @@ cv2.imwrite("vis_result.jpg", im) # 输出预测结果 print(result) -print(model.runtime_option) diff --git a/setup.py b/setup.py index 19c47ed9c..06e9b12be 100644 --- a/setup.py +++ b/setup.py @@ -126,7 +126,7 @@ class ONNXCommand(setuptools.Command): pass -def GetAllFiles(dirname): +def get_all_files(dirname): files = list() for root, dirs, filenames in os.walk(dirname): for f in filenames: @@ -353,23 +353,22 @@ if sys.argv[1] == "install" or sys.argv[1] == "bdist_wheel": if os.path.exists("fastdeploy/libs/third_libs"): shutil.rmtree("fastdeploy/libs/third_libs") - shutil.copytree( - ".setuptools-cmake-build/third_libs/install", - "fastdeploy/libs/third_libs", - symlinks=True) - - all_files = GetAllFiles("fastdeploy/libs") - for f in all_files: - package_data[PACKAGE_NAME].append(os.path.relpath(f, "fastdeploy")) +# shutil.copytree( +# ".setuptools-cmake-build/third_libs/install", +# "fastdeploy/libs/third_libs", +# symlinks=True) if platform.system().lower() == "linux": rpaths = ["${ORIGIN}"] - for root, dirs, files in os.walk("fastdeploy/libs/third_libs"): + for root, dirs, files in os.walk( + ".setuptools-cmake-build/third_libs/install"): for d in dirs: if d == "lib": path = os.path.relpath( - os.path.join(root, d), "fastdeploy/libs") - rpaths.append("${ORIGIN}/" + format(path)) + os.path.join(root, d), + ".setuptools-cmake-build/third_libs/install") + rpaths.append("${ORIGIN}/" + os.path.join( + "libs/third_libs", path)) rpaths = ":".join(rpaths) command = "patchelf --set-rpath '{}' ".format(rpaths) + os.path.join( "fastdeploy/libs", pybind_so_file) @@ -379,6 +378,12 @@ if sys.argv[1] == "install" or sys.argv[1] == "bdist_wheel": command) == 0, "patchelf {} failed, the command: {}".format( command, pybind_so_file) + all_files = get_all_files("fastdeploy/libs") + for f in all_files: + if f.count("third_libs") > 0: + continue + package_data[PACKAGE_NAME].append(os.path.relpath(f, "fastdeploy")) + setuptools.setup( name=PACKAGE_NAME, version=VersionInfo.version,