blob: 00a7c3ec4f596ccb10a453305ca30eb00084acd7 [file] [log] [blame]
Slava Shklyaev871be942018-09-12 14:52:02 +01001/*
2 * Copyright (C) 2018 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 "neuralnetworks_hidl_hal_test"
18
19#include "VtsHalNeuralnetworks.h"
20
21#include "Callbacks.h"
22#include "TestHarness.h"
23#include "Utils.h"
24
25#include <android-base/logging.h>
26#include <android/hidl/memory/1.0/IMemory.h>
27#include <hidlmemory/mapping.h>
28
29namespace android {
30namespace hardware {
31namespace neuralnetworks {
32namespace V1_2 {
33namespace vts {
34namespace functional {
35
Xusong Wangb5cb8f72018-10-31 08:43:12 -070036using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
37using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback;
Slava Shklyaev871be942018-09-12 14:52:02 +010038using ::android::hidl::memory::V1_0::IMemory;
39using test_helper::for_all;
40using test_helper::MixedTyped;
Michael K. Sanders941d61a2018-10-19 14:39:09 +010041using test_helper::MixedTypedExample;
Slava Shklyaev871be942018-09-12 14:52:02 +010042
43///////////////////////// UTILITY FUNCTIONS /////////////////////////
44
David Grosse3013492019-01-23 14:01:52 -080045static bool badTiming(Timing timing) {
46 return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX;
47}
48
Slava Shklyaev871be942018-09-12 14:52:02 +010049static void createPreparedModel(const sp<IDevice>& device, const Model& model,
50 sp<IPreparedModel>* preparedModel) {
51 ASSERT_NE(nullptr, preparedModel);
52
53 // see if service can handle model
54 bool fullySupportsModel = false;
55 Return<void> supportedOpsLaunchStatus = device->getSupportedOperations_1_2(
56 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
57 ASSERT_EQ(ErrorStatus::NONE, status);
58 ASSERT_NE(0ul, supported.size());
59 fullySupportsModel =
60 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
61 });
62 ASSERT_TRUE(supportedOpsLaunchStatus.isOk());
63
64 // launch prepare model
65 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
66 ASSERT_NE(nullptr, preparedModelCallback.get());
67 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_2(
68 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
69 ASSERT_TRUE(prepareLaunchStatus.isOk());
70 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
71
72 // retrieve prepared model
73 preparedModelCallback->wait();
74 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wangb5cb8f72018-10-31 08:43:12 -070075 *preparedModel = getPreparedModel_1_2(preparedModelCallback);
Slava Shklyaev871be942018-09-12 14:52:02 +010076
77 // The getSupportedOperations_1_2 call returns a list of operations that are
78 // guaranteed not to fail if prepareModel_1_2 is called, and
79 // 'fullySupportsModel' is true i.f.f. the entire model is guaranteed.
80 // If a driver has any doubt that it can prepare an operation, it must
81 // return false. So here, if a driver isn't sure if it can support an
82 // operation, but reports that it successfully prepared the model, the test
83 // can continue.
84 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
85 ASSERT_EQ(nullptr, preparedModel->get());
86 LOG(INFO) << "NN VTS: Unable to test Request validation because vendor service cannot "
87 "prepare model that it does not support.";
88 std::cout << "[ ] Unable to test Request validation because vendor service "
89 "cannot prepare model that it does not support."
90 << std::endl;
91 return;
92 }
93 ASSERT_EQ(ErrorStatus::NONE, prepareReturnStatus);
94 ASSERT_NE(nullptr, preparedModel->get());
95}
96
97// Primary validation function. This function will take a valid request, apply a
98// mutation to it to invalidate the request, then pass it to interface calls
99// that use the request. Note that the request here is passed by value, and any
100// mutation to the request does not leave this function.
101static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
102 Request request, const std::function<void(Request*)>& mutation) {
103 mutation(&request);
Slava Shklyaev871be942018-09-12 14:52:02 +0100104
David Grosse3013492019-01-23 14:01:52 -0800105 // We'd like to test both with timing requested and without timing
106 // requested. Rather than running each test both ways, we'll decide whether
107 // to request timing by hashing the message. We do not use std::hash because
108 // it is not guaranteed stable across executions.
109 char hash = 0;
110 for (auto c : message) {
111 hash ^= c;
112 };
113 MeasureTiming measure = (hash & 1) ? MeasureTiming::YES : MeasureTiming::NO;
114
David Gross49e41672018-12-21 11:20:26 -0800115 {
116 SCOPED_TRACE(message + " [execute_1_2]");
Slava Shklyaev871be942018-09-12 14:52:02 +0100117
David Gross49e41672018-12-21 11:20:26 -0800118 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
119 ASSERT_NE(nullptr, executionCallback.get());
120 Return<ErrorStatus> executeLaunchStatus =
David Grosse3013492019-01-23 14:01:52 -0800121 preparedModel->execute_1_2(request, measure, executionCallback);
David Gross49e41672018-12-21 11:20:26 -0800122 ASSERT_TRUE(executeLaunchStatus.isOk());
123 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
124
125 executionCallback->wait();
126 ErrorStatus executionReturnStatus = executionCallback->getStatus();
Xusong Wang187c5972018-11-07 09:33:59 -0800127 const auto& outputShapes = executionCallback->getOutputShapes();
David Grosse3013492019-01-23 14:01:52 -0800128 Timing timing = executionCallback->getTiming();
David Gross49e41672018-12-21 11:20:26 -0800129 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
Xusong Wang187c5972018-11-07 09:33:59 -0800130 ASSERT_EQ(outputShapes.size(), 0);
David Grosse3013492019-01-23 14:01:52 -0800131 ASSERT_TRUE(badTiming(timing));
David Gross49e41672018-12-21 11:20:26 -0800132 }
133
134 {
135 SCOPED_TRACE(message + " [executeSynchronously]");
136
Xusong Wang187c5972018-11-07 09:33:59 -0800137 Return<void> executeStatus = preparedModel->executeSynchronously(
David Grosse3013492019-01-23 14:01:52 -0800138 request, measure,
139 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes,
140 const Timing& timing) {
141 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
142 EXPECT_EQ(outputShapes.size(), 0);
143 EXPECT_TRUE(badTiming(timing));
144 });
David Gross49e41672018-12-21 11:20:26 -0800145 ASSERT_TRUE(executeStatus.isOk());
David Gross49e41672018-12-21 11:20:26 -0800146 }
Slava Shklyaev871be942018-09-12 14:52:02 +0100147}
148
149// Delete element from hidl_vec. hidl_vec doesn't support a "remove" operation,
150// so this is efficiently accomplished by moving the element to the end and
151// resizing the hidl_vec to one less.
152template <typename Type>
153static void hidl_vec_removeAt(hidl_vec<Type>* vec, uint32_t index) {
154 if (vec) {
155 std::rotate(vec->begin() + index, vec->begin() + index + 1, vec->end());
156 vec->resize(vec->size() - 1);
157 }
158}
159
160template <typename Type>
161static uint32_t hidl_vec_push_back(hidl_vec<Type>* vec, const Type& value) {
162 // assume vec is valid
163 const uint32_t index = vec->size();
164 vec->resize(index + 1);
165 (*vec)[index] = value;
166 return index;
167}
168
169///////////////////////// REMOVE INPUT ////////////////////////////////////
170
171static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
172 for (size_t input = 0; input < request.inputs.size(); ++input) {
173 const std::string message = "removeInput: removed input " + std::to_string(input);
174 validate(preparedModel, message, request,
175 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
176 }
177}
178
179///////////////////////// REMOVE OUTPUT ////////////////////////////////////
180
181static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
182 for (size_t output = 0; output < request.outputs.size(); ++output) {
183 const std::string message = "removeOutput: removed Output " + std::to_string(output);
184 validate(preparedModel, message, request,
185 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
186 }
187}
188
189///////////////////////////// ENTRY POINT //////////////////////////////////
190
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100191std::vector<Request> createRequests(const std::vector<MixedTypedExample>& examples) {
Slava Shklyaev871be942018-09-12 14:52:02 +0100192 const uint32_t INPUT = 0;
193 const uint32_t OUTPUT = 1;
194
195 std::vector<Request> requests;
196
197 for (auto& example : examples) {
Michael K. Sanders941d61a2018-10-19 14:39:09 +0100198 const MixedTyped& inputs = example.operands.first;
199 const MixedTyped& outputs = example.operands.second;
Slava Shklyaev871be942018-09-12 14:52:02 +0100200
201 std::vector<RequestArgument> inputs_info, outputs_info;
202 uint32_t inputSize = 0, outputSize = 0;
203
204 // This function only partially specifies the metadata (vector of RequestArguments).
205 // The contents are copied over below.
206 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
207 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
208 RequestArgument arg = {
209 .location = {.poolIndex = INPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
210 .dimensions = {},
211 };
212 RequestArgument arg_empty = {
213 .hasNoValue = true,
214 };
215 inputs_info[index] = s ? arg : arg_empty;
216 inputSize += s;
217 });
218 // Compute offset for inputs 1 and so on
219 {
220 size_t offset = 0;
221 for (auto& i : inputs_info) {
222 if (!i.hasNoValue) i.location.offset = offset;
223 offset += i.location.length;
224 }
225 }
226
227 // Go through all outputs, initialize RequestArgument descriptors
228 for_all(outputs, [&outputs_info, &outputSize](int index, auto, auto s) {
229 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
230 RequestArgument arg = {
231 .location = {.poolIndex = OUTPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
232 .dimensions = {},
233 };
234 outputs_info[index] = arg;
235 outputSize += s;
236 });
237 // Compute offset for outputs 1 and so on
238 {
239 size_t offset = 0;
240 for (auto& i : outputs_info) {
241 i.location.offset = offset;
242 offset += i.location.length;
243 }
244 }
245 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
246 nn::allocateSharedMemory(outputSize)};
247 if (pools[INPUT].size() == 0 || pools[OUTPUT].size() == 0) {
248 return {};
249 }
250
251 // map pool
252 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
253 if (inputMemory == nullptr) {
254 return {};
255 }
256 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
257 if (inputPtr == nullptr) {
258 return {};
259 }
260
261 // initialize pool
262 inputMemory->update();
263 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
264 char* begin = (char*)p;
265 char* end = begin + s;
266 // TODO: handle more than one input
267 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
268 });
269 inputMemory->commit();
270
271 requests.push_back({.inputs = inputs_info, .outputs = outputs_info, .pools = pools});
272 }
273
274 return requests;
275}
276
277void ValidationTest::validateRequests(const Model& model, const std::vector<Request>& requests) {
278 // create IPreparedModel
279 sp<IPreparedModel> preparedModel;
280 ASSERT_NO_FATAL_FAILURE(createPreparedModel(device, model, &preparedModel));
281 if (preparedModel == nullptr) {
282 return;
283 }
284
285 // validate each request
286 for (const Request& request : requests) {
287 removeInputTest(preparedModel, request);
288 removeOutputTest(preparedModel, request);
289 }
290}
291
292} // namespace functional
293} // namespace vts
294} // namespace V1_2
295} // namespace neuralnetworks
296} // namespace hardware
297} // namespace android