blob: cf5905f688d6faab2a2f5ec157d68d835912e590 [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
Michael Butler3835f612019-07-11 15:43:22 -070038using ::android::hardware::neuralnetworks::V1_0::RequestArgument;
Xusong Wang1a06e772018-10-31 08:43:12 -070039using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010040using ::android::hidl::memory::V1_0::IMemory;
41using test_helper::for_all;
42using test_helper::MixedTyped;
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +010043using test_helper::MixedTypedExample;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010044
45///////////////////////// UTILITY FUNCTIONS /////////////////////////
46
David Gross55a3d322019-01-23 14:01:52 -080047static bool badTiming(Timing timing) {
48 return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX;
49}
50
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010051// Primary validation function. This function will take a valid request, apply a
52// mutation to it to invalidate the request, then pass it to interface calls
53// that use the request. Note that the request here is passed by value, and any
54// mutation to the request does not leave this function.
55static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message,
56 Request request, const std::function<void(Request*)>& mutation) {
57 mutation(&request);
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010058
David Gross55a3d322019-01-23 14:01:52 -080059 // We'd like to test both with timing requested and without timing
60 // requested. Rather than running each test both ways, we'll decide whether
61 // to request timing by hashing the message. We do not use std::hash because
62 // it is not guaranteed stable across executions.
63 char hash = 0;
64 for (auto c : message) {
65 hash ^= c;
66 };
67 MeasureTiming measure = (hash & 1) ? MeasureTiming::YES : MeasureTiming::NO;
68
Michael Butler29471a82019-01-15 11:02:55 -080069 // asynchronous
David Gross4592ed12018-12-21 11:20:26 -080070 {
71 SCOPED_TRACE(message + " [execute_1_2]");
Slava Shklyaevfeb87a92018-09-12 14:52:02 +010072
David Gross4592ed12018-12-21 11:20:26 -080073 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
74 ASSERT_NE(nullptr, executionCallback.get());
75 Return<ErrorStatus> executeLaunchStatus =
David Gross55a3d322019-01-23 14:01:52 -080076 preparedModel->execute_1_2(request, measure, executionCallback);
David Gross4592ed12018-12-21 11:20:26 -080077 ASSERT_TRUE(executeLaunchStatus.isOk());
78 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus));
79
80 executionCallback->wait();
81 ErrorStatus executionReturnStatus = executionCallback->getStatus();
Xusong Wangb50bc312018-11-07 09:33:59 -080082 const auto& outputShapes = executionCallback->getOutputShapes();
David Gross55a3d322019-01-23 14:01:52 -080083 Timing timing = executionCallback->getTiming();
David Gross4592ed12018-12-21 11:20:26 -080084 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus);
Xusong Wangb50bc312018-11-07 09:33:59 -080085 ASSERT_EQ(outputShapes.size(), 0);
David Gross55a3d322019-01-23 14:01:52 -080086 ASSERT_TRUE(badTiming(timing));
David Gross4592ed12018-12-21 11:20:26 -080087 }
88
Michael Butler29471a82019-01-15 11:02:55 -080089 // synchronous
David Gross4592ed12018-12-21 11:20:26 -080090 {
91 SCOPED_TRACE(message + " [executeSynchronously]");
92
Xusong Wangb50bc312018-11-07 09:33:59 -080093 Return<void> executeStatus = preparedModel->executeSynchronously(
David Gross55a3d322019-01-23 14:01:52 -080094 request, measure,
95 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes,
96 const Timing& timing) {
97 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
98 EXPECT_EQ(outputShapes.size(), 0);
99 EXPECT_TRUE(badTiming(timing));
100 });
David Gross4592ed12018-12-21 11:20:26 -0800101 ASSERT_TRUE(executeStatus.isOk());
David Gross4592ed12018-12-21 11:20:26 -0800102 }
Michael Butler29471a82019-01-15 11:02:55 -0800103
104 // burst
105 {
106 SCOPED_TRACE(message + " [burst]");
107
108 // create burst
Michael Butler51c72182019-03-27 10:59:25 -0700109 std::shared_ptr<::android::nn::ExecutionBurstController> burst =
Michael Butler60a22532019-03-28 13:41:14 -0700110 ::android::nn::ExecutionBurstController::create(preparedModel, /*blocking=*/true);
Michael Butler29471a82019-01-15 11:02:55 -0800111 ASSERT_NE(nullptr, burst.get());
112
113 // create memory keys
114 std::vector<intptr_t> keys(request.pools.size());
115 for (size_t i = 0; i < keys.size(); ++i) {
116 keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]);
117 }
118
119 // execute and verify
120 ErrorStatus error;
121 std::vector<OutputShape> outputShapes;
122 Timing timing;
123 std::tie(error, outputShapes, timing) = burst->compute(request, measure, keys);
124 EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, error);
125 EXPECT_EQ(outputShapes.size(), 0);
126 EXPECT_TRUE(badTiming(timing));
127
128 // additional burst testing
129 if (request.pools.size() > 0) {
130 // valid free
131 burst->freeMemory(keys.front());
132
133 // negative test: invalid free of unknown (blank) memory
134 burst->freeMemory(intptr_t{});
135
136 // negative test: double free of memory
137 burst->freeMemory(keys.front());
138 }
139 }
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100140}
141
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100142///////////////////////// REMOVE INPUT ////////////////////////////////////
143
144static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
145 for (size_t input = 0; input < request.inputs.size(); ++input) {
146 const std::string message = "removeInput: removed input " + std::to_string(input);
147 validate(preparedModel, message, request,
148 [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); });
149 }
150}
151
152///////////////////////// REMOVE OUTPUT ////////////////////////////////////
153
154static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) {
155 for (size_t output = 0; output < request.outputs.size(); ++output) {
156 const std::string message = "removeOutput: removed Output " + std::to_string(output);
157 validate(preparedModel, message, request,
158 [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); });
159 }
160}
161
162///////////////////////////// ENTRY POINT //////////////////////////////////
163
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100164std::vector<Request> createRequests(const std::vector<MixedTypedExample>& examples) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100165 const uint32_t INPUT = 0;
166 const uint32_t OUTPUT = 1;
167
168 std::vector<Request> requests;
169
170 for (auto& example : examples) {
Michael K. Sandersda3bdbc2018-10-19 14:39:09 +0100171 const MixedTyped& inputs = example.operands.first;
172 const MixedTyped& outputs = example.operands.second;
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100173
174 std::vector<RequestArgument> inputs_info, outputs_info;
175 uint32_t inputSize = 0, outputSize = 0;
176
177 // This function only partially specifies the metadata (vector of RequestArguments).
178 // The contents are copied over below.
179 for_all(inputs, [&inputs_info, &inputSize](int index, auto, auto s) {
180 if (inputs_info.size() <= static_cast<size_t>(index)) inputs_info.resize(index + 1);
181 RequestArgument arg = {
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100182 .location = {.poolIndex = INPUT,
183 .offset = 0,
184 .length = static_cast<uint32_t>(s)},
185 .dimensions = {},
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100186 };
187 RequestArgument arg_empty = {
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100188 .hasNoValue = true,
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100189 };
190 inputs_info[index] = s ? arg : arg_empty;
191 inputSize += s;
192 });
193 // Compute offset for inputs 1 and so on
194 {
195 size_t offset = 0;
196 for (auto& i : inputs_info) {
197 if (!i.hasNoValue) i.location.offset = offset;
198 offset += i.location.length;
199 }
200 }
201
202 // Go through all outputs, initialize RequestArgument descriptors
203 for_all(outputs, [&outputs_info, &outputSize](int index, auto, auto s) {
204 if (outputs_info.size() <= static_cast<size_t>(index)) outputs_info.resize(index + 1);
205 RequestArgument arg = {
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100206 .location = {.poolIndex = OUTPUT,
207 .offset = 0,
208 .length = static_cast<uint32_t>(s)},
209 .dimensions = {},
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100210 };
211 outputs_info[index] = arg;
212 outputSize += s;
213 });
214 // Compute offset for outputs 1 and so on
215 {
216 size_t offset = 0;
217 for (auto& i : outputs_info) {
218 i.location.offset = offset;
219 offset += i.location.length;
220 }
221 }
222 std::vector<hidl_memory> pools = {nn::allocateSharedMemory(inputSize),
223 nn::allocateSharedMemory(outputSize)};
224 if (pools[INPUT].size() == 0 || pools[OUTPUT].size() == 0) {
225 return {};
226 }
227
228 // map pool
229 sp<IMemory> inputMemory = mapMemory(pools[INPUT]);
230 if (inputMemory == nullptr) {
231 return {};
232 }
233 char* inputPtr = reinterpret_cast<char*>(static_cast<void*>(inputMemory->getPointer()));
234 if (inputPtr == nullptr) {
235 return {};
236 }
237
238 // initialize pool
239 inputMemory->update();
240 for_all(inputs, [&inputs_info, inputPtr](int index, auto p, auto s) {
241 char* begin = (char*)p;
242 char* end = begin + s;
243 // TODO: handle more than one input
244 std::copy(begin, end, inputPtr + inputs_info[index].location.offset);
245 });
246 inputMemory->commit();
247
248 requests.push_back({.inputs = inputs_info, .outputs = outputs_info, .pools = pools});
249 }
250
251 return requests;
252}
253
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700254void ValidationTest::validateRequests(const sp<IPreparedModel>& preparedModel,
255 const std::vector<Request>& requests) {
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100256 // validate each request
257 for (const Request& request : requests) {
258 removeInputTest(preparedModel, request);
259 removeOutputTest(preparedModel, request);
260 }
261}
262
Slava Shklyaev95a59782019-05-10 16:08:30 +0100263void ValidationTest::validateRequestFailure(const sp<IPreparedModel>& preparedModel,
264 const std::vector<Request>& requests) {
265 for (const Request& request : requests) {
266 SCOPED_TRACE("Expecting request to fail [executeSynchronously]");
267 Return<void> executeStatus = preparedModel->executeSynchronously(
268 request, MeasureTiming::NO,
269 [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes,
270 const Timing& timing) {
271 ASSERT_NE(ErrorStatus::NONE, error);
272 EXPECT_EQ(outputShapes.size(), 0);
273 EXPECT_TRUE(badTiming(timing));
274 });
275 ASSERT_TRUE(executeStatus.isOk());
276 }
277}
278
Slava Shklyaevfeb87a92018-09-12 14:52:02 +0100279} // namespace functional
280} // namespace vts
281} // namespace V1_2
282} // namespace neuralnetworks
283} // namespace hardware
284} // namespace android