Skip to content

Commit 4c4e33b

Browse files
committed
Add seq len settings
1 parent 480d9a2 commit 4c4e33b

3 files changed

Lines changed: 69 additions & 5 deletions

File tree

llm/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/AppSettings.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,10 @@ package com.example.executorchllamademo
1313
*/
1414
data class AppSettings(
1515
val appearanceMode: AppearanceMode = AppearanceMode.SYSTEM,
16-
val saveChatHistory: Boolean = false
17-
)
16+
val saveChatHistory: Boolean = false,
17+
val maxSeqLen: Int = DEFAULT_MAX_SEQ_LEN
18+
) {
19+
companion object {
20+
const val DEFAULT_MAX_SEQ_LEN = 768
21+
}
22+
}

llm/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/ui/screens/AppSettingsScreen.kt

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.compose.foundation.layout.padding
2020
import androidx.compose.foundation.layout.size
2121
import androidx.compose.foundation.rememberScrollState
2222
import androidx.compose.foundation.shape.RoundedCornerShape
23+
import androidx.compose.foundation.text.KeyboardOptions
2324
import androidx.compose.foundation.verticalScroll
2425
import androidx.compose.material.icons.Icons
2526
import androidx.compose.material.icons.filled.ArrowBack
@@ -29,6 +30,7 @@ import androidx.compose.material3.Button
2930
import androidx.compose.material3.ButtonDefaults
3031
import androidx.compose.material3.Icon
3132
import androidx.compose.material3.IconButton
33+
import androidx.compose.material3.OutlinedTextField
3234
import androidx.compose.material3.RadioButton
3335
import androidx.compose.material3.Switch
3436
import androidx.compose.material3.SwitchDefaults
@@ -45,6 +47,7 @@ import androidx.compose.ui.Modifier
4547
import androidx.compose.ui.graphics.Color
4648
import androidx.compose.ui.platform.LocalContext
4749
import androidx.compose.ui.text.font.FontWeight
50+
import androidx.compose.ui.text.input.KeyboardType
4851
import androidx.compose.ui.unit.dp
4952
import androidx.compose.ui.unit.sp
5053
import com.example.executorchllamademo.AppSettings
@@ -68,11 +71,13 @@ fun AppSettingsScreen(
6871
var moduleSettings by remember { mutableStateOf(ModuleSettings()) }
6972
var showAppearanceDialog by remember { mutableStateOf(false) }
7073
var showClearChatDialog by remember { mutableStateOf(false) }
74+
var maxSeqLenText by remember { mutableStateOf("") }
7175

7276
LaunchedEffect(Unit) {
7377
val prefs = DemoSharedPreferences(context)
7478
appSettings = prefs.getAppSettings()
7579
moduleSettings = prefs.getModuleSettings()
80+
maxSeqLenText = appSettings.maxSeqLen.toString()
7681
}
7782

7883
Column(
@@ -133,6 +138,56 @@ fun AppSettingsScreen(
133138

134139
Spacer(modifier = Modifier.height(24.dp))
135140

141+
// Model Configuration section header
142+
Text(
143+
text = "Model Configuration",
144+
fontSize = 16.sp,
145+
fontWeight = FontWeight.Bold,
146+
color = appColors.settingsText
147+
)
148+
149+
Spacer(modifier = Modifier.height(8.dp))
150+
151+
// Max Seq Len input field
152+
Column(
153+
modifier = Modifier
154+
.fillMaxWidth()
155+
.background(appColors.settingsRowBackground, RoundedCornerShape(8.dp))
156+
.padding(horizontal = 16.dp, vertical = 12.dp)
157+
) {
158+
Text(
159+
text = "Max Sequence Length",
160+
fontSize = 14.sp,
161+
color = appColors.settingsText,
162+
fontWeight = FontWeight.Medium
163+
)
164+
Spacer(modifier = Modifier.height(8.dp))
165+
OutlinedTextField(
166+
value = maxSeqLenText,
167+
onValueChange = { newValue ->
168+
maxSeqLenText = newValue
169+
val newMaxSeqLen = newValue.toIntOrNull()
170+
if (newMaxSeqLen != null && newMaxSeqLen > 0) {
171+
appSettings = appSettings.copy(maxSeqLen = newMaxSeqLen)
172+
val prefs = DemoSharedPreferences(context)
173+
prefs.saveAppSettings(appSettings)
174+
}
175+
},
176+
label = { Text("Enter max sequence length") },
177+
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
178+
modifier = Modifier.fillMaxWidth(),
179+
singleLine = true
180+
)
181+
Text(
182+
text = "Maximum number of tokens to generate (default: ${AppSettings.DEFAULT_MAX_SEQ_LEN})",
183+
fontSize = 12.sp,
184+
color = appColors.settingsText.copy(alpha = 0.6f),
185+
modifier = Modifier.padding(top = 4.dp)
186+
)
187+
}
188+
189+
Spacer(modifier = Modifier.height(24.dp))
190+
136191
// Conversation section header
137192
Text(
138193
text = "Conversation",

llm/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/ui/viewmodel/ChatViewModel.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import androidx.compose.runtime.mutableStateOf
1919
import androidx.compose.runtime.setValue
2020
import androidx.lifecycle.AndroidViewModel
2121
import com.example.executorchllamademo.BackendType
22+
import com.example.executorchllamademo.AppSettings
2223
import com.example.executorchllamademo.DemoSharedPreferences
2324
import com.example.executorchllamademo.ETImage
2425
import com.example.executorchllamademo.ETLogging
@@ -85,6 +86,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), L
8586
private var resultMessage: Message? = null
8687
private val demoSharedPreferences = DemoSharedPreferences(application)
8788
private var currentSettingsFields = ModuleSettings()
89+
private var appSettings = AppSettings()
8890
private var promptID = 0
8991
private var sawStartHeaderId = false
9092
private var audioFileToPrefill: String? = null
@@ -96,6 +98,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), L
9698
init {
9799
// Check for clear chat history flag BEFORE loading saved messages
98100
val moduleSettings = demoSharedPreferences.getModuleSettings()
101+
appSettings = demoSharedPreferences.getAppSettings()
99102
if (moduleSettings.isClearChatHistory) {
100103
// Clear the flag and don't load messages
101104
// Keep isLoadModel flag so model still loads in checkAndLoadSettings()
@@ -144,6 +147,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), L
144147

145148
fun checkAndLoadSettings() {
146149
val updatedSettingsFields = demoSharedPreferences.getModuleSettings()
150+
appSettings = demoSharedPreferences.getAppSettings()
147151
val isUpdated = currentSettingsFields != updatedSettingsFields
148152
val isLoadModel = updatedSettingsFields.isLoadModel
149153

@@ -662,10 +666,10 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), L
662666
prefillVoxtralAudio(audioFileToPrefill!!, finalPrompt)
663667
audioFileToPrefill = null
664668
ETLogging.getInstance().log("Running vision model inference.. prompt=(empty after audio prefill)")
665-
module?.generate("", ModelUtils.VISION_MODEL_SEQ_LEN, this, false)
669+
module?.generate("", appSettings.maxSeqLen, this, false)
666670
} else {
667671
ETLogging.getInstance().log("Running vision model inference.. prompt=$finalPrompt")
668-
module?.generate(finalPrompt, ModelUtils.VISION_MODEL_SEQ_LEN, this, false)
672+
module?.generate(finalPrompt, appSettings.maxSeqLen, this, false)
669673
}
670674
} else if (currentSettingsFields.modelType == ModelType.LLAMA_GUARD_3) {
671675
val llamaGuardPromptForClassification =
@@ -679,7 +683,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), L
679683
)
680684
} else {
681685
ETLogging.getInstance().log("Running inference.. prompt=$finalPrompt")
682-
module?.generate(finalPrompt, ModelUtils.TEXT_MODEL_SEQ_LEN, this, false)
686+
module?.generate(finalPrompt, appSettings.maxSeqLen, this, false)
683687
}
684688

685689
val generateDuration = System.currentTimeMillis() - generateStartTime

0 commit comments

Comments
 (0)