Skip to content

Commit a12193d

Browse files
authored
Merge pull request #2493 from annietllnd/updates
Update Android LlamaDemo LP
2 parents eefa097 + 7d209e2 commit a12193d

File tree

4 files changed

+93
-61
lines changed

4 files changed

+93
-61
lines changed

content/learning-paths/mobile-graphics-and-gaming/build-llama3-chat-android-app-using-executorch-and-xnnpack/1-dev-env-setup.md

Lines changed: 72 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,61 +13,109 @@ In this Learning Path, you will learn how to build and deploy a simple LLM-based
1313
The first step is to prepare a development environment with the required software:
1414

1515
- Android Studio (latest version recommended).
16-
- Android NDK version 28.0.12433566.
16+
- Android NDK version 28.0.12433566 or later.
1717
- Java 17 JDK.
1818
- Git.
1919
- Python 3.10 or later (these instructions have been tested with 3.10 and 3.12).
2020

2121
The instructions assume macOS with Apple Silicon, an x86 Debian, or an Ubuntu Linux machine, with at least 16GB of RAM.
2222

23-
## Install Android Studio and Android NDK
23+
## Install Java 17 JDK
24+
25+
Open the [Java SE 17 Archive Downloads](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html) page in your browser.
26+
27+
Select an appropriate download for your development machine operating system.
2428

25-
Follow these steps to install and configure Android Studio:
29+
Downloads are available for macOS as well as Linux.
2630

