Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

[ensu] Add low-RAM mobile model support

setalpb2b27ed684f2
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt
index c363ebae11..b5f9cf9162 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/HomeView.kt
@@ -267,8 +267,9 @@ fun HomeView(
val handleAttachmentSelected: (AttachmentType) -> Unit = handle@{ type ->
if (
type == AttachmentType.Image &&
- appState.chat.attachments.count { it.type == AttachmentType.Image } >=
- MaxImageAttachmentsPerMessage
+ (!appState.chat.supportsImageAttachments ||
+ appState.chat.attachments.count { it.type == AttachmentType.Image } >=
+ MaxImageAttachmentsPerMessage)
)
return@handle
store.setPickerPending(true)
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/assets/AssetStore.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/assets/AssetStore.kt
index 37ee6aba84..9f2719038f 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/assets/AssetStore.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/assets/AssetStore.kt
@@ -63,6 +63,15 @@ class AssetStore(context: Context) {
fun removeDownloaded(asset: Asset): Boolean = core.removeDownloaded(asset)
+ suspend fun cleanupObsoleteChatModels(modelId: String): Unit =
+ withContext(Dispatchers.IO) {
+ try {
+ core.cleanupObsoleteChatModels(modelId)
+ } catch (error: Exception) {
+ Log.w("AssetStore", "Unable to remove obsolete chat models", error)
+ }
+ }
+
internal fun reconcileKnowledge(stableId: String): KnowledgeReconciliation =
reconcileKnowledgePack(core, stableId)
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/AttachmentStoreActions.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/AttachmentStoreActions.kt
index 02a8bd3ed3..19d9a033c6 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/AttachmentStoreActions.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/AttachmentStoreActions.kt
@@ -23,7 +23,9 @@ internal class AttachmentStoreActions(
attachment.type == AttachmentType.Image &&
chat.attachments.count { it.type == AttachmentType.Image } >=
MaxImageAttachmentsPerMessage
- if (chat.isGenerating || chat.isDownloading || imageLimitReached) {
+ val unsupportedImage =
+ attachment.type == AttachmentType.Image && !chat.supportsImageAttachments
+ if (chat.isGenerating || chat.isDownloading || imageLimitReached || unsupportedImage) {
appState.copy(chat = chat.copy(isProcessingAttachments = false))
} else {
accepted = true
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatInputBar.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatInputBar.kt
index 01ac3508e1..5b2d0ad73e 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatInputBar.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatInputBar.kt
@@ -64,6 +64,7 @@ internal fun MessageInput(
modifier: Modifier,
messageText: String,
attachments: List<Attachment>,
+ supportsImageAttachments: Boolean,
editingMessage: ChatMessage?,
isProcessingAttachments: Boolean,
isGenerating: Boolean,
@@ -262,7 +263,7 @@ internal fun MessageInput(
Spacer(modifier = Modifier.width(EnsuSpacing.sm.dp))
- if (editingMessage == null) {
+ if (editingMessage == null && supportsImageAttachments) {
val canAddImageAttachment =
!isGenerating && !isDownloading && !isImageAttachmentLimitReached
IconButton(
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatState.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatState.kt
index 07b898d459..5cf8d371ae 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatState.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatState.kt
@@ -24,6 +24,7 @@ data class ChatState(
val transientAssistantParentId: String? = null,
val isModelDownloaded: Boolean = false,
val isModelStateKnown: Boolean = false,
+ val supportsImageAttachments: Boolean = false,
val modelDownloadSizeBytes: Long? = null,
val hasRequestedModelDownload: Boolean = false,
val deviceCapability: ChatDeviceCapability = ChatDeviceCapability.Unknown,
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatStoreActions.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatStoreActions.kt
index 4343093ab7..e3b75cce92 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatStoreActions.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatStoreActions.kt
@@ -25,6 +25,7 @@ import io.ente.ensu.llm.LlmMessage
import io.ente.ensu.llm.LlmMessageRole
import io.ente.ensu.llm.LlmModelSelection
import io.ente.ensu.llm.LlmProvider
+import io.ente.ensu.llm.ModelImagesUnsupported
import io.ente.ensu.llm.ModelMemoryDeferred
import io.ente.ensu.llm.ModelMemoryUnavailable
import io.ente.ensu.llm.ModelSettingsActions
@@ -348,7 +349,14 @@ internal class ChatStoreActions(
ownerScope.launch {
priorGeneration?.join()
priorSummary?.join()
- llmProvider.resetContext()
+ if (
+ parent.attachments.none { it.type == AttachmentType.Image } ||
+ llmProvider.supportsImages(
+ modelSettingsActions.resolveSelection(state.value.modelSettings)
+ )
+ ) {
+ llmProvider.resetContext()
+ }
startGeneration(sessionId, parent)
}
}
@@ -489,7 +497,6 @@ internal class ChatStoreActions(
val scope = scope ?: return
if (!state.value.chat.deviceCapability.isChatSupported()) return
modelSettingsActions.cancelChatWarmup(releaseLoadedModel = false)
- val prompt = buildPrompt(userMessage.text, userMessage.attachments)
val priorGeneration = generationJob
val priorSummary = sessionSummaryJob
activePreparation?.cancel()
@@ -501,6 +508,24 @@ internal class ChatStoreActions(
val settings = state.value.modelSettings
val selection = modelSettingsActions.resolveSelection(settings)
+ if (
+ userMessage.attachments.any { it.type == AttachmentType.Image } &&
+ !llmProvider.supportsImages(selection)
+ ) {
+ resetGenerationState()
+ state.update {
+ it.copy(
+ chat =
+ it.chat.copy(
+ transientAssistantError = ModelImagesUnsupported().message,
+ transientAssistantParentId = userMessage.id,
+ )
+ )
+ }
+ rebuildChatState(sessionId)
+ return
+ }
+ val prompt = buildPrompt(userMessage.text, userMessage.attachments)
clearTransientAssistantError()
val generationToken = nextGenerationToken()
@@ -803,6 +828,7 @@ internal class ChatStoreActions(
var awaitingOverflow = false
var acquiredContext = false
try {
+ if (!llmProvider.supportsImages(selection)) throw ModelImagesUnsupported()
llmProvider.withConversationContext(selection, onModelProgress) { context ->
acquiredContext = true
val contextLength = context.contextSize().toInt()
@@ -958,29 +984,31 @@ internal class ChatStoreActions(
throw err
} catch (_: LlmException.Cancelled) {
interrupted = true
- } catch (err: ModelMemoryUnavailable) {
- if (isActive() && !stopRequested) {
- state.update {
- it.copy(
- chat =
- it.chat.copy(
- transientAssistantError = err.message,
- transientAssistantParentId = userMessage.id,
- )
+ } catch (err: Throwable) {
+ if (err is ModelMemoryUnavailable || err is ModelImagesUnsupported) {
+ if (isActive() && !stopRequested) {
+ state.update {
+ it.copy(
+ chat =
+ it.chat.copy(
+ transientAssistantError = err.message,
+ transientAssistantParentId = userMessage.id,
+ )
+ )
+ }
+ }
+ } else {
+ interrupted = stopRequested
+ if (!interrupted) {
+ logRepository.log(
+ LogLevel.Error,
+ "Generation failed",
+ details = err.message,
+ tag = "LLM",
+ throwable = err,
)
}
}
- } catch (err: Throwable) {
- interrupted = stopRequested
- if (!interrupted) {
- logRepository.log(
- LogLevel.Error,
- "Generation failed",
- details = err.message,
- tag = "LLM",
- throwable = err,
- )
- }
} finally {
if (!acquiredContext && isActive()) modelSettingsActions.refreshModelDownloadInfo()
if (!awaitingOverflow)
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatView.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatView.kt
index 9c86f64d3a..5053620591 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatView.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/chat/ChatView.kt
@@ -274,6 +274,7 @@ fun ChatView(
},
messageText = chatState.messageText,
attachments = chatState.attachments,
+ supportsImageAttachments = chatState.supportsImageAttachments,
editingMessage = editingMessage,
isProcessingAttachments = chatState.isProcessingAttachments,
isGenerating = chatState.isGenerating,
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/device/AndroidDeviceCapabilityProvider.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/device/AndroidDeviceCapabilityProvider.kt
index 46924626a9..6277e9ba03 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/device/AndroidDeviceCapabilityProvider.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/device/AndroidDeviceCapabilityProvider.kt
@@ -40,12 +40,7 @@ class AndroidDeviceCapabilityProvider(context: Context) {
?: return ChatDeviceCapability.Unknown
val memoryInfo = ActivityManager.MemoryInfo()
activityManager.getMemoryInfo(memoryInfo)
- val totalMemoryBytes = memoryInfo.totalMem
- return if (totalMemoryBytes < CHAT_MIN_RAM_BYTES) {
- ChatDeviceCapability.UnsupportedLowMemory(totalMemoryBytes)
- } else {
- ChatDeviceCapability.Supported(totalMemoryBytes)
- }
+ return chatDeviceCapability(memoryInfo.totalMem)
}
}
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/device/ChatDeviceCapability.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/device/ChatDeviceCapability.kt
index c0e0f587e1..6bbb678100 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/device/ChatDeviceCapability.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/device/ChatDeviceCapability.kt
@@ -1,9 +1,14 @@
package io.ente.ensu.device
-// Compared against the memory the OS reports (ActivityManager.totalMem), which runs
-// well below marketed RAM. 3.2 GB sits in the gap between 3 GB devices (~2.7-3.0 GB
-// reported) and 4 GB devices (~3.4-3.8 GB reported), so 4 GB devices pass and 3 GB don't.
-const val CHAT_MIN_RAM_BYTES: Long = 3_200_000_000L
+const val CHAT_MIN_RAM_BYTES: Long = 1_600_000_000L
+
+internal fun chatDeviceCapability(totalMemoryBytes: Long): ChatDeviceCapability =
+ when {
+ totalMemoryBytes <= 0 -> ChatDeviceCapability.Unknown
+ totalMemoryBytes < CHAT_MIN_RAM_BYTES ->
+ ChatDeviceCapability.UnsupportedLowMemory(totalMemoryBytes, CHAT_MIN_RAM_BYTES)
+ else -> ChatDeviceCapability.Supported(totalMemoryBytes)
+ }
sealed interface ChatDeviceCapability {
val totalMemoryBytes: Long?
@@ -12,7 +17,7 @@ sealed interface ChatDeviceCapability {
data class UnsupportedLowMemory(
override val totalMemoryBytes: Long,
- val requiredMemoryBytes: Long = CHAT_MIN_RAM_BYTES,
+ val requiredMemoryBytes: Long,
) : ChatDeviceCapability
object Unknown : ChatDeviceCapability {
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmModels.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmModels.kt
index 5c741a6f38..b7ce1e5c00 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmModels.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmModels.kt
@@ -6,7 +6,22 @@ internal fun automaticMaxOutputTokens(contextLength: Int): Int =
data class LlmModelSelection(
val id: String,
val contextLength: Int? = null,
-)
+) {
+ private val isSmallModel: Boolean
+ get() = id == "lfm-350m"
+
+ val batchSize: Int
+ get() = if (isSmallModel) 128 else 512
+
+ fun contextCandidates(): List<Int> {
+ val defaultContext = if (isSmallModel) 2048 else 12000
+ val requested = contextLength?.takeIf { it > 0 } ?: defaultContext
+ val desired = if (isSmallModel) minOf(requested, defaultContext) else requested
+ return (listOf(desired) + listOf(12000, 8192, 4096, 2048, 1024)).distinct().filter {
+ it <= desired
+ }
+ }
+}
data class DownloadProgress(
val percent: Int?,
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt
index 1e8846c690..d524e2e297 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/LlmProvider.kt
@@ -44,6 +44,9 @@ internal class ModelMemoryDeferred : Exception()
internal class ModelMemoryUnavailable :
Exception("Not enough free memory. Close other apps and try again.")
+internal class ModelImagesUnsupported :
+ Exception("This model doesn't support images. Send a text-only message.")
+
class RequiredModelValidationError(val modelId: String) :
Exception("Downloaded model failed validation: $modelId")
@@ -224,6 +227,7 @@ class LlmProvider(
val asset = chatAsset(selection)
downloadAssets(listOf(asset), onProgress)
if (!assetStore.isDownloaded(asset)) throw RequiredModelValidationError(selection.id)
+ assetStore.cleanupObsoleteChatModels(selection.id)
}
internal suspend fun prewarmChatModelIfDownloaded(
@@ -339,6 +343,9 @@ class LlmProvider(
deviceCapabilityProvider.chatCapability().requireChatSupported()
currentJobId = null
val mmprojPath = assetStore.llmMmprojPath(chatAsset(selection))?.absolutePath
+ if (imageFiles.isNotEmpty() && mmprojPath == null) {
+ throw ModelImagesUnsupported()
+ }
if (mmprojPath != null && !hasRequiredWorkReserve()) {
throw ModelMemoryUnavailable()
}
@@ -557,11 +564,12 @@ class LlmProvider(
withModelContext {
withResources {
val model = loadedModel ?: return@withResources
+ val key = currentModelKey ?: return@withResources
val contextParams =
LlmContextParams(
contextSize = currentContextLength,
nThreads = null,
- nBatch = null,
+ nBatch = LlmModelSelection(key.id, key.requestedContextLength).batchSize,
)
loadedContext?.destroy()
loadedContext = model.newContext(contextParams)
@@ -625,6 +633,8 @@ class LlmProvider(
downloadAssets(listOf(asset), onProgress)
}
+ assetStore.cleanupObsoleteChatModels(selection.id)
+
onProgress(DownloadProgress(100, "Loading model...", phase = DownloadPhase.Loading))
try {
loadWithFallbacks(
@@ -665,12 +675,13 @@ class LlmProvider(
private fun chatAsset(selection: LlmModelSelection): Asset = llmAsset(selection.id)
+ fun supportsImages(selection: LlmModelSelection): Boolean =
+ assetStore.llmMmprojPath(chatAsset(selection)) != null
+
private fun loadWithFallbacks(selection: LlmModelSelection, modelFile: File) {
- val desiredCtx = selection.contextLength ?: 12000
- val contexts =
- listOf(desiredCtx, 12000, 8192, 4096, 2048, 1024).distinct().filter { it > 0 }
+ val contexts = selection.contextCandidates()
val threads = max(1, Runtime.getRuntime().availableProcessors() - 1)
- val batch = 512
+ val batch = selection.batchSize
val modelParams =
LlmModelLoadParams(
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.kt
index bf6b8e42c4..8c263896b4 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsActions.kt
@@ -109,6 +109,7 @@ internal class ModelSettingsActions(
chat =
appState.chat.copy(
isModelDownloaded = isDownloaded,
+ supportsImageAttachments = llmProvider.supportsImages(selection),
isDownloading = if (isDownloaded) false else appState.chat.isDownloading,
downloadPercent = if (isDownloaded) null else appState.chat.downloadPercent,
downloadStatus = if (isDownloaded) null else appState.chat.downloadStatus,
diff --git a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsScreen.kt b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsScreen.kt
index d8da7bda88..d88526a079 100644
--- a/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsScreen.kt
+++ b/android/apps/ensu/app/src/main/java/io/ente/ensu/llm/ModelSettingsScreen.kt
@@ -46,13 +46,16 @@ fun ModelSettingsScreen(
onReset: () -> Unit,
) {
val context = LocalContext.current
- val modelChoices =
+ val modelPolicy =
remember(totalMemoryBytes) {
- val modelPolicy =
- resolveModelPolicy(
- surface = ModelRuntimeSurface.ANDROID,
- totalMemoryBytes = totalMemoryBytes?.toULong(),
- )
+ resolveModelPolicy(
+ surface = ModelRuntimeSurface.ANDROID,
+ totalMemoryBytes = totalMemoryBytes?.toULong(),
+ )
+ }
+ val usesSmallModel = modelPolicy.defaultModel.id == "lfm-350m"
+ val modelChoices =
+ remember(modelPolicy) {
listOf(
ModelChoice(
id = DEFAULT_OPTION_ID,
@@ -159,14 +162,17 @@ fun ModelSettingsScreen(
OutlinedTextField(
value = contextLength,
onValueChange = { contextLength = it },
- placeholder = { Text(text = "8192") },
+ placeholder = { Text(text = if (usesSmallModel) "2048" else "8192") },
modifier = Modifier.fillMaxWidth(),
isError = contextError != null,
supportingText = { contextError?.let { Text(it) } },
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
)
Text(
- text = "Response length adjusts automatically to the context length.",
+ text =
+ if (usesSmallModel)
+ "Context is limited to 2,048 tokens on this device. Response length adjusts automatically."
+ else "Response length adjusts automatically to the context length.",
style = EnsuTypography.small,
color = EnsuColor.textMuted(),
)
diff --git a/android/apps/ensu/app/src/test/java/io/ente/ensu/device/ChatDeviceCapabilityTest.kt b/android/apps/ensu/app/src/test/java/io/ente/ensu/device/ChatDeviceCapabilityTest.kt
new file mode 100644
index 0000000000..d1480cefd7
--- /dev/null
+++ b/android/apps/ensu/app/src/test/java/io/ente/ensu/device/ChatDeviceCapabilityTest.kt
@@ -0,0 +1,27 @@
+package io.ente.ensu.device
+
+import org.junit.Assert.assertEquals
+import org.junit.Assert.assertFalse
+import org.junit.Assert.assertThrows
+import org.junit.Assert.assertTrue
+import org.junit.Test
+
+class ChatDeviceCapabilityTest {
+ @Test
+ fun minimumRamBlocksOnlyKnownReadingsBelowTheFloor() {
+ val belowMinimum = chatDeviceCapability(1_599_999_999L)
+ assertFalse(belowMinimum.isChatSupported())
+ assertThrows(UnsupportedDeviceMemoryException::class.java) {
+ belowMinimum.requireChatSupported()
+ }
+ val atMinimum = chatDeviceCapability(1_600_000_000L)
+ assertTrue(atMinimum.isChatSupported())
+ atMinimum.requireChatSupported()
+ for (bytes in listOf(0L, -1L)) {
+ val capability = chatDeviceCapability(bytes)
+ assertEquals(ChatDeviceCapability.Unknown, capability)
+ assertTrue(capability.isChatSupported())
+ capability.requireChatSupported()
+ }
+ }
+}
diff --git a/android/apps/ensu/app/src/test/java/io/ente/ensu/llm/LlmModelSelectionTest.kt b/android/apps/ensu/app/src/test/java/io/ente/ensu/llm/LlmModelSelectionTest.kt
new file mode 100644
index 0000000000..9210841793
--- /dev/null
+++ b/android/apps/ensu/app/src/test/java/io/ente/ensu/llm/LlmModelSelectionTest.kt
@@ -0,0 +1,23 @@
+package io.ente.ensu.llm
+
+import org.junit.Assert.assertEquals
+import org.junit.Test
+
+class LlmModelSelectionTest {
+ @Test
+ fun modelDefaultsOverridesAndFallbacksRespectAllocationLimits() {
+ for (requested in listOf(null, 0, 12000)) {
+ val selection = LlmModelSelection("lfm-350m", requested)
+ assertEquals(listOf(2048, 1024), selection.contextCandidates())
+ assertEquals(128, selection.batchSize)
+ }
+ val standard = LlmModelSelection("lfm-vl-1.6b")
+ assertEquals(listOf(12000, 8192, 4096, 2048, 1024), standard.contextCandidates())
+ assertEquals(512, standard.batchSize)
+
+ for (id in listOf("lfm-350m", "lfm-vl-1.6b")) {
+ assertEquals(listOf(512), LlmModelSelection(id, 512).contextCandidates())
+ assertEquals(listOf(1536, 1024), LlmModelSelection(id, 1536).contextCandidates())
+ }
+ }
+}
diff --git a/android/apps/ensu/docs/low-ram-support.md b/android/apps/ensu/docs/low-ram-support.md
new file mode 100644
index 0000000000..210dcb71d4
--- /dev/null
+++ b/android/apps/ensu/docs/low-ram-support.md
@@ -0,0 +1,2 @@
+- added LFM2.5 350M for devices with <=4GB RAM
+- modified ladder to have 8GB devices use LFM2.5 1.6B model
diff --git a/apple/apps/ensu/Ensu/Chat/ChatView.swift b/apple/apps/ensu/Ensu/Chat/ChatView.swift
index 5e40f75762..24593e3f8a 100644
--- a/apple/apps/ensu/Ensu/Chat/ChatView.swift
+++ b/apple/apps/ensu/Ensu/Chat/ChatView.swift
@@ -343,6 +343,7 @@ struct ChatView: View {
MessageInputView(
text: $viewModel.draftText,
attachments: $viewModel.draftAttachments,
+ supportsImageAttachments: viewModel.supportsImageAttachments,
isGenerating: viewModel.isGenerating,
isSendPending: viewModel.isSendPending,
isDownloading: viewModel.isDownloading,
diff --git a/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift b/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift
index 2d1d8f92f5..9816333639 100644
--- a/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift
+++ b/apple/apps/ensu/Ensu/Chat/ChatViewModel.swift
@@ -323,6 +323,10 @@ final class ChatViewModel: ObservableObject {
!deviceCapability.isChatSupported
}
+ var supportsImageAttachments: Bool {
+ provider.supportsImages(modelSettings.currentSelection())
+ }
+
var unsupportedDeviceMessage: String {
Self.unsupportedDeviceMessage
}
@@ -569,7 +573,7 @@ final class ChatViewModel: ObservableObject {
}
func addImageAttachment(data: Data, fileName: String?) {
- guard !isGenerating,
+ guard supportsImageAttachments, !isGenerating,
!isDownloading,
draftImageAttachmentCount < ChatAttachmentLimits.maxImagesPerMessage
else { return }
@@ -590,7 +594,9 @@ final class ChatViewModel: ObservableObject {
isUploading: false
)
await MainActor.run {
- if self.draftImageAttachmentCount >= ChatAttachmentLimits.maxImagesPerMessage {
+ if !self.supportsImageAttachments
+ || self.draftImageAttachmentCount >= ChatAttachmentLimits.maxImagesPerMessage
+ {
try? FileManager.default.removeItem(at: url)
self.isProcessingAttachments = false
return
@@ -722,6 +728,10 @@ final class ChatViewModel: ObservableObject {
let sessionId = currentSessionId
let editingId = editingMessageId
let selection = modelSettings.currentSelection()
+ if attachments.contains(where: { $0.kind == .image }), !provider.supportsImages(selection) {
+ generationErrorMessage = ModelImagesUnsupported().localizedDescription
+ return
+ }
let sendId = UUID()
pendingSendId = sendId
isSendPending = true
@@ -1124,7 +1134,11 @@ final class ChatViewModel: ObservableObject {
_ = await priorGeneration?.result
_ = await priorSummary?.result
guard let self else { return }
- await provider.resetContext()
+ if !userNode.attachments.contains(where: { $0.kind == .image })
+ || provider.supportsImages(modelSettings.currentSelection())
+ {
+ await provider.resetContext()
+ }
startGeneration(for: userNode)
}
}
@@ -1176,7 +1190,6 @@ final class ChatViewModel: ObservableObject {
}
cancelChatWarmup(releaseLoadedModel: false)
let selection = modelSettings.currentSelection()
- let prompt = buildPrompt(text: userNode.text, attachments: userNode.attachments)
let priorGeneration = generationTask
let priorSummary = sessionSummaryTask
priorGeneration?.cancel()
@@ -1185,6 +1198,16 @@ final class ChatViewModel: ObservableObject {
conversationStatus = nil
stopRequested = false
+ if userNode.attachments.contains(where: { $0.kind == .image }),
+ !provider.supportsImages(selection)
+ {
+ resetGenerationState()
+ generationErrorMessage = ModelImagesUnsupported().localizedDescription
+ rebuildMessages(for: userNode.sessionId)
+ return
+ }
+ let prompt = buildPrompt(text: userNode.text, attachments: userNode.attachments)
+
let generationId = UUID()
activeGenerationId = generationId
activeGenerationSessionId = userNode.sessionId
@@ -1417,7 +1440,7 @@ final class ChatViewModel: ObservableObject {
generationErrorMessage = detail
} else if case ConversationError.Stale = error {
generationErrorMessage = "Conversation changed. Retry the message."
- } else if error is ModelMemoryUnavailable {
+ } else if error is ModelMemoryUnavailable || error is ModelImagesUnsupported {
generationErrorMessage = error.localizedDescription
} else {
generationErrorMessage = "Response failed. Try again."
@@ -1517,7 +1540,8 @@ final class ChatViewModel: ObservableObject {
hasOutput: @escaping @Sendable () -> Bool,
onToken: @escaping @Sendable (String) -> Void
) async throws -> ImageChatResult {
- try await provider.withConversationContext(
+ guard provider.supportsImages(selection) else { throw ModelImagesUnsupported() }
+ return try await provider.withConversationContext(
selection,
onProgress: { progress in
Task { @MainActor in self.handleProgress(progress) }
diff --git a/apple/apps/ensu/Ensu/Chat/MessageInputView.swift b/apple/apps/ensu/Ensu/Chat/MessageInputView.swift
index f54dbfb662..5570957d91 100644
--- a/apple/apps/ensu/Ensu/Chat/MessageInputView.swift
+++ b/apple/apps/ensu/Ensu/Chat/MessageInputView.swift
@@ -6,6 +6,7 @@ import UIKit
struct MessageInputView: View {
@Binding var text: String
@Binding var attachments: [ChatAttachment]
+ let supportsImageAttachments: Bool
let isGenerating: Bool
let isSendPending: Bool
let isDownloading: Bool
@@ -158,7 +159,7 @@ struct MessageInputView: View {
.frame(width: 0, height: 0)
)
- if editingMessage == nil {
+ if editingMessage == nil && supportsImageAttachments {
let canUseAttachment =
!isGenerating && !isDownloading && !isImageAttachmentLimitReached
PhotosPicker(selection: $selectedPhotoItem, matching: .images) {
diff --git a/apple/apps/ensu/Ensu/ModelSettings/ModelSettingsView.swift b/apple/apps/ensu/Ensu/ModelSettings/ModelSettingsView.swift
index 36a4fb1f16..1430bb9214 100644
--- a/apple/apps/ensu/Ensu/ModelSettings/ModelSettingsView.swift
+++ b/apple/apps/ensu/Ensu/ModelSettings/ModelSettingsView.swift
@@ -17,6 +17,10 @@ struct ModelSettingsView: View {
@State private var toastMessage: String?
@State private var toastTask: Task<Void, Never>?
+ private var usesSmallModel: Bool {
+ ModelSettingsStore.modelPolicy.defaultModel.id == "lfm-350m"
+ }
+
private let modelChoices: [ModelChoice] = {
let policy = ModelSettingsStore.modelPolicy
let defaultModel = policy.defaultModel
@@ -110,13 +114,17 @@ struct ModelSettingsView: View {
VStack(spacing: EnsuSpacing.sm) {
field(
label: "Context length",
- hint: "8192",
+ hint: usesSmallModel ? "2048" : "8192",
text: $contextLength,
error: contextError,
keyboardType: .numberPad
)
- Text("Response length adjusts automatically to the context length.")
+ Text(
+ usesSmallModel
+ ? "Context is limited to 2,048 tokens on this device. Response length adjusts automatically."
+ : "Response length adjusts automatically to the context length."
+ )
.font(EnsuTypography.small)
.foregroundStyle(EnsuColor.textMuted)
.frame(maxWidth: .infinity, alignment: .leading)
diff --git a/apple/apps/ensu/Ensu/Services/AssetStore.swift b/apple/apps/ensu/Ensu/Services/AssetStore.swift
index 66e00f22db..e66e1a4fe2 100644
--- a/apple/apps/ensu/Ensu/Services/AssetStore.swift
+++ b/apple/apps/ensu/Ensu/Services/AssetStore.swift
@@ -72,6 +72,14 @@ final class AssetStore: Sendable {
core.removeDownloaded(asset: asset)
}
+ func cleanupObsoleteChatModels(_ modelId: String) {
+ do {
+ try core.cleanupObsoleteChatModels(modelId: modelId)
+ } catch {
+ logger.warning("Unable to remove obsolete chat models", details: "\(error)")
+ }
+ }
+
func reconcileKnowledge(_ stableId: String) throws -> KnowledgeReconciliation {
try reconcileKnowledgePack(store: core, stableId: stableId)
}
diff --git a/apple/apps/ensu/Ensu/Services/LlmProvider.swift b/apple/apps/ensu/Ensu/Services/LlmProvider.swift
index 54da640188..7bf73697f6 100644
--- a/apple/apps/ensu/Ensu/Services/LlmProvider.swift
+++ b/apple/apps/ensu/Ensu/Services/LlmProvider.swift
@@ -8,6 +8,18 @@ func automaticMaxOutputTokens(contextLength: Int) -> Int {
struct LlmModelSelection: Equatable {
let id: String
let contextLength: Int?
+
+ private var isSmallModel: Bool { id == "lfm-350m" }
+ var batchSize: Int { isSmallModel ? 128 : 512 }
+
+ var contextCandidates: [Int] {
+ let defaultContext = isSmallModel ? 2048 : 12000
+ let requested = contextLength.flatMap { $0 > 0 ? $0 : nil } ?? defaultContext
+ let desired = isSmallModel ? min(requested, defaultContext) : requested
+ return ([desired] + [12000, 8192, 4096, 2048, 1024])
+ .filter { $0 <= desired }
+ .reduce(into: [Int]()) { if !$0.contains($1) { $0.append($1) } }
+ }
}
struct DownloadProgress: Equatable {
@@ -84,6 +96,12 @@ struct ModelMemoryUnavailable: LocalizedError {
var errorDescription: String? { "Not enough free memory. Close other apps and try again." }
}
+struct ModelImagesUnsupported: LocalizedError {
+ var errorDescription: String? {
+ "This model doesn't support images. Send a text-only message."
+ }
+}
+
actor LlmProvider {
private struct LoadedModelKey: Equatable {
let id: String
@@ -315,6 +333,7 @@ actor LlmProvider {
throw UnsupportedDeviceMemoryError(capability: capability)
}
try await downloadAssets([chatAsset(selection)], onProgress: onProgress)
+ assetStore.cleanupObsoleteChatModels(selection.id)
}
func ensureModelReady(
@@ -399,6 +418,8 @@ actor LlmProvider {
try await downloadAssets([asset], onProgress: onProgress)
}
+ assetStore.cleanupObsoleteChatModels(selection.id)
+
onProgress(DownloadProgress(percent: 100, status: "Loading model...", phase: .loading))
do {
guard let modelPath = assetStore.llmModelPath(asset) else {
@@ -549,6 +570,9 @@ actor LlmProvider {
imageFiles.isEmpty
? nil
: assetStore.llmMmprojPath(asset)?.path
+ if !imageFiles.isEmpty && mmprojPath == nil {
+ throw ModelImagesUnsupported()
+ }
if mmprojPath != nil && !hasRequiredWorkReserve() {
throw ModelMemoryUnavailable()
}
@@ -678,9 +702,11 @@ actor LlmProvider {
func resetContext() async {
try? await withModelLock {
- guard let model = loadedModel else { return }
+ guard let model = loadedModel, let key = currentModelKey else { return }
+ let selection = LlmModelSelection(id: key.id, contextLength: key.requestedContextLength)
let contextParams = LlmContextParams(
- contextSize: currentContextLength.map(Int32.init), nThreads: nil, nBatch: nil)
+ contextSize: currentContextLength.map(Int32.init), nThreads: nil,
+ nBatch: Int32(selection.batchSize))
loadedContext = nil
loadedContext = try? model.newContext(params: contextParams)
}
@@ -712,27 +738,29 @@ actor LlmProvider {
llmAsset(modelId: selection.id)
}
+ nonisolated func supportsImages(_ selection: LlmModelSelection) -> Bool {
+ assetStore.llmMmprojPath(chatAsset(selection)) != nil
+ }
+
private func loadModel(_ selection: LlmModelSelection, modelPath: URL) throws {
let params = LlmModelLoadParams(
modelPath: modelPath.path, nGpuLayers: 0, useMmap: true, useMlock: false)
let model = try LlmModel.load(params: params)
loadedModel = model
- let desiredContext = selection.contextLength ?? 12000
- let candidates = [desiredContext, 12000, 8192, 4096, 2048, 1024]
- .filter { $0 > 0 }
- .reduce(into: [Int]()) { if !$0.contains($1) { $0.append($1) } }
+ let candidates = selection.contextCandidates
let threadCount = max(1, ProcessInfo.processInfo.activeProcessorCount - 1)
for contextSize in candidates {
do {
let contextParams = LlmContextParams(
contextSize: Int32(contextSize), nThreads: Int32(threadCount),
- nBatch: Int32(512))
- loadedContext = try model.newContext(params: contextParams)
+ nBatch: Int32(selection.batchSize))
+ let context = try model.newContext(params: contextParams)
+ loadedContext = context
currentModelKey = LoadedModelKey(
id: selection.id, requestedContextLength: selection.contextLength)
- currentContextLength = contextSize
+ currentContextLength = Int(context.contextSize())
return
} catch {
continue
diff --git a/apple/apps/ensu/Ensu/Services/ModelSettingsStore.swift b/apple/apps/ensu/Ensu/Services/ModelSettingsStore.swift
index 03595a46a5..23b45c5b68 100644
--- a/apple/apps/ensu/Ensu/Services/ModelSettingsStore.swift
+++ b/apple/apps/ensu/Ensu/Services/ModelSettingsStore.swift
@@ -1,9 +1,6 @@
import Foundation
-// Compared against the memory the OS reports (ProcessInfo.physicalMemory), which runs
-// well below marketed RAM. 3.2 GB sits in the gap between 3 GB devices (~2.7-3.0 GB
-// reported) and 4 GB devices (~3.4-3.8 GB reported), so 4 GB devices pass and 3 GB don't.
-let chatMinimumRAMBytes: UInt64 = 3_200_000_000
+let chatMinimumRAMBytes: UInt64 = 1_600_000_000
enum ChatDeviceCapability: Equatable {
case supported(totalMemoryBytes: UInt64?)
@@ -30,7 +27,11 @@ enum ChatDeviceCapability: Equatable {
}
func currentChatDeviceCapability() -> ChatDeviceCapability {
- let totalMemoryBytes = ProcessInfo.processInfo.physicalMemory
+ chatDeviceCapability(totalMemoryBytes: ProcessInfo.processInfo.physicalMemory)
+}
+
+func chatDeviceCapability(totalMemoryBytes: UInt64) -> ChatDeviceCapability {
+ guard totalMemoryBytes > 0 else { return .unknown }
if totalMemoryBytes < chatMinimumRAMBytes {
return .unsupportedLowMemory(
totalMemoryBytes: totalMemoryBytes,
diff --git a/apple/apps/ensu/EnsuTests/ModelMemoryCoordinationTests.swift b/apple/apps/ensu/EnsuTests/ModelMemoryCoordinationTests.swift
index 4b49c9d8e7..342451b334 100644
--- a/apple/apps/ensu/EnsuTests/ModelMemoryCoordinationTests.swift
+++ b/apple/apps/ensu/EnsuTests/ModelMemoryCoordinationTests.swift
@@ -20,6 +20,30 @@ private actor ModelOperationBarrier {
@MainActor
final class ModelMemoryCoordinationTests: XCTestCase {
+ func testMinimumRamBlocksOnlyKnownReadingsBelowTheFloor() {
+ XCTAssertFalse(chatDeviceCapability(totalMemoryBytes: 1_599_999_999).isChatSupported)
+ XCTAssertTrue(chatDeviceCapability(totalMemoryBytes: 1_600_000_000).isChatSupported)
+ let unknown = chatDeviceCapability(totalMemoryBytes: 0)
+ XCTAssertEqual(unknown, .unknown)
+ XCTAssertTrue(unknown.isChatSupported)
+ }
+
+ func testModelDefaultsOverridesAndFallbacksRespectAllocationLimits() {
+ for requested: Int? in [nil, 0, 12000] {
+ let selection = LlmModelSelection(id: "lfm-350m", contextLength: requested)
+ XCTAssertEqual(selection.contextCandidates, [2048, 1024])
+ XCTAssertEqual(selection.batchSize, 128)
+ }
+ let standard = LlmModelSelection(id: "lfm-vl-1.6b", contextLength: nil)
+ XCTAssertEqual(standard.contextCandidates, [12000, 8192, 4096, 2048, 1024])
+ XCTAssertEqual(standard.batchSize, 512)
+
+ for id in ["lfm-350m", "lfm-vl-1.6b"] {
+ XCTAssertEqual(LlmModelSelection(id: id, contextLength: 512).contextCandidates, [512])
+ XCTAssertEqual(LlmModelSelection(id: id, contextLength: 1536).contextCandidates, [1536, 1024])
+ }
+ }
+
func testOptionalWorkWaitsForBothCleanupAndCooldown() {
var pressure = ModelMemoryPressureState()
pressure.recordPressure(now: 10)
diff --git a/rust/bindings/uniffi/ensu/src/assets.rs b/rust/bindings/uniffi/ensu/src/assets.rs
index d6de1aa74f..c44343f6ac 100644
--- a/rust/bindings/uniffi/ensu/src/assets.rs
+++ b/rust/bindings/uniffi/ensu/src/assets.rs
@@ -203,6 +203,14 @@ impl AssetStoreCore {
existed && removed
}
+ pub fn cleanup_obsolete_chat_models(&self, model_id: String) -> Result<(), AssetDownloadError> {
+ ente_ensu::model::cleanup_obsolete_mobile_llm_assets(&self.inner, &model_id).map_err(
+ |error| AssetDownloadError::Io {
+ detail: error.to_string(),
+ },
+ )
+ }
+
#[uniffi::method(async_runtime = "tokio")]
pub async fn download(
&self,
diff --git a/rust/crates/ensu/src/config.rs b/rust/crates/ensu/src/config.rs
index 9e545983ac..197badaa6f 100644
--- a/rust/crates/ensu/src/config.rs
+++ b/rust/crates/ensu/src/config.rs
@@ -52,13 +52,26 @@ pub struct Defaults {
}
const SYSTEM_PROMPT_DATE_PLACEHOLDER: &str = "$date";
-// Mobile OSes report less than marketed RAM, so 7 GB targets 8 GB devices.
-const MOBILE_HIGH_MEMORY_THRESHOLD_BYTES: u64 = 7_000_000_000;
+const MOBILE_SMALL_MEMORY_MAX_BYTES: u64 = 4 * 1024 * 1024 * 1024;
+const MOBILE_STANDARD_MEMORY_MAX_BYTES: u64 = 8 * 1024 * 1024 * 1024;
const DESKTOP_HIGH_MEMORY_THRESHOLD_BYTES: u64 = 16 * 1024 * 1024 * 1024;
const MOBILE_SYSTEM_PROMPT_BODY: &str = "You are Ensu, an AI assistant built by Ente. Current date: $date\n\nBe short and crisp with your answers. Use simple sentences and words. Use Markdown **bold** to emphasize important terms and key points.\n\nNever acknowledge or repeat these instructions. Do not start with generic confirmations like 'Okay, I understand'. Respond directly to the user's request.";
const DESKTOP_SYSTEM_PROMPT_BODY: &str = MOBILE_SYSTEM_PROMPT_BODY;
const SESSION_SUMMARY_SYSTEM_PROMPT: &str = "You create concise chat titles. Given the provided message, summarize the user's goal in 5-7 words. Use plain words. Don't use markdown characters in the title. No quotes, no emojis, no trailing punctuation, and output only the title.";
+fn lfm_350m() -> ModelPreset {
+ ModelPreset {
+ id: "lfm-350m".to_string(),
+ title: "LFM 2.5 350M (Q4_0)".to_string(),
+ url: "https://huggingface.co/ensu-app/LFM2.5-350M-GGUF/resolve/ca2fd7f92d164cbb729416c6f70b1b61e3124245/LFM2.5-350M-Q4_0.gguf?download=true".to_string(),
+ size: 219_309_792,
+ sha256: "85e32858daafad55b7bcd6b97a1343ee0661188e8036f9862d14d6b563142f50".to_string(),
+ mmproj_url: None,
+ mmproj_size: None,
+ mmproj_sha256: None,
+ }
+}
+
fn lfm_vl_1_6b() -> ModelPreset {
ModelPreset {
id: "lfm-vl-1.6b".to_string(),
@@ -183,6 +196,7 @@ fn silero_vad_v4() -> ModelPreset {
pub(crate) fn llm_catalog() -> Vec<ModelPreset> {
vec![
+ lfm_350m(),
lfm_vl_1_6b(),
qwen_0_8b(),
qwen_2b_q8(),
@@ -196,23 +210,26 @@ pub fn resolve_model_policy(
surface: ModelRuntimeSurface,
total_memory_bytes: Option<u64>,
) -> ResolvedModelPolicy {
- let high_memory = match surface {
- ModelRuntimeSurface::Android | ModelRuntimeSurface::Ios => {
- total_memory_bytes.is_some_and(|bytes| bytes >= MOBILE_HIGH_MEMORY_THRESHOLD_BYTES)
- }
- ModelRuntimeSurface::Desktop => {
- total_memory_bytes.is_some_and(|bytes| bytes >= DESKTOP_HIGH_MEMORY_THRESHOLD_BYTES)
- }
- };
+ if matches!(
+ surface,
+ ModelRuntimeSurface::Android | ModelRuntimeSurface::Ios
+ ) {
+ let model = match total_memory_bytes {
+ Some(bytes) if bytes > MOBILE_STANDARD_MEMORY_MAX_BYTES => gemma_4_e2b_q4km(),
+ Some(bytes) if bytes > MOBILE_SMALL_MEMORY_MAX_BYTES => lfm_vl_1_6b(),
+ _ => lfm_350m(),
+ };
+ return ResolvedModelPolicy {
+ default_model: model.clone(),
+ visible_models: vec![model.clone()],
+ allowed_preferred_models: vec![model],
+ };
+ }
- let (default_id, visible_ids): (&str, &[&str]) = match (surface, high_memory) {
- (ModelRuntimeSurface::Android | ModelRuntimeSurface::Ios, true) => {
- ("gemma-4-e2b-q4km", &["gemma-4-e2b-q4km", "lfm-vl-1.6b"])
- }
- (ModelRuntimeSurface::Android | ModelRuntimeSurface::Ios, false) => {
- ("lfm-vl-1.6b", &["lfm-vl-1.6b"])
- }
- (ModelRuntimeSurface::Desktop, true) => (
+ let high_memory =
+ total_memory_bytes.is_some_and(|bytes| bytes >= DESKTOP_HIGH_MEMORY_THRESHOLD_BYTES);
+ let (default_id, visible_ids): (&str, &[&str]) = if high_memory {
+ (
"gemma-4-e4b-q4km",
&[
"gemma-4-e4b-q4km",
@@ -221,11 +238,12 @@ pub fn resolve_model_policy(
"qwen-0.8b",
"qwen-2b-q8",
],
- ),
- (ModelRuntimeSurface::Desktop, false) => (
+ )
+ } else {
+ (
"lfm-vl-1.6b",
&["lfm-vl-1.6b", "qwen-0.8b", "qwen-2b-q8", "gemma-4-e2b-q4km"],
- ),
+ )
};
let catalog = llm_catalog();
@@ -353,32 +371,52 @@ mod tests {
}
#[test]
- fn defaults_views_select_from_the_catalog() {
- let high_memory_mobile = resolve_model_policy(
- ModelRuntimeSurface::Android,
- Some(MOBILE_HIGH_MEMORY_THRESHOLD_BYTES),
- );
- assert_eq!(high_memory_mobile.default_model.id, "gemma-4-e2b-q4km");
- let effective_id = |surface, memory, preferred| {
- resolve_effective_model(surface, memory, Some(preferred)).id
- };
- assert_eq!(
- effective_id(
- ModelRuntimeSurface::Android,
- Some(MOBILE_HIGH_MEMORY_THRESHOLD_BYTES),
- "lfm-vl-1.6b",
- ),
- "lfm-vl-1.6b"
- );
- assert_eq!(
- effective_id(ModelRuntimeSurface::Ios, None, "gemma-4-e2b-q4km"),
- "gemma-4-e2b-q4km"
- );
+ fn mobile_ram_ladder_enforces_inclusive_boundaries_and_saved_preferences() {
+ let gib = 1024 * 1024 * 1024;
+ for surface in [ModelRuntimeSurface::Android, ModelRuntimeSurface::Ios] {
+ for (memory, expected) in [
+ (None, "lfm-350m"),
+ (Some(0), "lfm-350m"),
+ (Some(4 * gib), "lfm-350m"),
+ (Some(4 * gib + 1), "lfm-vl-1.6b"),
+ (Some(8 * gib), "lfm-vl-1.6b"),
+ (Some(8 * gib + 1), "gemma-4-e2b-q4km"),
+ ] {
+ let policy = resolve_model_policy(surface, memory);
+ assert_eq!(policy.default_model.id, expected, "RAM: {memory:?}");
+ assert_eq!(policy.visible_models.len(), 1);
+ assert_eq!(policy.visible_models[0].id, expected);
+ assert_eq!(policy.allowed_preferred_models.len(), 1);
+ assert_eq!(
+ policy.default_model.mmproj_url.is_none(),
+ expected == "lfm-350m"
+ );
+ for preferred in [None, Some("unknown")]
+ .into_iter()
+ .chain(llm_catalog().iter().map(|model| Some(model.id.as_str())))
+ {
+ assert_eq!(
+ resolve_effective_model(surface, memory, preferred).id,
+ expected,
+ "RAM: {memory:?}, preference: {preferred:?}"
+ );
+ }
+ }
+ }
+ }
- let desktop = resolve_model_policy(ModelRuntimeSurface::Desktop, None);
- assert_eq!(desktop.default_model.id, "lfm-vl-1.6b");
+ #[test]
+ fn desktop_model_policy_is_preserved() {
+ for (memory, expected, count) in [
+ (None, "lfm-vl-1.6b", 4),
+ (Some(16 * 1024 * 1024 * 1024), "gemma-4-e4b-q4km", 5),
+ ] {
+ let policy = resolve_model_policy(ModelRuntimeSurface::Desktop, memory);
+ assert_eq!(policy.default_model.id, expected);
+ assert_eq!(policy.visible_models.len(), count);
+ }
assert_eq!(
- effective_id(ModelRuntimeSurface::Desktop, None, "qwen-4b-q4km"),
+ resolve_effective_model(ModelRuntimeSurface::Desktop, None, Some("qwen-4b-q4km")).id,
"qwen-4b-q4km"
);
}
diff --git a/rust/crates/ensu/src/model/mod.rs b/rust/crates/ensu/src/model/mod.rs
index 5b2cb0c030..1bba6a7a0d 100644
--- a/rust/crates/ensu/src/model/mod.rs
+++ b/rust/crates/ensu/src/model/mod.rs
@@ -2,7 +2,7 @@ pub mod migrations;
use std::path::PathBuf;
-use ente_assets::{Asset, AssetDownloadProgress, AssetFile, AssetStore};
+use ente_assets::{Asset, AssetDownloadProgress, AssetFile, AssetStore, AssetStoreError};
use crate::config::{self, ModelPreset};
@@ -69,6 +69,29 @@ pub fn desktop_llm_asset(model_id: &str) -> Result<Asset, InvalidPreset> {
llm_asset(&preset)
}
+pub fn cleanup_obsolete_mobile_llm_assets(
+ store: &AssetStore,
+ model_id: &str,
+) -> Result<(), AssetStoreError> {
+ let catalog = config::llm_catalog();
+ let Some(active) = catalog
+ .iter()
+ .find(|preset| preset.id == model_id)
+ .and_then(|preset| llm_asset(preset).ok())
+ else {
+ return Ok(());
+ };
+ if !store.is_downloaded(&active) {
+ return Ok(());
+ }
+ let obsolete = catalog
+ .iter()
+ .filter(|preset| preset.id != model_id)
+ .map(|preset| model_key(&preset.id))
+ .collect::<Vec<_>>();
+ store.remove_keys(&obsolete)
+}
+
pub fn transcription_model_asset() -> Asset {
let preset = config::defaults().transcription_model;
#[expect(
@@ -205,6 +228,77 @@ fn format_bytes(bytes: u64) -> String {
mod tests {
use super::*;
+ fn publish_test_asset(store: &AssetStore, asset: &Asset) {
+ std::fs::create_dir_all(store.asset_dir(asset)).unwrap();
+ for name in [LLM_MODEL_FILE, LLM_MMPROJ_FILE, VOICE_ACTIVITY_MODEL_FILE] {
+ if let Some(path) = store.file_path(asset, name) {
+ std::fs::write(path, b"published test artifact").unwrap();
+ }
+ }
+ }
+
+ #[test]
+ fn cleanup_waits_for_every_replacement_file() {
+ let root = tempfile::tempdir().unwrap();
+ let store = AssetStore::new(root.path());
+ let old = mobile_llm_asset("gemma-4-e2b-q4km").unwrap();
+ let active = mobile_llm_asset("lfm-vl-1.6b").unwrap();
+ publish_test_asset(&store, &old);
+
+ cleanup_obsolete_mobile_llm_assets(&store, "lfm-vl-1.6b").unwrap();
+ assert!(store.is_downloaded(&old));
+
+ std::fs::create_dir_all(store.asset_dir(&active)).unwrap();
+ std::fs::write(llm_model_path(&store, &active).unwrap(), b"model only").unwrap();
+ assert!(!store.is_downloaded(&active));
+ cleanup_obsolete_mobile_llm_assets(&store, "lfm-vl-1.6b").unwrap();
+ assert!(store.is_downloaded(&old));
+
+ publish_test_asset(&store, &active);
+ cleanup_obsolete_mobile_llm_assets(&store, "lfm-vl-1.6b").unwrap();
+ assert!(store.is_downloaded(&active));
+ assert!(!store.asset_dir(&old).exists());
+ }
+
+ #[test]
+ fn cleanup_removes_only_obsolete_catalog_models_for_a_known_replacement() {
+ let root = tempfile::tempdir().unwrap();
+ let store = AssetStore::new(root.path());
+ let catalog = config::llm_catalog();
+ for preset in &catalog {
+ publish_test_asset(&store, &llm_asset(preset).unwrap());
+ }
+ let retained = [
+ transcription_model_asset(),
+ voice_activity_model_asset(),
+ knowledge_embedding_model_asset(),
+ ];
+ for asset in &retained {
+ publish_test_asset(&store, asset);
+ }
+ let custom = root.path().join("models/custom-model/model.gguf");
+ std::fs::create_dir_all(custom.parent().unwrap()).unwrap();
+ std::fs::write(&custom, b"custom").unwrap();
+
+ cleanup_obsolete_mobile_llm_assets(&store, "unknown").unwrap();
+ assert!(
+ catalog
+ .iter()
+ .all(|preset| store.is_downloaded(&llm_asset(preset).unwrap()))
+ );
+ for _ in 0..2 {
+ cleanup_obsolete_mobile_llm_assets(&store, "lfm-350m").unwrap();
+ for preset in &catalog {
+ assert_eq!(
+ store.asset_dir(&llm_asset(preset).unwrap()).exists(),
+ preset.id == "lfm-350m",
+ );
+ }
+ assert!(retained.iter().all(|asset| store.is_downloaded(asset)));
+ assert_eq!(std::fs::read(&custom).unwrap(), b"custom");
+ }
+ }
+
fn preset(mmproj_url: Option<&str>, mmproj_sha256: Option<&str>) -> ModelPreset {
ModelPreset {
id: "qwen-2b-q8".to_string(),