From 0626adf4f49f967427b7db222ed50e736315e8ae Mon Sep 17 00:00:00 2001 From: Hansong <107070759+kirklandsign@users.noreply.github.com> Date: Thu, 10 Apr 2025 15:49:32 -0700 Subject: [PATCH] Update LlamaDemo AAR build docs (#10086) (cherry picked from commit 57ae20e0d6a684ea26150567ab7d70b26e183aa6) --- .../docs/delegates/mediatek_README.md | 22 ++++++++++- .../docs/delegates/qualcomm_README.md | 38 +++++++++++-------- .../docs/delegates/xnnpack_README.md | 31 ++++++++------- 3 files changed, 61 insertions(+), 30 deletions(-) diff --git a/examples/demo-apps/android/LlamaDemo/docs/delegates/mediatek_README.md b/examples/demo-apps/android/LlamaDemo/docs/delegates/mediatek_README.md index dabe0b6dc6d..cc05302ce78 100644 --- a/examples/demo-apps/android/LlamaDemo/docs/delegates/mediatek_README.md +++ b/examples/demo-apps/android/LlamaDemo/docs/delegates/mediatek_README.md @@ -126,13 +126,31 @@ The Mediatek runner (`examples/mediatek/executor_runner/mtk_llama_runner.cpp`) c ## Build AAR Library +1. Open a terminal window and navigate to the root directory of the executorch +2. Set the following environment variables: +```sh +export ANDROID_NDK= +export ANDROID_ABIS=arm64-v8a +export NEURON_BUFFER_ALLOCATOR_LIB= +``` +*Note: is the root for the NDK, which is usually under ~/Library/Android/sdk/ndk/XX.Y.ZZZZZ for macOS, and contains NOTICE and README.md. We use /build/cmake/android.toolchain.cmake for CMake to cross-compile.* -Next we need to build and compile the MediaTek backend and MediaTek Llama runner. By setting `NEURON_BUFFER_ALLOCATOR_LIB`, the script will build the MediaTek backend. +3. Create a directory to hold the AAR +```sh +mkdir -p aar-out +export BUILD_AAR_DIR=aar-out ``` + +4. Run the following command to build the AAR: +```sh sh scripts/build_android_library.sh ``` -**Output**: This will generate an .aar file that is already imported into the expected directory for the Android app. It will live in `examples/demo-apps/android/Llamademo/app/libs`. +5. Copy the AAR to the app: +```sh +mkdir -p examples/demo-apps/android/LlamaDemo/app/libs +cp aar-out/executorch.aar examples/demo-apps/android/LlamaDemo/app/libs/executorch.aar +``` If you were to unzip the .aar file or open it in Android Studio, verify it contains the following related to MediaTek backend: * libneuron_buffer_allocator.so diff --git a/examples/demo-apps/android/LlamaDemo/docs/delegates/qualcomm_README.md b/examples/demo-apps/android/LlamaDemo/docs/delegates/qualcomm_README.md index efb2f797614..05e6cc8f409 100644 --- a/examples/demo-apps/android/LlamaDemo/docs/delegates/qualcomm_README.md +++ b/examples/demo-apps/android/LlamaDemo/docs/delegates/qualcomm_README.md @@ -178,30 +178,38 @@ adb push tokenizer.bin /data/local/tmp/llama ## Build AAR Library -Open a terminal window and navigate to the root directory of the executorch. +1. Open a terminal window and navigate to the root directory of the executorch Set the following environment variables: -``` +```sh export ANDROID_NDK= -export ANDROID_ABI=arm64-v8a -``` -Note: is the root for the NDK, which is usually under ~/Library/Android/sdk/ndk/XX.Y.ZZZZZ for macOS, and contains NOTICE and README.md. We use /build/cmake/android.toolchain.cmake for CMake to cross-compile. -Build the Android Java extension code: +export ANDROID_ABIS=arm64-v8a +export QNN_SDK_ROOT= ``` -pushd extension/android -./gradlew build -popd + +*Note: is the root for the NDK, which is usually under ~/Library/Android/sdk/ndk/XX.Y.ZZZZZ for macOS, and contains NOTICE and README.md. We use /build/cmake/android.toolchain.cmake for CMake to cross-compile.* + +3. Create a directory to hold the AAR +```sh +mkdir -p aar-out +export BUILD_AAR_DIR=aar-out ``` -Run the following command set up the required JNI library: + +4. Run the following command to build the AAR: +```sh +sh scripts/build_android_library.sh ``` -pushd examples/demo-apps/android/LlamaDemo -./gradlew :app:setupQnn -popd + +5. Copy the AAR to the app: +```sh +mkdir -p examples/demo-apps/android/LlamaDemo/app/libs +cp aar-out/executorch.aar examples/demo-apps/android/LlamaDemo/app/libs/executorch.aar ``` + Alternative you can also just run the shell script directly as in the root directory: -``` +```sh sh examples/demo-apps/android/LlamaDemo/setup-with-qnn.sh ``` -This is running the shell script which configures the required core ExecuTorch, Llama2/3, and Android libraries, builds them, and copies them to jniLibs. +This is running the shell script which configures the required core ExecuTorch, Llama2/3, and Android libraries, builds them into AAR, and copies it to the app. Note: If you are building the Android app mentioned in the next section on a separate machine (i.e. MacOS but building and exporting for QNN backend on Linux), make sure you copy the aar file generated from setup-with-qnn script to “examples/demo-apps/android/LlamaDemo/app/libs” before building the Android app. diff --git a/examples/demo-apps/android/LlamaDemo/docs/delegates/xnnpack_README.md b/examples/demo-apps/android/LlamaDemo/docs/delegates/xnnpack_README.md index a94dd6a44c1..430db30eb66 100644 --- a/examples/demo-apps/android/LlamaDemo/docs/delegates/xnnpack_README.md +++ b/examples/demo-apps/android/LlamaDemo/docs/delegates/xnnpack_README.md @@ -132,30 +132,35 @@ adb push tokenizer.bin /data/local/tmp/llama ## Build AAR Library 1. Open a terminal window and navigate to the root directory of the executorch 2. Set the following environment variables: -``` +```sh export ANDROID_NDK= -export ANDROID_ABI=arm64-v8a +export ANDROID_ABIS=arm64-v8a ``` *Note: is the root for the NDK, which is usually under ~/Library/Android/sdk/ndk/XX.Y.ZZZZZ for macOS, and contains NOTICE and README.md. We use /build/cmake/android.toolchain.cmake for CMake to cross-compile.* -3. Build the Android Java extension code: -``` -pushd extension/android -./gradlew build -popd +3. Create a directory to hold the AAR +```sh +mkdir -p aar-out +export BUILD_AAR_DIR=aar-out ``` -4. Run the following command set up the required JNI library: + +4. Run the following command to build the AAR: +```sh +sh scripts/build_android_library.sh ``` -pushd examples/demo-apps/android/LlamaDemo -./gradlew :app:setup -popd + +5. Copy the AAR to the app: +```sh +mkdir -p examples/demo-apps/android/LlamaDemo/app/libs +cp aar-out/executorch.aar examples/demo-apps/android/LlamaDemo/app/libs/executorch.aar ``` + Alternative you can also just run the shell script directly as in the root directory: -``` +```sh sh examples/demo-apps/android/LlamaDemo/setup.sh ``` -This is running the shell script which configures the required core ExecuTorch, Llama2/3, and Android libraries, builds them, and copies them to jniLibs. +This is running the shell script which configures the required core ExecuTorch, Llama2/3, and Android libraries, builds them into AAR, and copies it to the app. **Output**: The executorch.aar file will be generated in a newly created folder in the example/demo-apps/android/LlamaDemo/app/libs directory. This is the path that the Android app expects it to be in.