-
Notifications
You must be signed in to change notification settings - Fork 685
Exynos Backend for Executorch to bring up on Exynos SoC #13677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9ab662c
24e6dd9
6f5720c
57579e5
15f6812
d547835
99313fd
aa31232
66bd9ea
5fa3dd3
ac7119d
385711b
0bc88fd
28f9dab
c46b081
96ea729
407b23a
1e41b20
05f1a44
83b7c6c
ed1112e
613b1d6
07d9c61
6870437
b4443e8
b36a000
2ee20e3
bff032f
b892967
b4ce9f3
9eb7a50
4f28bad
2b3bc79
40a27e3
527440c
95d8f3f
7f5a06b
9adac0c
bad80e0
ac315c2
602f714
02e347e
35fd1f3
f6b1025
4994e6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/bin/bash | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# Copyright (c) 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. | ||
|
||
set -ex | ||
|
||
|
||
download_ai_lite_core() { | ||
API_BASE="https://soc-developer.semiconductor.samsung.com/api/v1/resource/ai-litecore/download" | ||
API_KEY="kn10SoSY3hkC-9Qny5TqD2mnqVrlupv3krnjLeBt5cY" | ||
|
||
VERSION="0.5" | ||
OS_NAME="Ubuntu 22.04" | ||
OUT_FILE="/tmp/exynos-ai-litecore-v${VERSION}.tar.gz" | ||
TARGET_PATH="/tmp/exynos_ai_lite_core" | ||
|
||
mkdir -p ${TARGET_PATH} | ||
# Presigned issue URL | ||
JSON_RESP=$(curl -sS -G \ | ||
--location --fail --retry 3 \ | ||
-H "apikey: ${API_KEY}" \ | ||
--data-urlencode "version=${VERSION}" \ | ||
--data-urlencode "os=${OS_NAME}" \ | ||
"${API_BASE}") | ||
|
||
DOWNLOAD_URL=$(echo "$JSON_RESP" | sed -n 's/.*"data":[[:space:]]*"\([^"]*\)".*/\1/p') | ||
|
||
if [[ -z "$DOWNLOAD_URL" ]]; then | ||
echo "Failed to extract download URL" | ||
echo "$JSON_RESP" | ||
exit 1 | ||
fi | ||
|
||
# Download LiteCore | ||
curl -sS -L --fail --retry 3 \ | ||
--output "$OUT_FILE" \ | ||
"$DOWNLOAD_URL" | ||
|
||
echo "Download done: $OUT_FILE" | ||
|
||
|
||
tar -C "${TARGET_PATH}" --strip-components=1 -xzvf "${OUT_FILE}" | ||
|
||
export EXYNOS_AI_LITECORE_ROOT=${TARGET_PATH} | ||
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:-}:${EXYNOS_AI_LITECORE_ROOT}/lib/x86_64-linux | ||
} | ||
|
||
install_enn_backend() { | ||
NDK_INSTALLATION_DIR=/opt/ndk | ||
rm -rf "${NDK_INSTALLATION_DIR}" && sudo mkdir -p "${NDK_INSTALLATION_DIR}" | ||
ANDROID_NDK_VERSION=r27b | ||
|
||
pushd . | ||
cd /tmp | ||
curl -Os --retry 3 "https://ossci-android.s3.amazonaws.com/android-ndk-${ANDROID_NDK_VERSION}-linux.zip" | ||
unzip -qo "android-ndk-${ANDROID_NDK_VERSION}-linux.zip" | ||
|
||
# Print the content for manual verification | ||
ls -lah "android-ndk-${ANDROID_NDK_VERSION}" | ||
sudo mv "android-ndk-${ANDROID_NDK_VERSION}"/* "${NDK_INSTALLATION_DIR}" | ||
popd | ||
# build Exynos backend | ||
export ANDROID_NDK_ROOT=${ANDROID_NDK_ROOT:-/opt/ndk} | ||
bash backends/samsung/build.sh --build all | ||
# set env variable | ||
export EXECUTORCH_ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd)" | ||
export PYTHONPATH=${PYTHONPATH:-}:${EXECUTORCH_ROOT}/.. | ||
} | ||
|
||
AI_LITE_CORE_VERSION=0.5.0 | ||
|
||
download_ai_lite_core ${AI_LITE_CORE_VERSION} | ||
install_enn_backend |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -571,6 +571,11 @@ if(EXECUTORCH_BUILD_QNN) | |
list(APPEND _executorch_backends qnn_executorch_backend) | ||
endif() | ||
|
||
if(EXECUTORCH_BUILD_ENN) | ||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/samsung) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add the backend target to _executorch_backends (similar to line 585)? This will allow it to be included in the executorch_kernels target. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. I add enn_backend to _executorch_backends. |
||
list(APPEND _executorch_backends enn_backend) | ||
endif() | ||
|
||
if(EXECUTORCH_BUILD_XNNPACK) | ||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/xnnpack) | ||
list(APPEND _executorch_backends xnnpack_backend) | ||
|
@@ -817,6 +822,10 @@ if(EXECUTORCH_BUILD_PYBIND) | |
list(APPEND _dep_libs qnn_executorch_backend) | ||
endif() | ||
|
||
if(EXECUTORCH_BUILD_ENN) | ||
list(APPEND _dep_libs enn_backend) | ||
endif() | ||
|
||
if(EXECUTORCH_BUILD_XNNPACK) | ||
# need to explicitly specify XNNPACK and xnnpack-microkernels-prod here | ||
# otherwise uses XNNPACK and microkernel-prod symbols from libtorch_cpu | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
# 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) | ||
|
||
get_filename_component( | ||
EXECUTORCH_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}/../.." ABSOLUTE | ||
) | ||
|
||
if(NOT DEFINED EXYNOS_AI_LITECORE_ROOT) | ||
message( | ||
FATAL_ERROR | ||
"Please define EXYNOS_AI_LIRECORE_PATH by adding cmake parameter -DEXYNOS_AI_LITECORE_ROOT=<...>" | ||
) | ||
endif() | ||
if(CMAKE_TOOLCHAIN_FILE MATCHES ".*(iOS|ios\.toolchain)\.cmake$") | ||
message(FATAL_ERROR "IOS is not supported on Exynos.") | ||
endif() | ||
|
||
if(NOT FLATC_EXECUTABLE) | ||
set(FLATC_EXECUTABLE flatc) | ||
endif() | ||
|
||
add_compile_options(-Wall -Werror -fPIC) | ||
if(CMAKE_BUILD_TYPE STREQUAL "Release") | ||
# strip symbols | ||
add_link_options("-s") | ||
# hide dynamic symbols | ||
set(CMAKE_C_VISIBILITY_PRESET hidden) | ||
set(CMAKE_CXX_VISIBILITY_PRESET hidden) | ||
add_definitions(-DNDEBUG) | ||
endif() | ||
|
||
include_directories( | ||
${EXECUTORCH_SOURCE_DIR}/.. | ||
${EXECUTORCH_SOURCE_DIR}/runtime/core/portable_type/c10 | ||
${EXYNOS_AI_LITECORE_ROOT} | ||
) | ||
add_compile_definitions(C10_USING_CUSTOM_GENERATED_MACROS) | ||
|
||
if(${ANDROID}) | ||
find_library(android_log log) | ||
endif() | ||
|
||
# add logging library | ||
add_library(enn_logging STATIC) | ||
|
||
if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll likely want to build and install the python extensions as part of the install logic in setup.py. It will ensure that the extensions are placed in the proper python path and work out of box when enabled. That could be done as a follow-up. I'm looking at refactoring the QNN backend to do something similar and we can follow that pattern once merged. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for sugestion. Once the behavior for the models for Exynos backend is complete, we will consider reflecting that Refactoring feature. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created an issue to track here as a follow-up PR after this one lands: #14000 |
||
add_subdirectory( | ||
${EXECUTORCH_SOURCE_DIR}/third-party/pybind11 | ||
${CMAKE_CURRENT_BINARY_DIR}/pybind11 | ||
) | ||
add_library(PyEnnWrapperAdaptor MODULE) | ||
|
||
find_library( | ||
GG_API_LIB | ||
NAMES graphgen_api | ||
HINTS ${EXYNOS_AI_LITECORE_ROOT}/lib/x86_64-linux | ||
) | ||
add_library(graphgen_api SHARED IMPORTED GLOBAL) | ||
set_target_properties( | ||
graphgen_api | ||
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES | ||
"${EXYNOS_AI_LITECORE_ROOT}/include" IMPORTED_LOCATION | ||
"${GG_API_LIB}" | ||
) | ||
|
||
set(_enn_compile_options_schema | ||
${CMAKE_CURRENT_SOURCE_DIR}/serialization/compile_options_def.fbs | ||
) | ||
|
||
set(_enn_schema_generate_dir | ||
"${CMAKE_BINARY_DIR}/schema/include/executorch/backends/samsung" | ||
) | ||
# Paths to headers generated from the .fbs files. | ||
string(REGEX REPLACE "serialization/([^/]+)[.]fbs$" "\\1_generated.h" | ||
generated_header "${fbs_file}" | ||
) | ||
set(_enn_schema_output "${_enn_schema_generate_dir}/${generated_header}") | ||
|
||
# Generate the headers from the .fbs files. | ||
add_custom_command( | ||
OUTPUT ${_enn_schema_output} | ||
COMMAND ${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --scoped-enums -o | ||
"${_enn_schema_generate_dir}" ${_enn_compile_options_schema} | ||
DEPENDS ${_enn_compile_options_schema} | ||
WORKING_DIRECTORY ${EXECUTORCH_SOURCE_DIR} | ||
COMMENT "Generating enn compile options headers" | ||
VERBATIM | ||
) | ||
add_custom_target( | ||
enn_compile_options_output ALL DEPENDS ${_enn_schema_output} | ||
) | ||
|
||
set_target_properties( | ||
PyEnnWrapperAdaptor PROPERTIES CXX_VISIBILITY_PRESET hidden | ||
) | ||
target_link_libraries( | ||
PyEnnWrapperAdaptor PRIVATE pybind11::module pybind11::lto graphgen_api | ||
enn_logging | ||
) | ||
target_include_directories( | ||
PyEnnWrapperAdaptor BEFORE | ||
PRIVATE ${CMAKE_BINARY_DIR}/schema/include | ||
${EXECUTORCH_SOURCE_DIR}/third-party/flatbuffers/include | ||
) | ||
add_dependencies(PyEnnWrapperAdaptor enn_compile_options_output) | ||
pybind11_extension(PyEnnWrapperAdaptor) | ||
|
||
# PyGraphWrapperAdaptor | ||
add_library(PyGraphWrapperAdaptor MODULE) | ||
# | ||
find_library( | ||
GRAPH_WRAPPER_LIB | ||
NAMES graph_wrapper | ||
HINTS ${EXYNOS_AI_LITECORE_ROOT}/lib/x86_64-linux | ||
) | ||
add_library(graph_wrapper SHARED IMPORTED GLOBAL) | ||
set_target_properties( | ||
graph_wrapper | ||
PROPERTIES INTERFACE_INCLUDE_DIRECTORIES | ||
"${EXYNOS_AI_LITECORE_ROOT}/include" IMPORTED_LOCATION | ||
"${GRAPH_WRAPPER_LIB}" | ||
) | ||
set_target_properties( | ||
PyGraphWrapperAdaptor PROPERTIES CXX_VISIBILITY_PRESET hidden | ||
) | ||
target_link_libraries( | ||
PyGraphWrapperAdaptor PRIVATE pybind11::module pybind11::lto graph_wrapper | ||
enn_logging | ||
) | ||
pybind11_extension(PyGraphWrapperAdaptor) | ||
|
||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/aot) | ||
endif() | ||
|
||
if(${ANDROID}) | ||
target_link_libraries(enn_logging PRIVATE ${android_log}) | ||
add_library(enn_backend STATIC) | ||
target_link_libraries(enn_backend PRIVATE enn_logging) | ||
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 | ||
) | ||
set_target_properties( | ||
enn_executor_runner PROPERTIES CXX_VISIBILITY_PRESET hidden | ||
) | ||
install( | ||
TARGETS enn_backend enn_logging | ||
EXPORT ExecuTorchTargets | ||
DESTINATION lib | ||
) | ||
endif() | ||
|
||
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/runtime) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# ExecuTorch Samsung Exynos Delegate | ||
mergennachin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The subtree contains Exynos delegate implementation for ExecuTorch. The target of delegation | ||
is deploying torch model to run with exynos NPU/DSP. | ||
|
||
This backend is implemented on the top of [EXYNOS_LITECORE](https://soc-developer.semiconductor.samsung.com/global/development/ai-litecore) | ||
Please prepare the SDK before you start, it is important to code compilation and runtime. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How do we do that? Is there a documentation? Or clear readme? Ideal path is for us to have exynos_litecore as pip package so that you can just do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we'll consider installing the LiteCore library using pip later. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created an issue to track: #14004 |
||
|
||
## Delegate Options | ||
|
||
### Supported Chipset | ||
- Exynos 2500 (E9955) | ||
|
||
### Supported Inference Type | ||
- Quantized (i8/u8/i16/u16) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about int4? is it under dev? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, right. it is under dev now. if it's prepared, we will add int4. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Created an issue to track: #14003 |
||
- FP16 | ||
|
||
## Directory Structure | ||
|
||
``` | ||
backends/samsung | ||
├── aot # Codes for generating binary buffer for ENN runtime. | ||
├── builders # Codes for lowering each operators. | ||
├── partition # ENN Partitioner. | ||
├── passes # Various passes helping lower models to ENN backend. | ||
├── python # Places to put pybind artifacts for accessing samsung libraries. | ||
├── runtime # ENN runtime for executing lowered models. | ||
├── scripts # Misc supporting scripts, not related to core functionality. | ||
└── serialization # Codes for building Graph IR for Exynos and serializing. | ||
examples | ||
└── samsung # Examples to run ENN backends. | ||
``` | ||
|
||
## How to build | ||
Please download Exynos AI LiteCore, and set the root path of SDK directory to `EXYNOS_AI_LITECORE_ROOT`.</br> | ||
Please navigate to [Android NDK](https://developer.android.com/ndk) and download a version of NDK. | ||
`ANDROID_NDK` refers the root path of NDK directory.</br> | ||
|
||
### Set up environment variables | ||
```bash | ||
export LD_LIBRARY_PATH=${EXYNOS_AI_LITECORE_ROOT}/lib/x86_64-linux/ | ||
``` | ||
|
||
### Build AOT Targets | ||
Generates python artifacts that allow user call `Compile` interface to lower a model to Exynos backend in python script. | ||
```bash | ||
./backends/samsung/build.sh -b x86_64 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you wanna add a basic end-to-end integration test to our CI? Can we run everything in x86 environment by any chance? See example test cases in https://github.com/pytorch/executorch/blob/main/.github/workflows/pull.yml (runs on every PR before merging with main) and https://github.com/pytorch/executorch/blob/main/.github/workflows/trunk.yml (jobs that runs after the PR merges with main) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add e2e test to your CI after all codes which is developed are merged? and i think you also need to get samsung device for testing CI. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
yeah, we can look into it. also, is there an emulator on linux (either x86 or aarch64) or mac for exynos? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think we should add basic unit test, at least for ahead-of-time stuff such as partitioner and serialization. i suppose they won't require samsung phones, right? for e2e, we can do it after the PR lands as a fast-follow-on. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Unfortunately, we don't have the emulator. Could we discuss on slack for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
you mean we are going to test only until the PTE is generated, right? if yes, right it doesn't need to require samsung device. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For e2e integration test, created an issue to track: #14002 |
||
``` | ||
|
||
### Build ENN Executor Runner | ||
```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. | ||
|
||
### Build Anroid 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 \ | ||
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \ | ||
-DANDROID_ABI="${ANDROID_ABI}" \ | ||
-DCMAKE_INSTALL_PREFIX=cmake-android-out \ | ||
-Bcmake-android-out/extension/android | ||
Comment on lines
+59
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cc @Jiseong-oh please take a look at Kimish's question There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed it. |
||
|
||
cmake --build cmake-android-out/extension/android -j8 | ||
``` | ||
|
||
## Examples | ||
python -m executorch.examples.samsung.aot_compiler --chipset e9955 -m ic3 | ||
|
||
Please see this [README.md](../../examples/samsung/README.md). |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have another docker image that already contains android ndk so that you don't have do download and install.
You'll have faster CI turnaround time. Here's how the docker image is configured
executorch/.github/workflows/docker-builds.yml
Line 43 in cec1400
executorch/.ci/docker/build.sh
Lines 59 to 64 in cec1400
executorch/.ci/docker/ubuntu/Dockerfile
Lines 79 to 83 in cec1400
executorch/.ci/docker/common/install_android.sh
Line 4 in cec1400
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could i try to change the docker next pr? we can test to build with our SDK on this docker after this pr is merged.
Actually, we are preparing to change to executorch-ubuntu-22.04-clang12-android.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, please create an issue into the board so that we can track ExecuTorch Samsung (view)