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