Skip to content

Commit c867660

Browse files
authored
Update torchchat Android app to use latest ET llama demo app (pytorch#1292)
1 parent 020da2e commit c867660

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+3538
-486
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/build
1+
/build
Lines changed: 84 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,97 @@
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+
19
plugins {
2-
id("com.android.application")
10+
id("com.android.application")
11+
id("org.jetbrains.kotlin.android")
312
}
413

514
android {
6-
namespace = "org.pytorch.torchchat"
7-
compileSdk = 33
15+
namespace = "org.pytorch.torchchat"
16+
compileSdk = 34
817

9-
defaultConfig {
10-
applicationId = "org.pytorch.torchchat"
11-
minSdk = 24
12-
targetSdk = 33
13-
versionCode = 1
14-
versionName = "1.0"
18+
defaultConfig {
19+
applicationId = "org.pytorch.torchchat"
20+
minSdk = 28
21+
targetSdk = 33
22+
versionCode = 1
23+
versionName = "1.0"
1524

16-
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
17-
}
25+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
26+
vectorDrawables { useSupportLibrary = true }
27+
externalNativeBuild { cmake { cppFlags += "" } }
28+
}
1829

19-
buildTypes {
20-
release {
21-
isMinifyEnabled = false
22-
proguardFiles(
23-
getDefaultProguardFile("proguard-android-optimize.txt"),
24-
"proguard-rules.pro"
25-
)
26-
}
30+
buildTypes {
31+
release {
32+
isMinifyEnabled = false
33+
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
2734
}
28-
compileOptions {
29-
sourceCompatibility = JavaVersion.VERSION_1_8
30-
targetCompatibility = JavaVersion.VERSION_1_8
35+
}
36+
compileOptions {
37+
sourceCompatibility = JavaVersion.VERSION_1_8
38+
targetCompatibility = JavaVersion.VERSION_1_8
39+
}
40+
kotlinOptions { jvmTarget = "1.8" }
41+
buildFeatures { compose = true }
42+
composeOptions { kotlinCompilerExtensionVersion = "1.4.3" }
43+
packaging { resources { excludes += "/META-INF/{AL2.0,LGPL2.1}" } }
44+
}
45+
46+
dependencies {
47+
implementation("androidx.core:core-ktx:1.9.0")
48+
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
49+
implementation("androidx.activity:activity-compose:1.7.0")
50+
implementation(platform("androidx.compose:compose-bom:2023.03.00"))
51+
implementation("androidx.compose.ui:ui")
52+
implementation("androidx.compose.ui:ui-graphics")
53+
implementation("androidx.compose.ui:ui-tooling-preview")
54+
implementation("androidx.compose.material3:material3")
55+
implementation("androidx.appcompat:appcompat:1.6.1")
56+
implementation("androidx.camera:camera-core:1.3.0-rc02")
57+
implementation("androidx.constraintlayout:constraintlayout:2.2.0-alpha12")
58+
implementation("com.facebook.fbjni:fbjni:0.5.1")
59+
implementation("com.google.code.gson:gson:2.8.6")
60+
implementation(files("libs/executorch-llama.aar"))
61+
implementation("com.google.android.material:material:1.12.0")
62+
implementation("androidx.activity:activity:1.9.0")
63+
testImplementation("junit:junit:4.13.2")
64+
androidTestImplementation("androidx.test.ext:junit:1.1.5")
65+
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
66+
androidTestImplementation(platform("androidx.compose:compose-bom:2023.03.00"))
67+
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
68+
debugImplementation("androidx.compose.ui:ui-tooling")
69+
debugImplementation("androidx.compose.ui:ui-test-manifest")
70+
}
71+
72+
tasks.register("setup") {
73+
doFirst {
74+
exec {
75+
commandLine("sh", "examples/demo-apps/android/LlamaDemo/setup.sh")
76+
workingDir("../../../../../")
3177
}
32-
buildFeatures {
33-
viewBinding = true
78+
}
79+
}
80+
81+
tasks.register("setupQnn") {
82+
doFirst {
83+
exec {
84+
commandLine("sh", "examples/demo-apps/android/LlamaDemo/setup-with-qnn.sh")
85+
workingDir("../../../../../")
3486
}
87+
}
3588
}
3689

37-
dependencies {
38-
implementation("androidx.appcompat:appcompat:1.6.1")
39-
implementation("androidx.constraintlayout:constraintlayout:2.1.4")
40-
implementation("com.facebook.fbjni:fbjni:0.5.1")
41-
implementation(files("libs/executorch.aar"))
42-
testImplementation("junit:junit:4.13.2")
43-
androidTestImplementation("androidx.test.ext:junit:1.1.5")
44-
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
90+
tasks.register("download_prebuilt_lib") {
91+
doFirst {
92+
exec {
93+
commandLine("sh", "examples/demo-apps/android/LlamaDemo/download_prebuilt_lib.sh")
94+
workingDir("../../../../../")
95+
}
96+
}
4597
}

torchchat/edge/android/torchchat/app/proguard-rules.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818

1919
# If you keep the line number information, uncomment this to
2020
# hide the original source file name.
21-
#-renamesourcefileattribute SourceFile
21+
#-renamesourcefileattribute SourceFile

torchchat/edge/android/torchchat/app/src/androidTest/java/org/pytorch/torchchat/LlamaModuleTest.java

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.torchchat;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertFalse;
13+
14+
import android.os.Bundle;
15+
import androidx.test.ext.junit.runners.AndroidJUnit4;
16+
import androidx.test.platform.app.InstrumentationRegistry;
17+
import java.io.File;
18+
import java.util.ArrayList;
19+
import java.util.Arrays;
20+
import java.util.List;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.pytorch.executorch.LlamaCallback;
24+
import org.pytorch.executorch.LlamaModule;
25+
26+
@RunWith(AndroidJUnit4.class)
27+
public class PerfTest implements LlamaCallback {
28+
29+
private static final String RESOURCE_PATH = "/data/local/tmp/llama/";
30+
private static final String TOKENIZER_BIN = "tokenizer.bin";
31+
32+
private final List<String> results = new ArrayList<>();
33+
private final List<Float> tokensPerSecond = new ArrayList<>();
34+
35+
@Test
36+
public void testTokensPerSecond() {
37+
String tokenizerPath = RESOURCE_PATH + TOKENIZER_BIN;
38+
// Find out the model name
39+
File directory = new File(RESOURCE_PATH);
40+
Arrays.stream(directory.listFiles())
41+
.filter(file -> file.getName().endsWith(".pte"))
42+
.forEach(
43+
model -> {
44+
LlamaModule mModule = new LlamaModule(model.getPath(), tokenizerPath, 0.8f);
45+
// Print the model name because there might be more than one of them
46+
report("ModelName", model.getName());
47+
48+
int loadResult = mModule.load();
49+
// Check that the model can be load successfully
50+
assertEquals(0, loadResult);
51+
52+
// Run a testing prompt
53+
mModule.generate("How do you do! I'm testing llama2 on mobile device", PerfTest.this);
54+
assertFalse(tokensPerSecond.isEmpty());
55+
56+
final Float tps = tokensPerSecond.get(tokensPerSecond.size() - 1);
57+
report("TPS", tps);
58+
});
59+
}
60+
61+
@Override
62+
public void onResult(String result) {
63+
results.add(result);
64+
}
65+
66+
@Override
67+
public void onStats(float tps) {
68+
tokensPerSecond.add(tps);
69+
}
70+
71+
private void report(final String metric, final Float value) {
72+
Bundle bundle = new Bundle();
73+
bundle.putFloat(metric, value);
74+
InstrumentationRegistry.getInstrumentation().sendStatus(0, bundle);
75+
}
76+
77+
private void report(final String key, final String value) {
78+
Bundle bundle = new Bundle();
79+
bundle.putString(key, value);
80+
InstrumentationRegistry.getInstrumentation().sendStatus(0, bundle);
81+
}
82+
}
Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,61 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<!--
3-
Copyright (c) Meta Platforms, Inc. and affiliates.
4-
All rights reserved.
5-
6-
This source code is licensed under the BSD-style license found in the
7-
LICENSE file in the root directory of this source tree.
8-
-->
92
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
10-
xmlns:tools="http://schemas.android.com/tools">
3+
xmlns:tools="http://schemas.android.com/tools"
4+
package="org.pytorch.torchchat">
5+
6+
<uses-sdk
7+
android:maxSdkVersion="40"
8+
android:minSdkVersion="28"
9+
android:targetSdkVersion="34" />
10+
11+
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
12+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
13+
<uses-permission android:name="android.permission.CAMERA" />
14+
15+
<uses-feature android:name="android.hardware.camera" />
1116

1217
<application
18+
android:name=".ETLogging"
1319
android:allowBackup="true"
1420
android:dataExtractionRules="@xml/data_extraction_rules"
21+
android:extractNativeLibs="true"
1522
android:fullBackupContent="@xml/backup_rules"
16-
android:icon="@mipmap/ic_launcher"
23+
android:icon="@drawable/logo"
1724
android:label="@string/app_name"
18-
android:roundIcon="@mipmap/ic_launcher_round"
1925
android:supportsRtl="true"
20-
android:theme="@style/Theme.torchchat"
21-
tools:targetApi="31">
26+
android:theme="@style/Theme.AppCompat.Light.NoActionBar"
27+
tools:targetApi="34">
28+
<activity
29+
android:name=".LogsActivity"
30+
android:exported="false" />
31+
<activity
32+
android:name=".SettingsActivity"
33+
android:exported="false" />
34+
35+
<uses-native-library
36+
android:name="libcdsprpc.so"
37+
android:required="false" />
38+
2239
<activity
2340
android:name=".MainActivity"
2441
android:exported="true"
2542
android:label="@string/app_name"
26-
android:theme="@style/Theme.torchchat">
43+
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
2744
<intent-filter>
2845
<action android:name="android.intent.action.MAIN" />
2946

3047
<category android:name="android.intent.category.LAUNCHER" />
3148
</intent-filter>
3249
</activity>
50+
51+
<activity
52+
android:name=".LlmBenchmarkRunner"
53+
android:exported="true">
54+
<intent-filter>
55+
<action android:name="org.pytorch.torchchat.BENCHMARK" />
56+
</intent-filter>
57+
</activity>
58+
3359
</application>
3460

3561
</manifest>

0 commit comments

Comments
 (0)