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