blob: e2722aa7dc3d43d1b3aa56c2ca529ede084a384c [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);
100 SCOPED_TRACE(message + " [execute]");
101
102 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
103 ASSERT_NE(nullptr, executionCallback.get());
Xusong Wang1a06e772018-10-31 08:43:12 -0700104 Return<ErrorStatus> executeLaunchStatus =
105 preparedModel->execute_1_2(request, executionCallback);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100106 ASSERT_TRUE(executeLaunchStatus.isOk());
107 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
108
109 executionCallback->wait();
110 ErrorStatus executionReturnStatus = executionCallback->getStatus();
111 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
112}
113
114// Delete element from hidl_vec. hidl_vec doesn't support a "remove" operation,
115// so this is efficiently accomplished by moving the element to the end and
116// resizing the hidl_vec to one less.
117template <typename Type>
118static void hidl_vec_removeAt(hidl_vec<Type>* vec, uint32_t index) {
119 if (vec) {
120 std::rotate(vec->begin() + index, vec->begin() + index + 1, vec->end());
121 vec->resize(vec->size() - 1);
122 }
123}
124
125template <typename Type>
126static uint32_t hidl_vec_push_back(hidl_vec<Type>* vec, const Type& value) {
127 // assume vec is valid
128 const uint32_t index = vec->size();
129 vec->resize(index + 1);
130 (*vec)[index] = value;
131 return index;
132}
133
134///////////////////////// REMOVE INPUT ////////////////////////////////////
135
136static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
137 for (size_t input = 0; input < request.inputs.size(); ++input) {
138 const std::string message = "removeInput: removed input " + std::to_string(input);
139 validate(preparedModel, message, request,
140 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
141 }
142}
143
144///////////////////////// REMOVE OUTPUT ////////////////////////////////////
145
146static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
147 for (size_t output = 0; output < request.outputs.size(); ++output) {
148 const std::string message = "removeOutput: removed Output " + std::to_string(output);
149 validate(preparedModel, message, request,
150 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
151 }
152}
153
154///////////////////////////// ENTRY POINT //////////////////////////////////
155
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100156std::vector<Request> createRequests(const std::vector<MixedTypedExample>& examples) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100157 const uint32_t INPUT = 0;
158 const uint32_t OUTPUT = 1;
159
160 std::vector<Request> requests;
161
162 for (auto& example : examples) {
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100163 const MixedTyped& inputs = example.operands.first;
164 const MixedTyped& outputs = example.operands.second;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100165
166 std::vector<RequestArgument> inputs_info, outputs_info;
167 uint32_t inputSize = 0, outputSize = 0;
168
169 // This function only partially specifies the metadata (vector of RequestArguments).
170 // The contents are copied over below.
171 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
172 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
173 RequestArgument arg = {
174 .location = {.poolIndex = INPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
175 .dimensions = {},
176 };
177 RequestArgument arg_empty = {
178 .hasNoValue = true,
179 };
180 inputs_info[index] = s ? arg : arg_empty;
181 inputSize += s;
182 });
183 // Compute offset for inputs 1 and so on
184 {
185 size_t offset = 0;
186 for (auto& i : inputs_info) {
187 if (!i.hasNoValue) i.location.offset = offset;
188 offset += i.location.length;
189 }
190 }
191
192 // Go through all outputs, initialize RequestArgument descriptors
193 for_all(outputs, [&outputs_info, &outputSize](int index, auto, auto s) {
194 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
195 RequestArgument arg = {
196 .location = {.poolIndex = OUTPUT, .offset = 0, .length = static_cast<uint32_t>(s)},
197 .dimensions = {},
198 };
199 outputs_info[index] = arg;
200 outputSize += s;
201 });
202 // Compute offset for outputs 1 and so on
203 {
204 size_t offset = 0;
205 for (auto& i : outputs_info) {
206 i.location.offset = offset;
207 offset += i.location.length;
208 }
209 }
210 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
211 nn::allocateSharedMemory(outputSize)};
212 if (pools[INPUT].size() == 0 || pools[OUTPUT].size() == 0) {
213 return {};
214 }
215
216 // map pool
217 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
218 if (inputMemory == nullptr) {
219 return {};
220 }
221 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
222 if (inputPtr == nullptr) {
223 return {};
224 }
225
226 // initialize pool
227 inputMemory->update();
228 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
229 char* begin = (char*)p;
230 char* end = begin + s;
231 // TODO: handle more than one input
232 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
233 });
234 inputMemory->commit();
235
236 requests.push_back({.inputs = inputs_info, .outputs = outputs_info, .pools = pools});
237 }
238
239 return requests;
240}
241
242void ValidationTest::validateRequests(const Model& model, const std::vector<Request>& requests) {
243 // create IPreparedModel
244 sp<IPreparedModel> preparedModel;
245 ASSERT_NO_FATAL_FAILURE(createPreparedModel(device, model, &preparedModel));
246 if (preparedModel == nullptr) {
247 return;
248 }
249
250 // validate each request
251 for (const Request& request : requests) {
252 removeInputTest(preparedModel, request);
253 removeOutputTest(preparedModel, request);
254 }
255}
256
257} // namespace functional
258} // namespace vts
259} // namespace V1_2
260} // namespace neuralnetworks
261} // namespace hardware
262} // namespace android