Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +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 | |
Michael Butler | 5756887 | 2019-07-25 17:22:11 -0700 | [diff] [blame] | 19 | #include <chrono> |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 20 | #include "1.0/Utils.h" |
Michael Butler | 9449a28 | 2019-12-11 19:08:08 -0800 | [diff] [blame] | 21 | #include "1.3/Callbacks.h" |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 22 | #include "ExecutionBurstController.h" |
| 23 | #include "GeneratedTestHarness.h" |
| 24 | #include "TestHarness.h" |
| 25 | #include "Utils.h" |
| 26 | #include "VtsHalNeuralnetworks.h" |
| 27 | |
Lev Proleev | b49dadf | 2019-08-30 11:57:18 +0100 | [diff] [blame] | 28 | namespace android::hardware::neuralnetworks::V1_3::vts::functional { |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 29 | |
Michael Butler | 9449a28 | 2019-12-11 19:08:08 -0800 | [diff] [blame] | 30 | using implementation::ExecutionCallback; |
Lev Proleev | b49dadf | 2019-08-30 11:57:18 +0100 | [diff] [blame] | 31 | using V1_2::MeasureTiming; |
| 32 | using V1_2::OutputShape; |
| 33 | using V1_2::Timing; |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 34 | |
| 35 | ///////////////////////// UTILITY FUNCTIONS ///////////////////////// |
| 36 | |
| 37 | static bool badTiming(Timing timing) { |
| 38 | return timing.timeOnDevice == UINT64_MAX && timing.timeInDriver == UINT64_MAX; |
| 39 | } |
| 40 | |
| 41 | // Primary validation function. This function will take a valid request, apply a |
| 42 | // mutation to it to invalidate the request, then pass it to interface calls |
| 43 | // that use the request. Note that the request here is passed by value, and any |
| 44 | // mutation to the request does not leave this function. |
| 45 | static void validate(const sp<IPreparedModel>& preparedModel, const std::string& message, |
Michael Butler | 616701d | 2020-01-07 14:52:44 -0800 | [diff] [blame^] | 46 | Request request, const std::function<void(Request*)>& mutation, |
| 47 | bool testDeadline = false) { |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 48 | mutation(&request); |
| 49 | |
| 50 | // We'd like to test both with timing requested and without timing |
| 51 | // requested. Rather than running each test both ways, we'll decide whether |
| 52 | // to request timing by hashing the message. We do not use std::hash because |
| 53 | // it is not guaranteed stable across executions. |
| 54 | char hash = 0; |
| 55 | for (auto c : message) { |
| 56 | hash ^= c; |
| 57 | }; |
| 58 | MeasureTiming measure = (hash & 1) ? MeasureTiming::YES : MeasureTiming::NO; |
| 59 | |
Michael Butler | 616701d | 2020-01-07 14:52:44 -0800 | [diff] [blame^] | 60 | OptionalTimePoint deadline; |
| 61 | if (testDeadline) { |
| 62 | deadline.nanoseconds(std::numeric_limits<uint64_t>::max()); |
| 63 | } |
| 64 | |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 65 | // asynchronous |
| 66 | { |
Xusong Wang | 62a760c | 2019-10-25 12:07:17 -0700 | [diff] [blame] | 67 | SCOPED_TRACE(message + " [execute_1_3]"); |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 68 | |
| 69 | sp<ExecutionCallback> executionCallback = new ExecutionCallback(); |
| 70 | Return<ErrorStatus> executeLaunchStatus = |
Michael Butler | 616701d | 2020-01-07 14:52:44 -0800 | [diff] [blame^] | 71 | preparedModel->execute_1_3(request, measure, deadline, executionCallback); |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 72 | ASSERT_TRUE(executeLaunchStatus.isOk()); |
| 73 | ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, static_cast<ErrorStatus>(executeLaunchStatus)); |
| 74 | |
| 75 | executionCallback->wait(); |
| 76 | ErrorStatus executionReturnStatus = executionCallback->getStatus(); |
| 77 | const auto& outputShapes = executionCallback->getOutputShapes(); |
| 78 | Timing timing = executionCallback->getTiming(); |
| 79 | ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionReturnStatus); |
| 80 | ASSERT_EQ(outputShapes.size(), 0); |
| 81 | ASSERT_TRUE(badTiming(timing)); |
| 82 | } |
| 83 | |
| 84 | // synchronous |
| 85 | { |
Xusong Wang | ebd88ba | 2019-10-28 11:11:19 -0700 | [diff] [blame] | 86 | SCOPED_TRACE(message + " [executeSynchronously_1_3]"); |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 87 | |
Xusong Wang | ebd88ba | 2019-10-28 11:11:19 -0700 | [diff] [blame] | 88 | Return<void> executeStatus = preparedModel->executeSynchronously_1_3( |
Michael Butler | 616701d | 2020-01-07 14:52:44 -0800 | [diff] [blame^] | 89 | request, measure, deadline, |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 90 | [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes, |
| 91 | const Timing& timing) { |
| 92 | ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, error); |
| 93 | EXPECT_EQ(outputShapes.size(), 0); |
| 94 | EXPECT_TRUE(badTiming(timing)); |
| 95 | }); |
| 96 | ASSERT_TRUE(executeStatus.isOk()); |
| 97 | } |
| 98 | |
| 99 | // burst |
Xusong Wang | 931d5a1 | 2019-11-27 12:46:48 -0800 | [diff] [blame] | 100 | // TODO(butlermichael): Check if we need to test burst in V1_3 if the interface remains V1_2. |
Michael Butler | 616701d | 2020-01-07 14:52:44 -0800 | [diff] [blame^] | 101 | if (!testDeadline) { |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 102 | SCOPED_TRACE(message + " [burst]"); |
| 103 | |
Xusong Wang | 931d5a1 | 2019-11-27 12:46:48 -0800 | [diff] [blame] | 104 | ASSERT_TRUE(nn::compliantWithV1_0(request)); |
| 105 | V1_0::Request request10 = nn::convertToV1_0(request); |
| 106 | |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 107 | // create burst |
| 108 | std::shared_ptr<::android::nn::ExecutionBurstController> burst = |
Michael Butler | 5756887 | 2019-07-25 17:22:11 -0700 | [diff] [blame] | 109 | android::nn::ExecutionBurstController::create(preparedModel, |
| 110 | std::chrono::microseconds{0}); |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 111 | ASSERT_NE(nullptr, burst.get()); |
| 112 | |
| 113 | // create memory keys |
Xusong Wang | 931d5a1 | 2019-11-27 12:46:48 -0800 | [diff] [blame] | 114 | std::vector<intptr_t> keys(request10.pools.size()); |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 115 | for (size_t i = 0; i < keys.size(); ++i) { |
Xusong Wang | 931d5a1 | 2019-11-27 12:46:48 -0800 | [diff] [blame] | 116 | keys[i] = reinterpret_cast<intptr_t>(&request10.pools[i]); |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // execute and verify |
Xusong Wang | 931d5a1 | 2019-11-27 12:46:48 -0800 | [diff] [blame] | 120 | const auto [n, outputShapes, timing, fallback] = burst->compute(request10, measure, keys); |
Michael Butler | 5756887 | 2019-07-25 17:22:11 -0700 | [diff] [blame] | 121 | const ErrorStatus status = nn::convertResultCodeToErrorStatus(n); |
| 122 | EXPECT_EQ(ErrorStatus::INVALID_ARGUMENT, status); |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 123 | EXPECT_EQ(outputShapes.size(), 0); |
| 124 | EXPECT_TRUE(badTiming(timing)); |
Michael Butler | 5756887 | 2019-07-25 17:22:11 -0700 | [diff] [blame] | 125 | EXPECT_FALSE(fallback); |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 126 | |
| 127 | // additional burst testing |
Xusong Wang | 931d5a1 | 2019-11-27 12:46:48 -0800 | [diff] [blame] | 128 | if (request10.pools.size() > 0) { |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 129 | // valid free |
| 130 | burst->freeMemory(keys.front()); |
| 131 | |
| 132 | // negative test: invalid free of unknown (blank) memory |
| 133 | burst->freeMemory(intptr_t{}); |
| 134 | |
| 135 | // negative test: double free of memory |
| 136 | burst->freeMemory(keys.front()); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | ///////////////////////// REMOVE INPUT //////////////////////////////////// |
| 142 | |
| 143 | static void removeInputTest(const sp<IPreparedModel>& preparedModel, const Request& request) { |
| 144 | for (size_t input = 0; input < request.inputs.size(); ++input) { |
| 145 | const std::string message = "removeInput: removed input " + std::to_string(input); |
| 146 | validate(preparedModel, message, request, |
| 147 | [input](Request* request) { hidl_vec_removeAt(&request->inputs, input); }); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | ///////////////////////// REMOVE OUTPUT //////////////////////////////////// |
| 152 | |
| 153 | static void removeOutputTest(const sp<IPreparedModel>& preparedModel, const Request& request) { |
| 154 | for (size_t output = 0; output < request.outputs.size(); ++output) { |
| 155 | const std::string message = "removeOutput: removed Output " + std::to_string(output); |
| 156 | validate(preparedModel, message, request, |
| 157 | [output](Request* request) { hidl_vec_removeAt(&request->outputs, output); }); |
| 158 | } |
| 159 | } |
| 160 | |
Michael Butler | 616701d | 2020-01-07 14:52:44 -0800 | [diff] [blame^] | 161 | ///////////////////////// DEADLINE //////////////////////////////////// |
| 162 | |
| 163 | static void deadlineTest(const sp<IPreparedModel>& preparedModel, const Request& request) { |
| 164 | const std::string message = "deadlineTest: deadline not supported"; |
| 165 | const auto noop = [](Request*) {}; |
| 166 | validate(preparedModel, message, request, noop, /*testDeadline=*/true); |
| 167 | } |
| 168 | |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 169 | ///////////////////////////// ENTRY POINT ////////////////////////////////// |
| 170 | |
Michael Butler | 616701d | 2020-01-07 14:52:44 -0800 | [diff] [blame^] | 171 | void validateRequest(const sp<IPreparedModel>& preparedModel, const Request& request, |
| 172 | bool executionDeadlineSupported) { |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 173 | removeInputTest(preparedModel, request); |
| 174 | removeOutputTest(preparedModel, request); |
Michael Butler | 616701d | 2020-01-07 14:52:44 -0800 | [diff] [blame^] | 175 | if (!executionDeadlineSupported) { |
| 176 | deadlineTest(preparedModel, request); |
| 177 | } |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void validateRequestFailure(const sp<IPreparedModel>& preparedModel, const Request& request) { |
Xusong Wang | ebd88ba | 2019-10-28 11:11:19 -0700 | [diff] [blame] | 181 | SCOPED_TRACE("Expecting request to fail [executeSynchronously_1_3]"); |
| 182 | Return<void> executeStatus = preparedModel->executeSynchronously_1_3( |
Michael Butler | 9449a28 | 2019-12-11 19:08:08 -0800 | [diff] [blame] | 183 | request, MeasureTiming::NO, {}, |
Lev Proleev | 3b13b55 | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 184 | [](ErrorStatus error, const hidl_vec<OutputShape>& outputShapes, const Timing& timing) { |
| 185 | ASSERT_NE(ErrorStatus::NONE, error); |
| 186 | EXPECT_EQ(outputShapes.size(), 0); |
| 187 | EXPECT_TRUE(badTiming(timing)); |
| 188 | }); |
| 189 | ASSERT_TRUE(executeStatus.isOk()); |
| 190 | } |
| 191 | |
Lev Proleev | b49dadf | 2019-08-30 11:57:18 +0100 | [diff] [blame] | 192 | } // namespace android::hardware::neuralnetworks::V1_3::vts::functional |