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