Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [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 | |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 19 | #include <android-base/logging.h> |
| 20 | #include <android/hidl/memory/1.0/IMemory.h> |
| 21 | #include <hidlmemory/mapping.h> |
| 22 | |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 23 | #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 Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace hardware { |
| 33 | namespace neuralnetworks { |
| 34 | namespace V1_2 { |
| 35 | namespace vts { |
| 36 | namespace functional { |
| 37 | |
Michael Butler | 3835f61 | 2019-07-11 15:43:22 -0700 | [diff] [blame] | 38 | using ::android::hardware::neuralnetworks::V1_0::RequestArgument; |
Xusong Wang | 1a06e77 | 2018-10-31 08:43:12 -0700 | [diff] [blame] | 39 | using ::android::hardware::neuralnetworks::V1_2::implementation::ExecutionCallback; |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 40 | using ::android::hidl::memory::V1_0::IMemory; |
| 41 | using test_helper::for_all; |
| 42 | using test_helper::MixedTyped; |
Michael K. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 43 | using test_helper::MixedTypedExample; |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 44 | |
| 45 | ///////////////////////// UTILITY FUNCTIONS ///////////////////////// |
| 46 | |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 47 | static bool badTiming(Timing timing) { |
| 48 | return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX; |
| 49 | } |
| 50 | |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 51 | // 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. |
| 55 | static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message, |
| 56 | Request request, const std::function<void(Request*)>& mutation) { |
| 57 | mutation(&request); |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 58 | |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 59 | // 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 Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame] | 69 | // asynchronous |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 70 | { |
| 71 | SCOPED_TRACE(message + " [execute_1_2]"); |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 72 | |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 73 | sp<ExecutionCallback> executionCallback = new ExecutionCallback(); |
| 74 | ASSERT_NE(nullptr, executionCallback.get()); |
| 75 | Return<ErrorStatus> executeLaunchStatus = |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 76 | preparedModel->execute_1_2(request, measure, executionCallback); |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 77 | ASSERT_TRUE(executeLaunchStatus.isOk()); |
| 78 | ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus)); |
| 79 | |
| 80 | executionCallback->wait(); |
| 81 | ErrorStatus executionReturnStatus = executionCallback->getStatus(); |
Xusong Wang | b50bc31 | 2018-11-07 09:33:59 -0800 | [diff] [blame] | 82 | const auto& outputShapes = executionCallback->getOutputShapes(); |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 83 | Timing timing = executionCallback->getTiming(); |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 84 | ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus); |
Xusong Wang | b50bc31 | 2018-11-07 09:33:59 -0800 | [diff] [blame] | 85 | ASSERT_EQ(outputShapes.size(), 0); |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 86 | ASSERT_TRUE(badTiming(timing)); |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame] | 89 | // synchronous |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 90 | { |
| 91 | SCOPED_TRACE(message + " [executeSynchronously]"); |
| 92 | |
Xusong Wang | b50bc31 | 2018-11-07 09:33:59 -0800 | [diff] [blame] | 93 | Return<void> executeStatus = preparedModel->executeSynchronously( |
David Gross | 55a3d32 | 2019-01-23 14:01:52 -0800 | [diff] [blame] | 94 | 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 Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 101 | ASSERT_TRUE(executeStatus.isOk()); |
David Gross | 4592ed1 | 2018-12-21 11:20:26 -0800 | [diff] [blame] | 102 | } |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame] | 103 | |
| 104 | // burst |
| 105 | { |
| 106 | SCOPED_TRACE(message + " [burst]"); |
| 107 | |
| 108 | // create burst |
Michael Butler | 51c7218 | 2019-03-27 10:59:25 -0700 | [diff] [blame] | 109 | std::shared_ptr<::android::nn::ExecutionBurstController> burst = |
Michael Butler | 60a2253 | 2019-03-28 13:41:14 -0700 | [diff] [blame] | 110 | ::android::nn::ExecutionBurstController::create(preparedModel, /*blocking=*/true); |
Michael Butler | 29471a8 | 2019-01-15 11:02:55 -0800 | [diff] [blame] | 111 | 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 Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 140 | } |
| 141 | |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 142 | ///////////////////////// REMOVE INPUT //////////////////////////////////// |
| 143 | |
| 144 | static 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 | |
| 154 | static 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. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 164 | std::vector<Request> createRequests(const std::vector<MixedTypedExample>& examples) { |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 165 | 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. Sanders | da3bdbc | 2018-10-19 14:39:09 +0100 | [diff] [blame] | 171 | const MixedTyped& inputs = example.operands.first; |
| 172 | const MixedTyped& outputs = example.operands.second; |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 173 | |
| 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 Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 182 | .location = {.poolIndex = INPUT, |
| 183 | .offset = 0, |
| 184 | .length = static_cast<uint32_t>(s)}, |
| 185 | .dimensions = {}, |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 186 | }; |
| 187 | RequestArgument arg_empty = { |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 188 | .hasNoValue = true, |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 189 | }; |
| 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 Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 206 | .location = {.poolIndex = OUTPUT, |
| 207 | .offset = 0, |
| 208 | .length = static_cast<uint32_t>(s)}, |
| 209 | .dimensions = {}, |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 210 | }; |
| 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 Butler | d6e38fd | 2019-04-26 17:46:08 -0700 | [diff] [blame] | 254 | void ValidationTest::validateRequests(const sp<IPreparedModel>& preparedModel, |
| 255 | const std::vector<Request>& requests) { |
Slava Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 256 | // validate each request |
| 257 | for (const Request& request : requests) { |
| 258 | removeInputTest(preparedModel, request); |
| 259 | removeOutputTest(preparedModel, request); |
| 260 | } |
| 261 | } |
| 262 | |
Slava Shklyaev | 95a5978 | 2019-05-10 16:08:30 +0100 | [diff] [blame] | 263 | void 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 Shklyaev | feb87a9 | 2018-09-12 14:52:02 +0100 | [diff] [blame] | 279 | } // namespace functional |
| 280 | } // namespace vts |
| 281 | } // namespace V1_2 |
| 282 | } // namespace neuralnetworks |
| 283 | } // namespace hardware |
| 284 | } // namespace android |