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