blob: 8bb4934833dca703e6c0530f9ef4418133a459d7 [file] [log] [blame]
Michael Butler20f28a22019-04-26 17:46:08 -07001/*
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 "ExecutionBurstController.h"
23#include "ExecutionBurstServer.h"
24#include "TestHarness.h"
25#include "Utils.h"
26
27#include <android-base/logging.h>
Michael Butlerddb770f2019-05-02 18:16:13 -070028#include <cstring>
Michael Butler20f28a22019-04-26 17:46:08 -070029
30namespace android {
31namespace hardware {
32namespace neuralnetworks {
33namespace V1_2 {
34namespace vts {
35namespace functional {
36
37using ::android::nn::ExecutionBurstController;
38using ::android::nn::RequestChannelSender;
39using ::android::nn::ResultChannelReceiver;
40using ExecutionBurstCallback = ::android::nn::ExecutionBurstController::ExecutionBurstCallback;
41
42constexpr size_t kExecutionBurstChannelLength = 1024;
43constexpr size_t kExecutionBurstChannelSmallLength = 8;
44
45///////////////////////// UTILITY FUNCTIONS /////////////////////////
46
47static bool badTiming(Timing timing) {
48 return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX;
49}
50
51static void createBurst(const sp<IPreparedModel>& preparedModel, const sp<IBurstCallback>& callback,
52 std::unique_ptr<RequestChannelSender>* sender,
53 std::unique_ptr<ResultChannelReceiver>* receiver,
54 sp<IBurstContext>* context) {
55 ASSERT_NE(nullptr, preparedModel.get());
56 ASSERT_NE(nullptr, sender);
57 ASSERT_NE(nullptr, receiver);
58 ASSERT_NE(nullptr, context);
59
60 // create FMQ objects
61 auto [fmqRequestChannel, fmqRequestDescriptor] =
62 RequestChannelSender::create(kExecutionBurstChannelLength, /*blocking=*/true);
63 auto [fmqResultChannel, fmqResultDescriptor] =
64 ResultChannelReceiver::create(kExecutionBurstChannelLength, /*blocking=*/true);
65 ASSERT_NE(nullptr, fmqRequestChannel.get());
66 ASSERT_NE(nullptr, fmqResultChannel.get());
67 ASSERT_NE(nullptr, fmqRequestDescriptor);
68 ASSERT_NE(nullptr, fmqResultDescriptor);
69
70 // configure burst
71 ErrorStatus errorStatus;
72 sp<IBurstContext> burstContext;
73 const Return<void> ret = preparedModel->configureExecutionBurst(
74 callback, *fmqRequestDescriptor, *fmqResultDescriptor,
75 [&errorStatus, &burstContext](ErrorStatus status, const sp<IBurstContext>& context) {
76 errorStatus = status;
77 burstContext = context;
78 });
79 ASSERT_TRUE(ret.isOk());
80 ASSERT_EQ(ErrorStatus::NONE, errorStatus);
81 ASSERT_NE(nullptr, burstContext.get());
82
83 // return values
84 *sender = std::move(fmqRequestChannel);
85 *receiver = std::move(fmqResultChannel);
86 *context = burstContext;
87}
88
89static void createBurstWithResultChannelLength(
90 const sp<IPreparedModel>& preparedModel,
91 std::shared_ptr<ExecutionBurstController>* controller, size_t resultChannelLength) {
92 ASSERT_NE(nullptr, preparedModel.get());
93 ASSERT_NE(nullptr, controller);
94
95 // create FMQ objects
96 auto [fmqRequestChannel, fmqRequestDescriptor] =
97 RequestChannelSender::create(kExecutionBurstChannelLength, /*blocking=*/true);
98 auto [fmqResultChannel, fmqResultDescriptor] =
99 ResultChannelReceiver::create(resultChannelLength, /*blocking=*/true);
100 ASSERT_NE(nullptr, fmqRequestChannel.get());
101 ASSERT_NE(nullptr, fmqResultChannel.get());
102 ASSERT_NE(nullptr, fmqRequestDescriptor);
103 ASSERT_NE(nullptr, fmqResultDescriptor);
104
105 // configure burst
106 sp<ExecutionBurstCallback> callback = new ExecutionBurstCallback();
107 ErrorStatus errorStatus;
108 sp<IBurstContext> burstContext;
109 const Return<void> ret = preparedModel->configureExecutionBurst(
110 callback, *fmqRequestDescriptor, *fmqResultDescriptor,
111 [&errorStatus, &burstContext](ErrorStatus status, const sp<IBurstContext>& context) {
112 errorStatus = status;
113 burstContext = context;
114 });
115 ASSERT_TRUE(ret.isOk());
116 ASSERT_EQ(ErrorStatus::NONE, errorStatus);
117 ASSERT_NE(nullptr, burstContext.get());
118
119 // return values
120 *controller = std::make_shared<ExecutionBurstController>(
121 std::move(fmqRequestChannel), std::move(fmqResultChannel), burstContext, callback);
122}
123
124// Primary validation function. This function will take a valid serialized
125// request, apply a mutation to it to invalidate the serialized request, then
126// pass it to interface calls that use the serialized request. Note that the
127// serialized request here is passed by value, and any mutation to the
128// serialized request does not leave this function.
129static void validate(RequestChannelSender* sender, ResultChannelReceiver* receiver,
130 const std::string& message, std::vector<FmqRequestDatum> serialized,
131 const std::function<void(std::vector<FmqRequestDatum>*)>& mutation) {
132 mutation(&serialized);
133
134 // skip if packet is too large to send
135 if (serialized.size() > kExecutionBurstChannelLength) {
136 return;
137 }
138
139 SCOPED_TRACE(message);
140
141 // send invalid packet
142 sender->sendPacket(serialized);
143
144 // receive error
145 auto results = receiver->getBlocking();
146 ASSERT_TRUE(results.has_value());
147 const auto [status, outputShapes, timing] = std::move(*results);
148 EXPECT_NE(ErrorStatus::NONE, status);
149 EXPECT_EQ(0u, outputShapes.size());
150 EXPECT_TRUE(badTiming(timing));
151}
152
153static std::vector<FmqRequestDatum> createUniqueDatum() {
154 const FmqRequestDatum::PacketInformation packetInformation = {
155 /*.packetSize=*/10, /*.numberOfInputOperands=*/10, /*.numberOfOutputOperands=*/10,
156 /*.numberOfPools=*/10};
157 const FmqRequestDatum::OperandInformation operandInformation = {
158 /*.hasNoValue=*/false, /*.location=*/{}, /*.numberOfDimensions=*/10};
159 const int32_t invalidPoolIdentifier = std::numeric_limits<int32_t>::max();
160 std::vector<FmqRequestDatum> unique(7);
161 unique[0].packetInformation(packetInformation);
162 unique[1].inputOperandInformation(operandInformation);
163 unique[2].inputOperandDimensionValue(0);
164 unique[3].outputOperandInformation(operandInformation);
165 unique[4].outputOperandDimensionValue(0);
166 unique[5].poolIdentifier(invalidPoolIdentifier);
167 unique[6].measureTiming(MeasureTiming::YES);
168 return unique;
169}
170
171static const std::vector<FmqRequestDatum>& getUniqueDatum() {
172 static const std::vector<FmqRequestDatum> unique = createUniqueDatum();
173 return unique;
174}
175
176///////////////////////// REMOVE DATUM ////////////////////////////////////
177
178static void removeDatumTest(RequestChannelSender* sender, ResultChannelReceiver* receiver,
179 const std::vector<FmqRequestDatum>& serialized) {
180 for (size_t index = 0; index < serialized.size(); ++index) {
181 const std::string message = "removeDatum: removed datum at index " + std::to_string(index);
182 validate(sender, receiver, message, serialized,
183 [index](std::vector<FmqRequestDatum>* serialized) {
184 serialized->erase(serialized->begin() + index);
185 });
186 }
187}
188
189///////////////////////// ADD DATUM ////////////////////////////////////
190
191static void addDatumTest(RequestChannelSender* sender, ResultChannelReceiver* receiver,
192 const std::vector<FmqRequestDatum>& serialized) {
193 const std::vector<FmqRequestDatum>& extra = getUniqueDatum();
194 for (size_t index = 0; index <= serialized.size(); ++index) {
195 for (size_t type = 0; type < extra.size(); ++type) {
196 const std::string message = "addDatum: added datum type " + std::to_string(type) +
197 " at index " + std::to_string(index);
198 validate(sender, receiver, message, serialized,
199 [index, type, &extra](std::vector<FmqRequestDatum>* serialized) {
200 serialized->insert(serialized->begin() + index, extra[type]);
201 });
202 }
203 }
204}
205
206///////////////////////// MUTATE DATUM ////////////////////////////////////
207
208static bool interestingCase(const FmqRequestDatum& lhs, const FmqRequestDatum& rhs) {
209 using Discriminator = FmqRequestDatum::hidl_discriminator;
210
211 const bool differentValues = (lhs != rhs);
212 const bool sameSumType = (lhs.getDiscriminator() == rhs.getDiscriminator());
213 const auto discriminator = rhs.getDiscriminator();
214 const bool isDimensionValue = (discriminator == Discriminator::inputOperandDimensionValue ||
215 discriminator == Discriminator::outputOperandDimensionValue);
216
217 return differentValues && !(sameSumType && isDimensionValue);
218}
219
220static void mutateDatumTest(RequestChannelSender* sender, ResultChannelReceiver* receiver,
221 const std::vector<FmqRequestDatum>& serialized) {
222 const std::vector<FmqRequestDatum>& change = getUniqueDatum();
223 for (size_t index = 0; index < serialized.size(); ++index) {
224 for (size_t type = 0; type < change.size(); ++type) {
225 if (interestingCase(serialized[index], change[type])) {
226 const std::string message = "mutateDatum: changed datum at index " +
227 std::to_string(index) + " to datum type " +
228 std::to_string(type);
229 validate(sender, receiver, message, serialized,
230 [index, type, &change](std::vector<FmqRequestDatum>* serialized) {
231 (*serialized)[index] = change[type];
232 });
233 }
234 }
235 }
236}
237
238///////////////////////// BURST VALIATION TESTS ////////////////////////////////////
239
240static void validateBurstSerialization(const sp<IPreparedModel>& preparedModel,
241 const std::vector<Request>& requests) {
242 // create burst
243 std::unique_ptr<RequestChannelSender> sender;
244 std::unique_ptr<ResultChannelReceiver> receiver;
245 sp<ExecutionBurstCallback> callback = new ExecutionBurstCallback();
246 sp<IBurstContext> context;
247 ASSERT_NO_FATAL_FAILURE(createBurst(preparedModel, callback, &sender, &receiver, &context));
248 ASSERT_NE(nullptr, sender.get());
249 ASSERT_NE(nullptr, receiver.get());
250 ASSERT_NE(nullptr, context.get());
251
252 // validate each request
253 for (const Request& request : requests) {
254 // load memory into callback slots
255 std::vector<intptr_t> keys(request.pools.size());
256 for (size_t i = 0; i < keys.size(); ++i) {
257 keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]);
258 }
259 const std::vector<int32_t> slots = callback->getSlots(request.pools, keys);
260
261 // ensure slot std::numeric_limits<int32_t>::max() doesn't exist (for
262 // subsequent slot validation testing)
263 const auto maxElement = std::max_element(slots.begin(), slots.end());
264 ASSERT_NE(slots.end(), maxElement);
265 ASSERT_NE(std::numeric_limits<int32_t>::max(), *maxElement);
266
267 // serialize the request
268 const auto serialized = ::android::nn::serialize(request, MeasureTiming::YES, slots);
269
270 // validations
271 removeDatumTest(sender.get(), receiver.get(), serialized);
272 addDatumTest(sender.get(), receiver.get(), serialized);
273 mutateDatumTest(sender.get(), receiver.get(), serialized);
274 }
275}
276
277static void validateBurstFmqLength(const sp<IPreparedModel>& preparedModel,
278 const std::vector<Request>& requests) {
279 // create regular burst
280 std::shared_ptr<ExecutionBurstController> controllerRegular;
281 ASSERT_NO_FATAL_FAILURE(createBurstWithResultChannelLength(preparedModel, &controllerRegular,
282 kExecutionBurstChannelLength));
283 ASSERT_NE(nullptr, controllerRegular.get());
284
285 // create burst with small output channel
286 std::shared_ptr<ExecutionBurstController> controllerSmall;
287 ASSERT_NO_FATAL_FAILURE(createBurstWithResultChannelLength(preparedModel, &controllerSmall,
288 kExecutionBurstChannelSmallLength));
289 ASSERT_NE(nullptr, controllerSmall.get());
290
291 // validate each request
292 for (const Request& request : requests) {
293 // load memory into callback slots
294 std::vector<intptr_t> keys(request.pools.size());
295 for (size_t i = 0; i < keys.size(); ++i) {
296 keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]);
297 }
298
299 // collect serialized result by running regular burst
300 const auto [status1, outputShapes1, timing1] =
301 controllerRegular->compute(request, MeasureTiming::NO, keys);
302
303 // skip test if synchronous output isn't useful
304 const std::vector<FmqResultDatum> serialized =
305 ::android::nn::serialize(status1, outputShapes1, timing1);
306 if (status1 != ErrorStatus::NONE ||
307 serialized.size() <= kExecutionBurstChannelSmallLength) {
308 continue;
309 }
310
311 // by this point, execution should fail because the result channel isn't
312 // large enough to return the serialized result
313 const auto [status2, outputShapes2, timing2] =
314 controllerSmall->compute(request, MeasureTiming::NO, keys);
315 EXPECT_NE(ErrorStatus::NONE, status2);
316 EXPECT_EQ(0u, outputShapes2.size());
317 EXPECT_TRUE(badTiming(timing2));
318 }
319}
320
Michael Butlerddb770f2019-05-02 18:16:13 -0700321static bool isSanitized(const FmqResultDatum& datum) {
322 using Discriminator = FmqResultDatum::hidl_discriminator;
323
324 // check to ensure the padding values in the returned
325 // FmqResultDatum::OperandInformation are initialized to 0
326 if (datum.getDiscriminator() == Discriminator::operandInformation) {
327 static_assert(
328 offsetof(FmqResultDatum::OperandInformation, isSufficient) == 0,
329 "unexpected value for offset of FmqResultDatum::OperandInformation::isSufficient");
330 static_assert(
331 sizeof(FmqResultDatum::OperandInformation::isSufficient) == 1,
332 "unexpected value for size of FmqResultDatum::OperandInformation::isSufficient");
333 static_assert(offsetof(FmqResultDatum::OperandInformation, numberOfDimensions) == 4,
334 "unexpected value for offset of "
335 "FmqResultDatum::OperandInformation::numberOfDimensions");
336 static_assert(sizeof(FmqResultDatum::OperandInformation::numberOfDimensions) == 4,
337 "unexpected value for size of "
338 "FmqResultDatum::OperandInformation::numberOfDimensions");
339 static_assert(sizeof(FmqResultDatum::OperandInformation) == 8,
340 "unexpected value for size of "
341 "FmqResultDatum::OperandInformation");
342
343 constexpr size_t paddingOffset =
344 offsetof(FmqResultDatum::OperandInformation, isSufficient) +
345 sizeof(FmqResultDatum::OperandInformation::isSufficient);
346 constexpr size_t paddingSize =
347 offsetof(FmqResultDatum::OperandInformation, numberOfDimensions) - paddingOffset;
348
349 FmqResultDatum::OperandInformation initialized{};
350 std::memset(&initialized, 0, sizeof(initialized));
351
352 const char* initializedPaddingStart =
353 reinterpret_cast<const char*>(&initialized) + paddingOffset;
354 const char* datumPaddingStart =
355 reinterpret_cast<const char*>(&datum.operandInformation()) + paddingOffset;
356
357 return std::memcmp(datumPaddingStart, initializedPaddingStart, paddingSize) == 0;
358 }
359
360 // there are no other padding initialization checks required, so return true
361 // for any sum-type that isn't FmqResultDatum::OperandInformation
362 return true;
363}
364
365static void validateBurstSanitized(const sp<IPreparedModel>& preparedModel,
366 const std::vector<Request>& requests) {
367 // create burst
368 std::unique_ptr<RequestChannelSender> sender;
369 std::unique_ptr<ResultChannelReceiver> receiver;
370 sp<ExecutionBurstCallback> callback = new ExecutionBurstCallback();
371 sp<IBurstContext> context;
372 ASSERT_NO_FATAL_FAILURE(createBurst(preparedModel, callback, &sender, &receiver, &context));
373 ASSERT_NE(nullptr, sender.get());
374 ASSERT_NE(nullptr, receiver.get());
375 ASSERT_NE(nullptr, context.get());
376
377 // validate each request
378 for (const Request& request : requests) {
379 // load memory into callback slots
380 std::vector<intptr_t> keys;
381 keys.reserve(request.pools.size());
382 std::transform(request.pools.begin(), request.pools.end(), std::back_inserter(keys),
383 [](const auto& pool) { return reinterpret_cast<intptr_t>(&pool); });
384 const std::vector<int32_t> slots = callback->getSlots(request.pools, keys);
385
386 // send valid request
387 ASSERT_TRUE(sender->send(request, MeasureTiming::YES, slots));
388
389 // receive valid result
390 auto serialized = receiver->getPacketBlocking();
391 ASSERT_TRUE(serialized.has_value());
392
393 // sanitize result
394 ASSERT_TRUE(std::all_of(serialized->begin(), serialized->end(), isSanitized))
395 << "The result serialized data is not properly sanitized";
396 }
397}
398
Michael Butler20f28a22019-04-26 17:46:08 -0700399///////////////////////////// ENTRY POINT //////////////////////////////////
400
401void ValidationTest::validateBurst(const sp<IPreparedModel>& preparedModel,
402 const std::vector<Request>& requests) {
403 ASSERT_NO_FATAL_FAILURE(validateBurstSerialization(preparedModel, requests));
404 ASSERT_NO_FATAL_FAILURE(validateBurstFmqLength(preparedModel, requests));
Michael Butlerddb770f2019-05-02 18:16:13 -0700405 ASSERT_NO_FATAL_FAILURE(validateBurstSanitized(preparedModel, requests));
Michael Butler20f28a22019-04-26 17:46:08 -0700406}
407
408} // namespace functional
409} // namespace vts
410} // namespace V1_2
411} // namespace neuralnetworks
412} // namespace hardware
413} // namespace android