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