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