Michael Butler | 20f28a2 | 2019-04-26 17:46:08 -0700 | [diff] [blame] | 1 | /* |
| 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> |
| 28 | |
| 29 | namespace android { |
| 30 | namespace hardware { |
| 31 | namespace neuralnetworks { |
| 32 | namespace V1_2 { |
| 33 | namespace vts { |
| 34 | namespace functional { |
| 35 | |
| 36 | using ::android::nn::ExecutionBurstController; |
| 37 | using ::android::nn::RequestChannelSender; |
| 38 | using ::android::nn::ResultChannelReceiver; |
| 39 | using ExecutionBurstCallback = ::android::nn::ExecutionBurstController::ExecutionBurstCallback; |
| 40 | |
| 41 | constexpr size_t kExecutionBurstChannelLength = 1024; |
| 42 | constexpr size_t kExecutionBurstChannelSmallLength = 8; |
| 43 | |
| 44 | ///////////////////////// UTILITY FUNCTIONS ///////////////////////// |
| 45 | |
| 46 | static bool badTiming(Timing timing) { |
| 47 | return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX; |
| 48 | } |
| 49 | |
| 50 | static void createBurst(const sp<IPreparedModel>& preparedModel, const sp<IBurstCallback>& callback, |
| 51 | std::unique_ptr<RequestChannelSender>* sender, |
| 52 | std::unique_ptr<ResultChannelReceiver>* receiver, |
| 53 | sp<IBurstContext>* context) { |
| 54 | ASSERT_NE(nullptr, preparedModel.get()); |
| 55 | ASSERT_NE(nullptr, sender); |
| 56 | ASSERT_NE(nullptr, receiver); |
| 57 | ASSERT_NE(nullptr, context); |
| 58 | |
| 59 | // create FMQ objects |
| 60 | auto [fmqRequestChannel, fmqRequestDescriptor] = |
| 61 | RequestChannelSender::create(kExecutionBurstChannelLength, /*blocking=*/true); |
| 62 | auto [fmqResultChannel, fmqResultDescriptor] = |
| 63 | ResultChannelReceiver::create(kExecutionBurstChannelLength, /*blocking=*/true); |
| 64 | ASSERT_NE(nullptr, fmqRequestChannel.get()); |
| 65 | ASSERT_NE(nullptr, fmqResultChannel.get()); |
| 66 | ASSERT_NE(nullptr, fmqRequestDescriptor); |
| 67 | ASSERT_NE(nullptr, fmqResultDescriptor); |
| 68 | |
| 69 | // configure burst |
| 70 | ErrorStatus errorStatus; |
| 71 | sp<IBurstContext> burstContext; |
| 72 | const Return<void> ret = preparedModel->configureExecutionBurst( |
| 73 | callback, *fmqRequestDescriptor, *fmqResultDescriptor, |
| 74 | [&errorStatus, &burstContext](ErrorStatus status, const sp<IBurstContext>& context) { |
| 75 | errorStatus = status; |
| 76 | burstContext = context; |
| 77 | }); |
| 78 | ASSERT_TRUE(ret.isOk()); |
| 79 | ASSERT_EQ(ErrorStatus::NONE, errorStatus); |
| 80 | ASSERT_NE(nullptr, burstContext.get()); |
| 81 | |
| 82 | // return values |
| 83 | *sender = std::move(fmqRequestChannel); |
| 84 | *receiver = std::move(fmqResultChannel); |
| 85 | *context = burstContext; |
| 86 | } |
| 87 | |
| 88 | static void createBurstWithResultChannelLength( |
| 89 | const sp<IPreparedModel>& preparedModel, |
| 90 | std::shared_ptr<ExecutionBurstController>* controller, size_t resultChannelLength) { |
| 91 | ASSERT_NE(nullptr, preparedModel.get()); |
| 92 | ASSERT_NE(nullptr, controller); |
| 93 | |
| 94 | // create FMQ objects |
| 95 | auto [fmqRequestChannel, fmqRequestDescriptor] = |
| 96 | RequestChannelSender::create(kExecutionBurstChannelLength, /*blocking=*/true); |
| 97 | auto [fmqResultChannel, fmqResultDescriptor] = |
| 98 | ResultChannelReceiver::create(resultChannelLength, /*blocking=*/true); |
| 99 | ASSERT_NE(nullptr, fmqRequestChannel.get()); |
| 100 | ASSERT_NE(nullptr, fmqResultChannel.get()); |
| 101 | ASSERT_NE(nullptr, fmqRequestDescriptor); |
| 102 | ASSERT_NE(nullptr, fmqResultDescriptor); |
| 103 | |
| 104 | // configure burst |
| 105 | sp<ExecutionBurstCallback> callback = new ExecutionBurstCallback(); |
| 106 | ErrorStatus errorStatus; |
| 107 | sp<IBurstContext> burstContext; |
| 108 | const Return<void> ret = preparedModel->configureExecutionBurst( |
| 109 | callback, *fmqRequestDescriptor, *fmqResultDescriptor, |
| 110 | [&errorStatus, &burstContext](ErrorStatus status, const sp<IBurstContext>& context) { |
| 111 | errorStatus = status; |
| 112 | burstContext = context; |
| 113 | }); |
| 114 | ASSERT_TRUE(ret.isOk()); |
| 115 | ASSERT_EQ(ErrorStatus::NONE, errorStatus); |
| 116 | ASSERT_NE(nullptr, burstContext.get()); |
| 117 | |
| 118 | // return values |
| 119 | *controller = std::make_shared<ExecutionBurstController>( |
| 120 | std::move(fmqRequestChannel), std::move(fmqResultChannel), burstContext, callback); |
| 121 | } |
| 122 | |
| 123 | // Primary validation function. This function will take a valid serialized |
| 124 | // request, apply a mutation to it to invalidate the serialized request, then |
| 125 | // pass it to interface calls that use the serialized request. Note that the |
| 126 | // serialized request here is passed by value, and any mutation to the |
| 127 | // serialized request does not leave this function. |
| 128 | static void validate(RequestChannelSender* sender, ResultChannelReceiver* receiver, |
| 129 | const std::string& message, std::vector<FmqRequestDatum> serialized, |
| 130 | const std::function<void(std::vector<FmqRequestDatum>*)>& mutation) { |
| 131 | mutation(&serialized); |
| 132 | |
| 133 | // skip if packet is too large to send |
| 134 | if (serialized.size() > kExecutionBurstChannelLength) { |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | SCOPED_TRACE(message); |
| 139 | |
| 140 | // send invalid packet |
| 141 | sender->sendPacket(serialized); |
| 142 | |
| 143 | // receive error |
| 144 | auto results = receiver->getBlocking(); |
| 145 | ASSERT_TRUE(results.has_value()); |
| 146 | const auto [status, outputShapes, timing] = std::move(*results); |
| 147 | EXPECT_NE(ErrorStatus::NONE, status); |
| 148 | EXPECT_EQ(0u, outputShapes.size()); |
| 149 | EXPECT_TRUE(badTiming(timing)); |
| 150 | } |
| 151 | |
| 152 | static std::vector<FmqRequestDatum> createUniqueDatum() { |
| 153 | const FmqRequestDatum::PacketInformation packetInformation = { |
| 154 | /*.packetSize=*/10, /*.numberOfInputOperands=*/10, /*.numberOfOutputOperands=*/10, |
| 155 | /*.numberOfPools=*/10}; |
| 156 | const FmqRequestDatum::OperandInformation operandInformation = { |
| 157 | /*.hasNoValue=*/false, /*.location=*/{}, /*.numberOfDimensions=*/10}; |
| 158 | const int32_t invalidPoolIdentifier = std::numeric_limits<int32_t>::max(); |
| 159 | std::vector<FmqRequestDatum> unique(7); |
| 160 | unique[0].packetInformation(packetInformation); |
| 161 | unique[1].inputOperandInformation(operandInformation); |
| 162 | unique[2].inputOperandDimensionValue(0); |
| 163 | unique[3].outputOperandInformation(operandInformation); |
| 164 | unique[4].outputOperandDimensionValue(0); |
| 165 | unique[5].poolIdentifier(invalidPoolIdentifier); |
| 166 | unique[6].measureTiming(MeasureTiming::YES); |
| 167 | return unique; |
| 168 | } |
| 169 | |
| 170 | static const std::vector<FmqRequestDatum>& getUniqueDatum() { |
| 171 | static const std::vector<FmqRequestDatum> unique = createUniqueDatum(); |
| 172 | return unique; |
| 173 | } |
| 174 | |
| 175 | ///////////////////////// REMOVE DATUM //////////////////////////////////// |
| 176 | |
| 177 | static void removeDatumTest(RequestChannelSender* sender, ResultChannelReceiver* receiver, |
| 178 | const std::vector<FmqRequestDatum>& serialized) { |
| 179 | for (size_t index = 0; index < serialized.size(); ++index) { |
| 180 | const std::string message = "removeDatum: removed datum at index " + std::to_string(index); |
| 181 | validate(sender, receiver, message, serialized, |
| 182 | [index](std::vector<FmqRequestDatum>* serialized) { |
| 183 | serialized->erase(serialized->begin() + index); |
| 184 | }); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | ///////////////////////// ADD DATUM //////////////////////////////////// |
| 189 | |
| 190 | static void addDatumTest(RequestChannelSender* sender, ResultChannelReceiver* receiver, |
| 191 | const std::vector<FmqRequestDatum>& serialized) { |
| 192 | const std::vector<FmqRequestDatum>& extra = getUniqueDatum(); |
| 193 | for (size_t index = 0; index <= serialized.size(); ++index) { |
| 194 | for (size_t type = 0; type < extra.size(); ++type) { |
| 195 | const std::string message = "addDatum: added datum type " + std::to_string(type) + |
| 196 | " at index " + std::to_string(index); |
| 197 | validate(sender, receiver, message, serialized, |
| 198 | [index, type, &extra](std::vector<FmqRequestDatum>* serialized) { |
| 199 | serialized->insert(serialized->begin() + index, extra[type]); |
| 200 | }); |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | ///////////////////////// MUTATE DATUM //////////////////////////////////// |
| 206 | |
| 207 | static bool interestingCase(const FmqRequestDatum& lhs, const FmqRequestDatum& rhs) { |
| 208 | using Discriminator = FmqRequestDatum::hidl_discriminator; |
| 209 | |
| 210 | const bool differentValues = (lhs != rhs); |
| 211 | const bool sameSumType = (lhs.getDiscriminator() == rhs.getDiscriminator()); |
| 212 | const auto discriminator = rhs.getDiscriminator(); |
| 213 | const bool isDimensionValue = (discriminator == Discriminator::inputOperandDimensionValue || |
| 214 | discriminator == Discriminator::outputOperandDimensionValue); |
| 215 | |
| 216 | return differentValues && !(sameSumType && isDimensionValue); |
| 217 | } |
| 218 | |
| 219 | static void mutateDatumTest(RequestChannelSender* sender, ResultChannelReceiver* receiver, |
| 220 | const std::vector<FmqRequestDatum>& serialized) { |
| 221 | const std::vector<FmqRequestDatum>& change = getUniqueDatum(); |
| 222 | for (size_t index = 0; index < serialized.size(); ++index) { |
| 223 | for (size_t type = 0; type < change.size(); ++type) { |
| 224 | if (interestingCase(serialized[index], change[type])) { |
| 225 | const std::string message = "mutateDatum: changed datum at index " + |
| 226 | std::to_string(index) + " to datum type " + |
| 227 | std::to_string(type); |
| 228 | validate(sender, receiver, message, serialized, |
| 229 | [index, type, &change](std::vector<FmqRequestDatum>* serialized) { |
| 230 | (*serialized)[index] = change[type]; |
| 231 | }); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | ///////////////////////// BURST VALIATION TESTS //////////////////////////////////// |
| 238 | |
| 239 | static void validateBurstSerialization(const sp<IPreparedModel>& preparedModel, |
| 240 | const std::vector<Request>& requests) { |
| 241 | // create burst |
| 242 | std::unique_ptr<RequestChannelSender> sender; |
| 243 | std::unique_ptr<ResultChannelReceiver> receiver; |
| 244 | sp<ExecutionBurstCallback> callback = new ExecutionBurstCallback(); |
| 245 | sp<IBurstContext> context; |
| 246 | ASSERT_NO_FATAL_FAILURE(createBurst(preparedModel, callback, &sender, &receiver, &context)); |
| 247 | ASSERT_NE(nullptr, sender.get()); |
| 248 | ASSERT_NE(nullptr, receiver.get()); |
| 249 | ASSERT_NE(nullptr, context.get()); |
| 250 | |
| 251 | // validate each request |
| 252 | for (const Request& request : requests) { |
| 253 | // load memory into callback slots |
| 254 | std::vector<intptr_t> keys(request.pools.size()); |
| 255 | for (size_t i = 0; i < keys.size(); ++i) { |
| 256 | keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]); |
| 257 | } |
| 258 | const std::vector<int32_t> slots = callback->getSlots(request.pools, keys); |
| 259 | |
| 260 | // ensure slot std::numeric_limits<int32_t>::max() doesn't exist (for |
| 261 | // subsequent slot validation testing) |
| 262 | const auto maxElement = std::max_element(slots.begin(), slots.end()); |
| 263 | ASSERT_NE(slots.end(), maxElement); |
| 264 | ASSERT_NE(std::numeric_limits<int32_t>::max(), *maxElement); |
| 265 | |
| 266 | // serialize the request |
| 267 | const auto serialized = ::android::nn::serialize(request, MeasureTiming::YES, slots); |
| 268 | |
| 269 | // validations |
| 270 | removeDatumTest(sender.get(), receiver.get(), serialized); |
| 271 | addDatumTest(sender.get(), receiver.get(), serialized); |
| 272 | mutateDatumTest(sender.get(), receiver.get(), serialized); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | static void validateBurstFmqLength(const sp<IPreparedModel>& preparedModel, |
| 277 | const std::vector<Request>& requests) { |
| 278 | // create regular burst |
| 279 | std::shared_ptr<ExecutionBurstController> controllerRegular; |
| 280 | ASSERT_NO_FATAL_FAILURE(createBurstWithResultChannelLength(preparedModel, &controllerRegular, |
| 281 | kExecutionBurstChannelLength)); |
| 282 | ASSERT_NE(nullptr, controllerRegular.get()); |
| 283 | |
| 284 | // create burst with small output channel |
| 285 | std::shared_ptr<ExecutionBurstController> controllerSmall; |
| 286 | ASSERT_NO_FATAL_FAILURE(createBurstWithResultChannelLength(preparedModel, &controllerSmall, |
| 287 | kExecutionBurstChannelSmallLength)); |
| 288 | ASSERT_NE(nullptr, controllerSmall.get()); |
| 289 | |
| 290 | // validate each request |
| 291 | for (const Request& request : requests) { |
| 292 | // load memory into callback slots |
| 293 | std::vector<intptr_t> keys(request.pools.size()); |
| 294 | for (size_t i = 0; i < keys.size(); ++i) { |
| 295 | keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]); |
| 296 | } |
| 297 | |
| 298 | // collect serialized result by running regular burst |
| 299 | const auto [status1, outputShapes1, timing1] = |
| 300 | controllerRegular->compute(request, MeasureTiming::NO, keys); |
| 301 | |
| 302 | // skip test if synchronous output isn't useful |
| 303 | const std::vector<FmqResultDatum> serialized = |
| 304 | ::android::nn::serialize(status1, outputShapes1, timing1); |
| 305 | if (status1 != ErrorStatus::NONE || |
| 306 | serialized.size() <= kExecutionBurstChannelSmallLength) { |
| 307 | continue; |
| 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 [status2, outputShapes2, timing2] = |
| 313 | controllerSmall->compute(request, MeasureTiming::NO, keys); |
| 314 | EXPECT_NE(ErrorStatus::NONE, status2); |
| 315 | EXPECT_EQ(0u, outputShapes2.size()); |
| 316 | EXPECT_TRUE(badTiming(timing2)); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | ///////////////////////////// ENTRY POINT ////////////////////////////////// |
| 321 | |
| 322 | void ValidationTest::validateBurst(const sp<IPreparedModel>& preparedModel, |
| 323 | const std::vector<Request>& requests) { |
| 324 | ASSERT_NO_FATAL_FAILURE(validateBurstSerialization(preparedModel, requests)); |
| 325 | ASSERT_NO_FATAL_FAILURE(validateBurstFmqLength(preparedModel, requests)); |
| 326 | } |
| 327 | |
| 328 | } // namespace functional |
| 329 | } // namespace vts |
| 330 | } // namespace V1_2 |
| 331 | } // namespace neuralnetworks |
| 332 | } // namespace hardware |
| 333 | } // namespace android |