Skip to content

Commit 614b35c

Browse files
kirklandsignkeyprocedure
authored andcommitted
Improve android instrumentation test experience and add docs (pytorch#9989)
Add README for Android Make instrumentation test easier for users on their local development.
1 parent fa07c0d commit 614b35c

File tree

7 files changed

+83
-23
lines changed

7 files changed

+83
-23
lines changed

.ci/scripts/build_android_instrumentation.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.

.github/workflows/_android.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ jobs:
3636
cp ${BUILD_AAR_DIR}/executorch.aar $ARTIFACTS_DIR_NAME
3737
3838
mkdir -p ${ARTIFACTS_DIR_NAME}/library_test_dir
39-
bash .ci/scripts/build_android_instrumentation.sh
39+
bash extension/android/executorch_android/android_test_setup.sh
40+
(cd extension/android; ANDROID_HOME="${ANDROID_SDK:-/opt/android/sdk}" ./gradlew :executorch_android:assembleAndroidTest)
4041
cp extension/android/executorch_android/build/outputs/apk/androidTest/debug/executorch_android-debug-androidTest.apk "${ARTIFACTS_DIR_NAME}/library_test_dir"
4142
4243
mkdir -p ${ARTIFACTS_DIR_NAME}/fp32-xnnpack-custom

extension/android/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# ExecuTorch Android
2+
3+
This directory contains the Android Java/Kotlin binding. The final product is an AAR,
4+
which contains the `.so` libraries for c++ runtime, and `.jar` for Java API, and required
5+
metadata `AndroidManifest.xml`.
6+
7+
## Core contents
8+
9+
Under `extension/android/`,
10+
11+
- `executorch_android/` is the root for the Java `org.pytorch.executorch` package
12+
- `src/`
13+
- `androidTest/` contains the android instrumentation test source
14+
- `main/` contains the Java source
15+
- `test/` contains the Java unit test source
16+
- `build.gradle` is the rule to build the Java package.
17+
- `jni/` contains the JNI layer code, which depends on the ExecuTorch c++ runtime library.
18+
- `CMakeLists.txt` is the rule for building the JNI library.
19+
20+
## Build
21+
22+
`scripts/build_android_library.sh` is a helper script to build the Java library (into .jar), native library (into .so), and the packaged AAR file.
23+
24+
The usage is:
25+
```sh
26+
export ANDROID_HOME=/path/to/sdk
27+
export ANDROID_NDK=/path/to/ndk
28+
sh scripts/build_android_library.sh
29+
```
30+
31+
Please see [Android building from source](https://pytorch.org/executorch/main/using-executorch-android.html#building-from-source) for details
32+
33+
## Test
34+
35+
After the library is built,
36+
37+
```sh
38+
# Set up models for testing
39+
sh executorch_android/android_test_setup.sh
40+
41+
# Run unit test
42+
./gradlew :executorch_android:testDebugUnitTest
43+
44+
# Run instrumentation test
45+
./gradlew :executorch_android:connectedAndroidTest
46+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# All rights reserved.
4+
#
5+
# This source code is licensed under the BSD-style license found in the
6+
# LICENSE file in the root directory of this source tree.
7+
8+
set -ex
9+
10+
if [[ -z "${PYTHON_EXECUTABLE:-}" ]]; then
11+
PYTHON_EXECUTABLE=python3
12+
fi
13+
which "${PYTHON_EXECUTABLE}"
14+
15+
BASEDIR=$(dirname "$0")
16+
cp "${BASEDIR}/../../../extension/module/test/resources/add.pte" "${BASEDIR}/src/androidTest/resources"
17+
18+
pushd "${BASEDIR}/../../../"
19+
curl -Ls "https://huggingface.co/karpathy/tinyllamas/resolve/main/stories110M.pt" --output stories110M.pt
20+
curl -Ls "https://raw.githubusercontent.com/karpathy/llama2.c/master/tokenizer.model" --output tokenizer.model
21+
# Create params.json file
22+
touch params.json
23+
echo '{"dim": 768, "multiple_of": 32, "n_heads": 12, "n_layers": 12, "norm_eps": 1e-05, "vocab_size": 32000}' > params.json
24+
python -m examples.models.llama.export_llama -c stories110M.pt -p params.json -X -kv --model=stories110m
25+
26+
cp *.pte "${BASEDIR}/src/androidTest/resources/stories.pte"
27+
cp tokenizer.model "${BASEDIR}/src/androidTest/resources/tokenizer.bin"
28+
popd

extension/android/executorch_android/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ android {
3636
}
3737
}
3838

39+
task copyTestRes(type: Exec) {
40+
commandLine 'bash', 'android_test_setup.sh'
41+
}
42+
3943
dependencies {
4044
implementation 'com.facebook.fbjni:fbjni:0.5.1'
4145
implementation 'com.facebook.soloader:nativeloader:0.10.5'

extension/android/executorch_android/src/androidTest/java/org/pytorch/executorch/LlmModuleInstrumentationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
/** Unit tests for {@link org.pytorch.executorch.extension.llm.LlmModule}. */
4141
@RunWith(AndroidJUnit4.class)
4242
public class LlmModuleInstrumentationTest implements LlmCallback {
43-
private static String TEST_FILE_NAME = "/tinyllama_portable_fp16_h.pte";
43+
private static String TEST_FILE_NAME = "/stories.pte";
4444
private static String TOKENIZER_FILE_NAME = "/tokenizer.bin";
4545
private static String TEST_PROMPT = "Hello";
4646
private static int OK = 0x00;

scripts/build_android_library.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ build_aar() {
119119
fi
120120
pushd extension/android/
121121
ANDROID_HOME="${ANDROID_SDK:-/opt/android/sdk}" ./gradlew build
122+
# Use java unit test as sanity check
123+
ANDROID_HOME="${ANDROID_SDK:-/opt/android/sdk}" ./gradlew :executorch_android:testDebugUnitTest
122124
popd
123125
cp extension/android/executorch_android/build/outputs/aar/executorch_android-debug.aar "${BUILD_AAR_DIR}/executorch.aar"
124126
}

0 commit comments

Comments
 (0)