blob: 914a01a5e610ccabc84db886aaef7f6ec2ee154a [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 Wang91521ab2020-05-08 16:52:16 -0700571
572 constexpr uint32_t kInsufficientOutputIndex = 0;
Xusong Wange9da9852020-01-13 11:44:45 -0800573 if (testConfig.outputType == OutputType::INSUFFICIENT) {
Xusong Wang91521ab2020-05-08 16:52:16 -0700574 makeOutputInsufficientSize(kInsufficientOutputIndex, &request);
Xusong Wange9da9852020-01-13 11:44:45 -0800575 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100576
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000577 OptionalTimeoutDuration loopTimeoutDuration;
578 // OutputType::MISSED_DEADLINE is only used by
579 // TestKind::INTINITE_LOOP_TIMEOUT tests to verify that an infinite loop is
580 // aborted after a timeout.
581 if (testConfig.outputType == OutputType::MISSED_DEADLINE) {
582 // Override the default loop timeout duration with a small value to
583 // speed up test execution.
584 constexpr uint64_t kMillisecond = 1'000'000;
585 loopTimeoutDuration.nanoseconds(1 * kMillisecond);
586 }
587
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100588 ErrorStatus executionStatus;
589 hidl_vec<OutputShape> outputShapes;
590 Timing timing;
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100591 switch (testConfig.executor) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100592 case Executor::ASYNC: {
593 SCOPED_TRACE("asynchronous");
594
595 // launch execution
596 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000597 Return<ErrorStatus> executionLaunchStatus =
598 ExecutePreparedModel(preparedModel, request, testConfig.measureTiming,
599 loopTimeoutDuration, executionCallback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100600 ASSERT_TRUE(executionLaunchStatus.isOk());
601 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
602
603 // retrieve execution status
604 executionCallback->wait();
605 executionStatus = executionCallback->getStatus();
606 outputShapes = executionCallback->getOutputShapes();
607 timing = executionCallback->getTiming();
608
609 break;
610 }
611 case Executor::SYNC: {
612 SCOPED_TRACE("synchronous");
613
614 // execute
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000615 Return<ErrorStatus> executionReturnStatus =
616 ExecutePreparedModel(preparedModel, request, testConfig.measureTiming,
617 loopTimeoutDuration, &outputShapes, &timing);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100618 ASSERT_TRUE(executionReturnStatus.isOk());
619 executionStatus = static_cast<ErrorStatus>(executionReturnStatus);
620
621 break;
622 }
623 case Executor::BURST: {
Xusong Wangb345a462019-11-27 12:46:48 -0800624 // TODO(butlermichael): Check if we need to test burst in V1_3 if the interface remains
625 // V1_2.
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100626 SCOPED_TRACE("burst");
627
Xusong Wange9da9852020-01-13 11:44:45 -0800628 // check compliance
629 ASSERT_TRUE(nn::compliantWithV1_0(request));
630 V1_0::Request request10 = nn::convertToV1_0(request);
631
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100632 // create burst
633 const std::shared_ptr<::android::nn::ExecutionBurstController> controller =
634 CreateBurst(preparedModel);
635 ASSERT_NE(nullptr, controller.get());
636
637 // create memory keys
Xusong Wangb345a462019-11-27 12:46:48 -0800638 std::vector<intptr_t> keys(request10.pools.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100639 for (size_t i = 0; i < keys.size(); ++i) {
Xusong Wangb345a462019-11-27 12:46:48 -0800640 keys[i] = reinterpret_cast<intptr_t>(&request10.pools[i]);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100641 }
642
643 // execute burst
Michael Butler648ada52019-07-25 17:22:11 -0700644 int n;
645 std::tie(n, outputShapes, timing, std::ignore) =
Xusong Wangb345a462019-11-27 12:46:48 -0800646 controller->compute(request10, testConfig.measureTiming, keys);
Michael Butler648ada52019-07-25 17:22:11 -0700647 executionStatus = nn::convertResultCodeToErrorStatus(n);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100648
649 break;
650 }
Miao Wang2b5c4cd2019-12-26 18:03:56 -0800651 case Executor::FENCED: {
652 SCOPED_TRACE("fenced");
653 ErrorStatus result;
Miao Wang99753d92020-01-21 13:15:09 -0800654 hidl_handle syncFenceHandle;
655 sp<IFencedExecutionCallback> fencedCallback;
Miao Wang2d74d7d2020-02-25 15:54:08 -0800656 auto callbackFunc = [&result, &syncFenceHandle, &fencedCallback](
657 ErrorStatus error, const hidl_handle& handle,
658 const sp<IFencedExecutionCallback>& callback) {
659 result = error;
660 syncFenceHandle = handle;
661 fencedCallback = callback;
662 };
663 Return<void> ret =
664 preparedModel->executeFenced(request, {}, testConfig.measureTiming, {},
665 loopTimeoutDuration, {}, callbackFunc);
Miao Wang2b5c4cd2019-12-26 18:03:56 -0800666 ASSERT_TRUE(ret.isOk());
667 if (result != ErrorStatus::NONE) {
Miao Wang99753d92020-01-21 13:15:09 -0800668 ASSERT_EQ(syncFenceHandle.getNativeHandle(), nullptr);
669 ASSERT_EQ(fencedCallback, nullptr);
Miao Wang40689072020-02-25 16:57:53 -0800670 executionStatus = result;
Miao Wang702a91a2020-03-20 15:09:29 -0700671 timing = {UINT64_MAX, UINT64_MAX};
Miao Wang99753d92020-01-21 13:15:09 -0800672 } else if (syncFenceHandle.getNativeHandle()) {
Miao Wang2d74d7d2020-02-25 15:54:08 -0800673 // If a sync fence is returned, try start another run waiting for the sync fence.
674 ret = preparedModel->executeFenced(request, {syncFenceHandle},
675 testConfig.measureTiming, {},
676 loopTimeoutDuration, {}, callbackFunc);
677 ASSERT_TRUE(ret.isOk());
678 ASSERT_EQ(result, ErrorStatus::NONE);
Miao Wang99753d92020-01-21 13:15:09 -0800679 waitForSyncFence(syncFenceHandle.getNativeHandle()->data[0]);
Miao Wang2b5c4cd2019-12-26 18:03:56 -0800680 }
681 if (result == ErrorStatus::NONE) {
Miao Wang99753d92020-01-21 13:15:09 -0800682 ASSERT_NE(fencedCallback, nullptr);
683 Return<void> ret = fencedCallback->getExecutionInfo(
684 [&executionStatus, &timing](ErrorStatus error, Timing t, Timing) {
Miao Wang2b5c4cd2019-12-26 18:03:56 -0800685 executionStatus = error;
686 timing = t;
687 });
688 ASSERT_TRUE(ret.isOk());
689 }
690 break;
691 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100692 }
693
Miao Wang40689072020-02-25 16:57:53 -0800694 if (testConfig.outputType != OutputType::FULLY_SPECIFIED &&
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100695 executionStatus == ErrorStatus::GENERAL_FAILURE) {
Lev Proleev9226c1e2019-10-03 14:43:18 +0100696 if (skipped != nullptr) {
697 *skipped = true;
698 }
699 if (!testConfig.reportSkipping) {
700 return;
701 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100702 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
703 "execute model that it does not support.";
704 std::cout << "[ ] Early termination of test because vendor service cannot "
705 "execute model that it does not support."
706 << std::endl;
707 GTEST_SKIP();
708 }
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100709 if (testConfig.measureTiming == MeasureTiming::NO) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100710 EXPECT_EQ(UINT64_MAX, timing.timeOnDevice);
711 EXPECT_EQ(UINT64_MAX, timing.timeInDriver);
712 } else {
713 if (timing.timeOnDevice != UINT64_MAX && timing.timeInDriver != UINT64_MAX) {
714 EXPECT_LE(timing.timeOnDevice, timing.timeInDriver);
715 }
716 }
717
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100718 switch (testConfig.outputType) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100719 case OutputType::FULLY_SPECIFIED:
Xusong Wangf07a9572020-03-13 10:02:36 -0700720 if (testConfig.executor == Executor::FENCED && hasZeroSizedOutput(testModel)) {
721 // Executor::FENCED does not support zero-sized output.
722 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
723 return;
724 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100725 // If the model output operands are fully specified, outputShapes must be either
726 // either empty, or have the same number of elements as the number of outputs.
727 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
728 ASSERT_TRUE(outputShapes.size() == 0 ||
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000729 outputShapes.size() == testModel.main.outputIndexes.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100730 break;
731 case OutputType::UNSPECIFIED:
Miao Wang40689072020-02-25 16:57:53 -0800732 if (testConfig.executor == Executor::FENCED) {
733 // For Executor::FENCED, the output shape must be fully specified.
734 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
735 return;
736 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100737 // If the model output operands are not fully specified, outputShapes must have
738 // the same number of elements as the number of outputs.
739 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000740 ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100741 break;
742 case OutputType::INSUFFICIENT:
Miao Wang40689072020-02-25 16:57:53 -0800743 if (testConfig.executor == Executor::FENCED) {
744 // For Executor::FENCED, the output shape must be fully specified.
745 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
746 return;
747 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100748 ASSERT_EQ(ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, executionStatus);
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000749 ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size());
Xusong Wang91521ab2020-05-08 16:52:16 -0700750 // Check that all returned output dimensions are at least as fully specified as the
751 // union of the information about the corresponding operand in the model and in the
752 // request. In this test, all model outputs have known rank with all dimensions
753 // unspecified, and no dimensional information is provided in the request.
754 for (uint32_t i = 0; i < outputShapes.size(); i++) {
755 ASSERT_EQ(outputShapes[i].isSufficient, i != kInsufficientOutputIndex);
756 const auto& actual = outputShapes[i].dimensions;
757 const auto& golden =
758 testModel.main.operands[testModel.main.outputIndexes[i]].dimensions;
759 ASSERT_EQ(actual.size(), golden.size());
760 for (uint32_t j = 0; j < actual.size(); j++) {
761 if (actual[j] == 0) continue;
762 EXPECT_EQ(actual[j], golden[j]) << "index: " << j;
763 }
764 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100765 return;
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000766 case OutputType::MISSED_DEADLINE:
767 ASSERT_TRUE(executionStatus == ErrorStatus::MISSED_DEADLINE_TRANSIENT ||
768 executionStatus == ErrorStatus::MISSED_DEADLINE_PERSISTENT)
769 << "executionStatus = " << executionStatus;
770 return;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100771 }
772
773 // Go through all outputs, check returned output shapes.
774 for (uint32_t i = 0; i < outputShapes.size(); i++) {
775 EXPECT_TRUE(outputShapes[i].isSufficient);
Slava Shklyaev0fff59b2020-01-31 15:14:24 +0000776 const auto& expect = testModel.main.operands[testModel.main.outputIndexes[i]].dimensions;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100777 const std::vector<uint32_t> actual = outputShapes[i].dimensions;
778 EXPECT_EQ(expect, actual);
779 }
780
781 // Retrieve execution results.
Xusong Wang75e63ad2020-02-25 11:43:10 -0800782 const std::vector<TestBuffer> outputs = context.getOutputBuffers(testModel, request);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100783
784 // We want "close-enough" results.
785 checkResults(testModel, outputs);
786}
787
Xusong Wange9da9852020-01-13 11:44:45 -0800788void EvaluatePreparedModel(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel,
789 const TestModel& testModel, TestKind testKind) {
Lev Proleev56cda832019-12-05 14:49:47 +0000790 std::vector<OutputType> outputTypesList;
791 std::vector<MeasureTiming> measureTimingList;
792 std::vector<Executor> executorList;
Xusong Wang75e63ad2020-02-25 11:43:10 -0800793 std::vector<MemoryType> memoryTypeList;
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100794
Lev Proleev9226c1e2019-10-03 14:43:18 +0100795 switch (testKind) {
796 case TestKind::GENERAL: {
797 outputTypesList = {OutputType::FULLY_SPECIFIED};
798 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wange51e4a92020-01-23 13:53:21 -0800799 executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST};
Xusong Wang75e63ad2020-02-25 11:43:10 -0800800 memoryTypeList = {MemoryType::ASHMEM};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100801 } break;
802 case TestKind::DYNAMIC_SHAPE: {
803 outputTypesList = {OutputType::UNSPECIFIED, OutputType::INSUFFICIENT};
804 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wang40689072020-02-25 16:57:53 -0800805 executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST, Executor::FENCED};
Xusong Wang75e63ad2020-02-25 11:43:10 -0800806 memoryTypeList = {MemoryType::ASHMEM};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100807 } break;
Xusong Wange9da9852020-01-13 11:44:45 -0800808 case TestKind::MEMORY_DOMAIN: {
809 outputTypesList = {OutputType::FULLY_SPECIFIED};
810 measureTimingList = {MeasureTiming::NO};
Miao Wang2d74d7d2020-02-25 15:54:08 -0800811 executorList = {Executor::ASYNC, Executor::SYNC, Executor::FENCED};
Xusong Wang75e63ad2020-02-25 11:43:10 -0800812 memoryTypeList = {MemoryType::BLOB_AHWB, MemoryType::DEVICE};
Xusong Wange9da9852020-01-13 11:44:45 -0800813 } break;
Miao Wange51e4a92020-01-23 13:53:21 -0800814 case TestKind::FENCED_COMPUTE: {
815 outputTypesList = {OutputType::FULLY_SPECIFIED};
816 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
817 executorList = {Executor::FENCED};
Xusong Wang75e63ad2020-02-25 11:43:10 -0800818 memoryTypeList = {MemoryType::ASHMEM};
Miao Wange51e4a92020-01-23 13:53:21 -0800819 } break;
Lev Proleev9226c1e2019-10-03 14:43:18 +0100820 case TestKind::QUANTIZATION_COUPLING: {
821 LOG(FATAL) << "Wrong TestKind for EvaluatePreparedModel";
822 return;
823 } break;
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000824 case TestKind::INTINITE_LOOP_TIMEOUT: {
825 outputTypesList = {OutputType::MISSED_DEADLINE};
826 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
827 // Burst does not support V1_3 loop timeout.
828 executorList = {Executor::ASYNC, Executor::SYNC, Executor::FENCED};
Xusong Wang75e63ad2020-02-25 11:43:10 -0800829 memoryTypeList = {MemoryType::ASHMEM};
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000830 } break;
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100831 }
832
833 for (const OutputType outputType : outputTypesList) {
834 for (const MeasureTiming measureTiming : measureTimingList) {
835 for (const Executor executor : executorList) {
Xusong Wang75e63ad2020-02-25 11:43:10 -0800836 for (const MemoryType memoryType : memoryTypeList) {
837 const TestConfig testConfig(executor, measureTiming, outputType, memoryType);
838 EvaluatePreparedModel(device, preparedModel, testModel, testConfig);
839 }
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100840 }
841 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100842 }
843}
844
Xusong Wange9da9852020-01-13 11:44:45 -0800845void EvaluatePreparedCoupledModels(const sp<IDevice>& device,
846 const sp<IPreparedModel>& preparedModel,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100847 const TestModel& testModel,
848 const sp<IPreparedModel>& preparedCoupledModel,
849 const TestModel& coupledModel) {
Lev Proleev56cda832019-12-05 14:49:47 +0000850 const std::vector<OutputType> outputTypesList = {OutputType::FULLY_SPECIFIED};
851 const std::vector<MeasureTiming> measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wang2b5c4cd2019-12-26 18:03:56 -0800852 const std::vector<Executor> executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST,
853 Executor::FENCED};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100854
855 for (const OutputType outputType : outputTypesList) {
856 for (const MeasureTiming measureTiming : measureTimingList) {
857 for (const Executor executor : executorList) {
Xusong Wang75e63ad2020-02-25 11:43:10 -0800858 const TestConfig testConfig(executor, measureTiming, outputType, MemoryType::ASHMEM,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100859 /*reportSkipping=*/false);
860 bool baseSkipped = false;
Xusong Wange9da9852020-01-13 11:44:45 -0800861 EvaluatePreparedModel(device, preparedModel, testModel, testConfig, &baseSkipped);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100862 bool coupledSkipped = false;
Xusong Wange9da9852020-01-13 11:44:45 -0800863 EvaluatePreparedModel(device, preparedCoupledModel, coupledModel, testConfig,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100864 &coupledSkipped);
865 ASSERT_EQ(baseSkipped, coupledSkipped);
866 if (baseSkipped) {
867 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
868 "execute model that it does not support.";
869 std::cout << "[ ] Early termination of test because vendor service "
870 "cannot "
871 "execute model that it does not support."
872 << std::endl;
873 GTEST_SKIP();
874 }
875 }
876 }
877 }
878}
879
880void Execute(const sp<IDevice>& device, const TestModel& testModel, TestKind testKind) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100881 Model model = createModel(testModel);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100882 if (testKind == TestKind::DYNAMIC_SHAPE) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100883 makeOutputDimensionsUnspecified(&model);
884 }
885
886 sp<IPreparedModel> preparedModel;
Lev Proleev9226c1e2019-10-03 14:43:18 +0100887 switch (testKind) {
Xusong Wange9da9852020-01-13 11:44:45 -0800888 case TestKind::GENERAL:
889 case TestKind::DYNAMIC_SHAPE:
Miao Wange51e4a92020-01-23 13:53:21 -0800890 case TestKind::MEMORY_DOMAIN:
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000891 case TestKind::FENCED_COMPUTE:
892 case TestKind::INTINITE_LOOP_TIMEOUT: {
Lev Proleev9226c1e2019-10-03 14:43:18 +0100893 createPreparedModel(device, model, &preparedModel);
894 if (preparedModel == nullptr) return;
Xusong Wange9da9852020-01-13 11:44:45 -0800895 EvaluatePreparedModel(device, preparedModel, testModel, testKind);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100896 } break;
897 case TestKind::QUANTIZATION_COUPLING: {
Lev Proleev673fdcf2020-01-02 18:22:30 +0000898 ASSERT_TRUE(testModel.hasQuant8CoupledOperands());
Michael Butler95899b32020-01-07 14:52:44 -0800899 createPreparedModel(device, model, &preparedModel,
900 /*reportSkipping*/ false);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100901 TestModel signedQuantizedModel = convertQuant8AsymmOperandsToSigned(testModel);
902 sp<IPreparedModel> preparedCoupledModel;
903 createPreparedModel(device, createModel(signedQuantizedModel), &preparedCoupledModel,
904 /*reportSkipping*/ false);
905 // If we couldn't prepare a model with unsigned quantization, we must
906 // fail to prepare a model with signed quantization as well.
907 if (preparedModel == nullptr) {
908 ASSERT_EQ(preparedCoupledModel, nullptr);
909 // If we failed to prepare both of the models, we can safely skip
910 // the test.
911 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
912 "prepare model that it does not support.";
913 std::cout
914 << "[ ] Early termination of test because vendor service cannot "
915 "prepare model that it does not support."
916 << std::endl;
917 GTEST_SKIP();
918 }
919 ASSERT_NE(preparedCoupledModel, nullptr);
Xusong Wange9da9852020-01-13 11:44:45 -0800920 EvaluatePreparedCoupledModels(device, preparedModel, testModel, preparedCoupledModel,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100921 signedQuantizedModel);
922 } break;
923 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100924}
925
926void GeneratedTestBase::SetUp() {
927 testing::TestWithParam<GeneratedTestParam>::SetUp();
928 ASSERT_NE(kDevice, nullptr);
929}
930
931std::vector<NamedModel> getNamedModels(const FilterFn& filter) {
932 return TestModelManager::get().getTestModels(filter);
933}
934
Michael Butlerb1865b62020-03-19 17:10:34 -0700935std::vector<NamedModel> getNamedModels(const FilterNameFn& filter) {
936 return TestModelManager::get().getTestModels(filter);
937}
938
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100939std::string printGeneratedTest(const testing::TestParamInfo<GeneratedTestParam>& info) {
940 const auto& [namedDevice, namedModel] = info.param;
941 return gtestCompliantName(getName(namedDevice) + "_" + getName(namedModel));
942}
943
944// Tag for the generated tests
945class GeneratedTest : public GeneratedTestBase {};
946
947// Tag for the dynamic output shape tests
948class DynamicOutputShapeTest : public GeneratedTest {};
949
Xusong Wange9da9852020-01-13 11:44:45 -0800950// Tag for the memory domain tests
951class MemoryDomainTest : public GeneratedTest {};
952
Miao Wange51e4a92020-01-23 13:53:21 -0800953// Tag for the fenced compute tests
954class FencedComputeTest : public GeneratedTest {};
955
Lev Proleev9226c1e2019-10-03 14:43:18 +0100956// Tag for the dynamic output shape tests
Lev Proleev3c68b342020-01-09 16:37:28 +0000957class QuantizationCouplingTest : public GeneratedTest {};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100958
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000959// Tag for the loop timeout tests
960class InfiniteLoopTimeoutTest : public GeneratedTest {};
961
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100962TEST_P(GeneratedTest, Test) {
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000963 Execute(kDevice, kTestModel, TestKind::GENERAL);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100964}
965
966TEST_P(DynamicOutputShapeTest, Test) {
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000967 Execute(kDevice, kTestModel, TestKind::DYNAMIC_SHAPE);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100968}
969
Xusong Wange9da9852020-01-13 11:44:45 -0800970TEST_P(MemoryDomainTest, Test) {
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000971 Execute(kDevice, kTestModel, TestKind::MEMORY_DOMAIN);
Xusong Wange9da9852020-01-13 11:44:45 -0800972}
973
Miao Wange51e4a92020-01-23 13:53:21 -0800974TEST_P(FencedComputeTest, Test) {
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000975 Execute(kDevice, kTestModel, TestKind::FENCED_COMPUTE);
Miao Wange51e4a92020-01-23 13:53:21 -0800976}
977
Lev Proleev3c68b342020-01-09 16:37:28 +0000978TEST_P(QuantizationCouplingTest, Test) {
Slava Shklyaeva37c2c82020-02-18 16:11:32 +0000979 Execute(kDevice, kTestModel, TestKind::QUANTIZATION_COUPLING);
980}
981
982TEST_P(InfiniteLoopTimeoutTest, Test) {
983 Execute(kDevice, kTestModel, TestKind::INTINITE_LOOP_TIMEOUT);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100984}
985
986INSTANTIATE_GENERATED_TEST(GeneratedTest,
987 [](const TestModel& testModel) { return !testModel.expectFailure; });
988
Lev Proleev53a51cb2020-01-20 18:54:46 +0000989INSTANTIATE_GENERATED_TEST(DynamicOutputShapeTest, [](const TestModel& testModel) {
990 return !testModel.expectFailure && !testModel.hasScalarOutputs();
991});
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100992
Xusong Wange9da9852020-01-13 11:44:45 -0800993INSTANTIATE_GENERATED_TEST(MemoryDomainTest,
994 [](const TestModel& testModel) { return !testModel.expectFailure; });
995
Xusong Wangf07a9572020-03-13 10:02:36 -0700996INSTANTIATE_GENERATED_TEST(FencedComputeTest,
997 [](const TestModel& testModel) { return !testModel.expectFailure; });
Miao Wange51e4a92020-01-23 13:53:21 -0800998
Lev Proleev3c68b342020-01-09 16:37:28 +0000999INSTANTIATE_GENERATED_TEST(QuantizationCouplingTest, [](const TestModel& testModel) {
Xusong Wang3e64ecc2020-03-16 17:13:41 -07001000 return !testModel.expectFailure && testModel.hasQuant8CoupledOperands() &&
1001 testModel.main.operations.size() == 1;
Lev Proleev9226c1e2019-10-03 14:43:18 +01001002});
1003
Slava Shklyaeva37c2c82020-02-18 16:11:32 +00001004INSTANTIATE_GENERATED_TEST(InfiniteLoopTimeoutTest, [](const TestModel& testModel) {
1005 return testModel.isInfiniteLoopTimeoutTest();
1006});
1007
Lev Proleev26d1bc82019-08-30 11:57:18 +01001008} // namespace android::hardware::neuralnetworks::V1_3::vts::functional