blob: 603054dddf8f21df3ff38cc0949c87f72aa9991c [file] [log] [blame]
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -07001/*
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
Xusong Wang34058782019-01-18 17:28:26 -080017#include "GeneratedTestHarness.h"
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010018#include "1.0/Callbacks.h"
19#include "1.0/Utils.h"
20#include "MemoryUtils.h"
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070021#include "TestHarness.h"
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070022
23#include <android-base/logging.h>
Miao Wanga2d04c82018-02-05 17:26:54 -080024#include <android/hardware/neuralnetworks/1.0/IDevice.h>
Miao Wanga2d04c82018-02-05 17:26:54 -080025#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
Miao Wanga2d04c82018-02-05 17:26:54 -080026#include <android/hardware/neuralnetworks/1.0/types.h>
27#include <android/hidl/allocator/1.0/IAllocator.h>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070028#include <android/hidl/memory/1.0/IMemory.h>
29#include <hidlmemory/mapping.h>
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010030
Michael Butler0897ab32017-10-04 02:38:42 -070031#include <iostream>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070032
33namespace android {
34namespace hardware {
35namespace neuralnetworks {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070036namespace generated_tests {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010037
38using ::android::hardware::neuralnetworks::V1_0::ErrorStatus;
39using ::android::hardware::neuralnetworks::V1_0::IDevice;
40using ::android::hardware::neuralnetworks::V1_0::IPreparedModel;
41using ::android::hardware::neuralnetworks::V1_0::Model;
42using ::android::hardware::neuralnetworks::V1_0::Request;
43using ::android::hardware::neuralnetworks::V1_0::RequestArgument;
44using ::android::hardware::neuralnetworks::V1_0::implementation::ExecutionCallback;
45using ::android::hardware::neuralnetworks::V1_0::implementation::PreparedModelCallback;
46using ::android::hidl::memory::V1_0::IMemory;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010047using ::test_helper::compare;
Mika Raentode166942018-04-17 16:49:50 +010048using ::test_helper::filter;
49using ::test_helper::for_all;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010050using ::test_helper::MixedTyped;
51using ::test_helper::MixedTypedExample;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010052using ::test_helper::resize_accordingly;
Xusong Wangd2933152019-03-12 14:40:32 -070053
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070054// Top level driver for models and examples generated by test_generator.py
55// Test driver for those generated from ml/nn/runtime/test/spec
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010056void EvaluatePreparedModel(sp<IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
57 const std::vector<MixedTypedExample>& examples, float fpAtol,
58 float fpRtol) {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070059 const uint32_t INPUT = 0;
60 const uint32_t OUTPUT = 1;
61
62 int example_no = 1;
63 for (auto& example : examples) {
64 SCOPED_TRACE(example_no++);
Michael K. Sanders941d61a2018-10-19 14:39:09 +010065 const MixedTyped& inputs = example.operands.first;
66 const MixedTyped& golden = example.operands.second;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070067
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010068 CHECK(inputs.float16Operands.empty()) << "float16 is not supported in 1.0";
Michael K. Sandersefa4c812018-10-30 14:44:48 +000069
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070070 std::vector<RequestArgument> inputs_info, outputs_info;
71 uint32_t inputSize = 0, outputSize = 0;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070072 // This function only partially specifies the metadata (vector of RequestArguments).
73 // The contents are copied over below.
74 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
75 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
76 RequestArgument arg = {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010077 .location = {.poolIndex = INPUT,
78 .offset = 0,
79 .length = static_cast<uint32_t>(s)},
80 .dimensions = {},
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070081 };
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -070082 RequestArgument arg_empty = {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010083 .hasNoValue = true,
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -070084 };
85 inputs_info[index] = s ? arg : arg_empty;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070086 inputSize += s;
87 });
88 // Compute offset for inputs 1 and so on
89 {
90 size_t offset = 0;
91 for (auto& i : inputs_info) {
I-Jui (Ray) Sung959cd782017-10-04 20:49:57 -070092 if (!i.hasNoValue) i.location.offset = offset;
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070093 offset += i.location.length;
94 }
95 }
96
97 MixedTyped test; // holding test results
98
99 // Go through all outputs, initialize RequestArgument descriptors
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700100 resize_accordingly(golden, test);
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100101 for_all(golden, [&outputs_info, &outputSize](int index, auto, auto s) {
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700102 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
103 RequestArgument arg = {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100104 .location = {.poolIndex = OUTPUT,
105 .offset = 0,
106 .length = static_cast<uint32_t>(s)},
107 .dimensions = {},
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700108 };
109 outputs_info[index] = arg;
110 outputSize += s;
111 });
112 // Compute offset for outputs 1 and so on
113 {
114 size_t offset = 0;
115 for (auto& i : outputs_info) {
116 i.location.offset = offset;
117 offset += i.location.length;
118 }
119 }
Miao Wanga2d04c82018-02-05 17:26:54 -0800120 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
121 nn::allocateSharedMemory(outputSize)};
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700122 ASSERT_NE(0ull, pools[INPUT].size());
123 ASSERT_NE(0ull, pools[OUTPUT].size());
124
125 // load data
126 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
127 sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]);
128 ASSERT_NE(nullptr, inputMemory.get());
129 ASSERT_NE(nullptr, outputMemory.get());
130 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
131 char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer()));
132 ASSERT_NE(nullptr, inputPtr);
133 ASSERT_NE(nullptr, outputPtr);
134 inputMemory->update();
135 outputMemory->update();
136
137 // Go through all inputs, copy the values
138 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
139 char* begin = (char*)p;
140 char* end = begin + s;
141 // TODO: handle more than one input
142 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
143 });
144
145 inputMemory->commit();
146 outputMemory->commit();
Michael Butlercf22a572017-09-22 13:26:12 -0700147
Michael Butler814d8372019-01-15 11:02:55 -0800148 const Request request = {.inputs = inputs_info, .outputs = outputs_info, .pools = pools};
149
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100150 // launch execution
151 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
152 ASSERT_NE(nullptr, executionCallback.get());
153 Return<ErrorStatus> executionLaunchStatus =
154 preparedModel->execute(request, executionCallback);
155 ASSERT_TRUE(executionLaunchStatus.isOk());
156 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
Michael Butlercf22a572017-09-22 13:26:12 -0700157
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100158 // retrieve execution status
159 executionCallback->wait();
160 ASSERT_EQ(ErrorStatus::NONE, executionCallback->getStatus());
Xusong Wang187c5972018-11-07 09:33:59 -0800161
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700162 // validate results
163 outputMemory->read();
I-Jui (Ray) Sungf6b85502017-09-20 13:45:50 -0700164 copy_back(&test, outputs_info, outputPtr);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700165 outputMemory->commit();
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700166 // Filter out don't cares
I-Jui (Ray) Sung5bf4edf2017-10-06 13:22:39 -0700167 MixedTyped filtered_golden = filter(golden, is_ignored);
168 MixedTyped filtered_test = filter(test, is_ignored);
I-Jui (Ray) Sung7d765bd2017-09-13 18:47:12 -0700169
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700170 // We want "close-enough" results for float
Xusong Wang10d77e42018-08-28 16:50:01 -0700171 compare(filtered_golden, filtered_test, fpAtol, fpRtol);
Xusong Wang34058782019-01-18 17:28:26 -0800172 }
173}
174
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100175void Execute(const sp<IDevice>& device, std::function<Model(void)> create_model,
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100176 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100177 Model model = create_model();
Miao Wanga2d04c82018-02-05 17:26:54 -0800178
179 // see if service can handle model
180 bool fullySupportsModel = false;
Miao Wanga2d04c82018-02-05 17:26:54 -0800181 Return<void> supportedCall = device->getSupportedOperations(
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100182 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
183 ASSERT_EQ(ErrorStatus::NONE, status);
184 ASSERT_NE(0ul, supported.size());
185 fullySupportsModel = std::all_of(supported.begin(), supported.end(),
186 [](bool valid) { return valid; });
187 });
Miao Wanga2d04c82018-02-05 17:26:54 -0800188 ASSERT_TRUE(supportedCall.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800189
190 // launch prepare model
191 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
192 ASSERT_NE(nullptr, preparedModelCallback.get());
Miao Wanga2d04c82018-02-05 17:26:54 -0800193 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel(model, preparedModelCallback);
194 ASSERT_TRUE(prepareLaunchStatus.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800195 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wanga2d04c82018-02-05 17:26:54 -0800196
197 // retrieve prepared model
198 preparedModelCallback->wait();
199 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100200 sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel();
Miao Wanga2d04c82018-02-05 17:26:54 -0800201
202 // early termination if vendor service cannot fully prepare model
Michael Butler4d5bb102018-02-26 15:24:46 -0800203 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
Miao Wanga2d04c82018-02-05 17:26:54 -0800204 ASSERT_EQ(nullptr, preparedModel.get());
205 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
206 "prepare model that it does not support.";
207 std::cout << "[ ] Early termination of test because vendor service cannot "
208 "prepare model that it does not support."
209 << std::endl;
Miao Wangbb685a42019-01-08 12:27:35 -0800210 GTEST_SKIP();
Miao Wanga2d04c82018-02-05 17:26:54 -0800211 }
Michael Butler4d5bb102018-02-26 15:24:46 -0800212 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
Miao Wanga2d04c82018-02-05 17:26:54 -0800213 ASSERT_NE(nullptr, preparedModel.get());
214
Xusong Wang10d77e42018-08-28 16:50:01 -0700215 float fpAtol = 1e-5f, fpRtol = 5.0f * 1.1920928955078125e-7f;
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100216 EvaluatePreparedModel(preparedModel, is_ignored, examples, fpAtol, fpRtol);
Slava Shklyaev871be942018-09-12 14:52:02 +0100217}
218
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700219} // namespace generated_tests
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700220} // namespace neuralnetworks
221} // namespace hardware
222} // namespace android