blob: 4dbac1645a0b1d8092132714097a8b78306c28a4 [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 Wang2b5c4cd2019-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 Wang2b5c4cd2019-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 Butler95899b32020-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 Butler79a41d72019-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 Wange9da9852020-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 Shklyaeva37c2c82020-02-18 16:11:32 +000075enum class OutputType { FULLY_SPECIFIED, UNSPECIFIED, INSUFFICIENT, MISSED_DEADLINE };
Lev Proleev13fdfcd2019-08-30 11:35:34 +010076
Xusong Wange9da9852020-01-13 11:44:45 -080077enum class IOType { INPUT, OUTPUT };
78
Lev Proleev0d4ba3f2019-10-02 17:32:06 +010079struct TestConfig {
80 Executor executor;
81 MeasureTiming measureTiming;
82 OutputType outputType;
Xusong Wange9da9852020-01-13 11:44:45 -080083 MemoryType memoryType;
Lev Proleev9226c1e2019-10-03 14:43:18 +010084 // `reportSkipping` indicates if a test should print an info message in case
85 // it is skipped. The field is set to true by default and is set to false in
86 // quantization coupling tests to suppress skipping a test
87 bool reportSkipping;
Xusong Wange9da9852020-01-13 11:44:45 -080088 TestConfig(Executor executor, MeasureTiming measureTiming, OutputType outputType,
89 MemoryType memoryType)
Lev Proleev9226c1e2019-10-03 14:43:18 +010090 : executor(executor),
91 measureTiming(measureTiming),
92 outputType(outputType),
Xusong Wange9da9852020-01-13 11:44:45 -080093 memoryType(memoryType),
Lev Proleev9226c1e2019-10-03 14:43:18 +010094 reportSkipping(true) {}
95 TestConfig(Executor executor, MeasureTiming measureTiming, OutputType outputType,
Xusong Wange9da9852020-01-13 11:44:45 -080096 MemoryType memoryType, bool reportSkipping)
Lev Proleev9226c1e2019-10-03 14:43:18 +010097 : executor(executor),
98 measureTiming(measureTiming),
99 outputType(outputType),
Xusong Wange9da9852020-01-13 11:44:45 -0800100 memoryType(memoryType),
Lev Proleev9226c1e2019-10-03 14:43:18 +0100101 reportSkipping(reportSkipping) {}
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100102};
103
Xusong Wange9da9852020-01-13 11:44:45 -0800104class DeviceMemoryAllocator {
105 public:
106 DeviceMemoryAllocator(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel,
107 const TestModel& testModel)
108 : kDevice(device), kPreparedModel(preparedModel), kTestModel(testModel) {}
109
110 // Allocate device memory for a target input/output operand.
111 // Return {IBuffer object, token} if successful.
112 // Return {nullptr, 0} if device memory is not supported.
113 template <IOType ioType>
Michael Butlerc3e1a292020-02-04 16:15:04 -0800114 std::pair<sp<IBuffer>, uint32_t> allocate(uint32_t index) {
115 std::pair<sp<IBuffer>, uint32_t> buffer;
Xusong Wange9da9852020-01-13 11:44:45 -0800116 allocateInternal<ioType>(index, &buffer);
117 return buffer;
118 }
119
120 private:
121 template <IOType ioType>
Michael Butlerc3e1a292020-02-04 16:15:04 -0800122 void allocateInternal(uint32_t index, std::pair<sp<IBuffer>, uint32_t>* result) {
Xusong Wange9da9852020-01-13 11:44:45 -0800123 ASSERT_NE(result, nullptr);
124
125 // Prepare arguments.
126 BufferRole role = {.modelIndex = 0, .ioIndex = index, .frequency = 1.0f};
127 hidl_vec<BufferRole> inputRoles, outputRoles;
128 if constexpr (ioType == IOType::INPUT) {
129 inputRoles = {role};
130 } else {
131 outputRoles = {role};
132 }
133
134 // Allocate device memory.
135 ErrorStatus status;
136 sp<IBuffer> buffer;
Michael Butlerc3e1a292020-02-04 16:15:04 -0800137 uint32_t token;
138 auto cb = [&status, &buffer, &token](ErrorStatus error, const sp<IBuffer>& buf,
139 uint32_t tok) {
140 status = error;
141 buffer = buf;
142 token = tok;
143 };
144 const auto ret = kDevice->allocate({}, {kPreparedModel}, inputRoles, outputRoles, cb);
Xusong Wange9da9852020-01-13 11:44:45 -0800145
146 // Check allocation results.
147 ASSERT_TRUE(ret.isOk());
148 if (status == ErrorStatus::NONE) {
149 ASSERT_NE(buffer, nullptr);
150 ASSERT_GT(token, 0);
151 } else {
152 ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE);
153 ASSERT_EQ(buffer, nullptr);
154 ASSERT_EQ(token, 0);
155 }
156
157 // Initialize input data from TestBuffer.
158 if constexpr (ioType == IOType::INPUT) {
159 if (buffer != nullptr) {
160 // TestBuffer -> Shared memory.
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000161 const auto& testBuffer =
162 kTestModel.main.operands[kTestModel.main.inputIndexes[index]].data;
Xusong Wange9da9852020-01-13 11:44:45 -0800163 ASSERT_GT(testBuffer.size(), 0);
164 hidl_memory tmp = nn::allocateSharedMemory(testBuffer.size());
165 sp<IMemory> inputMemory = mapMemory(tmp);
166 ASSERT_NE(inputMemory.get(), nullptr);
167 uint8_t* inputPtr =
168 static_cast<uint8_t*>(static_cast<void*>(inputMemory->getPointer()));
169 ASSERT_NE(inputPtr, nullptr);
170 const uint8_t* begin = testBuffer.get<uint8_t>();
171 const uint8_t* end = begin + testBuffer.size();
172 std::copy(begin, end, inputPtr);
173
174 // Shared memory -> IBuffer.
175 auto ret = buffer->copyFrom(tmp, {});
176 ASSERT_TRUE(ret.isOk());
177 ASSERT_EQ(static_cast<ErrorStatus>(ret), ErrorStatus::NONE);
178 }
179 }
180 *result = {std::move(buffer), token};
181 }
182
183 const sp<IDevice> kDevice;
184 const sp<IPreparedModel> kPreparedModel;
185 const TestModel& kTestModel;
186};
187
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000188Subgraph createSubgraph(const TestSubgraph& testSubgraph, uint32_t* constCopySize,
189 std::vector<const TestBuffer*>* constCopies, uint32_t* constRefSize,
190 std::vector<const TestBuffer*>* constReferences) {
191 CHECK(constCopySize != nullptr);
192 CHECK(constCopies != nullptr);
193 CHECK(constRefSize != nullptr);
194 CHECK(constReferences != nullptr);
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100195
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000196 // Operands.
197 hidl_vec<Operand> operands(testSubgraph.operands.size());
198 for (uint32_t i = 0; i < testSubgraph.operands.size(); i++) {
199 const auto& op = testSubgraph.operands[i];
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100200
201 DataLocation loc = {};
202 if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) {
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000203 loc = {
204 .poolIndex = 0,
205 .offset = *constCopySize,
206 .length = static_cast<uint32_t>(op.data.size()),
207 };
208 constCopies->push_back(&op.data);
209 *constCopySize += op.data.alignedSize();
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100210 } else if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) {
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000211 loc = {
212 .poolIndex = 0,
213 .offset = *constRefSize,
214 .length = static_cast<uint32_t>(op.data.size()),
215 };
216 constReferences->push_back(&op.data);
217 *constRefSize += op.data.alignedSize();
218 } else if (op.lifetime == TestOperandLifeTime::SUBGRAPH) {
219 loc = {
220 .poolIndex = 0,
221 .offset = *op.data.get<uint32_t>(),
222 .length = 0,
223 };
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100224 }
225
Michael Butlerc3e1a292020-02-04 16:15:04 -0800226 V1_2::Operand::ExtraParams extraParams;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100227 if (op.type == TestOperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) {
228 extraParams.channelQuant(SymmPerChannelQuantParams{
229 .scales = op.channelQuant.scales, .channelDim = op.channelQuant.channelDim});
230 }
231
232 operands[i] = {.type = static_cast<OperandType>(op.type),
233 .dimensions = op.dimensions,
234 .numberOfConsumers = op.numberOfConsumers,
235 .scale = op.scale,
236 .zeroPoint = op.zeroPoint,
237 .lifetime = static_cast<OperandLifeTime>(op.lifetime),
238 .location = loc,
239 .extraParams = std::move(extraParams)};
240 }
241
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000242 // Operations.
243 hidl_vec<Operation> operations(testSubgraph.operations.size());
244 std::transform(testSubgraph.operations.begin(), testSubgraph.operations.end(),
245 operations.begin(), [](const TestOperation& op) -> Operation {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100246 return {.type = static_cast<OperationType>(op.type),
247 .inputs = op.inputs,
248 .outputs = op.outputs};
249 });
250
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000251 return {.operands = std::move(operands),
252 .operations = std::move(operations),
253 .inputIndexes = testSubgraph.inputIndexes,
254 .outputIndexes = testSubgraph.outputIndexes};
255}
256
257void copyTestBuffers(const std::vector<const TestBuffer*>& buffers, uint8_t* output) {
258 uint32_t offset = 0;
259 for (const TestBuffer* buffer : buffers) {
260 const uint8_t* begin = buffer->get<uint8_t>();
261 const uint8_t* end = begin + buffer->size();
262 std::copy(begin, end, output + offset);
263 offset += buffer->alignedSize();
264 }
265}
266
267} // namespace
268
Xusong Wang28441652020-03-20 14:49:54 -0700269void waitForSyncFence(int syncFd) {
270 constexpr int kInfiniteTimeout = -1;
271 ASSERT_GT(syncFd, 0);
272 int r = sync_wait(syncFd, kInfiniteTimeout);
273 ASSERT_GE(r, 0);
274}
275
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000276Model createModel(const TestModel& testModel) {
277 uint32_t constCopySize = 0;
278 uint32_t constRefSize = 0;
279 std::vector<const TestBuffer*> constCopies;
280 std::vector<const TestBuffer*> constReferences;
281
282 Subgraph mainSubgraph = createSubgraph(testModel.main, &constCopySize, &constCopies,
283 &constRefSize, &constReferences);
284 hidl_vec<Subgraph> refSubgraphs(testModel.referenced.size());
285 std::transform(testModel.referenced.begin(), testModel.referenced.end(), refSubgraphs.begin(),
286 [&constCopySize, &constCopies, &constRefSize,
287 &constReferences](const TestSubgraph& testSubgraph) {
288 return createSubgraph(testSubgraph, &constCopySize, &constCopies,
289 &constRefSize, &constReferences);
290 });
291
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100292 // Constant copies.
293 hidl_vec<uint8_t> operandValues(constCopySize);
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000294 copyTestBuffers(constCopies, operandValues.data());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100295
296 // Shared memory.
297 hidl_vec<hidl_memory> pools = {};
298 if (constRefSize > 0) {
299 hidl_vec_push_back(&pools, nn::allocateSharedMemory(constRefSize));
300 CHECK_NE(pools[0].size(), 0u);
301
302 // load data
303 sp<IMemory> mappedMemory = mapMemory(pools[0]);
304 CHECK(mappedMemory.get() != nullptr);
305 uint8_t* mappedPtr =
306 reinterpret_cast<uint8_t*>(static_cast<void*>(mappedMemory->getPointer()));
307 CHECK(mappedPtr != nullptr);
308
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000309 copyTestBuffers(constReferences, mappedPtr);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100310 }
311
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000312 return {.main = std::move(mainSubgraph),
313 .referenced = std::move(refSubgraphs),
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100314 .operandValues = std::move(operandValues),
315 .pools = std::move(pools),
316 .relaxComputationFloat32toFloat16 = testModel.isRelaxed};
317}
318
319static bool isOutputSizeGreaterThanOne(const TestModel& testModel, uint32_t index) {
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000320 const auto byteSize = testModel.main.operands[testModel.main.outputIndexes[index]].data.size();
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100321 return byteSize > 1u;
322}
323
Xusong Wange9da9852020-01-13 11:44:45 -0800324static void makeOutputInsufficientSize(uint32_t outputIndex, Request* request) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100325 auto& length = request->outputs[outputIndex].location.length;
326 ASSERT_GT(length, 1u);
327 length -= 1u;
328}
329
330static void makeOutputDimensionsUnspecified(Model* model) {
Slava Shklyaevf8124a82019-12-13 12:24:35 +0000331 for (auto i : model->main.outputIndexes) {
332 auto& dims = model->main.operands[i].dimensions;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100333 std::fill(dims.begin(), dims.end(), 0);
334 }
335}
336
Xusong Wang75e63ad2020-02-25 11:43:10 -0800337class ExecutionContextV1_3 {
338 public:
339 ExecutionContextV1_3(sp<IDevice> device, sp<IPreparedModel> preparedModel)
340 : kDevice(std::move(device)), kPreparedModel(std::move(preparedModel)) {}
Xusong Wange9da9852020-01-13 11:44:45 -0800341
Xusong Wang75e63ad2020-02-25 11:43:10 -0800342 std::optional<Request> createRequest(const TestModel& testModel, MemoryType memoryType);
343 std::vector<TestBuffer> getOutputBuffers(const TestModel& testModel,
344 const Request& request) const;
345
346 private:
347 // Get a TestBuffer with data copied from an IBuffer object.
348 void getBuffer(const sp<IBuffer>& buffer, size_t size, TestBuffer* testBuffer) const;
349
350 static constexpr uint32_t kInputPoolIndex = 0;
351 static constexpr uint32_t kOutputPoolIndex = 1;
352 static constexpr uint32_t kDeviceMemoryBeginIndex = 2;
353
354 const sp<IDevice> kDevice;
355 const sp<IPreparedModel> kPreparedModel;
356 std::unique_ptr<TestMemoryBase> mInputMemory, mOutputMemory;
357 std::vector<sp<IBuffer>> mBuffers;
358};
359
360std::optional<Request> ExecutionContextV1_3::createRequest(const TestModel& testModel,
361 MemoryType memoryType) {
Xusong Wange9da9852020-01-13 11:44:45 -0800362 // Memory pools are organized as:
363 // - 0: Input shared memory pool
364 // - 1: Output shared memory pool
365 // - [2, 2+i): Input device memories
366 // - [2+i, 2+i+o): Output device memories
Xusong Wang75e63ad2020-02-25 11:43:10 -0800367 DeviceMemoryAllocator allocator(kDevice, kPreparedModel, testModel);
Michael Butlerc3e1a292020-02-04 16:15:04 -0800368 std::vector<uint32_t> tokens;
Xusong Wang75e63ad2020-02-25 11:43:10 -0800369 mBuffers.clear();
Xusong Wange9da9852020-01-13 11:44:45 -0800370
371 // Model inputs.
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000372 hidl_vec<RequestArgument> inputs(testModel.main.inputIndexes.size());
Xusong Wange9da9852020-01-13 11:44:45 -0800373 size_t inputSize = 0;
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000374 for (uint32_t i = 0; i < testModel.main.inputIndexes.size(); i++) {
375 const auto& op = testModel.main.operands[testModel.main.inputIndexes[i]];
Xusong Wange9da9852020-01-13 11:44:45 -0800376 if (op.data.size() == 0) {
377 // Omitted input.
378 inputs[i] = {.hasNoValue = true};
379 continue;
Xusong Wang75e63ad2020-02-25 11:43:10 -0800380 } else if (memoryType == MemoryType::DEVICE) {
Xusong Wange9da9852020-01-13 11:44:45 -0800381 SCOPED_TRACE("Input index = " + std::to_string(i));
382 auto [buffer, token] = allocator.allocate<IOType::INPUT>(i);
383 if (buffer != nullptr) {
Xusong Wang75e63ad2020-02-25 11:43:10 -0800384 DataLocation loc = {.poolIndex = static_cast<uint32_t>(mBuffers.size() +
Xusong Wange9da9852020-01-13 11:44:45 -0800385 kDeviceMemoryBeginIndex)};
Xusong Wang75e63ad2020-02-25 11:43:10 -0800386 mBuffers.push_back(std::move(buffer));
Xusong Wange9da9852020-01-13 11:44:45 -0800387 tokens.push_back(token);
388 inputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}};
389 continue;
390 }
391 }
392
393 // Reserve shared memory for input.
394 DataLocation loc = {.poolIndex = kInputPoolIndex,
395 .offset = static_cast<uint32_t>(inputSize),
396 .length = static_cast<uint32_t>(op.data.size())};
397 inputSize += op.data.alignedSize();
398 inputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}};
399 }
400
401 // Model outputs.
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000402 hidl_vec<RequestArgument> outputs(testModel.main.outputIndexes.size());
Xusong Wange9da9852020-01-13 11:44:45 -0800403 size_t outputSize = 0;
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000404 for (uint32_t i = 0; i < testModel.main.outputIndexes.size(); i++) {
405 const auto& op = testModel.main.operands[testModel.main.outputIndexes[i]];
Xusong Wang75e63ad2020-02-25 11:43:10 -0800406 if (memoryType == MemoryType::DEVICE) {
Xusong Wange9da9852020-01-13 11:44:45 -0800407 SCOPED_TRACE("Output index = " + std::to_string(i));
408 auto [buffer, token] = allocator.allocate<IOType::OUTPUT>(i);
409 if (buffer != nullptr) {
Xusong Wang75e63ad2020-02-25 11:43:10 -0800410 DataLocation loc = {.poolIndex = static_cast<uint32_t>(mBuffers.size() +
Xusong Wange9da9852020-01-13 11:44:45 -0800411 kDeviceMemoryBeginIndex)};
Xusong Wang75e63ad2020-02-25 11:43:10 -0800412 mBuffers.push_back(std::move(buffer));
Xusong Wange9da9852020-01-13 11:44:45 -0800413 tokens.push_back(token);
414 outputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}};
415 continue;
416 }
417 }
418
419 // In the case of zero-sized output, we should at least provide a one-byte buffer.
420 // This is because zero-sized tensors are only supported internally to the driver, or
421 // reported in output shapes. It is illegal for the client to pre-specify a zero-sized
422 // tensor as model output. Otherwise, we will have two semantic conflicts:
423 // - "Zero dimension" conflicts with "unspecified dimension".
424 // - "Omitted operand buffer" conflicts with "zero-sized operand buffer".
425 size_t bufferSize = std::max<size_t>(op.data.size(), 1);
426
427 // Reserve shared memory for output.
428 DataLocation loc = {.poolIndex = kOutputPoolIndex,
429 .offset = static_cast<uint32_t>(outputSize),
430 .length = static_cast<uint32_t>(bufferSize)};
431 outputSize += op.data.size() == 0 ? TestBuffer::kAlignment : op.data.alignedSize();
432 outputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}};
433 }
434
Xusong Wang75e63ad2020-02-25 11:43:10 -0800435 if (memoryType == MemoryType::DEVICE && mBuffers.empty()) {
436 return std::nullopt;
437 }
438
Xusong Wange9da9852020-01-13 11:44:45 -0800439 // Memory pools.
Xusong Wang75e63ad2020-02-25 11:43:10 -0800440 hidl_vec<Request::MemoryPool> pools(kDeviceMemoryBeginIndex + mBuffers.size());
441 if (memoryType == MemoryType::BLOB_AHWB) {
442 mInputMemory = TestBlobAHWB::create(std::max<size_t>(inputSize, 1));
443 mOutputMemory = TestBlobAHWB::create(std::max<size_t>(outputSize, 1));
444 } else {
445 mInputMemory = TestAshmem::create(std::max<size_t>(inputSize, 1));
446 mOutputMemory = TestAshmem::create(std::max<size_t>(outputSize, 1));
447 }
448 EXPECT_NE(mInputMemory, nullptr);
449 EXPECT_NE(mOutputMemory, nullptr);
450 pools[kInputPoolIndex].hidlMemory(mInputMemory->getHidlMemory());
451 pools[kOutputPoolIndex].hidlMemory(mOutputMemory->getHidlMemory());
452 for (uint32_t i = 0; i < mBuffers.size(); i++) {
Xusong Wange9da9852020-01-13 11:44:45 -0800453 pools[kDeviceMemoryBeginIndex + i].token(tokens[i]);
454 }
455
456 // Copy input data to the input shared memory pool.
Xusong Wang75e63ad2020-02-25 11:43:10 -0800457 uint8_t* inputPtr = mInputMemory->getPointer();
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000458 for (uint32_t i = 0; i < testModel.main.inputIndexes.size(); i++) {
Xusong Wange9da9852020-01-13 11:44:45 -0800459 if (!inputs[i].hasNoValue && inputs[i].location.poolIndex == kInputPoolIndex) {
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000460 const auto& op = testModel.main.operands[testModel.main.inputIndexes[i]];
Xusong Wange9da9852020-01-13 11:44:45 -0800461 const uint8_t* begin = op.data.get<uint8_t>();
462 const uint8_t* end = begin + op.data.size();
463 std::copy(begin, end, inputPtr + inputs[i].location.offset);
464 }
465 }
Xusong Wang75e63ad2020-02-25 11:43:10 -0800466 return Request{
Xusong Wange9da9852020-01-13 11:44:45 -0800467 .inputs = std::move(inputs), .outputs = std::move(outputs), .pools = std::move(pools)};
Xusong Wang75e63ad2020-02-25 11:43:10 -0800468}
469
470std::vector<TestBuffer> ExecutionContextV1_3::getOutputBuffers(const TestModel& testModel,
471 const Request& request) const {
472 // Copy out output results.
473 uint8_t* outputPtr = mOutputMemory->getPointer();
474 std::vector<TestBuffer> outputBuffers;
475 for (uint32_t i = 0; i < request.outputs.size(); i++) {
476 const auto& outputLoc = request.outputs[i].location;
477 if (outputLoc.poolIndex == kOutputPoolIndex) {
478 outputBuffers.emplace_back(outputLoc.length, outputPtr + outputLoc.offset);
479 } else {
480 const auto& op = testModel.main.operands[testModel.main.outputIndexes[i]];
481 if (op.data.size() == 0) {
482 outputBuffers.emplace_back(0, nullptr);
483 } else {
484 SCOPED_TRACE("Output index = " + std::to_string(i));
485 const uint32_t bufferIndex = outputLoc.poolIndex - kDeviceMemoryBeginIndex;
486 TestBuffer buffer;
487 getBuffer(mBuffers[bufferIndex], op.data.size(), &buffer);
488 outputBuffers.push_back(std::move(buffer));
489 }
490 }
491 }
492 return outputBuffers;
Xusong Wange9da9852020-01-13 11:44:45 -0800493}
494
495// Get a TestBuffer with data copied from an IBuffer object.
Xusong Wang75e63ad2020-02-25 11:43:10 -0800496void ExecutionContextV1_3::getBuffer(const sp<IBuffer>& buffer, size_t size,
497 TestBuffer* testBuffer) const {
Xusong Wange9da9852020-01-13 11:44:45 -0800498 // IBuffer -> Shared memory.
499 hidl_memory tmp = nn::allocateSharedMemory(size);
500 const auto ret = buffer->copyTo(tmp);
501 ASSERT_TRUE(ret.isOk());
502 ASSERT_EQ(static_cast<ErrorStatus>(ret), ErrorStatus::NONE);
503
504 // Shared memory -> TestBuffer.
505 sp<IMemory> outputMemory = mapMemory(tmp);
506 ASSERT_NE(outputMemory.get(), nullptr);
507 uint8_t* outputPtr = static_cast<uint8_t*>(static_cast<void*>(outputMemory->getPointer()));
508 ASSERT_NE(outputPtr, nullptr);
509 ASSERT_NE(testBuffer, nullptr);
510 *testBuffer = TestBuffer(size, outputPtr);
511}
512
Xusong Wangf07a9572020-03-13 10:02:36 -0700513static bool hasZeroSizedOutput(const TestModel& testModel) {
514 return std::any_of(testModel.main.outputIndexes.begin(), testModel.main.outputIndexes.end(),
515 [&testModel](uint32_t index) {
516 return testModel.main.operands[index].data.size() == 0;
517 });
518}
519
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100520static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel,
521 const Request& request, MeasureTiming measure,
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000522 const OptionalTimeoutDuration& loopTimeoutDuration,
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100523 sp<ExecutionCallback>& callback) {
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000524 return preparedModel->execute_1_3(request, measure, {}, loopTimeoutDuration, callback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100525}
526static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel,
527 const Request& request, MeasureTiming measure,
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000528 const OptionalTimeoutDuration& loopTimeoutDuration,
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100529 hidl_vec<OutputShape>* outputShapes,
530 Timing* timing) {
531 ErrorStatus result;
Xusong Wangd4a060b2019-10-28 11:11:19 -0700532 Return<void> ret = preparedModel->executeSynchronously_1_3(
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000533 request, measure, {}, loopTimeoutDuration,
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100534 [&result, outputShapes, timing](ErrorStatus error, const hidl_vec<OutputShape>& shapes,
535 const Timing& time) {
536 result = error;
537 *outputShapes = shapes;
538 *timing = time;
539 });
540 if (!ret.isOk()) {
541 return ErrorStatus::GENERAL_FAILURE;
542 }
543 return result;
544}
545static std::shared_ptr<::android::nn::ExecutionBurstController> CreateBurst(
546 const sp<IPreparedModel>& preparedModel) {
Michael Butler648ada52019-07-25 17:22:11 -0700547 return android::nn::ExecutionBurstController::create(preparedModel,
548 std::chrono::microseconds{0});
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100549}
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100550
Xusong Wange9da9852020-01-13 11:44:45 -0800551void EvaluatePreparedModel(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel,
552 const TestModel& testModel, const TestConfig& testConfig,
553 bool* skipped = nullptr) {
Lev Proleev9226c1e2019-10-03 14:43:18 +0100554 if (skipped != nullptr) {
555 *skipped = false;
556 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100557 // If output0 does not have size larger than one byte, we can not test with insufficient buffer.
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100558 if (testConfig.outputType == OutputType::INSUFFICIENT &&
559 !isOutputSizeGreaterThanOne(testModel, 0)) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100560 return;
561 }
562
Xusong Wang75e63ad2020-02-25 11:43:10 -0800563 ExecutionContextV1_3 context(device, preparedModel);
564 auto maybeRequest = context.createRequest(testModel, testConfig.memoryType);
Xusong Wange9da9852020-01-13 11:44:45 -0800565 // Skip if testing memory domain but no device memory has been allocated.
Xusong Wang75e63ad2020-02-25 11:43:10 -0800566 if (!maybeRequest.has_value()) {
Xusong Wange9da9852020-01-13 11:44:45 -0800567 return;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100568 }
Xusong Wang75e63ad2020-02-25 11:43:10 -0800569
570 Request request = std::move(maybeRequest.value());
Xusong Wange9da9852020-01-13 11:44:45 -0800571 if (testConfig.outputType == OutputType::INSUFFICIENT) {
572 makeOutputInsufficientSize(/*outputIndex=*/0, &request);
573 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100574
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000575 OptionalTimeoutDuration loopTimeoutDuration;
576 // OutputType::MISSED_DEADLINE is only used by
577 // TestKind::INTINITE_LOOP_TIMEOUT tests to verify that an infinite loop is
578 // aborted after a timeout.
579 if (testConfig.outputType == OutputType::MISSED_DEADLINE) {
580 // Override the default loop timeout duration with a small value to
581 // speed up test execution.
582 constexpr uint64_t kMillisecond = 1'000'000;
583 loopTimeoutDuration.nanoseconds(1 * kMillisecond);
584 }
585
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100586 ErrorStatus executionStatus;
587 hidl_vec<OutputShape> outputShapes;
588 Timing timing;
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100589 switch (testConfig.executor) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100590 case Executor::ASYNC: {
591 SCOPED_TRACE("asynchronous");
592
593 // launch execution
594 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000595 Return<ErrorStatus> executionLaunchStatus =
596 ExecutePreparedModel(preparedModel, request, testConfig.measureTiming,
597 loopTimeoutDuration, executionCallback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100598 ASSERT_TRUE(executionLaunchStatus.isOk());
599 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
600
601 // retrieve execution status
602 executionCallback->wait();
603 executionStatus = executionCallback->getStatus();
604 outputShapes = executionCallback->getOutputShapes();
605 timing = executionCallback->getTiming();
606
607 break;
608 }
609 case Executor::SYNC: {
610 SCOPED_TRACE("synchronous");
611
612 // execute
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000613 Return<ErrorStatus> executionReturnStatus =
614 ExecutePreparedModel(preparedModel, request, testConfig.measureTiming,
615 loopTimeoutDuration, &outputShapes, &timing);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100616 ASSERT_TRUE(executionReturnStatus.isOk());
617 executionStatus = static_cast<ErrorStatus>(executionReturnStatus);
618
619 break;
620 }
621 case Executor::BURST: {
Xusong Wangb345a462019-11-27 12:46:48 -0800622 // TODO(butlermichael): Check if we need to test burst in V1_3 if the interface remains
623 // V1_2.
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100624 SCOPED_TRACE("burst");
625
Xusong Wange9da9852020-01-13 11:44:45 -0800626 // check compliance
627 ASSERT_TRUE(nn::compliantWithV1_0(request));
628 V1_0::Request request10 = nn::convertToV1_0(request);
629
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100630 // create burst
631 const std::shared_ptr<::android::nn::ExecutionBurstController> controller =
632 CreateBurst(preparedModel);
633 ASSERT_NE(nullptr, controller.get());
634
635 // create memory keys
Xusong Wangb345a462019-11-27 12:46:48 -0800636 std::vector<intptr_t> keys(request10.pools.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100637 for (size_t i = 0; i < keys.size(); ++i) {
Xusong Wangb345a462019-11-27 12:46:48 -0800638 keys[i] = reinterpret_cast<intptr_t>(&request10.pools[i]);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100639 }
640
641 // execute burst
Michael Butler648ada52019-07-25 17:22:11 -0700642 int n;
643 std::tie(n, outputShapes, timing, std::ignore) =
Xusong Wangb345a462019-11-27 12:46:48 -0800644 controller->compute(request10, testConfig.measureTiming, keys);
Michael Butler648ada52019-07-25 17:22:11 -0700645 executionStatus = nn::convertResultCodeToErrorStatus(n);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100646
647 break;
648 }
Miao Wang2b5c4cd2019-12-26 18:03:56 -0800649 case Executor::FENCED: {
650 SCOPED_TRACE("fenced");
651 ErrorStatus result;
Miao Wang99753d92020-01-21 13:15:09 -0800652 hidl_handle syncFenceHandle;
653 sp<IFencedExecutionCallback> fencedCallback;
Miao Wang2d74d7d2020-02-25 15:54:08 -0800654 auto callbackFunc = [&result, &syncFenceHandle, &fencedCallback](
655 ErrorStatus error, const hidl_handle& handle,
656 const sp<IFencedExecutionCallback>& callback) {
657 result = error;
658 syncFenceHandle = handle;
659 fencedCallback = callback;
660 };
661 Return<void> ret =
662 preparedModel->executeFenced(request, {}, testConfig.measureTiming, {},
663 loopTimeoutDuration, {}, callbackFunc);
Miao Wang2b5c4cd2019-12-26 18:03:56 -0800664 ASSERT_TRUE(ret.isOk());
665 if (result != ErrorStatus::NONE) {
Miao Wang99753d92020-01-21 13:15:09 -0800666 ASSERT_EQ(syncFenceHandle.getNativeHandle(), nullptr);
667 ASSERT_EQ(fencedCallback, nullptr);
Miao Wang40689072020-02-25 16:57:53 -0800668 executionStatus = result;
Miao Wang702a91a2020-03-20 15:09:29 -0700669 timing = {UINT64_MAX, UINT64_MAX};
Miao Wang99753d92020-01-21 13:15:09 -0800670 } else if (syncFenceHandle.getNativeHandle()) {
Miao Wang2d74d7d2020-02-25 15:54:08 -0800671 // If a sync fence is returned, try start another run waiting for the sync fence.
672 ret = preparedModel->executeFenced(request, {syncFenceHandle},
673 testConfig.measureTiming, {},
674 loopTimeoutDuration, {}, callbackFunc);
675 ASSERT_TRUE(ret.isOk());
676 ASSERT_EQ(result, ErrorStatus::NONE);
Miao Wang99753d92020-01-21 13:15:09 -0800677 waitForSyncFence(syncFenceHandle.getNativeHandle()->data[0]);
Miao Wang2b5c4cd2019-12-26 18:03:56 -0800678 }
679 if (result == ErrorStatus::NONE) {
Miao Wang99753d92020-01-21 13:15:09 -0800680 ASSERT_NE(fencedCallback, nullptr);
681 Return<void> ret = fencedCallback->getExecutionInfo(
682 [&executionStatus, &timing](ErrorStatus error, Timing t, Timing) {
Miao Wang2b5c4cd2019-12-26 18:03:56 -0800683 executionStatus = error;
684 timing = t;
685 });
686 ASSERT_TRUE(ret.isOk());
687 }
688 break;
689 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100690 }
691
Miao Wang40689072020-02-25 16:57:53 -0800692 if (testConfig.outputType != OutputType::FULLY_SPECIFIED &&
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100693 executionStatus == ErrorStatus::GENERAL_FAILURE) {
Lev Proleev9226c1e2019-10-03 14:43:18 +0100694 if (skipped != nullptr) {
695 *skipped = true;
696 }
697 if (!testConfig.reportSkipping) {
698 return;
699 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100700 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
701 "execute model that it does not support.";
702 std::cout << "[ ] Early termination of test because vendor service cannot "
703 "execute model that it does not support."
704 << std::endl;
705 GTEST_SKIP();
706 }
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100707 if (testConfig.measureTiming == MeasureTiming::NO) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100708 EXPECT_EQ(UINT64_MAX, timing.timeOnDevice);
709 EXPECT_EQ(UINT64_MAX, timing.timeInDriver);
710 } else {
711 if (timing.timeOnDevice != UINT64_MAX && timing.timeInDriver != UINT64_MAX) {
712 EXPECT_LE(timing.timeOnDevice, timing.timeInDriver);
713 }
714 }
715
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100716 switch (testConfig.outputType) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100717 case OutputType::FULLY_SPECIFIED:
Xusong Wangf07a9572020-03-13 10:02:36 -0700718 if (testConfig.executor == Executor::FENCED && hasZeroSizedOutput(testModel)) {
719 // Executor::FENCED does not support zero-sized output.
720 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
721 return;
722 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100723 // If the model output operands are fully specified, outputShapes must be either
724 // either empty, or have the same number of elements as the number of outputs.
725 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
726 ASSERT_TRUE(outputShapes.size() == 0 ||
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000727 outputShapes.size() == testModel.main.outputIndexes.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100728 break;
729 case OutputType::UNSPECIFIED:
Miao Wang40689072020-02-25 16:57:53 -0800730 if (testConfig.executor == Executor::FENCED) {
731 // For Executor::FENCED, the output shape must be fully specified.
732 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
733 return;
734 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100735 // If the model output operands are not fully specified, outputShapes must have
736 // the same number of elements as the number of outputs.
737 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000738 ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100739 break;
740 case OutputType::INSUFFICIENT:
Miao Wang40689072020-02-25 16:57:53 -0800741 if (testConfig.executor == Executor::FENCED) {
742 // For Executor::FENCED, the output shape must be fully specified.
743 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
744 return;
745 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100746 ASSERT_EQ(ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, executionStatus);
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000747 ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100748 ASSERT_FALSE(outputShapes[0].isSufficient);
749 return;
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000750 case OutputType::MISSED_DEADLINE:
751 ASSERT_TRUE(executionStatus == ErrorStatus::MISSED_DEADLINE_TRANSIENT ||
752 executionStatus == ErrorStatus::MISSED_DEADLINE_PERSISTENT)
753 << "executionStatus = " << executionStatus;
754 return;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100755 }
756
757 // Go through all outputs, check returned output shapes.
758 for (uint32_t i = 0; i < outputShapes.size(); i++) {
759 EXPECT_TRUE(outputShapes[i].isSufficient);
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000760 const auto& expect = testModel.main.operands[testModel.main.outputIndexes[i]].dimensions;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100761 const std::vector<uint32_t> actual = outputShapes[i].dimensions;
762 EXPECT_EQ(expect, actual);
763 }
764
765 // Retrieve execution results.
Xusong Wang75e63ad2020-02-25 11:43:10 -0800766 const std::vector<TestBuffer> outputs = context.getOutputBuffers(testModel, request);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100767
768 // We want "close-enough" results.
769 checkResults(testModel, outputs);
770}
771
Xusong Wange9da9852020-01-13 11:44:45 -0800772void EvaluatePreparedModel(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel,
773 const TestModel& testModel, TestKind testKind) {
Lev Proleev56cda832019-12-05 14:49:47 +0000774 std::vector<OutputType> outputTypesList;
775 std::vector<MeasureTiming> measureTimingList;
776 std::vector<Executor> executorList;
Xusong Wang75e63ad2020-02-25 11:43:10 -0800777 std::vector<MemoryType> memoryTypeList;
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100778
Lev Proleev9226c1e2019-10-03 14:43:18 +0100779 switch (testKind) {
780 case TestKind::GENERAL: {
781 outputTypesList = {OutputType::FULLY_SPECIFIED};
782 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wange51e4a92020-01-23 13:53:21 -0800783 executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST};
Xusong Wang75e63ad2020-02-25 11:43:10 -0800784 memoryTypeList = {MemoryType::ASHMEM};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100785 } break;
786 case TestKind::DYNAMIC_SHAPE: {
787 outputTypesList = {OutputType::UNSPECIFIED, OutputType::INSUFFICIENT};
788 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wang40689072020-02-25 16:57:53 -0800789 executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST, Executor::FENCED};
Xusong Wang75e63ad2020-02-25 11:43:10 -0800790 memoryTypeList = {MemoryType::ASHMEM};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100791 } break;
Xusong Wange9da9852020-01-13 11:44:45 -0800792 case TestKind::MEMORY_DOMAIN: {
793 outputTypesList = {OutputType::FULLY_SPECIFIED};
794 measureTimingList = {MeasureTiming::NO};
Miao Wang2d74d7d2020-02-25 15:54:08 -0800795 executorList = {Executor::ASYNC, Executor::SYNC, Executor::FENCED};
Xusong Wang75e63ad2020-02-25 11:43:10 -0800796 memoryTypeList = {MemoryType::BLOB_AHWB, MemoryType::DEVICE};
Xusong Wange9da9852020-01-13 11:44:45 -0800797 } break;
Miao Wange51e4a92020-01-23 13:53:21 -0800798 case TestKind::FENCED_COMPUTE: {
799 outputTypesList = {OutputType::FULLY_SPECIFIED};
800 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
801 executorList = {Executor::FENCED};
Xusong Wang75e63ad2020-02-25 11:43:10 -0800802 memoryTypeList = {MemoryType::ASHMEM};
Miao Wange51e4a92020-01-23 13:53:21 -0800803 } break;
Lev Proleev9226c1e2019-10-03 14:43:18 +0100804 case TestKind::QUANTIZATION_COUPLING: {
805 LOG(FATAL) << "Wrong TestKind for EvaluatePreparedModel";
806 return;
807 } break;
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000808 case TestKind::INTINITE_LOOP_TIMEOUT: {
809 outputTypesList = {OutputType::MISSED_DEADLINE};
810 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
811 // Burst does not support V1_3 loop timeout.
812 executorList = {Executor::ASYNC, Executor::SYNC, Executor::FENCED};
Xusong Wang75e63ad2020-02-25 11:43:10 -0800813 memoryTypeList = {MemoryType::ASHMEM};
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000814 } break;
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100815 }
816
817 for (const OutputType outputType : outputTypesList) {
818 for (const MeasureTiming measureTiming : measureTimingList) {
819 for (const Executor executor : executorList) {
Xusong Wang75e63ad2020-02-25 11:43:10 -0800820 for (const MemoryType memoryType : memoryTypeList) {
821 const TestConfig testConfig(executor, measureTiming, outputType, memoryType);
822 EvaluatePreparedModel(device, preparedModel, testModel, testConfig);
823 }
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100824 }
825 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100826 }
827}
828
Xusong Wange9da9852020-01-13 11:44:45 -0800829void EvaluatePreparedCoupledModels(const sp<IDevice>& device,
830 const sp<IPreparedModel>& preparedModel,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100831 const TestModel& testModel,
832 const sp<IPreparedModel>& preparedCoupledModel,
833 const TestModel& coupledModel) {
Lev Proleev56cda832019-12-05 14:49:47 +0000834 const std::vector<OutputType> outputTypesList = {OutputType::FULLY_SPECIFIED};
835 const std::vector<MeasureTiming> measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wang2b5c4cd2019-12-26 18:03:56 -0800836 const std::vector<Executor> executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST,
837 Executor::FENCED};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100838
839 for (const OutputType outputType : outputTypesList) {
840 for (const MeasureTiming measureTiming : measureTimingList) {
841 for (const Executor executor : executorList) {
Xusong Wang75e63ad2020-02-25 11:43:10 -0800842 const TestConfig testConfig(executor, measureTiming, outputType, MemoryType::ASHMEM,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100843 /*reportSkipping=*/false);
844 bool baseSkipped = false;
Xusong Wange9da9852020-01-13 11:44:45 -0800845 EvaluatePreparedModel(device, preparedModel, testModel, testConfig, &baseSkipped);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100846 bool coupledSkipped = false;
Xusong Wange9da9852020-01-13 11:44:45 -0800847 EvaluatePreparedModel(device, preparedCoupledModel, coupledModel, testConfig,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100848 &coupledSkipped);
849 ASSERT_EQ(baseSkipped, coupledSkipped);
850 if (baseSkipped) {
851 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
852 "execute model that it does not support.";
853 std::cout << "[ ] Early termination of test because vendor service "
854 "cannot "
855 "execute model that it does not support."
856 << std::endl;
857 GTEST_SKIP();
858 }
859 }
860 }
861 }
862}
863
864void Execute(const sp<IDevice>& device, const TestModel& testModel, TestKind testKind) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100865 Model model = createModel(testModel);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100866 if (testKind == TestKind::DYNAMIC_SHAPE) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100867 makeOutputDimensionsUnspecified(&model);
868 }
869
870 sp<IPreparedModel> preparedModel;
Lev Proleev9226c1e2019-10-03 14:43:18 +0100871 switch (testKind) {
Xusong Wange9da9852020-01-13 11:44:45 -0800872 case TestKind::GENERAL:
873 case TestKind::DYNAMIC_SHAPE:
Miao Wange51e4a92020-01-23 13:53:21 -0800874 case TestKind::MEMORY_DOMAIN:
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000875 case TestKind::FENCED_COMPUTE:
876 case TestKind::INTINITE_LOOP_TIMEOUT: {
Lev Proleev9226c1e2019-10-03 14:43:18 +0100877 createPreparedModel(device, model, &preparedModel);
878 if (preparedModel == nullptr) return;
Xusong Wange9da9852020-01-13 11:44:45 -0800879 EvaluatePreparedModel(device, preparedModel, testModel, testKind);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100880 } break;
881 case TestKind::QUANTIZATION_COUPLING: {
Lev Proleev673fdcf2020-01-02 18:22:30 +0000882 ASSERT_TRUE(testModel.hasQuant8CoupledOperands());
Michael Butler95899b32020-01-07 14:52:44 -0800883 createPreparedModel(device, model, &preparedModel,
884 /*reportSkipping*/ false);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100885 TestModel signedQuantizedModel = convertQuant8AsymmOperandsToSigned(testModel);
886 sp<IPreparedModel> preparedCoupledModel;
887 createPreparedModel(device, createModel(signedQuantizedModel), &preparedCoupledModel,
888 /*reportSkipping*/ false);
889 // If we couldn't prepare a model with unsigned quantization, we must
890 // fail to prepare a model with signed quantization as well.
891 if (preparedModel == nullptr) {
892 ASSERT_EQ(preparedCoupledModel, nullptr);
893 // If we failed to prepare both of the models, we can safely skip
894 // the test.
895 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
896 "prepare model that it does not support.";
897 std::cout
898 << "[ ] Early termination of test because vendor service cannot "
899 "prepare model that it does not support."
900 << std::endl;
901 GTEST_SKIP();
902 }
903 ASSERT_NE(preparedCoupledModel, nullptr);
Xusong Wange9da9852020-01-13 11:44:45 -0800904 EvaluatePreparedCoupledModels(device, preparedModel, testModel, preparedCoupledModel,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100905 signedQuantizedModel);
906 } break;
907 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100908}
909
910void GeneratedTestBase::SetUp() {
911 testing::TestWithParam<GeneratedTestParam>::SetUp();
912 ASSERT_NE(kDevice, nullptr);
913}
914
915std::vector<NamedModel> getNamedModels(const FilterFn& filter) {
916 return TestModelManager::get().getTestModels(filter);
917}
918
Michael Butlerb1865b62020-03-19 17:10:34 -0700919std::vector<NamedModel> getNamedModels(const FilterNameFn& filter) {
920 return TestModelManager::get().getTestModels(filter);
921}
922
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100923std::string printGeneratedTest(const testing::TestParamInfo<GeneratedTestParam>& info) {
924 const auto& [namedDevice, namedModel] = info.param;
925 return gtestCompliantName(getName(namedDevice) + "_" + getName(namedModel));
926}
927
928// Tag for the generated tests
929class GeneratedTest : public GeneratedTestBase {};
930
931// Tag for the dynamic output shape tests
932class DynamicOutputShapeTest : public GeneratedTest {};
933
Xusong Wange9da9852020-01-13 11:44:45 -0800934// Tag for the memory domain tests
935class MemoryDomainTest : public GeneratedTest {};
936
Miao Wange51e4a92020-01-23 13:53:21 -0800937// Tag for the fenced compute tests
938class FencedComputeTest : public GeneratedTest {};
939
Lev Proleev9226c1e2019-10-03 14:43:18 +0100940// Tag for the dynamic output shape tests
Lev Proleev3c68b342020-01-09 16:37:28 +0000941class QuantizationCouplingTest : public GeneratedTest {};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100942
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000943// Tag for the loop timeout tests
944class InfiniteLoopTimeoutTest : public GeneratedTest {};
945
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100946TEST_P(GeneratedTest, Test) {
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000947 Execute(kDevice, kTestModel, TestKind::GENERAL);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100948}
949
950TEST_P(DynamicOutputShapeTest, Test) {
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000951 Execute(kDevice, kTestModel, TestKind::DYNAMIC_SHAPE);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100952}
953
Xusong Wange9da9852020-01-13 11:44:45 -0800954TEST_P(MemoryDomainTest, Test) {
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000955 Execute(kDevice, kTestModel, TestKind::MEMORY_DOMAIN);
Xusong Wange9da9852020-01-13 11:44:45 -0800956}
957
Miao Wange51e4a92020-01-23 13:53:21 -0800958TEST_P(FencedComputeTest, Test) {
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000959 Execute(kDevice, kTestModel, TestKind::FENCED_COMPUTE);
Miao Wange51e4a92020-01-23 13:53:21 -0800960}
961
Lev Proleev3c68b342020-01-09 16:37:28 +0000962TEST_P(QuantizationCouplingTest, Test) {
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000963 Execute(kDevice, kTestModel, TestKind::QUANTIZATION_COUPLING);
964}
965
966TEST_P(InfiniteLoopTimeoutTest, Test) {
967 Execute(kDevice, kTestModel, TestKind::INTINITE_LOOP_TIMEOUT);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100968}
969
970INSTANTIATE_GENERATED_TEST(GeneratedTest,
971 [](const TestModel& testModel) { return !testModel.expectFailure; });
972
Lev Proleev53a51cb2020-01-20 18:54:46 +0000973INSTANTIATE_GENERATED_TEST(DynamicOutputShapeTest, [](const TestModel& testModel) {
974 return !testModel.expectFailure && !testModel.hasScalarOutputs();
975});
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100976
Xusong Wange9da9852020-01-13 11:44:45 -0800977INSTANTIATE_GENERATED_TEST(MemoryDomainTest,
978 [](const TestModel& testModel) { return !testModel.expectFailure; });
979
Xusong Wangf07a9572020-03-13 10:02:36 -0700980INSTANTIATE_GENERATED_TEST(FencedComputeTest,
981 [](const TestModel& testModel) { return !testModel.expectFailure; });
Miao Wange51e4a92020-01-23 13:53:21 -0800982
Lev Proleev3c68b342020-01-09 16:37:28 +0000983INSTANTIATE_GENERATED_TEST(QuantizationCouplingTest, [](const TestModel& testModel) {
Xusong Wang3e64ecc2020-03-16 17:13:41 -0700984 return !testModel.expectFailure && testModel.hasQuant8CoupledOperands() &&
985 testModel.main.operations.size() == 1;
Lev Proleev9226c1e2019-10-03 14:43:18 +0100986});
987
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000988INSTANTIATE_GENERATED_TEST(InfiniteLoopTimeoutTest, [](const TestModel& testModel) {
989 return testModel.isInfiniteLoopTimeoutTest();
990});
991
Lev Proleev26d1bc82019-08-30 11:57:18 +0100992} // namespace android::hardware::neuralnetworks::V1_3::vts::functional