Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion llm/android/LlamaDemo/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ dependencies {
if (useLocalAar == true) {
implementation(files("libs/executorch.aar"))
} else {
implementation("org.pytorch:executorch-android:1.0.0")
implementation("org.pytorch:executorch-android:1.0.1")
// https://mvnrepository.com/artifact/org.pytorch/executorch-android-qnn
// Uncomment this to enable QNN
// implementation("org.pytorch:executorch-android-qnn:1.0.1")

// https://mvnrepository.com/artifact/org.pytorch/executorch-android-vulkan
// uncomment to enable vulkan
// implementation("org.pytorch:executorch-android-vulkan:1.0.1")
}
implementation("com.google.android.material:material:1.12.0")
implementation("androidx.activity:activity:1.9.0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,17 @@ private void onModelRunStopped() {
}
addSelectedImagesToChatThread(mSelectedImageUri);
String rawPrompt = mEditTextMessage.getText().toString();
String finalPrompt =
(shouldAddSystemPrompt ? mCurrentSettingsFields.getFormattedSystemPrompt() : "")
+ mCurrentSettingsFields.getFormattedUserPrompt(rawPrompt, mThinkMode);
String finalPrompt;
// For LLaVA, the first turn uses a special template since the preset prompt
// already ends with "USER: "
if (mCurrentSettingsFields.getModelType() == ModelType.LLAVA_1_5 && shouldAddSystemPrompt) {
finalPrompt = PromptFormat.getLlavaFirstTurnUserPrompt()
.replace(PromptFormat.USER_PLACEHOLDER, rawPrompt);
} else {
finalPrompt =
(shouldAddSystemPrompt ? mCurrentSettingsFields.getFormattedSystemPrompt() : "")
+ mCurrentSettingsFields.getFormattedUserPrompt(rawPrompt, mThinkMode);
}
shouldAddSystemPrompt = false;
// We store raw prompt into message adapter, because we don't want to show the extra
// tokens from system prompt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static String getUserPromptTemplate(ModelType modelType) {
+ "<|im_start|>assistant\n"
+ THINKING_MODE_PLACEHOLDER;
case LLAVA_1_5:
return "User: " + USER_PLACEHOLDER;
return " USER: " + USER_PLACEHOLDER + " ASSISTANT:";
default:
return USER_PLACEHOLDER;
}
Expand Down Expand Up @@ -101,6 +101,9 @@ public static String getLlavaPresetPrompt() {
+ " gives helpful, detailed, and polite answers to the human's questions. USER: ";
}

public static String getLlavaFirstTurnUserPrompt() {
return USER_PLACEHOLDER + " ASSISTANT:";
}
public static String getFormattedLlamaGuardPrompt(String userPrompt) {
return getUserPromptTemplate(ModelType.LLAMA_GUARD_3)
.replace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,51 @@ private void setupSettings() {
view -> {
setupBackendSelectorDialog();
});
requireViewById(R.id.backendLayout)
.setOnClickListener(
view -> {
setupBackendSelectorDialog();
});

modelImageButton.setOnClickListener(
view -> {
setupModelSelectorDialog();
});
requireViewById(R.id.modelLayout)
.setOnClickListener(
view -> {
setupModelSelectorDialog();
});

tokenizerImageButton.setOnClickListener(
view -> {
setupTokenizerSelectorDialog();
});
requireViewById(R.id.tokenizerLayout)
.setOnClickListener(
view -> {
setupTokenizerSelectorDialog();
});

dataPathImageButton.setOnClickListener(
view -> {
setupDataPathSelectorDialog();
});
requireViewById(R.id.dataPathLayout)
.setOnClickListener(
view -> {
setupDataPathSelectorDialog();
});

modelTypeImageButton.setOnClickListener(
view -> {
setupModelTypeSelectorDialog();
});
requireViewById(R.id.modelTypeLayout)
.setOnClickListener(
view -> {
setupModelTypeSelectorDialog();
});
mModelFilePath = mSettingsFields.getModelFilePath();
if (!mModelFilePath.isEmpty()) {
mModelTextView.setText(getFilenameFromPath(mModelFilePath));
Expand Down Expand Up @@ -357,14 +386,13 @@ private void setupModelSelectorDialog() {
}

private void setupDataPathSelectorDialog() {
String[] dataPathFiles = listLocalFile("/data/local/tmp/llama/", new String[] {".ptd"});
String[] dataPathFiles =
listLocalFile("/data/local/tmp/llama/", new String[] {".ptd"});
AlertDialog.Builder dataPathBuilder = new AlertDialog.Builder(this);
dataPathBuilder.setTitle("Select data path");

String[] dataPathOptions = new String[dataPathFiles.length + 1];
for (int i = 0; i < dataPathFiles.length; i++) {
dataPathOptions[i] = dataPathFiles[i];
}
System.arraycopy(dataPathFiles, 0, dataPathOptions, 0, dataPathFiles.length);
dataPathOptions[dataPathOptions.length - 1] = "(unused)";

dataPathBuilder.setSingleChoiceItems(
Expand Down Expand Up @@ -393,7 +421,7 @@ static String[] listLocalFile(String path, String[] suffix) {
File directory = new File(path);
if (directory.exists() && directory.isDirectory()) {
File[] files = directory.listFiles((dir, name) -> (fileHasExtension(name, suffix)));
String[] result = new String[files.length];
String[] result = new String[files.length];
for (int i = 0; i < files.length; i++) {
if (files[i].isFile() && fileHasExtension(files[i].getName(), suffix)) {
result[i] = files[i].getAbsolutePath();
Expand Down
63 changes: 43 additions & 20 deletions llm/android/LlamaDemo/app/src/main/res/layout/activity_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,40 @@
android:layout_height="match_parent"
tools:context=".SettingsActivity">

<LinearLayout
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#16293D"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="1dp">

<TextView
android:id="@+id/textView"
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Settings"
android:textAlignment="viewStart"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:translationX="5dp"
android:translationY="5dp" />
android:background="#16293D"
android:orientation="vertical">

<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Settings"
android:textAlignment="viewStart"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:translationX="5dp"
android:translationY="5dp" />

<LinearLayout
android:id="@+id/backendLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:orientation="horizontal">
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:padding="10dp">

<TextView
android:id="@+id/backendLabel"
Expand Down Expand Up @@ -71,7 +79,11 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:padding="10dp">

<TextView
android:id="@+id/modelLabel"
Expand Down Expand Up @@ -110,7 +122,11 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:padding="10dp">

<TextView
android:id="@+id/tokenizerLabel"
Expand Down Expand Up @@ -148,7 +164,11 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:padding="10dp">

<TextView
android:id="@+id/dataPathLabel"
Expand Down Expand Up @@ -186,7 +206,11 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:orientation="horizontal"
android:padding="10dp">

<TextView
android:id="@+id/modelTypeLabel"
Expand Down Expand Up @@ -370,7 +394,6 @@
android:textColor="@android:color/white"
android:theme="@style/DefaultButton" />

</LinearLayout>


</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>