blob: a322011c6700b40de18519d6d982221e074c31e9 [file] [log] [blame]
Michael Butlerd6e38fd2019-04-26 17:46:08 -07001/*
Michael Butler353a6242019-04-30 13:51:24 -07002 * Copyright (C) 2019 The Android Open Source Project
Michael Butlerd6e38fd2019-04-26 17:46:08 -07003 *
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
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010021#include "1.2/Callbacks.h"
Michael Butlerd6e38fd2019-04-26 17:46:08 -070022#include "ExecutionBurstController.h"
23#include "ExecutionBurstServer.h"
Xusong Wangbcaa7822019-08-23 16:10:54 -070024#include "GeneratedTestHarness.h"
Michael Butlerd6e38fd2019-04-26 17:46:08 -070025#include "TestHarness.h"
26#include "Utils.h"
27
28#include <android-base/logging.h>
29
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
Michael Butler353a6242019-04-30 13:51:24 -070042// This constant value represents the length of an FMQ that is large enough to
43// return a result from a burst execution for all of the generated test cases.
Michael Butlerd6e38fd2019-04-26 17:46:08 -070044constexpr size_t kExecutionBurstChannelLength = 1024;
Michael Butler353a6242019-04-30 13:51:24 -070045
46// This constant value represents a length of an FMQ that is not large enough
47// to return a result from a burst execution for some of the generated test
48// cases.
Michael Butlerd6e38fd2019-04-26 17:46:08 -070049constexpr size_t kExecutionBurstChannelSmallLength = 8;
50
51///////////////////////// UTILITY FUNCTIONS /////////////////////////
52
53static bool badTiming(Timing timing) {
54 return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX;
55}
56
57static void createBurst(const sp<IPreparedModel>& preparedModel, const sp<IBurstCallback>& callback,
58 std::unique_ptr<RequestChannelSender>* sender,
59 std::unique_ptr<ResultChannelReceiver>* receiver,
Michael Butler353a6242019-04-30 13:51:24 -070060 sp<IBurstContext>* context,
61 size_t resultChannelLength = kExecutionBurstChannelLength) {
Michael Butlerd6e38fd2019-04-26 17:46:08 -070062 ASSERT_NE(nullptr, preparedModel.get());
63 ASSERT_NE(nullptr, sender);
64 ASSERT_NE(nullptr, receiver);
65 ASSERT_NE(nullptr, context);
66
67 // create FMQ objects
68 auto [fmqRequestChannel, fmqRequestDescriptor] =
69 RequestChannelSender::create(kExecutionBurstChannelLength, /*blocking=*/true);
70 auto [fmqResultChannel, fmqResultDescriptor] =
Michael Butler353a6242019-04-30 13:51:24 -070071 ResultChannelReceiver::create(resultChannelLength, /*blocking=*/true);
Michael Butlerd6e38fd2019-04-26 17:46:08 -070072 ASSERT_NE(nullptr, fmqRequestChannel.get());
73 ASSERT_NE(nullptr, fmqResultChannel.get());
74 ASSERT_NE(nullptr, fmqRequestDescriptor);
75 ASSERT_NE(nullptr, fmqResultDescriptor);
76
77 // configure burst
78 ErrorStatus errorStatus;
79 sp<IBurstContext> burstContext;
80 const Return<void> ret = preparedModel->configureExecutionBurst(
81 callback, *fmqRequestDescriptor, *fmqResultDescriptor,
82 [&errorStatus, &burstContext](ErrorStatus status, const sp<IBurstContext>& context) {
83 errorStatus = status;
84 burstContext = context;
85 });
86 ASSERT_TRUE(ret.isOk());
87 ASSERT_EQ(ErrorStatus::NONE, errorStatus);
88 ASSERT_NE(nullptr, burstContext.get());
89
90 // return values
91 *sender = std::move(fmqRequestChannel);
92 *receiver = std::move(fmqResultChannel);
93 *context = burstContext;
94}
95
96static void createBurstWithResultChannelLength(
Michael Butler353a6242019-04-30 13:51:24 -070097 const sp<IPreparedModel>& preparedModel, size_t resultChannelLength,
98 std::shared_ptr<ExecutionBurstController>* controller) {
Michael Butlerd6e38fd2019-04-26 17:46:08 -070099 ASSERT_NE(nullptr, preparedModel.get());
100 ASSERT_NE(nullptr, controller);
101
102 // create FMQ objects
Michael Butler353a6242019-04-30 13:51:24 -0700103 std::unique_ptr<RequestChannelSender> sender;
104 std::unique_ptr<ResultChannelReceiver> receiver;
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700105 sp<ExecutionBurstCallback> callback = new ExecutionBurstCallback();
Michael Butler353a6242019-04-30 13:51:24 -0700106 sp<IBurstContext> context;
107 ASSERT_NO_FATAL_FAILURE(createBurst(preparedModel, callback, &sender, &receiver, &context,
108 resultChannelLength));
109 ASSERT_NE(nullptr, sender.get());
110 ASSERT_NE(nullptr, receiver.get());
111 ASSERT_NE(nullptr, context.get());
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700112
113 // return values
Michael Butler353a6242019-04-30 13:51:24 -0700114 *controller = std::make_shared<ExecutionBurstController>(std::move(sender), std::move(receiver),
115 context, callback);
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700116}
117
118// Primary validation function. This function will take a valid serialized
119// request, apply a mutation to it to invalidate the serialized request, then
120// pass it to interface calls that use the serialized request. Note that the
121// serialized request here is passed by value, and any mutation to the
122// serialized request does not leave this function.
123static void validate(RequestChannelSender* sender, ResultChannelReceiver* receiver,
124 const std::string& message, std::vector<FmqRequestDatum> serialized,
125 const std::function<void(std::vector<FmqRequestDatum>*)>& mutation) {
126 mutation(&serialized);
127
128 // skip if packet is too large to send
129 if (serialized.size() > kExecutionBurstChannelLength) {
130 return;
131 }
132
133 SCOPED_TRACE(message);
134
135 // send invalid packet
Michael Butler353a6242019-04-30 13:51:24 -0700136 ASSERT_TRUE(sender->sendPacket(serialized));
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700137
138 // receive error
139 auto results = receiver->getBlocking();
140 ASSERT_TRUE(results.has_value());
141 const auto [status, outputShapes, timing] = std::move(*results);
142 EXPECT_NE(ErrorStatus::NONE, status);
143 EXPECT_EQ(0u, outputShapes.size());
144 EXPECT_TRUE(badTiming(timing));
145}
146
Michael Butler353a6242019-04-30 13:51:24 -0700147// For validation, valid packet entries are mutated to invalid packet entries,
148// or invalid packet entries are inserted into valid packets. This function
149// creates pre-set invalid packet entries for convenience.
150static std::vector<FmqRequestDatum> createBadRequestPacketEntries() {
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700151 const FmqRequestDatum::PacketInformation packetInformation = {
152 /*.packetSize=*/10, /*.numberOfInputOperands=*/10, /*.numberOfOutputOperands=*/10,
153 /*.numberOfPools=*/10};
154 const FmqRequestDatum::OperandInformation operandInformation = {
155 /*.hasNoValue=*/false, /*.location=*/{}, /*.numberOfDimensions=*/10};
156 const int32_t invalidPoolIdentifier = std::numeric_limits<int32_t>::max();
Michael Butler353a6242019-04-30 13:51:24 -0700157 std::vector<FmqRequestDatum> bad(7);
158 bad[0].packetInformation(packetInformation);
159 bad[1].inputOperandInformation(operandInformation);
160 bad[2].inputOperandDimensionValue(0);
161 bad[3].outputOperandInformation(operandInformation);
162 bad[4].outputOperandDimensionValue(0);
163 bad[5].poolIdentifier(invalidPoolIdentifier);
164 bad[6].measureTiming(MeasureTiming::YES);
165 return bad;
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700166}
167
Michael Butler353a6242019-04-30 13:51:24 -0700168// For validation, valid packet entries are mutated to invalid packet entries,
169// or invalid packet entries are inserted into valid packets. This function
170// retrieves pre-set invalid packet entries for convenience. This function
171// caches these data so they can be reused on subsequent validation checks.
172static const std::vector<FmqRequestDatum>& getBadRequestPacketEntries() {
173 static const std::vector<FmqRequestDatum> bad = createBadRequestPacketEntries();
174 return bad;
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700175}
176
177///////////////////////// REMOVE DATUM ////////////////////////////////////
178
179static void removeDatumTest(RequestChannelSender* sender, ResultChannelReceiver* receiver,
180 const std::vector<FmqRequestDatum>& serialized) {
181 for (size_t index = 0; index < serialized.size(); ++index) {
182 const std::string message = "removeDatum: removed datum at index " + std::to_string(index);
183 validate(sender, receiver, message, serialized,
184 [index](std::vector<FmqRequestDatum>* serialized) {
185 serialized->erase(serialized->begin() + index);
186 });
187 }
188}
189
190///////////////////////// ADD DATUM ////////////////////////////////////
191
192static void addDatumTest(RequestChannelSender* sender, ResultChannelReceiver* receiver,
193 const std::vector<FmqRequestDatum>& serialized) {
Michael Butler353a6242019-04-30 13:51:24 -0700194 const std::vector<FmqRequestDatum>& extra = getBadRequestPacketEntries();
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700195 for (size_t index = 0; index <= serialized.size(); ++index) {
196 for (size_t type = 0; type < extra.size(); ++type) {
197 const std::string message = "addDatum: added datum type " + std::to_string(type) +
198 " at index " + std::to_string(index);
199 validate(sender, receiver, message, serialized,
200 [index, type, &extra](std::vector<FmqRequestDatum>* serialized) {
201 serialized->insert(serialized->begin() + index, extra[type]);
202 });
203 }
204 }
205}
206
207///////////////////////// MUTATE DATUM ////////////////////////////////////
208
209static bool interestingCase(const FmqRequestDatum& lhs, const FmqRequestDatum& rhs) {
210 using Discriminator = FmqRequestDatum::hidl_discriminator;
211
212 const bool differentValues = (lhs != rhs);
Michael Butler353a6242019-04-30 13:51:24 -0700213 const bool sameDiscriminator = (lhs.getDiscriminator() == rhs.getDiscriminator());
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700214 const auto discriminator = rhs.getDiscriminator();
215 const bool isDimensionValue = (discriminator == Discriminator::inputOperandDimensionValue ||
216 discriminator == Discriminator::outputOperandDimensionValue);
217
Michael Butler353a6242019-04-30 13:51:24 -0700218 return differentValues && !(sameDiscriminator && isDimensionValue);
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700219}
220
221static void mutateDatumTest(RequestChannelSender* sender, ResultChannelReceiver* receiver,
222 const std::vector<FmqRequestDatum>& serialized) {
Michael Butler353a6242019-04-30 13:51:24 -0700223 const std::vector<FmqRequestDatum>& change = getBadRequestPacketEntries();
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700224 for (size_t index = 0; index < serialized.size(); ++index) {
225 for (size_t type = 0; type < change.size(); ++type) {
226 if (interestingCase(serialized[index], change[type])) {
227 const std::string message = "mutateDatum: changed datum at index " +
228 std::to_string(index) + " to datum type " +
229 std::to_string(type);
230 validate(sender, receiver, message, serialized,
231 [index, type, &change](std::vector<FmqRequestDatum>* serialized) {
232 (*serialized)[index] = change[type];
233 });
234 }
235 }
236 }
237}
238
239///////////////////////// BURST VALIATION TESTS ////////////////////////////////////
240
241static void validateBurstSerialization(const sp<IPreparedModel>& preparedModel,
Xusong Wangead950d2019-08-09 16:45:24 -0700242 const Request& request) {
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700243 // create burst
244 std::unique_ptr<RequestChannelSender> sender;
245 std::unique_ptr<ResultChannelReceiver> receiver;
246 sp<ExecutionBurstCallback> callback = new ExecutionBurstCallback();
247 sp<IBurstContext> context;
248 ASSERT_NO_FATAL_FAILURE(createBurst(preparedModel, callback, &sender, &receiver, &context));
249 ASSERT_NE(nullptr, sender.get());
250 ASSERT_NE(nullptr, receiver.get());
251 ASSERT_NE(nullptr, context.get());
252
Xusong Wangead950d2019-08-09 16:45:24 -0700253 // load memory into callback slots
254 std::vector<intptr_t> keys;
255 keys.reserve(request.pools.size());
256 std::transform(request.pools.begin(), request.pools.end(), std::back_inserter(keys),
257 [](const auto& pool) { return reinterpret_cast<intptr_t>(&pool); });
258 const std::vector<int32_t> slots = callback->getSlots(request.pools, keys);
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700259
Xusong Wangead950d2019-08-09 16:45:24 -0700260 // ensure slot std::numeric_limits<int32_t>::max() doesn't exist (for
261 // subsequent slot validation testing)
262 ASSERT_TRUE(std::all_of(slots.begin(), slots.end(), [](int32_t slot) {
263 return slot != std::numeric_limits<int32_t>::max();
264 }));
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700265
Xusong Wangead950d2019-08-09 16:45:24 -0700266 // serialize the request
267 const auto serialized = ::android::nn::serialize(request, MeasureTiming::YES, slots);
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700268
Xusong Wangead950d2019-08-09 16:45:24 -0700269 // validations
270 removeDatumTest(sender.get(), receiver.get(), serialized);
271 addDatumTest(sender.get(), receiver.get(), serialized);
272 mutateDatumTest(sender.get(), receiver.get(), serialized);
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700273}
274
Michael Butler353a6242019-04-30 13:51:24 -0700275// This test validates that when the Result message size exceeds length of the
276// result FMQ, the service instance gracefully fails and returns an error.
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700277static void validateBurstFmqLength(const sp<IPreparedModel>& preparedModel,
Xusong Wangead950d2019-08-09 16:45:24 -0700278 const Request& request) {
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700279 // create regular burst
280 std::shared_ptr<ExecutionBurstController> controllerRegular;
Michael Butler353a6242019-04-30 13:51:24 -0700281 ASSERT_NO_FATAL_FAILURE(createBurstWithResultChannelLength(
282 preparedModel, kExecutionBurstChannelLength, &controllerRegular));
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700283 ASSERT_NE(nullptr, controllerRegular.get());
284
285 // create burst with small output channel
286 std::shared_ptr<ExecutionBurstController> controllerSmall;
Michael Butler353a6242019-04-30 13:51:24 -0700287 ASSERT_NO_FATAL_FAILURE(createBurstWithResultChannelLength(
288 preparedModel, kExecutionBurstChannelSmallLength, &controllerSmall));
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700289 ASSERT_NE(nullptr, controllerSmall.get());
290
Xusong Wangead950d2019-08-09 16:45:24 -0700291 // load memory into callback slots
292 std::vector<intptr_t> keys(request.pools.size());
293 for (size_t i = 0; i < keys.size(); ++i) {
294 keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]);
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700295 }
Xusong Wangead950d2019-08-09 16:45:24 -0700296
297 // collect serialized result by running regular burst
298 const auto [statusRegular, outputShapesRegular, timingRegular] =
299 controllerRegular->compute(request, MeasureTiming::NO, keys);
300
301 // skip test if regular burst output isn't useful for testing a failure
302 // caused by having too small of a length for the result FMQ
303 const std::vector<FmqResultDatum> serialized =
304 ::android::nn::serialize(statusRegular, outputShapesRegular, timingRegular);
305 if (statusRegular != ErrorStatus::NONE ||
306 serialized.size() <= kExecutionBurstChannelSmallLength) {
307 return;
308 }
309
310 // by this point, execution should fail because the result channel isn't
311 // large enough to return the serialized result
312 const auto [statusSmall, outputShapesSmall, timingSmall] =
313 controllerSmall->compute(request, MeasureTiming::NO, keys);
314 EXPECT_NE(ErrorStatus::NONE, statusSmall);
315 EXPECT_EQ(0u, outputShapesSmall.size());
316 EXPECT_TRUE(badTiming(timingSmall));
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700317}
318
319///////////////////////////// ENTRY POINT //////////////////////////////////
320
321void ValidationTest::validateBurst(const sp<IPreparedModel>& preparedModel,
Xusong Wangead950d2019-08-09 16:45:24 -0700322 const Request& request) {
323 ASSERT_NO_FATAL_FAILURE(validateBurstSerialization(preparedModel, request));
324 ASSERT_NO_FATAL_FAILURE(validateBurstFmqLength(preparedModel, request));
Michael Butlerd6e38fd2019-04-26 17:46:08 -0700325}
326
327} // namespace functional
328} // namespace vts
329} // namespace V1_2
330} // namespace neuralnetworks
331} // namespace hardware
332} // namespace android