Skip to content

Instantly share code, notes, and snippets.

@Colibrow
Created June 17, 2021 09:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Colibrow/9d2b31bc7eff127cfe74c807fce86451 to your computer and use it in GitHub Desktop.
Save Colibrow/9d2b31bc7eff127cfe74c807fce86451 to your computer and use it in GitHub Desktop.
ml-compiler traning process
export LLVM_SRCDIR=~/llvm-project
export LLVM_INSTALLDIR=~/llvm-install
cd $LLVM_SRCDIR
git checkout fa4c3f70ff0768a270b0620dc6d158ed1205ec4e
sed -i '9i\set(ENABLE_EXPERIMENTAL_NEW_PASS_MANAGER ON CACHE BOOL "")' ${LLVM_SRCDIR}/clang/cmake/caches/Android-stage2.cmake
sed -i '10i\set(LLVM_ENABLE_LTO OFF CACHE BOOL "")' ${LLVM_SRCDIR}/clang/cmake/caches/Android-stage2.cmake
//build llvm for training
cmake -S llvm -B build -G Ninja -DLLVM_ENABLE_LTO=OFF -DCMAKE_INSTALL_PREFIX=${LLVM_INSTALLDIR} -DLLVM_ENABLE_PROJECTS="clang;lld" -DTENSORFLOW_C_LIB_PATH=${TENSORFLOW_C_LIB_PATH} -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=On -C ${LLVM_SRCDIR}/clang/cmake/caches/Android-stage2.cmake
cd build
ninja install -j80
//replace toolchain use the one builed
...
//add compile flag in Build.gn(maybe build/compiler/Build.gn etc) (make sure no flto is open and also remove function-sections && data-sections)
cflags += [
"-emit-llvm-bc",
"-fexperimental-new-pass-manager",
"-Oz",
"-Xclang",
"-fembed-bitcode=all",
]
// real build
gn --export-compile-commands args out/xxx
//then use ml-compiler-opt to run training process and wait all traning is done and saved_policy is saved
...
//build real llvm for applying ml-compiler
cd $LLVM_SRCDIR
rm -rf llvm/lib/Analysis/models/inliner/*
cp -rf $OUTPUT_DIR/saved_policy/* llvm/lib/Analysis/models/inliner/
cmake -S llvm -B build-release -G Ninja -DLLVM_ENABLE_LTO=OFF -DCMAKE_INSTALL_PREFIX=${LLVM_INSTALLDIR} -DLLVM_ENABLE_PROJECTS="clang;lld" -DTENSORFLOW_AOT_PATH=${TENSORFLOW_AOT_PATH} -C ${LLVM_SRCDIR}/clang/cmake/caches/Android-stage2.cmake
cd build-release
ninja install -j80
//replace the toochain and use new CFLAGS
cflags += [
"-fexperimental-new-pass-manager",
"-Oz",
"-mllvm -enable-ml-inliner=release"
]
//recompile the project and compare the size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment