Skip to content

Commit 6ef83f3

Browse files
authored
Add seq len settings (#204)
1 parent e54bb41 commit 6ef83f3

3 files changed

Lines changed: 93 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: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package com.example.executorchllamademo.ui.screens
1010

1111
import androidx.compose.foundation.background
12+
import androidx.compose.foundation.border
1213
import androidx.compose.foundation.clickable
1314
import androidx.compose.foundation.layout.Column
1415
import androidx.compose.foundation.layout.Row
@@ -20,6 +21,8 @@ import androidx.compose.foundation.layout.padding
2021
import androidx.compose.foundation.layout.size
2122
import androidx.compose.foundation.rememberScrollState
2223
import androidx.compose.foundation.shape.RoundedCornerShape
24+
import androidx.compose.foundation.text.BasicTextField
25+
import androidx.compose.foundation.text.KeyboardOptions
2326
import androidx.compose.foundation.verticalScroll
2427
import androidx.compose.material.icons.Icons
2528
import androidx.compose.material.icons.filled.ArrowBack
@@ -43,8 +46,11 @@ import androidx.compose.runtime.setValue
4346
import androidx.compose.ui.Alignment
4447
import androidx.compose.ui.Modifier
4548
import androidx.compose.ui.graphics.Color
49+
import androidx.compose.ui.graphics.SolidColor
4650
import androidx.compose.ui.platform.LocalContext
51+
import androidx.compose.ui.text.TextStyle
4752
import androidx.compose.ui.text.font.FontWeight
53+
import androidx.compose.ui.text.input.KeyboardType
4854
import androidx.compose.ui.unit.dp
4955
import androidx.compose.ui.unit.sp
5056
import com.example.executorchllamademo.AppSettings
@@ -68,11 +74,13 @@ fun AppSettingsScreen(
6874
var moduleSettings by remember { mutableStateOf(ModuleSettings()) }
6975
var showAppearanceDialog by remember { mutableStateOf(false) }
7076
var showClearChatDialog by remember { mutableStateOf(false) }
77+
var maxSeqLenText by remember { mutableStateOf("") }
7178

7279
LaunchedEffect(Unit) {
7380
val prefs = DemoSharedPreferences(context)
7481
appSettings = prefs.getAppSettings()
7582
moduleSettings = prefs.getModuleSettings()
83+
maxSeqLenText = appSettings.maxSeqLen.toString()
7684
}
7785

7886
Column(
@@ -133,6 +141,77 @@ fun AppSettingsScreen(
133141

134142
Spacer(modifier = Modifier.height(24.dp))
135143

144+
// Model Configuration section header
145+
Text(
146+
text = "Model Configuration",
147+
fontSize = 16.sp,
148+
fontWeight = FontWeight.Bold,
149+
color = appColors.settingsText
150+
)
151+
152+
Spacer(modifier = Modifier.height(8.dp))
153+
154+
// Max Seq Len input field
155+
Column(
156+
modifier = Modifier
157+
.fillMaxWidth()
158+
.background(appColors.settingsRowBackground, RoundedCornerShape(8.dp))
159+
.padding(horizontal = 16.dp, vertical = 12.dp)
160+
) {
161+
Text(
162+
text = "Max Sequence Length",
163+
fontSize = 14.sp,
164+
color = appColors.settingsText,
165+
fontWeight = FontWeight.Medium
166+
)
167+
Spacer(modifier = Modifier.height(8.dp))
168+
BasicTextField(
169+
value = maxSeqLenText,
170+
onValueChange = { newValue ->
171+
maxSeqLenText = newValue
172+
val newMaxSeqLen = newValue.toIntOrNull()
173+
if (newMaxSeqLen != null && newMaxSeqLen > 0) {
174+
appSettings = appSettings.copy(maxSeqLen = newMaxSeqLen)
175+
val prefs = DemoSharedPreferences(context)
176+
prefs.saveAppSettings(appSettings)
177+
}
178+
},
179+
keyboardOptions = KeyboardOptions(keyboardType = KeyboardType.Number),
180+
singleLine = true,
181+
textStyle = TextStyle(
182+
color = appColors.settingsText,
183+
fontSize = 16.sp
184+
),
185+
cursorBrush = SolidColor(appColors.settingsText),
186+
modifier = Modifier.fillMaxWidth(),
187+
decorationBox = { innerTextField ->
188+
Row(
189+
modifier = Modifier
190+
.fillMaxWidth()
191+
.border(1.dp, appColors.settingsText.copy(alpha = 0.5f), RoundedCornerShape(4.dp))
192+
.padding(horizontal = 12.dp, vertical = 14.dp)
193+
) {
194+
if (maxSeqLenText.isEmpty()) {
195+
Text(
196+
text = "Enter max sequence length",
197+
color = appColors.settingsText.copy(alpha = 0.5f),
198+
fontSize = 16.sp
199+
)
200+
}
201+
innerTextField()
202+
}
203+
}
204+
)
205+
Text(
206+
text = "Maximum number of tokens to generate (default: ${AppSettings.DEFAULT_MAX_SEQ_LEN})",
207+
fontSize = 12.sp,
208+
color = appColors.settingsText.copy(alpha = 0.6f),
209+
modifier = Modifier.padding(top = 4.dp)
210+
)
211+
}
212+
213+
Spacer(modifier = Modifier.height(24.dp))
214+
136215
// Conversation section header
137216
Text(
138217
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)