Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API/Gradle: Add an option to enable supporting llama2.c #106

Merged
merged 2 commits into from
Sep 20, 2024
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
31 changes: 19 additions & 12 deletions externals/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream
import org.jetbrains.kotlin.daemon.common.toHexString
import xyz.ronella.gradle.plugin.simple.git.task.GitTask
import java.io.FileOutputStream
import java.io.IOException
import java.net.URL
import java.nio.channels.Channels
import java.security.MessageDigest
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.Path
import kotlin.io.path.createDirectories
import kotlin.io.path.deleteIfExists
import kotlin.io.path.deleteRecursively
import kotlin.io.path.exists
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.isDirectory
import kotlin.io.path.Path
import org.apache.commons.compress.compressors.xz.XZCompressorInputStream
import org.jetbrains.kotlin.daemon.common.toHexString
import xyz.ronella.gradle.plugin.simple.git.task.*

plugins {
base
Expand Down Expand Up @@ -61,14 +61,15 @@ tasks {
// GStreamer Android Universal
val gstVersion = try {
libs.versions.gstreamer.get()
} catch(e: IllegalStateException) {
} catch (e: IllegalStateException) {
error("Failed to resolve the variable that defines the required GStreamer version.")
}

when {
!project.hasProperty("dir.gstAndroid") -> {
error("Could not find the project property, 'dir.gstAndroid'.")
}

project.properties["dir.gstAndroid"].toString().isBlank() -> {
error("The project property, 'dir.gstAndroid', is set to an invalid value.")
}
Expand All @@ -95,9 +96,11 @@ tasks {
!project.hasProperty("dir.tfliteAndroid") -> {
false
}

project.properties["dir.tfliteAndroid"].toString().isBlank() -> {
false
}

else -> {
true
}
Expand Down Expand Up @@ -133,7 +136,10 @@ tasks {
println("This step may take some time to complete...")

val downloadableName = "$tarFileName$downloadableFormat"
downloadFile(URL("$url/$downloadableName"), downloadablePath.resolve(downloadableName).toString())
downloadFile(
URL("$url/$downloadableName"),
downloadablePath.resolve(downloadableName).toString()
)

val verified = if (digestFileName.isEmpty().or(digestFileName.isBlank())) {
true
Expand All @@ -159,9 +165,10 @@ tasks {
when {
downloadableName.endsWith(".xz") -> {
try {
File("$downloadablePath/$downloadableName").inputStream().buffered().use { bufferedIn ->
XZCompressorInputStream(bufferedIn).toFile("$downloadablePath/$tarFileName")
}
File("$downloadablePath/$downloadableName").inputStream().buffered()
.use { bufferedIn ->
XZCompressorInputStream(bufferedIn).toFile("$downloadablePath/$tarFileName")
}
downloadablePath.resolve(downloadableName).deleteIfExists()
} catch (e: IOException) {
println("Failed to decompress $downloadablePath/$downloadableName")
Expand All @@ -174,8 +181,8 @@ tasks {

register("copyFromTar") {
doLast {
for (downloadble in downloadables) {
val (tarFileName, targetDir, _, isEnabled) = downloadble
for (downloadable in downloadables) {
val (tarFileName, targetDir, _, isEnabled) = downloadable

if (!isEnabled) {
continue
Expand Down
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dir.tfliteAndroid=tensorflow-lite
dir.nnstreamer=nnstreamer
dir.nnstreamerEdge=nnstreamer-edge
dir.mlApi=ml-api
dir.llama2c=llama2.c
kotlin.code.style=official
android.useAndroidX=true
android.enableJetifier=true
Expand Down
20 changes: 20 additions & 0 deletions nnstreamer-api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,26 @@ android {
}
}
}

if (project.hasProperty("dir.llama2c")) {
val llama2cDir = properties["dir.llama2c"].toString()

llama2cDir.also { dir ->
val rootPath = externalDirPath.resolve(dir)
val enableLlama2c = rootPath.isDirectory()

arguments("ENABLE_LLAMA2C=$enableLlama2c")
if (!enableLlama2c) {
val msg =
"The property, 'dir.llama2c', is specified in 'gradle.properties', " +
"but failed to resolve it to $rootPath. llama2.c support will be disabled."
project.logger.lifecycle("WARNING: $msg")
return@also
}

arguments("LLAMA2C_DIR=$rootPath")
}
}
}
}
}
Expand Down