Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #include "GeneratedTestHarness.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android/hardware/neuralnetworks/1.0/IDevice.h> |
| 21 | #include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h> |
| 22 | #include <android/hardware/neuralnetworks/1.0/IPreparedModel.h> |
| 23 | #include <android/hardware/neuralnetworks/1.0/IPreparedModelCallback.h> |
| 24 | #include <android/hardware/neuralnetworks/1.0/types.h> |
| 25 | #include <android/hardware/neuralnetworks/1.1/IDevice.h> |
| 26 | #include <android/hardware/neuralnetworks/1.2/IDevice.h> |
| 27 | #include <android/hardware/neuralnetworks/1.2/IExecutionCallback.h> |
| 28 | #include <android/hardware/neuralnetworks/1.2/IPreparedModel.h> |
| 29 | #include <android/hardware/neuralnetworks/1.2/IPreparedModelCallback.h> |
| 30 | #include <android/hidl/allocator/1.0/IAllocator.h> |
| 31 | #include <android/hidl/memory/1.0/IMemory.h> |
Lev Proleev | 56cda83 | 2019-12-05 14:49:47 +0000 | [diff] [blame] | 32 | #include <gtest/gtest.h> |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 33 | #include <hidlmemory/mapping.h> |
| 34 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 35 | #include <algorithm> |
Michael Butler | 648ada5 | 2019-07-25 17:22:11 -0700 | [diff] [blame] | 36 | #include <chrono> |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 37 | #include <iostream> |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 38 | #include <numeric> |
Lev Proleev | 56cda83 | 2019-12-05 14:49:47 +0000 | [diff] [blame] | 39 | #include <vector> |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 40 | |
| 41 | #include "1.0/Utils.h" |
| 42 | #include "1.2/Callbacks.h" |
| 43 | #include "ExecutionBurstController.h" |
| 44 | #include "MemoryUtils.h" |
| 45 | #include "TestHarness.h" |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 46 | #include "VtsHalNeuralnetworks.h" |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 47 | |
Michael Butler | 62749b9 | 2019-08-26 23:55:47 -0700 | [diff] [blame] | 48 | namespace android::hardware::neuralnetworks::V1_2::vts::functional { |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 49 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 50 | using namespace test_helper; |
Michael Butler | 62749b9 | 2019-08-26 23:55:47 -0700 | [diff] [blame] | 51 | using hidl::memory::V1_0::IMemory; |
| 52 | using implementation::ExecutionCallback; |
| 53 | using implementation::PreparedModelCallback; |
| 54 | using V1_0::DataLocation; |
| 55 | using V1_0::ErrorStatus; |
| 56 | using V1_0::OperandLifeTime; |
| 57 | using V1_0::Request; |
| 58 | using V1_1::ExecutionPreference; |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 59 | using HidlToken = hidl_array<uint8_t, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)>; |
| 60 | |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 61 | namespace { |
| 62 | |
| 63 | enum class Executor { ASYNC, SYNC, BURST }; |
| 64 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 65 | enum class OutputType { FULLY_SPECIFIED, UNSPECIFIED, INSUFFICIENT }; |
| 66 | |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 67 | struct TestConfig { |
| 68 | Executor executor; |
| 69 | MeasureTiming measureTiming; |
| 70 | OutputType outputType; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 71 | MemoryType memoryType; |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | } // namespace |
| 75 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 76 | Model createModel(const TestModel& testModel) { |
| 77 | // Model operands. |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 78 | CHECK_EQ(testModel.referenced.size(), 0u); // Not supported in 1.1. |
| 79 | hidl_vec<Operand> operands(testModel.main.operands.size()); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 80 | size_t constCopySize = 0, constRefSize = 0; |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 81 | for (uint32_t i = 0; i < testModel.main.operands.size(); i++) { |
| 82 | const auto& op = testModel.main.operands[i]; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 83 | |
| 84 | DataLocation loc = {}; |
| 85 | if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) { |
| 86 | loc = {.poolIndex = 0, |
| 87 | .offset = static_cast<uint32_t>(constCopySize), |
| 88 | .length = static_cast<uint32_t>(op.data.size())}; |
| 89 | constCopySize += op.data.alignedSize(); |
| 90 | } else if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) { |
| 91 | loc = {.poolIndex = 0, |
| 92 | .offset = static_cast<uint32_t>(constRefSize), |
| 93 | .length = static_cast<uint32_t>(op.data.size())}; |
| 94 | constRefSize += op.data.alignedSize(); |
| 95 | } |
| 96 | |
| 97 | Operand::ExtraParams extraParams; |
| 98 | if (op.type == TestOperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) { |
| 99 | extraParams.channelQuant(SymmPerChannelQuantParams{ |
| 100 | .scales = op.channelQuant.scales, .channelDim = op.channelQuant.channelDim}); |
| 101 | } |
| 102 | |
| 103 | operands[i] = {.type = static_cast<OperandType>(op.type), |
| 104 | .dimensions = op.dimensions, |
| 105 | .numberOfConsumers = op.numberOfConsumers, |
| 106 | .scale = op.scale, |
| 107 | .zeroPoint = op.zeroPoint, |
| 108 | .lifetime = static_cast<OperandLifeTime>(op.lifetime), |
| 109 | .location = loc, |
| 110 | .extraParams = std::move(extraParams)}; |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 111 | } |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 112 | |
| 113 | // Model operations. |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 114 | hidl_vec<Operation> operations(testModel.main.operations.size()); |
| 115 | std::transform(testModel.main.operations.begin(), testModel.main.operations.end(), |
| 116 | operations.begin(), [](const TestOperation& op) -> Operation { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 117 | return {.type = static_cast<OperationType>(op.type), |
| 118 | .inputs = op.inputs, |
| 119 | .outputs = op.outputs}; |
| 120 | }); |
| 121 | |
| 122 | // Constant copies. |
| 123 | hidl_vec<uint8_t> operandValues(constCopySize); |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 124 | for (uint32_t i = 0; i < testModel.main.operands.size(); i++) { |
| 125 | const auto& op = testModel.main.operands[i]; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 126 | if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) { |
| 127 | const uint8_t* begin = op.data.get<uint8_t>(); |
| 128 | const uint8_t* end = begin + op.data.size(); |
| 129 | std::copy(begin, end, operandValues.data() + operands[i].location.offset); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Shared memory. |
| 134 | hidl_vec<hidl_memory> pools = {}; |
| 135 | if (constRefSize > 0) { |
| 136 | hidl_vec_push_back(&pools, nn::allocateSharedMemory(constRefSize)); |
| 137 | CHECK_NE(pools[0].size(), 0u); |
| 138 | |
| 139 | // load data |
| 140 | sp<IMemory> mappedMemory = mapMemory(pools[0]); |
| 141 | CHECK(mappedMemory.get() != nullptr); |
| 142 | uint8_t* mappedPtr = |
| 143 | reinterpret_cast<uint8_t*>(static_cast<void*>(mappedMemory->getPointer())); |
| 144 | CHECK(mappedPtr != nullptr); |
| 145 | |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 146 | for (uint32_t i = 0; i < testModel.main.operands.size(); i++) { |
| 147 | const auto& op = testModel.main.operands[i]; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 148 | if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) { |
| 149 | const uint8_t* begin = op.data.get<uint8_t>(); |
| 150 | const uint8_t* end = begin + op.data.size(); |
| 151 | std::copy(begin, end, mappedPtr + operands[i].location.offset); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | return {.operands = std::move(operands), |
| 157 | .operations = std::move(operations), |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 158 | .inputIndexes = testModel.main.inputIndexes, |
| 159 | .outputIndexes = testModel.main.outputIndexes, |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 160 | .operandValues = std::move(operandValues), |
| 161 | .pools = std::move(pools), |
| 162 | .relaxComputationFloat32toFloat16 = testModel.isRelaxed}; |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 165 | static bool isOutputSizeGreaterThanOne(const TestModel& testModel, uint32_t index) { |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 166 | const auto byteSize = testModel.main.operands[testModel.main.outputIndexes[index]].data.size(); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 167 | return byteSize > 1u; |
| 168 | } |
| 169 | |
| 170 | static void makeOutputInsufficientSize(uint32_t outputIndex, Request* request) { |
| 171 | auto& length = request->outputs[outputIndex].location.length; |
| 172 | ASSERT_GT(length, 1u); |
| 173 | length -= 1u; |
| 174 | } |
| 175 | |
| 176 | static void makeOutputDimensionsUnspecified(Model* model) { |
| 177 | for (auto i : model->outputIndexes) { |
| 178 | auto& dims = model->operands[i].dimensions; |
| 179 | std::fill(dims.begin(), dims.end(), 0); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel, |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 184 | const Request& request, MeasureTiming measure, |
| 185 | sp<ExecutionCallback>& callback) { |
| 186 | return preparedModel->execute_1_2(request, measure, callback); |
| 187 | } |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 188 | static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel, |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 189 | const Request& request, MeasureTiming measure, |
| 190 | hidl_vec<OutputShape>* outputShapes, |
| 191 | Timing* timing) { |
| 192 | ErrorStatus result; |
| 193 | Return<void> ret = preparedModel->executeSynchronously( |
| 194 | request, measure, |
| 195 | [&result, outputShapes, timing](ErrorStatus error, const hidl_vec<OutputShape>& shapes, |
| 196 | const Timing& time) { |
| 197 | result = error; |
| 198 | *outputShapes = shapes; |
| 199 | *timing = time; |
| 200 | }); |
| 201 | if (!ret.isOk()) { |
| 202 | return ErrorStatus::GENERAL_FAILURE; |
| 203 | } |
| 204 | return result; |
| 205 | } |
| 206 | static std::shared_ptr<::android::nn::ExecutionBurstController> CreateBurst( |
| 207 | const sp<IPreparedModel>& preparedModel) { |
Michael Butler | 648ada5 | 2019-07-25 17:22:11 -0700 | [diff] [blame] | 208 | return android::nn::ExecutionBurstController::create(preparedModel, |
| 209 | std::chrono::microseconds{0}); |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 210 | } |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 211 | |
| 212 | void EvaluatePreparedModel(const sp<IPreparedModel>& preparedModel, const TestModel& testModel, |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 213 | const TestConfig& testConfig) { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 214 | // If output0 does not have size larger than one byte, we can not test with insufficient buffer. |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 215 | if (testConfig.outputType == OutputType::INSUFFICIENT && |
| 216 | !isOutputSizeGreaterThanOne(testModel, 0)) { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 217 | return; |
| 218 | } |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 219 | |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 220 | ExecutionContext context; |
| 221 | Request request = context.createRequest(testModel, testConfig.memoryType); |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 222 | if (testConfig.outputType == OutputType::INSUFFICIENT) { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 223 | makeOutputInsufficientSize(/*outputIndex=*/0, &request); |
| 224 | } |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 225 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 226 | ErrorStatus executionStatus; |
| 227 | hidl_vec<OutputShape> outputShapes; |
| 228 | Timing timing; |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 229 | switch (testConfig.executor) { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 230 | case Executor::ASYNC: { |
| 231 | SCOPED_TRACE("asynchronous"); |
| 232 | |
| 233 | // launch execution |
| 234 | sp<ExecutionCallback> executionCallback = new ExecutionCallback(); |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 235 | Return<ErrorStatus> executionLaunchStatus = ExecutePreparedModel( |
| 236 | preparedModel, request, testConfig.measureTiming, executionCallback); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 237 | ASSERT_TRUE(executionLaunchStatus.isOk()); |
| 238 | EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus)); |
| 239 | |
| 240 | // retrieve execution status |
| 241 | executionCallback->wait(); |
| 242 | executionStatus = executionCallback->getStatus(); |
| 243 | outputShapes = executionCallback->getOutputShapes(); |
| 244 | timing = executionCallback->getTiming(); |
| 245 | |
| 246 | break; |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 247 | } |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 248 | case Executor::SYNC: { |
| 249 | SCOPED_TRACE("synchronous"); |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 250 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 251 | // execute |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 252 | Return<ErrorStatus> executionReturnStatus = ExecutePreparedModel( |
| 253 | preparedModel, request, testConfig.measureTiming, &outputShapes, &timing); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 254 | ASSERT_TRUE(executionReturnStatus.isOk()); |
| 255 | executionStatus = static_cast<ErrorStatus>(executionReturnStatus); |
| 256 | |
| 257 | break; |
| 258 | } |
| 259 | case Executor::BURST: { |
| 260 | SCOPED_TRACE("burst"); |
| 261 | |
| 262 | // create burst |
| 263 | const std::shared_ptr<::android::nn::ExecutionBurstController> controller = |
| 264 | CreateBurst(preparedModel); |
| 265 | ASSERT_NE(nullptr, controller.get()); |
| 266 | |
| 267 | // create memory keys |
| 268 | std::vector<intptr_t> keys(request.pools.size()); |
| 269 | for (size_t i = 0; i < keys.size(); ++i) { |
| 270 | keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]); |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 271 | } |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 272 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 273 | // execute burst |
Michael Butler | 648ada5 | 2019-07-25 17:22:11 -0700 | [diff] [blame] | 274 | int n; |
| 275 | std::tie(n, outputShapes, timing, std::ignore) = |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 276 | controller->compute(request, testConfig.measureTiming, keys); |
Michael Butler | d09c0ee | 2020-03-12 15:12:23 -0700 | [diff] [blame] | 277 | executionStatus = nn::legacyConvertResultCodeToErrorStatus(n); |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 278 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 279 | break; |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 280 | } |
| 281 | } |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 282 | |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 283 | if (testConfig.outputType != OutputType::FULLY_SPECIFIED && |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 284 | executionStatus == ErrorStatus::GENERAL_FAILURE) { |
| 285 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 286 | "execute model that it does not support."; |
| 287 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 288 | "execute model that it does not support." |
| 289 | << std::endl; |
| 290 | GTEST_SKIP(); |
| 291 | } |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 292 | if (testConfig.measureTiming == MeasureTiming::NO) { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 293 | EXPECT_EQ(UINT64_MAX, timing.timeOnDevice); |
| 294 | EXPECT_EQ(UINT64_MAX, timing.timeInDriver); |
| 295 | } else { |
| 296 | if (timing.timeOnDevice != UINT64_MAX && timing.timeInDriver != UINT64_MAX) { |
| 297 | EXPECT_LE(timing.timeOnDevice, timing.timeInDriver); |
| 298 | } |
| 299 | } |
| 300 | |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 301 | switch (testConfig.outputType) { |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 302 | case OutputType::FULLY_SPECIFIED: |
| 303 | // If the model output operands are fully specified, outputShapes must be either |
| 304 | // either empty, or have the same number of elements as the number of outputs. |
| 305 | ASSERT_EQ(ErrorStatus::NONE, executionStatus); |
| 306 | ASSERT_TRUE(outputShapes.size() == 0 || |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 307 | outputShapes.size() == testModel.main.outputIndexes.size()); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 308 | break; |
| 309 | case OutputType::UNSPECIFIED: |
| 310 | // If the model output operands are not fully specified, outputShapes must have |
| 311 | // the same number of elements as the number of outputs. |
| 312 | ASSERT_EQ(ErrorStatus::NONE, executionStatus); |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 313 | ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size()); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 314 | break; |
| 315 | case OutputType::INSUFFICIENT: |
| 316 | ASSERT_EQ(ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, executionStatus); |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 317 | ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size()); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 318 | ASSERT_FALSE(outputShapes[0].isSufficient); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | // Go through all outputs, check returned output shapes. |
| 323 | for (uint32_t i = 0; i < outputShapes.size(); i++) { |
| 324 | EXPECT_TRUE(outputShapes[i].isSufficient); |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 325 | const auto& expect = testModel.main.operands[testModel.main.outputIndexes[i]].dimensions; |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 326 | const std::vector<uint32_t> actual = outputShapes[i].dimensions; |
| 327 | EXPECT_EQ(expect, actual); |
| 328 | } |
| 329 | |
| 330 | // Retrieve execution results. |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 331 | const std::vector<TestBuffer> outputs = context.getOutputBuffers(request); |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 332 | |
| 333 | // We want "close-enough" results. |
| 334 | checkResults(testModel, outputs); |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 335 | } |
| 336 | |
Xusong Wang | ead950d | 2019-08-09 16:45:24 -0700 | [diff] [blame] | 337 | void EvaluatePreparedModel(const sp<IPreparedModel>& preparedModel, const TestModel& testModel, |
| 338 | bool testDynamicOutputShape) { |
Lev Proleev | 56cda83 | 2019-12-05 14:49:47 +0000 | [diff] [blame] | 339 | std::vector<OutputType> outputTypesList; |
| 340 | std::vector<MeasureTiming> measureTimingList; |
| 341 | std::vector<Executor> executorList; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 342 | std::vector<MemoryType> memoryTypeList; |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 343 | |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 344 | if (testDynamicOutputShape) { |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 345 | outputTypesList = {OutputType::UNSPECIFIED, OutputType::INSUFFICIENT}; |
| 346 | measureTimingList = {MeasureTiming::NO, MeasureTiming::YES}; |
| 347 | executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST}; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 348 | memoryTypeList = {MemoryType::ASHMEM}; |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 349 | } else { |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 350 | outputTypesList = {OutputType::FULLY_SPECIFIED}; |
| 351 | measureTimingList = {MeasureTiming::NO, MeasureTiming::YES}; |
| 352 | executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST}; |
Xusong Wang | b79c31a | 2020-04-09 14:26:54 -0700 | [diff] [blame] | 353 | memoryTypeList = {MemoryType::ASHMEM}; |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | for (const OutputType outputType : outputTypesList) { |
| 357 | for (const MeasureTiming measureTiming : measureTimingList) { |
| 358 | for (const Executor executor : executorList) { |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 359 | for (const MemoryType memoryType : memoryTypeList) { |
| 360 | const TestConfig testConfig = {.executor = executor, |
| 361 | .measureTiming = measureTiming, |
| 362 | .outputType = outputType, |
| 363 | .memoryType = memoryType}; |
| 364 | EvaluatePreparedModel(preparedModel, testModel, testConfig); |
| 365 | } |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 366 | } |
| 367 | } |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
Michael Butler | 13b0516 | 2019-08-29 22:17:24 -0700 | [diff] [blame] | 371 | void Execute(const sp<IDevice>& device, const TestModel& testModel, bool testDynamicOutputShape) { |
| 372 | Model model = createModel(testModel); |
| 373 | if (testDynamicOutputShape) { |
| 374 | makeOutputDimensionsUnspecified(&model); |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 375 | } |
Michael Butler | 13b0516 | 2019-08-29 22:17:24 -0700 | [diff] [blame] | 376 | |
| 377 | sp<IPreparedModel> preparedModel; |
| 378 | createPreparedModel(device, model, &preparedModel); |
| 379 | if (preparedModel == nullptr) return; |
| 380 | |
| 381 | EvaluatePreparedModel(preparedModel, testModel, testDynamicOutputShape); |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 384 | void GeneratedTestBase::SetUp() { |
| 385 | testing::TestWithParam<GeneratedTestParam>::SetUp(); |
| 386 | ASSERT_NE(kDevice, nullptr); |
Michael Butler | 9c3c864 | 2021-08-23 18:14:50 -0700 | [diff] [blame] | 387 | const bool deviceIsResponsive = kDevice->ping().isOk(); |
| 388 | ASSERT_TRUE(deviceIsResponsive); |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | std::vector<NamedModel> getNamedModels(const FilterFn& filter) { |
| 392 | return TestModelManager::get().getTestModels(filter); |
| 393 | } |
| 394 | |
Michael Butler | b1865b6 | 2020-03-19 17:10:34 -0700 | [diff] [blame] | 395 | std::vector<NamedModel> getNamedModels(const FilterNameFn& filter) { |
| 396 | return TestModelManager::get().getTestModels(filter); |
| 397 | } |
| 398 | |
Michael Butler | 0763328 | 2019-08-29 11:08:25 -0700 | [diff] [blame] | 399 | std::string printGeneratedTest(const testing::TestParamInfo<GeneratedTestParam>& info) { |
| 400 | const auto& [namedDevice, namedModel] = info.param; |
| 401 | return gtestCompliantName(getName(namedDevice) + "_" + getName(namedModel)); |
| 402 | } |
| 403 | |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 404 | // Tag for the generated tests |
Michael Butler | 13b0516 | 2019-08-29 22:17:24 -0700 | [diff] [blame] | 405 | class GeneratedTest : public GeneratedTestBase {}; |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 406 | |
| 407 | // Tag for the dynamic output shape tests |
| 408 | class DynamicOutputShapeTest : public GeneratedTest {}; |
| 409 | |
| 410 | TEST_P(GeneratedTest, Test) { |
Michael Butler | 13b0516 | 2019-08-29 22:17:24 -0700 | [diff] [blame] | 411 | Execute(kDevice, kTestModel, /*testDynamicOutputShape=*/false); |
Slava Shklyaev | 73ee79d | 2019-05-14 14:15:14 +0100 | [diff] [blame] | 412 | } |
| 413 | |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 414 | TEST_P(DynamicOutputShapeTest, Test) { |
Michael Butler | 13b0516 | 2019-08-29 22:17:24 -0700 | [diff] [blame] | 415 | Execute(kDevice, kTestModel, /*testDynamicOutputShape=*/true); |
Xusong Wang | bcaa782 | 2019-08-23 16:10:54 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | INSTANTIATE_GENERATED_TEST(GeneratedTest, |
| 419 | [](const TestModel& testModel) { return !testModel.expectFailure; }); |
| 420 | |
| 421 | INSTANTIATE_GENERATED_TEST(DynamicOutputShapeTest, |
| 422 | [](const TestModel& testModel) { return !testModel.expectFailure; }); |
| 423 | |
Michael Butler | 62749b9 | 2019-08-26 23:55:47 -0700 | [diff] [blame] | 424 | } // namespace android::hardware::neuralnetworks::V1_2::vts::functional |