Skip to content

Commit 10a1055

Browse files
authored
Android add support for bpe and tiktoken (#897)
Add the prebuilt library for either tokenizer, and update docs
1 parent ceb9a3a commit 10a1055

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

README.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ source .venv/bin/activate
5454
```
5555
[skip default]: end
5656

57-
[shell default]: ./install_requirements.sh
57+
[shell default]: ./install_requirements.sh
5858

5959
Installations can be tested by
6060

@@ -269,17 +269,33 @@ The following assumes you've completed the steps for [Setting up ExecuTorch](#se
269269

270270
### Deploy and run on Android
271271

272-
#### Approach 1: Android Studio
272+
#### Approach 1 (Recommended): Android Studio
273273

274274
If you have Android Studio set up, and you have Java 17 and Android SDK 34 configured, you can follow this step.
275275

276-
First, you need to download the following AAR file which contains the required Java library and its corresponding JNI library, for the app to build and run. You need to put the file to `android/Torchchat/app/libs/executorch.aar`
276+
First, you need to download the ones of the following AAR files which contains the required Java library and its corresponding JNI library, for the app to build and run. You need to put the file to `android/Torchchat/app/libs/executorch.aar`
277277

278-
[executorch-llama-torchchat.aar](https://ossci-android.s3.us-west-1.amazonaws.com/executorch/release/0.2/executorch-llama-torchchat.aar) (SHASUM: e19ffb15aa3f1b1281de66dd6f71c9a332a82b92)
278+
If your model uses BPE tokenizer (llama2 model for example), download `executorch-llama-torchchat-bpe.aar`.
279279

280+
If your model uses tiktoken tokenizer (llama3 model for example), download `executorch-llama-torchchat-tiktoken.aar`.
281+
282+
Currently the tokenizer is built at compile time, so you need to re-build the app when you need to use a different tokenizer for different model.
283+
284+
NOTE: The script to build the AAR can be found [here](https://github.com/pytorch/executorch/blob/main/build/build_android_library.sh). If you need to tweak with the tokenizer or runtime (for example use your own tokenizer or runtime library), you can modify the ExecuTorch code and use that script to build the AAR library.
285+
286+
[executorch-llama-torchchat-bpe.aar](https://ossci-android.s3.amazonaws.com/executorch/release/0.3/executorch-llama-bpe-rc1.aar) (SHASUM: 673af4a1338a93d47369b68ec0d52b8ea7f983a2)
287+
[executorch-llama-torchchat-tiktoken.aar](https://ossci-android.s3.amazonaws.com/executorch/release/0.3/executorch-llama-tiktoken-rc1.aar) (SHASUM: 575190205dbb1ee932a277b50520dc4260a9a9cf)
288+
289+
For BPE tokenizer:
290+
```
291+
curl https://ossci-android.s3.amazonaws.com/executorch/release/0.3/executorch-llama-bpe-rc1.aar -o android/Torchchat/app/libs/executorch.aar --create-dirs
292+
echo "673af4a1338a93d47369b68ec0d52b8ea7f983a2 android/Torchchat/app/libs/executorch.aar" | shasum --check
293+
```
294+
295+
For tiktoken tokenizer:
280296
```
281-
curl https://ossci-android.s3.us-west-1.amazonaws.com/executorch/release/0.2/executorch-llama-torchchat.aar -o android/Torchchat/app/libs/executorch.aar --create-dirs
282-
echo "e19ffb15aa3f1b1281de66dd6f71c9a332a82b92 android/Torchchat/app/libs/executorch.aar" | shasum --check
297+
curl https://ossci-android.s3.amazonaws.com/executorch/release/0.3/executorch-llama-tiktoken-rc1.aar -o android/Torchchat/app/libs/executorch.aar --create-dirs
298+
echo "575190205dbb1ee932a277b50520dc4260a9a9cf android/Torchchat/app/libs/executorch.aar" | shasum --check
283299
```
284300

285301
You also need to push the model and tokenizer file to your device. Please refer to the docs above on generating the pte and bin file, or use E2E script (see section below) to generate and push the file.
@@ -300,10 +316,11 @@ Now, follow the app's UI guidelines to pick the model and tokenizer files from t
300316
301317
#### Approach 2: E2E Script
302318
303-
Alternatively, you can run `scripts/android_example.sh` which sets up Java, Android SDK Manager, Android SDK, Android emulator, builds the app, and launches it for you.
319+
Alternatively, you can run `scripts/android_example.sh` which sets up Java, Android SDK Manager, Android SDK, Android emulator (if no physical device is found), builds the app, and launches it for you. It can be used if you don't have a GUI.
304320

305321
```
306322
export TORCHCHAT_ROOT=$(pwd)
323+
export USE_TIKTOKEN=ON # Set this only for tiktoken tokenizer
307324
sh scripts/android_example.sh
308325
```
309326

scripts/android_example.sh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ else
3030
exit -1
3131
fi
3232

33-
LLAMA_AAR_URL="https://ossci-android.s3.us-west-1.amazonaws.com/executorch/release/0.2/executorch-llama-torchchat.aar"
34-
35-
LLAMA_AAR_SHASUM="e19ffb15aa3f1b1281de66dd6f71c9a332a82b92"
33+
if [ "${USE_TIKTOKEN:-OFF}" == "ON" ]; then
34+
LLAMA_AAR_URL="https://ossci-android.s3.amazonaws.com/executorch/release/0.3/executorch-llama-tiktoken-rc1.aar"
35+
LLAMA_AAR_SHASUM="575190205dbb1ee932a277b50520dc4260a9a9cf"
36+
else
37+
LLAMA_AAR_URL="https://ossci-android.s3.amazonaws.com/executorch/release/0.3/executorch-llama-bpe-rc1.aar"
38+
LLAMA_AAR_SHASUM="673af4a1338a93d47369b68ec0d52b8ea7f983a2"
39+
fi
3640

3741
mkdir -p ${TORCHCHAT_ROOT}/build/android
3842

@@ -65,6 +69,7 @@ setup_android_sdk_manager() {
6569
fi
6670
pushd ${TORCHCHAT_ROOT}/build/android
6771
mkdir -p sdk/cmdline-tools/latest
72+
rm -r sdk/cmdline-tools/latest/*
6873

6974
echo "Download Android SDK Manager"
7075
curl "${SDK_MANAGER_URL}" -o commandlinetools.zip

0 commit comments

Comments
 (0)