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