blob: a7e83289f1c06003d1381d6ac5dfda6e5c3383ff [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
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010019#include <android-base/logging.h>
20#include <android/hidl/memory/1.0/IMemory.h>
21#include <hidlmemory/mapping.h>
22
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010023#include "1.0/Utils.h"
24#include "1.2/Callbacks.h"
25#include "ExecutionBurstController.h"
26#include "MemoryUtils.h"
27#include "TestHarness.h"
28#include "Utils.h"
29#include "VtsHalNeuralnetworks.h"
30
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010031namespace android {
32namespace hardware {
33namespace neuralnetworks {
34namespace V1_2 {
35namespace vts {
36namespace functional {
37
Xusong Wang1a06e772018-10-31 08:43:12 -070038using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010039using ::android::hidl::memory::V1_0::IMemory;
40using test_helper::for_all;
41using test_helper::MixedTyped;
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +010042using test_helper::MixedTypedExample;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010043
44///////////////////////// UTILITY FUNCTIONS /////////////////////////
45
David Gross55a3d322019-01-23 14:01:52 -080046static bool badTiming(Timing timing) {
47 return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX;
48}
49
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010050// Primary validation function. This function will take a valid request, apply a
51// mutation to it to invalidate the request, then pass it to interface calls
52// that use the request. Note that the request here is passed by value, and any
53// mutation to the request does not leave this function.
54static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
55 Request request, const std::function<void(Request*)>& mutation) {
56 mutation(&request);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010057
David Gross55a3d322019-01-23 14:01:52 -080058 // We'd like to test both with timing requested and without timing
59 // requested. Rather than running each test both ways, we'll decide whether
60 // to request timing by hashing the message. We do not use std::hash because
61 // it is not guaranteed stable across executions.
62 char hash = 0;
63 for (auto c : message) {
64 hash ^= c;
65 };
66 MeasureTiming measure = (hash & 1) ? MeasureTiming::YES : MeasureTiming::NO;
67
Michael Butler29471a82019-01-15 11:02:55 -080068 // asynchronous
David Gross4592ed12018-12-21 11:20:26 -080069 {
70 SCOPED_TRACE(message + " [execute_1_2]");
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010071
David Gross4592ed12018-12-21 11:20:26 -080072 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
73 ASSERT_NE(nullptr, executionCallback.get());
74 Return<ErrorStatus> executeLaunchStatus =
David Gross55a3d322019-01-23 14:01:52 -080075 preparedModel->execute_1_2(request, measure, executionCallback);
David Gross4592ed12018-12-21 11:20:26 -080076 ASSERT_TRUE(executeLaunchStatus.isOk());
77 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
78
79 executionCallback->wait();
80 ErrorStatus executionReturnStatus = executionCallback->getStatus();
Xusong Wangb50bc312018-11-07 09:33:59 -080081 const auto& outputShapes = executionCallback->getOutputShapes();
David Gross55a3d322019-01-23 14:01:52 -080082 Timing timing = executionCallback->getTiming();
David Gross4592ed12018-12-21 11:20:26 -080083 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
Xusong Wangb50bc312018-11-07 09:33:59 -080084 ASSERT_EQ(outputShapes.size(), 0);
David Gross55a3d322019-01-23 14:01:52 -080085 ASSERT_TRUE(badTiming(timing));
David Gross4592ed12018-12-21 11:20:26 -080086 }
87
Michael Butler29471a82019-01-15 11:02:55 -080088 // synchronous
David Gross4592ed12018-12-21 11:20:26 -080089 {
90 SCOPED_TRACE(message + " [executeSynchronously]");
91
Xusong Wangb50bc312018-11-07 09:33:59 -080092 Return<void> executeStatus = preparedModel->executeSynchronously(
David Gross55a3d322019-01-23 14:01:52 -080093 request, measure,
94 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes,
95 const Timing& timing) {
96 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
97 EXPECT_EQ(outputShapes.size(), 0);
98 EXPECT_TRUE(badTiming(timing));
99 });
David Gross4592ed12018-12-21 11:20:26 -0800100 ASSERT_TRUE(executeStatus.isOk());
David Gross4592ed12018-12-21 11:20:26 -0800101 }
Michael Butler29471a82019-01-15 11:02:55 -0800102
103 // burst
104 {
105 SCOPED_TRACE(message + " [burst]");
106
107 // create burst
Michael Butler51c72182019-03-27 10:59:25 -0700108 std::shared_ptr<::android::nn::ExecutionBurstController> burst =
Michael Butler60a22532019-03-28 13:41:14 -0700109 ::android::nn::ExecutionBurstController::create(preparedModel, /*blocking=*/true);
Michael Butler29471a82019-01-15 11:02:55 -0800110 ASSERT_NE(nullptr, burst.get());
111
112 // create memory keys
113 std::vector<intptr_t> keys(request.pools.size());
114 for (size_t i = 0; i < keys.size(); ++i) {
115 keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]);
116 }
117
118 // execute and verify
119 ErrorStatus error;
120 std::vector<OutputShape> outputShapes;
121 Timing timing;
122 std::tie(error, outputShapes, timing) = burst->compute(request, measure, keys);
123 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
124 EXPECT_EQ(outputShapes.size(), 0);
125 EXPECT_TRUE(badTiming(timing));
126
127 // additional burst testing
128 if (request.pools.size() > 0) {
129 // valid free
130 burst->freeMemory(keys.front());
131
132 // negative test: invalid free of unknown (blank) memory
133 burst->freeMemory(intptr_t{});
134
135 // negative test: double free of memory
136 burst->freeMemory(keys.front());
137 }
138 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100139}
140
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100141///////////////////////// REMOVE INPUT ////////////////////////////////////
142
143static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
144 for (size_t input = 0; input < request.inputs.size(); ++input) {
145 const std::string message = "removeInput: removed input " + std::to_string(input);
146 validate(preparedModel, message, request,
147 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
148 }
149}
150
151///////////////////////// REMOVE OUTPUT ////////////////////////////////////
152
153static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
154 for (size_t output = 0; output < request.outputs.size(); ++output) {
155 const std::string message = "removeOutput: removed Output " + std::to_string(output);
156 validate(preparedModel, message, request,
157 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
158 }
159}
160
161///////////////////////////// ENTRY POINT //////////////////////////////////
162
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100163std::vector<Request> createRequests(const std::vector<MixedTypedExample>& examples) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100164 const uint32_t INPUT = 0;
165 const uint32_t OUTPUT = 1;
166
167 std::vector<Request> requests;
168
169 for (auto& example : examples) {
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100170 const MixedTyped& inputs = example.operands.first;
171 const MixedTyped& outputs = example.operands.second;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100172
173 std::vector<RequestArgument> inputs_info, outputs_info;
174 uint32_t inputSize = 0, outputSize = 0;
175
176 // This function only partially specifies the metadata (vector of RequestArguments).
177 // The contents are copied over below.
178 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
179 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
180 RequestArgument arg = {
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100181 .location = {.poolIndex = INPUT,
182 .offset = 0,
183 .length = static_cast<uint32_t>(s)},
184 .dimensions = {},
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100185 };
186 RequestArgument arg_empty = {
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100187 .hasNoValue = true,
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100188 };
189 inputs_info[index] = s ? arg : arg_empty;
190 inputSize += s;
191 });
192 // Compute offset for inputs 1 and so on
193 {
194 size_t offset = 0;
195 for (auto& i : inputs_info) {
196 if (!i.hasNoValue) i.location.offset = offset;
197 offset += i.location.length;
198 }
199 }
200
201 // Go through all outputs, initialize RequestArgument descriptors
202 for_all(outputs, [&outputs_info, &outputSize](int index, auto, auto s) {
203 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
204 RequestArgument arg = {
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100205 .location = {.poolIndex = OUTPUT,
206 .offset = 0,
207 .length = static_cast<uint32_t>(s)},
208 .dimensions = {},
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100209 };
210 outputs_info[index] = arg;
211 outputSize += s;
212 });
213 // Compute offset for outputs 1 and so on
214 {
215 size_t offset = 0;
216 for (auto& i : outputs_info) {
217 i.location.offset = offset;
218 offset += i.location.length;
219 }
220 }
221 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
222 nn::allocateSharedMemory(outputSize)};
223 if (pools[INPUT].size() == 0 || pools[OUTPUT].size() == 0) {
224 return {};
225 }
226
227 // map pool
228 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
229 if (inputMemory == nullptr) {
230 return {};
231 }
232 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
233 if (inputPtr == nullptr) {
234 return {};
235 }
236
237 // initialize pool
238 inputMemory->update();
239 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
240 char* begin = (char*)p;
241 char* end = begin + s;
242 // TODO: handle more than one input
243 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
244 });
245 inputMemory->commit();
246
247 requests.push_back({.inputs = inputs_info, .outputs = outputs_info, .pools = pools});
248 }
249
250 return requests;
251}
252
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700253void ValidationTest::validateRequests(const sp<IPreparedModel>& preparedModel,
254 const std::vector<Request>& requests) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100255 // validate each request
256 for (const Request& request : requests) {
257 removeInputTest(preparedModel, request);
258 removeOutputTest(preparedModel, request);
259 }
260}
261
Slava Shklyaev95a59782019-05-10 16:08:30 +0100262void ValidationTest::validateRequestFailure(const sp<IPreparedModel>& preparedModel,
263 const std::vector<Request>& requests) {
264 for (const Request& request : requests) {
265 SCOPED_TRACE("Expecting request to fail [executeSynchronously]");
266 Return<void> executeStatus = preparedModel->executeSynchronously(
267 request, MeasureTiming::NO,
268 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes,
269 const Timing& timing) {
270 ASSERT_NE(ErrorStatus::NONE, error);
271 EXPECT_EQ(outputShapes.size(), 0);
272 EXPECT_TRUE(badTiming(timing));
273 });
274 ASSERT_TRUE(executeStatus.isOk());
275 }
276}
277
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100278} // namespace functional
279} // namespace vts
280} // namespace V1_2
281} // namespace neuralnetworks
282} // namespace hardware
283} // namespace android