Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 38 additions & 30 deletions extension/android/executorch_android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@
plugins {
id "com.android.library" version "8.9.0"
id "com.vanniktech.maven.publish" version "0.31.0"
id 'com.diffplug.spotless' version '8.0.0'
alias(libs.plugins.jetbrains.kotlin.android)
}

spotless {
kotlin {
target '**/*.kt'
ktfmt()
}
}

def qnnVersion = System.properties['qnnVersion']
def execuTorchVersion = System.properties['execuTorchVersion']
def flavor = System.properties['flavor']
Expand All @@ -37,7 +45,7 @@ android {
jniLibs.srcDirs = ['../../../cmake-out-android-so/']
}
androidTest {
resources.srcDirs += [ 'src/androidTest/resources' ]
resources.srcDirs += ['src/androidTest/resources']
}
}
kotlinOptions {
Expand All @@ -46,7 +54,7 @@ android {
}

task copyTestRes(type: Exec) {
commandLine 'bash', 'android_test_setup.sh'
commandLine 'bash', 'android_test_setup.sh'
}

dependencies {
Expand All @@ -67,36 +75,36 @@ dependencies {
}

mavenPublishing {
publishToMavenCentral()
signAllPublications()

coordinates("org.pytorch", "executorch-android" + (flavor ? "-" + flavor : ""), execuTorchVersion ? execuTorchVersion : "1.0.0-SNAPSHOT")

pom {
name = "ExecuTorch Android"
description = "ExecuTorch Android API"
inceptionYear = "2025"
url = "https://github.com/pytorch/executorch/"
licenses {
license {
name = "BSD 3-Clause"
url = "https://github.com/pytorch/executorch/blob/main/LICENSE"
distribution = "https://github.com/pytorch/executorch/blob/main/LICENSE"
}
}
developers {
developer {
id = "pytorch"
name = "pytorch"
publishToMavenCentral()
signAllPublications()

coordinates("org.pytorch", "executorch-android" + (flavor ? "-" + flavor : ""), execuTorchVersion ? execuTorchVersion : "1.0.0-SNAPSHOT")

pom {
name = "ExecuTorch Android"
description = "ExecuTorch Android API"
inceptionYear = "2025"
url = "https://github.com/pytorch/executorch/"
}
}
scm {
url = "https://github.com/pytorch/executorch.git"
connection = "scm:git:https://github.com/pytorch/executorch"
developerConnection = "scm:git:[email protected]:pytorch/executorch.git"
licenses {
license {
name = "BSD 3-Clause"
url = "https://github.com/pytorch/executorch/blob/main/LICENSE"
distribution = "https://github.com/pytorch/executorch/blob/main/LICENSE"
}
}
developers {
developer {
id = "pytorch"
name = "pytorch"
url = "https://github.com/pytorch/executorch/"
}
}
scm {
url = "https://github.com/pytorch/executorch.git"
connection = "scm:git:https://github.com/pytorch/executorch"
developerConnection = "scm:git:[email protected]:pytorch/executorch.git"
}
}
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
package org.pytorch.executorch

import android.Manifest
import androidx.test.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.rule.GrantPermissionRule
import java.io.File
Expand All @@ -17,9 +16,7 @@ import java.net.URISyntaxException
import org.apache.commons.io.FileUtils
import org.json.JSONException
import org.json.JSONObject
import org.junit.Assert
import org.junit.Assert.assertEquals
import org.junit.Assert.assertThat
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
Expand All @@ -32,88 +29,87 @@ import org.pytorch.executorch.extension.llm.LlmModule
/** Unit tests for [org.pytorch.executorch.extension.llm.LlmModule]. */
@RunWith(AndroidJUnit4::class)
class LlmModuleInstrumentationTest : LlmCallback {
private val results: MutableList<String> = ArrayList()
private val tokensPerSecond: MutableList<Float> = ArrayList()
private lateinit var llmModule: LlmModule
private val results: MutableList<String> = ArrayList()
private val tokensPerSecond: MutableList<Float> = ArrayList()
private lateinit var llmModule: LlmModule

@Before
@Throws(IOException::class)
fun setUp() {
// copy zipped test resources to local device
val addPteFile = File(getTestFilePath(TEST_FILE_NAME))
var inputStream = javaClass.getResourceAsStream(TEST_FILE_NAME)
FileUtils.copyInputStreamToFile(inputStream, addPteFile)
inputStream.close()
@Before
@Throws(IOException::class)
fun setUp() {
// copy zipped test resources to local device
val addPteFile = File(getTestFilePath(TEST_FILE_NAME))
var inputStream = javaClass.getResourceAsStream(TEST_FILE_NAME)
FileUtils.copyInputStreamToFile(inputStream, addPteFile)
inputStream.close()

val tokenizerFile = File(getTestFilePath(TOKENIZER_FILE_NAME))
inputStream = javaClass.getResourceAsStream(TOKENIZER_FILE_NAME)
FileUtils.copyInputStreamToFile(inputStream, tokenizerFile)
inputStream.close()
val tokenizerFile = File(getTestFilePath(TOKENIZER_FILE_NAME))
inputStream = javaClass.getResourceAsStream(TOKENIZER_FILE_NAME)
FileUtils.copyInputStreamToFile(inputStream, tokenizerFile)
inputStream.close()

llmModule =
LlmModule(getTestFilePath(TEST_FILE_NAME), getTestFilePath(TOKENIZER_FILE_NAME), 0.0f)
}
llmModule =
LlmModule(getTestFilePath(TEST_FILE_NAME), getTestFilePath(TOKENIZER_FILE_NAME), 0.0f)
}

@get:Rule
var runtimePermissionRule: GrantPermissionRule =
GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE)
@get:Rule
var runtimePermissionRule: GrantPermissionRule =
GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE)

@Test
@Throws(IOException::class, URISyntaxException::class)
fun testGenerate() {
val loadResult = llmModule.load()
// Check that the model can be load successfully
assertEquals(OK.toLong(), loadResult.toLong())
@Test
@Throws(IOException::class, URISyntaxException::class)
fun testGenerate() {
val loadResult = llmModule.load()
// Check that the model can be load successfully
assertEquals(OK.toLong(), loadResult.toLong())

llmModule.generate(TEST_PROMPT, SEQ_LEN, this@LlmModuleInstrumentationTest)
assertEquals(results.size.toLong(), SEQ_LEN.toLong())
assertTrue(tokensPerSecond[tokensPerSecond.size - 1] > 0)
}
llmModule.generate(TEST_PROMPT, SEQ_LEN, this@LlmModuleInstrumentationTest)
assertEquals(results.size.toLong(), SEQ_LEN.toLong())
assertTrue(tokensPerSecond[tokensPerSecond.size - 1] > 0)
}

@Test
@Throws(IOException::class, URISyntaxException::class)
fun testGenerateAndStop() {
llmModule.generate(
TEST_PROMPT,
SEQ_LEN,
object : LlmCallback {
override fun onResult(result: String) {
[email protected](result)
llmModule.stop()
}
@Test
@Throws(IOException::class, URISyntaxException::class)
fun testGenerateAndStop() {
llmModule.generate(
TEST_PROMPT,
SEQ_LEN,
object : LlmCallback {
override fun onResult(result: String) {
[email protected](result)
llmModule.stop()
}

override fun onStats(stats: String) {
[email protected](stats)
}
},
)
override fun onStats(stats: String) {
[email protected](stats)
}
},
)

val stoppedResultSize = results.size
assertTrue(stoppedResultSize < SEQ_LEN)
}
val stoppedResultSize = results.size
assertTrue(stoppedResultSize < SEQ_LEN)
}

override fun onResult(result: String) {
results.add(result)
}
override fun onResult(result: String) {
results.add(result)
}

override fun onStats(stats: String) {
var tps = 0f
try {
val jsonObject = JSONObject(stats)
val numGeneratedTokens = jsonObject.getInt("generated_tokens")
val inferenceEndMs = jsonObject.getInt("inference_end_ms")
val promptEvalEndMs = jsonObject.getInt("prompt_eval_end_ms")
tps = numGeneratedTokens.toFloat() / (inferenceEndMs - promptEvalEndMs) * 1000
tokensPerSecond.add(tps)
} catch (_: JSONException) {
}
}
override fun onStats(stats: String) {
var tps = 0f
try {
val jsonObject = JSONObject(stats)
val numGeneratedTokens = jsonObject.getInt("generated_tokens")
val inferenceEndMs = jsonObject.getInt("inference_end_ms")
val promptEvalEndMs = jsonObject.getInt("prompt_eval_end_ms")
tps = numGeneratedTokens.toFloat() / (inferenceEndMs - promptEvalEndMs) * 1000
tokensPerSecond.add(tps)
} catch (_: JSONException) {}
}

companion object {
private const val TEST_FILE_NAME = "/stories.pte"
private const val TOKENIZER_FILE_NAME = "/tokenizer.bin"
private const val TEST_PROMPT = "Hello"
private const val OK = 0x00
private const val SEQ_LEN = 32
}
companion object {
private const val TEST_FILE_NAME = "/stories.pte"
private const val TOKENIZER_FILE_NAME = "/tokenizer.bin"
private const val TEST_PROMPT = "Hello"
private const val OK = 0x00
private const val SEQ_LEN = 32
}
}
Loading
Loading