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 #define LOG_TAG "NeuralNetworksTest"
18
19 #include "LogTestCaseToLogcat.h"
20 #include "TestNeuralNetworksWrapper.h"
21
22 #ifndef NNTEST_ONLY_PUBLIC_API
23 #include "Manager.h"
24 #include "Utils.h"
25 #endif
26
27 #include <android-base/logging.h>
28 #include <gtest/gtest.h>
29
30 #include <cctype>
31 #include <iostream>
32 #include <sstream>
33 #include <string>
34
35 namespace {
36
37 using namespace android::nn::test_wrapper;
38
39 // We run through the test suite several times, by invoking test() several
40 // times. Each run is a "pass".
41
42 // Bitmask of passes we're allowed to run.
43 static uint64_t allowedPasses = ~uint64_t(0);
44
45 // DeviceManager::setUseCpuOnly() and Execution::setComputeUsesSynchronousAPI()
46 // according to arguments, and return RUN_ALL_TESTS(). It is unspecified what
47 // values those settings have when this function returns.
48 //
49 // EXCEPTION: If NNTEST_ONLY_PUBLIC_API is defined, then we cannot call
50 // non-public DeviceManager::setUseCpuOnly(); we assume the setting is always
51 // false, and if we are asked to set it to true, we return 0 ("success") without
52 // running tests.
53 //
54 // EXCEPTION: If NNTEST_ONLY_PUBLIC_API is defined, then we cannot call
55 // non-public DeviceManager::setSyncExecHal(); we assume the setting is always
56 // true, and if we are asked to set it to false, we return 0 ("success") without
57 // running tests.
test(bool useCpuOnly,Execution::ComputeMode computeMode,bool allowSyncExecHal=true)58 static int test(bool useCpuOnly, Execution::ComputeMode computeMode, bool allowSyncExecHal = true) {
59 // NOTE: The test mapping configuration (frameworks/ml/nn/TEST_MAPPING) uses
60 // the value of 1024 to only run pass 10 of the test, corresponding to
61 // "useCpuOnly = 0, computeMode = ComputeMode::ASYNC, allowSyncExecHal = 1".
62 // If you change the bit representation here, also make the corresponding
63 // change to the TEST_MAPPING file to run the equivalent pass of the test.
64 uint32_t passIndex =
65 (useCpuOnly << 0) + (static_cast<uint32_t>(computeMode) << 1) + (allowSyncExecHal << 3);
66
67 #ifdef NNTEST_ONLY_PUBLIC_API
68 if (useCpuOnly || !allowSyncExecHal) {
69 return 0;
70 }
71 #else
72 android::nn::DeviceManager::get()->setUseCpuOnly(useCpuOnly);
73 android::nn::DeviceManager::get()->setSyncExecHal(allowSyncExecHal);
74 #endif
75
76 Execution::setComputeMode(computeMode);
77
78 auto computeModeText = [computeMode] {
79 switch (computeMode) {
80 case Execution::ComputeMode::SYNC:
81 return "ComputeMode::SYNC";
82 case Execution::ComputeMode::ASYNC:
83 return "ComputeMode::ASYNC";
84 case Execution::ComputeMode::BURST:
85 return "ComputeMode::BURST";
86 case Execution::ComputeMode::FENCED:
87 return "ComputeMode::FENCED";
88 }
89 return "<unknown ComputeMode>";
90 };
91
92 std::stringstream stream;
93 stream << "useCpuOnly = " << useCpuOnly << ", computeMode = " << computeModeText()
94 << ", allowSyncExecHal = " << allowSyncExecHal << " // pass " << passIndex;
95 const std::string message = stream.str();
96 LOG(INFO) << message;
97 std::cout << "[**********] " << message << std::endl;
98 SCOPED_TRACE(message);
99
100 if (!((uint64_t(1) << passIndex) & allowedPasses)) {
101 LOG(INFO) << "SKIPPED PASS";
102 std::cout << "SKIPPED PASS" << std::endl;
103 return 0;
104 }
105
106 return RUN_ALL_TESTS();
107 }
108
checkArgs(int argc,char ** argv,int nextArg)109 void checkArgs(int argc, char** argv, int nextArg) {
110 if (nextArg != argc) {
111 std::cerr << "Unexpected argument: " << argv[nextArg] << std::endl;
112 exit(1);
113 }
114 }
115
116 } // namespace
117
main(int argc,char ** argv)118 int main(int argc, char** argv) {
119 testing::InitGoogleTest(&argc, argv);
120 testing::UnitTest::GetInstance()->listeners().Append(new android::nn::LogTestCaseToLogcat());
121
122 if ((argc > 1) && std::isdigit(argv[1][0])) {
123 allowedPasses = std::stoull(argv[1]);
124 checkArgs(argc, argv, 2);
125 } else {
126 checkArgs(argc, argv, 1);
127 }
128
129 #ifndef NNTEST_ONLY_PUBLIC_API
130 android::nn::initVLogMask();
131 #endif
132
133 int n = test(/*useCpuOnly=*/false, Execution::ComputeMode::ASYNC) |
134 test(/*useCpuOnly=*/false, Execution::ComputeMode::SYNC) |
135 test(/*useCpuOnly=*/true, Execution::ComputeMode::ASYNC) |
136 test(/*useCpuOnly=*/true, Execution::ComputeMode::SYNC);
137
138 // Now try disabling use of synchronous execution HAL.
139 //
140 // Whether or not the use of synchronous execution HAL is enabled should make no
141 // difference when useCpuOnly = true; we already ran test(true, *, true) above,
142 // so there's no reason to run test(true, *, false) now.
143 n |= test(/*useCpuOnly=*/false, Execution::ComputeMode::ASYNC, /*allowSyncExecHal=*/false) |
144 test(/*useCpuOnly=*/false, Execution::ComputeMode::SYNC, /*allowSyncExecHal=*/false);
145
146 // Now try execution using a burst.
147 //
148 // The burst path is off by default in these tests. This is the first case
149 // where it is turned on. Both "useCpuOnly" and "allowSyncExecHal" are
150 // irrelevant here because the burst path is separate from both.
151 n |= test(/*useCpuOnly=*/false, Execution::ComputeMode::BURST);
152
153 return n;
154 }
155