blob: d9f64fdb5b53016703f8af003181f3b0b747978c [file] [log] [blame]
Slava Shklyaev73ee79d2019-05-14 14:15:14 +01001/*
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 "GeneratedTestHarness.h"
18
19#include <android-base/logging.h>
20#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
21#include <android/hardware/neuralnetworks/1.0/types.h>
22#include <android/hardware/neuralnetworks/1.1/IDevice.h>
23#include <android/hidl/allocator/1.0/IAllocator.h>
24#include <android/hidl/memory/1.0/IMemory.h>
25#include <hidlmemory/mapping.h>
26
27#include <iostream>
28
29#include "1.0/Callbacks.h"
30#include "1.0/Utils.h"
31#include "MemoryUtils.h"
32#include "TestHarness.h"
33
34namespace android {
35namespace hardware {
36namespace neuralnetworks {
37namespace generated_tests {
38
39using ::android::hardware::neuralnetworks::V1_0::ErrorStatus;
40using ::android::hardware::neuralnetworks::V1_0::IPreparedModel;
41using ::android::hardware::neuralnetworks::V1_0::Request;
42using ::android::hardware::neuralnetworks::V1_0::RequestArgument;
43using ::android::hardware::neuralnetworks::V1_0::implementation::ExecutionCallback;
44using ::android::hardware::neuralnetworks::V1_0::implementation::PreparedModelCallback;
45using ::android::hardware::neuralnetworks::V1_1::ExecutionPreference;
46using ::android::hardware::neuralnetworks::V1_1::IDevice;
47using ::android::hardware::neuralnetworks::V1_1::Model;
48using ::android::hidl::memory::V1_0::IMemory;
49using ::test_helper::compare;
50using ::test_helper::filter;
51using ::test_helper::for_all;
52using ::test_helper::MixedTyped;
53using ::test_helper::MixedTypedExample;
54using ::test_helper::resize_accordingly;
55
56// Top level driver for models and examples generated by test_generator.py
57// Test driver for those generated from ml/nn/runtime/test/spec
58void EvaluatePreparedModel(sp<IPreparedModel>& preparedModel, std::function<bool(int)> is_ignored,
59 const std::vector<MixedTypedExample>& examples,
60 bool hasRelaxedFloat32Model, float fpAtol, float fpRtol) {
61 const uint32_t INPUT = 0;
62 const uint32_t OUTPUT = 1;
63
64 int example_no = 1;
65 for (auto& example : examples) {
66 SCOPED_TRACE(example_no++);
67 const MixedTyped& inputs = example.operands.first;
68 const MixedTyped& golden = example.operands.second;
69
70 const bool hasFloat16Inputs = !inputs.float16Operands.empty();
71 if (hasRelaxedFloat32Model || hasFloat16Inputs) {
72 // TODO: Adjust the error limit based on testing.
73 // If in relaxed mode, set the absolute tolerance to be 5ULP of FP16.
74 fpAtol = 5.0f * 0.0009765625f;
75 // Set the relative tolerance to be 5ULP of the corresponding FP precision.
76 fpRtol = 5.0f * 0.0009765625f;
77 }
78
79 std::vector<RequestArgument> inputs_info, outputs_info;
80 uint32_t inputSize = 0, outputSize = 0;
81 // This function only partially specifies the metadata (vector of RequestArguments).
82 // The contents are copied over below.
83 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
84 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
85 RequestArgument arg = {
86 .location = {.poolIndex = INPUT,
87 .offset = 0,
88 .length = static_cast<uint32_t>(s)},
89 .dimensions = {},
90 };
91 RequestArgument arg_empty = {
92 .hasNoValue = true,
93 };
94 inputs_info[index] = s ? arg : arg_empty;
95 inputSize += s;
96 });
97 // Compute offset for inputs 1 and so on
98 {
99 size_t offset = 0;
100 for (auto& i : inputs_info) {
101 if (!i.hasNoValue) i.location.offset = offset;
102 offset += i.location.length;
103 }
104 }
105
106 MixedTyped test; // holding test results
107
108 // Go through all outputs, initialize RequestArgument descriptors
109 resize_accordingly(golden, test);
110 for_all(golden, [&outputs_info, &outputSize](int index, auto, auto s) {
111 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
112 RequestArgument arg = {
113 .location = {.poolIndex = OUTPUT,
114 .offset = 0,
115 .length = static_cast<uint32_t>(s)},
116 .dimensions = {},
117 };
118 outputs_info[index] = arg;
119 outputSize += s;
120 });
121 // Compute offset for outputs 1 and so on
122 {
123 size_t offset = 0;
124 for (auto& i : outputs_info) {
125 i.location.offset = offset;
126 offset += i.location.length;
127 }
128 }
129 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
130 nn::allocateSharedMemory(outputSize)};
131 ASSERT_NE(0ull, pools[INPUT].size());
132 ASSERT_NE(0ull, pools[OUTPUT].size());
133
134 // load data
135 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
136 sp<IMemory> outputMemory = mapMemory(pools[OUTPUT]);
137 ASSERT_NE(nullptr, inputMemory.get());
138 ASSERT_NE(nullptr, outputMemory.get());
139 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
140 char* outputPtr = reinterpret_cast<char*>(static_cast<void*>(outputMemory->getPointer()));
141 ASSERT_NE(nullptr, inputPtr);
142 ASSERT_NE(nullptr, outputPtr);
143 inputMemory->update();
144 outputMemory->update();
145
146 // Go through all inputs, copy the values
147 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
148 char* begin = (char*)p;
149 char* end = begin + s;
150 // TODO: handle more than one input
151 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
152 });
153
154 inputMemory->commit();
155 outputMemory->commit();
156
157 const Request request = {.inputs = inputs_info, .outputs = outputs_info, .pools = pools};
158
159 // launch execution
160 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
161 ASSERT_NE(nullptr, executionCallback.get());
162 Return<ErrorStatus> executionLaunchStatus =
163 preparedModel->execute(request, executionCallback);
164 ASSERT_TRUE(executionLaunchStatus.isOk());
165 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
166
167 // retrieve execution status
168 executionCallback->wait();
169 ASSERT_EQ(ErrorStatus::NONE, executionCallback->getStatus());
170
171 // validate results
172 outputMemory->read();
173 copy_back(&test, outputs_info, outputPtr);
174 outputMemory->commit();
175 // Filter out don't cares
176 MixedTyped filtered_golden = filter(golden, is_ignored);
177 MixedTyped filtered_test = filter(test, is_ignored);
178
179 // We want "close-enough" results for float
180 compare(filtered_golden, filtered_test, fpAtol, fpRtol);
181 }
182}
183
184void Execute(const sp<IDevice>& device, std::function<Model(void)> create_model,
185 std::function<bool(int)> is_ignored, const std::vector<MixedTypedExample>& examples) {
186 Model model = create_model();
187
188 // see if service can handle model
189 bool fullySupportsModel = false;
190 Return<void> supportedCall = device->getSupportedOperations_1_1(
191 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
192 ASSERT_EQ(ErrorStatus::NONE, status);
193 ASSERT_NE(0ul, supported.size());
194 fullySupportsModel = std::all_of(supported.begin(), supported.end(),
195 [](bool valid) { return valid; });
196 });
197 ASSERT_TRUE(supportedCall.isOk());
198
199 // launch prepare model
200 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
201 ASSERT_NE(nullptr, preparedModelCallback.get());
202 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_1(
203 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
204 ASSERT_TRUE(prepareLaunchStatus.isOk());
205 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
206
207 // retrieve prepared model
208 preparedModelCallback->wait();
209 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
210 sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel();
211
212 // early termination if vendor service cannot fully prepare model
213 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
214 ASSERT_EQ(nullptr, preparedModel.get());
215 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
216 "prepare model that it does not support.";
217 std::cout << "[ ] Early termination of test because vendor service cannot "
218 "prepare model that it does not support."
219 << std::endl;
220 GTEST_SKIP();
221 }
222 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
223 ASSERT_NE(nullptr, preparedModel.get());
224
225 EvaluatePreparedModel(preparedModel, is_ignored, examples,
226 model.relaxComputationFloat32toFloat16, 1e-5f, 1e-5f);
227}
228
229} // namespace generated_tests
230} // namespace neuralnetworks
231} // namespace hardware
232} // namespace android