!19 [sync] PR-18: Update AI4C from v0.1.0 to v0.2.0

From: @openeuler-sync-bot 
Reviewed-by: @liuf9 
Signed-off-by: @liuf9
This commit is contained in:
openeuler-ci-bot 2024-08-15 09:09:07 +00:00 committed by Gitee
commit 0d85b5a26a
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
6 changed files with 8 additions and 93855 deletions

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

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

@ -14,19 +14,14 @@
Summary: %{name} is a framework which enables compilers compilers to integrate ML-driven compiler optimization.
Name: AI4C
Version: 0.1.0
Release: 5
Version: 0.2.0
Release: 1
# Package onnxruntime and SafeInt have MIT License.
# Package onnx has Apache License 2.0.
License: MIT and ASL 2.0 and Boost and BSD
URL: https://gitee.com/openeuler/AI4C
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
BuildRequires: gcc
@ -52,13 +47,13 @@ tar -xzf %{SOURCE0} -C .
%build
# Construct dependency package `cmake`
cd %{build_dir_dep}/cmake-3.28.5
cd %{build_dir_dep}/cmake
mkdir -p build && cd build
cmake .. -DCMAKE_INSTALL_PREFIX=./install
make install -j %{max_jobs}
# Broken test in aarch64 architecture.
cd %{build_dir_dep}/onnxruntime-1.16.3
cd %{build_dir_dep}/onnxruntime
%ifarch aarch64
rm -v onnxruntime/test/optimizer/nhwc_transformer_test.cc
%endif
@ -66,7 +61,7 @@ rm -v onnxruntime/test/optimizer/nhwc_transformer_test.cc
# Construct dependency package `onnxruntime`.
mkdir -p %{build_libdir}
mkdir -p %{build_includedir}
%{build_dir_dep}/cmake-3.28.5/build/install/bin/cmake \
%{build_dir_dep}/cmake/build/install/bin/cmake \
-DCMAKE_INSTALL_PREFIX=%{build_dir_frm} \
-DCMAKE_INSTALL_LIBDIR=%{build_libdir} \
-DCMAKE_INSTALL_INCLUDEDIR=%{build_includedir} \
@ -108,6 +103,9 @@ popd
%changelog
* Wed Aug 14 2024 Zhenyu Zhao <zhaozhenyu17@huawei.com> - 0.2.0-1
- Update AI4C from v0.1.0 to v0.2.0.
* Tue Aug 06 2024 Feiyang Liu <liufeiyang6@huawei.com> - 0.1.0-5
- Remove path dependencies for building AI4Compiler framework