267 lines
9.2 KiB
Diff
267 lines
9.2 KiB
Diff
From 80327cffeff2641a68748ca5487c8b1d3e4ba448 Mon Sep 17 00:00:00 2001
|
|
From: liufeiyang <liufeiyang6@huawei.com>
|
|
Date: Mon, 5 Aug 2024 21:34:51 +0800
|
|
Subject: [PATCH] Remove path dependencies for building AI4Compiler framework.
|
|
|
|
---
|
|
aiframe/CMakeLists.txt | 104 +++++++++++++++---
|
|
aiframe/ONNXRunner.cpp | 1 -
|
|
aiframe/include/ONNXRunner.h | 46 +++++++-
|
|
.../cmake/external/eigen.cmake | 12 +-
|
|
.../external/onnxruntime_external_deps.cmake | 3 +-
|
|
5 files changed, 138 insertions(+), 28 deletions(-)
|
|
|
|
diff --git a/aiframe/CMakeLists.txt b/aiframe/CMakeLists.txt
|
|
index 9f8022f5..3b851d34 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 e369a916..2dcdbde8 100644
|
|
--- a/aiframe/ONNXRunner.cpp
|
|
+++ b/aiframe/ONNXRunner.cpp
|
|
@@ -1,6 +1,5 @@
|
|
#include "include/ONNXRunner.h"
|
|
#include <algorithm>
|
|
-#include <iostream>
|
|
#include <numeric>
|
|
#include <vector>
|
|
|
|
diff --git a/aiframe/include/ONNXRunner.h b/aiframe/include/ONNXRunner.h
|
|
index bc8535f1..dd35de51 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 <algorithm>
|
|
-#include <fstream>
|
|
+#include <cstdlib>
|
|
+#include <filesystem>
|
|
#include <iomanip>
|
|
-#include <iostream>
|
|
#include <numeric>
|
|
#include <openssl/sha.h>
|
|
#include <sstream>
|
|
@@ -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<std::string> &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<std::string> 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 c0f7ddc5..01bc8f7b 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 e63cb1f1..d37607a3 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")
|
|
--
|
|
2.33.0
|
|
|