Skip to content

Commit 03a655c

Browse files
committed
Add model selector memory summary
1 parent b4d3cac commit 03a655c

3 files changed

Lines changed: 307 additions & 47 deletions

File tree

‎app/src/main/java/tridefender/llama/snapdragon/ui/launcher/AllConfigScreen.kt‎

Lines changed: 251 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,28 @@ import androidx.compose.material.icons.filled.Memory
2222
import androidx.compose.material.icons.filled.Settings
2323
import androidx.compose.material.icons.filled.Storage
2424
import androidx.compose.material.icons.outlined.Check
25+
import androidx.compose.material.icons.outlined.Refresh
2526
import androidx.compose.material3.*
2627
import androidx.compose.runtime.*
2728
import androidx.compose.ui.Alignment
2829
import androidx.compose.ui.Modifier
30+
import androidx.compose.ui.graphics.Color
2931
import androidx.compose.ui.graphics.vector.ImageVector
3032
import androidx.compose.ui.platform.LocalContext
3133
import androidx.compose.ui.res.stringResource
34+
import androidx.compose.ui.text.font.FontFamily
3235
import androidx.compose.ui.text.font.FontWeight
36+
import androidx.compose.ui.text.style.TextOverflow
3337
import androidx.compose.ui.unit.dp
3438
import androidx.hilt.navigation.compose.hiltViewModel
3539
import tridefender.llama.snapdragon.R
3640
import tridefender.llama.snapdragon.model.CacheType
3741
import tridefender.llama.snapdragon.model.DeviceType
3842
import tridefender.llama.snapdragon.model.FlashAttentionMode
43+
import tridefender.llama.snapdragon.model.MemoryFitState
44+
import tridefender.llama.snapdragon.model.ModelMemoryEstimate
3945
import tridefender.llama.snapdragon.model.PoolingType
46+
import tridefender.llama.snapdragon.viewmodel.ModelCatalogEntry
4047
import tridefender.llama.snapdragon.viewmodel.ModelConfigViewModel
4148

