Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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_aidl_hal_test" |
| 18 | |
Michael Butler | 8b7e813 | 2021-03-10 22:51:53 -0800 | [diff] [blame] | 19 | #include <aidl/android/hardware/neuralnetworks/RequestMemoryPool.h> |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 20 | #include <android/binder_auto_utils.h> |
Michael Butler | 8b7e813 | 2021-03-10 22:51:53 -0800 | [diff] [blame] | 21 | #include <variant> |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 22 | |
| 23 | #include <chrono> |
| 24 | |
| 25 | #include <TestHarness.h> |
| 26 | #include <nnapi/hal/aidl/Utils.h> |
| 27 | |
| 28 | #include "Callbacks.h" |
| 29 | #include "GeneratedTestHarness.h" |
| 30 | #include "Utils.h" |
| 31 | #include "VtsHalNeuralnetworks.h" |
| 32 | |
| 33 | namespace aidl::android::hardware::neuralnetworks::vts::functional { |
| 34 | |
| 35 | using ExecutionMutation = std::function<void(Request*)>; |
| 36 | |
| 37 | ///////////////////////// UTILITY FUNCTIONS ///////////////////////// |
| 38 | |
Xusong Wang | 8592008 | 2022-01-11 14:25:55 -0800 | [diff] [blame] | 39 | // Test request validation with reusable execution. |
| 40 | static void validateReusableExecution(const std::shared_ptr<IPreparedModel>& preparedModel, |
| 41 | const std::string& message, const Request& request, |
| 42 | bool measure) { |
| 43 | // createReusableExecution |
| 44 | std::shared_ptr<IExecution> execution; |
| 45 | { |
| 46 | SCOPED_TRACE(message + " [createReusableExecution]"); |
| 47 | const auto createStatus = preparedModel->createReusableExecution( |
| 48 | request, measure, kOmittedTimeoutDuration, &execution); |
| 49 | if (!createStatus.isOk()) { |
| 50 | ASSERT_EQ(createStatus.getExceptionCode(), EX_SERVICE_SPECIFIC); |
| 51 | ASSERT_EQ(static_cast<ErrorStatus>(createStatus.getServiceSpecificError()), |
| 52 | ErrorStatus::INVALID_ARGUMENT); |
| 53 | ASSERT_EQ(nullptr, execution); |
| 54 | return; |
| 55 | } else { |
| 56 | ASSERT_NE(nullptr, execution); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // synchronous |
| 61 | { |
| 62 | SCOPED_TRACE(message + " [executeSynchronously]"); |
| 63 | ExecutionResult executionResult; |
| 64 | const auto executeStatus = execution->executeSynchronously(kNoDeadline, &executionResult); |
| 65 | ASSERT_FALSE(executeStatus.isOk()); |
| 66 | ASSERT_EQ(executeStatus.getExceptionCode(), EX_SERVICE_SPECIFIC); |
| 67 | ASSERT_EQ(static_cast<ErrorStatus>(executeStatus.getServiceSpecificError()), |
| 68 | ErrorStatus::INVALID_ARGUMENT); |
| 69 | } |
| 70 | |
| 71 | // fenced |
| 72 | { |
| 73 | SCOPED_TRACE(message + " [executeFenced]"); |
| 74 | FencedExecutionResult executionResult; |
| 75 | const auto executeStatus = |
| 76 | execution->executeFenced({}, kNoDeadline, kNoDuration, &executionResult); |
| 77 | ASSERT_FALSE(executeStatus.isOk()); |
| 78 | ASSERT_EQ(executeStatus.getExceptionCode(), EX_SERVICE_SPECIFIC); |
| 79 | ASSERT_EQ(static_cast<ErrorStatus>(executeStatus.getServiceSpecificError()), |
| 80 | ErrorStatus::INVALID_ARGUMENT); |
| 81 | } |
| 82 | } |
| 83 | |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 84 | // Primary validation function. This function will take a valid request, apply a |
| 85 | // mutation to it to invalidate the request, then pass it to interface calls |
| 86 | // that use the request. |
| 87 | static void validate(const std::shared_ptr<IPreparedModel>& preparedModel, |
| 88 | const std::string& message, const Request& originalRequest, |
| 89 | const ExecutionMutation& mutate) { |
| 90 | Request request = utils::clone(originalRequest).value(); |
| 91 | mutate(&request); |
| 92 | |
| 93 | // We'd like to test both with timing requested and without timing |
| 94 | // requested. Rather than running each test both ways, we'll decide whether |
| 95 | // to request timing by hashing the message. We do not use std::hash because |
| 96 | // it is not guaranteed stable across executions. |
| 97 | char hash = 0; |
| 98 | for (auto c : message) { |
| 99 | hash ^= c; |
| 100 | }; |
| 101 | bool measure = (hash & 1); |
| 102 | |
| 103 | // synchronous |
| 104 | { |
| 105 | SCOPED_TRACE(message + " [executeSynchronously]"); |
| 106 | ExecutionResult executionResult; |
| 107 | const auto executeStatus = preparedModel->executeSynchronously( |
| 108 | request, measure, kNoDeadline, kOmittedTimeoutDuration, &executionResult); |
| 109 | ASSERT_FALSE(executeStatus.isOk()); |
| 110 | ASSERT_EQ(executeStatus.getExceptionCode(), EX_SERVICE_SPECIFIC); |
| 111 | ASSERT_EQ(static_cast<ErrorStatus>(executeStatus.getServiceSpecificError()), |
| 112 | ErrorStatus::INVALID_ARGUMENT); |
| 113 | } |
| 114 | |
| 115 | // fenced |
| 116 | { |
| 117 | SCOPED_TRACE(message + " [executeFenced]"); |
Jooyung Han | d33893f | 2021-02-26 17:09:23 +0900 | [diff] [blame] | 118 | FencedExecutionResult executionResult; |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 119 | const auto executeStatus = preparedModel->executeFenced(request, {}, false, kNoDeadline, |
| 120 | kOmittedTimeoutDuration, |
Jooyung Han | d33893f | 2021-02-26 17:09:23 +0900 | [diff] [blame] | 121 | kNoDuration, &executionResult); |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 122 | ASSERT_FALSE(executeStatus.isOk()); |
| 123 | ASSERT_EQ(executeStatus.getExceptionCode(), EX_SERVICE_SPECIFIC); |
| 124 | ASSERT_EQ(static_cast<ErrorStatus>(executeStatus.getServiceSpecificError()), |
| 125 | ErrorStatus::INVALID_ARGUMENT); |
| 126 | } |
Michael Butler | 8b7e813 | 2021-03-10 22:51:53 -0800 | [diff] [blame] | 127 | |
| 128 | // burst |
| 129 | { |
| 130 | SCOPED_TRACE(message + " [burst]"); |
| 131 | |
| 132 | // create burst |
| 133 | std::shared_ptr<IBurst> burst; |
| 134 | auto ret = preparedModel->configureExecutionBurst(&burst); |
| 135 | ASSERT_TRUE(ret.isOk()) << ret.getDescription(); |
| 136 | ASSERT_NE(nullptr, burst.get()); |
| 137 | |
| 138 | // use -1 for all memory identifier tokens |
| 139 | const std::vector<int64_t> slots(request.pools.size(), -1); |
| 140 | |
| 141 | ExecutionResult executionResult; |
| 142 | const auto executeStatus = burst->executeSynchronously( |
| 143 | request, slots, measure, kNoDeadline, kOmittedTimeoutDuration, &executionResult); |
| 144 | ASSERT_FALSE(executeStatus.isOk()); |
| 145 | ASSERT_EQ(executeStatus.getExceptionCode(), EX_SERVICE_SPECIFIC); |
| 146 | ASSERT_EQ(static_cast<ErrorStatus>(executeStatus.getServiceSpecificError()), |
| 147 | ErrorStatus::INVALID_ARGUMENT); |
| 148 | } |
Xusong Wang | 8592008 | 2022-01-11 14:25:55 -0800 | [diff] [blame] | 149 | |
| 150 | int32_t aidlVersion; |
| 151 | ASSERT_TRUE(preparedModel->getInterfaceVersion(&aidlVersion).isOk()); |
| 152 | |
| 153 | // validate reusable execution |
| 154 | if (aidlVersion >= kMinAidlLevelForFL8) { |
| 155 | validateReusableExecution(preparedModel, message, request, measure); |
| 156 | } |
Michael Butler | 8b7e813 | 2021-03-10 22:51:53 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | std::shared_ptr<IBurst> createBurst(const std::shared_ptr<IPreparedModel>& preparedModel) { |
| 160 | std::shared_ptr<IBurst> burst; |
| 161 | const auto ret = preparedModel->configureExecutionBurst(&burst); |
| 162 | if (!ret.isOk()) return nullptr; |
| 163 | return burst; |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | ///////////////////////// REMOVE INPUT //////////////////////////////////// |
| 167 | |
| 168 | static void removeInputTest(const std::shared_ptr<IPreparedModel>& preparedModel, |
| 169 | const Request& request) { |
| 170 | for (size_t input = 0; input < request.inputs.size(); ++input) { |
| 171 | const std::string message = "removeInput: removed input " + std::to_string(input); |
| 172 | validate(preparedModel, message, request, [input](Request* request) { |
| 173 | request->inputs.erase(request->inputs.begin() + input); |
| 174 | }); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | ///////////////////////// REMOVE OUTPUT //////////////////////////////////// |
| 179 | |
| 180 | static void removeOutputTest(const std::shared_ptr<IPreparedModel>& preparedModel, |
| 181 | const Request& request) { |
| 182 | for (size_t output = 0; output < request.outputs.size(); ++output) { |
| 183 | const std::string message = "removeOutput: removed Output " + std::to_string(output); |
| 184 | validate(preparedModel, message, request, [output](Request* request) { |
| 185 | request->outputs.erase(request->outputs.begin() + output); |
| 186 | }); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | ///////////////////////////// ENTRY POINT ////////////////////////////////// |
| 191 | |
| 192 | void validateRequest(const std::shared_ptr<IPreparedModel>& preparedModel, const Request& request) { |
| 193 | removeInputTest(preparedModel, request); |
| 194 | removeOutputTest(preparedModel, request); |
| 195 | } |
| 196 | |
Michael Butler | 8b7e813 | 2021-03-10 22:51:53 -0800 | [diff] [blame] | 197 | void validateBurst(const std::shared_ptr<IPreparedModel>& preparedModel, const Request& request) { |
| 198 | // create burst |
| 199 | std::shared_ptr<IBurst> burst; |
| 200 | auto ret = preparedModel->configureExecutionBurst(&burst); |
| 201 | ASSERT_TRUE(ret.isOk()) << ret.getDescription(); |
| 202 | ASSERT_NE(nullptr, burst.get()); |
| 203 | |
| 204 | const auto test = [&burst, &request](const std::vector<int64_t>& slots) { |
| 205 | ExecutionResult executionResult; |
| 206 | const auto executeStatus = |
| 207 | burst->executeSynchronously(request, slots, /*measure=*/false, kNoDeadline, |
| 208 | kOmittedTimeoutDuration, &executionResult); |
| 209 | ASSERT_FALSE(executeStatus.isOk()); |
| 210 | ASSERT_EQ(executeStatus.getExceptionCode(), EX_SERVICE_SPECIFIC); |
| 211 | ASSERT_EQ(static_cast<ErrorStatus>(executeStatus.getServiceSpecificError()), |
| 212 | ErrorStatus::INVALID_ARGUMENT); |
| 213 | }; |
| 214 | |
| 215 | int64_t currentSlot = 0; |
| 216 | std::vector<int64_t> slots; |
| 217 | slots.reserve(request.pools.size()); |
| 218 | for (const auto& pool : request.pools) { |
| 219 | if (pool.getTag() == RequestMemoryPool::Tag::pool) { |
| 220 | slots.push_back(currentSlot++); |
| 221 | } else { |
| 222 | slots.push_back(-1); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | constexpr int64_t invalidSlot = -2; |
| 227 | |
| 228 | // validate failure when invalid memory identifier token value |
| 229 | for (size_t i = 0; i < request.pools.size(); ++i) { |
| 230 | const int64_t oldSlotValue = slots[i]; |
| 231 | |
| 232 | slots[i] = invalidSlot; |
| 233 | test(slots); |
| 234 | |
| 235 | slots[i] = oldSlotValue; |
| 236 | } |
| 237 | |
| 238 | // validate failure when request.pools.size() != memoryIdentifierTokens.size() |
| 239 | if (request.pools.size() > 0) { |
| 240 | slots = std::vector<int64_t>(request.pools.size() - 1, -1); |
| 241 | test(slots); |
| 242 | } |
| 243 | |
| 244 | // validate failure when request.pools.size() != memoryIdentifierTokens.size() |
| 245 | slots = std::vector<int64_t>(request.pools.size() + 1, -1); |
| 246 | test(slots); |
| 247 | |
| 248 | // validate failure when invalid memory identifier token value |
| 249 | const auto freeStatus = burst->releaseMemoryResource(invalidSlot); |
| 250 | ASSERT_FALSE(freeStatus.isOk()); |
| 251 | ASSERT_EQ(freeStatus.getExceptionCode(), EX_SERVICE_SPECIFIC); |
| 252 | ASSERT_EQ(static_cast<ErrorStatus>(freeStatus.getServiceSpecificError()), |
| 253 | ErrorStatus::INVALID_ARGUMENT); |
| 254 | } |
| 255 | |
Lev Proleev | c185e88 | 2020-12-15 19:25:32 +0000 | [diff] [blame] | 256 | void validateRequestFailure(const std::shared_ptr<IPreparedModel>& preparedModel, |
| 257 | const Request& request) { |
| 258 | SCOPED_TRACE("Expecting request to fail [executeSynchronously]"); |
| 259 | ExecutionResult executionResult; |
| 260 | const auto executeStatus = preparedModel->executeSynchronously( |
| 261 | request, false, kNoDeadline, kOmittedTimeoutDuration, &executionResult); |
| 262 | |
| 263 | ASSERT_FALSE(executeStatus.isOk()); |
| 264 | ASSERT_EQ(executeStatus.getExceptionCode(), EX_SERVICE_SPECIFIC); |
| 265 | ASSERT_NE(static_cast<ErrorStatus>(executeStatus.getServiceSpecificError()), ErrorStatus::NONE); |
| 266 | } |
| 267 | |
| 268 | } // namespace aidl::android::hardware::neuralnetworks::vts::functional |