Remove path dependencies for building AI4Compiler framework.

(cherry picked from commit e519471cea08789ab4f4e88fdea2e1ddc2f9c786)
This commit is contained in:
liuf9 2024-08-07 21:57:21 +08:00 committed by openeuler-sync-bot
parent a6c66f8eb9
commit b8065522c7
2 changed files with 298 additions and 40 deletions

View File

@ -0,0 +1,266 @@
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

View File

@ -2,21 +2,20 @@
%global build_dir %{_builddir}/%{pkg_version}
%global build_dir_dep %{build_dir}/third_party
%global build_dir_frm %{build_dir}/aiframe
%global build_dir_model %{build_dir}/models
%global build_libdir %{build_dir_frm}/lib64
%global build_dir_frm %{build_dir}/aiframe/build
%global build_libdir %{build_dir_frm}/%{_lib}
%global build_includedir %{build_dir_frm}/include
%global build_dir_model %{build_libdir}/%{name}
%global install_libdir %{buildroot}%{_libdir}
%global install_includedir %{buildroot}%{_includedir}
%global install_dir_model %{install_libdir}/%{name}
%global max_jobs 8
%global max_jobs 16
Summary: %{name} is a framework which enables compilers compilers to integrate ML-driven compiler optimization.
Name: AI4C
Version: 0.1.0
Release: 4
Release: 5
# Package onnxruntime and SafeInt have MIT License.
# Package onnx has Apache License 2.0.
License: MIT and ASL 2.0 and Boost and BSD
@ -26,6 +25,7 @@ Source0: %{pkg_version}.tar.gz
Patch1: 0001-Add-batch-inference-feature-and-optimizer-model.patch
Patch2: 0002-Bugfix-for-tensor-formation-and-update-block-correct.patch
Patch3: 0003-Update-block-correction-model.patch
Patch4: 0004-Remove-path-dependencies-for-building-AI4Compiler-fr.patch
BuildRequires: cmake >= 3.13
BuildRequires: make
@ -53,8 +53,7 @@ tar -xzf %{SOURCE0} -C .
%build
# Construct dependency package `cmake`
cd %{build_dir_dep}/cmake-3.28.5
mkdir build
cd build
mkdir -p build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=./install
make install -j %{max_jobs}
@ -64,9 +63,13 @@ cd %{build_dir_dep}/onnxruntime-1.16.3
rm -v onnxruntime/test/optimizer/nhwc_transformer_test.cc
%endif
%{build_dir_dep}/cmake-3.28.5/build/install/bin/cmake \
-DCMAKE_INSTALL_LIBDIR=%{_lib} \
-DCMAKE_INSTALL_INCLUDEDIR=include \
# Construct dependency package `onnxruntime`.
mkdir -p %{build_libdir}
mkdir -p %{build_includedir}
%{build_dir_dep}/cmake-3.28.5/build/install/bin/cmake \
-DCMAKE_INSTALL_PREFIX=%{build_dir_frm} \
-DCMAKE_INSTALL_LIBDIR=%{build_libdir} \
-DCMAKE_INSTALL_INCLUDEDIR=%{build_includedir} \
-Donnxruntime_BUILD_SHARED_LIB=ON \
-Donnxruntime_BUILD_UNIT_TESTS=ON \
-Donnxruntime_INSTALL_UNIT_TESTS=OFF \
@ -76,49 +79,38 @@ rm -v onnxruntime/test/optimizer/nhwc_transformer_test.cc
-Donnxruntime_ENABLE_CPUINFO=ON \
-Donnxruntime_DISABLE_ABSEIL=ON \
-Donnxruntime_USE_NEURAL_SPEED=OFF \
-Donnxruntime_ENABLE_PYTHON=ON \
-Donnxruntime_ENABLE_PYTHON=OFF \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
-S cmake
make -j %{max_jobs}
mkdir -p %{build_libdir}
cd %{build_dir_dep}/onnxruntime-1.16.3
cp libonnxruntime.so.1.16.3 %{build_libdir}
cp libonnxruntime.so %{build_libdir}
cp include/onnxruntime/core/session/onnxruntime_c_api.h %{build_includedir}
cp include/onnxruntime/core/session/onnxruntime_cxx_api.h %{build_includedir}
cp include/onnxruntime/core/session/onnxruntime_cxx_inline.h %{build_includedir}
cp include/onnxruntime/core/session/onnxruntime_float16.h %{build_includedir}
cp include/onnxruntime/core/session/onnxruntime_run_options_config_keys.h %{build_includedir}
cp include/onnxruntime/core/session/onnxruntime_session_options_config_keys.h %{build_includedir}
cp include/onnxruntime/core/providers/cpu/cpu_provider_factory.h %{build_includedir}
cp include/onnxruntime/core/session/onnxruntime_lite_custom_op.h %{build_includedir}
cp include/onnxruntime/core/framework/provider_options.h %{build_includedir}
cp orttraining/orttraining/training_api/include/onnxruntime_training_cxx_api.h %{build_includedir}
cp orttraining/orttraining/training_api/include/onnxruntime_training_cxx_inline.h %{build_includedir}
cp orttraining/orttraining/training_api/include/onnxruntime_training_c_api.h %{build_includedir}
make -j %{max_jobs} && make install
# Construct AI4C library `libONNXRunner.so`.
cd %{build_dir_frm}
cmake .
make -j %{max_jobs}
mv libONNXRunner.so %{build_libdir}
cmake \
-DCMAKE_INSTALL_PREFIX=%{build_dir_frm} \
-Donnxruntime_ROOTDIR=%{build_dir_frm} \
-DCMAKE_BUILD_TYPE=RelWithDebInfo \
../
make -j %{max_jobs} && make install
%install
install -d %{install_dir_model}
install %{build_dir_model}/* %{install_dir_model}
install %{build_libdir}/* %{install_libdir}
install %{build_dir_model}/* -t %{install_dir_model}
install %{build_libdir}/libonnxruntime.so* -t %{install_libdir}
install %{build_libdir}/libONNXRunner.so -t %{install_libdir}
pushd %{install_libdir}
ln -sf libonnxruntime.so.1.* libonnxruntime.so
popd
install -d %{install_includedir}
install %{build_includedir}/* %{install_includedir}
%files
%{_libdir}/*
%{_includedir}/*
%attr(0755,root,root) %{_libdir}/*
%changelog
* Tue Aug 06 2024 Feiyang Liu <liufeiyang6@huawei.com> - 0.1.0-5
- Remove path dependencies for building AI4Compiler framework
* Wed Jul 24 2024 Zhenyu Zhao <zhaozhenyu17@huawei.com> - 0.1.0-4
- Update-block-correction-model.patch