Skip to content

armgit / Edge0

Public Imported from GitHub
0

Run 35B MoE models in ~2.5 GB of RAM. Weights stream from SSD; a trained router predicts loads a step ahead. Mac, iPhone, Android, Windows supported

Python

fix(android): wire the Models screen to the document import

Jiahaoa34831b15577
diff --git a/android/app/src/main/java/dev/edge0/runtime/app/Edge0App.kt b/android/app/src/main/java/dev/edge0/runtime/app/Edge0App.kt
index 7e57d95..f499e4a 100644
--- a/android/app/src/main/java/dev/edge0/runtime/app/Edge0App.kt
+++ b/android/app/src/main/java/dev/edge0/runtime/app/Edge0App.kt
@@ -12,6 +12,7 @@ import dev.edge0.runtime.app.data.ChatRepository
import dev.edge0.runtime.app.data.RoomChatRepository
import dev.edge0.runtime.app.data.SettingsStore
import dev.edge0.runtime.app.runtime.LlamaRuntime
+import dev.edge0.runtime.app.runtime.ModelImportSession
import dev.edge0.runtime.app.runtime.Runtime
import dev.edge0.runtime.app.runtime.ThermalSource
import java.io.File
@@ -32,6 +33,7 @@ class AppContainer(private val app: Application) {
* reliable channel) plus external files/models; both roots are scanned. */
val modelRoots: List<File?>
get() = listOf(File(app.filesDir, "models"), app.getExternalFilesDir("models"))
+ val imports = ModelImportSession(File(app.filesDir, "models"))
}
class Edge0App : Application() {
diff --git a/android/app/src/main/java/dev/edge0/runtime/app/runtime/ModelImportSession.kt b/android/app/src/main/java/dev/edge0/runtime/app/runtime/ModelImportSession.kt
new file mode 100644
index 0000000..a2501d1
--- /dev/null
+++ b/android/app/src/main/java/dev/edge0/runtime/app/runtime/ModelImportSession.kt
@@ -0,0 +1,74 @@
+// runtime/ModelImportSession.kt - one in-flight document import for the process.
+// A 8B/35B GGUF is several gigabytes, so the copy is not owned by the Models
+// screen: leaving that screen must not cancel it.
+package dev.edge0.runtime.app.runtime
+
+import android.content.Context
+import android.net.Uri
+import java.io.File
+import kotlinx.coroutines.CancellationException
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.SupervisorJob
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.launch
+import kotlinx.coroutines.withContext
+
+class ModelImportSession(private val modelsDir: File) {
+ data class Progress(val name: String, val copied: Long, val total: Long?)
+
+ val progress = MutableStateFlow<Progress?>(null)
+ val status = MutableStateFlow<String?>(null)
+ val error = MutableStateFlow<String?>(null)
+ val running = MutableStateFlow(false)
+
+ private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
+
+ fun deleteStalePartials() {
+ if (!running.value) ModelImport.deleteStalePartials(modelsDir)
+ }
+
+ fun start(context: Context, uris: List<Uri>, activePath: () -> String?) {
+ if (uris.isEmpty() || running.value) return
+ val appContext = context.applicationContext
+ running.value = true
+ status.value = null
+ error.value = null
+ scope.launch {
+ val problems = ArrayList<String>()
+ var imported = 0
+ try {
+ for (uri in uris) {
+ try {
+ withContext(Dispatchers.IO) {
+ ModelDocumentImport.import(
+ appContext, uri, modelsDir,
+ blocked = { dest ->
+ if (activePath() == dest.absolutePath)
+ "Unload ${dest.name} before replacing it"
+ else null
+ },
+ ) { name, copied, total ->
+ progress.value = Progress(name, copied, total)
+ }
+ }
+ imported++
+ } catch (e: CancellationException) {
+ throw e
+ } catch (e: Exception) {
+ problems += e.message ?: "Import failed"
+ }
+ }
+ } finally {
+ running.value = false
+ progress.value = null
+ }
+ status.value = when (imported) {
+ 0 -> null
+ 1 -> "Imported 1 file"
+ else -> "Imported $imported files"
+ }
+ error.value = problems.takeIf { it.isNotEmpty() }?.joinToString("\n")
+ }
+ }
+}
diff --git a/android/app/src/main/java/dev/edge0/runtime/app/runtime/ModelInventory.kt b/android/app/src/main/java/dev/edge0/runtime/app/runtime/ModelInventory.kt
index 8773832..aa08870 100644
--- a/android/app/src/main/java/dev/edge0/runtime/app/runtime/ModelInventory.kt
+++ b/android/app/src/main/java/dev/edge0/runtime/app/runtime/ModelInventory.kt
@@ -41,11 +41,12 @@ object ModelInventory {
/** Import instructions shown on the models screen. */
fun importTemplate(pkg: String): String = listOf(
- "Place model files flat under files/models/ of this app.",
- "Option 1 - in-app: use the picker to copy from device storage.",
- "Option 2 - adb (debug builds):",
+ "Tap Import and choose a .gguf file. The app copies it into private storage.",
+ "This works on a release install. USB debugging is not required.",
+ "8B also needs lora_edge0_8b-gguf.gguf beside it. Import that file the same way.",
+ "Expected names: edge0-8b.gguf, lora_edge0_8b-gguf.gguf, edge0-35b.gguf.",
+ "A debug build can instead be staged over USB:",
" adb push <file> /data/local/tmp/",
" adb shell run-as $pkg cp /data/local/tmp/<file> files/models/<file>",
- "Expected names: edge0-8b.gguf (plus lora_edge0_8b-gguf.gguf), edge0-35b.gguf.",
).joinToString("\n")
}
diff --git a/android/app/src/test/java/dev/edge0/runtime/app/runtime/ModelImportTest.kt b/android/app/src/test/java/dev/edge0/runtime/app/runtime/ModelImportTest.kt
new file mode 100644
index 0000000..309f61c
--- /dev/null
+++ b/android/app/src/test/java/dev/edge0/runtime/app/runtime/ModelImportTest.kt
@@ -0,0 +1,87 @@
+// ModelImportTest - file-name gate and the partial-then-rename copy.
+package dev.edge0.runtime.app.runtime
+
+import java.io.ByteArrayInputStream
+import java.io.File
+import kotlinx.coroutines.runBlocking
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertNull
+import org.junit.Assert.assertTrue
+import org.junit.Rule
+import org.junit.Test
+import org.junit.rules.TemporaryFolder
+
+class ModelImportTest {
+ @get:Rule val tmp = TemporaryFolder()
+
+ @Test fun fileNameKeepsAFlatGgufBasename() {
+ assertEquals("edge0-8b.gguf", ModelImport.fileName("edge0-8b.gguf"))
+ assertEquals("lora_edge0_8b-gguf.gguf",
+ ModelImport.fileName("/storage/Download/lora_edge0_8b-gguf.gguf"))
+ assertEquals("EDGE0-35B.gguf", ModelImport.fileName("EDGE0-35B.GGUF"))
+ }
+
+ @Test fun fileNameRejectsAnythingElse() {
+ assertNull(ModelImport.fileName(null))
+ assertNull(ModelImport.fileName(" "))
+ assertNull(ModelImport.fileName("notes.txt"))
+ assertNull(ModelImport.fileName(".gguf"))
+ assertNull(ModelImport.fileName(".."))
+ assertNull(ModelImport.fileName("a/../b"))
+ }
+
+ @Test fun copyRenamesOnlyAfterTheFullStream() = runBlocking {
+ val dir = tmp.newFolder("models")
+ val dest = ModelImport.copy(
+ ByteArrayInputStream("gguf-bytes".toByteArray()),
+ dir, "edge0-8b.gguf", sizeBytes = 10,
+ )
+ assertEquals("gguf-bytes", dest.readText())
+ assertEquals("edge0-8b.gguf", dest.name)
+ assertFalse(File(dir, "edge0-8b.gguf.partial").exists())
+ val names = ModelInventory.scan(dir).map { it.name }
+ assertEquals(listOf("edge0-8b"), names)
+ }
+
+ @Test fun truncatedStreamDeletesThePartial() = runBlocking {
+ val dir = tmp.newFolder("models")
+ val error = runCatching {
+ ModelImport.copy(
+ ByteArrayInputStream("short".toByteArray()),
+ dir, "edge0-8b.gguf", sizeBytes = 10,
+ )
+ }.exceptionOrNull()
+ assertTrue(error is ModelImport.ImportException)
+ assertTrue(dir.listFiles().isNullOrEmpty())
+ }
+
+ @Test fun refusesWhenTheVolumeCannotHoldTheFile() = runBlocking {
+ val dir = tmp.newFolder("models")
+ val error = runCatching {
+ ModelImport.copy(
+ ByteArrayInputStream("x".toByteArray()),
+ dir, "edge0-35b.gguf", sizeBytes = 100,
+ availableBytes = 10,
+ )
+ }.exceptionOrNull()
+ assertTrue(error is ModelImport.ImportException)
+ assertTrue(dir.listFiles().isNullOrEmpty())
+ }
+
+ @Test fun stalePartialIsRemovedAndAFinishedFileIsKept() {
+ val dir = tmp.newFolder("models")
+ File(dir, "edge0-8b.gguf.partial").writeText("half")
+ File(dir, "edge0-8b.gguf").writeText("whole")
+ ModelImport.deleteStalePartials(dir)
+ assertFalse(File(dir, "edge0-8b.gguf.partial").exists())
+ assertEquals("whole", File(dir, "edge0-8b.gguf").readText())
+ }
+
+ @Test fun loraCompanionIsStoredButNotListed() = runBlocking {
+ val dir = tmp.newFolder("models")
+ ModelImport.copy(ByteArrayInputStream("lora".toByteArray()), dir, "lora_edge0_8b-gguf.gguf", 4)
+ assertTrue(File(dir, "lora_edge0_8b-gguf.gguf").exists())
+ assertTrue(ModelInventory.scan(dir).isEmpty())
+ }
+}