blob: 1eaea4b9a6cd3387704752aced5f5c5ff73048c5 [file] [log] [blame]
Slava Shklyaevfeb87a92018-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 Wang1a06e772018-10-31 08:43:12 -070036using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
37using ::android::hardware::neuralnetworks::V1_2::implementation::PreparedModelCallback;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010038using ::android::hidl::memory::V1_0::IMemory;
39using test_helper::for_all;
40using test_helper::MixedTyped;
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +010041using test_helper::MixedTypedExample;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010042
43///////////////////////// UTILITY FUNCTIONS /////////////////////////
44
45static void createPreparedModel(const sp<IDevice>& device, const Model& model,
46 sp<IPreparedModel>* preparedModel) {
47 ASSERT_NE(nullptr, preparedModel);
48
49 // see if service can handle model
50 bool fullySupportsModel = false;
51 Return<void> supportedOpsLaunchStatus = device->getSupportedOperations_1_2(
52 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
53 ASSERT_EQ(ErrorStatus::NONE, status);
54 ASSERT_NE(0ul, supported.size());
55 fullySupportsModel =
56 std::all_of(supported.begin(), supported.end(), [](bool valid) { return valid; });
57 });
58 ASSERT_TRUE(supportedOpsLaunchStatus.isOk());
59
60 // launch prepare model
61 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
62 ASSERT_NE(nullptr, preparedModelCallback.get());
63 Return<ErrorStatus> prepareLaunchStatus = device->prepareModel_1_2(
64 model, ExecutionPreference::FAST_SINGLE_ANSWER, preparedModelCallback);
65 ASSERT_TRUE(prepareLaunchStatus.isOk());
66 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
67
68 // retrieve prepared model
69 preparedModelCallback->wait();
70 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
Xusong Wang1a06e772018-10-31 08:43:12 -070071 *preparedModel = getPreparedModel_1_2(preparedModelCallback);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010072
73 // The getSupportedOperations_1_2 call returns a list of operations that are
74 // guaranteed not to fail if prepareModel_1_2 is called, and
75 // 'fullySupportsModel' is true i.f.f. the entire model is guaranteed.
76 // If a driver has any doubt that it can prepare an operation, it must
77 // return false. So here, if a driver isn't sure if it can support an
78 // operation, but reports that it successfully prepared the model, the test
79 // can continue.
80 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
81 ASSERT_EQ(nullptr, preparedModel->get());
82 LOG(INFO) << "NN VTS: Unable to test Request validation because vendor service cannot "
83 "prepare model that it does not support.";
84 std::cout << "[ ] Unable to test Request validation because vendor service "
85 "cannot prepare model that it does not support."
86 << std::endl;
87 return;
88 }
89 ASSERT_EQ(ErrorStatus::NONE, prepareReturnStatus);
90 ASSERT_NE(nullptr, preparedModel->get());
91}
92
93// Primary validation function. This function will take a valid request, apply a
94// mutation to it to invalidate the request, then pass it to interface calls
95// that use the request. Note that the request here is passed by value, and any
96// mutation to the request does not leave this function.
97static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
98 Request request, const std::function<void(Request*)>& mutation) {
99 mutation(&request);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100100
David Gross4592ed12018-12-21 11:20:26 -0800101 {
102 SCOPED_TRACE(message + " [execute_1_2]");
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100103
David Gross4592ed12018-12-21 11:20:26 -0800104 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
105 ASSERT_NE(nullptr, executionCallback.get());
106 Return<ErrorStatus> executeLaunchStatus =
107 preparedModel->execute_1_2(request, executionCallback);
108 ASSERT_TRUE(executeLaunchStatus.isOk());
109 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
110
111 executionCallback->wait();
112 ErrorStatus executionReturnStatus = executionCallback->getStatus();
Xusong Wangb50bc312018-11-07 09:33:59 -0800113 const auto& outputShapes = executionCallback->getOutputShapes();
David Gross4592ed12018-12-21 11:20:26 -0800114 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
Xusong Wangb50bc312018-11-07 09:33:59 -0800115 ASSERT_EQ(outputShapes.size(), 0);
David Gross4592ed12018-12-21 11:20:26 -0800116 }
117
118 {
119 SCOPED_TRACE(message + " [executeSynchronously]");
120
Xusong Wangb50bc312018-11-07 09:33:59 -0800121 Return<void> executeStatus = preparedModel->executeSynchronously(
122 request, [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes) {
123 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
124 EXPECT_EQ(outputShapes.size(), 0);
125 });
David Gross4592ed12018-12-21 11:20:26 -0800126 ASSERT_TRUE(executeStatus.isOk());
David Gross4592ed12018-12-21 11:20:26 -0800127 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100128}
129
130// Delete element from hidl_vec. hidl_vec doesn't support a "remove" operation,
131// so this is efficiently accomplished by moving the element to the end and
132// resizing the hidl_vec to one less.
133template <typename Type>
134static void hidl_vec_removeAt(hidl_vec<Type>* vec, uint32_t index) {
135 if (vec) {
136 std::rotate(vec->begin() + index, vec->begin() + index + 1, vec->end());
137 vec->resize(vec->size() - 1);
138 }
139}
140
141template <typename Type>
142static uint32_t hidl_vec_push_back(hidl_vec<Type>* vec, const Type& value) {
143 // assume vec is valid
144 const uint32_t index = vec->size();
145 vec->resize(index + 1);
146 (*vec)[index] = value;
147 return index;
148}
149
150///////////////////////// REMOVE INPUT ////////////////////////////////////
151
152static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
153 for (size_t input = 0; input < request.inputs.size(); ++input) {
154 const std::string message = "removeInput: removed input " + std::to_string(input);
155 validate(preparedModel, message, request,
156 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
157 }
158}
159
160///////////////////////// REMOVE OUTPUT ////////////////////////////////////
161
162static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
163 for (size_t output = 0; output < request.outputs.size(); ++output) {
164 const std::string message = "removeOutput: removed Output " + std::to_string(output);
165 validate(preparedModel, message, request,
166 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
167 }
168}
169
170///////////////////////////// ENTRY POINT //////////////////////////////////
171
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100172std::vector<Request> createRequests(const std::vector<MixedTypedExample>& examples) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100173 const uint32_t INPUT = 0;
174 const uint32_t OUTPUT = 1;
175
176 std::vector<Request> requests;
177
178 for (auto& example : examples) {
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100179 const MixedTyped& inputs = example.operands.first;
180 const MixedTyped& outputs = example.operands.second;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100181
182 std::vector<RequestArgument> inputs_info, outputs_info;
183 uint32_t inputSize = 0, outputSize = 0;
184
185 // This function only partially specifies the metadata (vector of RequestArguments).
186 // The contents are copied over below.
187 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
188 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
189 RequestArgument arg = {
190 .location = {.poolIndex = INPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
191 .dimensions = {},
192 };
193 RequestArgument arg_empty = {
194 .hasNoValue = true,
195 };
196 inputs_info[index] = s ? arg : arg_empty;
197 inputSize += s;
198 });
199 // Compute offset for inputs 1 and so on
200 {
201 size_t offset = 0;
202 for (auto& i : inputs_info) {
203 if (!i.hasNoValue) i.location.offset = offset;
204 offset += i.location.length;
205 }
206 }
207
208 // Go through all outputs, initialize RequestArgument descriptors
209 for_all(outputs, [&outputs_info, &outputSize](int index, auto, auto s) {
210 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
211 RequestArgument arg = {
212 .location = {.poolIndex = OUTPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
213 .dimensions = {},
214 };
215 outputs_info[index] = arg;
216 outputSize += s;
217 });
218 // Compute offset for outputs 1 and so on
219 {
220 size_t offset = 0;
221 for (auto& i : outputs_info) {
222 i.location.offset = offset;
223 offset += i.location.length;
224 }
225 }
226 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
227 nn::allocateSharedMemory(outputSize)};
228 if (pools[INPUT].size() == 0 || pools[OUTPUT].size() == 0) {
229 return {};
230 }
231
232 // map pool
233 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
234 if (inputMemory == nullptr) {
235 return {};
236 }
237 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
238 if (inputPtr == nullptr) {
239 return {};
240 }
241
242 // initialize pool
243 inputMemory->update();
244 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
245 char* begin = (char*)p;
246 char* end = begin + s;
247 // TODO: handle more than one input
248 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
249 });
250 inputMemory->commit();
251
252 requests.push_back({.inputs = inputs_info, .outputs = outputs_info, .pools = pools});
253 }
254
255 return requests;
256}
257
258void ValidationTest::validateRequests(const Model& model, const std::vector<Request>& requests) {
259 // create IPreparedModel
260 sp<IPreparedModel> preparedModel;
261 ASSERT_NO_FATAL_FAILURE(createPreparedModel(device, model, &preparedModel));
262 if (preparedModel == nullptr) {
263 return;
264 }
265
266 // validate each request
267 for (const Request& request : requests) {
268 removeInputTest(preparedModel, request);
269 removeOutputTest(preparedModel, request);
270 }
271}
272
273} // namespace functional
274} // namespace vts
275} // namespace V1_2
276} // namespace neuralnetworks
277} // namespace hardware
278} // namespace android