1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.nn.benchmark.app; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.os.Bundle; 22 import android.os.Parcelable; 23 import android.text.method.ScrollingMovementMethod; 24 import android.util.Log; 25 import android.view.Menu; 26 import android.view.MenuInflater; 27 import android.view.MenuItem; 28 import android.view.View; 29 import android.widget.ArrayAdapter; 30 import android.widget.ListView; 31 import android.widget.TextView; 32 33 import com.android.nn.benchmark.core.BenchmarkResult; 34 import com.android.nn.benchmark.core.TestModels; 35 import com.android.nn.benchmark.util.TestExternalStorageActivity; 36 37 import java.util.ArrayList; 38 import java.util.Arrays; 39 import java.util.Random; 40 41 public class NNControls extends Activity { 42 private static final String TAG = NNControls.class.getSimpleName(); 43 44 private ListView mTestListView; 45 private TextView mResultView; 46 47 private ArrayAdapter<String> mTestListAdapter; 48 private ArrayList<String> mTestList = new ArrayList<String>(); 49 50 private boolean mSettings[] = {false, false, false}; 51 private static final int SETTING_LONG_RUN = 0; 52 private static final int SETTING_PAUSE = 1; 53 private static final int SETTING_DISABLE_NNAPI = 2; 54 55 private float mResults[]; 56 private String mInfo[]; 57 58 private static int DOGFOOD_MODELS_PER_RUN = 20; 59 60 @Override onCreateOptionsMenu(Menu menu)61 public boolean onCreateOptionsMenu(Menu menu) { 62 // Inflate the menu items for use in the action bar 63 MenuInflater inflater = getMenuInflater(); 64 inflater.inflate(R.menu.main_activity_actions, menu); 65 66 return super.onCreateOptionsMenu(menu); 67 } 68 init()69 void init() { 70 TestExternalStorageActivity.testWriteExternalStorage(this, true); 71 72 for (TestModels.TestModelEntry testModel : TestModels.modelsList()) { 73 mTestList.add(testModel.toString()); 74 } 75 76 mTestListView = findViewById(R.id.test_list); 77 mTestListAdapter = new ArrayAdapter(this, 78 android.R.layout.simple_list_item_activated_1, 79 mTestList); 80 81 mTestListView.setAdapter(mTestListAdapter); 82 mTestListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 83 mTestListAdapter.notifyDataSetChanged(); 84 85 mResultView = findViewById(R.id.results); 86 mResultView.setMovementMethod(new ScrollingMovementMethod()); 87 } 88 89 @Override onCreate(Bundle savedInstanceState)90 protected void onCreate(Bundle savedInstanceState) { 91 super.onCreate(savedInstanceState); 92 setContentView(R.layout.controls); 93 init(); 94 } 95 96 @Override onPause()97 protected void onPause() { 98 super.onPause(); 99 } 100 101 @Override onResume()102 protected void onResume() { 103 super.onResume(); 104 } 105 makeBasicLaunchIntent()106 Intent makeBasicLaunchIntent() { 107 Intent intent = new Intent(this, NNBenchmark.class); 108 intent.putExtra(NNBenchmark.EXTRA_ENABLE_LONG, mSettings[SETTING_LONG_RUN]); 109 intent.putExtra(NNBenchmark.EXTRA_ENABLE_PAUSE, mSettings[SETTING_PAUSE]); 110 intent.putExtra(NNBenchmark.EXTRA_DISABLE_NNAPI, mSettings[SETTING_DISABLE_NNAPI]); 111 intent.putExtra(NNBenchmark.EXTRA_MAX_ITERATIONS, 1); 112 return intent; 113 } 114 btnRun(View v)115 public void btnRun(View v) { 116 int count = 0; 117 int modelsCount = TestModels.modelsList().size(); 118 for (int i = 0; i < modelsCount; i++) { 119 if (mTestListView.isItemChecked(i)) { 120 count++; 121 } 122 } 123 if (count == 0) { 124 return; 125 } 126 127 int testList[] = new int[count]; 128 count = 0; 129 for (int i = 0; i < modelsCount; i++) { 130 if (mTestListView.isItemChecked(i)) { 131 testList[count++] = i; 132 } 133 } 134 135 Intent intent = makeBasicLaunchIntent(); 136 intent.putExtra(NNBenchmark.EXTRA_TESTS, testList); 137 startActivityForResult(intent, 0); 138 } 139 getResultShortSummary(BenchmarkResult br, TestModels.TestModelEntry t)140 String getResultShortSummary(BenchmarkResult br, TestModels.TestModelEntry t) { 141 return br.getSummary(t.mBaselineSec); 142 } 143 onActivityResult(int requestCode, int resultCode, Intent data)144 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 145 if (requestCode == 0) { 146 if (resultCode == RESULT_OK) { 147 int size = TestModels.modelsList().size(); 148 mResults = new float[size]; 149 mInfo = new String[size]; 150 151 Parcelable r[] = data.getParcelableArrayExtra(NNBenchmark.EXTRA_RESULTS_RESULTS); 152 int id[] = data.getIntArrayExtra(NNBenchmark.EXTRA_RESULTS_TESTS); 153 154 String mOutResult = ""; 155 for (int ct = 0; ct < id.length; ct++) { 156 TestModels.TestModelEntry t = TestModels.modelsList().get(id[ct]); 157 BenchmarkResult br = (BenchmarkResult) r[ct]; 158 159 String s = t.toString() + " " + getResultShortSummary(br, t); 160 mTestList.set(id[ct], s); 161 mTestListAdapter.notifyDataSetChanged(); 162 mOutResult += s + '\n'; 163 mResults[id[ct]] = ((BenchmarkResult) r[ct]).getMeanTimeSec(); 164 } 165 166 mResultView.setText(mOutResult); 167 } 168 } 169 } 170 btnSelAll(View v)171 public void btnSelAll(View v) { 172 for (int i = 0; i < TestModels.modelsList().size(); i++) { 173 mTestListView.setItemChecked(i, true); 174 } 175 } 176 btnSelNone(View v)177 public void btnSelNone(View v) { 178 for (int i = 0; i < TestModels.modelsList().size(); i++) { 179 mTestListView.setItemChecked(i, false); 180 } 181 } 182 onOptionsItemSelected(MenuItem item)183 public boolean onOptionsItemSelected(MenuItem item) { 184 // Handle presses on the action bar items 185 switch (item.getItemId()) { 186 case R.id.action_settings: 187 NNSettings newFragment = new NNSettings(mSettings); 188 newFragment.show(getFragmentManager(), "settings"); 189 return true; 190 default: 191 return super.onOptionsItemSelected(item); 192 } 193 } 194 btnSettings(View v)195 public void btnSettings(View v) { 196 NNSettings newFragment = new NNSettings(mSettings); 197 newFragment.show(getFragmentManager(), "settings"); 198 } 199 btnRunDogfood(View v)200 public void btnRunDogfood(View v) { 201 // Update settings for dogfood. 202 mSettings[SETTING_LONG_RUN] = true; 203 mSettings[SETTING_PAUSE] = false; 204 mSettings[SETTING_DISABLE_NNAPI] = false; 205 206 // Select dogfood models. 207 long seed = System.currentTimeMillis(); 208 Log.v(NNBenchmark.TAG, "Dogfood run seed " + seed); 209 Random random = new Random(seed); 210 int numModelsToSelect = Math.min(DOGFOOD_MODELS_PER_RUN, mTestList.size()); 211 for (int i = 0; i < mTestList.size(); i++) { 212 mTestListView.setItemChecked(i, false); 213 } 214 while (numModelsToSelect > 0) { 215 int i = random.nextInt(mTestList.size()); 216 if (mTestListView.isItemChecked(i)) { 217 continue; 218 } 219 mTestListView.setItemChecked(i, true); 220 numModelsToSelect--; 221 } 222 223 // Run benchmark. 224 btnRun(v); 225 } 226 } 227