diff --git a/aiframe/CMakeLists.txt b/aiframe/CMakeLists.txt index 9f8022f59bd8d2c66926a107b42571caeb8bd2d0..3b851d34b03565b2ce327bd33c10b2dc7b87e804 100644 --- a/aiframe/CMakeLists.txt +++ b/aiframe/CMakeLists.txt @@ -4,18 +4,92 @@ project(ONNXRunner) set(CMAKE_CXX_STANDARD 17) -set(INC_DIR /usr/include) -set(INC_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/include) -set(LIB_DIR /usr/lib64) - -include_directories( - ${INC_HEADER} - ${INC_DIR}) - -link_directories(${LIB_DIR}) - -add_library(ONNXRunner SHARED ONNXRunner.cpp) - -target_link_libraries(ONNXRunner -PRIVATE -libcrypto.so) # libonnxruntime.so +#------------------------------------------------------------------------------- +# Dependency settings +#------------------------------------------------------------------------------- +# Set include file directory for AI4Compiler framework. +set(FRAMEWORK_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/include") + +# Set root directory for ONNXRuntime library. +set(DEFAULT_onnxruntime_ROOTDIR "/usr" CACHE FILEPATH + "Default root directory for ONNXRuntime") + +if(NOT DEFINED onnxruntime_ROOTDIR) + message(WARNING + "Set default root path to libonnxruntime as " + "${DEFAULT_onnxruntime_ROOTDIR}. " + "Use -Donnxruntime_ROOTDIR to change ONNXRuntime root path.") + set(onnxruntime_ROOTDIR ${DEFAULT_onnxruntime_ROOTDIR}) +endif() + +# Set directory for dependency libraries. +set(DEFAULT_LIB_DEP_DIR "/usr/lib64" CACHE FILEPATH + "Default directory for dependency libraries") + +# Search for dependency library libcrypto.so. +if(NOT DEFINED crypto_LIBDIR) + message("Set default path to search libcrypto as ${DEFAULT_LIB_DEP_DIR}. " + "Use -Dcrypto_LIBDIR to change lib path.") + set(crypto_LIBDIR ${DEFAULT_LIB_DEP_DIR}) +endif() + +find_library(LIBCRYPTO + NAMES crypto libcrypto + PATHS "${crypto_LIBDIR}") +if(NOT LIBCRYPTO) + message(FATAL_ERROR "libcrypto library is not found! ") +endif() + +add_library(libcrypto SHARED IMPORTED) +set_target_properties(libcrypto PROPERTIES + IMPORTED_LOCATION "${LIBCRYPTO}") + +#------------------------------------------------------------------------------- +# Framework Compilation and Installation +#------------------------------------------------------------------------------- +# Create a dynamic library for AI4Compiler framework. +set(ai4compiler ${PROJECT_NAME}) +add_library(${ai4compiler} SHARED ONNXRunner.cpp) + +target_include_directories(${ai4compiler} + PRIVATE + ${FRAMEWORK_INCLUDE} + "${onnxruntime_ROOTDIR}/include" + "${onnxruntime_ROOTDIR}/include/onnxruntime") + +target_link_libraries(${ai4compiler} + PRIVATE + libcrypto) + +# Install the targets and include files to expected locations. +if(NOT DEFINED CMAKE_INSTALL_LIBDIR) + if(CMAKE_SIZEOF_VOID_P EQUAL 4) + set(CMAKE_INSTALL_LIBDIR "./lib") + elseif(CMAKE_SIZEOF_VOID_P EQUAL 8) + set(CMAKE_INSTALL_LIBDIR "./lib64") + else() + message(WARNING "Unknown system architecture. Defaulting to './lib'.") + set(CMAKE_INSTALL_LIBDIR "./lib") + endif() + message(STATUS "Setting CMAKE_INSTALL_LIBDIR to '${CMAKE_INSTALL_LIBDIR}'.") +endif() + +if(NOT DEFINED CMAKE_INSTALL_INCLUDEDIR) + set(CMAKE_INSTALL_INCLUDEDIR "./include") + message(STATUS + "Setting CMAKE_INSTALL_INCLUDEDIR to " + "'${CMAKE_INSTALL_INCLUDEDIR}'.") +endif() + +install(TARGETS ${ai4compiler} + DESTINATION ${CMAKE_INSTALL_LIBDIR}) + +install(DIRECTORY "${FRAMEWORK_INCLUDE}/" + DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + FILES_MATCHING + PATTERN "*.h") + +install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../models/" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/AI4C" + FILES_MATCHING + PATTERN "*.onnx") diff --git a/aiframe/ONNXRunner.cpp b/aiframe/ONNXRunner.cpp index e369a9169777226e49fc4277a605e6fcd9caaaec..2dcdbde858f21f2128f3064fa2023713070c088f 100644 --- a/aiframe/ONNXRunner.cpp +++ b/aiframe/ONNXRunner.cpp @@ -1,6 +1,5 @@ #include "include/ONNXRunner.h" #include -#include #include #include diff --git a/aiframe/include/ONNXRunner.h b/aiframe/include/ONNXRunner.h index bc8535f1391446b305033674ef563f4f78b65a76..dd35de515f7474537a54741ecadb221659761dd9 100644 --- a/aiframe/include/ONNXRunner.h +++ b/aiframe/include/ONNXRunner.h @@ -4,9 +4,9 @@ #include "onnxruntime_c_api.h" #include "onnxruntime_cxx_api.h" #include -#include +#include +#include #include -#include #include #include #include @@ -16,7 +16,6 @@ extern "C" { namespace compilerONNXRunner { -const char* MODEL_PATH_OPT = "/usr/lib64/AI4C/optimizer.onnx"; const int FEATURE_SIZE_INT64_OPT = 6; const int FEATURE_SIZE_STRING_OPT = 11; @@ -57,8 +56,8 @@ private: }; extern ONNXRunner *createONNXRunner(const char *modelPath) { - std::ifstream file(modelPath); - if (file.good()) { + std::filesystem::path filePath(modelPath); + if (std::filesystem::exists(filePath)) { return new ONNXRunner(modelPath); } else { return nullptr; @@ -188,11 +187,46 @@ static void preprocessData(std::vector &inputString, } } +static bool findOptimizerModelPath(const std::string &modelRelPath, + std::string &optModelPath, + const char *envName = "LD_LIBRARY_PATH") { + + const char *paths = std::getenv(envName); + std::istringstream envPaths{paths ? paths : ""}; + std::vector modelPathList; + + // Split environment variables and concatenate complete model paths. + std::string modelPath; + while (std::getline(envPaths, modelPath, ':')) { + if (modelPath[modelPath.size() - 1] != '/') { + modelPath += '/'; + } + modelPath += modelRelPath; + modelPathList.push_back(modelPath); + } + + for (const auto &modelPath : modelPathList) { + std::filesystem::path filePath(modelPath); + if (std::filesystem::exists(filePath)) { + optModelPath = modelPath; + return true; + } + } + return false; +} + extern int64_t runONNXModelOptimizer(int argcSW, const char **argvSW, const char *mcpuOption, int argcHW, int64_t *argvHW) { // Create model runner. - ONNXRunner *instance = createONNXRunner(MODEL_PATH_OPT); + std::string optModelPath; + std::string modelRelPath = "AI4C/optimizer.onnx"; + const char *envName = "LD_LIBRARY_PATH"; + if (!findOptimizerModelPath(modelRelPath, optModelPath, envName)) { + return -1; + } + + ONNXRunner *instance = createONNXRunner(optModelPath.c_str()); if (instance == nullptr) { return -1; } diff --git a/third_party/onnxruntime-1.16.3/cmake/external/eigen.cmake b/third_party/onnxruntime-1.16.3/cmake/external/eigen.cmake index c0f7ddc50eb98f7fc4b3c5f68f12c433bbe6e819..01bc8f7bf30d0a9ccc79ef2dbbac901718b0c82e 100644 --- a/third_party/onnxruntime-1.16.3/cmake/external/eigen.cmake +++ b/third_party/onnxruntime-1.16.3/cmake/external/eigen.cmake @@ -7,15 +7,17 @@ else () if (onnxruntime_USE_ACL) FetchContent_Declare( eigen - URL ${DEP_URL_eigen} - URL_HASH SHA1=${DEP_SHA1_eigen} - PATCH_COMMAND ${Patch_EXECUTABLE} --ignore-space-change --ignore-whitespace < ${PROJECT_SOURCE_DIR}/patches/eigen/Fix_Eigen_Build_Break.patch +# Stop populating content from external project. +# URL ${DEP_URL_eigen} +# URL_HASH SHA1=${DEP_SHA1_eigen} +# PATCH_COMMAND ${Patch_EXECUTABLE} --ignore-space-change --ignore-whitespace < ${PROJECT_SOURCE_DIR}/patches/eigen/Fix_Eigen_Build_Break.patch ) else() FetchContent_Declare( eigen - URL ${DEP_URL_eigen} - URL_HASH SHA1=${DEP_SHA1_eigen} +# Stop populating content from external project. +# URL ${DEP_URL_eigen} +# URL_HASH SHA1=${DEP_SHA1_eigen} ) endif() FetchContent_Populate(eigen) diff --git a/third_party/onnxruntime-1.16.3/cmake/external/onnxruntime_external_deps.cmake b/third_party/onnxruntime-1.16.3/cmake/external/onnxruntime_external_deps.cmake index e63cb1f1a110ce90f750ae2a62c9de3fdddc8682..d37607a36e9639d3940843f7c5069a106b446df2 100644 --- a/third_party/onnxruntime-1.16.3/cmake/external/onnxruntime_external_deps.cmake +++ b/third_party/onnxruntime-1.16.3/cmake/external/onnxruntime_external_deps.cmake @@ -163,10 +163,11 @@ else() endif() FetchContent_Declare( Protobuf +# Stop populating content from external project. # URL ${DEP_URL_protobuf} # URL_HASH SHA1=${DEP_SHA1_protobuf} # PATCH_COMMAND ${ONNXRUNTIME_PROTOBUF_PATCH_COMMAND} - FIND_PACKAGE_ARGS 3.21.12 NAMES Protobuf +# FIND_PACKAGE_ARGS 3.21.12 NAMES Protobuf ) set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build protobuf tests" FORCE) if (CMAKE_SYSTEM_NAME STREQUAL "Android")