Convert minibench Java files to Kotlin#19760
Conversation
Summary: Convert BenchmarkActivity, BenchmarkMetric, LlmBenchmark, LlmModelRunner, and ModelRunner from Java to Kotlin. LlmModelRunnerCallback.kt was already Kotlin. Key changes: - Use Kotlin idioms: when expressions, data classes, string templates, scope functions, extension methods - Use lateinit/val where appropriate instead of mutable nullable fields - Replace anonymous Handler subclasses with private classes - Keep all public API signatures compatible - No BUCK changes needed (OSS-only gradle build) Differential Revision: D106195816
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/19760
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 New Failure, 2 Unrelated Failures, 1 Unclassified FailureAs of commit c035397 with merge base 03e14ef ( NEW FAILURE - The following job has failed:
UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:
BROKEN TRUNK - The following jobs failed but were present on the merge base:👉 Rebase onto the `viable/strict` branch to avoid these failures
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@kirklandsign has exported this pull request. If you are a Meta employee, you can view the originating Diff in D106195816. |
|
@claude review |
This PR needs a
|
|
Claude finished @kirklandsign's task in 1m 33s —— View job Review: Convert minibench Java files to Kotlin
Overall this is a clean, well-done Java-to-Kotlin conversion. The Kotlin idioms are used appropriately and the code is more concise and readable. A few observations: Bug fix (good catch)
mModule = new LlmModule(mModelFilePath, mTokenizerFilePath, 0.8f);The Kotlin version correctly passes through the val module: LlmModule = LlmModule(modelFilePath, tokenizerFilePath, temperature)Nice fix. Minor observations
VerdictLooks good — clean idiomatic conversion with proper use of |
There was a problem hiding this comment.
Pull request overview
This PR migrates the Android minibench benchmark app’s core runner/metric/activity logic from Java to Kotlin, aiming to preserve behavior and public surfaces while adopting Kotlin idioms.
Changes:
- Replaced Java implementations of
BenchmarkActivity,BenchmarkMetric,ModelRunner,LlmBenchmark, andLlmModelRunnerwith Kotlin equivalents. - Refactored background-thread message handling to dedicated
Handlerclasses and updated call sites accordingly. - Updated the sample unit test to idiomatic Kotlin imports/syntax.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| extension/benchmark/android/benchmark/app/src/test/java/org/pytorch/minibench/ExampleUnitTest.kt | Kotlin syntax/import cleanup for the example unit test. |
| extension/benchmark/android/benchmark/app/src/main/java/org/pytorch/minibench/ModelRunner.kt | Kotlin port of model load/forward benchmarking and metric emission. |
| extension/benchmark/android/benchmark/app/src/main/java/org/pytorch/minibench/ModelRunner.java | Removed Java implementation after Kotlin migration. |
| extension/benchmark/android/benchmark/app/src/main/java/org/pytorch/minibench/LlmModelRunner.kt | Kotlin port of LLM module worker-thread runner and callbacks. |
| extension/benchmark/android/benchmark/app/src/main/java/org/pytorch/minibench/LlmModelRunner.java | Removed Java implementation after Kotlin migration. |
| extension/benchmark/android/benchmark/app/src/main/java/org/pytorch/minibench/LlmBenchmark.kt | Kotlin port of LLM benchmark orchestration and metrics. |
| extension/benchmark/android/benchmark/app/src/main/java/org/pytorch/minibench/LlmBenchmark.java | Removed Java implementation after Kotlin migration. |
| extension/benchmark/android/benchmark/app/src/main/java/org/pytorch/minibench/BenchmarkMetric.kt | Kotlin port of metric/model parsing + device info container. |
| extension/benchmark/android/benchmark/app/src/main/java/org/pytorch/minibench/BenchmarkMetric.java | Removed Java implementation after Kotlin migration. |
| extension/benchmark/android/benchmark/app/src/main/java/org/pytorch/minibench/BenchmarkActivity.kt | Kotlin port of benchmark Activity startup, worker thread, and result writing. |
| extension/benchmark/android/benchmark/app/src/main/java/org/pytorch/minibench/BenchmarkActivity.java | Removed Java implementation after Kotlin migration. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| results.add( | ||
| BenchmarkMetric( | ||
| benchmarkModel, | ||
| "avg_inference_latency(ms)", | ||
| latency.average(), | ||
| 0.0, | ||
| )) | ||
| results.add( | ||
| BenchmarkMetric( | ||
| benchmarkModel, | ||
| "trimmean_inference_latency(ms)", | ||
| trimmed.average(), |
|
|
||
| results.add( | ||
| BenchmarkMetric( | ||
| benchmarkModel, | ||
| "avg_inference_latency(ms)", | ||
| latency.average(), | ||
| 0.0, | ||
| )) | ||
| results.add( | ||
| BenchmarkMetric( | ||
| benchmarkModel, | ||
| "trimmean_inference_latency(ms)", | ||
| trimmed.average(), |
| runner = LlmModelRunner(modelFile, tokenizerPath, temperature, this) | ||
| statsInfo.loadStart = System.nanoTime() |
|
|
||
| handler.sendEmptyMessage(BenchmarkHandler.MESSAGE_RUN_BENCHMARK) | ||
| } | ||
|
|
| val module: LlmModule = LlmModule(modelFilePath, tokenizerFilePath, temperature) | ||
| private val handlerThread: HandlerThread = HandlerThread("LlmModelRunner") | ||
| private val handler: Handler | ||
|
|
||
| init { | ||
| handlerThread.start() | ||
| handler = LlmModelRunnerHandler(handlerThread.looper, this) | ||
| handler.sendEmptyMessage(LlmModelRunnerHandler.MESSAGE_LOAD_MODEL) | ||
| } |
| class DeviceInfo { | ||
| val device: String = Build.BRAND | ||
| val arch: String = Build.MODEL | ||
| val os: String = "Android ${Build.VERSION.RELEASE}" | ||
| val totalMem: Long = ActivityManager.MemoryInfo().totalMem | ||
| val availMem: Long = ActivityManager.MemoryInfo().availMem | ||
| } |
Summary:
Convert BenchmarkActivity, BenchmarkMetric, LlmBenchmark, LlmModelRunner,
and ModelRunner from Java to Kotlin. LlmModelRunnerCallback.kt was already
Kotlin.
Key changes:
scope functions, extension methods
Differential Revision: D106195816