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