Skip to content

Commit e7e8647

Browse files
authored
Add a new MiniBench apk for benchmarking - app skeleton
So far it supports generic model (not LLM). Later we can combine two Build: ``` cd extension/android/benchmark mkdir app/libs cp <executorch.aar> app/libs/executorch.aar ./gradlew :app:installDebug When we have library changes ready (#5000), we can start trying it adb shell am start -n org.pytorch.minibench/org.pytorch.minibench.BenchmarkActivity --es model_path /data/local/tmp/model.pte adb shell run-as org.pytorch.minibench cat files/benchmark_results.txt ``` Pull Request resolved: #5015
1 parent 47ac24b commit e7e8647

File tree

18 files changed

+529
-0
lines changed

18 files changed

+529
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx
15+
local.properties
16+
*.aar
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
plugins {
2+
id("com.android.application")
3+
}
4+
5+
android {
6+
namespace = "org.pytorch.minibench"
7+
compileSdk = 34
8+
9+
defaultConfig {
10+
applicationId = "org.pytorch.minibench"
11+
minSdk = 28
12+
targetSdk = 33
13+
versionCode = 1
14+
versionName = "1.0"
15+
16+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
isMinifyEnabled = false
22+
proguardFiles(
23+
getDefaultProguardFile("proguard-android-optimize.txt"),
24+
"proguard-rules.pro"
25+
)
26+
}
27+
}
28+
compileOptions {
29+
sourceCompatibility = JavaVersion.VERSION_1_8
30+
targetCompatibility = JavaVersion.VERSION_1_8
31+
}
32+
}
33+
34+
dependencies {
35+
implementation(files("libs/executorch.aar"))
36+
implementation("com.facebook.soloader:soloader:0.10.5")
37+
implementation("com.facebook.fbjni:fbjni:0.5.1")
38+
testImplementation("junit:junit:4.13.2")
39+
androidTestImplementation("androidx.test.ext:junit:1.2.1")
40+
androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
41+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.pytorch.minibench;
2+
3+
import android.content.Context;
4+
5+
import androidx.test.platform.app.InstrumentationRegistry;
6+
import androidx.test.ext.junit.runners.AndroidJUnit4;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import static org.junit.Assert.*;
12+
13+
/**
14+
* Instrumented test, which will execute on an Android device.
15+
*
16+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
17+
*/
18+
@RunWith(AndroidJUnit4.class)
19+
public class ExampleInstrumentedTest {
20+
@Test
21+
public void useAppContext() {
22+
// Context of the app under test.
23+
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
24+
assertEquals("org.pytorch.minibench", appContext.getPackageName());
25+
}
26+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools">
4+
5+
<application
6+
android:label="@string/app_name"
7+
android:supportsRtl="true"
8+
android:theme="@style/Theme.MiniBench"
9+
tools:targetApi="31">
10+
11+
<activity
12+
android:name=".BenchmarkActivity"
13+
android:exported="true">
14+
<intent-filter>
15+
<action android:name="org.pytorch.minibench.BENCHMARK" />
16+
</intent-filter>
17+
</activity>
18+
19+
</application>
20+
21+
</manifest>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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.minibench;
10+
11+
import android.app.Activity;
12+
import android.content.Intent;
13+
import android.os.Bundle;
14+
15+
import org.pytorch.executorch.Module;
16+
17+
import java.io.FileWriter;
18+
import java.io.IOException;
19+
20+
public class BenchmarkActivity extends Activity {
21+
@Override
22+
protected void onCreate(Bundle savedInstanceState) {
23+
super.onCreate(savedInstanceState);
24+
Intent intent = getIntent();
25+
String modelPath = intent.getStringExtra("model_path");
26+
int numIter = intent.getIntExtra("num_iter", 10);
27+
28+
// TODO: Format the string with a parsable format
29+
StringBuilder resultText = new StringBuilder();
30+
31+
Module module = Module.load(modelPath);
32+
for (int i = 0; i < numIter; i++) {
33+
long start = System.currentTimeMillis();
34+
module.forward();
35+
long forwardMs = System.currentTimeMillis() - start;
36+
resultText.append(forwardMs).append(";");
37+
}
38+
39+
try (FileWriter writer = new FileWriter(getFilesDir() + "/benchmark_results.txt")) {
40+
writer.write(resultText.toString());
41+
} catch (IOException e) {
42+
e.printStackTrace();
43+
}
44+
45+
}
46+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="purple_200">#FFBB86FC</color>
4+
<color name="purple_500">#FF6200EE</color>
5+
<color name="purple_700">#FF3700B3</color>
6+
<color name="teal_200">#FF03DAC5</color>
7+
<color name="teal_700">#FF018786</color>
8+
<color name="black">#FF000000</color>
9+
<color name="white">#FFFFFFFF</color>
10+
</resources>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="app_name">MiniBench</string>
3+
</resources>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources xmlns:tools="http://schemas.android.com/tools">
2+
<!-- Base application theme. -->
3+
<style name="Theme.MiniBench" parent="android:Theme.DeviceDefault">
4+
</style>
5+
</resources>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.pytorch.minibench;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
/**
8+
* Example local unit test, which will execute on the development machine (host).
9+
*
10+
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
11+
*/
12+
public class ExampleUnitTest {
13+
@Test
14+
public void addition_isCorrect() {
15+
assertEquals(4, 2 + 2);
16+
}
17+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
2+
plugins {
3+
id("com.android.application") version "8.1.0" apply false
4+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Project-wide Gradle settings.
2+
# IDE (e.g. Android Studio) users:
3+
# Gradle settings configured through the IDE *will override*
4+
# any settings specified in this file.
5+
# For more details on how to configure your build environment visit
6+
# http://www.gradle.org/docs/current/userguide/build_environment.html
7+
# Specifies the JVM arguments used for the daemon process.
8+
# The setting is particularly useful for tweaking memory settings.
9+
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10+
# When configured, Gradle will run in incubating parallel mode.
11+
# This option should only be used with decoupled projects. More details, visit
12+
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13+
# org.gradle.parallel=true
14+
# AndroidX package structure to make it clearer which packages are bundled with the
15+
# Android operating system, and which are packaged with your app's APK
16+
# https://developer.android.com/topic/libraries/support-library/androidx-rn
17+
android.useAndroidX=true
18+
# Enables namespacing of each library's R class so that its R class includes only the
19+
# resources declared in the library itself and none from the library's dependencies,
20+
# thereby reducing the size of the R class for that library
21+
android.nonTransitiveRClass=true
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Thu Aug 29 23:29:08 PDT 2024
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
5+
zipStoreBase=GRADLE_USER_HOME
6+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)