From 95aee872fd10b51eea94256bb9ec47861848a2c0 Mon Sep 17 00:00:00 2001 From: wangzm509 Date: Sun, 15 Dec 2024 17:27:51 +0800 Subject: [PATCH 1/2] [ACPO] acpo support aot override --- build.sh | 121 ++++++++++++++++++++- llvm/cmake/modules/TensorFlowCompile.cmake | 2 + 2 files changed, 122 insertions(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 2e6d2167b3ef..b32cb2667c31 100755 --- a/build.sh +++ b/build.sh @@ -5,7 +5,6 @@ C_COMPILER_PATH=gcc CXX_COMPILER_PATH=g++ # Initialize our own variables: -enable_acpo="1" enable_autotuner="1" buildtype=RelWithDebInfo backends="all" @@ -26,7 +25,35 @@ build_dir_name="build" install_dir_name="install" build_prefix="$dir/$build_dir_name" install_prefix="$dir/$install_dir_name" +host_arch="$(uname -m)" +# ACPO specific flags +enable_acpo="1" # Support ACPO. 0: disable 1: enable +acpo_aot=0 # Support ACPO AOT recompile. 0: disable 1: enable +override_aot=1 # Support AOT compile by pre-compile model. 0: disable 1: enable +acpo_aot_models="fi" +acpo_aot_cmake_flags="" +export OPENEULER_ACPO_DIR="" # ACPO dir, need download from https://github.com/Huawei-CPLLab/ACPO. +export TENSORFLOW_AOT_PATH="" # Tensorflow dir. eg: PYTHON_DIR/site-packages/ +if [ $acpo_aot -eq 1 ] && [ $enable_acpo -eq 0 ]; then + echo "$0: acpo_aot need enable_acpo eq 1." + exit 1 +fi + +if [ $acpo_aot -eq 1 ]; then + if [ "$host_arch" = "aarch64" ]; then + aot_arch=${host_arch} + echo "$0: ACPO will be built for aarch64 architecture" + elif [ "$host_arch" = "x86_64" ]; then + aot_arch="x86" + echo "$0: ACPO will be built for x86 architecture" + else + echo "$0: ${host_arch} is not yet a supported architecture for ACPO. + Currently supporting aarch64 and x86 only. + Please use '-A' to disable ACPO for building BiSheng compiler." + exit 1 + fi +fi # Use 8 threads for builds and tests by default. Use more threads if possible, # but avoid overloading the system by using up to 50% of available cores. threads=8 @@ -79,6 +106,10 @@ while getopts :aAb:d:ceEhiI:j:orstvfX: optchr; do ;; A) enable_acpo="0" + acpo_aot=0 + acpo_aot_models="" + override_aot=0 + echo "$0: ACPO project is disabled!" ;; b) buildtype="$OPTARG" @@ -177,6 +208,93 @@ else llvm_binutils_incdir="-DLLVM_BINUTILS_INCDIR=$incdir" fi +# TensorFlow environment variables must be set if AOT is requested. +# If the user didn't specify them, try to determine the proper values. +if [ $acpo_aot -eq 1 ]; then + if [ -z "$TENSORFLOW_AOT_PATH" ]; then + echo "$0: TENSORFLOW_AOT_PATH not set, eg: export TENSORFLOW_AOT_PATH=PYTHON_DIR/site-packages/" + exit 1 + else + if [ -d "$TENSORFLOW_AOT_PATH/tensorflow" ]; then + echo "$0: tensorflow is found at $TENSORFLOW_AOT_PATH/tensorflow" + export TENSORFLOW_AOT_PATH=$TENSORFLOW_AOT_PATH/tensorflow + else + echo "$0: no tensorflow files at $TENSORFLOW_AOT_PATH" + exit 1 + fi + fi +fi + +if [ $acpo_aot -eq 1 ]; then + echo "$0: ACPO AOT compilation enabled." + + if [ -z "$OPENEULER_ACPO_DIR" ]; then + echo "$0: OPENEULER_ACPO_DIR not set" + echo "$OPENEULER_ACPO_DIR" + exit 1 + fi + echo "$0: ACPO directory is found at $OPENEULER_ACPO_DIR" + full_model_paths="" + acpo_aot_model_names="" + model_signatures="" + model_override_headers="" + override_dir="" + + if [ $override_aot -eq 1 ]; then + if [ -d "${OPENEULER_ACPO_DIR}/overrides/" ]; then + override_dir="${OPENEULER_ACPO_DIR}/overrides/" + else + echo "$0: ACPO overrrides directory for pre-compiled models is not found!" + exit 1 + fi + fi + + for model in $acpo_aot_models; do + acpo_file="${OPENEULER_ACPO_DIR}/model-${model}.acpo" + if [ -z "${acpo_file}" ]; then + echo "$0: Error loading model ${model}: Could not find ${acpo_file}" + exit 1 + fi + + full_path="${OPENEULER_ACPO_DIR}/$(grep ModelDirectory ${acpo_file} | cut -d= -f2)" + if [ -z "${full_path}" ]; then + echo "$0: Error loading model ${model}: Could not find ModelDirectory in ${acpo_file}" + exit 1 + fi + + model_name="$(grep ModelName ${acpo_file} | cut -d= -f2)" + if [ -z "${model_name}" ]; then + echo "$0: Error loading model ${model}: Could not find ModelName in ${acpo_file}" + exit 1 + fi + + model_signature="$(grep Signature ${acpo_file} | cut -d= -f2)" + if [ -z "${model_signature}" ]; then + echo "$0: Error loading model ${model}: Could not find Signature in ${acpo_file}" + exit 1 + fi + + full_model_paths="${full_model_paths}${full_path};" + acpo_aot_model_names="${acpo_aot_model_names}${model_name};" + model_signatures="${model_signatures}${model_signature};" + done + + # Remove trailing characters + full_model_paths=${full_model_paths%?} + acpo_aot_model_names=${acpo_aot_model_names%?} + model_signatures=${model_signatures%?} + + acpo_aot_cmake_flags="-DACPO_AOT=ON \ + -DTENSORFLOW_AOT_PATH=${TENSORFLOW_AOT_PATH} \ + -DLLVM_ACPO_MODEL_PATHS=${full_model_paths} \ + -DLLVM_ACPO_MODEL_NAMES=${acpo_aot_model_names} \ + -DLLVM_ACPO_MODEL_SIGNATURES=${model_signatures} \ + -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=True \ + -DLLVM_ACPO_OVERRIDE=${override_aot} \ + -DLLVM_ACPO_OVERRIDE_PATH=${override_dir} \ + -DLLVM_ACPO_OVERRIDE_ARCH=${aot_arch}" +fi + # Warning: the -DLLVM_ENABLE_PROJECTS option is specified with cmake # to avoid issues with nested quotation marks if [ $use_ccache == "1" ]; then @@ -274,6 +392,7 @@ cmake $CMAKE_OPTIONS \ -DLIBOMP_INSTALL_ALIASES=OFF \ $llvm_binutils_incdir \ $verbose \ + $acpo_aot_cmake_flags \ ../llvm make -j$threads diff --git a/llvm/cmake/modules/TensorFlowCompile.cmake b/llvm/cmake/modules/TensorFlowCompile.cmake index c4dae39f37e8..28b7a8e31690 100644 --- a/llvm/cmake/modules/TensorFlowCompile.cmake +++ b/llvm/cmake/modules/TensorFlowCompile.cmake @@ -82,6 +82,8 @@ function(tf_find_and_compile model default_url default_path test_model_generator if (EXISTS "${override_header}" AND EXISTS "${override_object}") configure_file(${override_header} ${hdr_file} COPYONLY) configure_file(${override_object} ${obj_file} COPYONLY) + string(REPLACE "lib" "include/llvm" new_hdr_file "${hdr_file}") + configure_file(${override_header} ${new_hdr_file} COPYONLY) message(STATUS "Using provided header " ${hdr_file} " and object " ${obj_file} " files for model " ${fname}) set(GENERATED_OBJS ${GENERATED_OBJS} ${obj_file}) -- Gitee From fab88fc26a172f3d698862bef9388e99d017c37a Mon Sep 17 00:00:00 2001 From: wangzm509 Date: Thu, 26 Dec 2024 10:33:45 +0800 Subject: [PATCH 2/2] [ACPO] amend bug fix for acpo override build --- build.sh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/build.sh b/build.sh index b32cb2667c31..da81fd6df043 100755 --- a/build.sh +++ b/build.sh @@ -50,8 +50,12 @@ if [ $acpo_aot -eq 1 ]; then else echo "$0: ${host_arch} is not yet a supported architecture for ACPO. Currently supporting aarch64 and x86 only. - Please use '-A' to disable ACPO for building BiSheng compiler." - exit 1 + ACPO will not building for openEuler LLVM compiler." + enable_acpo="0" + acpo_aot=0 + acpo_aot_models="" + override_aot=0 + export TENSORFLOW_AOT_PATH="" fi fi # Use 8 threads for builds and tests by default. Use more threads if possible, -- Gitee