Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +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> |
Lev Proleev | 26d1bc8 | 2019-08-30 11:57:18 +0100 | [diff] [blame] | 30 | #include <android/hardware/neuralnetworks/1.2/types.h> |
| 31 | #include <android/hardware/neuralnetworks/1.3/IDevice.h> |
Miao Wang | 2b5c4cd | 2019-12-26 18:03:56 -0800 | [diff] [blame] | 32 | #include <android/hardware/neuralnetworks/1.3/IFencedExecutionCallback.h> |
Xusong Wang | 1b3f426 | 2019-10-25 12:07:17 -0700 | [diff] [blame] | 33 | #include <android/hardware/neuralnetworks/1.3/IPreparedModel.h> |
Xusong Wang | cc47dff | 2019-10-23 10:35:07 -0700 | [diff] [blame] | 34 | #include <android/hardware/neuralnetworks/1.3/IPreparedModelCallback.h> |
Lev Proleev | 26d1bc8 | 2019-08-30 11:57:18 +0100 | [diff] [blame] | 35 | #include <android/hardware/neuralnetworks/1.3/types.h> |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 36 | #include <android/hidl/allocator/1.0/IAllocator.h> |
| 37 | #include <android/hidl/memory/1.0/IMemory.h> |
Miao Wang | 2b5c4cd | 2019-12-26 18:03:56 -0800 | [diff] [blame] | 38 | #include <android/sync.h> |
Lev Proleev | 56cda83 | 2019-12-05 14:49:47 +0000 | [diff] [blame] | 39 | #include <gtest/gtest.h> |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 40 | #include <hidlmemory/mapping.h> |
| 41 | |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 42 | #include <algorithm> |
Michael Butler | 648ada5 | 2019-07-25 17:22:11 -0700 | [diff] [blame] | 43 | #include <chrono> |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 44 | #include <iostream> |
| 45 | #include <numeric> |
Lev Proleev | 56cda83 | 2019-12-05 14:49:47 +0000 | [diff] [blame] | 46 | #include <vector> |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 47 | |
| 48 | #include "1.0/Utils.h" |
Xusong Wang | cc47dff | 2019-10-23 10:35:07 -0700 | [diff] [blame] | 49 | #include "1.3/Callbacks.h" |
Michael Butler | 95899b3 | 2020-01-07 14:52:44 -0800 | [diff] [blame] | 50 | #include "1.3/Utils.h" |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 51 | #include "ExecutionBurstController.h" |
| 52 | #include "MemoryUtils.h" |
| 53 | #include "TestHarness.h" |
| 54 | #include "Utils.h" |
| 55 | #include "VtsHalNeuralnetworks.h" |
| 56 | |
Lev Proleev | 26d1bc8 | 2019-08-30 11:57:18 +0100 | [diff] [blame] | 57 | namespace android::hardware::neuralnetworks::V1_3::vts::functional { |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 58 | |
| 59 | using namespace test_helper; |
| 60 | using hidl::memory::V1_0::IMemory; |
Michael Butler | 79a41d7 | 2019-12-11 19:08:08 -0800 | [diff] [blame] | 61 | using implementation::ExecutionCallback; |
Xusong Wang | cc47dff | 2019-10-23 10:35:07 -0700 | [diff] [blame] | 62 | using implementation::PreparedModelCallback; |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 63 | using V1_0::DataLocation; |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 64 | using V1_0::RequestArgument; |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 65 | using V1_1::ExecutionPreference; |
Lev Proleev | 26d1bc8 | 2019-08-30 11:57:18 +0100 | [diff] [blame] | 66 | using V1_2::Constant; |
Lev Proleev | 26d1bc8 | 2019-08-30 11:57:18 +0100 | [diff] [blame] | 67 | using V1_2::MeasureTiming; |
Lev Proleev | 26d1bc8 | 2019-08-30 11:57:18 +0100 | [diff] [blame] | 68 | using V1_2::OutputShape; |
| 69 | using V1_2::SymmPerChannelQuantParams; |
| 70 | using V1_2::Timing; |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 71 | using HidlToken = hidl_array<uint8_t, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)>; |
| 72 | |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 73 | namespace { |
| 74 | |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 75 | enum class OutputType { FULLY_SPECIFIED, UNSPECIFIED, INSUFFICIENT, MISSED_DEADLINE }; |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 76 | |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 77 | enum class IOType { INPUT, OUTPUT }; |
| 78 | |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 79 | struct TestConfig { |
| 80 | Executor executor; |
| 81 | MeasureTiming measureTiming; |
| 82 | OutputType outputType; |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 83 | MemoryType memoryType; |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 84 | // `reportSkipping` indicates if a test should print an info message in case |
| 85 | // it is skipped. The field is set to true by default and is set to false in |
| 86 | // quantization coupling tests to suppress skipping a test |
| 87 | bool reportSkipping; |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 88 | TestConfig(Executor executor, MeasureTiming measureTiming, OutputType outputType, |
| 89 | MemoryType memoryType) |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 90 | : executor(executor), |
| 91 | measureTiming(measureTiming), |
| 92 | outputType(outputType), |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 93 | memoryType(memoryType), |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 94 | reportSkipping(true) {} |
| 95 | TestConfig(Executor executor, MeasureTiming measureTiming, OutputType outputType, |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 96 | MemoryType memoryType, bool reportSkipping) |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 97 | : executor(executor), |
| 98 | measureTiming(measureTiming), |
| 99 | outputType(outputType), |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 100 | memoryType(memoryType), |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 101 | reportSkipping(reportSkipping) {} |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 102 | }; |
| 103 | |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 104 | class DeviceMemoryAllocator { |
| 105 | public: |
| 106 | DeviceMemoryAllocator(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel, |
| 107 | const TestModel& testModel) |
| 108 | : kDevice(device), kPreparedModel(preparedModel), kTestModel(testModel) {} |
| 109 | |
| 110 | // Allocate device memory for a target input/output operand. |
| 111 | // Return {IBuffer object, token} if successful. |
| 112 | // Return {nullptr, 0} if device memory is not supported. |
| 113 | template <IOType ioType> |
Michael Butler | c3e1a29 | 2020-02-04 16:15:04 -0800 | [diff] [blame] | 114 | std::pair<sp<IBuffer>, uint32_t> allocate(uint32_t index) { |
| 115 | std::pair<sp<IBuffer>, uint32_t> buffer; |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 116 | allocateInternal<ioType>(index, &buffer); |
| 117 | return buffer; |
| 118 | } |
| 119 | |
| 120 | private: |
| 121 | template <IOType ioType> |
Michael Butler | c3e1a29 | 2020-02-04 16:15:04 -0800 | [diff] [blame] | 122 | void allocateInternal(uint32_t index, std::pair<sp<IBuffer>, uint32_t>* result) { |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 123 | ASSERT_NE(result, nullptr); |
| 124 | |
| 125 | // Prepare arguments. |
| 126 | BufferRole role = {.modelIndex = 0, .ioIndex = index, .frequency = 1.0f}; |
| 127 | hidl_vec<BufferRole> inputRoles, outputRoles; |
| 128 | if constexpr (ioType == IOType::INPUT) { |
| 129 | inputRoles = {role}; |
| 130 | } else { |
| 131 | outputRoles = {role}; |
| 132 | } |
| 133 | |
| 134 | // Allocate device memory. |
| 135 | ErrorStatus status; |
| 136 | sp<IBuffer> buffer; |
Michael Butler | c3e1a29 | 2020-02-04 16:15:04 -0800 | [diff] [blame] | 137 | uint32_t token; |
| 138 | auto cb = [&status, &buffer, &token](ErrorStatus error, const sp<IBuffer>& buf, |
| 139 | uint32_t tok) { |
| 140 | status = error; |
| 141 | buffer = buf; |
| 142 | token = tok; |
| 143 | }; |
| 144 | const auto ret = kDevice->allocate({}, {kPreparedModel}, inputRoles, outputRoles, cb); |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 145 | |
| 146 | // Check allocation results. |
| 147 | ASSERT_TRUE(ret.isOk()); |
| 148 | if (status == ErrorStatus::NONE) { |
| 149 | ASSERT_NE(buffer, nullptr); |
| 150 | ASSERT_GT(token, 0); |
| 151 | } else { |
| 152 | ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE); |
| 153 | ASSERT_EQ(buffer, nullptr); |
| 154 | ASSERT_EQ(token, 0); |
| 155 | } |
| 156 | |
| 157 | // Initialize input data from TestBuffer. |
| 158 | if constexpr (ioType == IOType::INPUT) { |
| 159 | if (buffer != nullptr) { |
| 160 | // TestBuffer -> Shared memory. |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 161 | const auto& testBuffer = |
| 162 | kTestModel.main.operands[kTestModel.main.inputIndexes[index]].data; |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 163 | ASSERT_GT(testBuffer.size(), 0); |
| 164 | hidl_memory tmp = nn::allocateSharedMemory(testBuffer.size()); |
| 165 | sp<IMemory> inputMemory = mapMemory(tmp); |
| 166 | ASSERT_NE(inputMemory.get(), nullptr); |
| 167 | uint8_t* inputPtr = |
| 168 | static_cast<uint8_t*>(static_cast<void*>(inputMemory->getPointer())); |
| 169 | ASSERT_NE(inputPtr, nullptr); |
| 170 | const uint8_t* begin = testBuffer.get<uint8_t>(); |
| 171 | const uint8_t* end = begin + testBuffer.size(); |
| 172 | std::copy(begin, end, inputPtr); |
| 173 | |
| 174 | // Shared memory -> IBuffer. |
| 175 | auto ret = buffer->copyFrom(tmp, {}); |
| 176 | ASSERT_TRUE(ret.isOk()); |
| 177 | ASSERT_EQ(static_cast<ErrorStatus>(ret), ErrorStatus::NONE); |
| 178 | } |
| 179 | } |
| 180 | *result = {std::move(buffer), token}; |
| 181 | } |
| 182 | |
| 183 | const sp<IDevice> kDevice; |
| 184 | const sp<IPreparedModel> kPreparedModel; |
| 185 | const TestModel& kTestModel; |
| 186 | }; |
| 187 | |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 188 | Subgraph createSubgraph(const TestSubgraph& testSubgraph, uint32_t* constCopySize, |
| 189 | std::vector<const TestBuffer*>* constCopies, uint32_t* constRefSize, |
| 190 | std::vector<const TestBuffer*>* constReferences) { |
| 191 | CHECK(constCopySize != nullptr); |
| 192 | CHECK(constCopies != nullptr); |
| 193 | CHECK(constRefSize != nullptr); |
| 194 | CHECK(constReferences != nullptr); |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 195 | |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 196 | // Operands. |
| 197 | hidl_vec<Operand> operands(testSubgraph.operands.size()); |
| 198 | for (uint32_t i = 0; i < testSubgraph.operands.size(); i++) { |
| 199 | const auto& op = testSubgraph.operands[i]; |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 200 | |
| 201 | DataLocation loc = {}; |
| 202 | if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) { |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 203 | loc = { |
| 204 | .poolIndex = 0, |
| 205 | .offset = *constCopySize, |
| 206 | .length = static_cast<uint32_t>(op.data.size()), |
| 207 | }; |
| 208 | constCopies->push_back(&op.data); |
| 209 | *constCopySize += op.data.alignedSize(); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 210 | } else if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) { |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 211 | loc = { |
| 212 | .poolIndex = 0, |
| 213 | .offset = *constRefSize, |
| 214 | .length = static_cast<uint32_t>(op.data.size()), |
| 215 | }; |
| 216 | constReferences->push_back(&op.data); |
| 217 | *constRefSize += op.data.alignedSize(); |
| 218 | } else if (op.lifetime == TestOperandLifeTime::SUBGRAPH) { |
| 219 | loc = { |
| 220 | .poolIndex = 0, |
| 221 | .offset = *op.data.get<uint32_t>(), |
| 222 | .length = 0, |
| 223 | }; |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Michael Butler | c3e1a29 | 2020-02-04 16:15:04 -0800 | [diff] [blame] | 226 | V1_2::Operand::ExtraParams extraParams; |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 227 | if (op.type == TestOperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) { |
| 228 | extraParams.channelQuant(SymmPerChannelQuantParams{ |
| 229 | .scales = op.channelQuant.scales, .channelDim = op.channelQuant.channelDim}); |
| 230 | } |
| 231 | |
| 232 | operands[i] = {.type = static_cast<OperandType>(op.type), |
| 233 | .dimensions = op.dimensions, |
| 234 | .numberOfConsumers = op.numberOfConsumers, |
| 235 | .scale = op.scale, |
| 236 | .zeroPoint = op.zeroPoint, |
| 237 | .lifetime = static_cast<OperandLifeTime>(op.lifetime), |
| 238 | .location = loc, |
| 239 | .extraParams = std::move(extraParams)}; |
| 240 | } |
| 241 | |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 242 | // Operations. |
| 243 | hidl_vec<Operation> operations(testSubgraph.operations.size()); |
| 244 | std::transform(testSubgraph.operations.begin(), testSubgraph.operations.end(), |
| 245 | operations.begin(), [](const TestOperation& op) -> Operation { |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 246 | return {.type = static_cast<OperationType>(op.type), |
| 247 | .inputs = op.inputs, |
| 248 | .outputs = op.outputs}; |
| 249 | }); |
| 250 | |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 251 | return {.operands = std::move(operands), |
| 252 | .operations = std::move(operations), |
| 253 | .inputIndexes = testSubgraph.inputIndexes, |
| 254 | .outputIndexes = testSubgraph.outputIndexes}; |
| 255 | } |
| 256 | |
| 257 | void copyTestBuffers(const std::vector<const TestBuffer*>& buffers, uint8_t* output) { |
| 258 | uint32_t offset = 0; |
| 259 | for (const TestBuffer* buffer : buffers) { |
| 260 | const uint8_t* begin = buffer->get<uint8_t>(); |
| 261 | const uint8_t* end = begin + buffer->size(); |
| 262 | std::copy(begin, end, output + offset); |
| 263 | offset += buffer->alignedSize(); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | } // namespace |
| 268 | |
Xusong Wang | 2844165 | 2020-03-20 14:49:54 -0700 | [diff] [blame] | 269 | void waitForSyncFence(int syncFd) { |
| 270 | constexpr int kInfiniteTimeout = -1; |
| 271 | ASSERT_GT(syncFd, 0); |
| 272 | int r = sync_wait(syncFd, kInfiniteTimeout); |
| 273 | ASSERT_GE(r, 0); |
| 274 | } |
| 275 | |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 276 | Model createModel(const TestModel& testModel) { |
| 277 | uint32_t constCopySize = 0; |
| 278 | uint32_t constRefSize = 0; |
| 279 | std::vector<const TestBuffer*> constCopies; |
| 280 | std::vector<const TestBuffer*> constReferences; |
| 281 | |
| 282 | Subgraph mainSubgraph = createSubgraph(testModel.main, &constCopySize, &constCopies, |
| 283 | &constRefSize, &constReferences); |
| 284 | hidl_vec<Subgraph> refSubgraphs(testModel.referenced.size()); |
| 285 | std::transform(testModel.referenced.begin(), testModel.referenced.end(), refSubgraphs.begin(), |
| 286 | [&constCopySize, &constCopies, &constRefSize, |
| 287 | &constReferences](const TestSubgraph& testSubgraph) { |
| 288 | return createSubgraph(testSubgraph, &constCopySize, &constCopies, |
| 289 | &constRefSize, &constReferences); |
| 290 | }); |
| 291 | |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 292 | // Constant copies. |
| 293 | hidl_vec<uint8_t> operandValues(constCopySize); |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 294 | copyTestBuffers(constCopies, operandValues.data()); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 295 | |
| 296 | // Shared memory. |
| 297 | hidl_vec<hidl_memory> pools = {}; |
| 298 | if (constRefSize > 0) { |
| 299 | hidl_vec_push_back(&pools, nn::allocateSharedMemory(constRefSize)); |
| 300 | CHECK_NE(pools[0].size(), 0u); |
| 301 | |
| 302 | // load data |
| 303 | sp<IMemory> mappedMemory = mapMemory(pools[0]); |
| 304 | CHECK(mappedMemory.get() != nullptr); |
| 305 | uint8_t* mappedPtr = |
| 306 | reinterpret_cast<uint8_t*>(static_cast<void*>(mappedMemory->getPointer())); |
| 307 | CHECK(mappedPtr != nullptr); |
| 308 | |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 309 | copyTestBuffers(constReferences, mappedPtr); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 310 | } |
| 311 | |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 312 | return {.main = std::move(mainSubgraph), |
| 313 | .referenced = std::move(refSubgraphs), |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 314 | .operandValues = std::move(operandValues), |
| 315 | .pools = std::move(pools), |
| 316 | .relaxComputationFloat32toFloat16 = testModel.isRelaxed}; |
| 317 | } |
| 318 | |
| 319 | static bool isOutputSizeGreaterThanOne(const TestModel& testModel, uint32_t index) { |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 320 | const auto byteSize = testModel.main.operands[testModel.main.outputIndexes[index]].data.size(); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 321 | return byteSize > 1u; |
| 322 | } |
| 323 | |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 324 | static void makeOutputInsufficientSize(uint32_t outputIndex, Request* request) { |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 325 | auto& length = request->outputs[outputIndex].location.length; |
| 326 | ASSERT_GT(length, 1u); |
| 327 | length -= 1u; |
| 328 | } |
| 329 | |
| 330 | static void makeOutputDimensionsUnspecified(Model* model) { |
Slava Shklyaev | f8124a8 | 2019-12-13 12:24:35 +0000 | [diff] [blame] | 331 | for (auto i : model->main.outputIndexes) { |
| 332 | auto& dims = model->main.operands[i].dimensions; |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 333 | std::fill(dims.begin(), dims.end(), 0); |
| 334 | } |
| 335 | } |
| 336 | |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 337 | class ExecutionContextV1_3 { |
| 338 | public: |
| 339 | ExecutionContextV1_3(sp<IDevice> device, sp<IPreparedModel> preparedModel) |
| 340 | : kDevice(std::move(device)), kPreparedModel(std::move(preparedModel)) {} |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 341 | |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 342 | std::optional<Request> createRequest(const TestModel& testModel, MemoryType memoryType); |
| 343 | std::vector<TestBuffer> getOutputBuffers(const TestModel& testModel, |
| 344 | const Request& request) const; |
| 345 | |
| 346 | private: |
| 347 | // Get a TestBuffer with data copied from an IBuffer object. |
| 348 | void getBuffer(const sp<IBuffer>& buffer, size_t size, TestBuffer* testBuffer) const; |
| 349 | |
| 350 | static constexpr uint32_t kInputPoolIndex = 0; |
| 351 | static constexpr uint32_t kOutputPoolIndex = 1; |
| 352 | static constexpr uint32_t kDeviceMemoryBeginIndex = 2; |
| 353 | |
| 354 | const sp<IDevice> kDevice; |
| 355 | const sp<IPreparedModel> kPreparedModel; |
| 356 | std::unique_ptr<TestMemoryBase> mInputMemory, mOutputMemory; |
| 357 | std::vector<sp<IBuffer>> mBuffers; |
| 358 | }; |
| 359 | |
| 360 | std::optional<Request> ExecutionContextV1_3::createRequest(const TestModel& testModel, |
| 361 | MemoryType memoryType) { |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 362 | // Memory pools are organized as: |
| 363 | // - 0: Input shared memory pool |
| 364 | // - 1: Output shared memory pool |
| 365 | // - [2, 2+i): Input device memories |
| 366 | // - [2+i, 2+i+o): Output device memories |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 367 | DeviceMemoryAllocator allocator(kDevice, kPreparedModel, testModel); |
Michael Butler | c3e1a29 | 2020-02-04 16:15:04 -0800 | [diff] [blame] | 368 | std::vector<uint32_t> tokens; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 369 | mBuffers.clear(); |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 370 | |
| 371 | // Model inputs. |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 372 | hidl_vec<RequestArgument> inputs(testModel.main.inputIndexes.size()); |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 373 | size_t inputSize = 0; |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 374 | for (uint32_t i = 0; i < testModel.main.inputIndexes.size(); i++) { |
| 375 | const auto& op = testModel.main.operands[testModel.main.inputIndexes[i]]; |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 376 | if (op.data.size() == 0) { |
| 377 | // Omitted input. |
| 378 | inputs[i] = {.hasNoValue = true}; |
| 379 | continue; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 380 | } else if (memoryType == MemoryType::DEVICE) { |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 381 | SCOPED_TRACE("Input index = " + std::to_string(i)); |
| 382 | auto [buffer, token] = allocator.allocate<IOType::INPUT>(i); |
| 383 | if (buffer != nullptr) { |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 384 | DataLocation loc = {.poolIndex = static_cast<uint32_t>(mBuffers.size() + |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 385 | kDeviceMemoryBeginIndex)}; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 386 | mBuffers.push_back(std::move(buffer)); |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 387 | tokens.push_back(token); |
| 388 | inputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}}; |
| 389 | continue; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | // Reserve shared memory for input. |
| 394 | DataLocation loc = {.poolIndex = kInputPoolIndex, |
| 395 | .offset = static_cast<uint32_t>(inputSize), |
| 396 | .length = static_cast<uint32_t>(op.data.size())}; |
| 397 | inputSize += op.data.alignedSize(); |
| 398 | inputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}}; |
| 399 | } |
| 400 | |
| 401 | // Model outputs. |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 402 | hidl_vec<RequestArgument> outputs(testModel.main.outputIndexes.size()); |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 403 | size_t outputSize = 0; |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 404 | for (uint32_t i = 0; i < testModel.main.outputIndexes.size(); i++) { |
| 405 | const auto& op = testModel.main.operands[testModel.main.outputIndexes[i]]; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 406 | if (memoryType == MemoryType::DEVICE) { |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 407 | SCOPED_TRACE("Output index = " + std::to_string(i)); |
| 408 | auto [buffer, token] = allocator.allocate<IOType::OUTPUT>(i); |
| 409 | if (buffer != nullptr) { |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 410 | DataLocation loc = {.poolIndex = static_cast<uint32_t>(mBuffers.size() + |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 411 | kDeviceMemoryBeginIndex)}; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 412 | mBuffers.push_back(std::move(buffer)); |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 413 | tokens.push_back(token); |
| 414 | outputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}}; |
| 415 | continue; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // In the case of zero-sized output, we should at least provide a one-byte buffer. |
| 420 | // This is because zero-sized tensors are only supported internally to the driver, or |
| 421 | // reported in output shapes. It is illegal for the client to pre-specify a zero-sized |
| 422 | // tensor as model output. Otherwise, we will have two semantic conflicts: |
| 423 | // - "Zero dimension" conflicts with "unspecified dimension". |
| 424 | // - "Omitted operand buffer" conflicts with "zero-sized operand buffer". |
| 425 | size_t bufferSize = std::max<size_t>(op.data.size(), 1); |
| 426 | |
| 427 | // Reserve shared memory for output. |
| 428 | DataLocation loc = {.poolIndex = kOutputPoolIndex, |
| 429 | .offset = static_cast<uint32_t>(outputSize), |
| 430 | .length = static_cast<uint32_t>(bufferSize)}; |
| 431 | outputSize += op.data.size() == 0 ? TestBuffer::kAlignment : op.data.alignedSize(); |
| 432 | outputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}}; |
| 433 | } |
| 434 | |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 435 | if (memoryType == MemoryType::DEVICE && mBuffers.empty()) { |
| 436 | return std::nullopt; |
| 437 | } |
| 438 | |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 439 | // Memory pools. |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 440 | hidl_vec<Request::MemoryPool> pools(kDeviceMemoryBeginIndex + mBuffers.size()); |
| 441 | if (memoryType == MemoryType::BLOB_AHWB) { |
| 442 | mInputMemory = TestBlobAHWB::create(std::max<size_t>(inputSize, 1)); |
| 443 | mOutputMemory = TestBlobAHWB::create(std::max<size_t>(outputSize, 1)); |
| 444 | } else { |
| 445 | mInputMemory = TestAshmem::create(std::max<size_t>(inputSize, 1)); |
| 446 | mOutputMemory = TestAshmem::create(std::max<size_t>(outputSize, 1)); |
| 447 | } |
| 448 | EXPECT_NE(mInputMemory, nullptr); |
| 449 | EXPECT_NE(mOutputMemory, nullptr); |
| 450 | pools[kInputPoolIndex].hidlMemory(mInputMemory->getHidlMemory()); |
| 451 | pools[kOutputPoolIndex].hidlMemory(mOutputMemory->getHidlMemory()); |
| 452 | for (uint32_t i = 0; i < mBuffers.size(); i++) { |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 453 | pools[kDeviceMemoryBeginIndex + i].token(tokens[i]); |
| 454 | } |
| 455 | |
| 456 | // Copy input data to the input shared memory pool. |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 457 | uint8_t* inputPtr = mInputMemory->getPointer(); |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 458 | for (uint32_t i = 0; i < testModel.main.inputIndexes.size(); i++) { |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 459 | if (!inputs[i].hasNoValue && inputs[i].location.poolIndex == kInputPoolIndex) { |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 460 | const auto& op = testModel.main.operands[testModel.main.inputIndexes[i]]; |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 461 | const uint8_t* begin = op.data.get<uint8_t>(); |
| 462 | const uint8_t* end = begin + op.data.size(); |
| 463 | std::copy(begin, end, inputPtr + inputs[i].location.offset); |
| 464 | } |
| 465 | } |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 466 | return Request{ |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 467 | .inputs = std::move(inputs), .outputs = std::move(outputs), .pools = std::move(pools)}; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | std::vector<TestBuffer> ExecutionContextV1_3::getOutputBuffers(const TestModel& testModel, |
| 471 | const Request& request) const { |
| 472 | // Copy out output results. |
| 473 | uint8_t* outputPtr = mOutputMemory->getPointer(); |
| 474 | std::vector<TestBuffer> outputBuffers; |
| 475 | for (uint32_t i = 0; i < request.outputs.size(); i++) { |
| 476 | const auto& outputLoc = request.outputs[i].location; |
| 477 | if (outputLoc.poolIndex == kOutputPoolIndex) { |
| 478 | outputBuffers.emplace_back(outputLoc.length, outputPtr + outputLoc.offset); |
| 479 | } else { |
| 480 | const auto& op = testModel.main.operands[testModel.main.outputIndexes[i]]; |
| 481 | if (op.data.size() == 0) { |
| 482 | outputBuffers.emplace_back(0, nullptr); |
| 483 | } else { |
| 484 | SCOPED_TRACE("Output index = " + std::to_string(i)); |
| 485 | const uint32_t bufferIndex = outputLoc.poolIndex - kDeviceMemoryBeginIndex; |
| 486 | TestBuffer buffer; |
| 487 | getBuffer(mBuffers[bufferIndex], op.data.size(), &buffer); |
| 488 | outputBuffers.push_back(std::move(buffer)); |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 | return outputBuffers; |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | // Get a TestBuffer with data copied from an IBuffer object. |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 496 | void ExecutionContextV1_3::getBuffer(const sp<IBuffer>& buffer, size_t size, |
| 497 | TestBuffer* testBuffer) const { |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 498 | // IBuffer -> Shared memory. |
| 499 | hidl_memory tmp = nn::allocateSharedMemory(size); |
| 500 | const auto ret = buffer->copyTo(tmp); |
| 501 | ASSERT_TRUE(ret.isOk()); |
| 502 | ASSERT_EQ(static_cast<ErrorStatus>(ret), ErrorStatus::NONE); |
| 503 | |
| 504 | // Shared memory -> TestBuffer. |
| 505 | sp<IMemory> outputMemory = mapMemory(tmp); |
| 506 | ASSERT_NE(outputMemory.get(), nullptr); |
| 507 | uint8_t* outputPtr = static_cast<uint8_t*>(static_cast<void*>(outputMemory->getPointer())); |
| 508 | ASSERT_NE(outputPtr, nullptr); |
| 509 | ASSERT_NE(testBuffer, nullptr); |
| 510 | *testBuffer = TestBuffer(size, outputPtr); |
| 511 | } |
| 512 | |
Xusong Wang | f07a957 | 2020-03-13 10:02:36 -0700 | [diff] [blame] | 513 | static bool hasZeroSizedOutput(const TestModel& testModel) { |
| 514 | return std::any_of(testModel.main.outputIndexes.begin(), testModel.main.outputIndexes.end(), |
| 515 | [&testModel](uint32_t index) { |
| 516 | return testModel.main.operands[index].data.size() == 0; |
| 517 | }); |
| 518 | } |
| 519 | |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 520 | static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel, |
| 521 | const Request& request, MeasureTiming measure, |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 522 | const OptionalTimeoutDuration& loopTimeoutDuration, |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 523 | sp<ExecutionCallback>& callback) { |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 524 | return preparedModel->execute_1_3(request, measure, {}, loopTimeoutDuration, callback); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 525 | } |
| 526 | static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel, |
| 527 | const Request& request, MeasureTiming measure, |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 528 | const OptionalTimeoutDuration& loopTimeoutDuration, |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 529 | hidl_vec<OutputShape>* outputShapes, |
| 530 | Timing* timing) { |
| 531 | ErrorStatus result; |
Xusong Wang | d4a060b | 2019-10-28 11:11:19 -0700 | [diff] [blame] | 532 | Return<void> ret = preparedModel->executeSynchronously_1_3( |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 533 | request, measure, {}, loopTimeoutDuration, |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 534 | [&result, outputShapes, timing](ErrorStatus error, const hidl_vec<OutputShape>& shapes, |
| 535 | const Timing& time) { |
| 536 | result = error; |
| 537 | *outputShapes = shapes; |
| 538 | *timing = time; |
| 539 | }); |
| 540 | if (!ret.isOk()) { |
| 541 | return ErrorStatus::GENERAL_FAILURE; |
| 542 | } |
| 543 | return result; |
| 544 | } |
| 545 | static std::shared_ptr<::android::nn::ExecutionBurstController> CreateBurst( |
| 546 | const sp<IPreparedModel>& preparedModel) { |
Michael Butler | 648ada5 | 2019-07-25 17:22:11 -0700 | [diff] [blame] | 547 | return android::nn::ExecutionBurstController::create(preparedModel, |
| 548 | std::chrono::microseconds{0}); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 549 | } |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 550 | |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 551 | void EvaluatePreparedModel(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel, |
| 552 | const TestModel& testModel, const TestConfig& testConfig, |
| 553 | bool* skipped = nullptr) { |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 554 | if (skipped != nullptr) { |
| 555 | *skipped = false; |
| 556 | } |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 557 | // 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] | 558 | if (testConfig.outputType == OutputType::INSUFFICIENT && |
| 559 | !isOutputSizeGreaterThanOne(testModel, 0)) { |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 560 | return; |
| 561 | } |
| 562 | |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 563 | ExecutionContextV1_3 context(device, preparedModel); |
| 564 | auto maybeRequest = context.createRequest(testModel, testConfig.memoryType); |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 565 | // Skip if testing memory domain but no device memory has been allocated. |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 566 | if (!maybeRequest.has_value()) { |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 567 | return; |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 568 | } |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 569 | |
| 570 | Request request = std::move(maybeRequest.value()); |
Xusong Wang | 91521ab | 2020-05-08 16:52:16 -0700 | [diff] [blame] | 571 | |
| 572 | constexpr uint32_t kInsufficientOutputIndex = 0; |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 573 | if (testConfig.outputType == OutputType::INSUFFICIENT) { |
Xusong Wang | 91521ab | 2020-05-08 16:52:16 -0700 | [diff] [blame] | 574 | makeOutputInsufficientSize(kInsufficientOutputIndex, &request); |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 575 | } |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 576 | |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 577 | OptionalTimeoutDuration loopTimeoutDuration; |
| 578 | // OutputType::MISSED_DEADLINE is only used by |
| 579 | // TestKind::INTINITE_LOOP_TIMEOUT tests to verify that an infinite loop is |
| 580 | // aborted after a timeout. |
| 581 | if (testConfig.outputType == OutputType::MISSED_DEADLINE) { |
| 582 | // Override the default loop timeout duration with a small value to |
| 583 | // speed up test execution. |
| 584 | constexpr uint64_t kMillisecond = 1'000'000; |
| 585 | loopTimeoutDuration.nanoseconds(1 * kMillisecond); |
| 586 | } |
| 587 | |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 588 | ErrorStatus executionStatus; |
| 589 | hidl_vec<OutputShape> outputShapes; |
| 590 | Timing timing; |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 591 | switch (testConfig.executor) { |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 592 | case Executor::ASYNC: { |
| 593 | SCOPED_TRACE("asynchronous"); |
| 594 | |
| 595 | // launch execution |
| 596 | sp<ExecutionCallback> executionCallback = new ExecutionCallback(); |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 597 | Return<ErrorStatus> executionLaunchStatus = |
| 598 | ExecutePreparedModel(preparedModel, request, testConfig.measureTiming, |
| 599 | loopTimeoutDuration, executionCallback); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 600 | ASSERT_TRUE(executionLaunchStatus.isOk()); |
| 601 | EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus)); |
| 602 | |
| 603 | // retrieve execution status |
| 604 | executionCallback->wait(); |
| 605 | executionStatus = executionCallback->getStatus(); |
| 606 | outputShapes = executionCallback->getOutputShapes(); |
| 607 | timing = executionCallback->getTiming(); |
| 608 | |
| 609 | break; |
| 610 | } |
| 611 | case Executor::SYNC: { |
| 612 | SCOPED_TRACE("synchronous"); |
| 613 | |
| 614 | // execute |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 615 | Return<ErrorStatus> executionReturnStatus = |
| 616 | ExecutePreparedModel(preparedModel, request, testConfig.measureTiming, |
| 617 | loopTimeoutDuration, &outputShapes, &timing); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 618 | ASSERT_TRUE(executionReturnStatus.isOk()); |
| 619 | executionStatus = static_cast<ErrorStatus>(executionReturnStatus); |
| 620 | |
| 621 | break; |
| 622 | } |
| 623 | case Executor::BURST: { |
Xusong Wang | b345a46 | 2019-11-27 12:46:48 -0800 | [diff] [blame] | 624 | // TODO(butlermichael): Check if we need to test burst in V1_3 if the interface remains |
| 625 | // V1_2. |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 626 | SCOPED_TRACE("burst"); |
| 627 | |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 628 | // check compliance |
| 629 | ASSERT_TRUE(nn::compliantWithV1_0(request)); |
| 630 | V1_0::Request request10 = nn::convertToV1_0(request); |
| 631 | |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 632 | // create burst |
| 633 | const std::shared_ptr<::android::nn::ExecutionBurstController> controller = |
| 634 | CreateBurst(preparedModel); |
| 635 | ASSERT_NE(nullptr, controller.get()); |
| 636 | |
| 637 | // create memory keys |
Xusong Wang | b345a46 | 2019-11-27 12:46:48 -0800 | [diff] [blame] | 638 | std::vector<intptr_t> keys(request10.pools.size()); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 639 | for (size_t i = 0; i < keys.size(); ++i) { |
Xusong Wang | b345a46 | 2019-11-27 12:46:48 -0800 | [diff] [blame] | 640 | keys[i] = reinterpret_cast<intptr_t>(&request10.pools[i]); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | // execute burst |
Michael Butler | 648ada5 | 2019-07-25 17:22:11 -0700 | [diff] [blame] | 644 | int n; |
| 645 | std::tie(n, outputShapes, timing, std::ignore) = |
Xusong Wang | b345a46 | 2019-11-27 12:46:48 -0800 | [diff] [blame] | 646 | controller->compute(request10, testConfig.measureTiming, keys); |
Michael Butler | 648ada5 | 2019-07-25 17:22:11 -0700 | [diff] [blame] | 647 | executionStatus = nn::convertResultCodeToErrorStatus(n); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 648 | |
| 649 | break; |
| 650 | } |
Miao Wang | 2b5c4cd | 2019-12-26 18:03:56 -0800 | [diff] [blame] | 651 | case Executor::FENCED: { |
| 652 | SCOPED_TRACE("fenced"); |
| 653 | ErrorStatus result; |
Miao Wang | 99753d9 | 2020-01-21 13:15:09 -0800 | [diff] [blame] | 654 | hidl_handle syncFenceHandle; |
| 655 | sp<IFencedExecutionCallback> fencedCallback; |
Miao Wang | 2d74d7d | 2020-02-25 15:54:08 -0800 | [diff] [blame] | 656 | auto callbackFunc = [&result, &syncFenceHandle, &fencedCallback]( |
| 657 | ErrorStatus error, const hidl_handle& handle, |
| 658 | const sp<IFencedExecutionCallback>& callback) { |
| 659 | result = error; |
| 660 | syncFenceHandle = handle; |
| 661 | fencedCallback = callback; |
| 662 | }; |
| 663 | Return<void> ret = |
| 664 | preparedModel->executeFenced(request, {}, testConfig.measureTiming, {}, |
| 665 | loopTimeoutDuration, {}, callbackFunc); |
Miao Wang | 2b5c4cd | 2019-12-26 18:03:56 -0800 | [diff] [blame] | 666 | ASSERT_TRUE(ret.isOk()); |
| 667 | if (result != ErrorStatus::NONE) { |
Miao Wang | 99753d9 | 2020-01-21 13:15:09 -0800 | [diff] [blame] | 668 | ASSERT_EQ(syncFenceHandle.getNativeHandle(), nullptr); |
| 669 | ASSERT_EQ(fencedCallback, nullptr); |
Miao Wang | 4068907 | 2020-02-25 16:57:53 -0800 | [diff] [blame] | 670 | executionStatus = result; |
Miao Wang | 702a91a | 2020-03-20 15:09:29 -0700 | [diff] [blame] | 671 | timing = {UINT64_MAX, UINT64_MAX}; |
Miao Wang | 99753d9 | 2020-01-21 13:15:09 -0800 | [diff] [blame] | 672 | } else if (syncFenceHandle.getNativeHandle()) { |
Miao Wang | 2d74d7d | 2020-02-25 15:54:08 -0800 | [diff] [blame] | 673 | // If a sync fence is returned, try start another run waiting for the sync fence. |
| 674 | ret = preparedModel->executeFenced(request, {syncFenceHandle}, |
| 675 | testConfig.measureTiming, {}, |
| 676 | loopTimeoutDuration, {}, callbackFunc); |
| 677 | ASSERT_TRUE(ret.isOk()); |
| 678 | ASSERT_EQ(result, ErrorStatus::NONE); |
Miao Wang | 99753d9 | 2020-01-21 13:15:09 -0800 | [diff] [blame] | 679 | waitForSyncFence(syncFenceHandle.getNativeHandle()->data[0]); |
Miao Wang | 2b5c4cd | 2019-12-26 18:03:56 -0800 | [diff] [blame] | 680 | } |
| 681 | if (result == ErrorStatus::NONE) { |
Miao Wang | 99753d9 | 2020-01-21 13:15:09 -0800 | [diff] [blame] | 682 | ASSERT_NE(fencedCallback, nullptr); |
| 683 | Return<void> ret = fencedCallback->getExecutionInfo( |
| 684 | [&executionStatus, &timing](ErrorStatus error, Timing t, Timing) { |
Miao Wang | 2b5c4cd | 2019-12-26 18:03:56 -0800 | [diff] [blame] | 685 | executionStatus = error; |
| 686 | timing = t; |
| 687 | }); |
| 688 | ASSERT_TRUE(ret.isOk()); |
| 689 | } |
| 690 | break; |
| 691 | } |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 692 | } |
| 693 | |
Miao Wang | 4068907 | 2020-02-25 16:57:53 -0800 | [diff] [blame] | 694 | if (testConfig.outputType != OutputType::FULLY_SPECIFIED && |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 695 | executionStatus == ErrorStatus::GENERAL_FAILURE) { |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 696 | if (skipped != nullptr) { |
| 697 | *skipped = true; |
| 698 | } |
| 699 | if (!testConfig.reportSkipping) { |
| 700 | return; |
| 701 | } |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 702 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 703 | "execute model that it does not support."; |
| 704 | std::cout << "[ ] Early termination of test because vendor service cannot " |
| 705 | "execute model that it does not support." |
| 706 | << std::endl; |
| 707 | GTEST_SKIP(); |
| 708 | } |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 709 | if (testConfig.measureTiming == MeasureTiming::NO) { |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 710 | EXPECT_EQ(UINT64_MAX, timing.timeOnDevice); |
| 711 | EXPECT_EQ(UINT64_MAX, timing.timeInDriver); |
| 712 | } else { |
| 713 | if (timing.timeOnDevice != UINT64_MAX && timing.timeInDriver != UINT64_MAX) { |
| 714 | EXPECT_LE(timing.timeOnDevice, timing.timeInDriver); |
| 715 | } |
| 716 | } |
| 717 | |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 718 | switch (testConfig.outputType) { |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 719 | case OutputType::FULLY_SPECIFIED: |
Xusong Wang | f07a957 | 2020-03-13 10:02:36 -0700 | [diff] [blame] | 720 | if (testConfig.executor == Executor::FENCED && hasZeroSizedOutput(testModel)) { |
| 721 | // Executor::FENCED does not support zero-sized output. |
| 722 | ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus); |
| 723 | return; |
| 724 | } |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 725 | // If the model output operands are fully specified, outputShapes must be either |
| 726 | // either empty, or have the same number of elements as the number of outputs. |
| 727 | ASSERT_EQ(ErrorStatus::NONE, executionStatus); |
| 728 | ASSERT_TRUE(outputShapes.size() == 0 || |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 729 | outputShapes.size() == testModel.main.outputIndexes.size()); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 730 | break; |
| 731 | case OutputType::UNSPECIFIED: |
Miao Wang | 4068907 | 2020-02-25 16:57:53 -0800 | [diff] [blame] | 732 | if (testConfig.executor == Executor::FENCED) { |
| 733 | // For Executor::FENCED, the output shape must be fully specified. |
| 734 | ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus); |
| 735 | return; |
| 736 | } |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 737 | // If the model output operands are not fully specified, outputShapes must have |
| 738 | // the same number of elements as the number of outputs. |
| 739 | ASSERT_EQ(ErrorStatus::NONE, executionStatus); |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 740 | ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size()); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 741 | break; |
| 742 | case OutputType::INSUFFICIENT: |
Miao Wang | 4068907 | 2020-02-25 16:57:53 -0800 | [diff] [blame] | 743 | if (testConfig.executor == Executor::FENCED) { |
| 744 | // For Executor::FENCED, the output shape must be fully specified. |
| 745 | ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus); |
| 746 | return; |
| 747 | } |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 748 | ASSERT_EQ(ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, executionStatus); |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 749 | ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size()); |
Xusong Wang | 91521ab | 2020-05-08 16:52:16 -0700 | [diff] [blame] | 750 | // Check that all returned output dimensions are at least as fully specified as the |
| 751 | // union of the information about the corresponding operand in the model and in the |
| 752 | // request. In this test, all model outputs have known rank with all dimensions |
| 753 | // unspecified, and no dimensional information is provided in the request. |
| 754 | for (uint32_t i = 0; i < outputShapes.size(); i++) { |
| 755 | ASSERT_EQ(outputShapes[i].isSufficient, i != kInsufficientOutputIndex); |
| 756 | const auto& actual = outputShapes[i].dimensions; |
| 757 | const auto& golden = |
| 758 | testModel.main.operands[testModel.main.outputIndexes[i]].dimensions; |
| 759 | ASSERT_EQ(actual.size(), golden.size()); |
| 760 | for (uint32_t j = 0; j < actual.size(); j++) { |
| 761 | if (actual[j] == 0) continue; |
| 762 | EXPECT_EQ(actual[j], golden[j]) << "index: " << j; |
| 763 | } |
| 764 | } |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 765 | return; |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 766 | case OutputType::MISSED_DEADLINE: |
| 767 | ASSERT_TRUE(executionStatus == ErrorStatus::MISSED_DEADLINE_TRANSIENT || |
| 768 | executionStatus == ErrorStatus::MISSED_DEADLINE_PERSISTENT) |
| 769 | << "executionStatus = " << executionStatus; |
| 770 | return; |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | // Go through all outputs, check returned output shapes. |
| 774 | for (uint32_t i = 0; i < outputShapes.size(); i++) { |
| 775 | EXPECT_TRUE(outputShapes[i].isSufficient); |
Slava Shklyaev | 0fff59b | 2020-01-31 15:14:24 +0000 | [diff] [blame] | 776 | const auto& expect = testModel.main.operands[testModel.main.outputIndexes[i]].dimensions; |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 777 | const std::vector<uint32_t> actual = outputShapes[i].dimensions; |
| 778 | EXPECT_EQ(expect, actual); |
| 779 | } |
| 780 | |
| 781 | // Retrieve execution results. |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 782 | const std::vector<TestBuffer> outputs = context.getOutputBuffers(testModel, request); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 783 | |
| 784 | // We want "close-enough" results. |
| 785 | checkResults(testModel, outputs); |
| 786 | } |
| 787 | |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 788 | void EvaluatePreparedModel(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel, |
| 789 | const TestModel& testModel, TestKind testKind) { |
Lev Proleev | 56cda83 | 2019-12-05 14:49:47 +0000 | [diff] [blame] | 790 | std::vector<OutputType> outputTypesList; |
| 791 | std::vector<MeasureTiming> measureTimingList; |
| 792 | std::vector<Executor> executorList; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 793 | std::vector<MemoryType> memoryTypeList; |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 794 | |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 795 | switch (testKind) { |
| 796 | case TestKind::GENERAL: { |
| 797 | outputTypesList = {OutputType::FULLY_SPECIFIED}; |
| 798 | measureTimingList = {MeasureTiming::NO, MeasureTiming::YES}; |
Miao Wang | e51e4a9 | 2020-01-23 13:53:21 -0800 | [diff] [blame] | 799 | executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST}; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 800 | memoryTypeList = {MemoryType::ASHMEM}; |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 801 | } break; |
| 802 | case TestKind::DYNAMIC_SHAPE: { |
| 803 | outputTypesList = {OutputType::UNSPECIFIED, OutputType::INSUFFICIENT}; |
| 804 | measureTimingList = {MeasureTiming::NO, MeasureTiming::YES}; |
Miao Wang | 4068907 | 2020-02-25 16:57:53 -0800 | [diff] [blame] | 805 | executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST, Executor::FENCED}; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 806 | memoryTypeList = {MemoryType::ASHMEM}; |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 807 | } break; |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 808 | case TestKind::MEMORY_DOMAIN: { |
| 809 | outputTypesList = {OutputType::FULLY_SPECIFIED}; |
| 810 | measureTimingList = {MeasureTiming::NO}; |
Miao Wang | 2d74d7d | 2020-02-25 15:54:08 -0800 | [diff] [blame] | 811 | executorList = {Executor::ASYNC, Executor::SYNC, Executor::FENCED}; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 812 | memoryTypeList = {MemoryType::BLOB_AHWB, MemoryType::DEVICE}; |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 813 | } break; |
Miao Wang | e51e4a9 | 2020-01-23 13:53:21 -0800 | [diff] [blame] | 814 | case TestKind::FENCED_COMPUTE: { |
| 815 | outputTypesList = {OutputType::FULLY_SPECIFIED}; |
| 816 | measureTimingList = {MeasureTiming::NO, MeasureTiming::YES}; |
| 817 | executorList = {Executor::FENCED}; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 818 | memoryTypeList = {MemoryType::ASHMEM}; |
Miao Wang | e51e4a9 | 2020-01-23 13:53:21 -0800 | [diff] [blame] | 819 | } break; |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 820 | case TestKind::QUANTIZATION_COUPLING: { |
| 821 | LOG(FATAL) << "Wrong TestKind for EvaluatePreparedModel"; |
| 822 | return; |
| 823 | } break; |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 824 | case TestKind::INTINITE_LOOP_TIMEOUT: { |
| 825 | outputTypesList = {OutputType::MISSED_DEADLINE}; |
| 826 | measureTimingList = {MeasureTiming::NO, MeasureTiming::YES}; |
| 827 | // Burst does not support V1_3 loop timeout. |
| 828 | executorList = {Executor::ASYNC, Executor::SYNC, Executor::FENCED}; |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 829 | memoryTypeList = {MemoryType::ASHMEM}; |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 830 | } break; |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | for (const OutputType outputType : outputTypesList) { |
| 834 | for (const MeasureTiming measureTiming : measureTimingList) { |
| 835 | for (const Executor executor : executorList) { |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 836 | for (const MemoryType memoryType : memoryTypeList) { |
| 837 | const TestConfig testConfig(executor, measureTiming, outputType, memoryType); |
| 838 | EvaluatePreparedModel(device, preparedModel, testModel, testConfig); |
| 839 | } |
Lev Proleev | 0d4ba3f | 2019-10-02 17:32:06 +0100 | [diff] [blame] | 840 | } |
| 841 | } |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 842 | } |
| 843 | } |
| 844 | |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 845 | void EvaluatePreparedCoupledModels(const sp<IDevice>& device, |
| 846 | const sp<IPreparedModel>& preparedModel, |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 847 | const TestModel& testModel, |
| 848 | const sp<IPreparedModel>& preparedCoupledModel, |
| 849 | const TestModel& coupledModel) { |
Lev Proleev | 56cda83 | 2019-12-05 14:49:47 +0000 | [diff] [blame] | 850 | const std::vector<OutputType> outputTypesList = {OutputType::FULLY_SPECIFIED}; |
| 851 | const std::vector<MeasureTiming> measureTimingList = {MeasureTiming::NO, MeasureTiming::YES}; |
Miao Wang | 2b5c4cd | 2019-12-26 18:03:56 -0800 | [diff] [blame] | 852 | const std::vector<Executor> executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST, |
| 853 | Executor::FENCED}; |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 854 | |
| 855 | for (const OutputType outputType : outputTypesList) { |
| 856 | for (const MeasureTiming measureTiming : measureTimingList) { |
| 857 | for (const Executor executor : executorList) { |
Xusong Wang | 75e63ad | 2020-02-25 11:43:10 -0800 | [diff] [blame] | 858 | const TestConfig testConfig(executor, measureTiming, outputType, MemoryType::ASHMEM, |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 859 | /*reportSkipping=*/false); |
| 860 | bool baseSkipped = false; |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 861 | EvaluatePreparedModel(device, preparedModel, testModel, testConfig, &baseSkipped); |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 862 | bool coupledSkipped = false; |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 863 | EvaluatePreparedModel(device, preparedCoupledModel, coupledModel, testConfig, |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 864 | &coupledSkipped); |
| 865 | ASSERT_EQ(baseSkipped, coupledSkipped); |
| 866 | if (baseSkipped) { |
| 867 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 868 | "execute model that it does not support."; |
| 869 | std::cout << "[ ] Early termination of test because vendor service " |
| 870 | "cannot " |
| 871 | "execute model that it does not support." |
| 872 | << std::endl; |
| 873 | GTEST_SKIP(); |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | void Execute(const sp<IDevice>& device, const TestModel& testModel, TestKind testKind) { |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 881 | Model model = createModel(testModel); |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 882 | if (testKind == TestKind::DYNAMIC_SHAPE) { |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 883 | makeOutputDimensionsUnspecified(&model); |
| 884 | } |
| 885 | |
| 886 | sp<IPreparedModel> preparedModel; |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 887 | switch (testKind) { |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 888 | case TestKind::GENERAL: |
| 889 | case TestKind::DYNAMIC_SHAPE: |
Miao Wang | e51e4a9 | 2020-01-23 13:53:21 -0800 | [diff] [blame] | 890 | case TestKind::MEMORY_DOMAIN: |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 891 | case TestKind::FENCED_COMPUTE: |
| 892 | case TestKind::INTINITE_LOOP_TIMEOUT: { |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 893 | createPreparedModel(device, model, &preparedModel); |
| 894 | if (preparedModel == nullptr) return; |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 895 | EvaluatePreparedModel(device, preparedModel, testModel, testKind); |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 896 | } break; |
| 897 | case TestKind::QUANTIZATION_COUPLING: { |
Lev Proleev | 673fdcf | 2020-01-02 18:22:30 +0000 | [diff] [blame] | 898 | ASSERT_TRUE(testModel.hasQuant8CoupledOperands()); |
Michael Butler | 95899b3 | 2020-01-07 14:52:44 -0800 | [diff] [blame] | 899 | createPreparedModel(device, model, &preparedModel, |
| 900 | /*reportSkipping*/ false); |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 901 | TestModel signedQuantizedModel = convertQuant8AsymmOperandsToSigned(testModel); |
| 902 | sp<IPreparedModel> preparedCoupledModel; |
| 903 | createPreparedModel(device, createModel(signedQuantizedModel), &preparedCoupledModel, |
| 904 | /*reportSkipping*/ false); |
| 905 | // If we couldn't prepare a model with unsigned quantization, we must |
| 906 | // fail to prepare a model with signed quantization as well. |
| 907 | if (preparedModel == nullptr) { |
| 908 | ASSERT_EQ(preparedCoupledModel, nullptr); |
| 909 | // If we failed to prepare both of the models, we can safely skip |
| 910 | // the test. |
| 911 | LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot " |
| 912 | "prepare model that it does not support."; |
| 913 | std::cout |
| 914 | << "[ ] Early termination of test because vendor service cannot " |
| 915 | "prepare model that it does not support." |
| 916 | << std::endl; |
| 917 | GTEST_SKIP(); |
| 918 | } |
| 919 | ASSERT_NE(preparedCoupledModel, nullptr); |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 920 | EvaluatePreparedCoupledModels(device, preparedModel, testModel, preparedCoupledModel, |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 921 | signedQuantizedModel); |
| 922 | } break; |
| 923 | } |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | void GeneratedTestBase::SetUp() { |
| 927 | testing::TestWithParam<GeneratedTestParam>::SetUp(); |
| 928 | ASSERT_NE(kDevice, nullptr); |
| 929 | } |
| 930 | |
| 931 | std::vector<NamedModel> getNamedModels(const FilterFn& filter) { |
| 932 | return TestModelManager::get().getTestModels(filter); |
| 933 | } |
| 934 | |
Michael Butler | b1865b6 | 2020-03-19 17:10:34 -0700 | [diff] [blame] | 935 | std::vector<NamedModel> getNamedModels(const FilterNameFn& filter) { |
| 936 | return TestModelManager::get().getTestModels(filter); |
| 937 | } |
| 938 | |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 939 | std::string printGeneratedTest(const testing::TestParamInfo<GeneratedTestParam>& info) { |
| 940 | const auto& [namedDevice, namedModel] = info.param; |
| 941 | return gtestCompliantName(getName(namedDevice) + "_" + getName(namedModel)); |
| 942 | } |
| 943 | |
| 944 | // Tag for the generated tests |
| 945 | class GeneratedTest : public GeneratedTestBase {}; |
| 946 | |
| 947 | // Tag for the dynamic output shape tests |
| 948 | class DynamicOutputShapeTest : public GeneratedTest {}; |
| 949 | |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 950 | // Tag for the memory domain tests |
| 951 | class MemoryDomainTest : public GeneratedTest {}; |
| 952 | |
Miao Wang | e51e4a9 | 2020-01-23 13:53:21 -0800 | [diff] [blame] | 953 | // Tag for the fenced compute tests |
| 954 | class FencedComputeTest : public GeneratedTest {}; |
| 955 | |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 956 | // Tag for the dynamic output shape tests |
Lev Proleev | 3c68b34 | 2020-01-09 16:37:28 +0000 | [diff] [blame] | 957 | class QuantizationCouplingTest : public GeneratedTest {}; |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 958 | |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 959 | // Tag for the loop timeout tests |
| 960 | class InfiniteLoopTimeoutTest : public GeneratedTest {}; |
| 961 | |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 962 | TEST_P(GeneratedTest, Test) { |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 963 | Execute(kDevice, kTestModel, TestKind::GENERAL); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | TEST_P(DynamicOutputShapeTest, Test) { |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 967 | Execute(kDevice, kTestModel, TestKind::DYNAMIC_SHAPE); |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 968 | } |
| 969 | |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 970 | TEST_P(MemoryDomainTest, Test) { |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 971 | Execute(kDevice, kTestModel, TestKind::MEMORY_DOMAIN); |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 972 | } |
| 973 | |
Miao Wang | e51e4a9 | 2020-01-23 13:53:21 -0800 | [diff] [blame] | 974 | TEST_P(FencedComputeTest, Test) { |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 975 | Execute(kDevice, kTestModel, TestKind::FENCED_COMPUTE); |
Miao Wang | e51e4a9 | 2020-01-23 13:53:21 -0800 | [diff] [blame] | 976 | } |
| 977 | |
Lev Proleev | 3c68b34 | 2020-01-09 16:37:28 +0000 | [diff] [blame] | 978 | TEST_P(QuantizationCouplingTest, Test) { |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 979 | Execute(kDevice, kTestModel, TestKind::QUANTIZATION_COUPLING); |
| 980 | } |
| 981 | |
| 982 | TEST_P(InfiniteLoopTimeoutTest, Test) { |
| 983 | Execute(kDevice, kTestModel, TestKind::INTINITE_LOOP_TIMEOUT); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 984 | } |
| 985 | |
| 986 | INSTANTIATE_GENERATED_TEST(GeneratedTest, |
| 987 | [](const TestModel& testModel) { return !testModel.expectFailure; }); |
| 988 | |
Lev Proleev | 53a51cb | 2020-01-20 18:54:46 +0000 | [diff] [blame] | 989 | INSTANTIATE_GENERATED_TEST(DynamicOutputShapeTest, [](const TestModel& testModel) { |
| 990 | return !testModel.expectFailure && !testModel.hasScalarOutputs(); |
| 991 | }); |
Lev Proleev | 13fdfcd | 2019-08-30 11:35:34 +0100 | [diff] [blame] | 992 | |
Xusong Wang | e9da985 | 2020-01-13 11:44:45 -0800 | [diff] [blame] | 993 | INSTANTIATE_GENERATED_TEST(MemoryDomainTest, |
| 994 | [](const TestModel& testModel) { return !testModel.expectFailure; }); |
| 995 | |
Xusong Wang | f07a957 | 2020-03-13 10:02:36 -0700 | [diff] [blame] | 996 | INSTANTIATE_GENERATED_TEST(FencedComputeTest, |
| 997 | [](const TestModel& testModel) { return !testModel.expectFailure; }); |
Miao Wang | e51e4a9 | 2020-01-23 13:53:21 -0800 | [diff] [blame] | 998 | |
Lev Proleev | 3c68b34 | 2020-01-09 16:37:28 +0000 | [diff] [blame] | 999 | INSTANTIATE_GENERATED_TEST(QuantizationCouplingTest, [](const TestModel& testModel) { |
Xusong Wang | 3e64ecc | 2020-03-16 17:13:41 -0700 | [diff] [blame] | 1000 | return !testModel.expectFailure && testModel.hasQuant8CoupledOperands() && |
| 1001 | testModel.main.operations.size() == 1; |
Lev Proleev | 9226c1e | 2019-10-03 14:43:18 +0100 | [diff] [blame] | 1002 | }); |
| 1003 | |
Slava Shklyaev | a37c2c8 | 2020-02-18 16:11:32 +0000 | [diff] [blame] | 1004 | INSTANTIATE_GENERATED_TEST(InfiniteLoopTimeoutTest, [](const TestModel& testModel) { |
| 1005 | return testModel.isInfiniteLoopTimeoutTest(); |
| 1006 | }); |
| 1007 | |
Lev Proleev | 26d1bc8 | 2019-08-30 11:57:18 +0100 | [diff] [blame] | 1008 | } // namespace android::hardware::neuralnetworks::V1_3::vts::functional |