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