• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         return intent;
112     }
113 
btnRun(View v)114     public void btnRun(View v) {
115         int count = 0;
116         int modelsCount = TestModels.modelsList().size();
117         for (int i = 0; i < modelsCount; i++) {
118             if (mTestListView.isItemChecked(i)) {
119                 count++;
120             }
121         }
122         if (count == 0) {
123             return;
124         }
125 
126         int testList[] = new int[count];
127         count = 0;
128         for (int i = 0; i < modelsCount; i++) {
129             if (mTestListView.isItemChecked(i)) {
130                 testList[count++] = i;
131             }
132         }
133 
134         Intent intent = makeBasicLaunchIntent();
135         intent.putExtra(NNBenchmark.EXTRA_TESTS, testList);
136         startActivityForResult(intent, 0);
137     }
138 
getResultShortSummary(BenchmarkResult br, TestModels.TestModelEntry t)139     String getResultShortSummary(BenchmarkResult br, TestModels.TestModelEntry t) {
140         return br.getSummary(t.mBaselineSec);
141     }
142 
onActivityResult(int requestCode, int resultCode, Intent data)143     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
144         if (requestCode == 0) {
145             if (resultCode == RESULT_OK) {
146                 int size = TestModels.modelsList().size();
147                 mResults = new float[size];
148                 mInfo = new String[size];
149 
150                 Parcelable r[] = data.getParcelableArrayExtra(NNBenchmark.EXTRA_RESULTS_RESULTS);
151                 int id[] = data.getIntArrayExtra(NNBenchmark.EXTRA_RESULTS_TESTS);
152 
153                 String mOutResult = "";
154                 for (int ct = 0; ct < id.length; ct++) {
155                     TestModels.TestModelEntry t = TestModels.modelsList().get(id[ct]);
156                     BenchmarkResult br = (BenchmarkResult) r[ct];
157 
158                     String s = t.toString() + " " + getResultShortSummary(br, t);
159                     mTestList.set(id[ct], s);
160                     mTestListAdapter.notifyDataSetChanged();
161                     mOutResult += s + '\n';
162                     mResults[id[ct]] = ((BenchmarkResult) r[ct]).getMeanTimeSec();
163                 }
164 
165                 mResultView.setText(mOutResult);
166             }
167         }
168     }
169 
btnSelAll(View v)170     public void btnSelAll(View v) {
171         for (int i = 0; i < TestModels.modelsList().size(); i++) {
172             mTestListView.setItemChecked(i, true);
173         }
174     }
175 
btnSelNone(View v)176     public void btnSelNone(View v) {
177         for (int i = 0; i < TestModels.modelsList().size(); i++) {
178             mTestListView.setItemChecked(i, false);
179         }
180     }
181 
onOptionsItemSelected(MenuItem item)182     public boolean onOptionsItemSelected(MenuItem item) {
183         // Handle presses on the action bar items
184         switch (item.getItemId()) {
185             case R.id.action_settings:
186                 NNSettings newFragment = new NNSettings(mSettings);
187                 newFragment.show(getFragmentManager(), "settings");
188                 return true;
189             default:
190                 return super.onOptionsItemSelected(item);
191         }
192     }
193 
btnSettings(View v)194     public void btnSettings(View v) {
195         NNSettings newFragment = new NNSettings(mSettings);
196         newFragment.show(getFragmentManager(), "settings");
197     }
198 
btnRunDogfood(View v)199     public void btnRunDogfood(View v) {
200         // Update settings for dogfood.
201         mSettings[SETTING_LONG_RUN] = true;
202         mSettings[SETTING_PAUSE] = false;
203         mSettings[SETTING_DISABLE_NNAPI] = false;
204 
205         // Select dogfood models.
206         long seed = System.currentTimeMillis();
207         Log.v(NNBenchmark.TAG, "Dogfood run seed " + seed);
208         Random random = new Random(seed);
209         int numModelsToSelect = Math.min(DOGFOOD_MODELS_PER_RUN, mTestList.size());
210         for (int i = 0; i < mTestList.size(); i++) {
211           mTestListView.setItemChecked(i, false);
212         }
213         while (numModelsToSelect > 0) {
214             int i = random.nextInt(mTestList.size());
215             if (mTestListView.isItemChecked(i)) {
216                 continue;
217             }
218             mTestListView.setItemChecked(i, true);
219             numModelsToSelect--;
220         }
221 
222         // Run benchmark.
223         btnRun(v);
224     }
225 }
226