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