Skip to content

Commit 1d5dfab

Browse files
committed
Merge branch 'gemma' into gemma-test
2 parents bf39024 + 2fe4f7e commit 1d5dfab

2 files changed

Lines changed: 31 additions & 14 deletions

File tree

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,22 @@ object PromptFormat {
8585
@JvmStatic
8686
fun getLlavaPresetPrompt(): String {
8787
return "A chat between a curious human and an artificial intelligence assistant. The assistant" +
88-
" gives helpful, detailed, and polite answers to the human's questions. USER: "
88+
" gives helpful, detailed, and polite answers to the human's questions."
8989
}
9090

9191
@JvmStatic
92-
fun getLlavaFirstTurnUserPrompt(): String {
93-
return "$USER_PLACEHOLDER ASSISTANT:"
92+
fun getLlavaMultimodalUserPrompt(): String {
93+
return "USER: $USER_PLACEHOLDER ASSISTANT:"
94+
}
95+
96+
@JvmStatic
97+
fun getGemmaPreImagePrompt(): String {
98+
return "<start_of_turn>user\n<start_of_image>"
99+
}
100+
101+
@JvmStatic
102+
fun getGemmaMultimodalUserPrompt(): String {
103+
return "$USER_PLACEHOLDER<end_of_turn>\n<start_of_turn>model"
94104
}
95105

96106
@JvmStatic

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,23 +110,26 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), L
110110
val isLoadModel = updatedSettingsFields.isLoadModel
111111
if (isUpdated) {
112112
checkForClearChatHistory(updatedSettingsFields)
113-
// Update media capabilities after settings are updated
114-
setBackendMode(updatedSettingsFields.backendType)
115113

116114
if (isLoadModel) {
115+
// Update local copy BEFORE checking media capabilities
116+
val settingsWithLoadFlagCleared = updatedSettingsFields.copy(isLoadModel = false)
117+
currentSettingsFields = settingsWithLoadFlagCleared
118+
demoSharedPreferences.saveModuleSettings(settingsWithLoadFlagCleared)
119+
120+
// Update media capabilities after settings are updated
121+
setBackendMode(updatedSettingsFields.backendType)
122+
117123
loadLocalModelAndParameters(
118124
updatedSettingsFields.modelFilePath,
119125
updatedSettingsFields.tokenizerFilePath,
120126
updatedSettingsFields.dataPath,
121127
updatedSettingsFields.temperature.toFloat()
122128
)
123-
// Save with isLoadModel = false and update local copy to match,
124-
// preventing duplicate "To get started..." messages on subsequent calls
125-
val settingsWithLoadFlagCleared = updatedSettingsFields.copy(isLoadModel = false)
126-
demoSharedPreferences.saveModuleSettings(settingsWithLoadFlagCleared)
127-
currentSettingsFields = settingsWithLoadFlagCleared
128129
} else {
129130
currentSettingsFields = updatedSettingsFields.copy()
131+
// Update media capabilities after settings are updated
132+
setBackendMode(updatedSettingsFields.backendType)
130133
if (module == null) {
131134
addSystemMessage(systemPromptMessage)
132135
}
@@ -317,13 +320,13 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), L
317320
val processedImageList = getProcessedImagesForModel(_selectedImages)
318321
if (processedImageList.isNotEmpty()) {
319322
_messages.add(
320-
Message("Llava - Starting image Prefill.", false, MessageType.SYSTEM, 0)
323+
Message("Starting image prefill.", false, MessageType.SYSTEM, 0)
321324
)
322325
executor.execute {
323326
android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_MORE_FAVORABLE)
324327
ETLogging.getInstance().log("Starting runnable prefill image")
325328
val img = processedImageList[0]
326-
ETLogging.getInstance().log("Llava start prefill image")
329+
ETLogging.getInstance().log("Starting prefill image")
327330
if (currentSettingsFields.modelType == ModelType.LLAVA_1_5) {
328331
module?.prefillImages(
329332
img.getInts(),
@@ -332,6 +335,7 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), L
332335
ModelUtils.VISION_MODEL_IMAGE_CHANNELS
333336
)
334337
} else if (currentSettingsFields.modelType == ModelType.GEMMA_3) {
338+
module?.prefillPrompt(PromptFormat.getGemmaPreImagePrompt())
335339
module?.prefillImages(
336340
img.getFloats(),
337341
img.width,
@@ -372,8 +376,11 @@ class ChatViewModel(application: Application) : AndroidViewModel(application), L
372376
val rawPrompt = inputText
373377
val finalPrompt: String
374378

375-
if (currentSettingsFields.modelType == ModelType.LLAVA_1_5 && shouldAddSystemPrompt) {
376-
finalPrompt = PromptFormat.getLlavaFirstTurnUserPrompt()
379+
if (currentSettingsFields.modelType == ModelType.LLAVA_1_5 && _selectedImages.isNotEmpty()) {
380+
finalPrompt = PromptFormat.getLlavaMultimodalUserPrompt()
381+
.replace(PromptFormat.USER_PLACEHOLDER, rawPrompt)
382+
} else if (currentSettingsFields.modelType == ModelType.GEMMA_3 && _selectedImages.isNotEmpty()) {
383+
finalPrompt = PromptFormat.getGemmaMultimodalUserPrompt()
377384
.replace(PromptFormat.USER_PLACEHOLDER, rawPrompt)
378385
} else {
379386
finalPrompt = (if (shouldAddSystemPrompt) currentSettingsFields.getFormattedSystemPrompt() else "") +

0 commit comments

Comments
 (0)