From 4d9762a85390cc9c48d8e35ca0ef0826bf4b3ac7 Mon Sep 17 00:00:00 2001 From: Hans Date: Sun, 6 Oct 2024 04:16:00 +0800 Subject: [PATCH 01/12] refactor: try implement build everything in `build.rs` --- .gitignore | 4 +- CMakeLists.txt | 110 +++++++++++++++++++++++++++++++ Cargo.lock | 17 +++++ Cargo.toml | 2 + build.rs | 164 ++++++++++++++++++++--------------------------- scripts/build.sh | 17 +++++ 6 files changed, 219 insertions(+), 95 deletions(-) create mode 100644 CMakeLists.txt create mode 100755 scripts/build.sh diff --git a/.gitignore b/.gitignore index b74212a..2d38f88 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,6 @@ npm-debug.log cargo.log cross.log lib/*.js -bin/**/* \ No newline at end of file +bin/**/* +.venv +/qairt \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a680507 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,110 @@ +cmake_minimum_required(VERSION 3.10) +project(executorch_all) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_POSITION_INDEPENDENT_CODE ON) + +include(executorch/build/Utils.cmake) + +# CONFIG + +option(EXECUTORCH_INSTALL_PATH "Path to install executorch" "" FORCE) + +set(link_libraries "executorch" "executorch_core") +if (CMAKE_BUILD_TYPE STREQUAL "Debug") + list( + APPEND link_libraries + extension_data_loader + extension_module_static + portable_ops_lib + portable_kernels + ) +else() + list( + APPEND link_libraries + extension_data_loader + extension_module_static + optimized_native_cpu_ops_lib + optimized_kernels + portable_kernels + eigen_blas + quantized_ops_lib + quantized_kernels + custom_ops + extension_tensor + extension_runner_util + cpuinfo + pthreadpool + xnnpack_backend + XNNPACK + microkernels-prod + ) + if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64") + list(APPEND link_libraries "qnn_executorch_backend") + endif() + if (CMAKE_SYSTEM_NAME STREQUAL "Darwin") + set(ET_CMAKE_ARGS "${ET_CMAKE_ARGS} -DEXECUTORCH_BUILD_COREML=ON") + endif() +endif() + +set(executorch_DIR ${EXECUTORCH_INSTALL_PATH}) +find_package(executorch REQUIRED CONFIG) + +# TOKENIZERS + +set(ABSL_ENABLE_INSTALL ON) +set(ABSL_PROPAGATE_CXX_STD ON) +add_subdirectory(executorch/extension/llm/third-party/abseil-cpp) +add_subdirectory(executorch/extension/llm/third-party/re2) +add_subdirectory(executorch/extension/llm/third-party/sentencepiece) + +file(GLOB_RECURSE TOKENIZER_SRCS + executorch/extension/llm/tokenizer/bpe_tokenizer.cpp + executorch/extension/llm/tokenizer/tiktoken.cpp +) +file(GLOB_RECURSE TOKENIZER_HDRS + executorch/extension/llm/tokenizer/*.h +) +add_library(tokenizer STATIC + ${TOKENIZER_SRCS} + ${TOKENIZER_HDRS} +) +target_include_directories(tokenizer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) + +# list( +# APPEND link_libraries +# tokenizer +# re2::re2 +# sentencepiece-static +# ) + + +# ALL + +add_library(executorch_all INTERFACE) + +target_link_options_shared_lib(executorch) +if (CMAKE_BUILD_TYPE STREQUAL "Debug") + target_link_options_shared_lib(portable_ops_lib) +else() + target_link_options_shared_lib(optimized_native_cpu_ops_lib) + target_link_options_shared_lib(quantized_ops_lib) + target_link_options_shared_lib(custom_ops) + target_link_options_shared_lib(xnnpack_backend) + if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64") + target_link_options_shared_lib(qnn_executorch_backend) + endif() + if (CMAKE_SYSTEM_NAME STREQUAL "Darwin") + target_link_options_shared_lib(coremldelegate) + endif() +endif() + +target_link_libraries(executorch_all INTERFACE ${link_libraries}) + +# INSTALL + +install(TARGETS executorch_all + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + RUNTIME DESTINATION bin +) diff --git a/Cargo.lock b/Cargo.lock index 78686ad..d45bed5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -38,6 +38,15 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "cmake" +version = "0.1.51" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb1e43aa7fd152b1f968787f7dbcdeb306d1867ff373c69955211876c053f91a" +dependencies = [ + "cc", +] + [[package]] name = "cpp" version = "0.5.9" @@ -147,11 +156,13 @@ version = "0.0.0" dependencies = [ "build-target", "cc", + "cmake", "cpp", "cpp_build", "cpp_macros", "libc", "neon", + "project-root", ] [[package]] @@ -169,6 +180,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "project-root" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bccbff07d5ed689c4087d20d7307a52ab6141edeedf487c3876a55b86cf63df" + [[package]] name = "quote" version = "1.0.37" diff --git a/Cargo.toml b/Cargo.toml index 9a4afce..7db5b01 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,3 +25,5 @@ libc = "0.2" cc = "1.1.21" cpp_build = "0.5" build-target = "0.4" +cmake = "0.1" +project-root = "*" \ No newline at end of file diff --git a/build.rs b/build.rs index 00ffea3..3edda73 100644 --- a/build.rs +++ b/build.rs @@ -1,50 +1,13 @@ extern crate build_target; extern crate cpp_build; -use build_target::Os; -use std::path::Path; - -fn link_lib(lib_path: &Path, lib: &str, whole_link: bool) -> Result<(), ()> { - let so_ext = match build_target::target_os().unwrap() { - Os::Linux => "so", - Os::MacOs => "a", - Os::Windows => "dll", - _ => panic!("Unsupported OS"), - }; - let filename = match lib { - "extension_module" => format!("lib{}.{}", lib, so_ext), - "qnn_executorch_backend" => format!("lib{}.{}", lib, so_ext), - _ => format!("lib{}.a", lib), - }; - if lib_path.join(&filename).exists() { - if filename.ends_with(so_ext) && so_ext != "a" { - println!("cargo:rustc-link-lib=dylib={}", lib); - } else { - if whole_link { - println!("cargo:rustc-link-lib=static:+whole-archive={}", lib); - } else { - println!("cargo:rustc-link-lib=static={}", lib); - } - } - return Ok(()); - } - Err(()) -} +use std::path; +use build_target::{Arch, Os}; +use cmake::Config; fn main() { - println!("cargo:rerun-if-changed=src/sampler.rs"); - println!("cargo:rerun-if-changed=src/tensor.rs"); - println!("cargo:rerun-if-changed=src/tensor.hpp"); - println!("cargo:rerun-if-changed=src/module.rs"); - println!("cargo:rerun-if-changed=src/module.hpp"); - println!("cargo:rerun-if-changed=src/method_meta.rs"); - println!("cargo:rerun-if-changed=src/evalue.rs"); - println!("cargo:rerun-if-changed=src/evalue.hpp"); - println!("cargo:rerun-if-changed=src/eterror.rs"); - println!("cargo:rerun-if-changed=src/lib.rs"); - - let install_prefix = std::env::var("EXECUTORCH_INSTALL_PREFIX") - .unwrap_or_else(|_| "executorch/cmake-out".to_string()); - let lib_path = Path::new(&install_prefix).join("lib"); + println!("cargo:rerun-if-changed=executorch/version.txt"); + println!("cargo:rerun-if-changed=CMakeLists.txt"); + println!("cargo:rerun-if-changed=src/"); let node_platform = match std::env::var("CARGO_CFG_TARGET_OS").unwrap().as_str() { "linux" => "linux", @@ -58,7 +21,70 @@ fn main() { _ => panic!("Unsupported arch"), }; - println!("cargo:rustc-link-search=native={}", lib_path.display()); + // // let executorch = cmake::build("."); + // let mut et = Config::new("executorch"); + // // debug flags + // if std::env::var("PROFILE").unwrap() == "debug" { + // et.define("EXECUTORCH_ENABLE_LOGGING", "ON"); + // et.define("EXECUTORCH_BUILD_XNNPACK", "ON"); + // et.define("EXECUTORCH_BUILD_EXTENSION_DATA_LOADER", "ON"); + // et.define("EXECUTORCH_BUILD_EXTENSION_MODULE", "ON"); + // } else { + // et.define("EXECUTORCH_BUILD_EXTENSION_DATA_LOADER", "ON"); + // et.define("EXECUTORCH_BUILD_EXTENSION_MODULE", "ON"); + // et.define("EXECUTORCH_BUILD_KERNELS_CUSTOM", "ON"); + // et.define("EXECUTORCH_BUILD_KERNELS_QUANTIZED", "ON"); + // et.define("EXECUTORCH_BUILD_KERNELS_OPTIMIZED", "ON"); + // et.define("EXECUTORCH_BUILD_EXTENSION_TENSOR", "ON"); + // et.define("EXECUTORCH_BUILD_CPUINFO", "ON"); + // et.define("EXECUTORCH_BUILD_XNNPACK", "ON"); + // et.define("EXECUTORCH_BUILD_PTHREADPOOL", "ON"); + // et.define("EXECUTORCH_BUILD_SDK", "ON"); + // et.define("EXECUTORCH_ENABLE_LOGGING", "ON"); + // if os == Os::Windows && arch == Arch::AARCH64 { + // if let Ok(qnn_sdk_root) = std::env::var("QNN_SDK_ROOT") { + // et.define("QNN_SDK_ROOT", qnn_sdk_root); + // et.define("EXECUTORCH_BUILD_QNN", "ON"); + // } + // } + // if os == Os::MacOs { + // et.define("EXECUTORCH_BUILD_COREML", "ON"); + // } + // } + // // resolve toolchain file absolute path + // // project_root::get_project_root().unwrap() + cmake/mingw-w64-aarch64.clang.toolchain.cmake + // let toolchain_file = Path::new(&project_root::get_project_root().unwrap()).join("cmake/mingw-w64-aarch64.clang.toolchain.cmake"); + // et.define("CMAKE_TOOLCHAIN_FILE", toolchain_file.display().to_string()); + // let et_path = et.build(); + // println!("cargo:rustc-link-search=native={}/lib", et_path.display()); + + let build_script = path::Path::new(&project_root::get_project_root().unwrap()).join("scripts/build.sh"); + assert!(build_script.exists(), "build.sh not found"); + let build_script_path = build_script.to_str().unwrap(); + let out_dir = path::absolute("build").unwrap().display().to_string(); + + // if env has no EXECUTORCH_INSTALL_PREFIX, run scripts/build.sh + if std::env::var("EXECUTORCH_INSTALL_PREFIX").is_err() { + let mut cmd = std::process::Command::new(build_script_path); + cmd.args(&[node_platform, node_arch, &out_dir]); + cmd.status().unwrap(); + } + + let et_path = std::env::var("EXECUTORCH_INSTALL_PREFIX").unwrap_or_else(|_| out_dir); + + println!("cargo:rustc-link-search=native={}/lib", et_path); + + let et_all = Config::new(".") + .define("EXECUTORCH_INSTALL_PATH", et_path) + .build(); + println!("cargo:rustc-link-search=native={}/lib", et_all.display()); + println!("cargo:rustc-link-lib=static=executorch_all"); + + // let install_prefix = std::env::var("EXECUTORCH_INSTALL_PREFIX") + // .unwrap_or_else(|_| "executorch/cmake-out".to_string()); + // let lib_path = Path::new(&install_prefix).join("lib"); + + // println!("cargo:rustc-link-search=native={}", lib_path.display()); // for nodejs/electron usage println!( @@ -74,56 +100,6 @@ fn main() { node_platform, node_arch ); - assert!(link_lib(&lib_path, "executorch", true).is_ok()); - if !link_lib(&lib_path, "executorch_no_prim_ops", true).is_ok() { - assert!(link_lib(&lib_path, "executorch_core", true).is_ok()); - } - if !link_lib(&lib_path, "extension_module_static", false).is_ok() { - assert!(link_lib(&lib_path, "extension_module", false).is_ok()); - } - assert!(link_lib(&lib_path, "extension_data_loader", false).is_ok()); - - // Optimized Kernels - if link_lib(&lib_path, "optimized_native_cpu_ops_lib", true).is_ok() { - assert!(link_lib(&lib_path, "optimized_kernels", false).is_ok()); - assert!(link_lib(&lib_path, "portable_kernels", false).is_ok()); - // assert!(link_lib(&lib_path, "cpublas", false).is_ok()); - assert!(link_lib(&lib_path, "eigen_blas", false).is_ok()); - } else { - assert!(link_lib(&lib_path, "portable_ops_lib", true).is_ok()); - assert!(link_lib(&lib_path, "portable_kernels", false).is_ok()); - } - - // Quantized Kernels - if link_lib(&lib_path, "quantized_ops_lib", true).is_ok() { - assert!(link_lib(&lib_path, "quantized_kernels", false).is_ok()); - } - - // Custom Ops - let _ = link_lib(&lib_path, "custom_ops", true); - - // Tensor extension - let _ = link_lib(&lib_path, "extension_tensor", false); - - // Runner Util extension - let _ = link_lib(&lib_path, "extension_runner_util", false); - - // misc. - let _ = link_lib(&lib_path, "cpuinfo", false); - let _ = link_lib(&lib_path, "pthreadpool", false); - - // XNNPACK - if link_lib(&lib_path, "xnnpack_backend", true).is_ok() { - assert!(link_lib(&lib_path, "XNNPACK", false).is_ok()); - let _ = link_lib(&lib_path, "microkernels-prod", false); - } - - // Vulkan - let _ = link_lib(&lib_path, "vulkan_backend", true); - - // QNN - let _ = link_lib(&lib_path, "qnn_executorch_backend", true); - cpp_build::Config::new() .flag("-std=c++17") .build("src/lib.rs"); diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100755 index 0000000..ad5b485 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +set -e + +TARGET_OS=$1 +TARGET_ARCH=$2 +OUT_DIR=$3 + +if [ -z "$TARGET_OS" ] || [ -z "$TARGET_ARCH" ]; then + echo "Usage: $0 " + exit 1 +fi + +if [ "$TARGET_OS" = "windows" ] && [ "$TARGET_ARCH" = "aarch64" ]; then + echo "Cross compiling for Windows on ARM64 is not supported" + exit 1 +fi From 5192e011e260ee8924f4db914cbb62548cea8751 Mon Sep 17 00:00:00 2001 From: Hans Date: Tue, 8 Oct 2024 01:39:20 +0800 Subject: [PATCH 02/12] feat: complete build cmake in rust and make Windows link stdc++ statically --- .github/workflows/ci.yml | 33 ++----- .github/workflows/release.yml | 57 +----------- CMakeLists.txt | 169 +++++++++++++++++++--------------- Cargo.lock | 14 --- Cargo.toml | 2 - build.rs | 137 ++++++++++++++------------- scripts/build.sh | 17 ---- scripts/postneon-dist.js | 17 ---- 8 files changed, 182 insertions(+), 264 deletions(-) delete mode 100755 scripts/build.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bed56e3..336f993 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -19,6 +19,8 @@ jobs: with: toolchain: nightly target: x86_64-unknown-linux-gnu + - name: Rust cache + uses: Swatinem/rust-cache@v2 - name: Use Node.js 20.x uses: actions/setup-node@v4 with: @@ -37,33 +39,16 @@ jobs: with: path: .venv key: ${{ runner.os }}-venv-3_10 - - name: Cached build - uses: actions/cache@v2 - with: - path: executorch/cmake-out - key: ${{ runner.os }}-cmake-${{ hashFiles('build.rs', 'executorch/version.txt') }} - - name: Build executorch + - name: Install python dependencies run: | - python -m venv .venv + python3 -m venv .venv source .venv/bin/activate - cd executorch - pip install tomli zstd setuptools wheel - pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cpu - if [ ! -d cmake-out ]; then - ./install_requirements.sh - cmake \ - -S . \ - -B cmake-out \ - -DCMAKE_INSTALL_PREFIX=cmake-out \ - -DEXECUTORCH_BUILD_XNNPACK=ON \ - -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \ - -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \ - -DEXECUTORCH_ENABLE_LOGGING=ON - cmake --build cmake-out --target install --config Release - fi + ./executorch/install_dependencies.sh + echo "$VIRTUAL_ENV/bin" >> $GITHUB_PATH + echo "VIRTUAL_ENV=$VIRTUAL_ENV" >> $GITHUB_ENV - name: Install dependencies run: yarn install - - name: Build - run: yarn build + - name: Minimal Build + run: yarn debug - name: Run tests run: yarn test-all diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ecad423..d632e00 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -105,57 +105,16 @@ jobs: rm llvm-mingw.tar.xz MINGW_PATH=$(realpath llvm-mingw-*) echo "PATH=$MINGW_PATH/bin:$PATH" >> $GITHUB_ENV - - name: Build executorch - shell: bash - env: - PLATFORM: ${{ matrix.plat }} - ARCH: ${{ matrix.arch }} + - name: Install python dependencies run: | python3 -m venv .venv source .venv/bin/activate - cd executorch - cd backends/xnnpack/third-party/cpuinfo/ - patch -p1 -i ${{ github.workspace }}/scripts/cpuinfo.patch - cd ${{ github.workspace }}/executorch - pip install tomli zstd setuptools wheel - pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cpu - if [ ! -d cmake-out ]; then - ./install_requirements.sh - EXTRA_CMAKE_ARGS="" - if [[ "$PLATFORM" == "windows" ]]; then - EXTRA_CMAKE_ARGS="-DCMAKE_TOOLCHAIN_FILE=../cmake/mingw-w64-${ARCH}.clang.toolchain.cmake" - if [[ "$ARCH" == "aarch64" ]]; then - EXTRA_CMAKE_ARGS="${EXTRA_CMAKE_ARGS} -DEXECUTORCH_BUILD_QNN=ON" - fi - elif [[ "$PLATFORM" == "macos" ]]; then - EXTRA_CMAKE_ARGS="-DEXECUTORCH_BUILD_COREML=ON -DCMAKE_OSX_ARCHITECTURES=${ARCH}" - elif [[ "$PLATFORM" == "linux" ]] && [[ "$ARCH" == "aarch64" ]]; then - EXTRA_CMAKE_ARGS="-DCMAKE_TOOLCHAIN_FILE=../cmake/aarch64-linux-gnu.gcc.toolchain.cmake" - fi - cmake \ - -S . \ - -B cmake-out \ - -DCMAKE_INSTALL_PREFIX=cmake-out \ - -DEXECUTORCH_BUILD_XNNPACK=ON \ - -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \ - -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON \ - -DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \ - -DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \ - -DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \ - -DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON \ - -DQNN_SDK_ROOT=$QNN_SDK_ROOT \ - -DEXECUTORCH_BUILD_CPUINFO=ON \ - -DEXECUTORCH_BUILD_XNNPACK=ON \ - -DEXECUTORCH_BUILD_PTHREADPOOL=ON \ - -DEXECUTORCH_BUILD_SDK=ON \ - -DEXECUTORCH_ENABLE_LOGGING=ON \ - $EXTRA_CMAKE_ARGS - cmake --build cmake-out --target install --config Release -j$(nproc) - fi + ./executorch/install_dependencies.sh + echo "$VIRTUAL_ENV/bin" >> $GITHUB_PATH + echo "VIRTUAL_ENV=$VIRTUAL_ENV" >> $GITHUB_ENV - name: Install dependencies run: yarn install - name: Build - shell: bash run: yarn build --target ${{ matrix.target }} - name: Upload build artifacts uses: actions/upload-artifact@v4 @@ -190,11 +149,3 @@ jobs: uses: JS-DevTools/npm-publish@v3 with: token: ${{ secrets.NPM_TOKEN }} - - name: Draft release - uses: actions/create-release@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - tag_name: ${{ github.ref }} - release_name: ${{ github.ref }} - draft: true diff --git a/CMakeLists.txt b/CMakeLists.txt index a680507..ef26b1d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,54 +1,99 @@ -cmake_minimum_required(VERSION 3.10) +cmake_minimum_required(VERSION 3.18) project(executorch_all) set(CMAKE_CXX_STANDARD 17) set(CMAKE_POSITION_INDEPENDENT_CODE ON) -include(executorch/build/Utils.cmake) +include(ExternalProject) + +if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/executorch) + ExternalProject_Add(executorch_src + PREFIX executorch + GIT_REPOSITORY https://github.com/mybigday/node-executorch.git + GIT_TAG dev + GIT_SHALLOW TRUE + GIT_SUBMODULES_RECURSE TRUE + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + ) + set(EXECUTORCH_DIR ${CMAKE_CURRENT_SOURCE_DIR}/executorch) +else() + set(EXECUTORCH_DIR ${CMAKE_CURRENT_SOURCE_DIR}/executorch) +endif() -# CONFIG +# Apply patch for cpuinfo -option(EXECUTORCH_INSTALL_PATH "Path to install executorch" "" FORCE) +find_program(GIT git REQUIRED) -set(link_libraries "executorch" "executorch_core") -if (CMAKE_BUILD_TYPE STREQUAL "Debug") - list( - APPEND link_libraries - extension_data_loader - extension_module_static - portable_ops_lib - portable_kernels - ) -else() - list( - APPEND link_libraries - extension_data_loader - extension_module_static - optimized_native_cpu_ops_lib - optimized_kernels - portable_kernels - eigen_blas - quantized_ops_lib - quantized_kernels - custom_ops - extension_tensor - extension_runner_util - cpuinfo - pthreadpool - xnnpack_backend - XNNPACK - microkernels-prod +cmake_language(DEFER DIRECTORY ${CMAKE_SOURCE_DIR} CALL _reverse_patch_cpuinfo()) +function(_reverse_patch_cpuinfo) + # reverse patch + execute_process( + COMMAND ${GIT} apply ${CMAKE_CURRENT_SOURCE_DIR}/scripts/cpuinfo.patch -R + WORKING_DIRECTORY ${EXECUTORCH_DIR}/backends/xnnpack/third-party/cpuinfo ) - if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64") - list(APPEND link_libraries "qnn_executorch_backend") +endfunction(_reverse_patch_cpuinfo) + +add_custom_target( + patch_cpuinfo ALL + COMMAND ${GIT} apply ${CMAKE_CURRENT_SOURCE_DIR}/scripts/cpuinfo.patch || true + WORKING_DIRECTORY ${EXECUTORCH_DIR}/backends/xnnpack/third-party/cpuinfo +) + +# ExecuTorch main + +if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64") + get_filename_component(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cmake/mingw-w64-aarch64.clang.toolchain.cmake ABSOLUTE) + if (QNN_SDK_ROOT AND CMAKE_BUILD_TYPE STREQUAL "Release") + list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_BUILD_QNN=ON") endif() - if (CMAKE_SYSTEM_NAME STREQUAL "Darwin") - set(ET_CMAKE_ARGS "${ET_CMAKE_ARGS} -DEXECUTORCH_BUILD_COREML=ON") +endif() + +if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64") + get_filename_component(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cmake/mingw-w64-x86_64.clang.toolchain.cmake ABSOLUTE) +endif() + +if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_BUILD_TYPE STREQUAL "Release") + set(EXECUTORCH_CMAKE_ARGS "${EXECUTORCH_CMAKE_ARGS} -DEXECUTORCH_BUILD_COREML=ON") +endif() + +if (CMAKE_BUILD_TYPE STREQUAL "Release") + list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON") + list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON") + list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON") + list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON") + list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_BUILD_CPUINFO=ON") + list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_XNNPACK_SHARED_WORKSPACE=ON") + if (CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64" AND NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") + list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_XNNPACK_ENABLE_KLEIDI=ON") endif() endif() -set(executorch_DIR ${EXECUTORCH_INSTALL_PATH}) -find_package(executorch REQUIRED CONFIG) +ExternalProject_Add(executorch + PREFIX ${CMAKE_CURRENT_BINARY_DIR}/executorch + SOURCE_DIR ${EXECUTORCH_DIR} + DEPENDS patch_cpuinfo + CMAKE_ARGS + -DCMAKE_TOOLCHAIN_FILE:PATH=${CMAKE_TOOLCHAIN_FILE} + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER} + -DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES} + -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} + -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME} + -DCMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR} + -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX} + -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM} + -DEXECUTORCH_ENABLE_LOGGING=ON + -DEXECUTORCH_BUILD_XNNPACK=ON + -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON + -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON + -DQNN_SDK_ROOT:PATH=$ENV{QNN_SDK_ROOT} + ${EXECUTORCH_CMAKE_ARGS} +) + +ExternalProject_Get_Property(executorch SOURCE_DIR) # TOKENIZERS @@ -69,42 +114,16 @@ add_library(tokenizer STATIC ${TOKENIZER_SRCS} ${TOKENIZER_HDRS} ) -target_include_directories(tokenizer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) - -# list( -# APPEND link_libraries -# tokenizer -# re2::re2 -# sentencepiece-static -# ) - - -# ALL - -add_library(executorch_all INTERFACE) - -target_link_options_shared_lib(executorch) -if (CMAKE_BUILD_TYPE STREQUAL "Debug") - target_link_options_shared_lib(portable_ops_lib) -else() - target_link_options_shared_lib(optimized_native_cpu_ops_lib) - target_link_options_shared_lib(quantized_ops_lib) - target_link_options_shared_lib(custom_ops) - target_link_options_shared_lib(xnnpack_backend) - if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64") - target_link_options_shared_lib(qnn_executorch_backend) - endif() - if (CMAKE_SYSTEM_NAME STREQUAL "Darwin") - target_link_options_shared_lib(coremldelegate) - endif() -endif() - -target_link_libraries(executorch_all INTERFACE ${link_libraries}) - -# INSTALL +target_include_directories( + tokenizer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/executorch/extension/llm/third-party/abseil-cpp + ${CMAKE_CURRENT_SOURCE_DIR}/executorch/extension/llm/third-party/re2 +) +target_link_libraries(tokenizer PUBLIC re2::re2 sentencepiece-static) -install(TARGETS executorch_all - LIBRARY DESTINATION lib - ARCHIVE DESTINATION lib - RUNTIME DESTINATION bin +install( + TARGETS tokenizer + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ) diff --git a/Cargo.lock b/Cargo.lock index d45bed5..ca3cb49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,12 +11,6 @@ dependencies = [ "memchr", ] -[[package]] -name = "build-target" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "832133bbabbbaa9fbdba793456a2827627a7d2b8fb96032fa1e7666d7895832b" - [[package]] name = "byteorder" version = "1.5.0" @@ -154,7 +148,6 @@ dependencies = [ name = "node-executorch" version = "0.0.0" dependencies = [ - "build-target", "cc", "cmake", "cpp", @@ -162,7 +155,6 @@ dependencies = [ "cpp_macros", "libc", "neon", - "project-root", ] [[package]] @@ -180,12 +172,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "project-root" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bccbff07d5ed689c4087d20d7307a52ab6141edeedf487c3876a55b86cf63df" - [[package]] name = "quote" version = "1.0.37" diff --git a/Cargo.toml b/Cargo.toml index 7db5b01..4c342c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,4 @@ libc = "0.2" [build-dependencies] cc = "1.1.21" cpp_build = "0.5" -build-target = "0.4" cmake = "0.1" -project-root = "*" \ No newline at end of file diff --git a/build.rs b/build.rs index 3edda73..afadfb6 100644 --- a/build.rs +++ b/build.rs @@ -1,8 +1,18 @@ -extern crate build_target; extern crate cpp_build; -use std::path; -use build_target::{Arch, Os}; -use cmake::Config; +use std::path::Path; +use std::fs; + +fn link_shared(lib: &str) { + println!("cargo:rustc-link-lib=dylib={}", lib); +} + +fn link_whole(lib: &str) { + println!("cargo:rustc-link-lib=static:+whole-archive={}", lib); +} + +fn link(lib: &str) { + println!("cargo:rustc-link-lib=static={}", lib); +} fn main() { println!("cargo:rerun-if-changed=executorch/version.txt"); @@ -21,70 +31,73 @@ fn main() { _ => panic!("Unsupported arch"), }; - // // let executorch = cmake::build("."); - // let mut et = Config::new("executorch"); - // // debug flags - // if std::env::var("PROFILE").unwrap() == "debug" { - // et.define("EXECUTORCH_ENABLE_LOGGING", "ON"); - // et.define("EXECUTORCH_BUILD_XNNPACK", "ON"); - // et.define("EXECUTORCH_BUILD_EXTENSION_DATA_LOADER", "ON"); - // et.define("EXECUTORCH_BUILD_EXTENSION_MODULE", "ON"); - // } else { - // et.define("EXECUTORCH_BUILD_EXTENSION_DATA_LOADER", "ON"); - // et.define("EXECUTORCH_BUILD_EXTENSION_MODULE", "ON"); - // et.define("EXECUTORCH_BUILD_KERNELS_CUSTOM", "ON"); - // et.define("EXECUTORCH_BUILD_KERNELS_QUANTIZED", "ON"); - // et.define("EXECUTORCH_BUILD_KERNELS_OPTIMIZED", "ON"); - // et.define("EXECUTORCH_BUILD_EXTENSION_TENSOR", "ON"); - // et.define("EXECUTORCH_BUILD_CPUINFO", "ON"); - // et.define("EXECUTORCH_BUILD_XNNPACK", "ON"); - // et.define("EXECUTORCH_BUILD_PTHREADPOOL", "ON"); - // et.define("EXECUTORCH_BUILD_SDK", "ON"); - // et.define("EXECUTORCH_ENABLE_LOGGING", "ON"); - // if os == Os::Windows && arch == Arch::AARCH64 { - // if let Ok(qnn_sdk_root) = std::env::var("QNN_SDK_ROOT") { - // et.define("QNN_SDK_ROOT", qnn_sdk_root); - // et.define("EXECUTORCH_BUILD_QNN", "ON"); - // } - // } - // if os == Os::MacOs { - // et.define("EXECUTORCH_BUILD_COREML", "ON"); - // } - // } - // // resolve toolchain file absolute path - // // project_root::get_project_root().unwrap() + cmake/mingw-w64-aarch64.clang.toolchain.cmake - // let toolchain_file = Path::new(&project_root::get_project_root().unwrap()).join("cmake/mingw-w64-aarch64.clang.toolchain.cmake"); - // et.define("CMAKE_TOOLCHAIN_FILE", toolchain_file.display().to_string()); - // let et_path = et.build(); - // println!("cargo:rustc-link-search=native={}/lib", et_path.display()); - - let build_script = path::Path::new(&project_root::get_project_root().unwrap()).join("scripts/build.sh"); - assert!(build_script.exists(), "build.sh not found"); - let build_script_path = build_script.to_str().unwrap(); - let out_dir = path::absolute("build").unwrap().display().to_string(); - - // if env has no EXECUTORCH_INSTALL_PREFIX, run scripts/build.sh - if std::env::var("EXECUTORCH_INSTALL_PREFIX").is_err() { - let mut cmd = std::process::Command::new(build_script_path); - cmd.args(&[node_platform, node_arch, &out_dir]); - cmd.status().unwrap(); + let install_path = cmake::build("."); + println!("cargo:rustc-link-search=native={}/lib", install_path.display()); + + if node_platform == "win32" { + link("c++"); + link("c++abi"); + link("unwind"); } - let et_path = std::env::var("EXECUTORCH_INSTALL_PREFIX").unwrap_or_else(|_| out_dir); + // common + link_whole("executorch"); + link("executorch_core"); + link("extension_module_static"); + link("extension_data_loader"); + link("tokenizer"); + + // XNNPACK + link_whole("xnnpack_backend"); + link("XNNPACK"); + link("microkernels-prod"); + link("cpuinfo"); + link("pthreadpool"); + + if cfg!(debug_assertions) { + link_whole("portable_ops_lib"); + link("portable_kernels"); + } else { + link("extension_tensor"); + link("extension_threadpool"); - println!("cargo:rustc-link-search=native={}/lib", et_path); + // Optimized Ops + link_whole("optimized_native_cpu_ops_lib"); + link("optimized_kernels"); + link("portable_kernels"); + link("eigen_blas"); - let et_all = Config::new(".") - .define("EXECUTORCH_INSTALL_PATH", et_path) - .build(); - println!("cargo:rustc-link-search=native={}/lib", et_all.display()); - println!("cargo:rustc-link-lib=static=executorch_all"); + // Quantized Ops + link_whole("quantized_ops_lib"); + link("quantized_kernels"); - // let install_prefix = std::env::var("EXECUTORCH_INSTALL_PREFIX") - // .unwrap_or_else(|_| "executorch/cmake-out".to_string()); - // let lib_path = Path::new(&install_prefix).join("lib"); + // Custom Ops + link_whole("custom_ops"); - // println!("cargo:rustc-link-search=native={}", lib_path.display()); + // XNNPACK extra + if node_platform != "darwin" && node_arch == "arm64" { + link_whole("kleidiai"); + } + + // CoreML + if node_platform == "darwin" { + link_whole("coremldelegate"); + link("sqlite3"); + println!("cargo:rustc-link-arg=-framework Foundation"); + println!("cargo:rustc-link-arg=-framework CoreML"); + println!("cargo:rustc-link-arg=-framework Accelerate"); + } + + // QNN + if node_platform == "win32" && node_arch == "arm64" && std::env::var("QNN_SDK_ROOT").is_ok() { + println!("cargo:rerun-if-env-changed=QNN_SDK_ROOT"); + link_shared("qnn_executorch_backend"); + let src = Path::new(&install_path).join("lib/qnn_executorch_backend.dll"); + let out_dir = std::env::var("OUT_DIR").unwrap_or_else(|_| ".".to_string()); + let dst = Path::new(&out_dir).join("bin").join(node_platform).join(node_arch).join("qnn_executorch_backend.dll"); + fs::copy(src, dst).unwrap(); + } + } // for nodejs/electron usage println!( diff --git a/scripts/build.sh b/scripts/build.sh deleted file mode 100755 index ad5b485..0000000 --- a/scripts/build.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -set -e - -TARGET_OS=$1 -TARGET_ARCH=$2 -OUT_DIR=$3 - -if [ -z "$TARGET_OS" ] || [ -z "$TARGET_ARCH" ]; then - echo "Usage: $0 " - exit 1 -fi - -if [ "$TARGET_OS" = "windows" ] && [ "$TARGET_ARCH" = "aarch64" ]; then - echo "Cross compiling for Windows on ARM64 is not supported" - exit 1 -fi diff --git a/scripts/postneon-dist.js b/scripts/postneon-dist.js index 8c0892b..23a9f0d 100644 --- a/scripts/postneon-dist.js +++ b/scripts/postneon-dist.js @@ -47,23 +47,6 @@ if (!fs.existsSync(`bin/${platform}/${arch}`)) { // Rename the index.node file to executorch.node fs.renameSync('index.node', `bin/${platform}/${arch}/executorch.node`); -// Copy LLVM libraries for MSVCRT -if (platform === 'win32') { - const libs = ['libc++.dll', 'libomp.dll', 'libunwind.dll', 'libwinpthread-1.dll']; - // find install prefix in PATH - const mingw_prefix = arch === 'arm64' ? 'aarch64-w64-mingw32' : 'x86_64-w64-mingw32'; - const cmdPath = process.env.PATH.split(':').find(p => fs.existsSync(`${p}/${mingw_prefix}-gcc`)); - if (!cmdPath) { - console.warn('Could not find mingw in PATH'); - } - const platformDir = path.join(cmdPath, '..', mingw_prefix); - if (fs.existsSync(platformDir)) { - for (const lib of libs) { - fs.copyFileSync(`${platformDir}/bin/${lib}`, `bin/${platform}/${arch}/${lib}`); - } - } -} - // Copy the shared libraries to the bin directory const installPrefix = process.env.EXECUTORCH_INSTALL_PREFIX || 'executorch/cmake-out'; From a70d3986fb0d22e9b547009270cd90b2549febd5 Mon Sep 17 00:00:00 2001 From: Hans Date: Tue, 8 Oct 2024 01:42:43 +0800 Subject: [PATCH 03/12] ci: correct install path --- .github/workflows/ci.yml | 2 +- .github/workflows/release.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 336f993..0743e7c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -43,7 +43,7 @@ jobs: run: | python3 -m venv .venv source .venv/bin/activate - ./executorch/install_dependencies.sh + ./executorch/install_requirements.sh echo "$VIRTUAL_ENV/bin" >> $GITHUB_PATH echo "VIRTUAL_ENV=$VIRTUAL_ENV" >> $GITHUB_ENV - name: Install dependencies diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index d632e00..ed75714 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -109,7 +109,7 @@ jobs: run: | python3 -m venv .venv source .venv/bin/activate - ./executorch/install_dependencies.sh + ./executorch/install_requirements.sh echo "$VIRTUAL_ENV/bin" >> $GITHUB_PATH echo "VIRTUAL_ENV=$VIRTUAL_ENV" >> $GITHUB_ENV - name: Install dependencies From 6e855872e966aa579933ce6874b19f89135444f2 Mon Sep 17 00:00:00 2001 From: Hans Date: Tue, 8 Oct 2024 01:58:33 +0800 Subject: [PATCH 04/12] chore: move sampler compile to cmake --- CMakeLists.txt | 26 ++++++++++++++++++++++++-- build.rs | 1 + src/sampler.rs | 1 - 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ef26b1d..c4b4b10 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -103,11 +103,11 @@ add_subdirectory(executorch/extension/llm/third-party/abseil-cpp) add_subdirectory(executorch/extension/llm/third-party/re2) add_subdirectory(executorch/extension/llm/third-party/sentencepiece) -file(GLOB_RECURSE TOKENIZER_SRCS +file(GLOB TOKENIZER_SRCS executorch/extension/llm/tokenizer/bpe_tokenizer.cpp executorch/extension/llm/tokenizer/tiktoken.cpp ) -file(GLOB_RECURSE TOKENIZER_HDRS +file(GLOB TOKENIZER_HDRS executorch/extension/llm/tokenizer/*.h ) add_library(tokenizer STATIC @@ -127,3 +127,25 @@ install( LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ) + +# SAMPLER + +file(GLOB SAMPLER_SRCS + executorch/extension/llm/sampler/sampler.cpp +) +file(GLOB SAMPLER_HDRS + executorch/extension/llm/sampler/sampler.h +) +add_library(sampler STATIC + ${SAMPLER_SRCS} + ${SAMPLER_HDRS} +) +target_include_directories(sampler PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_link_libraries(sampler PUBLIC tokenizer) + +install( + TARGETS sampler + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} +) diff --git a/build.rs b/build.rs index afadfb6..25e4f3d 100644 --- a/build.rs +++ b/build.rs @@ -46,6 +46,7 @@ fn main() { link("extension_module_static"); link("extension_data_loader"); link("tokenizer"); + link("sampler"); // XNNPACK link_whole("xnnpack_backend"); diff --git a/src/sampler.rs b/src/sampler.rs index d0d0e43..ef6a515 100644 --- a/src/sampler.rs +++ b/src/sampler.rs @@ -7,7 +7,6 @@ use neon::types::Finalize; cpp! {{ #include - #include }} cpp_class!(pub unsafe struct Sampler as "torch::executor::Sampler"); From e8157340929adfc06edefb77d6d4394b56581133 Mon Sep 17 00:00:00 2001 From: Hans Date: Tue, 8 Oct 2024 01:58:44 +0800 Subject: [PATCH 05/12] chore: format rust --- build.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/build.rs b/build.rs index 25e4f3d..5033d8c 100644 --- a/build.rs +++ b/build.rs @@ -1,6 +1,6 @@ extern crate cpp_build; -use std::path::Path; use std::fs; +use std::path::Path; fn link_shared(lib: &str) { println!("cargo:rustc-link-lib=dylib={}", lib); @@ -32,7 +32,10 @@ fn main() { }; let install_path = cmake::build("."); - println!("cargo:rustc-link-search=native={}/lib", install_path.display()); + println!( + "cargo:rustc-link-search=native={}/lib", + install_path.display() + ); if node_platform == "win32" { link("c++"); @@ -90,12 +93,17 @@ fn main() { } // QNN - if node_platform == "win32" && node_arch == "arm64" && std::env::var("QNN_SDK_ROOT").is_ok() { + if node_platform == "win32" && node_arch == "arm64" && std::env::var("QNN_SDK_ROOT").is_ok() + { println!("cargo:rerun-if-env-changed=QNN_SDK_ROOT"); link_shared("qnn_executorch_backend"); let src = Path::new(&install_path).join("lib/qnn_executorch_backend.dll"); let out_dir = std::env::var("OUT_DIR").unwrap_or_else(|_| ".".to_string()); - let dst = Path::new(&out_dir).join("bin").join(node_platform).join(node_arch).join("qnn_executorch_backend.dll"); + let dst = Path::new(&out_dir) + .join("bin") + .join(node_platform) + .join(node_arch) + .join("qnn_executorch_backend.dll"); fs::copy(src, dst).unwrap(); } } From c48ab8a6b4cc57e746df7dcbd8600d2b0649b251 Mon Sep 17 00:00:00 2001 From: Hans Date: Tue, 8 Oct 2024 02:01:28 +0800 Subject: [PATCH 06/12] chore: format cmake --- CMakeLists.txt | 125 +++++++++++++++++++++++-------------------------- 1 file changed, 59 insertions(+), 66 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c4b4b10..6d4c107 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,8 +6,9 @@ set(CMAKE_POSITION_INDEPENDENT_CODE ON) include(ExternalProject) -if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/executorch) - ExternalProject_Add(executorch_src +if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/executorch) + ExternalProject_Add( + executorch_src PREFIX executorch GIT_REPOSITORY https://github.com/mybigday/node-executorch.git GIT_TAG dev @@ -15,8 +16,7 @@ if (NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/executorch) GIT_SUBMODULES_RECURSE TRUE CONFIGURE_COMMAND "" BUILD_COMMAND "" - INSTALL_COMMAND "" - ) + INSTALL_COMMAND "") set(EXECUTORCH_DIR ${CMAKE_CURRENT_SOURCE_DIR}/executorch) else() set(EXECUTORCH_DIR ${CMAKE_CURRENT_SOURCE_DIR}/executorch) @@ -26,72 +26,80 @@ endif() find_program(GIT git REQUIRED) -cmake_language(DEFER DIRECTORY ${CMAKE_SOURCE_DIR} CALL _reverse_patch_cpuinfo()) +cmake_language(DEFER DIRECTORY ${CMAKE_SOURCE_DIR} CALL _reverse_patch_cpuinfo + ()) function(_reverse_patch_cpuinfo) # reverse patch execute_process( COMMAND ${GIT} apply ${CMAKE_CURRENT_SOURCE_DIR}/scripts/cpuinfo.patch -R - WORKING_DIRECTORY ${EXECUTORCH_DIR}/backends/xnnpack/third-party/cpuinfo - ) + WORKING_DIRECTORY ${EXECUTORCH_DIR}/backends/xnnpack/third-party/cpuinfo) endfunction(_reverse_patch_cpuinfo) add_custom_target( patch_cpuinfo ALL COMMAND ${GIT} apply ${CMAKE_CURRENT_SOURCE_DIR}/scripts/cpuinfo.patch || true - WORKING_DIRECTORY ${EXECUTORCH_DIR}/backends/xnnpack/third-party/cpuinfo -) + WORKING_DIRECTORY ${EXECUTORCH_DIR}/backends/xnnpack/third-party/cpuinfo) # ExecuTorch main -if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64") - get_filename_component(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cmake/mingw-w64-aarch64.clang.toolchain.cmake ABSOLUTE) - if (QNN_SDK_ROOT AND CMAKE_BUILD_TYPE STREQUAL "Release") +if(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL + "ARM64") + get_filename_component( + CMAKE_TOOLCHAIN_FILE + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/mingw-w64-aarch64.clang.toolchain.cmake + ABSOLUTE) + if(QNN_SDK_ROOT AND CMAKE_BUILD_TYPE STREQUAL "Release") list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_BUILD_QNN=ON") endif() endif() -if (CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64") - get_filename_component(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/cmake/mingw-w64-x86_64.clang.toolchain.cmake ABSOLUTE) +if(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL + "x86_64") + get_filename_component( + CMAKE_TOOLCHAIN_FILE + ${CMAKE_CURRENT_SOURCE_DIR}/cmake/mingw-w64-x86_64.clang.toolchain.cmake + ABSOLUTE) endif() -if (CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_BUILD_TYPE STREQUAL "Release") - set(EXECUTORCH_CMAKE_ARGS "${EXECUTORCH_CMAKE_ARGS} -DEXECUTORCH_BUILD_COREML=ON") +if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_BUILD_TYPE STREQUAL "Release") + set(EXECUTORCH_CMAKE_ARGS + "${EXECUTORCH_CMAKE_ARGS} -DEXECUTORCH_BUILD_COREML=ON") endif() -if (CMAKE_BUILD_TYPE STREQUAL "Release") +if(CMAKE_BUILD_TYPE STREQUAL "Release") list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON") list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON") list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON") list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON") list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_BUILD_CPUINFO=ON") list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_XNNPACK_SHARED_WORKSPACE=ON") - if (CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64" AND NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin") + if(CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64" AND NOT CMAKE_SYSTEM_NAME STREQUAL + "Darwin") list(APPEND EXECUTORCH_CMAKE_ARGS "-DEXECUTORCH_XNNPACK_ENABLE_KLEIDI=ON") endif() endif() -ExternalProject_Add(executorch +ExternalProject_Add( + executorch PREFIX ${CMAKE_CURRENT_BINARY_DIR}/executorch SOURCE_DIR ${EXECUTORCH_DIR} DEPENDS patch_cpuinfo - CMAKE_ARGS - -DCMAKE_TOOLCHAIN_FILE:PATH=${CMAKE_TOOLCHAIN_FILE} - -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} - -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} - -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER} - -DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES} - -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} - -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME} - -DCMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR} - -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX} - -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM} - -DEXECUTORCH_ENABLE_LOGGING=ON - -DEXECUTORCH_BUILD_XNNPACK=ON - -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON - -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON - -DQNN_SDK_ROOT:PATH=$ENV{QNN_SDK_ROOT} - ${EXECUTORCH_CMAKE_ARGS} -) + CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE:PATH=${CMAKE_TOOLCHAIN_FILE} + -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER} + -DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER} + -DCMAKE_ASM_COMPILER=${CMAKE_ASM_COMPILER} + -DCMAKE_OSX_ARCHITECTURES=${CMAKE_OSX_ARCHITECTURES} + -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE} + -DCMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME} + -DCMAKE_SYSTEM_PROCESSOR=${CMAKE_SYSTEM_PROCESSOR} + -DCMAKE_INSTALL_PREFIX:PATH=${CMAKE_INSTALL_PREFIX} + -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM} + -DEXECUTORCH_ENABLE_LOGGING=ON + -DEXECUTORCH_BUILD_XNNPACK=ON + -DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON + -DEXECUTORCH_BUILD_EXTENSION_MODULE=ON + -DQNN_SDK_ROOT:PATH=$ENV{QNN_SDK_ROOT} + ${EXECUTORCH_CMAKE_ARGS}) ExternalProject_Get_Property(executorch SOURCE_DIR) @@ -103,43 +111,29 @@ add_subdirectory(executorch/extension/llm/third-party/abseil-cpp) add_subdirectory(executorch/extension/llm/third-party/re2) add_subdirectory(executorch/extension/llm/third-party/sentencepiece) -file(GLOB TOKENIZER_SRCS - executorch/extension/llm/tokenizer/bpe_tokenizer.cpp - executorch/extension/llm/tokenizer/tiktoken.cpp -) -file(GLOB TOKENIZER_HDRS - executorch/extension/llm/tokenizer/*.h -) -add_library(tokenizer STATIC - ${TOKENIZER_SRCS} - ${TOKENIZER_HDRS} -) +file(GLOB TOKENIZER_SRCS executorch/extension/llm/tokenizer/bpe_tokenizer.cpp + executorch/extension/llm/tokenizer/tiktoken.cpp) +file(GLOB TOKENIZER_HDRS executorch/extension/llm/tokenizer/*.h) +add_library(tokenizer STATIC ${TOKENIZER_SRCS} ${TOKENIZER_HDRS}) target_include_directories( - tokenizer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} - ${CMAKE_CURRENT_SOURCE_DIR}/executorch/extension/llm/third-party/abseil-cpp - ${CMAKE_CURRENT_SOURCE_DIR}/executorch/extension/llm/third-party/re2 -) + tokenizer + PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/executorch/extension/llm/third-party/abseil-cpp + ${CMAKE_CURRENT_SOURCE_DIR}/executorch/extension/llm/third-party/re2) target_link_libraries(tokenizer PUBLIC re2::re2 sentencepiece-static) install( TARGETS tokenizer RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} -) + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) # SAMPLER -file(GLOB SAMPLER_SRCS - executorch/extension/llm/sampler/sampler.cpp -) -file(GLOB SAMPLER_HDRS - executorch/extension/llm/sampler/sampler.h -) -add_library(sampler STATIC - ${SAMPLER_SRCS} - ${SAMPLER_HDRS} -) +file(GLOB SAMPLER_SRCS executorch/extension/llm/sampler/sampler.cpp) +file(GLOB SAMPLER_HDRS executorch/extension/llm/sampler/sampler.h) +add_library(sampler STATIC ${SAMPLER_SRCS} ${SAMPLER_HDRS}) target_include_directories(sampler PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_link_libraries(sampler PUBLIC tokenizer) @@ -147,5 +141,4 @@ install( TARGETS sampler RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} -) + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) From b26b1b6dc45e3125040920382fc184f62ccefcc1 Mon Sep 17 00:00:00 2001 From: Hans Date: Tue, 8 Oct 2024 02:05:01 +0800 Subject: [PATCH 07/12] ci: add cmake format workflow --- .github/workflows/cmake-format.yml | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 .github/workflows/cmake-format.yml diff --git a/.github/workflows/cmake-format.yml b/.github/workflows/cmake-format.yml new file mode 100644 index 0000000..991da08 --- /dev/null +++ b/.github/workflows/cmake-format.yml @@ -0,0 +1,23 @@ +name: Run cmake-format linter + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Format CMake files + id: cmake-format + uses: PuneetMatharu/cmake-format-lint-action@v1.0.4 + with: + args: --in-place + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_user_name: cmake-format-bot + commit_message: 'Automated commit of cmake-format changes.' From 7a6d6eb57e55a1a61821d5b70d41a26e3d2ac687 Mon Sep 17 00:00:00 2001 From: hans00 Date: Mon, 7 Oct 2024 18:05:25 +0000 Subject: [PATCH 08/12] Automated commit of cmake-format changes. --- cmake/aarch64-linux-gnu.gcc.toolchain.cmake | 10 +++++++--- cmake/mingw-w64-aarch64.clang.toolchain.cmake | 8 ++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cmake/aarch64-linux-gnu.gcc.toolchain.cmake b/cmake/aarch64-linux-gnu.gcc.toolchain.cmake index 61584ec..776dcd6 100644 --- a/cmake/aarch64-linux-gnu.gcc.toolchain.cmake +++ b/cmake/aarch64-linux-gnu.gcc.toolchain.cmake @@ -1,10 +1,14 @@ set(CMAKE_SYSTEM_NAME Linux) set(CMAKE_SYSTEM_PROCESSOR arm64) -find_program(AARCH64_LINUX_CC NAMES aarch64-linux-gnu-gcc-13 aarch64-linux-gnu-gcc-14 aarch64-linux-gnu-gcc) -find_program(AARCH64_LINUX_CXX NAMES aarch64-linux-gnu-g++-13 aarch64-linux-gnu-g++-14 aarch64-linux-gnu-g++) +find_program( + AARCH64_LINUX_CC NAMES aarch64-linux-gnu-gcc-13 aarch64-linux-gnu-gcc-14 + aarch64-linux-gnu-gcc) +find_program( + AARCH64_LINUX_CXX NAMES aarch64-linux-gnu-g++-13 aarch64-linux-gnu-g++-14 + aarch64-linux-gnu-g++) if(NOT AARCH64_LINUX_CC) - message(FATAL_ERROR "aarch64-linux-gnu-gcc not found") + message(FATAL_ERROR "aarch64-linux-gnu-gcc not found") endif() set(CMAKE_C_COMPILER ${AARCH64_LINUX_CC}) diff --git a/cmake/mingw-w64-aarch64.clang.toolchain.cmake b/cmake/mingw-w64-aarch64.clang.toolchain.cmake index bb20bcd..df55e47 100644 --- a/cmake/mingw-w64-aarch64.clang.toolchain.cmake +++ b/cmake/mingw-w64-aarch64.clang.toolchain.cmake @@ -13,5 +13,9 @@ set(CMAKE_C_FLAGS "-march=armv8.4-a") set(CMAKE_CXX_FLAGS "-march=armv8.4-a") # cache flags -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" CACHE STRING "c flags") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" CACHE STRING "c++ flags") +set(CMAKE_C_FLAGS + "${CMAKE_C_FLAGS}" + CACHE STRING "c flags") +set(CMAKE_CXX_FLAGS + "${CMAKE_CXX_FLAGS}" + CACHE STRING "c++ flags") From 3e8d1da7113bed2327e666d4257c04b64f3f592b Mon Sep 17 00:00:00 2001 From: Hans Date: Tue, 8 Oct 2024 02:06:32 +0800 Subject: [PATCH 09/12] ci: correct bot commit message --- .github/workflows/cmake-format.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/cmake-format.yml b/.github/workflows/cmake-format.yml index 991da08..5c9d257 100644 --- a/.github/workflows/cmake-format.yml +++ b/.github/workflows/cmake-format.yml @@ -20,4 +20,4 @@ jobs: uses: stefanzweifel/git-auto-commit-action@v4 with: commit_user_name: cmake-format-bot - commit_message: 'Automated commit of cmake-format changes.' + commit_message: 'chore: automated commit of cmake-format changes.' From d8907344afe57bc8c14d94c6b5b88fa3bf3b817f Mon Sep 17 00:00:00 2001 From: hans00 Date: Mon, 7 Oct 2024 18:11:03 +0000 Subject: [PATCH 10/12] chore: automated commit of cmake-format changes. --- cmake/mingw-w64-aarch64.clang.toolchain.cmake | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmake/mingw-w64-aarch64.clang.toolchain.cmake b/cmake/mingw-w64-aarch64.clang.toolchain.cmake index 2ffbd69..3d2fb74 100644 --- a/cmake/mingw-w64-aarch64.clang.toolchain.cmake +++ b/cmake/mingw-w64-aarch64.clang.toolchain.cmake @@ -11,5 +11,5 @@ set(CMAKE_LINKER aarch64-w64-mingw32-ld) set(arch_c_flags "-march=armv8.7-a") -set(CMAKE_C_FLAGS_INIT "${arch_c_flags}") +set(CMAKE_C_FLAGS_INIT "${arch_c_flags}") set(CMAKE_CXX_FLAGS_INIT "${arch_c_flags}") From e5cd505d7cc5099e3aed6b5e73d8f002a36aa39c Mon Sep 17 00:00:00 2001 From: Hans Date: Tue, 8 Oct 2024 22:24:22 +0800 Subject: [PATCH 11/12] fix: fix macOS build --- CMakeLists.txt | 4 ++-- build.rs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d4c107..fc94be0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,8 +62,8 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL endif() if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_BUILD_TYPE STREQUAL "Release") - set(EXECUTORCH_CMAKE_ARGS - "${EXECUTORCH_CMAKE_ARGS} -DEXECUTORCH_BUILD_COREML=ON") + list(APPEND EXECUTORCH_CMAKE_ARGS + -DEXECUTORCH_BUILD_COREML=ON) endif() if(CMAKE_BUILD_TYPE STREQUAL "Release") diff --git a/build.rs b/build.rs index 5033d8c..40079e0 100644 --- a/build.rs +++ b/build.rs @@ -87,9 +87,9 @@ fn main() { if node_platform == "darwin" { link_whole("coremldelegate"); link("sqlite3"); - println!("cargo:rustc-link-arg=-framework Foundation"); - println!("cargo:rustc-link-arg=-framework CoreML"); - println!("cargo:rustc-link-arg=-framework Accelerate"); + println!("cargo:rustc-link-lib=framework=Foundation"); + println!("cargo:rustc-link-lib=framework=CoreML"); + println!("cargo:rustc-link-lib=framework=Accelerate"); } // QNN From aaaf6945195e9d6cc5512e71e8531002839de114 Mon Sep 17 00:00:00 2001 From: hans00 Date: Tue, 8 Oct 2024 14:25:05 +0000 Subject: [PATCH 12/12] chore: automated commit of cmake-format changes. --- CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fc94be0..1ab2d18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,8 +62,7 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows" AND CMAKE_SYSTEM_PROCESSOR STREQUAL endif() if(CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND CMAKE_BUILD_TYPE STREQUAL "Release") - list(APPEND EXECUTORCH_CMAKE_ARGS - -DEXECUTORCH_BUILD_COREML=ON) + list(APPEND EXECUTORCH_CMAKE_ARGS -DEXECUTORCH_BUILD_COREML=ON) endif() if(CMAKE_BUILD_TYPE STREQUAL "Release")