Skip to content

Commit 9791f40

Browse files
authored
android : module (ggml-org#7502)
* move ndk code to a new library * add gradle file
1 parent 902184d commit 9791f40

File tree

15 files changed

+213
-49
lines changed

15 files changed

+213
-49
lines changed

examples/llama.android/app/build.gradle.kts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ android {
77
namespace = "com.example.llama"
88
compileSdk = 34
99

10-
ndkVersion = "26.1.10909125"
11-
1210
defaultConfig {
1311
applicationId = "com.example.llama"
1412
minSdk = 33
@@ -20,17 +18,6 @@ android {
2018
vectorDrawables {
2119
useSupportLibrary = true
2220
}
23-
ndk {
24-
// Add NDK properties if wanted, e.g.
25-
// abiFilters += listOf("arm64-v8a")
26-
}
27-
externalNativeBuild {
28-
cmake {
29-
arguments += "-DCMAKE_BUILD_TYPE=Release"
30-
cppFlags += listOf()
31-
arguments += listOf()
32-
}
33-
}
3421
}
3522

3623
buildTypes {
@@ -55,17 +42,6 @@ android {
5542
composeOptions {
5643
kotlinCompilerExtensionVersion = "1.5.1"
5744
}
58-
packaging {
59-
resources {
60-
excludes += "/META-INF/{AL2.0,LGPL2.1}"
61-
}
62-
}
63-
externalNativeBuild {
64-
cmake {
65-
path = file("src/main/cpp/CMakeLists.txt")
66-
version = "3.22.1"
67-
}
68-
}
6945
}
7046

7147
dependencies {
@@ -78,6 +54,7 @@ dependencies {
7854
implementation("androidx.compose.ui:ui-graphics")
7955
implementation("androidx.compose.ui:ui-tooling-preview")
8056
implementation("androidx.compose.material3:material3")
57+
implementation(project(":llama"))
8158
testImplementation("junit:junit:4.13.2")
8259
androidTestImplementation("androidx.test.ext:junit:1.1.5")
8360
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

examples/llama.android/app/src/main/java/com/example/llama/MainViewModel.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.example.llama
22

3+
import android.llama.cpp.LLamaAndroid
34
import android.util.Log
45
import androidx.compose.runtime.getValue
56
import androidx.compose.runtime.mutableStateOf
@@ -9,7 +10,7 @@ import androidx.lifecycle.viewModelScope
910
import kotlinx.coroutines.flow.catch
1011
import kotlinx.coroutines.launch
1112

12-
class MainViewModel(private val llm: Llm = Llm.instance()): ViewModel() {
13+
class MainViewModel(private val llamaAndroid: LLamaAndroid = LLamaAndroid.instance()): ViewModel() {
1314
companion object {
1415
@JvmStatic
1516
private val NanosPerSecond = 1_000_000_000.0
@@ -28,7 +29,7 @@ class MainViewModel(private val llm: Llm = Llm.instance()): ViewModel() {
2829

2930
viewModelScope.launch {
3031
try {
31-
llm.unload()
32+
llamaAndroid.unload()
3233
} catch (exc: IllegalStateException) {
3334
messages += exc.message!!
3435
}
@@ -44,7 +45,7 @@ class MainViewModel(private val llm: Llm = Llm.instance()): ViewModel() {
4445
messages += ""
4546

4647
viewModelScope.launch {
47-
llm.send(text)
48+
llamaAndroid.send(text)
4849
.catch {
4950
Log.e(tag, "send() failed", it)
5051
messages += it.message!!
@@ -57,7 +58,7 @@ class MainViewModel(private val llm: Llm = Llm.instance()): ViewModel() {
5758
viewModelScope.launch {
5859
try {
5960
val start = System.nanoTime()
60-
val warmupResult = llm.bench(pp, tg, pl, nr)
61+
val warmupResult = llamaAndroid.bench(pp, tg, pl, nr)
6162
val end = System.nanoTime()
6263

6364
messages += warmupResult
@@ -70,7 +71,7 @@ class MainViewModel(private val llm: Llm = Llm.instance()): ViewModel() {
7071
return@launch
7172
}
7273

73-
messages += llm.bench(512, 128, 1, 3)
74+
messages += llamaAndroid.bench(512, 128, 1, 3)
7475
} catch (exc: IllegalStateException) {
7576
Log.e(tag, "bench() failed", exc)
7677
messages += exc.message!!
@@ -81,7 +82,7 @@ class MainViewModel(private val llm: Llm = Llm.instance()): ViewModel() {
8182
fun load(pathToModel: String) {
8283
viewModelScope.launch {
8384
try {
84-
llm.load(pathToModel)
85+
llamaAndroid.load(pathToModel)
8586
messages += "Loaded $pathToModel"
8687
} catch (exc: IllegalStateException) {
8788
Log.e(tag, "load() failed", exc)

examples/llama.android/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
plugins {
33
id("com.android.application") version "8.2.0" apply false
44
id("org.jetbrains.kotlin.android") version "1.9.0" apply false
5+
id("com.android.library") version "8.2.0" apply false
56
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build

examples/llama.android/app/src/main/cpp/CMakeLists.txt renamed to examples/llama.android/llama/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ add_subdirectory(../../../../../../ build-llama)
4242
# used in the AndroidManifest.xml file.
4343
add_library(${CMAKE_PROJECT_NAME} SHARED
4444
# List C/C++ source files with relative paths to this CMakeLists.txt.
45-
llama-android.cpp)
45+
llama-android.cpp)
4646

4747
# Specifies libraries CMake should link to your target library. You
4848
# can link libraries from various origins, such as libraries defined in this
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
plugins {
2+
id("com.android.library")
3+
id("org.jetbrains.kotlin.android")
4+
}
5+
6+
android {
7+
namespace = "android.llama.cpp"
8+
compileSdk = 34
9+
10+
defaultConfig {
11+
minSdk = 33
12+
13+
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
14+
consumerProguardFiles("consumer-rules.pro")
15+
ndk {
16+
// Add NDK properties if wanted, e.g.
17+
// abiFilters += listOf("arm64-v8a")
18+
}
19+
externalNativeBuild {
20+
cmake {
21+
arguments += "-DCMAKE_BUILD_TYPE=Release"
22+
cppFlags += listOf()
23+
arguments += listOf()
24+
25+
cppFlags("")
26+
}
27+
}
28+
}
29+
30+
buildTypes {
31+
release {
32+
isMinifyEnabled = false
33+
proguardFiles(
34+
getDefaultProguardFile("proguard-android-optimize.txt"),
35+
"proguard-rules.pro"
36+
)
37+
}
38+
}
39+
externalNativeBuild {
40+
cmake {
41+
path("src/main/cpp/CMakeLists.txt")
42+
version = "3.22.1"
43+
}
44+
}
45+
compileOptions {
46+
sourceCompatibility = JavaVersion.VERSION_1_8
47+
targetCompatibility = JavaVersion.VERSION_1_8
48+
}
49+
kotlinOptions {
50+
jvmTarget = "1.8"
51+
}
52+
53+
packaging {
54+
resources {
55+
excludes += "/META-INF/{AL2.0,LGPL2.1}"
56+
}
57+
}
58+
}
59+
60+
dependencies {
61+
62+
implementation("androidx.core:core-ktx:1.12.0")
63+
implementation("androidx.appcompat:appcompat:1.6.1")
64+
implementation("com.google.android.material:material:1.11.0")
65+
testImplementation("junit:junit:4.13.2")
66+
androidTestImplementation("androidx.test.ext:junit:1.1.5")
67+
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")
68+
}

examples/llama.android/llama/consumer-rules.pro

Whitespace-only changes.
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package android.llama.cpp
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("android.llama.cpp.test", appContext.packageName)
23+
}
24+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
4+
</manifest>

0 commit comments

Comments
 (0)