Skip to content

Commit c56dd53

Browse files
committed
MTK Android app changes
1 parent a167d66 commit c56dd53

File tree

4 files changed

+134
-2
lines changed

4 files changed

+134
-2
lines changed

examples/demo-apps/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/MainActivity.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ protected void onResume() {
285285
}
286286
boolean isUpdated = !mCurrentSettingsFields.equals(updatedSettingsFields);
287287
boolean isLoadModel = updatedSettingsFields.getIsLoadModel();
288+
setBackendMode(updatedSettingsFields.getBackendType());
288289
if (isUpdated) {
289290
if (isLoadModel) {
290291
// If users change the model file, but not pressing loadModelButton, we won't load the new
@@ -293,6 +294,7 @@ protected void onResume() {
293294
} else {
294295
askUserToSelectModel();
295296
}
297+
296298
checkForClearChatHistory(updatedSettingsFields);
297299
// Update current to point to the latest
298300
mCurrentSettingsFields = new SettingsFields(updatedSettingsFields);
@@ -302,6 +304,22 @@ protected void onResume() {
302304
}
303305
}
304306

307+
private void setBackendMode(BackendType backendType) {
308+
if(backendType.equals(BackendType.XNNPACK)) {
309+
setXNNPACKMode();
310+
} else if(backendType.equals(BackendType.MEDIATEK)) {
311+
setMediaTekMode();
312+
}
313+
}
314+
315+
private void setXNNPACKMode() {
316+
requireViewById(R.id.addMediaButton).setVisibility(View.VISIBLE);
317+
}
318+
319+
private void setMediaTekMode() {
320+
requireViewById(R.id.addMediaButton).setVisibility(View.GONE);
321+
}
322+
305323
private void checkForClearChatHistory(SettingsFields updatedSettingsFields) {
306324
if (updatedSettingsFields.getIsClearChatHistory()) {
307325
mMessageAdapter.clear();

examples/demo-apps/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/SettingsActivity.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414
import android.os.Bundle;
1515
import android.text.Editable;
1616
import android.text.TextWatcher;
17+
import android.view.View;
1718
import android.widget.Button;
1819
import android.widget.EditText;
1920
import android.widget.ImageButton;
2021
import android.widget.TextView;
2122
import androidx.appcompat.app.AppCompatActivity;
23+
import androidx.compose.foundation.BackgroundKt;
2224
import androidx.core.content.ContextCompat;
2325
import androidx.core.graphics.Insets;
2426
import androidx.core.view.ViewCompat;
@@ -32,6 +34,7 @@ public class SettingsActivity extends AppCompatActivity {
3234

3335
private String mModelFilePath = "";
3436
private String mTokenizerFilePath = "";
37+
private TextView mBackendTextView;
3538
private TextView mModelTextView;
3639
private TextView mTokenizerTextView;
3740
private TextView mModelTypeTextView;
@@ -41,6 +44,7 @@ public class SettingsActivity extends AppCompatActivity {
4144
private double mSetTemperature;
4245
private String mSystemPrompt;
4346
private String mUserPrompt;
47+
private BackendType mBackendType;
4448
private ModelType mModelType;
4549
public SettingsFields mSettingsFields;
4650

@@ -68,9 +72,11 @@ protected void onCreate(Bundle savedInstanceState) {
6872
}
6973

7074
private void setupSettings() {
75+
mBackendTextView = requireViewById(R.id.backendTextView);
7176
mModelTextView = requireViewById(R.id.modelTextView);
7277
mTokenizerTextView = requireViewById(R.id.tokenizerTextView);
7378
mModelTypeTextView = requireViewById(R.id.modelTypeTextView);
79+
ImageButton backendImageButton = requireViewById(R.id.backendImageButton);
7480
ImageButton modelImageButton = requireViewById(R.id.modelImageButton);
7581
ImageButton tokenizerImageButton = requireViewById(R.id.tokenizerImageButton);
7682
ImageButton modelTypeImageButton = requireViewById(R.id.modelTypeImageButton);
@@ -79,6 +85,10 @@ private void setupSettings() {
7985
loadSettings();
8086

8187
// TODO: The two setOnClickListeners will be removed after file path issue is resolved
88+
backendImageButton.setOnClickListener(
89+
view -> {
90+
setupBackendSelectorDialog();
91+
});
8292
modelImageButton.setOnClickListener(
8393
view -> {
8494
setupModelSelectorDialog();
@@ -104,6 +114,12 @@ private void setupSettings() {
104114
if (mModelType != null) {
105115
mModelTypeTextView.setText(mModelType.toString());
106116
}
117+
mBackendType = mSettingsFields.getBackendType();
118+
ETLogging.getInstance().log("mBackendType from settings " + mBackendType);
119+
if (mBackendType != null) {
120+
mBackendTextView.setText(mBackendType.toString());
121+
setBackendSettingMode();
122+
}
107123

108124
setupParameterSettings();
109125
setupPromptSettings();
@@ -285,6 +301,29 @@ private void showInvalidPromptDialog() {
285301
.show();
286302
}
287303

304+
private void setupBackendSelectorDialog() {
305+
// Convert enum to list
306+
List<String> backendTypesList = new ArrayList<>();
307+
for (BackendType backendType : BackendType.values()) {
308+
backendTypesList.add(backendType.toString());
309+
}
310+
// Alert dialog builder takes in arr of string instead of list
311+
String[] backendTypes = backendTypesList.toArray(new String[0]);
312+
AlertDialog.Builder backendTypeBuilder = new AlertDialog.Builder(this);
313+
backendTypeBuilder.setTitle("Select backend type");
314+
backendTypeBuilder.setSingleChoiceItems(
315+
backendTypes,
316+
-1,
317+
(dialog, item) -> {
318+
mBackendTextView.setText(backendTypes[item]);
319+
mBackendType = BackendType.valueOf(backendTypes[item]);
320+
setBackendSettingMode();
321+
dialog.dismiss();
322+
});
323+
324+
backendTypeBuilder.create().show();
325+
}
326+
288327
private void setupModelSelectorDialog() {
289328
String[] pteFiles = listLocalFile("/data/local/tmp/llama/", ".pte");
290329
AlertDialog.Builder modelPathBuilder = new AlertDialog.Builder(this);
@@ -370,6 +409,28 @@ private String getFilenameFromPath(String uriFilePath) {
370409
return "";
371410
}
372411

412+
private void setBackendSettingMode() {
413+
if(mBackendType.equals(BackendType.XNNPACK)) {
414+
setXNNPACKSettingMode();
415+
} else if(mBackendType.equals(BackendType.MEDIATEK)) {
416+
setMediaTekSettingMode();
417+
}
418+
}
419+
420+
private void setXNNPACKSettingMode() {
421+
requireViewById(R.id.modelLayout).setVisibility(View.VISIBLE);
422+
requireViewById(R.id.tokenizerLayout).setVisibility(View.VISIBLE);
423+
requireViewById(R.id.parametersView).setVisibility(View.VISIBLE);
424+
requireViewById(R.id.temperatureLayout).setVisibility(View.VISIBLE);
425+
}
426+
427+
private void setMediaTekSettingMode() {
428+
requireViewById(R.id.modelLayout).setVisibility(View.GONE);
429+
requireViewById(R.id.tokenizerLayout).setVisibility(View.GONE);
430+
requireViewById(R.id.parametersView).setVisibility(View.GONE);
431+
requireViewById(R.id.temperatureLayout).setVisibility(View.GONE);
432+
}
433+
373434
private void loadSettings() {
374435
Gson gson = new Gson();
375436
String settingsFieldsJSON = mDemoSharedPreferences.getSettings();
@@ -384,6 +445,7 @@ private void saveSettings() {
384445
mSettingsFields.saveParameters(mSetTemperature);
385446
mSettingsFields.savePrompts(mSystemPrompt, mUserPrompt);
386447
mSettingsFields.saveModelType(mModelType);
448+
mSettingsFields.saveBackendType(mBackendType);
387449
mDemoSharedPreferences.addSettings(mSettingsFields);
388450
}
389451

examples/demo-apps/android/LlamaDemo/app/src/main/java/com/example/executorchllamademo/SettingsFields.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public ModelType getModelType() {
3030
return modelType;
3131
}
3232

33+
public BackendType getBackendType(){ return backendType; }
34+
3335
public String getUserPrompt() {
3436
return userPrompt;
3537
}
@@ -63,9 +65,11 @@ public boolean getIsLoadModel() {
6365
private boolean isClearChatHistory;
6466
private boolean isLoadModel;
6567
private ModelType modelType;
68+
private BackendType backendType;
6669

6770
public SettingsFields() {
6871
ModelType DEFAULT_MODEL = ModelType.LLAMA_3;
72+
BackendType DEFAULT_BACKEND= BackendType.XNNPACK;
6973

7074
modelFilePath = "";
7175
tokenizerFilePath = "";
@@ -75,6 +79,7 @@ public SettingsFields() {
7579
isClearChatHistory = false;
7680
isLoadModel = false;
7781
modelType = DEFAULT_MODEL;
82+
backendType = DEFAULT_BACKEND;
7883
}
7984

8085
public SettingsFields(SettingsFields settingsFields) {
@@ -86,6 +91,7 @@ public SettingsFields(SettingsFields settingsFields) {
8691
this.isClearChatHistory = settingsFields.getIsClearChatHistory();
8792
this.isLoadModel = settingsFields.getIsLoadModel();
8893
this.modelType = settingsFields.modelType;
94+
this.backendType = settingsFields.backendType;
8995
}
9096

9197
public void saveModelPath(String modelFilePath) {
@@ -100,6 +106,8 @@ public void saveModelType(ModelType modelType) {
100106
this.modelType = modelType;
101107
}
102108

109+
public void saveBackendType(BackendType backendType) { this.backendType = backendType;}
110+
103111
public void saveParameters(Double temperature) {
104112
this.temperature = temperature;
105113
}
@@ -126,6 +134,7 @@ public boolean equals(SettingsFields anotherSettingsFields) {
126134
&& userPrompt.equals(anotherSettingsFields.userPrompt)
127135
&& isClearChatHistory == anotherSettingsFields.isClearChatHistory
128136
&& isLoadModel == anotherSettingsFields.isLoadModel
129-
&& modelType == anotherSettingsFields.modelType;
137+
&& modelType == anotherSettingsFields.modelType
138+
&& backendType == anotherSettingsFields.backendType;
130139
}
131140
}

examples/demo-apps/android/LlamaDemo/app/src/main/res/layout/activity_settings.xml

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,51 @@
2828
android:translationY="5dp" />
2929

3030
<LinearLayout
31+
android:id="@+id/backendLayout"
3132
android:layout_width="match_parent"
3233
android:layout_height="wrap_content"
3334
android:layout_marginTop="40dp"
3435
android:orientation="horizontal">
3536

37+
<TextView
38+
android:id="@+id/backendLabel"
39+
android:layout_width="wrap_content"
40+
android:layout_height="wrap_content"
41+
android:gravity="center_vertical"
42+
android:text="Backend"
43+
android:textColor="#FFFFFF"
44+
android:textSize="16sp"
45+
android:translationX="5dp" />
46+
47+
<TextView
48+
android:id="@+id/backendTextView"
49+
android:layout_width="0dp"
50+
android:layout_height="match_parent"
51+
android:layout_weight="1"
52+
android:gravity="center_vertical|end"
53+
android:text="no backend selected"
54+
android:textColor="#FFFFFF" />
55+
56+
<ImageButton
57+
android:id="@+id/backendImageButton"
58+
android:layout_width="wrap_content"
59+
android:layout_height="wrap_content"
60+
android:layout_marginStart="5dp"
61+
android:background="#00FFFFFF"
62+
android:scaleType="center"
63+
android:scaleX="0.7"
64+
android:scaleY="0.7"
65+
android:src="@drawable/expand_circle_down" />
66+
67+
</LinearLayout>
68+
69+
<LinearLayout
70+
android:id="@+id/modelLayout"
71+
android:layout_width="match_parent"
72+
android:layout_height="wrap_content"
73+
android:layout_marginTop="20dp"
74+
android:orientation="horizontal">
75+
3676
<TextView
3777
android:id="@+id/modelLabel"
3878
android:layout_width="wrap_content"
@@ -66,6 +106,7 @@
66106
</LinearLayout>
67107

68108
<LinearLayout
109+
android:id="@+id/tokenizerLayout"
69110
android:layout_width="match_parent"
70111
android:layout_height="wrap_content"
71112
android:layout_marginTop="20dp"
@@ -103,6 +144,7 @@
103144
</LinearLayout>
104145

105146
<LinearLayout
147+
android:id="@+id/modelTypeLayout"
106148
android:layout_width="match_parent"
107149
android:layout_height="wrap_content"
108150
android:layout_marginTop="20dp"
@@ -152,7 +194,7 @@
152194
android:theme="@style/DefaultButton" />
153195

154196
<TextView
155-
android:id="@+id/textView4"
197+
android:id="@+id/parametersView"
156198
android:layout_width="match_parent"
157199
android:layout_height="wrap_content"
158200
android:layout_marginTop="20dp"
@@ -164,6 +206,7 @@
164206
android:translationX="5dp" />
165207

166208
<LinearLayout
209+
android:id="@+id/temperatureLayout"
167210
android:layout_width="match_parent"
168211
android:layout_height="wrap_content"
169212
android:layout_marginBottom="10dp"

0 commit comments

Comments
 (0)