27-
1. Download and install the latest version of [Android Studio](https://developer.android.com/studio/).
31+
## Install and configure Android Studio
2832

29-
2. Start Android Studio and open the **Settings** dialog.
33+
Start by downloading and installing the latest version of Android Studio by navigating to the Downloads page:
3034

31-
3. Navigate to **Languages & Frameworks**, then **Android SDK**.
35+
```
36+
https://developer.android.com/studio/
37+
```
3238

33-
4. In the **SDK Platforms** tab, check **Android 14.0 ("UpsideDownCake")**.
39+
### For MacOS: Using UI
3440

35-
Next, install the specific version of the Android NDK that you require by first installing the Android command line tools:
41+
Follow these steps to configure Android Studio:
3642

37-
Linux:
43+
1. Start Android Studio and open the **Settings** dialog.
3844

45+
2. Navigate to **Languages & Frameworks**, then **Android SDK**.
46+
47+
3. In the **SDK Platforms** tab, check **Android 14.0 ("UpsideDownCake")**. Click **Apply** to install.
48+
49+
4. In the **SDK Tools** tab, check **NDK (Side by side)**. Click **Apply** to install.
50+
51+
Initiate the `ANDROID_HOME` environment variable:
52+
53+
```bash
54+
export ANDROID_HOME="$(realpath ~/Library/Android/sdk)"
3955
```
40-
curl https://dl.google.com/android/repository/commandlinetools-linux-11076708_latest.zip -o commandlinetools.zip
41-
```
4256

43-
macOS:
57+
### For Linux: Using the CLI
58+
59+
Command-line tools allow you to manage Android SDK components without the GUI. Create SDK directory and download command-line tools:
4460

61+
```bash
62+
mkdir -p ~/Android/cmdline-tools
63+
cd ~/Android/cmdline-tools
64+
wget https://dl.google.com/android/repository/commandlinetools-linux-10406996_latest.zip
4565
```
46-
curl https://dl.google.com/android/repository/commandlinetools-mac-11076708_latest.zip -o commandlinetools.zip
66+
67+
Unzip and move into the `cmdline-tools` directory.
68+
69+
```bash
70+
unzip commandlinetools-linux-*.zip
71+
mv cmdline-tools latest
4772
```
4873

49-
Unzip the Android command line tools:
74+
Initiate the `ANDROID_HOME` environment variable and add the `sdkmanager` to `PATH`:
5075

76+
```bash
77+
export ANDROID_HOME="~/Android"
78+
export PATH="$ANDROID_HOME/cmdline-tools/latest/bin:$ANDROID_HOME/platform-tools:$PATH"
5179
```
52-
unzip commandlinetools.zip -d android-sdk
80+
81+
The next step is to accept the license agreements. Press 'y', then 'Enter', as many times as prompted.
82+
83+
```bash
84+
sdkmanager --licenses
5385
```
5486

55-
Install the NDK in the same directory that Android Studio installed the SDK. This is generally `~/Library/Android/sdk` by default. Set the requirement environment variables:
87+
Finally, you can install the required Android SDK components:
5688

89+
```bash
90+
sdkmanager "platform-tools" \
91+
"platforms;android-34" \
92+
"build-tools;34.0.0" \
93+
"ndk;29.0.14206865"
5794
```
58-
export ANDROID_HOME="$(realpath ~/Library/Android/sdk)"
59-
export PATH=$ANDROID_HOME/cmdline-tools/bin/:$PATH
60-
sdkmanager --sdk_root="${ANDROID_HOME}" --install "ndk;28.0.12433566"
61-
export ANDROID_NDK=$ANDROID_HOME/ndk/28.0.12433566/
95+
96+
## Verify NDK installation
97+
98+
Verify by checking that the NDK was installed in the same directory that Android Studio installed the SDK.
99+
100+
{{% notice Default Path %}}
101+
On macOS, this is generally `~/Library/Android/sdk`, and on Linux, it's `~/Android/Sdk`. You should also update the command to use the installed NDK version.
102+
{{% /notice %}}
103+
104+
```bash
105+
ls $ANDROID_HOME
62106
```
63107

64-
## Install Java 17 JDK
108+
It should print the installed version, for example:
65109

66-
Open the [Java SE 17 Archive Downloads](https://www.oracle.com/java/technologies/javase/jdk17-archive-downloads.html) page in your browser.
110+
```output
111+
29.0.14206865
112+
```
67113

68-
Select an appropriate download for your development machine operating system.
114+
Set the required environment variable:
69115

70-
Downloads are available for macOS as well as Linux.
116+
```
117+
export ANDROID_NDK="$ANDROID_HOME/ndk/29.0.14206865/"
118+
```
71119

72120
## Install Git and cmake
73121

content/learning-paths/mobile-graphics-and-gaming/build-llama3-chat-android-app-using-executorch-and-xnnpack/2-executorch-setup.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ ExecuTorch is an end-to-end solution for enabling on-device inference capabiliti
1212

1313
The best practice is to generate an isolated Python environment in which to install your ExecuTorch dependencies. We provide instructions for both a Python virtual environment and a Conda virtual environment, but you only need one.
1414

15-
### Option 1: Create a Python virtual environment
15+
### Create a Python virtual environment
16+
17+
Use the `venv` module that is available through Python:
1618

1719
```bash
1820
python3.10 -m venv executorch-venv
@@ -21,24 +23,14 @@ source executorch-venv/bin/activate
2123

2224
The prompt of your terminal has `executorch` as a prefix to indicate the virtual environment is active.
2325

24-
### Option 2: Create a Conda virtual environment
25-
26-
Install Miniconda on your development machine by following the [Installing conda](https://conda.io/projects/conda/en/latest/user-guide/install/index.html) instructions.
27-
28-
Once `conda` is installed, create the environment:
29-
30-
```bash
31-
conda create -yn executorch-venv python=3.10.0
32-
conda activate executorch-venv
33-
```
34-
3526
### Clone ExecuTorch and install the required dependencies
3627

37-
From within the conda environment, run the commands below to download the ExecuTorch repository and install the required packages:
28+
From within the virtual environment, run the commands below to download the ExecuTorch repository and install the required packages:
3829

3930
``` bash
4031
git clone https://github.com/pytorch/executorch.git
4132
cd executorch
33+
git checkout release/1.0
4234
git submodule sync
4335
git submodule update --init --recursive
4436
./install_executorch.sh

content/learning-paths/mobile-graphics-and-gaming/build-llama3-chat-android-app-using-executorch-and-xnnpack/5-run-benchmark-on-android.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Use `cmake` to cross-compile ExecuTorch:
3232
cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
3333
-DANDROID_ABI=arm64-v8a \
3434
-DANDROID_PLATFORM=android-23 \
35+
-DEXECUTORCH_BUILD_EXTENSION_NAMED_DATA_MAP=ON \
3536
-DCMAKE_INSTALL_PREFIX=cmake-out-android \
3637
-DEXECUTORCH_ENABLE_LOGGING=1 \
3738
-DCMAKE_BUILD_TYPE=Release \
@@ -69,11 +70,7 @@ cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake \
6970
-DCMAKE_INSTALL_PREFIX=cmake-out-android \
7071
-DCMAKE_BUILD_TYPE=Release \
7172
-DPYTHON_EXECUTABLE=python \
72-
-DEXECUTORCH_BUILD_XNNPACK=ON \
7373
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
74-
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
75-
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
76-
-DSUPPORT_REGEX_LOOKAHEAD=ON \
7774
-DBUILD_TESTING=OFF \
7875
-Bcmake-out-android/examples/models/llama \
7976
examples/models/llama

content/learning-paths/mobile-graphics-and-gaming/build-llama3-chat-android-app-using-executorch-and-xnnpack/6-build-android-chat-app.md

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ weight: 7
66
layout: learningpathall
77
---
88

9+
In this section, you will use a Android demo application to demonstrate local inference with ExecuTorch.
10+
911
## Build the Android Archive (AAR)
10-
{{% notice Note %}}
11-
You can use the Android demo application included in ExecuTorch repository [LlamaDemo](https://github.com/pytorch/executorch/tree/main/examples/demo-apps/android/LlamaDemo) to demonstrate local inference with ExecuTorch.
12-
{{% /notice %}}
1312

1413
1. Open a terminal window and navigate to the root directory of the `executorch` repository.
1514

16-
2. Set the following environment variables:
15+
2. If you haven't already, set the following environment variables:
1716

1817
``` bash
19-
export ANDROID_NDK=$ANDROID_HOME/ndk/28.0.12433566/
18+
export ANDROID_NDK=$ANDROID_HOME/ndk/29.0.14206865/
2019
export ANDROID_ABI=arm64-v8a
2120
```
2221

@@ -25,21 +24,12 @@ You can use the Android demo application included in ExecuTorch repository [Llam
2524
Make sure you can confirm <path_to_android_ndk>/build/cmake/android.toolchain.cmake is available for CMake to cross-compile.
2625
{{% /notice %}}
2726

28-
3. Run the following commands to set up the required JNI library:
27+
3. Run the following command to set up the required JNI library:
2928

3029
``` bash
31-
pushd extension/android
32-
./gradlew build
33-
popd
34-
pushd examples/demo-apps/android/LlamaDemo
35-
./gradlew :app:setup
36-
popd
30+
sh scripts/build_android_library.sh
3731
```
3832

39-
{{% notice Note %}}
40-
This is running the shell script setup.sh which configures and builds the required core ExecuTorch, Llama, and Android libraries.
41-
{{% /notice %}}
42-
4333
## Getting models
4434

4535
Make sure the exported model and tokenizer are copied to the Android phone:
@@ -74,21 +64,26 @@ If the files are not on the device, use the device explorer to copy them.
7464
7565
## Build the Android Package Kit
7666
67+
Before starting, you need to obtain the example app by cloning `executorch-examples`:
68+
69+
```
70+
git clone https://github.com/meta-pytorch/executorch-examples.git
71+
```
72+
7773
### Option 1: Using Android Studio
7874
7975
This is the recommended option.
8076
81-
1. Open Android Studio and select **Open an existing Android Studio project** and navigate to open `examples/demo-apps/android/LlamaDemo`.
77+
1. Open Android Studio and select **Open an existing Android Studio project** and navigate to open `executorch-examples/llm/android/LlamaDemo`.
8278
8379
2. Run the app (^R). This builds and launches the app on the phone.
8480
8581
### Option 2: Command line
8682
87-
Without Android Studio UI, you can run gradle directly to build the app. You need to set up the Android SDK path and invoke gradle.
83+
Without Android Studio UI, you can run gradle directly to build the app. You need to set up the Android SDK path and invoke gradle. Navigate to the newly cloned `executorch-examples` repository.
8884
8985
``` bash
90-
export ANDROID_HOME=<path_to_android_sdk_home>
91-
pushd examples/demo-apps/android/LlamaDemo
86+
pushd llm/android/LlamaDemo
9287
./gradlew :app:installDebug
9388
popd
9489
```

0 commit comments

Comments
 (0)