blob: 45e0f827c06774edc70fc22b2ef3f297c86b06e7 [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
Miao Wang2c4e0232019-12-26 18:03:56 -080075enum class Executor { ASYNC, SYNC, BURST, FENCED };
Lev Proleev0d4ba3f2019-10-02 17:32:06 +010076
Slava Shklyaevcc873ae2020-02-18 16:11:32 +000077enum class OutputType { FULLY_SPECIFIED, UNSPECIFIED, INSUFFICIENT, MISSED_DEADLINE };
Lev Proleev13fdfcd2019-08-30 11:35:34 +010078
Xusong Wang1f50e542020-01-13 11:44:45 -080079enum class IOType { INPUT, OUTPUT };
80
Miao Wang3fd70b02020-01-21 13:15:09 -080081static void waitForSyncFence(int syncFd) {
82 constexpr int kInfiniteTimeout = -1;
83 ASSERT_GT(syncFd, 0);
84 int r = sync_wait(syncFd, kInfiniteTimeout);
85 ASSERT_GE(r, 0);
86}
87
Lev Proleev0d4ba3f2019-10-02 17:32:06 +010088struct TestConfig {
89 Executor executor;
90 MeasureTiming measureTiming;
91 OutputType outputType;
Xusong Wang1f50e542020-01-13 11:44:45 -080092 MemoryType memoryType;
Lev Proleev9226c1e2019-10-03 14:43:18 +010093 // `reportSkipping` indicates if a test should print an info message in case
94 // it is skipped. The field is set to true by default and is set to false in
95 // quantization coupling tests to suppress skipping a test
96 bool reportSkipping;
Xusong Wang1f50e542020-01-13 11:44:45 -080097 TestConfig(Executor executor, MeasureTiming measureTiming, OutputType outputType,
98 MemoryType memoryType)
Lev Proleev9226c1e2019-10-03 14:43:18 +010099 : executor(executor),
100 measureTiming(measureTiming),
101 outputType(outputType),
Xusong Wang1f50e542020-01-13 11:44:45 -0800102 memoryType(memoryType),
Lev Proleev9226c1e2019-10-03 14:43:18 +0100103 reportSkipping(true) {}
104 TestConfig(Executor executor, MeasureTiming measureTiming, OutputType outputType,
Xusong Wang1f50e542020-01-13 11:44:45 -0800105 MemoryType memoryType, bool reportSkipping)
Lev Proleev9226c1e2019-10-03 14:43:18 +0100106 : executor(executor),
107 measureTiming(measureTiming),
108 outputType(outputType),
Xusong Wang1f50e542020-01-13 11:44:45 -0800109 memoryType(memoryType),
Lev Proleev9226c1e2019-10-03 14:43:18 +0100110 reportSkipping(reportSkipping) {}
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100111};
112
Xusong Wang1f50e542020-01-13 11:44:45 -0800113class DeviceMemoryAllocator {
114 public:
115 DeviceMemoryAllocator(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel,
116 const TestModel& testModel)
117 : kDevice(device), kPreparedModel(preparedModel), kTestModel(testModel) {}
118
119 // Allocate device memory for a target input/output operand.
120 // Return {IBuffer object, token} if successful.
121 // Return {nullptr, 0} if device memory is not supported.
122 template <IOType ioType>
Michael Butler6a4172c2020-02-04 16:15:04 -0800123 std::pair<sp<IBuffer>, uint32_t> allocate(uint32_t index) {
124 std::pair<sp<IBuffer>, uint32_t> buffer;
Xusong Wang1f50e542020-01-13 11:44:45 -0800125 allocateInternal<ioType>(index, &buffer);
126 return buffer;
127 }
128
129 private:
130 template <IOType ioType>
Michael Butler6a4172c2020-02-04 16:15:04 -0800131 void allocateInternal(uint32_t index, std::pair<sp<IBuffer>, uint32_t>* result) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800132 ASSERT_NE(result, nullptr);
133
134 // Prepare arguments.
135 BufferRole role = {.modelIndex = 0, .ioIndex = index, .frequency = 1.0f};
136 hidl_vec<BufferRole> inputRoles, outputRoles;
137 if constexpr (ioType == IOType::INPUT) {
138 inputRoles = {role};
139 } else {
140 outputRoles = {role};
141 }
142
143 // Allocate device memory.
144 ErrorStatus status;
145 sp<IBuffer> buffer;
Michael Butler6a4172c2020-02-04 16:15:04 -0800146 uint32_t token;
147 auto cb = [&status, &buffer, &token](ErrorStatus error, const sp<IBuffer>& buf,
148 uint32_t tok) {
149 status = error;
150 buffer = buf;
151 token = tok;
152 };
153 const auto ret = kDevice->allocate({}, {kPreparedModel}, inputRoles, outputRoles, cb);
Xusong Wang1f50e542020-01-13 11:44:45 -0800154
155 // Check allocation results.
156 ASSERT_TRUE(ret.isOk());
157 if (status == ErrorStatus::NONE) {
158 ASSERT_NE(buffer, nullptr);
159 ASSERT_GT(token, 0);
160 } else {
161 ASSERT_EQ(status, ErrorStatus::GENERAL_FAILURE);
162 ASSERT_EQ(buffer, nullptr);
163 ASSERT_EQ(token, 0);
164 }
165
166 // Initialize input data from TestBuffer.
167 if constexpr (ioType == IOType::INPUT) {
168 if (buffer != nullptr) {
169 // TestBuffer -> Shared memory.
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000170 const auto& testBuffer =
171 kTestModel.main.operands[kTestModel.main.inputIndexes[index]].data;
Xusong Wang1f50e542020-01-13 11:44:45 -0800172 ASSERT_GT(testBuffer.size(), 0);
173 hidl_memory tmp = nn::allocateSharedMemory(testBuffer.size());
174 sp<IMemory> inputMemory = mapMemory(tmp);
175 ASSERT_NE(inputMemory.get(), nullptr);
176 uint8_t* inputPtr =
177 static_cast<uint8_t*>(static_cast<void*>(inputMemory->getPointer()));
178 ASSERT_NE(inputPtr, nullptr);
179 const uint8_t* begin = testBuffer.get<uint8_t>();
180 const uint8_t* end = begin + testBuffer.size();
181 std::copy(begin, end, inputPtr);
182
183 // Shared memory -> IBuffer.
184 auto ret = buffer->copyFrom(tmp, {});
185 ASSERT_TRUE(ret.isOk());
186 ASSERT_EQ(static_cast<ErrorStatus>(ret), ErrorStatus::NONE);
187 }
188 }
189 *result = {std::move(buffer), token};
190 }
191
192 const sp<IDevice> kDevice;
193 const sp<IPreparedModel> kPreparedModel;
194 const TestModel& kTestModel;
195};
196
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000197Subgraph createSubgraph(const TestSubgraph& testSubgraph, uint32_t* constCopySize,
198 std::vector<const TestBuffer*>* constCopies, uint32_t* constRefSize,
199 std::vector<const TestBuffer*>* constReferences) {
200 CHECK(constCopySize != nullptr);
201 CHECK(constCopies != nullptr);
202 CHECK(constRefSize != nullptr);
203 CHECK(constReferences != nullptr);
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100204
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000205 // Operands.
206 hidl_vec<Operand> operands(testSubgraph.operands.size());
207 for (uint32_t i = 0; i < testSubgraph.operands.size(); i++) {
208 const auto& op = testSubgraph.operands[i];
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100209
210 DataLocation loc = {};
211 if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) {
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000212 loc = {
213 .poolIndex = 0,
214 .offset = *constCopySize,
215 .length = static_cast<uint32_t>(op.data.size()),
216 };
217 constCopies->push_back(&op.data);
218 *constCopySize += op.data.alignedSize();
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100219 } else if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) {
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000220 loc = {
221 .poolIndex = 0,
222 .offset = *constRefSize,
223 .length = static_cast<uint32_t>(op.data.size()),
224 };
225 constReferences->push_back(&op.data);
226 *constRefSize += op.data.alignedSize();
227 } else if (op.lifetime == TestOperandLifeTime::SUBGRAPH) {
228 loc = {
229 .poolIndex = 0,
230 .offset = *op.data.get<uint32_t>(),
231 .length = 0,
232 };
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100233 }
234
Michael Butler6a4172c2020-02-04 16:15:04 -0800235 V1_2::Operand::ExtraParams extraParams;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100236 if (op.type == TestOperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) {
237 extraParams.channelQuant(SymmPerChannelQuantParams{
238 .scales = op.channelQuant.scales, .channelDim = op.channelQuant.channelDim});
239 }
240
241 operands[i] = {.type = static_cast<OperandType>(op.type),
242 .dimensions = op.dimensions,
243 .numberOfConsumers = op.numberOfConsumers,
244 .scale = op.scale,
245 .zeroPoint = op.zeroPoint,
246 .lifetime = static_cast<OperandLifeTime>(op.lifetime),
247 .location = loc,
248 .extraParams = std::move(extraParams)};
249 }
250
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000251 // Operations.
252 hidl_vec<Operation> operations(testSubgraph.operations.size());
253 std::transform(testSubgraph.operations.begin(), testSubgraph.operations.end(),
254 operations.begin(), [](const TestOperation& op) -> Operation {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100255 return {.type = static_cast<OperationType>(op.type),
256 .inputs = op.inputs,
257 .outputs = op.outputs};
258 });
259
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000260 return {.operands = std::move(operands),
261 .operations = std::move(operations),
262 .inputIndexes = testSubgraph.inputIndexes,
263 .outputIndexes = testSubgraph.outputIndexes};
264}
265
266void copyTestBuffers(const std::vector<const TestBuffer*>& buffers, uint8_t* output) {
267 uint32_t offset = 0;
268 for (const TestBuffer* buffer : buffers) {
269 const uint8_t* begin = buffer->get<uint8_t>();
270 const uint8_t* end = begin + buffer->size();
271 std::copy(begin, end, output + offset);
272 offset += buffer->alignedSize();
273 }
274}
275
276} // namespace
277
278Model createModel(const TestModel& testModel) {
279 uint32_t constCopySize = 0;
280 uint32_t constRefSize = 0;
281 std::vector<const TestBuffer*> constCopies;
282 std::vector<const TestBuffer*> constReferences;
283
284 Subgraph mainSubgraph = createSubgraph(testModel.main, &constCopySize, &constCopies,
285 &constRefSize, &constReferences);
286 hidl_vec<Subgraph> refSubgraphs(testModel.referenced.size());
287 std::transform(testModel.referenced.begin(), testModel.referenced.end(), refSubgraphs.begin(),
288 [&constCopySize, &constCopies, &constRefSize,
289 &constReferences](const TestSubgraph& testSubgraph) {
290 return createSubgraph(testSubgraph, &constCopySize, &constCopies,
291 &constRefSize, &constReferences);
292 });
293
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100294 // Constant copies.
295 hidl_vec<uint8_t> operandValues(constCopySize);
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000296 copyTestBuffers(constCopies, operandValues.data());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100297
298 // Shared memory.
299 hidl_vec<hidl_memory> pools = {};
300 if (constRefSize > 0) {
301 hidl_vec_push_back(&pools, nn::allocateSharedMemory(constRefSize));
302 CHECK_NE(pools[0].size(), 0u);
303
304 // load data
305 sp<IMemory> mappedMemory = mapMemory(pools[0]);
306 CHECK(mappedMemory.get() != nullptr);
307 uint8_t* mappedPtr =
308 reinterpret_cast<uint8_t*>(static_cast<void*>(mappedMemory->getPointer()));
309 CHECK(mappedPtr != nullptr);
310
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000311 copyTestBuffers(constReferences, mappedPtr);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100312 }
313
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000314 return {.main = std::move(mainSubgraph),
315 .referenced = std::move(refSubgraphs),
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100316 .operandValues = std::move(operandValues),
317 .pools = std::move(pools),
318 .relaxComputationFloat32toFloat16 = testModel.isRelaxed};
319}
320
321static bool isOutputSizeGreaterThanOne(const TestModel& testModel, uint32_t index) {
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000322 const auto byteSize = testModel.main.operands[testModel.main.outputIndexes[index]].data.size();
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100323 return byteSize > 1u;
324}
325
Xusong Wang1f50e542020-01-13 11:44:45 -0800326static void makeOutputInsufficientSize(uint32_t outputIndex, Request* request) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100327 auto& length = request->outputs[outputIndex].location.length;
328 ASSERT_GT(length, 1u);
329 length -= 1u;
330}
331
332static void makeOutputDimensionsUnspecified(Model* model) {
Slava Shklyaeva785a3f2019-12-13 12:24:35 +0000333 for (auto i : model->main.outputIndexes) {
334 auto& dims = model->main.operands[i].dimensions;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100335 std::fill(dims.begin(), dims.end(), 0);
336 }
337}
338
Xusong Wang41adc5b2020-02-25 11:43:10 -0800339class ExecutionContextV1_3 {
340 public:
341 ExecutionContextV1_3(sp<IDevice> device, sp<IPreparedModel> preparedModel)
342 : kDevice(std::move(device)), kPreparedModel(std::move(preparedModel)) {}
Xusong Wang1f50e542020-01-13 11:44:45 -0800343
Xusong Wang41adc5b2020-02-25 11:43:10 -0800344 std::optional<Request> createRequest(const TestModel& testModel, MemoryType memoryType);
345 std::vector<TestBuffer> getOutputBuffers(const TestModel& testModel,
346 const Request& request) const;
347
348 private:
349 // Get a TestBuffer with data copied from an IBuffer object.
350 void getBuffer(const sp<IBuffer>& buffer, size_t size, TestBuffer* testBuffer) const;
351
352 static constexpr uint32_t kInputPoolIndex = 0;
353 static constexpr uint32_t kOutputPoolIndex = 1;
354 static constexpr uint32_t kDeviceMemoryBeginIndex = 2;
355
356 const sp<IDevice> kDevice;
357 const sp<IPreparedModel> kPreparedModel;
358 std::unique_ptr<TestMemoryBase> mInputMemory, mOutputMemory;
359 std::vector<sp<IBuffer>> mBuffers;
360};
361
362std::optional<Request> ExecutionContextV1_3::createRequest(const TestModel& testModel,
363 MemoryType memoryType) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800364 // Memory pools are organized as:
365 // - 0: Input shared memory pool
366 // - 1: Output shared memory pool
367 // - [2, 2+i): Input device memories
368 // - [2+i, 2+i+o): Output device memories
Xusong Wang41adc5b2020-02-25 11:43:10 -0800369 DeviceMemoryAllocator allocator(kDevice, kPreparedModel, testModel);
Michael Butler6a4172c2020-02-04 16:15:04 -0800370 std::vector<uint32_t> tokens;
Xusong Wang41adc5b2020-02-25 11:43:10 -0800371 mBuffers.clear();
Xusong Wang1f50e542020-01-13 11:44:45 -0800372
373 // Model inputs.
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000374 hidl_vec<RequestArgument> inputs(testModel.main.inputIndexes.size());
Xusong Wang1f50e542020-01-13 11:44:45 -0800375 size_t inputSize = 0;
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000376 for (uint32_t i = 0; i < testModel.main.inputIndexes.size(); i++) {
377 const auto& op = testModel.main.operands[testModel.main.inputIndexes[i]];
Xusong Wang1f50e542020-01-13 11:44:45 -0800378 if (op.data.size() == 0) {
379 // Omitted input.
380 inputs[i] = {.hasNoValue = true};
381 continue;
Xusong Wang41adc5b2020-02-25 11:43:10 -0800382 } else if (memoryType == MemoryType::DEVICE) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800383 SCOPED_TRACE("Input index = " + std::to_string(i));
384 auto [buffer, token] = allocator.allocate<IOType::INPUT>(i);
385 if (buffer != nullptr) {
Xusong Wang41adc5b2020-02-25 11:43:10 -0800386 DataLocation loc = {.poolIndex = static_cast<uint32_t>(mBuffers.size() +
Xusong Wang1f50e542020-01-13 11:44:45 -0800387 kDeviceMemoryBeginIndex)};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800388 mBuffers.push_back(std::move(buffer));
Xusong Wang1f50e542020-01-13 11:44:45 -0800389 tokens.push_back(token);
390 inputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}};
391 continue;
392 }
393 }
394
395 // Reserve shared memory for input.
396 DataLocation loc = {.poolIndex = kInputPoolIndex,
397 .offset = static_cast<uint32_t>(inputSize),
398 .length = static_cast<uint32_t>(op.data.size())};
399 inputSize += op.data.alignedSize();
400 inputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}};
401 }
402
403 // Model outputs.
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000404 hidl_vec<RequestArgument> outputs(testModel.main.outputIndexes.size());
Xusong Wang1f50e542020-01-13 11:44:45 -0800405 size_t outputSize = 0;
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000406 for (uint32_t i = 0; i < testModel.main.outputIndexes.size(); i++) {
407 const auto& op = testModel.main.operands[testModel.main.outputIndexes[i]];
Xusong Wang41adc5b2020-02-25 11:43:10 -0800408 if (memoryType == MemoryType::DEVICE) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800409 SCOPED_TRACE("Output index = " + std::to_string(i));
410 auto [buffer, token] = allocator.allocate<IOType::OUTPUT>(i);
411 if (buffer != nullptr) {
Xusong Wang41adc5b2020-02-25 11:43:10 -0800412 DataLocation loc = {.poolIndex = static_cast<uint32_t>(mBuffers.size() +
Xusong Wang1f50e542020-01-13 11:44:45 -0800413 kDeviceMemoryBeginIndex)};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800414 mBuffers.push_back(std::move(buffer));
Xusong Wang1f50e542020-01-13 11:44:45 -0800415 tokens.push_back(token);
416 outputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}};
417 continue;
418 }
419 }
420
421 // In the case of zero-sized output, we should at least provide a one-byte buffer.
422 // This is because zero-sized tensors are only supported internally to the driver, or
423 // reported in output shapes. It is illegal for the client to pre-specify a zero-sized
424 // tensor as model output. Otherwise, we will have two semantic conflicts:
425 // - "Zero dimension" conflicts with "unspecified dimension".
426 // - "Omitted operand buffer" conflicts with "zero-sized operand buffer".
427 size_t bufferSize = std::max<size_t>(op.data.size(), 1);
428
429 // Reserve shared memory for output.
430 DataLocation loc = {.poolIndex = kOutputPoolIndex,
431 .offset = static_cast<uint32_t>(outputSize),
432 .length = static_cast<uint32_t>(bufferSize)};
433 outputSize += op.data.size() == 0 ? TestBuffer::kAlignment : op.data.alignedSize();
434 outputs[i] = {.hasNoValue = false, .location = loc, .dimensions = {}};
435 }
436
Xusong Wang41adc5b2020-02-25 11:43:10 -0800437 if (memoryType == MemoryType::DEVICE && mBuffers.empty()) {
438 return std::nullopt;
439 }
440
Xusong Wang1f50e542020-01-13 11:44:45 -0800441 // Memory pools.
Xusong Wang41adc5b2020-02-25 11:43:10 -0800442 hidl_vec<Request::MemoryPool> pools(kDeviceMemoryBeginIndex + mBuffers.size());
443 if (memoryType == MemoryType::BLOB_AHWB) {
444 mInputMemory = TestBlobAHWB::create(std::max<size_t>(inputSize, 1));
445 mOutputMemory = TestBlobAHWB::create(std::max<size_t>(outputSize, 1));
446 } else {
447 mInputMemory = TestAshmem::create(std::max<size_t>(inputSize, 1));
448 mOutputMemory = TestAshmem::create(std::max<size_t>(outputSize, 1));
449 }
450 EXPECT_NE(mInputMemory, nullptr);
451 EXPECT_NE(mOutputMemory, nullptr);
452 pools[kInputPoolIndex].hidlMemory(mInputMemory->getHidlMemory());
453 pools[kOutputPoolIndex].hidlMemory(mOutputMemory->getHidlMemory());
454 for (uint32_t i = 0; i < mBuffers.size(); i++) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800455 pools[kDeviceMemoryBeginIndex + i].token(tokens[i]);
456 }
457
458 // Copy input data to the input shared memory pool.
Xusong Wang41adc5b2020-02-25 11:43:10 -0800459 uint8_t* inputPtr = mInputMemory->getPointer();
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000460 for (uint32_t i = 0; i < testModel.main.inputIndexes.size(); i++) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800461 if (!inputs[i].hasNoValue && inputs[i].location.poolIndex == kInputPoolIndex) {
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000462 const auto& op = testModel.main.operands[testModel.main.inputIndexes[i]];
Xusong Wang1f50e542020-01-13 11:44:45 -0800463 const uint8_t* begin = op.data.get<uint8_t>();
464 const uint8_t* end = begin + op.data.size();
465 std::copy(begin, end, inputPtr + inputs[i].location.offset);
466 }
467 }
Xusong Wang41adc5b2020-02-25 11:43:10 -0800468 return Request{
Xusong Wang1f50e542020-01-13 11:44:45 -0800469 .inputs = std::move(inputs), .outputs = std::move(outputs), .pools = std::move(pools)};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800470}
471
472std::vector<TestBuffer> ExecutionContextV1_3::getOutputBuffers(const TestModel& testModel,
473 const Request& request) const {
474 // Copy out output results.
475 uint8_t* outputPtr = mOutputMemory->getPointer();
476 std::vector<TestBuffer> outputBuffers;
477 for (uint32_t i = 0; i < request.outputs.size(); i++) {
478 const auto& outputLoc = request.outputs[i].location;
479 if (outputLoc.poolIndex == kOutputPoolIndex) {
480 outputBuffers.emplace_back(outputLoc.length, outputPtr + outputLoc.offset);
481 } else {
482 const auto& op = testModel.main.operands[testModel.main.outputIndexes[i]];
483 if (op.data.size() == 0) {
484 outputBuffers.emplace_back(0, nullptr);
485 } else {
486 SCOPED_TRACE("Output index = " + std::to_string(i));
487 const uint32_t bufferIndex = outputLoc.poolIndex - kDeviceMemoryBeginIndex;
488 TestBuffer buffer;
489 getBuffer(mBuffers[bufferIndex], op.data.size(), &buffer);
490 outputBuffers.push_back(std::move(buffer));
491 }
492 }
493 }
494 return outputBuffers;
Xusong Wang1f50e542020-01-13 11:44:45 -0800495}
496
497// Get a TestBuffer with data copied from an IBuffer object.
Xusong Wang41adc5b2020-02-25 11:43:10 -0800498void ExecutionContextV1_3::getBuffer(const sp<IBuffer>& buffer, size_t size,
499 TestBuffer* testBuffer) const {
Xusong Wang1f50e542020-01-13 11:44:45 -0800500 // IBuffer -> Shared memory.
501 hidl_memory tmp = nn::allocateSharedMemory(size);
502 const auto ret = buffer->copyTo(tmp);
503 ASSERT_TRUE(ret.isOk());
504 ASSERT_EQ(static_cast<ErrorStatus>(ret), ErrorStatus::NONE);
505
506 // Shared memory -> TestBuffer.
507 sp<IMemory> outputMemory = mapMemory(tmp);
508 ASSERT_NE(outputMemory.get(), nullptr);
509 uint8_t* outputPtr = static_cast<uint8_t*>(static_cast<void*>(outputMemory->getPointer()));
510 ASSERT_NE(outputPtr, nullptr);
511 ASSERT_NE(testBuffer, nullptr);
512 *testBuffer = TestBuffer(size, outputPtr);
513}
514
Xusong Wang5882d6d2020-03-13 10:02:36 -0700515static bool hasZeroSizedOutput(const TestModel& testModel) {
516 return std::any_of(testModel.main.outputIndexes.begin(), testModel.main.outputIndexes.end(),
517 [&testModel](uint32_t index) {
518 return testModel.main.operands[index].data.size() == 0;
519 });
520}
521
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100522static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel,
523 const Request& request, MeasureTiming measure,
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000524 const OptionalTimeoutDuration& loopTimeoutDuration,
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100525 sp<ExecutionCallback>& callback) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000526 return preparedModel->execute_1_3(request, measure, {}, loopTimeoutDuration, callback);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100527}
528static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel,
529 const Request& request, MeasureTiming measure,
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000530 const OptionalTimeoutDuration& loopTimeoutDuration,
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100531 hidl_vec<OutputShape>* outputShapes,
532 Timing* timing) {
533 ErrorStatus result;
Xusong Wangebd88ba2019-10-28 11:11:19 -0700534 Return<void> ret = preparedModel->executeSynchronously_1_3(
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000535 request, measure, {}, loopTimeoutDuration,
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100536 [&result, outputShapes, timing](ErrorStatus error, const hidl_vec<OutputShape>& shapes,
537 const Timing& time) {
538 result = error;
539 *outputShapes = shapes;
540 *timing = time;
541 });
542 if (!ret.isOk()) {
543 return ErrorStatus::GENERAL_FAILURE;
544 }
545 return result;
546}
547static std::shared_ptr<::android::nn::ExecutionBurstController> CreateBurst(
548 const sp<IPreparedModel>& preparedModel) {
Michael Butler648ada52019-07-25 17:22:11 -0700549 return android::nn::ExecutionBurstController::create(preparedModel,
550 std::chrono::microseconds{0});
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100551}
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100552
Xusong Wang1f50e542020-01-13 11:44:45 -0800553void EvaluatePreparedModel(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel,
554 const TestModel& testModel, const TestConfig& testConfig,
555 bool* skipped = nullptr) {
Lev Proleev9226c1e2019-10-03 14:43:18 +0100556 if (skipped != nullptr) {
557 *skipped = false;
558 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100559 // If output0 does not have size larger than one byte, we can not test with insufficient buffer.
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100560 if (testConfig.outputType == OutputType::INSUFFICIENT &&
561 !isOutputSizeGreaterThanOne(testModel, 0)) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100562 return;
563 }
564
Xusong Wang41adc5b2020-02-25 11:43:10 -0800565 ExecutionContextV1_3 context(device, preparedModel);
566 auto maybeRequest = context.createRequest(testModel, testConfig.memoryType);
Xusong Wang1f50e542020-01-13 11:44:45 -0800567 // Skip if testing memory domain but no device memory has been allocated.
Xusong Wang41adc5b2020-02-25 11:43:10 -0800568 if (!maybeRequest.has_value()) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800569 return;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100570 }
Xusong Wang41adc5b2020-02-25 11:43:10 -0800571
572 Request request = std::move(maybeRequest.value());
Xusong Wang1f50e542020-01-13 11:44:45 -0800573 if (testConfig.outputType == OutputType::INSUFFICIENT) {
574 makeOutputInsufficientSize(/*outputIndex=*/0, &request);
575 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100576
Slava Shklyaevcc873ae2020-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 Shklyaevcc873ae2020-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 Shklyaevcc873ae2020-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 Wang931d5a12019-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 Wang1f50e542020-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 Wang931d5a12019-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 Wang931d5a12019-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 Wang931d5a12019-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 Wang2c4e0232019-12-26 18:03:56 -0800651 case Executor::FENCED: {
652 SCOPED_TRACE("fenced");
653 ErrorStatus result;
Miao Wang3fd70b02020-01-21 13:15:09 -0800654 hidl_handle syncFenceHandle;
655 sp<IFencedExecutionCallback> fencedCallback;
Miao Wang34dfa2f2020-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 Wang2c4e0232019-12-26 18:03:56 -0800666 ASSERT_TRUE(ret.isOk());
667 if (result != ErrorStatus::NONE) {
Miao Wang3fd70b02020-01-21 13:15:09 -0800668 ASSERT_EQ(syncFenceHandle.getNativeHandle(), nullptr);
669 ASSERT_EQ(fencedCallback, nullptr);
Miao Wangfa161c72020-02-25 16:57:53 -0800670 executionStatus = result;
Miao Wang3fd70b02020-01-21 13:15:09 -0800671 } else if (syncFenceHandle.getNativeHandle()) {
Miao Wang34dfa2f2020-02-25 15:54:08 -0800672 // If a sync fence is returned, try start another run waiting for the sync fence.
673 ret = preparedModel->executeFenced(request, {syncFenceHandle},
674 testConfig.measureTiming, {},
675 loopTimeoutDuration, {}, callbackFunc);
676 ASSERT_TRUE(ret.isOk());
677 ASSERT_EQ(result, ErrorStatus::NONE);
Miao Wang3fd70b02020-01-21 13:15:09 -0800678 waitForSyncFence(syncFenceHandle.getNativeHandle()->data[0]);
Miao Wang2c4e0232019-12-26 18:03:56 -0800679 }
680 if (result == ErrorStatus::NONE) {
Miao Wang3fd70b02020-01-21 13:15:09 -0800681 ASSERT_NE(fencedCallback, nullptr);
682 Return<void> ret = fencedCallback->getExecutionInfo(
683 [&executionStatus, &timing](ErrorStatus error, Timing t, Timing) {
Miao Wang2c4e0232019-12-26 18:03:56 -0800684 executionStatus = error;
685 timing = t;
686 });
687 ASSERT_TRUE(ret.isOk());
688 }
689 break;
690 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100691 }
692
Miao Wangfa161c72020-02-25 16:57:53 -0800693 if (testConfig.outputType != OutputType::FULLY_SPECIFIED &&
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100694 executionStatus == ErrorStatus::GENERAL_FAILURE) {
Lev Proleev9226c1e2019-10-03 14:43:18 +0100695 if (skipped != nullptr) {
696 *skipped = true;
697 }
698 if (!testConfig.reportSkipping) {
699 return;
700 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100701 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
702 "execute model that it does not support.";
703 std::cout << "[ ] Early termination of test because vendor service cannot "
704 "execute model that it does not support."
705 << std::endl;
706 GTEST_SKIP();
707 }
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100708 if (testConfig.measureTiming == MeasureTiming::NO) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100709 EXPECT_EQ(UINT64_MAX, timing.timeOnDevice);
710 EXPECT_EQ(UINT64_MAX, timing.timeInDriver);
711 } else {
712 if (timing.timeOnDevice != UINT64_MAX && timing.timeInDriver != UINT64_MAX) {
713 EXPECT_LE(timing.timeOnDevice, timing.timeInDriver);
714 }
715 }
716
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100717 switch (testConfig.outputType) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100718 case OutputType::FULLY_SPECIFIED:
Xusong Wang5882d6d2020-03-13 10:02:36 -0700719 if (testConfig.executor == Executor::FENCED && hasZeroSizedOutput(testModel)) {
720 // Executor::FENCED does not support zero-sized output.
721 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
722 return;
723 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100724 // If the model output operands are fully specified, outputShapes must be either
725 // either empty, or have the same number of elements as the number of outputs.
726 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
727 ASSERT_TRUE(outputShapes.size() == 0 ||
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000728 outputShapes.size() == testModel.main.outputIndexes.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100729 break;
730 case OutputType::UNSPECIFIED:
Miao Wangfa161c72020-02-25 16:57:53 -0800731 if (testConfig.executor == Executor::FENCED) {
732 // For Executor::FENCED, the output shape must be fully specified.
733 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
734 return;
735 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100736 // If the model output operands are not fully specified, outputShapes must have
737 // the same number of elements as the number of outputs.
738 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000739 ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100740 break;
741 case OutputType::INSUFFICIENT:
Miao Wangfa161c72020-02-25 16:57:53 -0800742 if (testConfig.executor == Executor::FENCED) {
743 // For Executor::FENCED, the output shape must be fully specified.
744 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
745 return;
746 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100747 ASSERT_EQ(ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, executionStatus);
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000748 ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size());
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100749 ASSERT_FALSE(outputShapes[0].isSufficient);
750 return;
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000751 case OutputType::MISSED_DEADLINE:
752 ASSERT_TRUE(executionStatus == ErrorStatus::MISSED_DEADLINE_TRANSIENT ||
753 executionStatus == ErrorStatus::MISSED_DEADLINE_PERSISTENT)
754 << "executionStatus = " << executionStatus;
755 return;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100756 }
757
758 // Go through all outputs, check returned output shapes.
759 for (uint32_t i = 0; i < outputShapes.size(); i++) {
760 EXPECT_TRUE(outputShapes[i].isSufficient);
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000761 const auto& expect = testModel.main.operands[testModel.main.outputIndexes[i]].dimensions;
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100762 const std::vector<uint32_t> actual = outputShapes[i].dimensions;
763 EXPECT_EQ(expect, actual);
764 }
765
766 // Retrieve execution results.
Xusong Wang41adc5b2020-02-25 11:43:10 -0800767 const std::vector<TestBuffer> outputs = context.getOutputBuffers(testModel, request);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100768
769 // We want "close-enough" results.
770 checkResults(testModel, outputs);
771}
772
Xusong Wang1f50e542020-01-13 11:44:45 -0800773void EvaluatePreparedModel(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel,
774 const TestModel& testModel, TestKind testKind) {
Lev Proleev56cda832019-12-05 14:49:47 +0000775 std::vector<OutputType> outputTypesList;
776 std::vector<MeasureTiming> measureTimingList;
777 std::vector<Executor> executorList;
Xusong Wang41adc5b2020-02-25 11:43:10 -0800778 std::vector<MemoryType> memoryTypeList;
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100779
Lev Proleev9226c1e2019-10-03 14:43:18 +0100780 switch (testKind) {
781 case TestKind::GENERAL: {
782 outputTypesList = {OutputType::FULLY_SPECIFIED};
783 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wang33173a32020-01-23 13:53:21 -0800784 executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800785 memoryTypeList = {MemoryType::ASHMEM};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100786 } break;
787 case TestKind::DYNAMIC_SHAPE: {
788 outputTypesList = {OutputType::UNSPECIFIED, OutputType::INSUFFICIENT};
789 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wangfa161c72020-02-25 16:57:53 -0800790 executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST, Executor::FENCED};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800791 memoryTypeList = {MemoryType::ASHMEM};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100792 } break;
Xusong Wang1f50e542020-01-13 11:44:45 -0800793 case TestKind::MEMORY_DOMAIN: {
794 outputTypesList = {OutputType::FULLY_SPECIFIED};
795 measureTimingList = {MeasureTiming::NO};
Miao Wang34dfa2f2020-02-25 15:54:08 -0800796 executorList = {Executor::ASYNC, Executor::SYNC, Executor::FENCED};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800797 memoryTypeList = {MemoryType::BLOB_AHWB, MemoryType::DEVICE};
Xusong Wang1f50e542020-01-13 11:44:45 -0800798 } break;
Miao Wang33173a32020-01-23 13:53:21 -0800799 case TestKind::FENCED_COMPUTE: {
800 outputTypesList = {OutputType::FULLY_SPECIFIED};
801 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
802 executorList = {Executor::FENCED};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800803 memoryTypeList = {MemoryType::ASHMEM};
Miao Wang33173a32020-01-23 13:53:21 -0800804 } break;
Lev Proleev9226c1e2019-10-03 14:43:18 +0100805 case TestKind::QUANTIZATION_COUPLING: {
806 LOG(FATAL) << "Wrong TestKind for EvaluatePreparedModel";
807 return;
808 } break;
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000809 case TestKind::INTINITE_LOOP_TIMEOUT: {
810 outputTypesList = {OutputType::MISSED_DEADLINE};
811 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
812 // Burst does not support V1_3 loop timeout.
813 executorList = {Executor::ASYNC, Executor::SYNC, Executor::FENCED};
Xusong Wang41adc5b2020-02-25 11:43:10 -0800814 memoryTypeList = {MemoryType::ASHMEM};
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000815 } break;
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100816 }
817
818 for (const OutputType outputType : outputTypesList) {
819 for (const MeasureTiming measureTiming : measureTimingList) {
820 for (const Executor executor : executorList) {
Xusong Wang41adc5b2020-02-25 11:43:10 -0800821 for (const MemoryType memoryType : memoryTypeList) {
822 const TestConfig testConfig(executor, measureTiming, outputType, memoryType);
823 EvaluatePreparedModel(device, preparedModel, testModel, testConfig);
824 }
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100825 }
826 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100827 }
828}
829
Xusong Wang1f50e542020-01-13 11:44:45 -0800830void EvaluatePreparedCoupledModels(const sp<IDevice>& device,
831 const sp<IPreparedModel>& preparedModel,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100832 const TestModel& testModel,
833 const sp<IPreparedModel>& preparedCoupledModel,
834 const TestModel& coupledModel) {
Lev Proleev56cda832019-12-05 14:49:47 +0000835 const std::vector<OutputType> outputTypesList = {OutputType::FULLY_SPECIFIED};
836 const std::vector<MeasureTiming> measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
Miao Wang2c4e0232019-12-26 18:03:56 -0800837 const std::vector<Executor> executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST,
838 Executor::FENCED};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100839
840 for (const OutputType outputType : outputTypesList) {
841 for (const MeasureTiming measureTiming : measureTimingList) {
842 for (const Executor executor : executorList) {
Xusong Wang41adc5b2020-02-25 11:43:10 -0800843 const TestConfig testConfig(executor, measureTiming, outputType, MemoryType::ASHMEM,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100844 /*reportSkipping=*/false);
845 bool baseSkipped = false;
Xusong Wang1f50e542020-01-13 11:44:45 -0800846 EvaluatePreparedModel(device, preparedModel, testModel, testConfig, &baseSkipped);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100847 bool coupledSkipped = false;
Xusong Wang1f50e542020-01-13 11:44:45 -0800848 EvaluatePreparedModel(device, preparedCoupledModel, coupledModel, testConfig,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100849 &coupledSkipped);
850 ASSERT_EQ(baseSkipped, coupledSkipped);
851 if (baseSkipped) {
852 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
853 "execute model that it does not support.";
854 std::cout << "[ ] Early termination of test because vendor service "
855 "cannot "
856 "execute model that it does not support."
857 << std::endl;
858 GTEST_SKIP();
859 }
860 }
861 }
862 }
863}
864
865void Execute(const sp<IDevice>& device, const TestModel& testModel, TestKind testKind) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100866 Model model = createModel(testModel);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100867 if (testKind == TestKind::DYNAMIC_SHAPE) {
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100868 makeOutputDimensionsUnspecified(&model);
869 }
870
871 sp<IPreparedModel> preparedModel;
Lev Proleev9226c1e2019-10-03 14:43:18 +0100872 switch (testKind) {
Xusong Wang1f50e542020-01-13 11:44:45 -0800873 case TestKind::GENERAL:
874 case TestKind::DYNAMIC_SHAPE:
Miao Wang33173a32020-01-23 13:53:21 -0800875 case TestKind::MEMORY_DOMAIN:
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000876 case TestKind::FENCED_COMPUTE:
877 case TestKind::INTINITE_LOOP_TIMEOUT: {
Lev Proleev9226c1e2019-10-03 14:43:18 +0100878 createPreparedModel(device, model, &preparedModel);
879 if (preparedModel == nullptr) return;
Xusong Wang1f50e542020-01-13 11:44:45 -0800880 EvaluatePreparedModel(device, preparedModel, testModel, testKind);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100881 } break;
882 case TestKind::QUANTIZATION_COUPLING: {
Lev Proleev6a1d5e42020-01-02 18:22:30 +0000883 ASSERT_TRUE(testModel.hasQuant8CoupledOperands());
Michael Butler616701d2020-01-07 14:52:44 -0800884 createPreparedModel(device, model, &preparedModel,
885 /*reportSkipping*/ false);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100886 TestModel signedQuantizedModel = convertQuant8AsymmOperandsToSigned(testModel);
887 sp<IPreparedModel> preparedCoupledModel;
888 createPreparedModel(device, createModel(signedQuantizedModel), &preparedCoupledModel,
889 /*reportSkipping*/ false);
890 // If we couldn't prepare a model with unsigned quantization, we must
891 // fail to prepare a model with signed quantization as well.
892 if (preparedModel == nullptr) {
893 ASSERT_EQ(preparedCoupledModel, nullptr);
894 // If we failed to prepare both of the models, we can safely skip
895 // the test.
896 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
897 "prepare model that it does not support.";
898 std::cout
899 << "[ ] Early termination of test because vendor service cannot "
900 "prepare model that it does not support."
901 << std::endl;
902 GTEST_SKIP();
903 }
904 ASSERT_NE(preparedCoupledModel, nullptr);
Xusong Wang1f50e542020-01-13 11:44:45 -0800905 EvaluatePreparedCoupledModels(device, preparedModel, testModel, preparedCoupledModel,
Lev Proleev9226c1e2019-10-03 14:43:18 +0100906 signedQuantizedModel);
907 } break;
908 }
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100909}
910
911void GeneratedTestBase::SetUp() {
912 testing::TestWithParam<GeneratedTestParam>::SetUp();
913 ASSERT_NE(kDevice, nullptr);
914}
915
916std::vector<NamedModel> getNamedModels(const FilterFn& filter) {
917 return TestModelManager::get().getTestModels(filter);
918}
919
920std::string printGeneratedTest(const testing::TestParamInfo<GeneratedTestParam>& info) {
921 const auto& [namedDevice, namedModel] = info.param;
922 return gtestCompliantName(getName(namedDevice) + "_" + getName(namedModel));
923}
924
925// Tag for the generated tests
926class GeneratedTest : public GeneratedTestBase {};
927
928// Tag for the dynamic output shape tests
929class DynamicOutputShapeTest : public GeneratedTest {};
930
Xusong Wang1f50e542020-01-13 11:44:45 -0800931// Tag for the memory domain tests
932class MemoryDomainTest : public GeneratedTest {};
933
Miao Wang33173a32020-01-23 13:53:21 -0800934// Tag for the fenced compute tests
935class FencedComputeTest : public GeneratedTest {};
936
Lev Proleev9226c1e2019-10-03 14:43:18 +0100937// Tag for the dynamic output shape tests
Lev Proleevbaac15d2020-01-09 16:37:28 +0000938class QuantizationCouplingTest : public GeneratedTest {};
Lev Proleev9226c1e2019-10-03 14:43:18 +0100939
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000940// Tag for the loop timeout tests
941class InfiniteLoopTimeoutTest : public GeneratedTest {};
942
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100943TEST_P(GeneratedTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000944 Execute(kDevice, kTestModel, TestKind::GENERAL);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100945}
946
947TEST_P(DynamicOutputShapeTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000948 Execute(kDevice, kTestModel, TestKind::DYNAMIC_SHAPE);
Lev Proleev9226c1e2019-10-03 14:43:18 +0100949}
950
Xusong Wang1f50e542020-01-13 11:44:45 -0800951TEST_P(MemoryDomainTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000952 Execute(kDevice, kTestModel, TestKind::MEMORY_DOMAIN);
Xusong Wang1f50e542020-01-13 11:44:45 -0800953}
954
Miao Wang33173a32020-01-23 13:53:21 -0800955TEST_P(FencedComputeTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000956 Execute(kDevice, kTestModel, TestKind::FENCED_COMPUTE);
Miao Wang33173a32020-01-23 13:53:21 -0800957}
958
Lev Proleevbaac15d2020-01-09 16:37:28 +0000959TEST_P(QuantizationCouplingTest, Test) {
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000960 Execute(kDevice, kTestModel, TestKind::QUANTIZATION_COUPLING);
961}
962
963TEST_P(InfiniteLoopTimeoutTest, Test) {
964 Execute(kDevice, kTestModel, TestKind::INTINITE_LOOP_TIMEOUT);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100965}
966
967INSTANTIATE_GENERATED_TEST(GeneratedTest,
968 [](const TestModel& testModel) { return !testModel.expectFailure; });
969
Lev Proleev8b3f2402020-01-20 18:54:46 +0000970INSTANTIATE_GENERATED_TEST(DynamicOutputShapeTest, [](const TestModel& testModel) {
971 return !testModel.expectFailure && !testModel.hasScalarOutputs();
972});
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100973
Xusong Wang1f50e542020-01-13 11:44:45 -0800974INSTANTIATE_GENERATED_TEST(MemoryDomainTest,
975 [](const TestModel& testModel) { return !testModel.expectFailure; });
976
Xusong Wang5882d6d2020-03-13 10:02:36 -0700977INSTANTIATE_GENERATED_TEST(FencedComputeTest,
978 [](const TestModel& testModel) { return !testModel.expectFailure; });
Miao Wang33173a32020-01-23 13:53:21 -0800979
Lev Proleevbaac15d2020-01-09 16:37:28 +0000980INSTANTIATE_GENERATED_TEST(QuantizationCouplingTest, [](const TestModel& testModel) {
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000981 return testModel.hasQuant8CoupledOperands() && testModel.main.operations.size() == 1;
Lev Proleev9226c1e2019-10-03 14:43:18 +0100982});
983
Slava Shklyaevcc873ae2020-02-18 16:11:32 +0000984INSTANTIATE_GENERATED_TEST(InfiniteLoopTimeoutTest, [](const TestModel& testModel) {
985 return testModel.isInfiniteLoopTimeoutTest();
986});
987
Lev Proleev26d1bc82019-08-30 11:57:18 +0100988} // namespace android::hardware::neuralnetworks::V1_3::vts::functional