Skip to content

Commit fdfb3eb

Browse files
Use I/O dispatcher to load LLM into memory
1 parent 547e055 commit fdfb3eb

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

app/src/main/java/org/vonderheidt/hips/ui/screens/SettingsScreen.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ import androidx.compose.ui.unit.sp
5050
import androidx.navigation.NavController
5151
import androidx.navigation.NavHostController
5252
import androidx.navigation.compose.rememberNavController
53+
import kotlinx.coroutines.CoroutineScope
54+
import kotlinx.coroutines.Dispatchers
5355
import kotlinx.coroutines.launch
5456
import org.vonderheidt.hips.data.HiPSDataStore
5557
import org.vonderheidt.hips.data.Settings
@@ -189,14 +191,19 @@ fun SettingsScreen(navController: NavController, modifier: Modifier) {
189191
Button(
190192
onClick = {
191193
// Check if the LLM is in memory already, load/unload it and update the state variable
192-
// Needs coroutineScope.launch{...} around every function call to update the state only after loading/unloading
194+
// Use coroutine with Dispatchers.IO since loading the LLM is I/O-bound, doesn't block UI in main thread anymore this way
193195
if (!isInMemory) {
194-
coroutineScope.launch { LlamaCpp.startInstance() }
196+
CoroutineScope(Dispatchers.IO).launch {
197+
LlamaCpp.startInstance()
198+
isInMemory = LlamaCpp.isInMemory()
199+
}
195200
}
196201
else {
197-
coroutineScope.launch { LlamaCpp.stopInstance() }
202+
CoroutineScope(Dispatchers.IO).launch {
203+
LlamaCpp.stopInstance()
204+
isInMemory = LlamaCpp.isInMemory()
205+
}
198206
}
199-
coroutineScope.launch { isInMemory = LlamaCpp.isInMemory() }
200207
},
201208
shape = RoundedCornerShape(4.dp)
202209
) {

0 commit comments

Comments
 (0)