已合并
[Bugfix] Remove path dependencies for building AI4Compiler framework. #14
liuf9创建于 2024年8月7日
[Bugfix] Remove path dependencies for building AI4Compiler framework. #14
已合并
liuf9创建于 2024年8月7日
从refs/pull/14/head合入到master
共 2 个文件变更+298-40
@@ -0,0 +1,266 @@
1+From 80327cffeff2641a68748ca5487c8b1d3e4ba448 Mon Sep 17 00:00:00 2001
2+From: liufeiyang <liufeiyang6@huawei.com>
3+Date: Mon, 5 Aug 2024 21:34:51 +0800
4+Subject: [PATCH] Remove path dependencies for building AI4Compiler framework.
5+ 
6+---
7+ aiframe/CMakeLists.txt | 104 +++++++++++++++---
8+ aiframe/ONNXRunner.cpp | 1 -
9+ aiframe/include/ONNXRunner.h | 46 +++++++-
10+ .../cmake/external/eigen.cmake | 12 +-
11+ .../external/onnxruntime_external_deps.cmake | 3 +-
12+ 5 files changed, 138 insertions(+), 28 deletions(-)
13+ 
14+diff --git a/aiframe/CMakeLists.txt b/aiframe/CMakeLists.txt
15+index 9f8022f5..3b851d34 100644
16+--- a/aiframe/CMakeLists.txt
17++++ b/aiframe/CMakeLists.txt
18+@@ -4,18 +4,92 @@ project(ONNXRunner)
19+
20+ set(CMAKE_CXX_STANDARD 17)
21+
22+-set(INC_DIR /usr/include)
23+-set(INC_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/include)
24+-set(LIB_DIR /usr/lib64)
25+-
26+-include_directories(
27+- ${INC_HEADER}
28+- ${INC_DIR})
29+-
30+-link_directories(${LIB_DIR})
31+-
32+-add_library(ONNXRunner SHARED ONNXRunner.cpp)
33+-
34+-target_link_libraries(ONNXRunner
35+-PRIVATE
36+-libcrypto.so) # libonnxruntime.so
37++#-------------------------------------------------------------------------------
38++# Dependency settings
39++#-------------------------------------------------------------------------------
40++# Set include file directory for AI4Compiler framework.
41++set(FRAMEWORK_INCLUDE "${CMAKE_CURRENT_SOURCE_DIR}/include")
42++
43++# Set root directory for ONNXRuntime library.
44++set(DEFAULT_onnxruntime_ROOTDIR "/usr" CACHE FILEPATH
45++ "Default root directory for ONNXRuntime")
46++
47++if(NOT DEFINED onnxruntime_ROOTDIR)
48++ message(WARNING
49++ "Set default root path to libonnxruntime as "
50++ "${DEFAULT_onnxruntime_ROOTDIR}. "
51++ "Use -Donnxruntime_ROOTDIR to change ONNXRuntime root path.")
52++ set(onnxruntime_ROOTDIR ${DEFAULT_onnxruntime_ROOTDIR})
53++endif()
54++
55++# Set directory for dependency libraries.
56++set(DEFAULT_LIB_DEP_DIR "/usr/lib64" CACHE FILEPATH
57++ "Default directory for dependency libraries")
58++
59++# Search for dependency library libcrypto.so.
60++if(NOT DEFINED crypto_LIBDIR)
61++ message("Set default path to search libcrypto as ${DEFAULT_LIB_DEP_DIR}. "
62++ "Use -Dcrypto_LIBDIR to change lib path.")
63++ set(crypto_LIBDIR ${DEFAULT_LIB_DEP_DIR})
64++endif()
65++
66++find_library(LIBCRYPTO
67++ NAMES crypto libcrypto
68++ PATHS "${crypto_LIBDIR}")
69++if(NOT LIBCRYPTO)
70++ message(FATAL_ERROR "libcrypto library is not found! ")
71++endif()
72++
73++add_library(libcrypto SHARED IMPORTED)
74++set_target_properties(libcrypto PROPERTIES
75++ IMPORTED_LOCATION "${LIBCRYPTO}")
76++
77++#-------------------------------------------------------------------------------
78++# Framework Compilation and Installation
79++#-------------------------------------------------------------------------------
80++# Create a dynamic library for AI4Compiler framework.
81++set(ai4compiler ${PROJECT_NAME})
82++add_library(${ai4compiler} SHARED ONNXRunner.cpp)
83++
84++target_include_directories(${ai4compiler}
85++ PRIVATE
86++ ${FRAMEWORK_INCLUDE}
87++ "${onnxruntime_ROOTDIR}/include"
88++ "${onnxruntime_ROOTDIR}/include/onnxruntime")
89++
90++target_link_libraries(${ai4compiler}
91++ PRIVATE
92++ libcrypto)
93++
94++# Install the targets and include files to expected locations.
95++if(NOT DEFINED CMAKE_INSTALL_LIBDIR)
96++ if(CMAKE_SIZEOF_VOID_P EQUAL 4)
97++ set(CMAKE_INSTALL_LIBDIR "./lib")
98++ elseif(CMAKE_SIZEOF_VOID_P EQUAL 8)
99++ set(CMAKE_INSTALL_LIBDIR "./lib64")
100++ else()
101++ message(WARNING "Unknown system architecture. Defaulting to './lib'.")
102++ set(CMAKE_INSTALL_LIBDIR "./lib")
103++ endif()
104++ message(STATUS "Setting CMAKE_INSTALL_LIBDIR to '${CMAKE_INSTALL_LIBDIR}'.")
105++endif()
106++
107++if(NOT DEFINED CMAKE_INSTALL_INCLUDEDIR)
108++ set(CMAKE_INSTALL_INCLUDEDIR "./include")
109++ message(STATUS
110++ "Setting CMAKE_INSTALL_INCLUDEDIR to "
111++ "'${CMAKE_INSTALL_INCLUDEDIR}'.")
112++endif()
113++
114++install(TARGETS ${ai4compiler}
115++ DESTINATION ${CMAKE_INSTALL_LIBDIR})
116++
117++install(DIRECTORY "${FRAMEWORK_INCLUDE}/"
118++ DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
119++ FILES_MATCHING
120++ PATTERN "*.h")
121++
122++install(DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../models/"
123++ DESTINATION "${CMAKE_INSTALL_LIBDIR}/AI4C"
124++ FILES_MATCHING
125++ PATTERN "*.onnx")
126+diff --git a/aiframe/ONNXRunner.cpp b/aiframe/ONNXRunner.cpp
127+index e369a916..2dcdbde8 100644
128+--- a/aiframe/ONNXRunner.cpp
129++++ b/aiframe/ONNXRunner.cpp
130+@@ -1,6 +1,5 @@
131+ #include "include/ONNXRunner.h"
132+ #include <algorithm>
133+-#include <iostream>
134+ #include <numeric>
135+ #include <vector>
136+
137+diff --git a/aiframe/include/ONNXRunner.h b/aiframe/include/ONNXRunner.h
138+index bc8535f1..dd35de51 100644
139+--- a/aiframe/include/ONNXRunner.h
140++++ b/aiframe/include/ONNXRunner.h
141+@@ -4,9 +4,9 @@
142+ #include "onnxruntime_c_api.h"
143+ #include "onnxruntime_cxx_api.h"
144+ #include <algorithm>
145+-#include <fstream>
146++#include <cstdlib>
147++#include <filesystem>
148+ #include <iomanip>
149+-#include <iostream>
150+ #include <numeric>
151+ #include <openssl/sha.h>
152+ #include <sstream>
153+@@ -16,7 +16,6 @@
154+ extern "C" {
155+ namespace compilerONNXRunner {
156+
157+-const char* MODEL_PATH_OPT = "/usr/lib64/AI4C/optimizer.onnx";
158+ const int FEATURE_SIZE_INT64_OPT = 6;
159+ const int FEATURE_SIZE_STRING_OPT = 11;
160+
161+@@ -57,8 +56,8 @@ private:
162+ };
163+
164+ extern ONNXRunner *createONNXRunner(const char *modelPath) {
165+- std::ifstream file(modelPath);
166+- if (file.good()) {
167++ std::filesystem::path filePath(modelPath);
168++ if (std::filesystem::exists(filePath)) {
169+ return new ONNXRunner(modelPath);
170+ } else {
171+ return nullptr;
172+@@ -188,11 +187,46 @@ static void preprocessData(std::vector<std::string> &inputString,
173+ }
174+ }
175+
176++static bool findOptimizerModelPath(const std::string &modelRelPath,
177++ std::string &optModelPath,
178++ const char *envName = "LD_LIBRARY_PATH") {
179++
180++ const char *paths = std::getenv(envName);
181++ std::istringstream envPaths{paths ? paths : ""};
182++ std::vector<std::string> modelPathList;
183++
184++ // Split environment variables and concatenate complete model paths.
185++ std::string modelPath;
186++ while (std::getline(envPaths, modelPath, ':')) {
187++ if (modelPath[modelPath.size() - 1] != '/') {
188++ modelPath += '/';
189++ }
190++ modelPath += modelRelPath;
191++ modelPathList.push_back(modelPath);
192++ }
193++
194++ for (const auto &modelPath : modelPathList) {
195++ std::filesystem::path filePath(modelPath);
196++ if (std::filesystem::exists(filePath)) {
197++ optModelPath = modelPath;
198++ return true;
199++ }
200++ }
201++ return false;
202++}
203++
204+ extern int64_t runONNXModelOptimizer(int argcSW, const char **argvSW,
205+ const char *mcpuOption, int argcHW,
206+ int64_t *argvHW) {
207+ // Create model runner.
208+- ONNXRunner *instance = createONNXRunner(MODEL_PATH_OPT);
209++ std::string optModelPath;
210++ std::string modelRelPath = "AI4C/optimizer.onnx";
211++ const char *envName = "LD_LIBRARY_PATH";
212++ if (!findOptimizerModelPath(modelRelPath, optModelPath, envName)) {
213++ return -1;
214++ }
215++
216++ ONNXRunner *instance = createONNXRunner(optModelPath.c_str());
217+ if (instance == nullptr) {
218+ return -1;
219+ }
220+diff --git a/third_party/onnxruntime-1.16.3/cmake/external/eigen.cmake b/third_party/onnxruntime-1.16.3/cmake/external/eigen.cmake
221+index c0f7ddc5..01bc8f7b 100644
222+--- a/third_party/onnxruntime-1.16.3/cmake/external/eigen.cmake
223++++ b/third_party/onnxruntime-1.16.3/cmake/external/eigen.cmake
224+@@ -7,15 +7,17 @@ else ()
225+ if (onnxruntime_USE_ACL)
226+ FetchContent_Declare(
227+ eigen
228+- URL ${DEP_URL_eigen}
229+- URL_HASH SHA1=${DEP_SHA1_eigen}
230+- PATCH_COMMAND ${Patch_EXECUTABLE} --ignore-space-change --ignore-whitespace < ${PROJECT_SOURCE_DIR}/patches/eigen/Fix_Eigen_Build_Break.patch
231++# Stop populating content from external project.
232++# URL ${DEP_URL_eigen}
233++# URL_HASH SHA1=${DEP_SHA1_eigen}
234++# PATCH_COMMAND ${Patch_EXECUTABLE} --ignore-space-change --ignore-whitespace < ${PROJECT_SOURCE_DIR}/patches/eigen/Fix_Eigen_Build_Break.patch
235+ )
236+ else()
237+ FetchContent_Declare(
238+ eigen
239+- URL ${DEP_URL_eigen}
240+- URL_HASH SHA1=${DEP_SHA1_eigen}
241++# Stop populating content from external project.
242++# URL ${DEP_URL_eigen}
243++# URL_HASH SHA1=${DEP_SHA1_eigen}
244+ )
245+ endif()
246+ FetchContent_Populate(eigen)
247+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
248+index e63cb1f1..d37607a3 100644
249+--- a/third_party/onnxruntime-1.16.3/cmake/external/onnxruntime_external_deps.cmake
250++++ b/third_party/onnxruntime-1.16.3/cmake/external/onnxruntime_external_deps.cmake
251+@@ -163,10 +163,11 @@ else()
252+ endif()
253+ FetchContent_Declare(
254+ Protobuf
255++# Stop populating content from external project.
256+ # URL ${DEP_URL_protobuf}
257+ # URL_HASH SHA1=${DEP_SHA1_protobuf}
258+ # PATCH_COMMAND ${ONNXRUNTIME_PROTOBUF_PATCH_COMMAND}
259+- FIND_PACKAGE_ARGS 3.21.12 NAMES Protobuf
260++# FIND_PACKAGE_ARGS 3.21.12 NAMES Protobuf
261+ )
262+ set(protobuf_BUILD_TESTS OFF CACHE BOOL "Build protobuf tests" FORCE)
263+ if (CMAKE_SYSTEM_NAME STREQUAL "Android")
264+--
265+2.33.0
266+ 
MAI4C.spec+32-40
@@ -2,21 +2,20 @@
2 2 
3%global build_dir %{_builddir}/%{pkg_version}3%global build_dir %{_builddir}/%{pkg_version}
4%global build_dir_dep %{build_dir}/third_party4%global build_dir_dep %{build_dir}/third_party
5-%global build_dir_frm %{build_dir}/aiframe5+%global build_dir_frm %{build_dir}/aiframe/build
6-%global build_dir_model %{build_dir}/models6+%global build_libdir %{build_dir_frm}/%{_lib}
7-%global build_libdir %{build_dir_frm}/lib64
8%global build_includedir %{build_dir_frm}/include7%global build_includedir %{build_dir_frm}/include
8+%global build_dir_model %{build_libdir}/%{name}
9 9 
10%global install_libdir %{buildroot}%{_libdir}10%global install_libdir %{buildroot}%{_libdir}
11-%global install_includedir %{buildroot}%{_includedir}
12%global install_dir_model %{install_libdir}/%{name}11%global install_dir_model %{install_libdir}/%{name}
13 12 
14-%global max_jobs 813+%global max_jobs 16
15 14 
16Summary: %{name} is a framework which enables compilers compilers to integrate ML-driven compiler optimization.15Summary: %{name} is a framework which enables compilers compilers to integrate ML-driven compiler optimization.
17Name: AI4C16Name: AI4C
18Version: 0.1.017Version: 0.1.0
19-Release: 418+Release: 5
20# Package onnxruntime and SafeInt have MIT License.19# Package onnxruntime and SafeInt have MIT License.
21# Package onnx has Apache License 2.0.20# Package onnx has Apache License 2.0.
22License: MIT and ASL 2.0 and Boost and BSD21License: MIT and ASL 2.0 and Boost and BSD
@@ -26,6 +25,7 @@ Source0: %{pkg_version}.tar.gz
26Patch1: 0001-Add-batch-inference-feature-and-optimizer-model.patch25Patch1: 0001-Add-batch-inference-feature-and-optimizer-model.patch
27Patch2: 0002-Bugfix-for-tensor-formation-and-update-block-correct.patch26Patch2: 0002-Bugfix-for-tensor-formation-and-update-block-correct.patch
28Patch3: 0003-Update-block-correction-model.patch27Patch3: 0003-Update-block-correction-model.patch
28+Patch4: 0004-Remove-path-dependencies-for-building-AI4Compiler-fr.patch
29 29 
30BuildRequires: cmake >= 3.1330BuildRequires: cmake >= 3.13
31BuildRequires: make31BuildRequires: make
@@ -53,8 +53,7 @@ tar -xzf %{SOURCE0} -C .
53%build53%build
54# Construct dependency package `cmake`54# Construct dependency package `cmake`
55cd %{build_dir_dep}/cmake-3.28.555cd %{build_dir_dep}/cmake-3.28.5
56-mkdir build56+mkdir -p build && cd build
57-cd build
58cmake .. -DCMAKE_INSTALL_PREFIX=./install57cmake .. -DCMAKE_INSTALL_PREFIX=./install
59make install -j %{max_jobs} 58make install -j %{max_jobs}
60 59 
@@ -64,9 +63,13 @@ cd %{build_dir_dep}/onnxruntime-1.16.3
64rm -v onnxruntime/test/optimizer/nhwc_transformer_test.cc63rm -v onnxruntime/test/optimizer/nhwc_transformer_test.cc
65%endif64%endif
66 65 
67-%{build_dir_dep}/cmake-3.28.5/build/install/bin/cmake \66+# Construct dependency package `onnxruntime`.
68- -DCMAKE_INSTALL_LIBDIR=%{_lib} \67+mkdir -p %{build_libdir}
69- -DCMAKE_INSTALL_INCLUDEDIR=include \68+mkdir -p %{build_includedir}
69+%{build_dir_dep}/cmake-3.28.5/build/install/bin/cmake \
70+ -DCMAKE_INSTALL_PREFIX=%{build_dir_frm} \
71+ -DCMAKE_INSTALL_LIBDIR=%{build_libdir} \
72+ -DCMAKE_INSTALL_INCLUDEDIR=%{build_includedir} \
70 -Donnxruntime_BUILD_SHARED_LIB=ON \73 -Donnxruntime_BUILD_SHARED_LIB=ON \
71 -Donnxruntime_BUILD_UNIT_TESTS=ON \74 -Donnxruntime_BUILD_UNIT_TESTS=ON \
72 -Donnxruntime_INSTALL_UNIT_TESTS=OFF \75 -Donnxruntime_INSTALL_UNIT_TESTS=OFF \
@@ -76,49 +79,38 @@ rm -v onnxruntime/test/optimizer/nhwc_transformer_test.cc
76 -Donnxruntime_ENABLE_CPUINFO=ON \79 -Donnxruntime_ENABLE_CPUINFO=ON \
77 -Donnxruntime_DISABLE_ABSEIL=ON \80 -Donnxruntime_DISABLE_ABSEIL=ON \
78 -Donnxruntime_USE_NEURAL_SPEED=OFF \81 -Donnxruntime_USE_NEURAL_SPEED=OFF \
79- -Donnxruntime_ENABLE_PYTHON=ON \82+ -Donnxruntime_ENABLE_PYTHON=OFF \
80 -DCMAKE_BUILD_TYPE=RelWithDebInfo \83 -DCMAKE_BUILD_TYPE=RelWithDebInfo \
81 -S cmake84 -S cmake
82-make -j %{max_jobs}85+make -j %{max_jobs} && make install
83- 
84-mkdir -p %{build_libdir}
85-cd %{build_dir_dep}/onnxruntime-1.16.3
86-cp libonnxruntime.so.1.16.3 %{build_libdir}
87-cp libonnxruntime.so %{build_libdir}
88-cp include/onnxruntime/core/session/onnxruntime_c_api.h %{build_includedir}
89-cp include/onnxruntime/core/session/onnxruntime_cxx_api.h %{build_includedir}
90-cp include/onnxruntime/core/session/onnxruntime_cxx_inline.h %{build_includedir}
91-cp include/onnxruntime/core/session/onnxruntime_float16.h %{build_includedir}
92-cp include/onnxruntime/core/session/onnxruntime_run_options_config_keys.h %{build_includedir}
93-cp include/onnxruntime/core/session/onnxruntime_session_options_config_keys.h %{build_includedir}
94-cp include/onnxruntime/core/providers/cpu/cpu_provider_factory.h %{build_includedir}
95-cp include/onnxruntime/core/session/onnxruntime_lite_custom_op.h %{build_includedir}
96-cp include/onnxruntime/core/framework/provider_options.h %{build_includedir}
97-cp orttraining/orttraining/training_api/include/onnxruntime_training_cxx_api.h %{build_includedir}
98-cp orttraining/orttraining/training_api/include/onnxruntime_training_cxx_inline.h %{build_includedir}
99-cp orttraining/orttraining/training_api/include/onnxruntime_training_c_api.h %{build_includedir}
100- 
101 86 
102# Construct AI4C library `libONNXRunner.so`.87# Construct AI4C library `libONNXRunner.so`.
103cd %{build_dir_frm}88cd %{build_dir_frm}
104-cmake .89+cmake \
105-make -j %{max_jobs}90+ -DCMAKE_INSTALL_PREFIX=%{build_dir_frm} \
106-mv libONNXRunner.so %{build_libdir}91+ -Donnxruntime_ROOTDIR=%{build_dir_frm} \
92+ -DCMAKE_BUILD_TYPE=RelWithDebInfo \
93+ ../
94+make -j %{max_jobs} && make install
107 95 
108%install96%install
109install -d %{install_dir_model}97install -d %{install_dir_model}
110-install %{build_dir_model}/* %{install_dir_model}98+install %{build_dir_model}/* -t %{install_dir_model}
111-install %{build_libdir}/* %{install_libdir}99+install %{build_libdir}/libonnxruntime.so* -t %{install_libdir}
100+install %{build_libdir}/libONNXRunner.so -t %{install_libdir}
101+pushd %{install_libdir}
102+ln -sf libonnxruntime.so.1.* libonnxruntime.so
103+popd
112 104 
113-install -d %{install_includedir}
114-install %{build_includedir}/* %{install_includedir}
115 105 
116%files106%files
117-%{_libdir}/*107+%attr(0755,root,root) %{_libdir}/*
118-%{_includedir}/*
119 108 
120 109 
121%changelog110%changelog
111+* Tue Aug 06 2024 Feiyang Liu <liufeiyang6@huawei.com> - 0.1.0-5
112+- Remove path dependencies for building AI4Compiler framework
113+ 
122* Wed Jul 24 2024 Zhenyu Zhao <zhaozhenyu17@huawei.com> - 0.1.0-4114* Wed Jul 24 2024 Zhenyu Zhao <zhaozhenyu17@huawei.com> - 0.1.0-4
123- Update-block-correction-model.patch115- Update-block-correction-model.patch
124 116