• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.crashtest.app;
18 
19 import android.content.Intent;
20 import android.os.RemoteException;
21 import android.test.ActivityInstrumentationTestCase2;
22 import android.test.UiThreadTest;
23 
24 import androidx.test.InstrumentationRegistry;
25 import androidx.test.filters.LargeTest;
26 
27 import com.android.nn.benchmark.app.AcceleratorSpecificTestSupport;
28 import com.android.nn.benchmark.core.NnApiDelegationFailure;
29 import com.android.nn.benchmark.core.TestModels;
30 
31 import junit.framework.Assert;
32 
33 import org.junit.Before;
34 import org.junit.Rule;
35 import org.junit.Test;
36 import org.junit.rules.TestName;
37 import org.junit.runner.RunWith;
38 import org.junit.runners.Parameterized;
39 import org.junit.runners.Parameterized.Parameters;
40 
41 import java.time.Duration;
42 import java.util.Optional;
43 import java.util.concurrent.ExecutionException;
44 import java.util.concurrent.ExecutorService;
45 import java.util.concurrent.Executors;
46 import java.util.concurrent.Future;
47 import java.util.stream.IntStream;
48 
49 @RunWith(Parameterized.class)
50 public class NNMemoryMappedModelCompilationTest extends
51         ActivityInstrumentationTestCase2<NNParallelTestActivity> implements
52     AcceleratorSpecificTestSupport {
53 
54     private static final String TAG = "NNMemoryMappedModelCompilation";
55     private static final Duration MAX_SEPARATE_PROCESS_EXECUTION_TIME = Duration.ofSeconds(70);
56     public static final int NNAPI_CLIENTS_COUNT = 4;
57 
58     private final ExecutorService mDriverLivenessValidationExecutor =
59             Executors.newSingleThreadExecutor();
60     private final String mAcceleratorName;
61 
62     @Rule
63     public TestName mTestName = new TestName();
64 
NNMemoryMappedModelCompilationTest(String acceleratorName)65     public NNMemoryMappedModelCompilationTest(String acceleratorName) {
66         super(NNParallelTestActivity.class);
67         mAcceleratorName = acceleratorName;
68     }
69 
70     @Parameters(name = "Accelerator({0})")
targetAccelerators()71     public static Iterable<String> targetAccelerators() {
72         return AcceleratorSpecificTestSupport.getTargetAcceleratorNames();
73     }
74 
75     @Before
76     @Override
setUp()77     public void setUp() {
78         injectInstrumentation(InstrumentationRegistry.getInstrumentation());
79         final Intent runSomeInferencesInASeparateProcess;
80         try {
81             runSomeInferencesInASeparateProcess = compileSupportedModelsMemoryMappedOnNThreadsFor(
82                     NNAPI_CLIENTS_COUNT,
83                     MAX_SEPARATE_PROCESS_EXECUTION_TIME);
84         } catch (NnApiDelegationFailure nnApiDelegationFailure) {
85             throw new RuntimeException(
86                     "Cannot initialize test, failure looking for supported models, please check "
87                             + "the driver status",
88                     nnApiDelegationFailure);
89         }
90         setActivityIntent(runSomeInferencesInASeparateProcess);
91     }
92 
93     @Test
94     @LargeTest
95     @UiThreadTest
testDriverDoesNotFailWithParallelThreads()96     public void testDriverDoesNotFailWithParallelThreads()
97             throws ExecutionException, InterruptedException, RemoteException,
98             NnApiDelegationFailure {
99         final NNParallelTestActivity activity = getActivity();
100 
101         Optional<TestModels.TestModelEntry> modelForLivenessTest =
102                 AcceleratorSpecificTestSupport.findTestModelRunningOnAccelerator(activity, mAcceleratorName);
103         assertTrue("No model available to be run on accelerator " + mAcceleratorName,
104                 modelForLivenessTest.isPresent());
105 
106         // This should not be necessary
107         final DriverLivenessChecker driverLivenessChecker = new DriverLivenessChecker(activity,
108                 mAcceleratorName, modelForLivenessTest.get());
109         Future<Boolean> driverDidNotCrash = mDriverLivenessValidationExecutor.submit(
110                 driverLivenessChecker);
111 
112         Assert.assertEquals(CrashTestStatus.TestResult.SUCCESS, activity.testResult());
113 
114         driverLivenessChecker.stop();
115         assertTrue("Driver shouldn't have crashed", driverDidNotCrash.get());
116     }
117 
compileSupportedModelsMemoryMappedOnNThreadsFor(int threadCount, Duration testDuration)118     private Intent compileSupportedModelsMemoryMappedOnNThreadsFor(int threadCount,
119             Duration testDuration) throws NnApiDelegationFailure {
120         Intent intent = new Intent();
121         int modelsCount = TestModels.modelsList().size();
122         intent.putExtra(NNParallelTestActivity.EXTRA_TEST_LIST, IntStream.range(0, modelsCount).toArray());
123         intent.putExtra(NNParallelTestActivity.EXTRA_THREAD_COUNT, threadCount);
124         intent.putExtra(NNParallelTestActivity.EXTRA_TEST_DURATION_MILLIS, testDuration.toMillis());
125         intent.putExtra(NNParallelTestActivity.EXTRA_RUN_IN_SEPARATE_PROCESS, true);
126         intent.putExtra(NNParallelTestActivity.EXTRA_TEST_NAME, mTestName.getMethodName());
127         intent.putExtra(NNParallelTestActivity.EXTRA_ACCELERATOR_NAME, mAcceleratorName);
128         intent.putExtra(NNParallelTestActivity.EXTRA_RUN_MODEL_COMPILATION_ONLY, true);
129         intent.putExtra(NNParallelTestActivity.EXTRA_MEMORY_MAP_MODEL, true);
130         intent.putExtra(NNParallelTestActivity.EXTRA_IGNORE_UNSUPPORTED_MODELS, true);
131         return intent;
132     }
133 }
134