Skip to content

Commit 303bcce

Browse files
committed
support importing custom npu models
1 parent 3536f22 commit 303bcce

5 files changed

Lines changed: 334 additions & 21 deletions

File tree

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ android {
1212
minSdk = 28
1313
// minSdk = 31
1414
targetSdk = 35
15-
versionCode = 42
16-
versionName = "1.8.3"
15+
versionCode = 43
16+
versionName = "1.8.4"
1717

1818
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
1919
vectorDrawables {

app/src/main/java/io/github/xororz/localdream/data/Model.kt

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ data class Model(
280280
return getChipsetSuffix(soc) == "min"
281281
}
282282

283+
fun isQualcommDevice(): Boolean {
284+
val soc = getDeviceSoc()
285+
return soc.startsWith("SM") || soc.startsWith("QCS") || soc.startsWith("QCM")
286+
}
287+
283288
fun getChipsetSuffix(soc: String): String? {
284289
if (soc in chipsetModelSuffixes) {
285290
return chipsetModelSuffixes[soc]
@@ -392,8 +397,13 @@ class ModelRepository(private val context: Context) {
392397
modelsDir.listFiles()?.forEach { dir ->
393398
if (dir.isDirectory) {
394399
val finishedFile = File(dir, "finished")
400+
val npuCustomFile = File(dir, "npucustom")
401+
395402
if (finishedFile.exists()) {
396-
val customModel = createCustomModel(dir)
403+
val customModel = createCustomModel(dir, isNpu = false)
404+
customModels.add(customModel)
405+
} else if (npuCustomFile.exists()) {
406+
val customModel = createCustomModel(dir, isNpu = true)
397407
customModels.add(customModel)
398408
}
399409
}
@@ -403,28 +413,42 @@ class ModelRepository(private val context: Context) {
403413
return customModels
404414
}
405415

406-
private fun createCustomModel(modelDir: File): Model {
416+
private fun createCustomModel(modelDir: File, isNpu: Boolean = false): Model {
407417
val modelId = modelDir.name
408418
val files = mutableListOf<ModelFile>()
409419

410-
val commonFiles = listOf(
411-
"tokenizer.json" to "tokenizer",
412-
"clip.mnn" to "clip",
413-
"unet.mnn" to "unet",
414-
"vae_decoder.mnn" to "vae_decoder",
415-
"vae_encoder.mnn" to "vae_encoder"
416-
)
420+
if (isNpu) {
421+
modelDir.listFiles()?.forEach { file ->
422+
if (file.isFile && file.name != "npucustom") {
423+
files.add(
424+
ModelFile(
425+
name = file.name,
426+
displayName = file.nameWithoutExtension,
427+
uri = ""
428+
)
429+
)
430+
}
431+
}
432+
} else {
433+
val commonFiles = listOf(
434+
"tokenizer.json" to "tokenizer",
435+
"clip.mnn" to "clip",
436+
"unet.mnn" to "unet",
437+
"vae_decoder.mnn" to "vae_decoder",
438+
"vae_encoder.mnn" to "vae_encoder"
439+
)
417440

418-
commonFiles.forEach { (fileName, displayName) ->
419-
val file = File(modelDir, fileName)
420-
if (file.exists()) {
421-
files.add(
422-
ModelFile(
423-
name = fileName,
424-
displayName = displayName,
425-
uri = ""
441+
commonFiles.forEach { (fileName, displayName) ->
442+
val file = File(modelDir, fileName)
443+
if (file.exists()) {
444+
files.add(
445+
ModelFile(
446+
name = fileName,
447+
displayName = displayName,
448+
uri = ""
449+
)
426450
)
427-
)
451+
}
428452
}
429453
}
430454

@@ -439,7 +463,7 @@ class ModelRepository(private val context: Context) {
439463
isPartiallyDownloaded = false,
440464
defaultPrompt = "masterpiece, best quality, flowers,",
441465
defaultNegativePrompt = "worst quality, low quality, normal quality, poorly drawn, lowres, low resolution, signature, watermarks, ugly, out of focus, error, blurry, unclear photo, bad photo",
442-
runOnCpu = true,
466+
runOnCpu = !isNpu,
443467
useCpuClip = true,
444468
isCustom = true
445469
)

0 commit comments

Comments
 (0)