Skip to content

Commit 18c0803

Browse files
authored
Samsung AI LiteCore - buildsystem: separate backend and example builds. (pytorch#22582)
### Summary To keep backend and example artifacts distinct. AS-IS: - The backend library and 'enn_executor_runner' were built together. - The example artifact 'enn_executor_runner' was placed under 'backend/samsung', it is not proper location for example artifacts. TO-BE: - Build the backend library first, then let the example projects refer to already-built backend library. - Adjust CMakeLists so that 'enn_executor_runner' is compiled in the 'examples/samsung/' directory and its output is placed in there. ### Test plan ${EXECUTORCH_ROOT}/backends/samsung/build.sh -b all cc @SS-JIA @digantdesai @kimishpatel --------- Signed-off-by: Jiseong.oh <jiseong.oh@samsung.coms> Signed-off-by: jiseong.oh <jiseong.oh@samsung.com>
1 parent 897fc8e commit 18c0803

8 files changed

Lines changed: 139 additions & 26 deletions

File tree

‎.ci/scripts/setup-samsung-linux-deps.sh‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ install_enn_backend() {
165165
echo "NDK will be installed/used at: ${ANDROID_NDK_ROOT}"
166166

167167
bash backends/samsung/build.sh --build all
168+
bash examples/samsung/build.sh
168169

169170
export EXECUTORCH_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
170171
export PYTHONPATH="${PYTHONPATH:-}:${EXECUTORCH_ROOT}/.."

‎backends/samsung/CMakeLists.txt‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,24 +146,6 @@ if(${ANDROID})
146146
executorch_target_link_options_shared_lib(enn_backend)
147147
target_compile_options(enn_backend PRIVATE -Wno-deprecated-declarations)
148148

149-
set(__enn_executor_runner_srcs
150-
${EXECUTORCH_SOURCE_DIR}/examples/samsung/executor_runner/enn_executor_runner.cpp
151-
)
152-
add_executable(enn_executor_runner ${__enn_executor_runner_srcs})
153-
add_dependencies(enn_executor_runner enn_backend)
154-
target_link_libraries(
155-
enn_executor_runner
156-
PRIVATE enn_logging
157-
enn_backend
158-
gflags
159-
executorch
160-
extension_data_loader
161-
portable_ops_lib
162-
android
163-
)
164-
set_target_properties(
165-
enn_executor_runner PROPERTIES CXX_VISIBILITY_PRESET hidden
166-
)
167149
install(
168150
TARGETS enn_backend enn_logging
169151
EXPORT ExecuTorchTargets

‎backends/samsung/README.md‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,16 @@ Generates python artifacts that allow user call `Compile` interface to lower a m
4949
./backends/samsung/build.sh -b x86_64
5050
```
5151

52-
### Build ENN Executor Runner
52+
### Build Backend Delegate
5353
```bash
5454
./backends/samsung/build.sh -b android --ndk ${ANDROID_NDK}
5555
```
56-
ANDROID_ABI=arm64-v8a is default, necessary runtime executable generated in `build_exynos_android` directory.
56+
ANDROID_ABI=arm64-v8a is default, necessary runtime backend library generated in `build_samsung_android` directory.
5757

58-
### Build Anroid Extension
58+
### Build Executable
59+
Please see the [README.md](../../examples/samsung/README.md).
60+
61+
### Build Android Extension
5962
This is later exposed Java app. Please turn on CMake option `EXECUTORCH_BUILD_ENN`, and ENN runtime will be added.
6063
```bash
6164
cmake extension/android \

‎backends/samsung/build.sh‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,17 @@ function build_android() {
6666
ANDROID_ABI=arm64-v8a
6767
ANDROID_PLATFORM=android-28 # Trace requires over android-23
6868

69+
local host_flatcc=${X86_64_BUILD_DIR}/third-party/flatcc_ep/bin/flatcc
70+
local flatcc_args=()
71+
if [[ -x ${host_flatcc} ]]; then
72+
flatcc_args=(-DFLATCC_EXECUTABLE=${host_flatcc})
73+
else
74+
echo "Warning: ${host_flatcc} not found. Build the x86_64 target first" \
75+
"('-b x86_64' or '-b all') if executor_runner fails to link libflatccrt.a."
76+
fi
77+
6978
cmake \
79+
"${flatcc_args[@]}" \
7080
-DCMAKE_INSTALL_PREFIX=${ANDROID_BUILD_DIR} \
7181
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK_ROOT}/build/cmake/android.toolchain.cmake" \
7282
-DANDROID_NDK=${ANDROID_NDK} \

‎backends/samsung/test/utils/runtime_executor.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_runner_path() -> Path:
2626
cwd=os.path.dirname(os.path.realpath(__file__)),
2727
text=True,
2828
).strip()
29-
return Path(git_root) / "build_samsung_android/backends/samsung/enn_executor_runner"
29+
return Path(git_root) / "build_samsung_android/examples/samsung/enn_executor_runner"
3030

3131

3232
class EDBTestManager:

‎examples/samsung/CMakeLists.txt‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright (c) 2025 Samsung Electronics Co. LTD
2+
# All rights reserved
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
cmake_minimum_required(VERSION 3.15)
8+
set(CMAKE_CXX_STANDARD 17)
9+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
10+
project(samsung_example)
11+
12+
# Source root directory for executorch.
13+
if(NOT EXECUTORCH_ROOT)
14+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..)
15+
endif()
16+
17+
include(${EXECUTORCH_ROOT}/tools/cmake/Utils.cmake)
18+
include(${EXECUTORCH_ROOT}/tools/cmake/Codegen.cmake)
19+
20+
if(NOT PYTHON_EXECUTABLE)
21+
resolve_python_executable()
22+
endif()
23+
24+
if(NOT CMAKE_BUILD_TYPE)
25+
set(CMAKE_BUILD_TYPE RelWithDebInfo)
26+
endif()
27+
28+
if(CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$")
29+
message(FATAL_ERROR "ios is not supported by Samsung AI system.")
30+
endif()
31+
32+
# prebuilt libraries. executorch package should contain portable_ops_lib,
33+
# etdump, bundled_program.
34+
find_package(executorch CONFIG REQUIRED)
35+
target_compile_options(executorch INTERFACE -DET_EVENT_TRACER_ENABLED)
36+
find_package(gflags REQUIRED)
37+
38+
set(_common_compile_options -Wno-deprecated-declarations -fPIC)
39+
40+
add_compile_options(-Wall -Werror -fPIC)
41+
42+
message("Build Samsung Android Examples")
43+
44+
set(__enn_executor_runner__srcs
45+
${CMAKE_CURRENT_LIST_DIR}/executor_runner/enn_executor_runner.cpp
46+
)
47+
48+
add_executable(enn_executor_runner ${__enn_executor_runner__srcs})
49+
50+
target_include_directories(enn_executor_runner PRIVATE ${EXECUTORCH_ROOT}/..)
51+
52+
target_compile_options(enn_executor_runner PRIVATE ${_common_compile_options})
53+
54+
target_link_libraries(
55+
enn_executor_runner PRIVATE enn_logging enn_backend gflags executorch
56+
extension_data_loader portable_ops_lib
57+
)
58+
59+
set_target_properties(
60+
enn_executor_runner PROPERTIES CXX_VISIBILITY_PRESET hidden
61+
)

‎examples/samsung/README.md‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Exynos backend Examples
1+
# Exynos Backend examples
22

33
This directory contains examples for some AI models.
44

5-
Please make sure you have built the library and executable before
6-
you start, if you have no idea how to build, please refer to [backend README](../../backends/samsung/README.md).
5+
Please make sure you have built the library before you start,
6+
if you have no idea how to build, please refer to [backend README](../../backends/samsung/README.md).
77

88
## Environment
99
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.
4242
If you want to use this mode on your model, verify your model on devicefarm which can use samsung developer society site
4343
firstly for checking stability. (https://soc-developer.semiconductor.samsung.com/)
4444

45+
## Building Executable
46+
### Prerequisites
47+
Please set up the backend before building the executable, See the [backend README](../../backends/samsung/README.md) for details.
48+
### Building 'enn_executor_runner' for Android
49+
```bash
50+
export EXYNOS_AI_LITECORE_ROOT=/path/to/enn_sdk
51+
export ANDROID_NDK_ROOT=/path/to/android_ndk
52+
${EXECUTORCH_ROOT}/examples/samsung/build.sh
53+
```
54+
After the build completes, `enn_executor_runner` can be found at `${EXECUTORCH_ROOT}/build_samsung_android/examples/samsung/`
4555
## Execution
4656

57+
4758
After lowering, we could get a pte model and then run it on mobile phone.
4859

4960
#### Step 1: Push required ENN libraries and executor runner to device
5061
```bash
5162
DEVICE_DIR=/data/local/tmp/executorch
5263
adb shell mkdir ${DEVICE_DIR}
53-
adb push ${EXECUTORCH_ROOT}/cmake-android-out/backends/samsung/enn_executor_runner ${DEVICE_DIR}
64+
adb push ${EXECUTORCH_ROOT}/build_samsung_android/examples/samsung/enn_executor_runner ${DEVICE_DIR}
5465
```
5566

5667
#### Step 2: Indicate dynamic linkers and execute model

‎examples/samsung/build.sh‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
BASE_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
6+
PROJECT_DIR=$(realpath ${BASE_DIR}/../../)
7+
8+
echo PROJECT_DIR=${PROJECT_DIR}
9+
10+
if [[ -z ${ANDROID_NDK_ROOT} ]]; then
11+
echo "Please export ANDROID_NDK_ROOT"
12+
exit 1
13+
fi
14+
15+
ANDROID_ABI=arm64-v8a
16+
ANDROID_PLATFORM=android-28 # Trace requires over android-23
17+
18+
echo ANDROID_NDK_ROOT=${ANDROID_NDK_ROOT}
19+
echo ANDROID_ABI=${ANDROID_ABI}
20+
echo ANDROID_PLATFORM=${ANDROID_PLATFORM}
21+
22+
main() {
23+
cd "$PROJECT_DIR"
24+
local build_dir_root="build_samsung_android"
25+
local example_root="examples/samsung"
26+
local build_dir_example="$PROJECT_DIR/${build_dir_root}/${example_root}"
27+
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;"
28+
29+
echo build_dir=${build_dir_root}
30+
echo build_dir_example=${build_dir_example}
31+
echo cmake_prefix_path=${cmake_prefix_path}
32+
33+
cmake -DCMAKE_PREFIX_PATH=${cmake_prefix_path} \
34+
-DCMAKE_TOOLCHAIN_FILE="$ANDROID_NDK_ROOT/build/cmake/android.toolchain.cmake" \
35+
-DANDROID_NDK=$ANDROID_NDK \
36+
-DANDROID_ABI="$ANDROID_ABI" \
37+
-DANDROID_PLATFORM=$ANDROID_PLATFORM \
38+
-DCMAKE_FIND_ROOT_PATH_MODE_PACKAGE=BOTH \
39+
-DCMAKE_BUILD_TYPE=Release \
40+
-B"${build_dir_example}" \
41+
"$PROJECT_DIR/${example_root}"
42+
cmake --build build_samsung_android/examples/samsung/ --config Release
43+
}
44+
45+
main "$@"

0 commit comments

Comments
 (0)