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 #include <CanonicalDevice.h>
18 #include <TestHarness.h>
19 #include <android-base/logging.h>
20 #include <nnapi/IDevice.h>
21 #include <nnapi/IPreparedModel.h>
22 #include <nnapi/Result.h>
23 #include <nnapi/TestUtils.h>
24 #include <nnapi/TypeUtils.h>
25 #include <nnapi/Types.h>
26
27 #include <memory>
28
29 namespace android::nn {
30 namespace {
31
getDevice()32 SharedDevice getDevice() {
33 /**
34 * TODO: INSERT CUSTOM DEVICE HERE
35 *
36 * This code can test a canonical IDevice directly, a HIDL IDevice by using the corresponding
37 * wrapper in neuralnetworks_utils_hal_1_*, or an AIDL IDevice by using the corresponding
38 * wrapper in neuralnetworks_utils_hal_aidl. E.g.:
39 * HIDL 1.0 -- ::android::hardware::neuralnetworks::V1_0::utils::Device::create
40 * HIDL 1.1 -- ::android::hardware::neuralnetworks::V1_1::utils::Device::create
41 * HIDL 1.2 -- ::android::hardware::neuralnetworks::V1_2::utils::Device::create
42 * HIDL 1.3 -- ::android::hardware::neuralnetworks::V1_3::utils::Device::create
43 * AIDL -- ::aidl::android::hardware::neuralnetworks::utils::Device::create
44 */
45 static const SharedDevice device = std::make_shared<const sample::Device>("example-driver");
46 return device;
47 }
48
runTest(const::test_helper::TestModel & testModel)49 ExecutionResult<void> runTest(const ::test_helper::TestModel& testModel) {
50 // Set up device.
51 const auto device = getDevice();
52 CHECK(device != nullptr);
53
54 // Set up model.
55 const auto model = NN_TRY(test::createModel(testModel));
56
57 // Attempt to prepare the model.
58 const auto preparedModel = NN_TRY(device->prepareModel(
59 model, ExecutionPreference::DEFAULT, Priority::DEFAULT,
60 /*deadline=*/{}, /*modelCache=*/{},
61 /*dataCache=*/{}, /*token=*/{}, /*hints=*/{}, /*extensionPrefix*/ {}));
62 CHECK(preparedModel != nullptr);
63
64 // Set up request.
65 const auto request = NN_TRY(test::createRequest(testModel));
66
67 // Perform execution.
68 NN_TRY(preparedModel->execute(request, MeasureTiming::YES, /*deadline=*/{},
69 /*loopTimeoutDuration=*/{}, /*hints=*/{},
70 /*extensionPrefix*/ {}));
71
72 return {};
73 }
74
75 } // namespace
76 } // namespace android::nn
77
nnapiFuzzTest(const::test_helper::TestModel & testModel)78 void nnapiFuzzTest(const ::test_helper::TestModel& testModel) {
79 android::nn::runTest(testModel);
80 }
81