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