Skip to content

Commit f692ff5

Browse files
authored
Android MV2 E2E instrumentation test (#10219)
Add the test and setup step.
1 parent 2e81e8c commit f692ff5

File tree

3 files changed

+94
-15
lines changed

3 files changed

+94
-15
lines changed

extension/android/executorch_android/android_test_setup.sh

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,33 @@ fi
1313
which "${PYTHON_EXECUTABLE}"
1414

1515
BASEDIR=$(dirname "$(realpath $0)")
16-
cp "${BASEDIR}/../../../extension/module/test/resources/add.pte" "${BASEDIR}/src/androidTest/resources"
17-
18-
pushd "${BASEDIR}/../../../"
19-
curl -C - -Ls "https://huggingface.co/karpathy/tinyllamas/resolve/main/stories110M.pt" --output stories110M.pt
20-
curl -C - -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 -d fp16 -n stories110m_h.pte -kv
25-
python -m pytorch_tokenizers.tools.llama2c.convert -t tokenizer.model -o tokenizer.bin
26-
27-
cp stories110m_h.pte "${BASEDIR}/src/androidTest/resources/stories.pte"
28-
cp tokenizer.bin "${BASEDIR}/src/androidTest/resources/tokenizer.bin"
29-
popd
16+
17+
prepare_add() {
18+
cp "${BASEDIR}/../../../extension/module/test/resources/add.pte" "${BASEDIR}/src/androidTest/resources"
19+
}
20+
21+
prepare_tinyllama() {
22+
pushd "${BASEDIR}/../../../"
23+
curl -C - -Ls "https://huggingface.co/karpathy/tinyllamas/resolve/main/stories15M.pt" --output stories15M.pt
24+
curl -C - -Ls "https://raw.githubusercontent.com/karpathy/llama2.c/master/tokenizer.model" --output tokenizer.model
25+
# Create params.json file
26+
touch params.json
27+
echo '{"dim": 288, "multiple_of": 32, "n_heads": 6, "n_layers": 6, "norm_eps": 1e-05, "vocab_size": 32000}' > params.json
28+
python -m examples.models.llama.export_llama -c stories15M.pt -p params.json -d fp16 -n stories15m_h.pte -kv
29+
python -m pytorch_tokenizers.tools.llama2c.convert -t tokenizer.model -o tokenizer.bin
30+
31+
cp stories15m_h.pte "${BASEDIR}/src/androidTest/resources/stories.pte"
32+
cp tokenizer.bin "${BASEDIR}/src/androidTest/resources/tokenizer.bin"
33+
popd
34+
}
35+
36+
prepare_vision() {
37+
pushd "${BASEDIR}/../../../"
38+
python3 -m examples.xnnpack.aot_compiler --model_name "mv2" --delegate
39+
cp mv2*.pte "${BASEDIR}/src/androidTest/resources/"
40+
popd
41+
}
42+
43+
prepare_add
44+
prepare_tinyllama
45+
prepare_vision
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
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+
9+
package org.pytorch.executorch;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertTrue;
13+
import static org.junit.Assert.assertFalse;
14+
import static org.junit.Assert.assertNotEquals;
15+
import static org.junit.Assert.fail;
16+
17+
import android.os.Environment;
18+
import androidx.test.rule.GrantPermissionRule;
19+
import android.Manifest;
20+
import android.content.Context;
21+
import org.junit.Test;
22+
import org.junit.Before;
23+
import org.junit.Rule;
24+
import org.junit.runner.RunWith;
25+
import java.io.InputStream;
26+
import java.net.URI;
27+
import java.net.URISyntaxException;
28+
import java.util.concurrent.CountDownLatch;
29+
import java.util.concurrent.atomic.AtomicInteger;
30+
import java.io.IOException;
31+
import java.io.File;
32+
import java.io.FileOutputStream;
33+
import org.junit.runners.JUnit4;
34+
import org.apache.commons.io.FileUtils;
35+
import androidx.test.ext.junit.runners.AndroidJUnit4;
36+
import androidx.test.InstrumentationRegistry;
37+
38+
/** Unit tests for {@link Module}. */
39+
@RunWith(AndroidJUnit4.class)
40+
public class ModuleE2ETest {
41+
private static String getTestFilePath(String fileName) {
42+
return InstrumentationRegistry.getInstrumentation().getTargetContext().getExternalCacheDir() + fileName;
43+
}
44+
45+
@Rule
46+
public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE);
47+
48+
@Test
49+
public void testMv2Fp32() throws IOException, URISyntaxException{
50+
String filePath = "/mv2_xnnpack_fp32.pte";
51+
File pteFile = new File(getTestFilePath(filePath));
52+
InputStream inputStream = getClass().getResourceAsStream(filePath);
53+
FileUtils.copyInputStreamToFile(inputStream, pteFile);
54+
inputStream.close();
55+
56+
Module module = Module.load(getTestFilePath(filePath));
57+
58+
EValue[] results = module.forward();
59+
assertTrue(results[0].isTensor());
60+
}
61+
62+
}

scripts/run_android_emulator.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ adb uninstall org.pytorch.executorch.test || true
2222
adb install -t android-test-debug-androidTest.apk
2323

2424
adb logcat -c
25-
adb shell am instrument -w -r -e class org.pytorch.executorch.ModuleInstrumentationTest \
25+
adb shell am instrument -w -r -e \
26+
class org.pytorch.executorch.ModuleInstrumentationTest,org.pytorch.executorch.ModuleE2ETest \
2627
org.pytorch.executorch.test/androidx.test.runner.AndroidJUnitRunner >result.txt 2>&1
2728
adb logcat -d > logcat.txt
2829
cat logcat.txt

0 commit comments

Comments
 (0)