Skip to content

Commit 51bd8ed

Browse files
phaitingHaiting Pu
andauthored
Introduce ktfmt for kotlin files under executorch_android (#15094)
### Summary The google-jave-format doesn't work for kotlin file, this diff introduced the standard kotlin format - ktfmt. run $./gradlew spotlessCheck to see the format issues run $./gradlew spotlessApply to apply the kotlin format. ### Test plan run via CI jobs Co-authored-by: Haiting Pu <[email protected]>
1 parent e43315c commit 51bd8ed

File tree

10 files changed

+1076
-1078
lines changed

10 files changed

+1076
-1078
lines changed

extension/android/executorch_android/build.gradle

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,17 @@
1010
plugins {
1111
id "com.android.library" version "8.9.0"
1212
id "com.vanniktech.maven.publish" version "0.31.0"
13+
id 'com.diffplug.spotless' version '8.0.0'
1314
alias(libs.plugins.jetbrains.kotlin.android)
1415
}
1516

17+
spotless {
18+
kotlin {
19+
target '**/*.kt'
20+
ktfmt()
21+
}
22+
}
23+
1624
def qnnVersion = System.properties['qnnVersion']
1725
def execuTorchVersion = System.properties['execuTorchVersion']
1826
def flavor = System.properties['flavor']
@@ -37,7 +45,7 @@ android {
3745
jniLibs.srcDirs = ['../../../cmake-out-android-so/']
3846
}
3947
androidTest {
40-
resources.srcDirs += [ 'src/androidTest/resources' ]
48+
resources.srcDirs += ['src/androidTest/resources']
4149
}
4250
}
4351
kotlinOptions {
@@ -46,7 +54,7 @@ android {
4654
}
4755

4856
task copyTestRes(type: Exec) {
49-
commandLine 'bash', 'android_test_setup.sh'
57+
commandLine 'bash', 'android_test_setup.sh'
5058
}
5159

5260
dependencies {
@@ -67,36 +75,36 @@ dependencies {
6775
}
6876

6977
mavenPublishing {
70-
publishToMavenCentral()
71-
signAllPublications()
72-
73-
coordinates("org.pytorch", "executorch-android" + (flavor ? "-" + flavor : ""), execuTorchVersion ? execuTorchVersion : "1.0.0-SNAPSHOT")
74-
75-
pom {
76-
name = "ExecuTorch Android"
77-
description = "ExecuTorch Android API"
78-
inceptionYear = "2025"
79-
url = "https://github.com/pytorch/executorch/"
80-
licenses {
81-
license {
82-
name = "BSD 3-Clause"
83-
url = "https://github.com/pytorch/executorch/blob/main/LICENSE"
84-
distribution = "https://github.com/pytorch/executorch/blob/main/LICENSE"
85-
}
86-
}
87-
developers {
88-
developer {
89-
id = "pytorch"
90-
name = "pytorch"
78+
publishToMavenCentral()
79+
signAllPublications()
80+
81+
coordinates("org.pytorch", "executorch-android" + (flavor ? "-" + flavor : ""), execuTorchVersion ? execuTorchVersion : "1.0.0-SNAPSHOT")
82+
83+
pom {
84+
name = "ExecuTorch Android"
85+
description = "ExecuTorch Android API"
86+
inceptionYear = "2025"
9187
url = "https://github.com/pytorch/executorch/"
92-
}
93-
}
94-
scm {
95-
url = "https://github.com/pytorch/executorch.git"
96-
connection = "scm:git:https://github.com/pytorch/executorch"
97-
developerConnection = "scm:git:[email protected]:pytorch/executorch.git"
88+
licenses {
89+
license {
90+
name = "BSD 3-Clause"
91+
url = "https://github.com/pytorch/executorch/blob/main/LICENSE"
92+
distribution = "https://github.com/pytorch/executorch/blob/main/LICENSE"
93+
}
94+
}
95+
developers {
96+
developer {
97+
id = "pytorch"
98+
name = "pytorch"
99+
url = "https://github.com/pytorch/executorch/"
100+
}
101+
}
102+
scm {
103+
url = "https://github.com/pytorch/executorch.git"
104+
connection = "scm:git:https://github.com/pytorch/executorch"
105+
developerConnection = "scm:git:[email protected]:pytorch/executorch.git"
106+
}
98107
}
99-
}
100108

101109
}
102110

extension/android/executorch_android/src/androidTest/java/org/pytorch/executorch/LlmModuleInstrumentationTest.kt

Lines changed: 71 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
package org.pytorch.executorch
99

1010
import android.Manifest
11-
import androidx.test.InstrumentationRegistry
1211
import androidx.test.ext.junit.runners.AndroidJUnit4
1312
import androidx.test.rule.GrantPermissionRule
1413
import java.io.File
@@ -17,9 +16,7 @@ import java.net.URISyntaxException
1716
import org.apache.commons.io.FileUtils
1817
import org.json.JSONException
1918
import org.json.JSONObject
20-
import org.junit.Assert
2119
import org.junit.Assert.assertEquals
22-
import org.junit.Assert.assertThat
2320
import org.junit.Assert.assertTrue
2421
import org.junit.Before
2522
import org.junit.Rule
@@ -32,88 +29,87 @@ import org.pytorch.executorch.extension.llm.LlmModule
3229
/** Unit tests for [org.pytorch.executorch.extension.llm.LlmModule]. */
3330
@RunWith(AndroidJUnit4::class)
3431
class LlmModuleInstrumentationTest : LlmCallback {
35-
private val results: MutableList<String> = ArrayList()
36-
private val tokensPerSecond: MutableList<Float> = ArrayList()
37-
private lateinit var llmModule: LlmModule
32+
private val results: MutableList<String> = ArrayList()
33+
private val tokensPerSecond: MutableList<Float> = ArrayList()
34+
private lateinit var llmModule: LlmModule
3835

39-
@Before
40-
@Throws(IOException::class)
41-
fun setUp() {
42-
// copy zipped test resources to local device
43-
val addPteFile = File(getTestFilePath(TEST_FILE_NAME))
44-
var inputStream = javaClass.getResourceAsStream(TEST_FILE_NAME)
45-
FileUtils.copyInputStreamToFile(inputStream, addPteFile)
46-
inputStream.close()
36+
@Before
37+
@Throws(IOException::class)
38+
fun setUp() {
39+
// copy zipped test resources to local device
40+
val addPteFile = File(getTestFilePath(TEST_FILE_NAME))
41+
var inputStream = javaClass.getResourceAsStream(TEST_FILE_NAME)
42+
FileUtils.copyInputStreamToFile(inputStream, addPteFile)
43+
inputStream.close()
4744

48-
val tokenizerFile = File(getTestFilePath(TOKENIZER_FILE_NAME))
49-
inputStream = javaClass.getResourceAsStream(TOKENIZER_FILE_NAME)
50-
FileUtils.copyInputStreamToFile(inputStream, tokenizerFile)
51-
inputStream.close()
45+
val tokenizerFile = File(getTestFilePath(TOKENIZER_FILE_NAME))
46+
inputStream = javaClass.getResourceAsStream(TOKENIZER_FILE_NAME)
47+
FileUtils.copyInputStreamToFile(inputStream, tokenizerFile)
48+
inputStream.close()
5249

53-
llmModule =
54-
LlmModule(getTestFilePath(TEST_FILE_NAME), getTestFilePath(TOKENIZER_FILE_NAME), 0.0f)
55-
}
50+
llmModule =
51+
LlmModule(getTestFilePath(TEST_FILE_NAME), getTestFilePath(TOKENIZER_FILE_NAME), 0.0f)
52+
}
5653

57-
@get:Rule
58-
var runtimePermissionRule: GrantPermissionRule =
59-
GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE)
54+
@get:Rule
55+
var runtimePermissionRule: GrantPermissionRule =
56+
GrantPermissionRule.grant(Manifest.permission.READ_EXTERNAL_STORAGE)
6057

61-
@Test
62-
@Throws(IOException::class, URISyntaxException::class)
63-
fun testGenerate() {
64-
val loadResult = llmModule.load()
65-
// Check that the model can be load successfully
66-
assertEquals(OK.toLong(), loadResult.toLong())
58+
@Test
59+
@Throws(IOException::class, URISyntaxException::class)
60+
fun testGenerate() {
61+
val loadResult = llmModule.load()
62+
// Check that the model can be load successfully
63+
assertEquals(OK.toLong(), loadResult.toLong())
6764

68-
llmModule.generate(TEST_PROMPT, SEQ_LEN, this@LlmModuleInstrumentationTest)
69-
assertEquals(results.size.toLong(), SEQ_LEN.toLong())
70-
assertTrue(tokensPerSecond[tokensPerSecond.size - 1] > 0)
71-
}
65+
llmModule.generate(TEST_PROMPT, SEQ_LEN, this@LlmModuleInstrumentationTest)
66+
assertEquals(results.size.toLong(), SEQ_LEN.toLong())
67+
assertTrue(tokensPerSecond[tokensPerSecond.size - 1] > 0)
68+
}
7269

73-
@Test
74-
@Throws(IOException::class, URISyntaxException::class)
75-
fun testGenerateAndStop() {
76-
llmModule.generate(
77-
TEST_PROMPT,
78-
SEQ_LEN,
79-
object : LlmCallback {
80-
override fun onResult(result: String) {
81-
this@LlmModuleInstrumentationTest.onResult(result)
82-
llmModule.stop()
83-
}
70+
@Test
71+
@Throws(IOException::class, URISyntaxException::class)
72+
fun testGenerateAndStop() {
73+
llmModule.generate(
74+
TEST_PROMPT,
75+
SEQ_LEN,
76+
object : LlmCallback {
77+
override fun onResult(result: String) {
78+
this@LlmModuleInstrumentationTest.onResult(result)
79+
llmModule.stop()
80+
}
8481

85-
override fun onStats(stats: String) {
86-
this@LlmModuleInstrumentationTest.onStats(stats)
87-
}
88-
},
89-
)
82+
override fun onStats(stats: String) {
83+
this@LlmModuleInstrumentationTest.onStats(stats)
84+
}
85+
},
86+
)
9087

91-
val stoppedResultSize = results.size
92-
assertTrue(stoppedResultSize < SEQ_LEN)
93-
}
88+
val stoppedResultSize = results.size
89+
assertTrue(stoppedResultSize < SEQ_LEN)
90+
}
9491

95-
override fun onResult(result: String) {
96-
results.add(result)
97-
}
92+
override fun onResult(result: String) {
93+
results.add(result)
94+
}
9895

99-
override fun onStats(stats: String) {
100-
var tps = 0f
101-
try {
102-
val jsonObject = JSONObject(stats)
103-
val numGeneratedTokens = jsonObject.getInt("generated_tokens")
104-
val inferenceEndMs = jsonObject.getInt("inference_end_ms")
105-
val promptEvalEndMs = jsonObject.getInt("prompt_eval_end_ms")
106-
tps = numGeneratedTokens.toFloat() / (inferenceEndMs - promptEvalEndMs) * 1000
107-
tokensPerSecond.add(tps)
108-
} catch (_: JSONException) {
109-
}
110-
}
96+
override fun onStats(stats: String) {
97+
var tps = 0f
98+
try {
99+
val jsonObject = JSONObject(stats)
100+
val numGeneratedTokens = jsonObject.getInt("generated_tokens")
101+
val inferenceEndMs = jsonObject.getInt("inference_end_ms")
102+
val promptEvalEndMs = jsonObject.getInt("prompt_eval_end_ms")
103+
tps = numGeneratedTokens.toFloat() / (inferenceEndMs - promptEvalEndMs) * 1000
104+
tokensPerSecond.add(tps)
105+
} catch (_: JSONException) {}
106+
}
111107

112-
companion object {
113-
private const val TEST_FILE_NAME = "/stories.pte"
114-
private const val TOKENIZER_FILE_NAME = "/tokenizer.bin"
115-
private const val TEST_PROMPT = "Hello"
116-
private const val OK = 0x00
117-
private const val SEQ_LEN = 32
118-
}
108+
companion object {
109+
private const val TEST_FILE_NAME = "/stories.pte"
110+
private const val TOKENIZER_FILE_NAME = "/tokenizer.bin"
111+
private const val TEST_PROMPT = "Hello"
112+
private const val OK = 0x00
113+
private const val SEQ_LEN = 32
114+
}
119115
}

0 commit comments

Comments
 (0)