diff --git a/backends/samsung/CMakeLists.txt b/backends/samsung/CMakeLists.txt index 1f647c5bbe2..c8b4bedbc4a 100644 --- a/backends/samsung/CMakeLists.txt +++ b/backends/samsung/CMakeLists.txt @@ -146,24 +146,6 @@ if(${ANDROID}) executorch_target_link_options_shared_lib(enn_backend) target_compile_options(enn_backend PRIVATE -Wno-deprecated-declarations) - set(__enn_executor_runner_srcs - ${EXECUTORCH_SOURCE_DIR}/examples/samsung/executor_runner/enn_executor_runner.cpp - ) - add_executable(enn_executor_runner ${__enn_executor_runner_srcs}) - add_dependencies(enn_executor_runner enn_backend) - target_link_libraries( - enn_executor_runner - PRIVATE enn_logging - enn_backend - gflags - executorch - extension_data_loader - portable_ops_lib - android - ) - set_target_properties( - enn_executor_runner PROPERTIES CXX_VISIBILITY_PRESET hidden - ) install( TARGETS enn_backend enn_logging EXPORT ExecuTorchTargets diff --git a/backends/samsung/README.md b/backends/samsung/README.md index bc48bad830a..249cd5bea11 100644 --- a/backends/samsung/README.md +++ b/backends/samsung/README.md @@ -49,13 +49,16 @@ Generates python artifacts that allow user call `Compile` interface to lower a m ./backends/samsung/build.sh -b x86_64 ``` -### Build ENN Executor Runner +### Build Backend Delegate ```bash ./backends/samsung/build.sh -b android --ndk ${ANDROID_NDK} ``` -ANDROID_ABI=arm64-v8a is default, necessary runtime executable generated in `build_exynos_android` directory. +ANDROID_ABI=arm64-v8a is default, necessary runtime backend library generated in `build_samsung_android` directory. -### Build Anroid Extension +### Build Executable +Please see the [README.md](../../examples/samsung/README.md). + +### Build Android Extension This is later exposed Java app. Please turn on CMake option `EXECUTORCH_BUILD_ENN`, and ENN runtime will be added. ```bash cmake extension/android \ diff --git a/backends/samsung/build.sh b/backends/samsung/build.sh index a4871feb50c..e6258152785 100755 --- a/backends/samsung/build.sh +++ b/backends/samsung/build.sh @@ -66,7 +66,17 @@ function build_android() { ANDROID_ABI=arm64-v8a ANDROID_PLATFORM=android-28 # Trace requires over android-23 + local host_flatcc=${X86_64_BUILD_DIR}/third-party/flatcc_ep/bin/flatcc + local flatcc_args=() + if [[ -x ${host_flatcc} ]]; then + flatcc_args=(-DFLATCC_EXECUTABLE=${host_flatcc}) + else + echo "Warning: ${host_flatcc} not found. Build the x86_64 target first" \ + "('-b x86_64' or '-b all') if executor_runner fails to link libflatccrt.a." + fi + cmake \ + "${flatcc_args[@]}" \ -DCMAKE_INSTALL_PREFIX=${ANDROID_BUILD_DIR} \ -DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK_ROOT}/build/cmake/android.toolchain.cmake" \ -DANDROID_NDK=${ANDROID_NDK} \ diff --git a/examples/samsung/CMakeLists.txt b/examples/samsung/CMakeLists.txt new file mode 100644 index 00000000000..28a6dcc6660 --- /dev/null +++ b/examples/samsung/CMakeLists.txt @@ -0,0 +1,61 @@ +# Copyright (c) 2025 Samsung Electronics Co. LTD +# All rights reserved +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +cmake_minimum_required(VERSION 3.15) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +project(samsung_example) + +# Source root directory for executorch. +if(NOT EXECUTORCH_ROOT) + set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..) +endif() + +include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake) +include(${EXECUTORCH_ROOT}/tools/cmake/Codegen.cmake) + +if(NOT PYTHON_EXECUTABLE) + resolve_python_executable() +endif() + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE RelWithDebInfo) +endif() + +if(CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$") + message(FATAL_ERROR "ios is not supported by Samsung AI system.") +endif() + +# prebuilt libraries. executorch package should contain portable_ops_lib, +# etdump, bundled_program. +find_package(executorch CONFIG REQUIRED) +target_compile_options(executorch INTERFACE -DET_EVENT_TRACER_ENABLED) +find_package(gflags REQUIRED) + +set(_common_compile_options -Wno-deprecated-declarations -fPIC) + +add_compile_options(-Wall -Werror -fPIC) + +message("Build Samsung Android Examples") + +set(__enn_executor_runner__srcs + ${CMAKE_CURRENT_LIST_DIR}/executor_runner/enn_executor_runner.cpp +) + +add_executable(enn_executor_runner ${__enn_executor_runner__srcs}) + +target_include_directories(enn_executor_runner PRIVATE ${EXECUTORCH_ROOT}/..) + +target_compile_options(enn_executor_runner PRIVATE ${_common_compile_options}) + +target_link_libraries( + enn_executor_runner PRIVATE enn_logging enn_backend gflags executorch + extension_data_loader portable_ops_lib +) + +set_target_properties( + enn_executor_runner PROPERTIES CXX_VISIBILITY_PRESET hidden +) diff --git a/examples/samsung/README.md b/examples/samsung/README.md index 8b21c48a34f..a64169bd9a1 100644 --- a/examples/samsung/README.md +++ b/examples/samsung/README.md @@ -1,9 +1,9 @@ -# Exynos backend Examples +# Exynos Backend examples This directory contains examples for some AI models. -Please make sure you have built the library and executable before -you start, if you have no idea how to build, please refer to [backend README](../../backends/samsung/README.md). +Please make sure you have built the library before you start, +if you have no idea how to build, please refer to [backend README](../../backends/samsung/README.md). ## Environment We set up `PYTHONPATH` because it's easier to develop and import executorch Python APIs. @@ -42,15 +42,26 @@ Examples use "PerformanceMode.HIGH_PERFORMANCE" mode, this mode is experimental. If you want to use this mode on your model, verify your model on devicefarm which can use samsung developer society site firstly for checking stability. (https://soc-developer.semiconductor.samsung.com/) +## Building Executable +### Prerequisites +Please set up the backend before building the executable, See the [backend README](../../backends/samsung/README.md) for details. +### Building 'enn_executor_runner' for Android +```bash +export EXYNOS_AI_LITECORE_ROOT=/path/to/enn_sdk +export ANDROID_NDK_ROOT=/path/to/android_ndk +${EXECUTORCH_ROOT}/examples/samsung/build.sh +``` +After the build completes, `enn_executor_runner` can be found at `${EXECUTORCH_ROOT}/build_samsung_android/examples/samsung/` ## Execution + After lowering, we could get a pte model and then run it on mobile phone. #### Step 1: Push required ENN libraries and executor runner to device ```bash DEVICE_DIR=/data/local/tmp/executorch adb shell mkdir ${DEVICE_DIR} -adb push ${EXECUTORCH_ROOT}/cmake-android-out/backends/samsung/enn_executor_runner ${DEVICE_DIR} +adb push ${EXECUTORCH_ROOT}/build_samsung_android/examples/samsung/enn_executor_runner ${DEVICE_DIR} ``` #### Step 2: Indicate dynamic linkers and execute model diff --git a/examples/samsung/build.sh b/examples/samsung/build.sh new file mode 100755 index 00000000000..4568180777a --- /dev/null +++ b/examples/samsung/build.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +set -e + +BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +PROJECT_DIR=$(realpath ${BASE_DIR}/../../) + +echo PROJECT_DIR=${PROJECT_DIR} + +if [[ -z ${ANDROID_NDK_ROOT} ]]; then + echo "Please export ANDROID_NDK_ROOT" + exit 1 +fi + +ANDROID_ABI=arm64-v8a +ANDROID_PLATFORM=android-28 # Trace requires over android-23 + +echo ANDROID_NDK_ROOT=${ANDROID_NDK_ROOT} +echo ANDROID_ABI=${ANDROID_ABI} +echo ANDROID_PLATFORM=${ANDROID_PLATFORM} + +main() { + cd "$PROJECT_DIR" + local build_dir_root="build_samsung_android" + local example_root="examples/samsung" + local build_dir_example="$PROJECT_DIR/${build_dir_root}/${example_root}" + local cmake_prefix_path="$PROJECT_DIR/${build_dir_root}/lib/cmake/ExecuTorch;$PROJECT_DIR/${build_dir_root}/third-party/gflags;$PROJECT_DIR/${build_dir_root}/lib/cmake/tokenizers;$PROJECT_DIR/${build_dir_root}/lib/cmake/re2;$PROJECT_DIR/${build_dir_root}/lib/cmake/absl;" + + echo build_dir=${build_dir_root} + echo build_dir_example=${build_dir_example} + echo cmake_prefix_path=${cmake_prefix_path} + + cmake -DCMAKE_PREFIX_PATH=${cmake_prefix_path} \ + -DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK_ROOT/build/cmake/android.toolchain.cmake" \ + -DANDROID_NDK=$ANDROID_NDK \ + -DANDROID_ABI="$ANDROID_ABI" \ + -DANDROID_PLATFORM=$ANDROID_PLATFORM \ + -DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH \ + -DCMAKE_BUILD_TYPE=Release \ + -B"${build_dir_example}" \ + "$PROJECT_DIR/${example_root}" + cmake --build build_samsung_android/examples/samsung/ --config Release +} + +main "$@"