blob: ff71778d8308a2c0f04c458a809cfde1a72b2bbb [file] [log] [blame]
Lev Proleev13fdfcd2019-08-30 11:35:34 +01001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "GeneratedTestHarness.h"
18
19#include <android-base/logging.h>
20#include <android/hardware/neuralnetworks/1.0/IDevice.h>
21#include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h>
22#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
23#include <android/hardware/neuralnetworks/1.0/IPreparedModelCallback.h>
24#include <android/hardware/neuralnetworks/1.0/types.h>
25#include <android/hardware/neuralnetworks/1.1/IDevice.h>
26#include <android/hardware/neuralnetworks/1.2/IDevice.h>
27#include <android/hardware/neuralnetworks/1.2/IExecutionCallback.h>
28#include <android/hardware/neuralnetworks/1.2/IPreparedModel.h>
29#include <android/hardware/neuralnetworks/1.2/IPreparedModelCallback.h>
Lev Proleev26d1bc82019-08-30 11:57:18 +010030#include <android/hardware/neuralnetworks/1.2/types.h>
31#include <android/hardware/neuralnetworks/1.3/IDevice.h>
Miao Wang2c4e0232019-12-26 18:03:56 -080032#include <android/hardware/neuralnetworks/1.3/IFencedExecutionCallback.h>
Xusong Wang1b3f4262019-10-25 12:07:17 -070033#include <android/hardware/neuralnetworks/1.3/IPreparedModel.h>
Xusong Wangcc47dff2019-10-23 10:35:07 -070034#include <android/hardware/neuralnetworks/1.3/IPreparedModelCallback.h>
Lev Proleev26d1bc82019-08-30 11:57:18 +010035#include <android/hardware/neuralnetworks/1.3/types.h>
Lev Proleev13fdfcd2019-08-30 11:35:34 +010036#include <android/hidl/allocator/1.0/IAllocator.h>
37#include <android/hidl/memory/1.0/IMemory.h>
Miao Wang2c4e0232019-12-26 18:03:56 -080038#include <android/sync.h>
Lev Proleev56cda832019-12-05 14:49:47 +000039#include <gtest/gtest.h>
Lev Proleev13fdfcd2019-08-30 11:35:34 +010040#include <hidlmemory/mapping.h>
41
Lev Proleev13fdfcd2019-08-30 11:35:34 +010042#include <algorithm>
Michael Butler648ada52019-07-25 17:22:11 -070043#include <chrono>
Lev Proleev13fdfcd2019-08-30 11:35:34 +010044#include <iostream>
45#include <numeric>
Lev Proleev56cda832019-12-05 14:49:47 +000046#include <vector>
Lev Proleev13fdfcd2019-08-30 11:35:34 +010047
48#include "1.0/Utils.h"
Xusong Wangcc47dff2019-10-23 10:35:07 -070049#include "1.3/Callbacks.h"
Michael Butler616701d2020-01-07 14:52:44 -080050#include "1.3/Utils.h"
Lev Proleev13fdfcd2019-08-30 11:35:34 +010051#include "ExecutionBurstController.h"
52#include "MemoryUtils.h"
53#include "TestHarness.h"
54#include "Utils.h"
55#include "VtsHalNeuralnetworks.h"
56
Lev Proleev26d1bc82019-08-30 11:57:18 +010057namespace android::hardware::neuralnetworks::V1_3::vts::functional {
Lev Proleev13fdfcd2019-08-30 11:35:34 +010058
59using namespace test_helper;
60using hidl::memory::V1_0::IMemory;
Michael Butler9449a282019-12-11 19:08:08 -080061using implementation::ExecutionCallback;
Xusong Wangcc47dff2019-10-23 10:35:07 -070062using implementation::PreparedModelCallback;
Lev Proleev13fdfcd2019-08-30 11:35:34 +010063using V1_0::DataLocation;
Xusong Wang1f50e542020-01-13 11:44:45 -080064using V1_0::RequestArgument;
Lev Proleev13fdfcd2019-08-30 11:35:34 +010065using V1_1::ExecutionPreference;
Lev Proleev26d1bc82019-08-30 11:57:18 +010066using V1_2::Constant;
Lev Proleev26d1bc82019-08-30 11:57:18 +010067using V1_2::MeasureTiming;
Lev Proleev26d1bc82019-08-30 11:57:18 +010068using V1_2::OutputShape;
69using V1_2::SymmPerChannelQuantParams;
70using V1_2::Timing;
Lev Proleev13fdfcd2019-08-30 11:35:34 +010071using HidlToken = hidl_array<uint8_t, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
72
Lev Proleev0d4ba3f2019-10-02 17:32:06 +010073namespace {
74
Slava Shklyaevcc873ae2020-02-18 16:11:32 +000075enum class OutputType { FULLY_SPECIFIED, UNSPECIFIED, INSUFFICIENT, MISSED_DEADLINE };
Lev Proleev13fdfcd2019-08-30 11:35:34 +010076
Xusong Wang1f50e542020-01-13 11:44:45 -080077enum class IOType { INPUT, OUTPUT };
78
Lev Proleev0d4ba3f2019-10-02 17:32:06 +010079struct TestConfig {
80 Executor executor;
81 MeasureTiming measureTiming;
82 OutputType outputType;
Xusong Wang1f50e542020-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 Wang1f50e542020-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 Wang1f50e542020-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 Wang1f50e542020-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 Wang1f50e542020-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 Wang1f50e542020-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 Butler6a4172c2020-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 Wang1f50e542020-01-13 11:44:45 -0800116 allocateInternal<ioType>(index, &buffer);
117 return buffer;
118 }
119
120 private:
121 template <IOType ioType>
Michael Butler6a4172c2020-02-04 16:15:04 -0800122 void allocateInternal(uint32_t index, std::pair<sp<IBuffer>, uint32_t>* result) {
Xusong Wang1f50e542020-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 Butler6a4172c2020-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 Wang1f50e542020-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 Shklyaev1f98e2e2020-01-31 15:14:24 +0000161 const auto& testBuffer =
162 kTestModel.main.operands[kTestModel.main.inputIndexes[index]].data;
Xusong Wang1f50e542020-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 Shklyaev1f98e2e2020-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 Shklyaev1f98e2e2020-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 Shklyaev1f98e2e2020-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 Shklyaev1f98e2e2020-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 Butler6a4172c2020-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 Shklyaev1f98e2e2020-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 Shklyaev1f98e2e2020-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 Wang9c415912020-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 Shklyaev1f98e2e2020-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 Shklyaev1f98e2e2020-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 Shklyaev1f98e2e2020-01-31 15:14:24 +0000309 copyTestBuffers(constReferences, mappedPtr);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100310 }
311
Slava Shklyaev1f98e2e2020-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 Shklyaev1f98e2e2020-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 Wang1f50e542020-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 Shklyaeva785a3f2019-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 Wang41adc5b2020-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 Wang1f50e542020-01-13 11:44:45 -0800341
Xusong Wang41adc5b2020-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 Wang1f50e542020-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 Wang41adc5b2020-02-25 11:43:10 -0800367 DeviceMemoryAllocator allocator(kDevice, kPreparedModel, testModel);
Michael Butler6a4172c2020-02-04 16:15:04 -0800368 std::vector<uint32_t> tokens;
Xusong Wang41adc5b2020-02-25 11:43:10 -0800369 mBuffers.clear();
Xusong Wang1f50e542020-01-13 11:44:45 -0800370
371 // Model inputs.
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000372 hidl_vec<RequestArgument> inputs(testModel.main.inputIndexes.size());
Xusong Wang1f50e542020-01-13 11:44:45 -0800373 size_t inputSize = 0;
Slava Shklyaev1f98e2e2020-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 Wang1f50e542020-01-13 11:44:45 -0800376 if (op.data.size() == 0) {
377 // Omitted input.
378 inputs[i] = {.hasNoValue = true};
379 continue;
Xusong Wang41adc5b2020-02-25 11:43:10 -0800380 } else if (memoryType == MemoryType::DEVICE) {
Xusong Wang1f50e542020-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 Wang41adc5b2020-02-25 11:43:10 -0800384 DataLocation loc = {.poolIndex = static_cast<uint32_t>(mBuffers.size() +
Xusong Wang1f50e542020-01-13 11:44:45 -0800385 kDeviceMemoryBeginIndex)};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800386 mBuffers.push_back(std::move(buffer));
Xusong Wang1f50e542020-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 Shklyaev1f98e2e2020-01-31 15:14:24 +0000402 hidl_vec<RequestArgument> outputs(testModel.main.outputIndexes.size());
Xusong Wang1f50e542020-01-13 11:44:45 -0800403 size_t outputSize = 0;
Slava Shklyaev1f98e2e2020-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 Wang41adc5b2020-02-25 11:43:10 -0800406 if (memoryType == MemoryType::DEVICE) {
Xusong Wang1f50e542020-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 Wang41adc5b2020-02-25 11:43:10 -0800410 DataLocation loc = {.poolIndex = static_cast<uint32_t>(mBuffers.size() +
Xusong Wang1f50e542020-01-13 11:44:45 -0800411 kDeviceMemoryBeginIndex)};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800412 mBuffers.push_back(std::move(buffer));
Xusong Wang1f50e542020-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 Wang41adc5b2020-02-25 11:43:10 -0800435 if (memoryType == MemoryType::DEVICE && mBuffers.empty()) {
436 return std::nullopt;
437 }
438
Xusong Wang1f50e542020-01-13 11:44:45 -0800439 // Memory pools.
Xusong Wang41adc5b2020-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 Wang1f50e542020-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 Wang41adc5b2020-02-25 11:43:10 -0800457 uint8_t* inputPtr = mInputMemory->getPointer();
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000458 for (uint32_t i = 0; i < testModel.main.inputIndexes.size(); i++) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800459 if (!inputs[i].hasNoValue && inputs[i].location.poolIndex == kInputPoolIndex) {
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000460 const auto& op = testModel.main.operands[testModel.main.inputIndexes[i]];
Xusong Wang1f50e542020-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 Wang41adc5b2020-02-25 11:43:10 -0800466 return Request{
Xusong Wang1f50e542020-01-13 11:44:45 -0800467 .inputs = std::move(inputs), .outputs = std::move(outputs), .pools = std::move(pools)};
Xusong Wang41adc5b2020-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 Wang1f50e542020-01-13 11:44:45 -0800493}
494
495// Get a TestBuffer with data copied from an IBuffer object.
Xusong Wang41adc5b2020-02-25 11:43:10 -0800496void ExecutionContextV1_3::getBuffer(const sp<IBuffer>& buffer, size_t size,
497 TestBuffer* testBuffer) const {
Xusong Wang1f50e542020-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 Wang5882d6d2020-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 Shklyaevcc873ae2020-02-18 16:11:32 +0000522 const OptionalTimeoutDuration& loopTimeoutDuration,
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100523 sp<ExecutionCallback>& callback) {
Slava Shklyaevcc873ae2020-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 Shklyaevcc873ae2020-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 Wangebd88ba2019-10-28 11:11:19 -0700532 Return<void> ret = preparedModel->executeSynchronously_1_3(
Slava Shklyaevcc873ae2020-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 Wang1f50e542020-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 Wang41adc5b2020-02-25 11:43:10 -0800563 ExecutionContextV1_3 context(device, preparedModel);
564 auto maybeRequest = context.createRequest(testModel, testConfig.memoryType);
Xusong Wang1f50e542020-01-13 11:44:45 -0800565 // Skip if testing memory domain but no device memory has been allocated.
Xusong Wang41adc5b2020-02-25 11:43:10 -0800566 if (!maybeRequest.has_value()) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800567 return;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100568 }
Xusong Wang41adc5b2020-02-25 11:43:10 -0800569
570 Request request = std::move(maybeRequest.value());
Xusong Wang1f50e542020-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 Shklyaevcc873ae2020-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 Shklyaevcc873ae2020-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 Shklyaevcc873ae2020-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 Wang931d5a12019-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 Wang1f50e542020-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 Wang931d5a12019-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 Wang931d5a12019-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 Wang931d5a12019-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 Wang2c4e0232019-12-26 18:03:56 -0800649 case Executor::FENCED: {
650 SCOPED_TRACE("fenced");
651 ErrorStatus result;
Miao Wang3fd70b02020-01-21 13:15:09 -0800652 hidl_handle syncFenceHandle;
653 sp<IFencedExecutionCallback> fencedCallback;
Miao Wang34dfa2f2020-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 Wang2c4e0232019-12-26 18:03:56 -0800664 ASSERT_TRUE(ret.isOk());
665 if (result != ErrorStatus::NONE) {
Miao Wang3fd70b02020-01-21 13:15:09 -0800666 ASSERT_EQ(syncFenceHandle.getNativeHandle(), nullptr);
667 ASSERT_EQ(fencedCallback, nullptr);
Miao Wangfa161c72020-02-25 16:57:53 -0800668 executionStatus = result;
Miao Wang3fd70b02020-01-21 13:15:09 -0800669 } else if (syncFenceHandle.getNativeHandle()) {
Miao Wang34dfa2f2020-02-25 15:54:08 -0800670 // If a sync fence is returned, try start another run waiting for the sync fence.
671 ret = preparedModel->executeFenced(request, {syncFenceHandle},
672 testConfig.measureTiming, {},
673 loopTimeoutDuration, {}, callbackFunc);
674 ASSERT_TRUE(ret.isOk());
675 ASSERT_EQ(result, ErrorStatus::NONE);
Miao Wang3fd70b02020-01-21 13:15:09 -0800676 waitForSyncFence(syncFenceHandle.getNativeHandle()->data[0]);
Miao Wang2c4e0232019-12-26 18:03:56 -0800677 }
678 if (result == ErrorStatus::NONE) {
Miao Wang3fd70b02020-01-21 13:15:09 -0800679 ASSERT_NE(fencedCallback, nullptr);
680 Return<void> ret = fencedCallback->getExecutionInfo(
681 [&executionStatus, &timing](ErrorStatus error, Timing t, Timing) {
Miao Wang2c4e0232019-12-26 18:03:56 -0800682 executionStatus = error;
683 timing = t;
684 });
685 ASSERT_TRUE(ret.isOk());
686 }
687 break;
688 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100689 }
690
Miao Wangfa161c72020-02-25 16:57:53 -0800691 if (testConfig.outputType != OutputType::FULLY_SPECIFIED &&
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100692 executionStatus == ErrorStatus::GENERAL_FAILURE) {
Lev Proleev9226c1e2019-10-03 14:43:18 +0100693 if (skipped != nullptr) {
694 *skipped = true;
695 }
696 if (!testConfig.reportSkipping) {
697 return;
698 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100699 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
700 "execute model that it does not support.";
701 std::cout << "[ ] Early termination of test because vendor service cannot "
702 "execute model that it does not support."
703 << std::endl;
704 GTEST_SKIP();
705 }
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100706 if (testConfig.measureTiming == MeasureTiming::NO) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100707 EXPECT_EQ(UINT64_MAX, timing.timeOnDevice);
708 EXPECT_EQ(UINT64_MAX, timing.timeInDriver);
709 } else {
710 if (timing.timeOnDevice != UINT64_MAX && timing.timeInDriver != UINT64_MAX) {
711 EXPECT_LE(timing.timeOnDevice, timing.timeInDriver);
712 }
713 }
714
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100715 switch (testConfig.outputType) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100716 case OutputType::FULLY_SPECIFIED:
Xusong Wang5882d6d2020-03-13 10:02:36 -0700717 if (testConfig.executor == Executor::FENCED && hasZeroSizedOutput(testModel)) {
718 // Executor::FENCED does not support zero-sized output.
719 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
720 return;
721 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100722 // If the model output operands are fully specified, outputShapes must be either
723 // either empty, or have the same number of elements as the number of outputs.
724 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
725 ASSERT_TRUE(outputShapes.size() == 0 ||
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000726 outputShapes.size() == testModel.main.outputIndexes.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100727 break;
728 case OutputType::UNSPECIFIED:
Miao Wangfa161c72020-02-25 16:57:53 -0800729 if (testConfig.executor == Executor::FENCED) {
730 // For Executor::FENCED, the output shape must be fully specified.
731 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
732 return;
733 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100734 // If the model output operands are not fully specified, outputShapes must have
735 // the same number of elements as the number of outputs.
736 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000737 ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100738 break;
739 case OutputType::INSUFFICIENT:
Miao Wangfa161c72020-02-25 16:57:53 -0800740 if (testConfig.executor == Executor::FENCED) {
741 // For Executor::FENCED, the output shape must be fully specified.
742 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
743 return;
744 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100745 ASSERT_EQ(ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, executionStatus);
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000746 ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100747 ASSERT_FALSE(outputShapes[0].isSufficient);
748 return;
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000749 case OutputType::MISSED_DEADLINE:
750 ASSERT_TRUE(executionStatus == ErrorStatus::MISSED_DEADLINE_TRANSIENT ||
751 executionStatus == ErrorStatus::MISSED_DEADLINE_PERSISTENT)
752 << "executionStatus = " << executionStatus;
753 return;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100754 }
755
756 // Go through all outputs, check returned output shapes.
757 for (uint32_t i = 0; i < outputShapes.size(); i++) {
758 EXPECT_TRUE(outputShapes[i].isSufficient);
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000759 const auto& expect = testModel.main.operands[testModel.main.outputIndexes[i]].dimensions;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100760 const std::vector<uint32_t> actual = outputShapes[i].dimensions;
761 EXPECT_EQ(expect, actual);
762 }
763
764 // Retrieve execution results.
Xusong Wang41adc5b2020-02-25 11:43:10 -0800765 const std::vector<TestBuffer> outputs = context.getOutputBuffers(testModel, request);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100766
767 // We want "close-enough" results.
768 checkResults(testModel, outputs);
769}
770
Xusong Wang1f50e542020-01-13 11:44:45 -0800771void EvaluatePreparedModel(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel,
772 const TestModel& testModel, TestKind testKind) {
Lev Proleev56cda832019-12-05 14:49:47 +0000773 std::vector<OutputType> outputTypesList;
774 std::vector<MeasureTiming> measureTimingList;
775 std::vector<Executor> executorList;
Xusong Wang41adc5b2020-02-25 11:43:10 -0800776 std::vector<MemoryType> memoryTypeList;
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100777
Lev Proleev9226c1e2019-10-03 14:43:18 +0100778 switch (testKind) {
779 case TestKind::GENERAL: {
780 outputTypesList = {OutputType::FULLY_SPECIFIED};
781 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wang33173a32020-01-23 13:53:21 -0800782 executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800783 memoryTypeList = {MemoryType::ASHMEM};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100784 } break;
785 case TestKind::DYNAMIC_SHAPE: {
786 outputTypesList = {OutputType::UNSPECIFIED, OutputType::INSUFFICIENT};
787 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wangfa161c72020-02-25 16:57:53 -0800788 executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST, Executor::FENCED};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800789 memoryTypeList = {MemoryType::ASHMEM};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100790 } break;
Xusong Wang1f50e542020-01-13 11:44:45 -0800791 case TestKind::MEMORY_DOMAIN: {
792 outputTypesList = {OutputType::FULLY_SPECIFIED};
793 measureTimingList = {MeasureTiming::NO};
Miao Wang34dfa2f2020-02-25 15:54:08 -0800794 executorList = {Executor::ASYNC, Executor::SYNC, Executor::FENCED};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800795 memoryTypeList = {MemoryType::BLOB_AHWB, MemoryType::DEVICE};
Xusong Wang1f50e542020-01-13 11:44:45 -0800796 } break;
Miao Wang33173a32020-01-23 13:53:21 -0800797 case TestKind::FENCED_COMPUTE: {
798 outputTypesList = {OutputType::FULLY_SPECIFIED};
799 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
800 executorList = {Executor::FENCED};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800801 memoryTypeList = {MemoryType::ASHMEM};
Miao Wang33173a32020-01-23 13:53:21 -0800802 } break;
Lev Proleev9226c1e2019-10-03 14:43:18 +0100803 case TestKind::QUANTIZATION_COUPLING: {
804 LOG(FATAL) << "Wrong TestKind for EvaluatePreparedModel";
805 return;
806 } break;
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000807 case TestKind::INTINITE_LOOP_TIMEOUT: {
808 outputTypesList = {OutputType::MISSED_DEADLINE};
809 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
810 // Burst does not support V1_3 loop timeout.
811 executorList = {Executor::ASYNC, Executor::SYNC, Executor::FENCED};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800812 memoryTypeList = {MemoryType::ASHMEM};
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000813 } break;
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100814 }
815
816 for (const OutputType outputType : outputTypesList) {
817 for (const MeasureTiming measureTiming : measureTimingList) {
818 for (const Executor executor : executorList) {
Xusong Wang41adc5b2020-02-25 11:43:10 -0800819 for (const MemoryType memoryType : memoryTypeList) {
820 const TestConfig testConfig(executor, measureTiming, outputType, memoryType);
821 EvaluatePreparedModel(device, preparedModel, testModel, testConfig);
822 }
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100823 }
824 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100825 }
826}
827
Xusong Wang1f50e542020-01-13 11:44:45 -0800828void EvaluatePreparedCoupledModels(const sp<IDevice>& device,
829 const sp<IPreparedModel>& preparedModel,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100830 const TestModel& testModel,
831 const sp<IPreparedModel>& preparedCoupledModel,
832 const TestModel& coupledModel) {
Lev Proleev56cda832019-12-05 14:49:47 +0000833 const std::vector<OutputType> outputTypesList = {OutputType::FULLY_SPECIFIED};
834 const std::vector<MeasureTiming> measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wang2c4e0232019-12-26 18:03:56 -0800835 const std::vector<Executor> executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST,
836 Executor::FENCED};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100837
838 for (const OutputType outputType : outputTypesList) {
839 for (const MeasureTiming measureTiming : measureTimingList) {
840 for (const Executor executor : executorList) {
Xusong Wang41adc5b2020-02-25 11:43:10 -0800841 const TestConfig testConfig(executor, measureTiming, outputType, MemoryType::ASHMEM,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100842 /*reportSkipping=*/false);
843 bool baseSkipped = false;
Xusong Wang1f50e542020-01-13 11:44:45 -0800844 EvaluatePreparedModel(device, preparedModel, testModel, testConfig, &baseSkipped);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100845 bool coupledSkipped = false;
Xusong Wang1f50e542020-01-13 11:44:45 -0800846 EvaluatePreparedModel(device, preparedCoupledModel, coupledModel, testConfig,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100847 &coupledSkipped);
848 ASSERT_EQ(baseSkipped, coupledSkipped);
849 if (baseSkipped) {
850 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
851 "execute model that it does not support.";
852 std::cout << "[ ] Early termination of test because vendor service "
853 "cannot "
854 "execute model that it does not support."
855 << std::endl;
856 GTEST_SKIP();
857 }
858 }
859 }
860 }
861}
862
863void Execute(const sp<IDevice>& device, const TestModel& testModel, TestKind testKind) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100864 Model model = createModel(testModel);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100865 if (testKind == TestKind::DYNAMIC_SHAPE) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100866 makeOutputDimensionsUnspecified(&model);
867 }
868
869 sp<IPreparedModel> preparedModel;
Lev Proleev9226c1e2019-10-03 14:43:18 +0100870 switch (testKind) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800871 case TestKind::GENERAL:
872 case TestKind::DYNAMIC_SHAPE:
Miao Wang33173a32020-01-23 13:53:21 -0800873 case TestKind::MEMORY_DOMAIN:
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000874 case TestKind::FENCED_COMPUTE:
875 case TestKind::INTINITE_LOOP_TIMEOUT: {
Lev Proleev9226c1e2019-10-03 14:43:18 +0100876 createPreparedModel(device, model, &preparedModel);
877 if (preparedModel == nullptr) return;
Xusong Wang1f50e542020-01-13 11:44:45 -0800878 EvaluatePreparedModel(device, preparedModel, testModel, testKind);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100879 } break;
880 case TestKind::QUANTIZATION_COUPLING: {
Lev Proleev6a1d5e42020-01-02 18:22:30 +0000881 ASSERT_TRUE(testModel.hasQuant8CoupledOperands());
Michael Butler616701d2020-01-07 14:52:44 -0800882 createPreparedModel(device, model, &preparedModel,
883 /*reportSkipping*/ false);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100884 TestModel signedQuantizedModel = convertQuant8AsymmOperandsToSigned(testModel);
885 sp<IPreparedModel> preparedCoupledModel;
886 createPreparedModel(device, createModel(signedQuantizedModel), &preparedCoupledModel,
887 /*reportSkipping*/ false);
888 // If we couldn't prepare a model with unsigned quantization, we must
889 // fail to prepare a model with signed quantization as well.
890 if (preparedModel == nullptr) {
891 ASSERT_EQ(preparedCoupledModel, nullptr);
892 // If we failed to prepare both of the models, we can safely skip
893 // the test.
894 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
895 "prepare model that it does not support.";
896 std::cout
897 << "[ ] Early termination of test because vendor service cannot "
898 "prepare model that it does not support."
899 << std::endl;
900 GTEST_SKIP();
901 }
902 ASSERT_NE(preparedCoupledModel, nullptr);
Xusong Wang1f50e542020-01-13 11:44:45 -0800903 EvaluatePreparedCoupledModels(device, preparedModel, testModel, preparedCoupledModel,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100904 signedQuantizedModel);
905 } break;
906 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100907}
908
909void GeneratedTestBase::SetUp() {
910 testing::TestWithParam<GeneratedTestParam>::SetUp();
911 ASSERT_NE(kDevice, nullptr);
912}
913
914std::vector<NamedModel> getNamedModels(const FilterFn& filter) {
915 return TestModelManager::get().getTestModels(filter);
916}
917
918std::string printGeneratedTest(const testing::TestParamInfo<GeneratedTestParam>& info) {
919 const auto& [namedDevice, namedModel] = info.param;
920 return gtestCompliantName(getName(namedDevice) + "_" + getName(namedModel));
921}
922
923// Tag for the generated tests
924class GeneratedTest : public GeneratedTestBase {};
925
926// Tag for the dynamic output shape tests
927class DynamicOutputShapeTest : public GeneratedTest {};
928
Xusong Wang1f50e542020-01-13 11:44:45 -0800929// Tag for the memory domain tests
930class MemoryDomainTest : public GeneratedTest {};
931
Miao Wang33173a32020-01-23 13:53:21 -0800932// Tag for the fenced compute tests
933class FencedComputeTest : public GeneratedTest {};
934
Lev Proleev9226c1e2019-10-03 14:43:18 +0100935// Tag for the dynamic output shape tests
Lev Proleevbaac15d2020-01-09 16:37:28 +0000936class QuantizationCouplingTest : public GeneratedTest {};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100937
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000938// Tag for the loop timeout tests
939class InfiniteLoopTimeoutTest : public GeneratedTest {};
940
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100941TEST_P(GeneratedTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000942 Execute(kDevice, kTestModel, TestKind::GENERAL);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100943}
944
945TEST_P(DynamicOutputShapeTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000946 Execute(kDevice, kTestModel, TestKind::DYNAMIC_SHAPE);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100947}
948
Xusong Wang1f50e542020-01-13 11:44:45 -0800949TEST_P(MemoryDomainTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000950 Execute(kDevice, kTestModel, TestKind::MEMORY_DOMAIN);
Xusong Wang1f50e542020-01-13 11:44:45 -0800951}
952
Miao Wang33173a32020-01-23 13:53:21 -0800953TEST_P(FencedComputeTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000954 Execute(kDevice, kTestModel, TestKind::FENCED_COMPUTE);
Miao Wang33173a32020-01-23 13:53:21 -0800955}
956
Lev Proleevbaac15d2020-01-09 16:37:28 +0000957TEST_P(QuantizationCouplingTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000958 Execute(kDevice, kTestModel, TestKind::QUANTIZATION_COUPLING);
959}
960
961TEST_P(InfiniteLoopTimeoutTest, Test) {
962 Execute(kDevice, kTestModel, TestKind::INTINITE_LOOP_TIMEOUT);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100963}
964
965INSTANTIATE_GENERATED_TEST(GeneratedTest,
966 [](const TestModel& testModel) { return !testModel.expectFailure; });
967
Lev Proleev8b3f2402020-01-20 18:54:46 +0000968INSTANTIATE_GENERATED_TEST(DynamicOutputShapeTest, [](const TestModel& testModel) {
969 return !testModel.expectFailure && !testModel.hasScalarOutputs();
970});
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100971
Xusong Wang1f50e542020-01-13 11:44:45 -0800972INSTANTIATE_GENERATED_TEST(MemoryDomainTest,
973 [](const TestModel& testModel) { return !testModel.expectFailure; });
974
Xusong Wang5882d6d2020-03-13 10:02:36 -0700975INSTANTIATE_GENERATED_TEST(FencedComputeTest,
976 [](const TestModel& testModel) { return !testModel.expectFailure; });
Miao Wang33173a32020-01-23 13:53:21 -0800977
Lev Proleevbaac15d2020-01-09 16:37:28 +0000978INSTANTIATE_GENERATED_TEST(QuantizationCouplingTest, [](const TestModel& testModel) {
Xusong Wang962fdb22020-03-16 17:13:41 -0700979 return !testModel.expectFailure && testModel.hasQuant8CoupledOperands() &&
980 testModel.main.operations.size() == 1;
Lev Proleev9226c1e2019-10-03 14:43:18 +0100981});
982
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000983INSTANTIATE_GENERATED_TEST(InfiniteLoopTimeoutTest, [](const TestModel& testModel) {
984 return testModel.isInfiniteLoopTimeoutTest();
985});
986
Lev Proleev26d1bc82019-08-30 11:57:18 +0100987} // namespace android::hardware::neuralnetworks::V1_3::vts::functional