4249
private const val TAG = "AllConfigScreen"
@@ -82,6 +89,7 @@ fun AllConfigScreen(
8289
viewModel: ModelConfigViewModel = hiltViewModel()
8390
) {
8491
val config by viewModel.config.collectAsState()
92+
val analysis by viewModel.analysis.collectAsState()
8593
val context = LocalContext.current
8694

8795
val filePickerLauncher = rememberLauncherForActivityResult(
@@ -98,7 +106,7 @@ fun AllConfigScreen(
98106
Log.w(TAG, "Failed to persist URI permission for $it", error)
99107
}
100108

101-
viewModel.updateModelPath(it.toString())
109+
viewModel.importModelUri(it.toString())
102110
}
103111
}
104112
}
@@ -112,10 +120,15 @@ fun AllConfigScreen(
112120
) {
113121
ModelSection(
114122
modelPath = config.modelPath,
123+
modelCatalog = analysis.catalog,
124+
selectedEstimate = analysis.selectedEstimate,
125+
freeBytes = analysis.freeBytes,
115126
isEmbedding = config.isEmbedding,
116127
poolingType = config.poolingType,
128+
onModelSelect = { viewModel.updateModelPath(it) },
117129
onIsEmbeddingChange = { viewModel.updateIsEmbedding(it) },
118130
onPoolingTypeChange = { viewModel.updatePoolingType(it) },
131+
onRefreshModels = { viewModel.refreshModelCatalog() },
119132
onBrowseClick = {
120133
filePickerLauncher.launch(
121134
Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
@@ -179,10 +192,15 @@ fun AllConfigScreen(
179192
@Composable
180193
fun ModelSection(
181194
modelPath: String,
195+
modelCatalog: List<ModelCatalogEntry>,
196+
selectedEstimate: ModelMemoryEstimate?,
197+
freeBytes: Long,
182198
isEmbedding: Boolean,
183199
poolingType: PoolingType,
200+
onModelSelect: (String) -> Unit,
184201
onIsEmbeddingChange: (Boolean) -> Unit,
185202
onPoolingTypeChange: (PoolingType) -> Unit,
203+
onRefreshModels: () -> Unit,
186204
onBrowseClick: () -> Unit
187205
) {
188206
ConfigSectionCard(
@@ -191,20 +209,113 @@ fun ModelSection(
191209
iconColor = MaterialTheme.colorScheme.primary
192210
) {
193211
val displayName = modelPath.substringAfterLast("%2F").substringAfterLast("/")
194-
195-
OutlinedTextField(
196-
value = displayName.ifEmpty { stringResource(R.string.tap_to_select_model_file) },
197-
onValueChange = {},
198-
label = { Text(stringResource(R.string.model_file)) },
199-
modifier = Modifier.fillMaxWidth(),
200-
readOnly = true,
201-
singleLine = true,
202-
trailingIcon = {
203-
IconButton(onClick = onBrowseClick) {
204-
Icon(Icons.Default.Folder, contentDescription = stringResource(R.string.browse))
212+
213+
var modelMenuExpanded by remember { mutableStateOf(false) }
214+
ExposedDropdownMenuBox(
215+
expanded = modelMenuExpanded,
216+
onExpandedChange = { modelMenuExpanded = it && modelCatalog.isNotEmpty() }
217+
) {
218+
OutlinedTextField(
219+
value = displayName.ifEmpty { stringResource(R.string.tap_to_select_model_file) },
220+
onValueChange = {},
221+
label = { Text(stringResource(R.string.model_file)) },
222+
supportingText = {
223+
Text(
224+
text = if (modelCatalog.isEmpty()) {
225+
stringResource(R.string.no_cached_models)
226+
} else {
227+
stringResource(R.string.cached_models_count, modelCatalog.size)
228+
},
229+
maxLines = 1,
230+
overflow = TextOverflow.Ellipsis
231+
)
232+
},
233+
modifier = Modifier
234+
.menuAnchor()
235+
.fillMaxWidth(),
236+
readOnly = true,
237+
singleLine = true,
238+
trailingIcon = { ExposedDropdownMenuDefaults.TrailingIcon(expanded = modelMenuExpanded) },
239+
shape = RoundedCornerShape(12.dp)
240+
)
241+
242+
ExposedDropdownMenu(
243+
expanded = modelMenuExpanded,
244+
onDismissRequest = { modelMenuExpanded = false }
245+
) {
246+
modelCatalog.forEach { entry ->
247+
DropdownMenuItem(
248+
text = {
249+
Column(verticalArrangement = Arrangement.spacedBy(2.dp)) {
250+
Text(
251+
text = entry.name,
252+
maxLines = 1,
253+
overflow = TextOverflow.Ellipsis
254+
)
255+
Text(
256+
text = entry.estimate?.let {
257+
stringResource(
258+
R.string.model_menu_memory_line,
259+
formatBytes(it.totalBytes),
260+
fitLabel(it.fitState)
261+
)
262+
} ?: stringResource(R.string.model_metadata_unavailable),
263+
style = MaterialTheme.typography.bodySmall,
264+
color = entry.estimate?.fitState?.fitColor()
265+
?: MaterialTheme.colorScheme.onSurfaceVariant
266+
)
267+
}
268+
},
269+
onClick = {
270+
onModelSelect(entry.path)
271+
modelMenuExpanded = false
272+
},
273+
trailingIcon = {
274+
if (entry.path == modelPath) {
275+
Icon(
276+
Icons.Outlined.Check,
277+
contentDescription = null,
278+
tint = MaterialTheme.colorScheme.primary,
279+
modifier = Modifier.size(18.dp)
280+
)
281+
}
282+
}
283+
)
205284
}
206-
},
207-
shape = RoundedCornerShape(12.dp)
285+
}
286+
}
287+
288+
Row(
289+
modifier = Modifier.fillMaxWidth(),
290+
horizontalArrangement = Arrangement.End,
291+
verticalAlignment = Alignment.CenterVertically
292+
) {
293+
TextButton(onClick = onRefreshModels) {
294+
Icon(
295+
Icons.Outlined.Refresh,
296+
contentDescription = null,
297+
modifier = Modifier.size(18.dp)
298+
)
299+
Spacer(modifier = Modifier.width(6.dp))
300+
Text(stringResource(R.string.refresh_models))
301+
}
302+
TextButton(onClick = onBrowseClick) {
303+
Icon(
304+
Icons.Default.Folder,
305+
contentDescription = null,
306+
modifier = Modifier.size(18.dp)
307+
)
308+
Spacer(modifier = Modifier.width(6.dp))
309+
Text(stringResource(R.string.browse))
310+
}
311+
}
312+
313+
Spacer(modifier = Modifier.height(12.dp))
314+
315+
ModelMemoryCard(
316+
estimate = selectedEstimate,
317+
freeBytes = freeBytes,
318+
modifier = Modifier.fillMaxWidth()
208319
)
209320

210321
Spacer(modifier = Modifier.height(16.dp))
@@ -642,6 +753,102 @@ fun ExtraParamsSection(
642753
}
643754
}
644755

756+
@Composable
757+
private fun ModelMemoryCard(
758+
estimate: ModelMemoryEstimate?,
759+
freeBytes: Long,
760+
modifier: Modifier = Modifier
761+
) {
762+
Surface(
763+
modifier = modifier,
764+
shape = RoundedCornerShape(12.dp),
765+
color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f)
766+
) {
767+
Column(
768+
modifier = Modifier
769+
.fillMaxWidth()
770+
.padding(16.dp),
771+
verticalArrangement = Arrangement.spacedBy(10.dp)
772+
) {
773+
Row(
774+
modifier = Modifier.fillMaxWidth(),
775+
horizontalArrangement = Arrangement.SpaceBetween,
776+
verticalAlignment = Alignment.CenterVertically
777+
) {
778+
Column(modifier = Modifier.weight(1f)) {
779+
Text(
780+
text = stringResource(R.string.model_memory_calculator),
781+
style = MaterialTheme.typography.bodyLarge,
782+
fontWeight = FontWeight.Medium
783+
)
784+
Text(
785+
text = stringResource(R.string.unified_memory_note),
786+
style = MaterialTheme.typography.bodySmall,
787+
color = MaterialTheme.colorScheme.onSurfaceVariant
788+
)
789+
}
790+
791+
if (estimate != null) {
792+
AssistChip(
793+
onClick = {},
794+
label = { Text(fitLabel(estimate.fitState)) },
795+
colors = AssistChipDefaults.assistChipColors(
796+
labelColor = estimate.fitState.fitColor()
797+
)
798+
)
799+
}
800+
}
801+
802+
if (estimate == null) {
803+
Text(
804+
text = stringResource(
805+
R.string.model_memory_waiting,
806+
formatBytes(freeBytes)
807+
),
808+
style = MaterialTheme.typography.bodySmall,
809+
color = MaterialTheme.colorScheme.onSurfaceVariant
810+
)
811+
} else {
812+
MemoryInfoRow(stringResource(R.string.available_memory), formatBytes(estimate.freeBytes))
813+
MemoryInfoRow(stringResource(R.string.model_weights), formatBytes(estimate.modelBytes))
814+
MemoryInfoRow(stringResource(R.string.kv_cache_estimate), formatBytes(estimate.kvCacheBytes))
815+
MemoryInfoRow(stringResource(R.string.runtime_overhead), formatBytes(estimate.overheadBytes))
816+
MemoryInfoRow(stringResource(R.string.total_memory_estimate), formatBytes(estimate.totalBytes))
817+
MemoryInfoRow(
818+
label = stringResource(R.string.memory_after_load),
819+
value = formatSignedBytes(estimate.remainingBytes),
820+
valueColor = estimate.fitState.fitColor()
821+
)
822+
}
823+
}
824+
}
825+
}
826+
827+
@Composable
828+
private fun MemoryInfoRow(
829+
label: String,
830+
value: String,
831+
valueColor: Color = MaterialTheme.colorScheme.onSurface
832+
) {
833+
Row(
834+
modifier = Modifier.fillMaxWidth(),
835+
horizontalArrangement = Arrangement.SpaceBetween,
836+
verticalAlignment = Alignment.CenterVertically
837+
) {
838+
Text(
839+
text = label,
840+
style = MaterialTheme.typography.bodySmall,
841+
color = MaterialTheme.colorScheme.onSurfaceVariant
842+
)
843+
Text(
844+
text = value,
845+
style = MaterialTheme.typography.bodySmall,
846+
fontFamily = FontFamily.Monospace,
847+
color = valueColor
848+
)
849+
}
850+
}
851+
645852
@Composable
646853
private fun PoolingType.localizedName(): String = when (this) {
647854
PoolingType.NONE -> stringResource(R.string.none)
@@ -651,6 +858,36 @@ private fun PoolingType.localizedName(): String = when (this) {
651858
PoolingType.RANK -> stringResource(R.string.rank)
652859
}
653860

861+
@Composable
862+
private fun fitLabel(state: MemoryFitState): String = when (state) {
863+
MemoryFitState.WONT_FIT -> stringResource(R.string.fit_wont_fit)
864+
MemoryFitState.TIGHT_FIT -> stringResource(R.string.fit_tight_fit)
865+
MemoryFitState.GOOD_FIT -> stringResource(R.string.fit_good_fit)
866+
}
867+
868+
@Composable
869+
private fun MemoryFitState.fitColor(): Color = when (this) {
870+
MemoryFitState.WONT_FIT -> MaterialTheme.colorScheme.error
871+
MemoryFitState.TIGHT_FIT -> Color(0xFFB26A00)
872+
MemoryFitState.GOOD_FIT -> Color(0xFF2E7D32)
873+
}
874+
875+
private fun formatBytes(bytes: Long): String {
876+
val absBytes = kotlin.math.abs(bytes.toDouble())
877+
val gib = 1024.0 * 1024.0 * 1024.0
878+
val mib = 1024.0 * 1024.0
879+
return if (absBytes >= gib) {
880+
"%.2f GiB".format(bytes / gib)
881+
} else {
882+
"%.0f MiB".format(bytes / mib)
883+
}
884+
}
885+
886+
private fun formatSignedBytes(bytes: Long): String {
887+
val prefix = if (bytes > 0) "+" else ""
888+
return prefix + formatBytes(bytes)
889+
}
890+
654891
@Composable
655892
fun ConfigSectionCard(
656893
title: String,

0 commit comments

Comments
 (0)