Skip to content

Commit 9b55f48

Browse files
huydhnfacebook-github-bot
authored andcommitted
Fix Android llama2 demo app after #2962 (#3032)
Summary: This fixes the issue when the demo Android app fails to load llama2 model and returns an exit code 20. As this failure can be captured by running the instrumentation test suite on Android devices, I also add the test spec that I'm using there for future reference. ### Testing https://github.com/pytorch/executorch/actions/runs/8682469360/job/23808274556?pr=3032#step:12:80 loads the model successfully and shows the observed TSP now Pull Request resolved: #3032 Reviewed By: kirklandsign Differential Revision: D56124177 Pulled By: huydhn fbshipit-source-id: 7cc3987d186e670143f2ca739d29f02649091ec2
1 parent 54f9f3e commit 9b55f48

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

.github/workflows/android.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ on:
1010
- .ci/docker/**
1111
- .github/workflows/android.yml
1212
- install_requirements.sh
13-
- examples/demo-apps/**
13+
- examples/demo-apps/android/**
14+
- extension/android/**
1415
- extension/module/**
1516
workflow_dispatch:
1617

@@ -101,7 +102,7 @@ jobs:
101102
android-app-archive: https://gha-artifacts.s3.amazonaws.com/${{ github.repository }}/${{ github.run_id }}/artifact/app-debug.apk
102103
android-test-archive: https://gha-artifacts.s3.amazonaws.com/${{ github.repository }}/${{ github.run_id }}/artifact/app-debug-androidTest.apk
103104
# The test spec can be downloaded from https://ossci-assets.s3.amazonaws.com/android-llama2-device-farm-test-spec.yml
104-
test-spec: arn:aws:devicefarm:us-west-2:308535385114:upload:02a2cf0f-6d9b-45ee-ba1a-a086587469e6/414cb54d-4d83-4576-8317-93244e4dc50e
105+
test-spec: arn:aws:devicefarm:us-west-2:308535385114:upload:02a2cf0f-6d9b-45ee-ba1a-a086587469e6/abd86868-fa63-467e-a5c7-218194665a77
105106
# The exported llama2 model and its tokenizer, can be downloaded from https://ossci-assets.s3.amazonaws.com/executorch-android-llama2-7b.zip.
106107
# Among the input, this is the biggest file and uploading it to AWS beforehand makes the test run much faster
107108
extra-data: arn:aws:devicefarm:us-west-2:308535385114:upload:02a2cf0f-6d9b-45ee-ba1a-a086587469e6/bd15825b-ddab-4e47-9fef-a9c8935778dd
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
version: 0.1
2+
3+
android_test_host: amazon_linux_2
4+
5+
phases:
6+
install:
7+
commands:
8+
9+
pre_test:
10+
commands:
11+
# Prepare the model and the tokenizer
12+
- adb -s $DEVICEFARM_DEVICE_UDID shell "ls -la /sdcard/"
13+
- adb -s $DEVICEFARM_DEVICE_UDID shell "mkdir -p /data/local/tmp/llama/"
14+
- adb -s $DEVICEFARM_DEVICE_UDID shell "mv /sdcard/tokenizer.bin /data/local/tmp/llama/tokenizer.bin"
15+
- adb -s $DEVICEFARM_DEVICE_UDID shell "mv /sdcard/xnnpack_llama2.pte /data/local/tmp/llama/xnnpack_llama2.pte"
16+
- adb -s $DEVICEFARM_DEVICE_UDID shell "chmod 664 /data/local/tmp/llama/tokenizer.bin"
17+
- adb -s $DEVICEFARM_DEVICE_UDID shell "chmod 664 /data/local/tmp/llama/xnnpack_llama2.pte"
18+
- adb -s $DEVICEFARM_DEVICE_UDID shell "ls -la /data/local/tmp/llama/"
19+
20+
test:
21+
commands:
22+
# By default, the following ADB command is used by Device Farm to run your Instrumentation test.
23+
# Please refer to Android's documentation for more options on running instrumentation tests with adb:
24+
# https://developer.android.com/studio/test/command-line#run-tests-with-adb
25+
- echo "Starting the Instrumentation test"
26+
- |
27+
adb -s $DEVICEFARM_DEVICE_UDID shell "am instrument -r -w --no-window-animation \
28+
$DEVICEFARM_TEST_PACKAGE_NAME/$DEVICEFARM_TEST_PACKAGE_RUNNER 2>&1 || echo \": -1\"" |
29+
tee $DEVICEFARM_LOG_DIR/instrument.log
30+
31+
# Parse the results
32+
- |-
33+
INSTRUMENT_LOG="$DEVICEFARM_LOG_DIR/instrument.log"
34+
35+
DID_ANY_TESTS_START=$(grep "INSTRUMENTATION_STATUS_CODE: 1" $INSTRUMENT_LOG | wc -l);
36+
TESTS_PASSED=$(grep "INSTRUMENTATION_STATUS_CODE: 0" $INSTRUMENT_LOG | wc -l);
37+
TESTS_ERRORED=$(grep "INSTRUMENTATION_STATUS_CODE: -1" $INSTRUMENT_LOG | wc -l);
38+
TESTS_FAILED=$(grep "INSTRUMENTATION_STATUS_CODE: -2" $INSTRUMENT_LOG | wc -l);
39+
TESTS_IGNORED=$(grep "INSTRUMENTATION_STATUS_CODE: -3" $INSTRUMENT_LOG | wc -l);
40+
TESTS_ASSUMPTION_FAILED=$(grep "INSTRUMENTATION_STATUS_CODE: -4" $INSTRUMENT_LOG | wc -l);
41+
TESTS_PROCESSES_CRASHED=$(grep "INSTRUMENTATION_RESULT: shortMsg=Process crashed." $INSTRUMENT_LOG | wc -l);
42+
43+
# And print the results so that the CI job can show them later
44+
- |
45+
INSTRUMENT_LOG="$DEVICEFARM_LOG_DIR/instrument.log"
46+
47+
if [ $DID_ANY_TESTS_START -eq 0 ];
48+
then
49+
echo "[PyTorch] Marking the test suite as failed because no tests started!";
50+
false;
51+
elif [ $TESTS_FAILED -ne 0 ];
52+
then
53+
OBSERVED_TPS=$(grep "The observed TPS " $INSTRUMENT_LOG | tail -n 1)
54+
55+
if [ -n "${OBSERVED_TPS}" ];
56+
then
57+
echo "[PyTorch] ${OBSERVED_TPS}";
58+
else
59+
echo "[PyTorch] Marking the test suite as failed because it failed to load the model";
60+
fi
61+
elif [ $TESTS_ERRORED -ne 0 ];
62+
then
63+
echo "[PyTorch] Marking the test suite as failed because $TESTS_ERRORED tests errored!";
64+
false;
65+
elif [ $TESTS_PROCESSES_CRASHED -ne 0 ];
66+
then
67+
echo "[PyTorch] Marking the test suite as failed because the app crashed due to OOM!";
68+
false;
69+
fi;
70+
71+
post_test:
72+
commands:
73+
74+
artifacts:
75+
# By default, Device Farm will collect your artifacts from the $DEVICEFARM_LOG_DIR directory.
76+
- $DEVICEFARM_LOG_DIR

extension/android/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ if(EXECUTORCH_BUILD_LLAMA_JNI)
6161
set(CUSTOM_OPS_PATH ${CMAKE_CURRENT_BINARY_DIR}/../../examples/models/llama2/custom_ops/libcustom_ops.a)
6262
add_library(custom_ops STATIC IMPORTED)
6363
set_property(TARGET custom_ops PROPERTY IMPORTED_LOCATION ${CUSTOM_OPS_PATH})
64+
target_link_options_shared_lib(custom_ops)
6465

6566
if(TARGET pthreadpool)
6667
set(LLAMA_JNI_SRCS jni/jni_layer_llama.cpp ../../backends/xnnpack/threadpool/cpuinfo_utils.cpp)

0 commit comments

Comments
 (0)