blob: 29fdee43985d333305f95b8d514aa1589e04a881 [file] [log] [blame]
Lev Proleev13fdfcd2019-08-30 11:35:34 +01001/*
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 Proleev26d1bc82019-08-30 11:57:18 +010030#include <android/hardware/neuralnetworks/1.2/types.h>
31#include <android/hardware/neuralnetworks/1.3/IDevice.h>
Miao Wang2c4e0232019-12-26 18:03:56 -080032#include <android/hardware/neuralnetworks/1.3/IFencedExecutionCallback.h>
Xusong Wang1b3f4262019-10-25 12:07:17 -070033#include <android/hardware/neuralnetworks/1.3/IPreparedModel.h>
Xusong Wangcc47dff2019-10-23 10:35:07 -070034#include <android/hardware/neuralnetworks/1.3/IPreparedModelCallback.h>
Lev Proleev26d1bc82019-08-30 11:57:18 +010035#include <android/hardware/neuralnetworks/1.3/types.h>
Lev Proleev13fdfcd2019-08-30 11:35:34 +010036#include <android/hidl/allocator/1.0/IAllocator.h>
37#include <android/hidl/memory/1.0/IMemory.h>
Miao Wang2c4e0232019-12-26 18:03:56 -080038#include <android/sync.h>
Lev Proleev56cda832019-12-05 14:49:47 +000039#include <gtest/gtest.h>
Lev Proleev13fdfcd2019-08-30 11:35:34 +010040#include <hidlmemory/mapping.h>
41
Lev Proleev13fdfcd2019-08-30 11:35:34 +010042#include <algorithm>
Michael Butler648ada52019-07-25 17:22:11 -070043#include <chrono>
Lev Proleev13fdfcd2019-08-30 11:35:34 +010044#include <iostream>
45#include <numeric>
Lev Proleev56cda832019-12-05 14:49:47 +000046#include <vector>
Lev Proleev13fdfcd2019-08-30 11:35:34 +010047
48#include "1.0/Utils.h"
Xusong Wangcc47dff2019-10-23 10:35:07 -070049#include "1.3/Callbacks.h"
Michael Butler616701d2020-01-07 14:52:44 -080050#include "1.3/Utils.h"
Lev Proleev13fdfcd2019-08-30 11:35:34 +010051#include "ExecutionBurstController.h"
52#include "MemoryUtils.h"
53#include "TestHarness.h"
54#include "Utils.h"
55#include "VtsHalNeuralnetworks.h"
56
Lev Proleev26d1bc82019-08-30 11:57:18 +010057namespace android::hardware::neuralnetworks::V1_3::vts::functional {
Lev Proleev13fdfcd2019-08-30 11:35:34 +010058
59using namespace test_helper;
60using hidl::memory::V1_0::IMemory;
Michael Butler9449a282019-12-11 19:08:08 -080061using implementation::ExecutionCallback;
Xusong Wangcc47dff2019-10-23 10:35:07 -070062using implementation::PreparedModelCallback;
Lev Proleev13fdfcd2019-08-30 11:35:34 +010063using V1_0::DataLocation;
Xusong Wang1f50e542020-01-13 11:44:45 -080064using V1_0::RequestArgument;
Lev Proleev13fdfcd2019-08-30 11:35:34 +010065using V1_1::ExecutionPreference;
Lev Proleev26d1bc82019-08-30 11:57:18 +010066using V1_2::Constant;
Lev Proleev26d1bc82019-08-30 11:57:18 +010067using V1_2::MeasureTiming;
Lev Proleev26d1bc82019-08-30 11:57:18 +010068using V1_2::OutputShape;
69using V1_2::SymmPerChannelQuantParams;
70using V1_2::Timing;
Lev Proleev13fdfcd2019-08-30 11:35:34 +010071using HidlToken = hidl_array<uint8_t, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
72
Lev Proleev0d4ba3f2019-10-02 17:32:06 +010073namespace {
74
Slava Shklyaevcc873ae2020-02-18 16:11:32 +000075enum class OutputType { FULLY_SPECIFIED, UNSPECIFIED, INSUFFICIENT, MISSED_DEADLINE };
Lev Proleev13fdfcd2019-08-30 11:35:34 +010076
Xusong Wang1f50e542020-01-13 11:44:45 -080077enum class MemoryType { SHARED, DEVICE };
78
79enum class IOType { INPUT, OUTPUT };
80
Lev Proleev0d4ba3f2019-10-02 17:32:06 +010081struct TestConfig {
82 Executor executor;
83 MeasureTiming measureTiming;
84 OutputType outputType;
Xusong Wang1f50e542020-01-13 11:44:45 -080085 MemoryType memoryType;
Lev Proleev9226c1e2019-10-03 14:43:18 +010086 // `reportSkipping` indicates if a test should print an info message in case
87 // it is skipped. The field is set to true by default and is set to false in
88 // quantization coupling tests to suppress skipping a test
89 bool reportSkipping;
Xusong Wang1f50e542020-01-13 11:44:45 -080090 TestConfig(Executor executor, MeasureTiming measureTiming, OutputType outputType,
91 MemoryType memoryType)
Lev Proleev9226c1e2019-10-03 14:43:18 +010092 : executor(executor),
93 measureTiming(measureTiming),
94 outputType(outputType),
Xusong Wang1f50e542020-01-13 11:44:45 -080095 memoryType(memoryType),
Lev Proleev9226c1e2019-10-03 14:43:18 +010096 reportSkipping(true) {}
97 TestConfig(Executor executor, MeasureTiming measureTiming, OutputType outputType,
Xusong Wang1f50e542020-01-13 11:44:45 -080098 MemoryType memoryType, bool reportSkipping)
Lev Proleev9226c1e2019-10-03 14:43:18 +010099 : executor(executor),
100 measureTiming(measureTiming),
101 outputType(outputType),
Xusong Wang1f50e542020-01-13 11:44:45 -0800102 memoryType(memoryType),
Lev Proleev9226c1e2019-10-03 14:43:18 +0100103 reportSkipping(reportSkipping) {}
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100104};
105
Xusong Wang1f50e542020-01-13 11:44:45 -0800106class DeviceMemoryAllocator {
107 public:
108 DeviceMemoryAllocator(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel,
109 const TestModel& testModel)
110 : kDevice(device), kPreparedModel(preparedModel), kTestModel(testModel) {}
111
112 // Allocate device memory for a target input/output operand.
113 // Return {IBuffer object, token} if successful.
114 // Return {nullptr, 0} if device memory is not supported.
115 template <IOType ioType>
Michael Butler6a4172c2020-02-04 16:15:04 -0800116 std::pair<sp<IBuffer>, uint32_t> allocate(uint32_t index) {
117 std::pair<sp<IBuffer>, uint32_t> buffer;
Xusong Wang1f50e542020-01-13 11:44:45 -0800118 allocateInternal<ioType>(index, &buffer);
119 return buffer;
120 }
121
122 private:
123 template <IOType ioType>
Michael Butler6a4172c2020-02-04 16:15:04 -0800124 void allocateInternal(uint32_t index, std::pair<sp<IBuffer>, uint32_t>* result) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800125 ASSERT_NE(result, nullptr);
126
127 // Prepare arguments.
128 BufferRole role = {.modelIndex = 0, .ioIndex = index, .frequency = 1.0f};
129 hidl_vec<BufferRole> inputRoles, outputRoles;
130 if constexpr (ioType == IOType::INPUT) {
131 inputRoles = {role};
132 } else {
133 outputRoles = {role};
134 }
135
136 // Allocate device memory.
137 ErrorStatus status;
138 sp<IBuffer> buffer;
Michael Butler6a4172c2020-02-04 16:15:04 -0800139 uint32_t token;
140 auto cb = [&status, &buffer, &token](ErrorStatus error, const sp<IBuffer>& buf,
141 uint32_t tok) {
142 status = error;
143 buffer = buf;
144 token = tok;
145 };
146 const auto ret = kDevice->allocate({}, {kPreparedModel}, inputRoles, outputRoles, cb);
Xusong Wang1f50e542020-01-13 11:44:45 -0800147
148 // Check allocation results.
149 ASSERT_TRUE(ret.isOk());
150 if (status == ErrorStatus::NONE) {
151 ASSERT_NE(buffer, nullptr);
152 ASSERT_GT(token, 0);
153 } else {
154 ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE);
155 ASSERT_EQ(buffer, nullptr);
156 ASSERT_EQ(token, 0);
157 }
158
159 // Initialize input data from TestBuffer.
160 if constexpr (ioType == IOType::INPUT) {
161 if (buffer != nullptr) {
162 // TestBuffer -> Shared memory.
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000163 const auto& testBuffer =
164 kTestModel.main.operands[kTestModel.main.inputIndexes[index]].data;
Xusong Wang1f50e542020-01-13 11:44:45 -0800165 ASSERT_GT(testBuffer.size(), 0);
166 hidl_memory tmp = nn::allocateSharedMemory(testBuffer.size());
167 sp<IMemory> inputMemory = mapMemory(tmp);
168 ASSERT_NE(inputMemory.get(), nullptr);
169 uint8_t* inputPtr =
170 static_cast<uint8_t*>(static_cast<void*>(inputMemory->getPointer()));
171 ASSERT_NE(inputPtr, nullptr);
172 const uint8_t* begin = testBuffer.get<uint8_t>();
173 const uint8_t* end = begin + testBuffer.size();
174 std::copy(begin, end, inputPtr);
175
176 // Shared memory -> IBuffer.
177 auto ret = buffer->copyFrom(tmp, {});
178 ASSERT_TRUE(ret.isOk());
179 ASSERT_EQ(static_cast<ErrorStatus>(ret), ErrorStatus::NONE);
180 }
181 }
182 *result = {std::move(buffer), token};
183 }
184
185 const sp<IDevice> kDevice;
186 const sp<IPreparedModel> kPreparedModel;
187 const TestModel& kTestModel;
188};
189
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000190Subgraph createSubgraph(const TestSubgraph& testSubgraph, uint32_t* constCopySize,
191 std::vector<const TestBuffer*>* constCopies, uint32_t* constRefSize,
192 std::vector<const TestBuffer*>* constReferences) {
193 CHECK(constCopySize != nullptr);
194 CHECK(constCopies != nullptr);
195 CHECK(constRefSize != nullptr);
196 CHECK(constReferences != nullptr);
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100197
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000198 // Operands.
199 hidl_vec<Operand> operands(testSubgraph.operands.size());
200 for (uint32_t i = 0; i < testSubgraph.operands.size(); i++) {
201 const auto& op = testSubgraph.operands[i];
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100202
203 DataLocation loc = {};
204 if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) {
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000205 loc = {
206 .poolIndex = 0,
207 .offset = *constCopySize,
208 .length = static_cast<uint32_t>(op.data.size()),
209 };
210 constCopies->push_back(&op.data);
211 *constCopySize += op.data.alignedSize();
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100212 } else if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) {
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000213 loc = {
214 .poolIndex = 0,
215 .offset = *constRefSize,
216 .length = static_cast<uint32_t>(op.data.size()),
217 };
218 constReferences->push_back(&op.data);
219 *constRefSize += op.data.alignedSize();
220 } else if (op.lifetime == TestOperandLifeTime::SUBGRAPH) {
221 loc = {
222 .poolIndex = 0,
223 .offset = *op.data.get<uint32_t>(),
224 .length = 0,
225 };
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100226 }
227
Michael Butler6a4172c2020-02-04 16:15:04 -0800228 V1_2::Operand::ExtraParams extraParams;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100229 if (op.type == TestOperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) {
230 extraParams.channelQuant(SymmPerChannelQuantParams{
231 .scales = op.channelQuant.scales, .channelDim = op.channelQuant.channelDim});
232 }
233
234 operands[i] = {.type = static_cast<OperandType>(op.type),
235 .dimensions = op.dimensions,
236 .numberOfConsumers = op.numberOfConsumers,
237 .scale = op.scale,
238 .zeroPoint = op.zeroPoint,
239 .lifetime = static_cast<OperandLifeTime>(op.lifetime),
240 .location = loc,
241 .extraParams = std::move(extraParams)};
242 }
243
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000244 // Operations.
245 hidl_vec<Operation> operations(testSubgraph.operations.size());
246 std::transform(testSubgraph.operations.begin(), testSubgraph.operations.end(),
247 operations.begin(), [](const TestOperation& op) -> Operation {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100248 return {.type = static_cast<OperationType>(op.type),
249 .inputs = op.inputs,
250 .outputs = op.outputs};
251 });
252
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000253 return {.operands = std::move(operands),
254 .operations = std::move(operations),
255 .inputIndexes = testSubgraph.inputIndexes,
256 .outputIndexes = testSubgraph.outputIndexes};
257}
258
259void copyTestBuffers(const std::vector<const TestBuffer*>& buffers, uint8_t* output) {
260 uint32_t offset = 0;
261 for (const TestBuffer* buffer : buffers) {
262 const uint8_t* begin = buffer->get<uint8_t>();
263 const uint8_t* end = begin + buffer->size();
264 std::copy(begin, end, output + offset);
265 offset += buffer->alignedSize();
266 }
267}
268
269} // namespace
270
Xusong Wang9c415912020-03-20 14:49:54 -0700271void waitForSyncFence(int syncFd) {
272 constexpr int kInfiniteTimeout = -1;
273 ASSERT_GT(syncFd, 0);
274 int r = sync_wait(syncFd, kInfiniteTimeout);
275 ASSERT_GE(r, 0);
276}
277
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000278Model createModel(const TestModel& testModel) {
279 uint32_t constCopySize = 0;
280 uint32_t constRefSize = 0;
281 std::vector<const TestBuffer*> constCopies;
282 std::vector<const TestBuffer*> constReferences;
283
284 Subgraph mainSubgraph = createSubgraph(testModel.main, &constCopySize, &constCopies,
285 &constRefSize, &constReferences);
286 hidl_vec<Subgraph> refSubgraphs(testModel.referenced.size());
287 std::transform(testModel.referenced.begin(), testModel.referenced.end(), refSubgraphs.begin(),
288 [&constCopySize, &constCopies, &constRefSize,
289 &constReferences](const TestSubgraph& testSubgraph) {
290 return createSubgraph(testSubgraph, &constCopySize, &constCopies,
291 &constRefSize, &constReferences);
292 });
293
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100294 // Constant copies.
295 hidl_vec<uint8_t> operandValues(constCopySize);
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000296 copyTestBuffers(constCopies, operandValues.data());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100297
298 // Shared memory.
299 hidl_vec<hidl_memory> pools = {};
300 if (constRefSize > 0) {
301 hidl_vec_push_back(&pools, nn::allocateSharedMemory(constRefSize));
302 CHECK_NE(pools[0].size(), 0u);
303
304 // load data
305 sp<IMemory> mappedMemory = mapMemory(pools[0]);
306 CHECK(mappedMemory.get() != nullptr);
307 uint8_t* mappedPtr =
308 reinterpret_cast<uint8_t*>(static_cast<void*>(mappedMemory->getPointer()));
309 CHECK(mappedPtr != nullptr);
310
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000311 copyTestBuffers(constReferences, mappedPtr);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100312 }
313
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000314 return {.main = std::move(mainSubgraph),
315 .referenced = std::move(refSubgraphs),
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100316 .operandValues = std::move(operandValues),
317 .pools = std::move(pools),
318 .relaxComputationFloat32toFloat16 = testModel.isRelaxed};
319}
320
321static bool isOutputSizeGreaterThanOne(const TestModel& testModel, uint32_t index) {
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000322 const auto byteSize = testModel.main.operands[testModel.main.outputIndexes[index]].data.size();
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100323 return byteSize > 1u;
324}
325
Xusong Wang1f50e542020-01-13 11:44:45 -0800326static void makeOutputInsufficientSize(uint32_t outputIndex, Request* request) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100327 auto& length = request->outputs[outputIndex].location.length;
328 ASSERT_GT(length, 1u);
329 length -= 1u;
330}
331
332static void makeOutputDimensionsUnspecified(Model* model) {
Slava Shklyaeva785a3f2019-12-13 12:24:35 +0000333 for (auto i : model->main.outputIndexes) {
334 auto& dims = model->main.operands[i].dimensions;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100335 std::fill(dims.begin(), dims.end(), 0);
336 }
337}
338
Xusong Wang1f50e542020-01-13 11:44:45 -0800339constexpr uint32_t kInputPoolIndex = 0;
340constexpr uint32_t kOutputPoolIndex = 1;
341constexpr uint32_t kDeviceMemoryBeginIndex = 2;
342
343static std::pair<Request, std::vector<sp<IBuffer>>> createRequest(
344 const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel,
345 const TestModel& testModel, bool preferDeviceMemory) {
346 // Memory pools are organized as:
347 // - 0: Input shared memory pool
348 // - 1: Output shared memory pool
349 // - [2, 2+i): Input device memories
350 // - [2+i, 2+i+o): Output device memories
351 DeviceMemoryAllocator allocator(device, preparedModel, testModel);
352 std::vector<sp<IBuffer>> buffers;
Michael Butler6a4172c2020-02-04 16:15:04 -0800353 std::vector<uint32_t> tokens;
Xusong Wang1f50e542020-01-13 11:44:45 -0800354
355 // Model inputs.
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000356 hidl_vec<RequestArgument> inputs(testModel.main.inputIndexes.size());
Xusong Wang1f50e542020-01-13 11:44:45 -0800357 size_t inputSize = 0;
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000358 for (uint32_t i = 0; i < testModel.main.inputIndexes.size(); i++) {
359 const auto& op = testModel.main.operands[testModel.main.inputIndexes[i]];
Xusong Wang1f50e542020-01-13 11:44:45 -0800360 if (op.data.size() == 0) {
361 // Omitted input.
362 inputs[i] = {.hasNoValue = true};
363 continue;
364 } else if (preferDeviceMemory) {
365 SCOPED_TRACE("Input index = " + std::to_string(i));
366 auto [buffer, token] = allocator.allocate<IOType::INPUT>(i);
367 if (buffer != nullptr) {
368 DataLocation loc = {.poolIndex = static_cast<uint32_t>(buffers.size() +
369 kDeviceMemoryBeginIndex)};
370 buffers.push_back(std::move(buffer));
371 tokens.push_back(token);
372 inputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}};
373 continue;
374 }
375 }
376
377 // Reserve shared memory for input.
378 DataLocation loc = {.poolIndex = kInputPoolIndex,
379 .offset = static_cast<uint32_t>(inputSize),
380 .length = static_cast<uint32_t>(op.data.size())};
381 inputSize += op.data.alignedSize();
382 inputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}};
383 }
384
385 // Model outputs.
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000386 hidl_vec<RequestArgument> outputs(testModel.main.outputIndexes.size());
Xusong Wang1f50e542020-01-13 11:44:45 -0800387 size_t outputSize = 0;
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000388 for (uint32_t i = 0; i < testModel.main.outputIndexes.size(); i++) {
389 const auto& op = testModel.main.operands[testModel.main.outputIndexes[i]];
Xusong Wang1f50e542020-01-13 11:44:45 -0800390 if (preferDeviceMemory) {
391 SCOPED_TRACE("Output index = " + std::to_string(i));
392 auto [buffer, token] = allocator.allocate<IOType::OUTPUT>(i);
393 if (buffer != nullptr) {
394 DataLocation loc = {.poolIndex = static_cast<uint32_t>(buffers.size() +
395 kDeviceMemoryBeginIndex)};
396 buffers.push_back(std::move(buffer));
397 tokens.push_back(token);
398 outputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}};
399 continue;
400 }
401 }
402
403 // In the case of zero-sized output, we should at least provide a one-byte buffer.
404 // This is because zero-sized tensors are only supported internally to the driver, or
405 // reported in output shapes. It is illegal for the client to pre-specify a zero-sized
406 // tensor as model output. Otherwise, we will have two semantic conflicts:
407 // - "Zero dimension" conflicts with "unspecified dimension".
408 // - "Omitted operand buffer" conflicts with "zero-sized operand buffer".
409 size_t bufferSize = std::max<size_t>(op.data.size(), 1);
410
411 // Reserve shared memory for output.
412 DataLocation loc = {.poolIndex = kOutputPoolIndex,
413 .offset = static_cast<uint32_t>(outputSize),
414 .length = static_cast<uint32_t>(bufferSize)};
415 outputSize += op.data.size() == 0 ? TestBuffer::kAlignment : op.data.alignedSize();
416 outputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}};
417 }
418
419 // Memory pools.
420 hidl_vec<Request::MemoryPool> pools(kDeviceMemoryBeginIndex + buffers.size());
421 pools[kInputPoolIndex].hidlMemory(nn::allocateSharedMemory(std::max<size_t>(inputSize, 1)));
422 pools[kOutputPoolIndex].hidlMemory(nn::allocateSharedMemory(std::max<size_t>(outputSize, 1)));
423 CHECK_NE(pools[kInputPoolIndex].hidlMemory().size(), 0u);
424 CHECK_NE(pools[kOutputPoolIndex].hidlMemory().size(), 0u);
425 for (uint32_t i = 0; i < buffers.size(); i++) {
426 pools[kDeviceMemoryBeginIndex + i].token(tokens[i]);
427 }
428
429 // Copy input data to the input shared memory pool.
430 sp<IMemory> inputMemory = mapMemory(pools[kInputPoolIndex].hidlMemory());
431 CHECK(inputMemory.get() != nullptr);
432 uint8_t* inputPtr = static_cast<uint8_t*>(static_cast<void*>(inputMemory->getPointer()));
433 CHECK(inputPtr != nullptr);
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000434 for (uint32_t i = 0; i < testModel.main.inputIndexes.size(); i++) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800435 if (!inputs[i].hasNoValue && inputs[i].location.poolIndex == kInputPoolIndex) {
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000436 const auto& op = testModel.main.operands[testModel.main.inputIndexes[i]];
Xusong Wang1f50e542020-01-13 11:44:45 -0800437 const uint8_t* begin = op.data.get<uint8_t>();
438 const uint8_t* end = begin + op.data.size();
439 std::copy(begin, end, inputPtr + inputs[i].location.offset);
440 }
441 }
442
443 Request request = {
444 .inputs = std::move(inputs), .outputs = std::move(outputs), .pools = std::move(pools)};
445 return {std::move(request), std::move(buffers)};
446}
447
448// Get a TestBuffer with data copied from an IBuffer object.
449static void getBuffer(const sp<IBuffer>& buffer, size_t size, TestBuffer* testBuffer) {
450 // IBuffer -> Shared memory.
451 hidl_memory tmp = nn::allocateSharedMemory(size);
452 const auto ret = buffer->copyTo(tmp);
453 ASSERT_TRUE(ret.isOk());
454 ASSERT_EQ(static_cast<ErrorStatus>(ret), ErrorStatus::NONE);
455
456 // Shared memory -> TestBuffer.
457 sp<IMemory> outputMemory = mapMemory(tmp);
458 ASSERT_NE(outputMemory.get(), nullptr);
459 uint8_t* outputPtr = static_cast<uint8_t*>(static_cast<void*>(outputMemory->getPointer()));
460 ASSERT_NE(outputPtr, nullptr);
461 ASSERT_NE(testBuffer, nullptr);
462 *testBuffer = TestBuffer(size, outputPtr);
463}
464
465static std::vector<TestBuffer> getOutputBuffers(const TestModel& testModel, const Request& request,
466 const std::vector<sp<IBuffer>>& buffers) {
467 sp<IMemory> outputMemory = mapMemory(request.pools[kOutputPoolIndex].hidlMemory());
468 CHECK(outputMemory.get() != nullptr);
469 uint8_t* outputPtr = static_cast<uint8_t*>(static_cast<void*>(outputMemory->getPointer()));
470 CHECK(outputPtr != nullptr);
471
472 // Copy out output results.
473 std::vector<TestBuffer> outputBuffers;
474 for (uint32_t i = 0; i < request.outputs.size(); i++) {
475 const auto& outputLoc = request.outputs[i].location;
476 if (outputLoc.poolIndex == kOutputPoolIndex) {
477 outputBuffers.emplace_back(outputLoc.length, outputPtr + outputLoc.offset);
478 } else {
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000479 const auto& op = testModel.main.operands[testModel.main.outputIndexes[i]];
Xusong Wang1f50e542020-01-13 11:44:45 -0800480 if (op.data.size() == 0) {
481 outputBuffers.emplace_back();
482 } else {
483 SCOPED_TRACE("Output index = " + std::to_string(i));
484 const uint32_t bufferIndex = outputLoc.poolIndex - kDeviceMemoryBeginIndex;
485 TestBuffer buffer;
486 getBuffer(buffers[bufferIndex], op.data.size(), &buffer);
487 outputBuffers.push_back(std::move(buffer));
488 }
489 }
490 }
491 return outputBuffers;
492}
493
Xusong Wang5882d6d2020-03-13 10:02:36 -0700494static bool hasZeroSizedOutput(const TestModel& testModel) {
495 return std::any_of(testModel.main.outputIndexes.begin(), testModel.main.outputIndexes.end(),
496 [&testModel](uint32_t index) {
497 return testModel.main.operands[index].data.size() == 0;
498 });
499}
500
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100501static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel,
502 const Request& request, MeasureTiming measure,
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000503 const OptionalTimeoutDuration& loopTimeoutDuration,
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100504 sp<ExecutionCallback>& callback) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000505 return preparedModel->execute_1_3(request, measure, {}, loopTimeoutDuration, callback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100506}
507static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel,
508 const Request& request, MeasureTiming measure,
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000509 const OptionalTimeoutDuration& loopTimeoutDuration,
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100510 hidl_vec<OutputShape>* outputShapes,
511 Timing* timing) {
512 ErrorStatus result;
Xusong Wangebd88ba2019-10-28 11:11:19 -0700513 Return<void> ret = preparedModel->executeSynchronously_1_3(
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000514 request, measure, {}, loopTimeoutDuration,
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100515 [&result, outputShapes, timing](ErrorStatus error, const hidl_vec<OutputShape>& shapes,
516 const Timing& time) {
517 result = error;
518 *outputShapes = shapes;
519 *timing = time;
520 });
521 if (!ret.isOk()) {
522 return ErrorStatus::GENERAL_FAILURE;
523 }
524 return result;
525}
526static std::shared_ptr<::android::nn::ExecutionBurstController> CreateBurst(
527 const sp<IPreparedModel>& preparedModel) {
Michael Butler648ada52019-07-25 17:22:11 -0700528 return android::nn::ExecutionBurstController::create(preparedModel,
529 std::chrono::microseconds{0});
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100530}
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100531
Xusong Wang1f50e542020-01-13 11:44:45 -0800532void EvaluatePreparedModel(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel,
533 const TestModel& testModel, const TestConfig& testConfig,
534 bool* skipped = nullptr) {
Lev Proleev9226c1e2019-10-03 14:43:18 +0100535 if (skipped != nullptr) {
536 *skipped = false;
537 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100538 // If output0 does not have size larger than one byte, we can not test with insufficient buffer.
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100539 if (testConfig.outputType == OutputType::INSUFFICIENT &&
540 !isOutputSizeGreaterThanOne(testModel, 0)) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100541 return;
542 }
543
Xusong Wang1f50e542020-01-13 11:44:45 -0800544 auto [request, buffers] =
545 createRequest(device, preparedModel, testModel,
546 /*preferDeviceMemory=*/testConfig.memoryType == MemoryType::DEVICE);
547 // Skip if testing memory domain but no device memory has been allocated.
548 if (testConfig.memoryType == MemoryType::DEVICE && buffers.empty()) {
549 return;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100550 }
Xusong Wang1f50e542020-01-13 11:44:45 -0800551 if (testConfig.outputType == OutputType::INSUFFICIENT) {
552 makeOutputInsufficientSize(/*outputIndex=*/0, &request);
553 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100554
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000555 OptionalTimeoutDuration loopTimeoutDuration;
556 // OutputType::MISSED_DEADLINE is only used by
557 // TestKind::INTINITE_LOOP_TIMEOUT tests to verify that an infinite loop is
558 // aborted after a timeout.
559 if (testConfig.outputType == OutputType::MISSED_DEADLINE) {
560 // Override the default loop timeout duration with a small value to
561 // speed up test execution.
562 constexpr uint64_t kMillisecond = 1'000'000;
563 loopTimeoutDuration.nanoseconds(1 * kMillisecond);
564 }
565
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100566 ErrorStatus executionStatus;
567 hidl_vec<OutputShape> outputShapes;
568 Timing timing;
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100569 switch (testConfig.executor) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100570 case Executor::ASYNC: {
571 SCOPED_TRACE("asynchronous");
572
573 // launch execution
574 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000575 Return<ErrorStatus> executionLaunchStatus =
576 ExecutePreparedModel(preparedModel, request, testConfig.measureTiming,
577 loopTimeoutDuration, executionCallback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100578 ASSERT_TRUE(executionLaunchStatus.isOk());
579 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
580
581 // retrieve execution status
582 executionCallback->wait();
583 executionStatus = executionCallback->getStatus();
584 outputShapes = executionCallback->getOutputShapes();
585 timing = executionCallback->getTiming();
586
587 break;
588 }
589 case Executor::SYNC: {
590 SCOPED_TRACE("synchronous");
591
592 // execute
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000593 Return<ErrorStatus> executionReturnStatus =
594 ExecutePreparedModel(preparedModel, request, testConfig.measureTiming,
595 loopTimeoutDuration, &outputShapes, &timing);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100596 ASSERT_TRUE(executionReturnStatus.isOk());
597 executionStatus = static_cast<ErrorStatus>(executionReturnStatus);
598
599 break;
600 }
601 case Executor::BURST: {
Xusong Wang931d5a12019-11-27 12:46:48 -0800602 // TODO(butlermichael): Check if we need to test burst in V1_3 if the interface remains
603 // V1_2.
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100604 SCOPED_TRACE("burst");
605
Xusong Wang1f50e542020-01-13 11:44:45 -0800606 // check compliance
607 ASSERT_TRUE(nn::compliantWithV1_0(request));
608 V1_0::Request request10 = nn::convertToV1_0(request);
609
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100610 // create burst
611 const std::shared_ptr<::android::nn::ExecutionBurstController> controller =
612 CreateBurst(preparedModel);
613 ASSERT_NE(nullptr, controller.get());
614
615 // create memory keys
Xusong Wang931d5a12019-11-27 12:46:48 -0800616 std::vector<intptr_t> keys(request10.pools.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100617 for (size_t i = 0; i < keys.size(); ++i) {
Xusong Wang931d5a12019-11-27 12:46:48 -0800618 keys[i] = reinterpret_cast<intptr_t>(&request10.pools[i]);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100619 }
620
621 // execute burst
Michael Butler648ada52019-07-25 17:22:11 -0700622 int n;
623 std::tie(n, outputShapes, timing, std::ignore) =
Xusong Wang931d5a12019-11-27 12:46:48 -0800624 controller->compute(request10, testConfig.measureTiming, keys);
Michael Butler648ada52019-07-25 17:22:11 -0700625 executionStatus = nn::convertResultCodeToErrorStatus(n);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100626
627 break;
628 }
Miao Wang2c4e0232019-12-26 18:03:56 -0800629 case Executor::FENCED: {
630 SCOPED_TRACE("fenced");
631 ErrorStatus result;
Miao Wang3fd70b02020-01-21 13:15:09 -0800632 hidl_handle syncFenceHandle;
633 sp<IFencedExecutionCallback> fencedCallback;
Miao Wang34dfa2f2020-02-25 15:54:08 -0800634 auto callbackFunc = [&result, &syncFenceHandle, &fencedCallback](
635 ErrorStatus error, const hidl_handle& handle,
636 const sp<IFencedExecutionCallback>& callback) {
637 result = error;
638 syncFenceHandle = handle;
639 fencedCallback = callback;
640 };
641 Return<void> ret =
642 preparedModel->executeFenced(request, {}, testConfig.measureTiming, {},
643 loopTimeoutDuration, {}, callbackFunc);
Miao Wang2c4e0232019-12-26 18:03:56 -0800644 ASSERT_TRUE(ret.isOk());
645 if (result != ErrorStatus::NONE) {
Miao Wang3fd70b02020-01-21 13:15:09 -0800646 ASSERT_EQ(syncFenceHandle.getNativeHandle(), nullptr);
647 ASSERT_EQ(fencedCallback, nullptr);
Miao Wangfa161c72020-02-25 16:57:53 -0800648 executionStatus = result;
Miao Wang3fd70b02020-01-21 13:15:09 -0800649 } else if (syncFenceHandle.getNativeHandle()) {
Miao Wang34dfa2f2020-02-25 15:54:08 -0800650 // If a sync fence is returned, try start another run waiting for the sync fence.
651 ret = preparedModel->executeFenced(request, {syncFenceHandle},
652 testConfig.measureTiming, {},
653 loopTimeoutDuration, {}, callbackFunc);
654 ASSERT_TRUE(ret.isOk());
655 ASSERT_EQ(result, ErrorStatus::NONE);
Miao Wang3fd70b02020-01-21 13:15:09 -0800656 waitForSyncFence(syncFenceHandle.getNativeHandle()->data[0]);
Miao Wang2c4e0232019-12-26 18:03:56 -0800657 }
658 if (result == ErrorStatus::NONE) {
Miao Wang3fd70b02020-01-21 13:15:09 -0800659 ASSERT_NE(fencedCallback, nullptr);
660 Return<void> ret = fencedCallback->getExecutionInfo(
661 [&executionStatus, &timing](ErrorStatus error, Timing t, Timing) {
Miao Wang2c4e0232019-12-26 18:03:56 -0800662 executionStatus = error;
663 timing = t;
664 });
665 ASSERT_TRUE(ret.isOk());
666 }
667 break;
668 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100669 }
670
Miao Wangfa161c72020-02-25 16:57:53 -0800671 if (testConfig.outputType != OutputType::FULLY_SPECIFIED &&
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100672 executionStatus == ErrorStatus::GENERAL_FAILURE) {
Lev Proleev9226c1e2019-10-03 14:43:18 +0100673 if (skipped != nullptr) {
674 *skipped = true;
675 }
676 if (!testConfig.reportSkipping) {
677 return;
678 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100679 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
680 "execute model that it does not support.";
681 std::cout << "[ ] Early termination of test because vendor service cannot "
682 "execute model that it does not support."
683 << std::endl;
684 GTEST_SKIP();
685 }
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100686 if (testConfig.measureTiming == MeasureTiming::NO) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100687 EXPECT_EQ(UINT64_MAX, timing.timeOnDevice);
688 EXPECT_EQ(UINT64_MAX, timing.timeInDriver);
689 } else {
690 if (timing.timeOnDevice != UINT64_MAX && timing.timeInDriver != UINT64_MAX) {
691 EXPECT_LE(timing.timeOnDevice, timing.timeInDriver);
692 }
693 }
694
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100695 switch (testConfig.outputType) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100696 case OutputType::FULLY_SPECIFIED:
Xusong Wang5882d6d2020-03-13 10:02:36 -0700697 if (testConfig.executor == Executor::FENCED && hasZeroSizedOutput(testModel)) {
698 // Executor::FENCED does not support zero-sized output.
699 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
700 return;
701 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100702 // If the model output operands are fully specified, outputShapes must be either
703 // either empty, or have the same number of elements as the number of outputs.
704 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
705 ASSERT_TRUE(outputShapes.size() == 0 ||
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000706 outputShapes.size() == testModel.main.outputIndexes.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100707 break;
708 case OutputType::UNSPECIFIED:
Miao Wangfa161c72020-02-25 16:57:53 -0800709 if (testConfig.executor == Executor::FENCED) {
710 // For Executor::FENCED, the output shape must be fully specified.
711 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
712 return;
713 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100714 // If the model output operands are not fully specified, outputShapes must have
715 // the same number of elements as the number of outputs.
716 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000717 ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100718 break;
719 case OutputType::INSUFFICIENT:
Miao Wangfa161c72020-02-25 16:57:53 -0800720 if (testConfig.executor == Executor::FENCED) {
721 // For Executor::FENCED, the output shape must be fully specified.
722 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
723 return;
724 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100725 ASSERT_EQ(ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, executionStatus);
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000726 ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100727 ASSERT_FALSE(outputShapes[0].isSufficient);
728 return;
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000729 case OutputType::MISSED_DEADLINE:
730 ASSERT_TRUE(executionStatus == ErrorStatus::MISSED_DEADLINE_TRANSIENT ||
731 executionStatus == ErrorStatus::MISSED_DEADLINE_PERSISTENT)
732 << "executionStatus = " << executionStatus;
733 return;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100734 }
735
736 // Go through all outputs, check returned output shapes.
737 for (uint32_t i = 0; i < outputShapes.size(); i++) {
738 EXPECT_TRUE(outputShapes[i].isSufficient);
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000739 const auto& expect = testModel.main.operands[testModel.main.outputIndexes[i]].dimensions;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100740 const std::vector<uint32_t> actual = outputShapes[i].dimensions;
741 EXPECT_EQ(expect, actual);
742 }
743
744 // Retrieve execution results.
Xusong Wang1f50e542020-01-13 11:44:45 -0800745 const std::vector<TestBuffer> outputs = getOutputBuffers(testModel, request, buffers);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100746
747 // We want "close-enough" results.
748 checkResults(testModel, outputs);
749}
750
Xusong Wang1f50e542020-01-13 11:44:45 -0800751void EvaluatePreparedModel(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel,
752 const TestModel& testModel, TestKind testKind) {
Lev Proleev56cda832019-12-05 14:49:47 +0000753 std::vector<OutputType> outputTypesList;
754 std::vector<MeasureTiming> measureTimingList;
755 std::vector<Executor> executorList;
Xusong Wang1f50e542020-01-13 11:44:45 -0800756 MemoryType memoryType = MemoryType::SHARED;
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100757
Lev Proleev9226c1e2019-10-03 14:43:18 +0100758 switch (testKind) {
759 case TestKind::GENERAL: {
760 outputTypesList = {OutputType::FULLY_SPECIFIED};
761 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wang33173a32020-01-23 13:53:21 -0800762 executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100763 } break;
764 case TestKind::DYNAMIC_SHAPE: {
765 outputTypesList = {OutputType::UNSPECIFIED, OutputType::INSUFFICIENT};
766 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wangfa161c72020-02-25 16:57:53 -0800767 executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST, Executor::FENCED};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100768 } break;
Xusong Wang1f50e542020-01-13 11:44:45 -0800769 case TestKind::MEMORY_DOMAIN: {
770 outputTypesList = {OutputType::FULLY_SPECIFIED};
771 measureTimingList = {MeasureTiming::NO};
Miao Wang34dfa2f2020-02-25 15:54:08 -0800772 executorList = {Executor::ASYNC, Executor::SYNC, Executor::FENCED};
Xusong Wang1f50e542020-01-13 11:44:45 -0800773 memoryType = MemoryType::DEVICE;
774 } break;
Miao Wang33173a32020-01-23 13:53:21 -0800775 case TestKind::FENCED_COMPUTE: {
776 outputTypesList = {OutputType::FULLY_SPECIFIED};
777 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
778 executorList = {Executor::FENCED};
779 } break;
Lev Proleev9226c1e2019-10-03 14:43:18 +0100780 case TestKind::QUANTIZATION_COUPLING: {
781 LOG(FATAL) << "Wrong TestKind for EvaluatePreparedModel";
782 return;
783 } break;
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000784 case TestKind::INTINITE_LOOP_TIMEOUT: {
785 outputTypesList = {OutputType::MISSED_DEADLINE};
786 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
787 // Burst does not support V1_3 loop timeout.
788 executorList = {Executor::ASYNC, Executor::SYNC, Executor::FENCED};
789 } break;
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100790 }
791
792 for (const OutputType outputType : outputTypesList) {
793 for (const MeasureTiming measureTiming : measureTimingList) {
794 for (const Executor executor : executorList) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800795 const TestConfig testConfig(executor, measureTiming, outputType, memoryType);
796 EvaluatePreparedModel(device, preparedModel, testModel, testConfig);
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100797 }
798 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100799 }
800}
801
Xusong Wang1f50e542020-01-13 11:44:45 -0800802void EvaluatePreparedCoupledModels(const sp<IDevice>& device,
803 const sp<IPreparedModel>& preparedModel,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100804 const TestModel& testModel,
805 const sp<IPreparedModel>& preparedCoupledModel,
806 const TestModel& coupledModel) {
Lev Proleev56cda832019-12-05 14:49:47 +0000807 const std::vector<OutputType> outputTypesList = {OutputType::FULLY_SPECIFIED};
808 const std::vector<MeasureTiming> measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wang2c4e0232019-12-26 18:03:56 -0800809 const std::vector<Executor> executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST,
810 Executor::FENCED};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100811
812 for (const OutputType outputType : outputTypesList) {
813 for (const MeasureTiming measureTiming : measureTimingList) {
814 for (const Executor executor : executorList) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800815 const TestConfig testConfig(executor, measureTiming, outputType, MemoryType::SHARED,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100816 /*reportSkipping=*/false);
817 bool baseSkipped = false;
Xusong Wang1f50e542020-01-13 11:44:45 -0800818 EvaluatePreparedModel(device, preparedModel, testModel, testConfig, &baseSkipped);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100819 bool coupledSkipped = false;
Xusong Wang1f50e542020-01-13 11:44:45 -0800820 EvaluatePreparedModel(device, preparedCoupledModel, coupledModel, testConfig,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100821 &coupledSkipped);
822 ASSERT_EQ(baseSkipped, coupledSkipped);
823 if (baseSkipped) {
824 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
825 "execute model that it does not support.";
826 std::cout << "[ ] Early termination of test because vendor service "
827 "cannot "
828 "execute model that it does not support."
829 << std::endl;
830 GTEST_SKIP();
831 }
832 }
833 }
834 }
835}
836
837void Execute(const sp<IDevice>& device, const TestModel& testModel, TestKind testKind) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100838 Model model = createModel(testModel);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100839 if (testKind == TestKind::DYNAMIC_SHAPE) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100840 makeOutputDimensionsUnspecified(&model);
841 }
842
843 sp<IPreparedModel> preparedModel;
Lev Proleev9226c1e2019-10-03 14:43:18 +0100844 switch (testKind) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800845 case TestKind::GENERAL:
846 case TestKind::DYNAMIC_SHAPE:
Miao Wang33173a32020-01-23 13:53:21 -0800847 case TestKind::MEMORY_DOMAIN:
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000848 case TestKind::FENCED_COMPUTE:
849 case TestKind::INTINITE_LOOP_TIMEOUT: {
Lev Proleev9226c1e2019-10-03 14:43:18 +0100850 createPreparedModel(device, model, &preparedModel);
851 if (preparedModel == nullptr) return;
Xusong Wang1f50e542020-01-13 11:44:45 -0800852 EvaluatePreparedModel(device, preparedModel, testModel, testKind);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100853 } break;
854 case TestKind::QUANTIZATION_COUPLING: {
Lev Proleev6a1d5e42020-01-02 18:22:30 +0000855 ASSERT_TRUE(testModel.hasQuant8CoupledOperands());
Michael Butler616701d2020-01-07 14:52:44 -0800856 createPreparedModel(device, model, &preparedModel,
857 /*reportSkipping*/ false);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100858 TestModel signedQuantizedModel = convertQuant8AsymmOperandsToSigned(testModel);
859 sp<IPreparedModel> preparedCoupledModel;
860 createPreparedModel(device, createModel(signedQuantizedModel), &preparedCoupledModel,
861 /*reportSkipping*/ false);
862 // If we couldn't prepare a model with unsigned quantization, we must
863 // fail to prepare a model with signed quantization as well.
864 if (preparedModel == nullptr) {
865 ASSERT_EQ(preparedCoupledModel, nullptr);
866 // If we failed to prepare both of the models, we can safely skip
867 // the test.
868 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
869 "prepare model that it does not support.";
870 std::cout
871 << "[ ] Early termination of test because vendor service cannot "
872 "prepare model that it does not support."
873 << std::endl;
874 GTEST_SKIP();
875 }
876 ASSERT_NE(preparedCoupledModel, nullptr);
Xusong Wang1f50e542020-01-13 11:44:45 -0800877 EvaluatePreparedCoupledModels(device, preparedModel, testModel, preparedCoupledModel,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100878 signedQuantizedModel);
879 } break;
880 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100881}
882
883void GeneratedTestBase::SetUp() {
884 testing::TestWithParam<GeneratedTestParam>::SetUp();
885 ASSERT_NE(kDevice, nullptr);
886}
887
888std::vector<NamedModel> getNamedModels(const FilterFn& filter) {
889 return TestModelManager::get().getTestModels(filter);
890}
891
892std::string printGeneratedTest(const testing::TestParamInfo<GeneratedTestParam>& info) {
893 const auto& [namedDevice, namedModel] = info.param;
894 return gtestCompliantName(getName(namedDevice) + "_" + getName(namedModel));
895}
896
897// Tag for the generated tests
898class GeneratedTest : public GeneratedTestBase {};
899
900// Tag for the dynamic output shape tests
901class DynamicOutputShapeTest : public GeneratedTest {};
902
Xusong Wang1f50e542020-01-13 11:44:45 -0800903// Tag for the memory domain tests
904class MemoryDomainTest : public GeneratedTest {};
905
Miao Wang33173a32020-01-23 13:53:21 -0800906// Tag for the fenced compute tests
907class FencedComputeTest : public GeneratedTest {};
908
Lev Proleev9226c1e2019-10-03 14:43:18 +0100909// Tag for the dynamic output shape tests
Lev Proleevbaac15d2020-01-09 16:37:28 +0000910class QuantizationCouplingTest : public GeneratedTest {};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100911
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000912// Tag for the loop timeout tests
913class InfiniteLoopTimeoutTest : public GeneratedTest {};
914
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100915TEST_P(GeneratedTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000916 Execute(kDevice, kTestModel, TestKind::GENERAL);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100917}
918
919TEST_P(DynamicOutputShapeTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000920 Execute(kDevice, kTestModel, TestKind::DYNAMIC_SHAPE);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100921}
922
Xusong Wang1f50e542020-01-13 11:44:45 -0800923TEST_P(MemoryDomainTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000924 Execute(kDevice, kTestModel, TestKind::MEMORY_DOMAIN);
Xusong Wang1f50e542020-01-13 11:44:45 -0800925}
926
Miao Wang33173a32020-01-23 13:53:21 -0800927TEST_P(FencedComputeTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000928 Execute(kDevice, kTestModel, TestKind::FENCED_COMPUTE);
Miao Wang33173a32020-01-23 13:53:21 -0800929}
930
Lev Proleevbaac15d2020-01-09 16:37:28 +0000931TEST_P(QuantizationCouplingTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000932 Execute(kDevice, kTestModel, TestKind::QUANTIZATION_COUPLING);
933}
934
935TEST_P(InfiniteLoopTimeoutTest, Test) {
936 Execute(kDevice, kTestModel, TestKind::INTINITE_LOOP_TIMEOUT);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100937}
938
939INSTANTIATE_GENERATED_TEST(GeneratedTest,
940 [](const TestModel& testModel) { return !testModel.expectFailure; });
941
Lev Proleev8b3f2402020-01-20 18:54:46 +0000942INSTANTIATE_GENERATED_TEST(DynamicOutputShapeTest, [](const TestModel& testModel) {
943 return !testModel.expectFailure && !testModel.hasScalarOutputs();
944});
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100945
Xusong Wang1f50e542020-01-13 11:44:45 -0800946INSTANTIATE_GENERATED_TEST(MemoryDomainTest,
947 [](const TestModel& testModel) { return !testModel.expectFailure; });
948
Xusong Wang5882d6d2020-03-13 10:02:36 -0700949INSTANTIATE_GENERATED_TEST(FencedComputeTest,
950 [](const TestModel& testModel) { return !testModel.expectFailure; });
Miao Wang33173a32020-01-23 13:53:21 -0800951
Lev Proleevbaac15d2020-01-09 16:37:28 +0000952INSTANTIATE_GENERATED_TEST(QuantizationCouplingTest, [](const TestModel& testModel) {
Xusong Wang962fdb22020-03-16 17:13:41 -0700953 return !testModel.expectFailure && testModel.hasQuant8CoupledOperands() &&
954 testModel.main.operations.size() == 1;
Lev Proleev9226c1e2019-10-03 14:43:18 +0100955});
956
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000957INSTANTIATE_GENERATED_TEST(InfiniteLoopTimeoutTest, [](const TestModel& testModel) {
958 return testModel.isInfiniteLoopTimeoutTest();
959});
960
Lev Proleev26d1bc82019-08-30 11:57:18 +0100961} // namespace android::hardware::neuralnetworks::V1_3::vts::functional