From e5314f318a00d36cbdb2ebd85e88656290ad9525 Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Fri, 11 Apr 2025 15:46:55 -0700 Subject: [PATCH 01/15] Fix android instrumentation --- .../executorch_android/android_test_setup.sh | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/extension/android/executorch_android/android_test_setup.sh b/extension/android/executorch_android/android_test_setup.sh index 68f4d9dd6f6..41f22a97ecb 100644 --- a/extension/android/executorch_android/android_test_setup.sh +++ b/extension/android/executorch_android/android_test_setup.sh @@ -12,17 +12,18 @@ if [[ -z "${PYTHON_EXECUTABLE:-}" ]]; then fi which "${PYTHON_EXECUTABLE}" -BASEDIR=$(dirname "$0") +BASEDIR=$(dirname "$(realpath $0)") cp "${BASEDIR}/../../../extension/module/test/resources/add.pte" "${BASEDIR}/src/androidTest/resources" pushd "${BASEDIR}/../../../" -curl -Ls "https://huggingface.co/karpathy/tinyllamas/resolve/main/stories110M.pt" --output stories110M.pt -curl -Ls "https://raw.githubusercontent.com/karpathy/llama2.c/master/tokenizer.model" --output tokenizer.model +curl -C - -Ls "https://huggingface.co/karpathy/tinyllamas/resolve/main/stories110M.pt" --output stories110M.pt +curl -C - -Ls "https://raw.githubusercontent.com/karpathy/llama2.c/master/tokenizer.model" --output tokenizer.model # Create params.json file touch params.json echo '{"dim": 768, "multiple_of": 32, "n_heads": 12, "n_layers": 12, "norm_eps": 1e-05, "vocab_size": 32000}' > params.json -python -m examples.models.llama.export_llama -c stories110M.pt -p params.json -X -kv --model=stories110m +python -m examples.models.llama.export_llama -c stories110M.pt -p params.json -d fp16 --model=stories110m +python -m pytorch_tokenizers.tools.llama2c.convert -t tokenizer.model -o tokenizer.bin -cp *.pte "${BASEDIR}/src/androidTest/resources/stories.pte" -cp tokenizer.model "${BASEDIR}/src/androidTest/resources/tokenizer.bin" +cp stories110m_h.pte "${BASEDIR}/src/androidTest/resources/stories.pte" +cp tokenizer.bin "${BASEDIR}/src/androidTest/resources/tokenizer.bin" popd From b97c65c2e80eb88cf6be0711faf46f3d5e82dc10 Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Fri, 11 Apr 2025 16:13:17 -0700 Subject: [PATCH 02/15] Use their config --- extension/android/executorch_android/android_test_setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension/android/executorch_android/android_test_setup.sh b/extension/android/executorch_android/android_test_setup.sh index 41f22a97ecb..fff8ba33618 100644 --- a/extension/android/executorch_android/android_test_setup.sh +++ b/extension/android/executorch_android/android_test_setup.sh @@ -21,7 +21,7 @@ curl -C - -Ls "https://raw.githubusercontent.com/karpathy/llama2.c/master/tokeni # Create params.json file touch params.json echo '{"dim": 768, "multiple_of": 32, "n_heads": 12, "n_layers": 12, "norm_eps": 1e-05, "vocab_size": 32000}' > params.json -python -m examples.models.llama.export_llama -c stories110M.pt -p params.json -d fp16 --model=stories110m +python -m examples.models.llama.export_llama -c stories110M.pt -p params.json -d fp16 -n stories110m_h.pte -kv python -m pytorch_tokenizers.tools.llama2c.convert -t tokenizer.model -o tokenizer.bin cp stories110m_h.pte "${BASEDIR}/src/androidTest/resources/stories.pte" From a66e01f722903dea774a9489af173002dca4d557 Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Fri, 11 Apr 2025 16:30:08 -0700 Subject: [PATCH 03/15] Enforce result --- .../executorch/LlmModuleInstrumentationTest.java | 1 - scripts/run_android_emulator.sh | 12 ++++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/extension/android/executorch_android/src/androidTest/java/org/pytorch/executorch/LlmModuleInstrumentationTest.java b/extension/android/executorch_android/src/androidTest/java/org/pytorch/executorch/LlmModuleInstrumentationTest.java index ae81a21f420..e95b42f2650 100644 --- a/extension/android/executorch_android/src/androidTest/java/org/pytorch/executorch/LlmModuleInstrumentationTest.java +++ b/extension/android/executorch_android/src/androidTest/java/org/pytorch/executorch/LlmModuleInstrumentationTest.java @@ -86,7 +86,6 @@ public void testGenerate() throws IOException, URISyntaxException{ @Test public void testGenerateAndStop() throws IOException, URISyntaxException{ - int seqLen = 32; mModule.generate(TEST_PROMPT, SEQ_LEN, new LlmCallback() { @Override public void onResult(String result) { diff --git a/scripts/run_android_emulator.sh b/scripts/run_android_emulator.sh index fe73ec8a1d7..e03243abd5d 100755 --- a/scripts/run_android_emulator.sh +++ b/scripts/run_android_emulator.sh @@ -26,9 +26,17 @@ adb install -t app-debug-androidTest.apk adb shell mkdir -p /data/local/tmp/llama adb push model.pte /data/local/tmp/llama adb push tokenizer.bin /data/local/tmp/llama -adb shell am instrument -w -r com.example.executorchllamademo.test/androidx.test.runner.AndroidJUnitRunner +adb shell am instrument -w -r com.example.executorchllamademo.test/androidx.test.runner.AndroidJUnitRunner >result.txt 2>&1 +if [ grep -q FAILURES result.txt ]; then + cat result.txt + exit 1 +fi adb uninstall org.pytorch.executorch.test || true adb install -t android-test-debug-androidTest.apk -adb shell am instrument -w -r org.pytorch.executorch.test/androidx.test.runner.AndroidJUnitRunner +adb shell am instrument -w -r org.pytorch.executorch.test/androidx.test.runner.AndroidJUnitRunner >result.txt 2>&1 +if [ grep -q FAILURES result.txt ]; then + cat result.txt + exit 1 +fi From 6a2628f0136de1c892b9b77636bde21d75f4db35 Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Fri, 11 Apr 2025 17:24:12 -0700 Subject: [PATCH 04/15] Fix --- scripts/run_android_emulator.sh | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/scripts/run_android_emulator.sh b/scripts/run_android_emulator.sh index e03243abd5d..58b147f643d 100755 --- a/scripts/run_android_emulator.sh +++ b/scripts/run_android_emulator.sh @@ -27,16 +27,10 @@ adb shell mkdir -p /data/local/tmp/llama adb push model.pte /data/local/tmp/llama adb push tokenizer.bin /data/local/tmp/llama adb shell am instrument -w -r com.example.executorchllamademo.test/androidx.test.runner.AndroidJUnitRunner >result.txt 2>&1 -if [ grep -q FAILURES result.txt ]; then - cat result.txt - exit 1 -fi +grep -q FAILURES result.txt || cat result.txt adb uninstall org.pytorch.executorch.test || true adb install -t android-test-debug-androidTest.apk adb shell am instrument -w -r org.pytorch.executorch.test/androidx.test.runner.AndroidJUnitRunner >result.txt 2>&1 -if [ grep -q FAILURES result.txt ]; then - cat result.txt - exit 1 -fi +grep -q FAILURES result.txt || cat result.txt From 3f66ec356eaccf2520f13b16701bfa134f9a3e83 Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Mon, 14 Apr 2025 14:22:48 -0700 Subject: [PATCH 05/15] store logcat --- scripts/run_android_emulator.sh | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scripts/run_android_emulator.sh b/scripts/run_android_emulator.sh index 58b147f643d..43ecfc1671a 100755 --- a/scripts/run_android_emulator.sh +++ b/scripts/run_android_emulator.sh @@ -26,11 +26,17 @@ adb install -t app-debug-androidTest.apk adb shell mkdir -p /data/local/tmp/llama adb push model.pte /data/local/tmp/llama adb push tokenizer.bin /data/local/tmp/llama +adb logcat -c adb shell am instrument -w -r com.example.executorchllamademo.test/androidx.test.runner.AndroidJUnitRunner >result.txt 2>&1 +adb logcat -d > logcat.txt +cat logcat.txt grep -q FAILURES result.txt || cat result.txt adb uninstall org.pytorch.executorch.test || true adb install -t android-test-debug-androidTest.apk +adb logcat -c adb shell am instrument -w -r org.pytorch.executorch.test/androidx.test.runner.AndroidJUnitRunner >result.txt 2>&1 +adb logcat -d > logcat.txt +cat logcat.txt grep -q FAILURES result.txt || cat result.txt From ddeeab78949fc984ab470662359a5594e8b21553 Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Mon, 14 Apr 2025 14:29:19 -0700 Subject: [PATCH 06/15] Run emu --- .github/workflows/_android.yml | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/_android.yml b/.github/workflows/_android.yml index a38ed4527e9..05965fd8be3 100644 --- a/.github/workflows/_android.yml +++ b/.github/workflows/_android.yml @@ -59,7 +59,6 @@ jobs: # Running Android emulator directly on the runner and not using Docker run-emulator: - needs: build-llm-demo # NB: Use metal install for KVM support to run the emulator faster runs-on: linux.24xl.spr-metal env: @@ -99,10 +98,10 @@ jobs: shell: bash run: | set -eux - curl -O https://gha-artifacts.s3.amazonaws.com/${{ github.repository }}/${{ github.run_id }}/artifacts/llm_demo/app-debug.apk - curl -O https://gha-artifacts.s3.amazonaws.com/${{ github.repository }}/${{ github.run_id }}/artifacts/llm_demo/app-debug-androidTest.apk - curl -O https://gha-artifacts.s3.amazonaws.com/${{ github.repository }}/${{ github.run_id }}/artifacts/fp32-xnnpack-custom/model.zip - curl -o android-test-debug-androidTest.apk https://gha-artifacts.s3.amazonaws.com/${{ github.repository }}/${{ github.run_id }}/artifacts/library_test_dir/executorch_android-debug-androidTest.apk + curl -O https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14456045058/artifacts/llm_demo/app-debug.apk + curl -O https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14456045058/artifacts/llm_demo/app-debug-androidTest.apk + curl -O https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14456045058/artifacts/fp32-xnnpack-custom/model.zip + curl -o android-test-debug-androidTest.apk https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14456045058/artifacts/library_test_dir/executorch_android-debug-androidTest.apk unzip model.zip mv *.pte model.pte From 62e753efe63c2e5cc7470a3a7ae23805ad4dc62a Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Mon, 14 Apr 2025 14:34:21 -0700 Subject: [PATCH 07/15] Run emu --- .github/workflows/_android.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/_android.yml b/.github/workflows/_android.yml index 05965fd8be3..4b79695042a 100644 --- a/.github/workflows/_android.yml +++ b/.github/workflows/_android.yml @@ -98,10 +98,10 @@ jobs: shell: bash run: | set -eux - curl -O https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14456045058/artifacts/llm_demo/app-debug.apk - curl -O https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14456045058/artifacts/llm_demo/app-debug-androidTest.apk - curl -O https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14456045058/artifacts/fp32-xnnpack-custom/model.zip - curl -o android-test-debug-androidTest.apk https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14456045058/artifacts/library_test_dir/executorch_android-debug-androidTest.apk + curl -O https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14413548251/artifacts/llm_demo/app-debug.apk + curl -O https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14413548251/artifacts/llm_demo/app-debug-androidTest.apk + curl -O https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14413548251/artifacts/fp32-xnnpack-custom/model.zip + curl -o android-test-debug-androidTest.apk https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14413548251/artifacts/library_test_dir/executorch_android-debug-androidTest.apk unzip model.zip mv *.pte model.pte From f7d5a533bcfee68d5bccf646b5687f319637369c Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Mon, 14 Apr 2025 15:09:16 -0700 Subject: [PATCH 08/15] RAM size --- .github/workflows/_android.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/_android.yml b/.github/workflows/_android.yml index 4b79695042a..6a5ebdc845b 100644 --- a/.github/workflows/_android.yml +++ b/.github/workflows/_android.yml @@ -130,7 +130,7 @@ jobs: # https://github.com/ReactiveCircus/android-emulator-runner. The max number # of cores we can set is 6, any higher number will be reduced to 6. cores: 6 - ram-size: 12288M + ram-size: 16384M force-avd-creation: false disable-animations: true emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none From 3e739bc4e0d3d599eab7589104c144c1422f3a5b Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Mon, 14 Apr 2025 15:16:32 -0700 Subject: [PATCH 09/15] Fix --- .github/workflows/_android.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/_android.yml b/.github/workflows/_android.yml index 6a5ebdc845b..ee9b9345c1c 100644 --- a/.github/workflows/_android.yml +++ b/.github/workflows/_android.yml @@ -131,6 +131,7 @@ jobs: # of cores we can set is 6, any higher number will be reduced to 6. cores: 6 ram-size: 16384M + heap-size: 12288M force-avd-creation: false disable-animations: true emulator-options: -no-snapshot-save -no-window -gpu swiftshader_indirect -noaudio -no-boot-anim -camera-back none From f31573a80888042d0d3f4674dde3b281fda5f768 Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Mon, 14 Apr 2025 15:28:43 -0700 Subject: [PATCH 10/15] skip LLM --- scripts/run_android_emulator.sh | 17 ++--------------- 1 file changed, 2 insertions(+), 15 deletions(-) diff --git a/scripts/run_android_emulator.sh b/scripts/run_android_emulator.sh index 43ecfc1671a..8c9abe93146 100755 --- a/scripts/run_android_emulator.sh +++ b/scripts/run_android_emulator.sh @@ -18,25 +18,12 @@ $ADB_PATH wait-for-device shell 'while [[ -z $(getprop sys.boot_completed) ]]; d echo "List all running emulators" $ADB_PATH devices -adb uninstall com.example.executorchllamademo || true -adb uninstall com.example.executorchllamademo.test || true -adb install -t app-debug.apk -adb install -t app-debug-androidTest.apk - -adb shell mkdir -p /data/local/tmp/llama -adb push model.pte /data/local/tmp/llama -adb push tokenizer.bin /data/local/tmp/llama -adb logcat -c -adb shell am instrument -w -r com.example.executorchllamademo.test/androidx.test.runner.AndroidJUnitRunner >result.txt 2>&1 -adb logcat -d > logcat.txt -cat logcat.txt -grep -q FAILURES result.txt || cat result.txt - adb uninstall org.pytorch.executorch.test || true adb install -t android-test-debug-androidTest.apk adb logcat -c -adb shell am instrument -w -r org.pytorch.executorch.test/androidx.test.runner.AndroidJUnitRunner >result.txt 2>&1 +adb shell am instrument -w -r org.pytorch.executorch.test/androidx.test.runner.AndroidJUnitRunner \ + -e class org.pytorch.executorch.ModuleInstrumentationTest >result.txt 2>&1 adb logcat -d > logcat.txt cat logcat.txt grep -q FAILURES result.txt || cat result.txt From 24a69350125bd49992214fd7bc81b27e42383868 Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Mon, 14 Apr 2025 15:37:20 -0700 Subject: [PATCH 11/15] Fix --- scripts/run_android_emulator.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/run_android_emulator.sh b/scripts/run_android_emulator.sh index 8c9abe93146..9f7666e33e6 100755 --- a/scripts/run_android_emulator.sh +++ b/scripts/run_android_emulator.sh @@ -22,8 +22,8 @@ adb uninstall org.pytorch.executorch.test || true adb install -t android-test-debug-androidTest.apk adb logcat -c -adb shell am instrument -w -r org.pytorch.executorch.test/androidx.test.runner.AndroidJUnitRunner \ - -e class org.pytorch.executorch.ModuleInstrumentationTest >result.txt 2>&1 +adb shell am instrument -w -r -e class org.pytorch.executorch.ModuleInstrumentationTest >result.txt \ + org.pytorch.executorch.test/androidx.test.runner.AndroidJUnitRunner 2>&1 adb logcat -d > logcat.txt cat logcat.txt grep -q FAILURES result.txt || cat result.txt From 4260054d3d67132bfcbb2585ab6a27b385d5153a Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Mon, 14 Apr 2025 15:44:51 -0700 Subject: [PATCH 12/15] Fix --- scripts/run_android_emulator.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/run_android_emulator.sh b/scripts/run_android_emulator.sh index 9f7666e33e6..f7533a7815c 100755 --- a/scripts/run_android_emulator.sh +++ b/scripts/run_android_emulator.sh @@ -22,8 +22,9 @@ adb uninstall org.pytorch.executorch.test || true adb install -t android-test-debug-androidTest.apk adb logcat -c -adb shell am instrument -w -r -e class org.pytorch.executorch.ModuleInstrumentationTest >result.txt \ - org.pytorch.executorch.test/androidx.test.runner.AndroidJUnitRunner 2>&1 +adb shell am instrument -w -r -e class org.pytorch.executorch.ModuleInstrumentationTest \ + org.pytorch.executorch.test/androidx.test.runner.AndroidJUnitRunner >result.txt 2>&1 adb logcat -d > logcat.txt cat logcat.txt grep -q FAILURES result.txt || cat result.txt +grep -q FAILURES result.txt || exit -1 From 4a11acd23be43c7da6a0f78d3dada61557ff73b5 Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Mon, 14 Apr 2025 15:54:12 -0700 Subject: [PATCH 13/15] Fix --- scripts/run_android_emulator.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/run_android_emulator.sh b/scripts/run_android_emulator.sh index f7533a7815c..a550997b388 100755 --- a/scripts/run_android_emulator.sh +++ b/scripts/run_android_emulator.sh @@ -26,5 +26,5 @@ adb shell am instrument -w -r -e class org.pytorch.executorch.ModuleInstrumentat org.pytorch.executorch.test/androidx.test.runner.AndroidJUnitRunner >result.txt 2>&1 adb logcat -d > logcat.txt cat logcat.txt -grep -q FAILURES result.txt || cat result.txt -grep -q FAILURES result.txt || exit -1 +grep -q FAILURES result.txt && cat result.txt +grep -q FAILURES result.txt && exit -1 From d4f16548cc8fed1b34b09662630240b9dc056563 Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Mon, 14 Apr 2025 15:56:23 -0700 Subject: [PATCH 14/15] Ready --- .github/workflows/_android.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/_android.yml b/.github/workflows/_android.yml index ee9b9345c1c..2a91fb45ffa 100644 --- a/.github/workflows/_android.yml +++ b/.github/workflows/_android.yml @@ -59,6 +59,7 @@ jobs: # Running Android emulator directly on the runner and not using Docker run-emulator: + needs: build-llm-demo # NB: Use metal install for KVM support to run the emulator faster runs-on: linux.24xl.spr-metal env: @@ -98,10 +99,10 @@ jobs: shell: bash run: | set -eux - curl -O https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14413548251/artifacts/llm_demo/app-debug.apk - curl -O https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14413548251/artifacts/llm_demo/app-debug-androidTest.apk - curl -O https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14413548251/artifacts/fp32-xnnpack-custom/model.zip - curl -o android-test-debug-androidTest.apk https://gha-artifacts.s3.amazonaws.com/pytorch/executorch/14413548251/artifacts/library_test_dir/executorch_android-debug-androidTest.apk + curl -O https://gha-artifacts.s3.amazonaws.com/${{ github.repository }}/${{ github.run_id }}/artifacts/llm_demo/app-debug.apk + curl -O https://gha-artifacts.s3.amazonaws.com/${{ github.repository }}/${{ github.run_id }}/artifacts/llm_demo/app-debug-androidTest.apk + curl -O https://gha-artifacts.s3.amazonaws.com/${{ github.repository }}/${{ github.run_id }}/artifacts/fp32-xnnpack-custom/model.zip + curl -o android-test-debug-androidTest.apk https://gha-artifacts.s3.amazonaws.com/${{ github.repository }}/${{ github.run_id }}/artifacts/library_test_dir/executorch_android-debug-androidTest.apk unzip model.zip mv *.pte model.pte From 82f75d9cccde209e98999437f68a621152a1d6c0 Mon Sep 17 00:00:00 2001 From: Hansong Zhang Date: Mon, 14 Apr 2025 17:27:25 -0700 Subject: [PATCH 15/15] Fix --- scripts/run_android_emulator.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/run_android_emulator.sh b/scripts/run_android_emulator.sh index a550997b388..52f5fe55bd8 100755 --- a/scripts/run_android_emulator.sh +++ b/scripts/run_android_emulator.sh @@ -28,3 +28,4 @@ adb logcat -d > logcat.txt cat logcat.txt grep -q FAILURES result.txt && cat result.txt grep -q FAILURES result.txt && exit -1 +exit 0