1 /*
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>
30 #include <android/hardware/neuralnetworks/1.2/types.h>
31 #include <android/hardware/neuralnetworks/1.3/IDevice.h>
32 #include <android/hardware/neuralnetworks/1.3/IFencedExecutionCallback.h>
33 #include <android/hardware/neuralnetworks/1.3/IPreparedModel.h>
34 #include <android/hardware/neuralnetworks/1.3/IPreparedModelCallback.h>
35 #include <android/hardware/neuralnetworks/1.3/types.h>
36 #include <android/hidl/allocator/1.0/IAllocator.h>
37 #include <android/hidl/memory/1.0/IMemory.h>
38 #include <android/sync.h>
39 #include <gtest/gtest.h>
40 #include <hidlmemory/mapping.h>
41
42 #include <algorithm>
43 #include <chrono>
44 #include <iostream>
45 #include <numeric>
46 #include <vector>
47
48 #include "1.0/Utils.h"
49 #include "1.3/Callbacks.h"
50 #include "1.3/Utils.h"
51 #include "ExecutionBurstController.h"
52 #include "MemoryUtils.h"
53 #include "TestHarness.h"
54 #include "Utils.h"
55 #include "VtsHalNeuralnetworks.h"
56
57 namespace android::hardware::neuralnetworks::V1_3::vts::functional {
58
59 using namespace test_helper;
60 using hidl::memory::V1_0::IMemory;
61 using implementation::ExecutionCallback;
62 using implementation::PreparedModelCallback;
63 using V1_0::DataLocation;
64 using V1_0::RequestArgument;
65 using V1_1::ExecutionPreference;
66 using V1_2::Constant;
67 using V1_2::MeasureTiming;
68 using V1_2::OutputShape;
69 using V1_2::SymmPerChannelQuantParams;
70 using V1_2::Timing;
71 using HidlToken = hidl_array<uint8_t, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
72
73 namespace {
74
75 enum class OutputType { FULLY_SPECIFIED, UNSPECIFIED, INSUFFICIENT, MISSED_DEADLINE };
76
77 enum class IOType { INPUT, OUTPUT };
78
79 struct TestConfig {
80 Executor executor;
81 MeasureTiming measureTiming;
82 OutputType outputType;
83 MemoryType memoryType;
84 // `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;
TestConfigandroid::hardware::neuralnetworks::V1_3::vts::functional::__anon404ff7050111::TestConfig88 TestConfig(Executor executor, MeasureTiming measureTiming, OutputType outputType,
89 MemoryType memoryType)
90 : executor(executor),
91 measureTiming(measureTiming),
92 outputType(outputType),
93 memoryType(memoryType),
94 reportSkipping(true) {}
TestConfigandroid::hardware::neuralnetworks::V1_3::vts::functional::__anon404ff7050111::TestConfig95 TestConfig(Executor executor, MeasureTiming measureTiming, OutputType outputType,
96 MemoryType memoryType, bool reportSkipping)
97 : executor(executor),
98 measureTiming(measureTiming),
99 outputType(outputType),
100 memoryType(memoryType),
101 reportSkipping(reportSkipping) {}
102 };
103
104 class DeviceMemoryAllocator {
105 public:
DeviceMemoryAllocator(const sp<IDevice> & device,const sp<IPreparedModel> & preparedModel,const TestModel & testModel)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>
allocate(uint32_t index)114 std::pair<sp<IBuffer>, uint32_t> allocate(uint32_t index) {
115 std::pair<sp<IBuffer>, uint32_t> buffer;
116 allocateInternal<ioType>(index, &buffer);
117 return buffer;
118 }
119
120 private:
121 template <IOType ioType>
allocateInternal(uint32_t index,std::pair<sp<IBuffer>,uint32_t> * result)122 void allocateInternal(uint32_t index, std::pair<sp<IBuffer>, uint32_t>* result) {
123 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;
137 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);
145
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.
161 const auto& testBuffer =
162 kTestModel.main.operands[kTestModel.main.inputIndexes[index]].data;
163 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
createSubgraph(const TestSubgraph & testSubgraph,uint32_t * constCopySize,std::vector<const TestBuffer * > * constCopies,uint32_t * constRefSize,std::vector<const TestBuffer * > * constReferences)188 Subgraph 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);
195
196 // 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];
200
201 DataLocation loc = {};
202 if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) {
203 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();
210 } else if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) {
211 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 };
224 }
225
226 V1_2::Operand::ExtraParams extraParams;
227 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
242 // 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 {
246 return {.type = static_cast<OperationType>(op.type),
247 .inputs = op.inputs,
248 .outputs = op.outputs};
249 });
250
251 return {.operands = std::move(operands),
252 .operations = std::move(operations),
253 .inputIndexes = testSubgraph.inputIndexes,
254 .outputIndexes = testSubgraph.outputIndexes};
255 }
256
copyTestBuffers(const std::vector<const TestBuffer * > & buffers,uint8_t * output)257 void 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
waitForSyncFence(int syncFd)269 void 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
createModel(const TestModel & testModel)276 Model 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
292 // Constant copies.
293 hidl_vec<uint8_t> operandValues(constCopySize);
294 copyTestBuffers(constCopies, operandValues.data());
295
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
309 copyTestBuffers(constReferences, mappedPtr);
310 }
311
312 return {.main = std::move(mainSubgraph),
313 .referenced = std::move(refSubgraphs),
314 .operandValues = std::move(operandValues),
315 .pools = std::move(pools),
316 .relaxComputationFloat32toFloat16 = testModel.isRelaxed};
317 }
318
isOutputSizeGreaterThanOne(const TestModel & testModel,uint32_t index)319 static bool isOutputSizeGreaterThanOne(const TestModel& testModel, uint32_t index) {
320 const auto byteSize = testModel.main.operands[testModel.main.outputIndexes[index]].data.size();
321 return byteSize > 1u;
322 }
323
makeOutputInsufficientSize(uint32_t outputIndex,Request * request)324 static void makeOutputInsufficientSize(uint32_t outputIndex, Request* request) {
325 auto& length = request->outputs[outputIndex].location.length;
326 ASSERT_GT(length, 1u);
327 length -= 1u;
328 }
329
makeOutputDimensionsUnspecified(Model * model)330 static void makeOutputDimensionsUnspecified(Model* model) {
331 for (auto i : model->main.outputIndexes) {
332 auto& dims = model->main.operands[i].dimensions;
333 std::fill(dims.begin(), dims.end(), 0);
334 }
335 }
336
337 class ExecutionContextV1_3 {
338 public:
ExecutionContextV1_3(sp<IDevice> device,sp<IPreparedModel> preparedModel)339 ExecutionContextV1_3(sp<IDevice> device, sp<IPreparedModel> preparedModel)
340 : kDevice(std::move(device)), kPreparedModel(std::move(preparedModel)) {}
341
342 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
createRequest(const TestModel & testModel,MemoryType memoryType)360 std::optional<Request> ExecutionContextV1_3::createRequest(const TestModel& testModel,
361 MemoryType memoryType) {
362 // 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
367 DeviceMemoryAllocator allocator(kDevice, kPreparedModel, testModel);
368 std::vector<uint32_t> tokens;
369 mBuffers.clear();
370
371 // Model inputs.
372 hidl_vec<RequestArgument> inputs(testModel.main.inputIndexes.size());
373 size_t inputSize = 0;
374 for (uint32_t i = 0; i < testModel.main.inputIndexes.size(); i++) {
375 const auto& op = testModel.main.operands[testModel.main.inputIndexes[i]];
376 if (op.data.size() == 0) {
377 // Omitted input.
378 inputs[i] = {.hasNoValue = true};
379 continue;
380 } else if (memoryType == MemoryType::DEVICE) {
381 SCOPED_TRACE("Input index = " + std::to_string(i));
382 auto [buffer, token] = allocator.allocate<IOType::INPUT>(i);
383 if (buffer != nullptr) {
384 DataLocation loc = {.poolIndex = static_cast<uint32_t>(mBuffers.size() +
385 kDeviceMemoryBeginIndex)};
386 mBuffers.push_back(std::move(buffer));
387 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.
402 hidl_vec<RequestArgument> outputs(testModel.main.outputIndexes.size());
403 size_t outputSize = 0;
404 for (uint32_t i = 0; i < testModel.main.outputIndexes.size(); i++) {
405 const auto& op = testModel.main.operands[testModel.main.outputIndexes[i]];
406 if (memoryType == MemoryType::DEVICE) {
407 SCOPED_TRACE("Output index = " + std::to_string(i));
408 auto [buffer, token] = allocator.allocate<IOType::OUTPUT>(i);
409 if (buffer != nullptr) {
410 DataLocation loc = {.poolIndex = static_cast<uint32_t>(mBuffers.size() +
411 kDeviceMemoryBeginIndex)};
412 mBuffers.push_back(std::move(buffer));
413 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
435 if (memoryType == MemoryType::DEVICE && mBuffers.empty()) {
436 return std::nullopt;
437 }
438
439 // Memory pools.
440 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++) {
453 pools[kDeviceMemoryBeginIndex + i].token(tokens[i]);
454 }
455
456 // Copy input data to the input shared memory pool.
457 uint8_t* inputPtr = mInputMemory->getPointer();
458 for (uint32_t i = 0; i < testModel.main.inputIndexes.size(); i++) {
459 if (!inputs[i].hasNoValue && inputs[i].location.poolIndex == kInputPoolIndex) {
460 const auto& op = testModel.main.operands[testModel.main.inputIndexes[i]];
461 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 }
466 return Request{
467 .inputs = std::move(inputs), .outputs = std::move(outputs), .pools = std::move(pools)};
468 }
469
getOutputBuffers(const TestModel & testModel,const Request & request) const470 std::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;
493 }
494
495 // Get a TestBuffer with data copied from an IBuffer object.
getBuffer(const sp<IBuffer> & buffer,size_t size,TestBuffer * testBuffer) const496 void ExecutionContextV1_3::getBuffer(const sp<IBuffer>& buffer, size_t size,
497 TestBuffer* testBuffer) const {
498 // 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
hasZeroSizedOutput(const TestModel & testModel)513 static 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
ExecutePreparedModel(const sp<IPreparedModel> & preparedModel,const Request & request,MeasureTiming measure,const OptionalTimeoutDuration & loopTimeoutDuration,sp<ExecutionCallback> & callback)520 static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel,
521 const Request& request, MeasureTiming measure,
522 const OptionalTimeoutDuration& loopTimeoutDuration,
523 sp<ExecutionCallback>& callback) {
524 return preparedModel->execute_1_3(request, measure, {}, loopTimeoutDuration, callback);
525 }
ExecutePreparedModel(const sp<IPreparedModel> & preparedModel,const Request & request,MeasureTiming measure,const OptionalTimeoutDuration & loopTimeoutDuration,hidl_vec<OutputShape> * outputShapes,Timing * timing)526 static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel,
527 const Request& request, MeasureTiming measure,
528 const OptionalTimeoutDuration& loopTimeoutDuration,
529 hidl_vec<OutputShape>* outputShapes,
530 Timing* timing) {
531 ErrorStatus result;
532 Return<void> ret = preparedModel->executeSynchronously_1_3(
533 request, measure, {}, loopTimeoutDuration,
534 [&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 }
CreateBurst(const sp<IPreparedModel> & preparedModel)545 static std::shared_ptr<::android::nn::ExecutionBurstController> CreateBurst(
546 const sp<IPreparedModel>& preparedModel) {
547 return android::nn::ExecutionBurstController::create(preparedModel,
548 std::chrono::microseconds{0});
549 }
550
EvaluatePreparedModel(const sp<IDevice> & device,const sp<IPreparedModel> & preparedModel,const TestModel & testModel,const TestConfig & testConfig,bool * skipped=nullptr)551 void EvaluatePreparedModel(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel,
552 const TestModel& testModel, const TestConfig& testConfig,
553 bool* skipped = nullptr) {
554 if (skipped != nullptr) {
555 *skipped = false;
556 }
557 // If output0 does not have size larger than one byte, we can not test with insufficient buffer.
558 if (testConfig.outputType == OutputType::INSUFFICIENT &&
559 !isOutputSizeGreaterThanOne(testModel, 0)) {
560 return;
561 }
562
563 ExecutionContextV1_3 context(device, preparedModel);
564 auto maybeRequest = context.createRequest(testModel, testConfig.memoryType);
565 // Skip if testing memory domain but no device memory has been allocated.
566 if (!maybeRequest.has_value()) {
567 return;
568 }
569
570 Request request = std::move(maybeRequest.value());
571
572 constexpr uint32_t kInsufficientOutputIndex = 0;
573 if (testConfig.outputType == OutputType::INSUFFICIENT) {
574 makeOutputInsufficientSize(kInsufficientOutputIndex, &request);
575 }
576
577 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
588 ErrorStatus executionStatus;
589 hidl_vec<OutputShape> outputShapes;
590 Timing timing;
591 switch (testConfig.executor) {
592 case Executor::ASYNC: {
593 SCOPED_TRACE("asynchronous");
594
595 // launch execution
596 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
597 Return<ErrorStatus> executionLaunchStatus =
598 ExecutePreparedModel(preparedModel, request, testConfig.measureTiming,
599 loopTimeoutDuration, executionCallback);
600 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
615 Return<ErrorStatus> executionReturnStatus =
616 ExecutePreparedModel(preparedModel, request, testConfig.measureTiming,
617 loopTimeoutDuration, &outputShapes, &timing);
618 ASSERT_TRUE(executionReturnStatus.isOk());
619 executionStatus = static_cast<ErrorStatus>(executionReturnStatus);
620
621 break;
622 }
623 case Executor::BURST: {
624 // TODO(butlermichael): Check if we need to test burst in V1_3 if the interface remains
625 // V1_2.
626 SCOPED_TRACE("burst");
627
628 // check compliance
629 ASSERT_TRUE(nn::compliantWithV1_0(request));
630 V1_0::Request request10 = nn::convertToV1_0(request);
631
632 // 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
638 std::vector<intptr_t> keys(request10.pools.size());
639 for (size_t i = 0; i < keys.size(); ++i) {
640 keys[i] = reinterpret_cast<intptr_t>(&request10.pools[i]);
641 }
642
643 // execute burst
644 int n;
645 std::tie(n, outputShapes, timing, std::ignore) =
646 controller->compute(request10, testConfig.measureTiming, keys);
647 executionStatus = nn::convertResultCodeToErrorStatus(n);
648
649 break;
650 }
651 case Executor::FENCED: {
652 SCOPED_TRACE("fenced");
653 ErrorStatus result;
654 hidl_handle syncFenceHandle;
655 sp<IFencedExecutionCallback> fencedCallback;
656 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);
666 ASSERT_TRUE(ret.isOk());
667 if (result != ErrorStatus::NONE) {
668 ASSERT_EQ(syncFenceHandle.getNativeHandle(), nullptr);
669 ASSERT_EQ(fencedCallback, nullptr);
670 executionStatus = result;
671 timing = {UINT64_MAX, UINT64_MAX};
672 } else if (syncFenceHandle.getNativeHandle()) {
673 // If a sync fence is returned, try start another run waiting for the sync fence.
674 ret = preparedModel->executeFenced(request, {syncFenceHandle},
675 testConfig.measureTiming, {},
676 loopTimeoutDuration, {}, callbackFunc);
677 ASSERT_TRUE(ret.isOk());
678 ASSERT_EQ(result, ErrorStatus::NONE);
679 waitForSyncFence(syncFenceHandle.getNativeHandle()->data[0]);
680 }
681 if (result == ErrorStatus::NONE) {
682 ASSERT_NE(fencedCallback, nullptr);
683 Return<void> ret = fencedCallback->getExecutionInfo(
684 [&executionStatus, &timing](ErrorStatus error, Timing t, Timing) {
685 executionStatus = error;
686 timing = t;
687 });
688 ASSERT_TRUE(ret.isOk());
689 }
690 break;
691 }
692 }
693
694 if (testConfig.outputType != OutputType::FULLY_SPECIFIED &&
695 executionStatus == ErrorStatus::GENERAL_FAILURE) {
696 if (skipped != nullptr) {
697 *skipped = true;
698 }
699 if (!testConfig.reportSkipping) {
700 return;
701 }
702 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
703 "execute model that it does not support.";
704 std::cout << "[ ] Early termination of test because vendor service cannot "
705 "execute model that it does not support."
706 << std::endl;
707 GTEST_SKIP();
708 }
709 if (testConfig.measureTiming == MeasureTiming::NO) {
710 EXPECT_EQ(UINT64_MAX, timing.timeOnDevice);
711 EXPECT_EQ(UINT64_MAX, timing.timeInDriver);
712 } else {
713 if (timing.timeOnDevice != UINT64_MAX && timing.timeInDriver != UINT64_MAX) {
714 EXPECT_LE(timing.timeOnDevice, timing.timeInDriver);
715 }
716 }
717
718 switch (testConfig.outputType) {
719 case OutputType::FULLY_SPECIFIED:
720 if (testConfig.executor == Executor::FENCED && hasZeroSizedOutput(testModel)) {
721 // Executor::FENCED does not support zero-sized output.
722 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
723 return;
724 }
725 // If the model output operands are fully specified, outputShapes must be either
726 // either empty, or have the same number of elements as the number of outputs.
727 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
728 ASSERT_TRUE(outputShapes.size() == 0 ||
729 outputShapes.size() == testModel.main.outputIndexes.size());
730 break;
731 case OutputType::UNSPECIFIED:
732 if (testConfig.executor == Executor::FENCED) {
733 // For Executor::FENCED, the output shape must be fully specified.
734 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
735 return;
736 }
737 // If the model output operands are not fully specified, outputShapes must have
738 // the same number of elements as the number of outputs.
739 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
740 ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size());
741 break;
742 case OutputType::INSUFFICIENT:
743 if (testConfig.executor == Executor::FENCED) {
744 // For Executor::FENCED, the output shape must be fully specified.
745 ASSERT_EQ(ErrorStatus::INVALID_ARGUMENT, executionStatus);
746 return;
747 }
748 ASSERT_EQ(ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, executionStatus);
749 ASSERT_EQ(outputShapes.size(), testModel.main.outputIndexes.size());
750 // Check that all returned output dimensions are at least as fully specified as the
751 // union of the information about the corresponding operand in the model and in the
752 // request. In this test, all model outputs have known rank with all dimensions
753 // unspecified, and no dimensional information is provided in the request.
754 for (uint32_t i = 0; i < outputShapes.size(); i++) {
755 ASSERT_EQ(outputShapes[i].isSufficient, i != kInsufficientOutputIndex);
756 const auto& actual = outputShapes[i].dimensions;
757 const auto& golden =
758 testModel.main.operands[testModel.main.outputIndexes[i]].dimensions;
759 ASSERT_EQ(actual.size(), golden.size());
760 for (uint32_t j = 0; j < actual.size(); j++) {
761 if (actual[j] == 0) continue;
762 EXPECT_EQ(actual[j], golden[j]) << "index: " << j;
763 }
764 }
765 return;
766 case OutputType::MISSED_DEADLINE:
767 ASSERT_TRUE(executionStatus == ErrorStatus::MISSED_DEADLINE_TRANSIENT ||
768 executionStatus == ErrorStatus::MISSED_DEADLINE_PERSISTENT)
769 << "executionStatus = " << executionStatus;
770 return;
771 }
772
773 // Go through all outputs, check returned output shapes.
774 for (uint32_t i = 0; i < outputShapes.size(); i++) {
775 EXPECT_TRUE(outputShapes[i].isSufficient);
776 const auto& expect = testModel.main.operands[testModel.main.outputIndexes[i]].dimensions;
777 const std::vector<uint32_t> actual = outputShapes[i].dimensions;
778 EXPECT_EQ(expect, actual);
779 }
780
781 // Retrieve execution results.
782 const std::vector<TestBuffer> outputs = context.getOutputBuffers(testModel, request);
783
784 // We want "close-enough" results.
785 checkResults(testModel, outputs);
786 }
787
EvaluatePreparedModel(const sp<IDevice> & device,const sp<IPreparedModel> & preparedModel,const TestModel & testModel,TestKind testKind)788 void EvaluatePreparedModel(const sp<IDevice>& device, const sp<IPreparedModel>& preparedModel,
789 const TestModel& testModel, TestKind testKind) {
790 std::vector<OutputType> outputTypesList;
791 std::vector<MeasureTiming> measureTimingList;
792 std::vector<Executor> executorList;
793 std::vector<MemoryType> memoryTypeList;
794
795 switch (testKind) {
796 case TestKind::GENERAL: {
797 outputTypesList = {OutputType::FULLY_SPECIFIED};
798 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
799 executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST};
800 memoryTypeList = {MemoryType::ASHMEM};
801 } break;
802 case TestKind::DYNAMIC_SHAPE: {
803 outputTypesList = {OutputType::UNSPECIFIED, OutputType::INSUFFICIENT};
804 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
805 executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST, Executor::FENCED};
806 memoryTypeList = {MemoryType::ASHMEM};
807 } break;
808 case TestKind::MEMORY_DOMAIN: {
809 outputTypesList = {OutputType::FULLY_SPECIFIED};
810 measureTimingList = {MeasureTiming::NO};
811 executorList = {Executor::ASYNC, Executor::SYNC, Executor::FENCED};
812 memoryTypeList = {MemoryType::BLOB_AHWB, MemoryType::DEVICE};
813 } break;
814 case TestKind::FENCED_COMPUTE: {
815 outputTypesList = {OutputType::FULLY_SPECIFIED};
816 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
817 executorList = {Executor::FENCED};
818 memoryTypeList = {MemoryType::ASHMEM};
819 } break;
820 case TestKind::QUANTIZATION_COUPLING: {
821 LOG(FATAL) << "Wrong TestKind for EvaluatePreparedModel";
822 return;
823 } break;
824 case TestKind::INTINITE_LOOP_TIMEOUT: {
825 outputTypesList = {OutputType::MISSED_DEADLINE};
826 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
827 // Burst does not support V1_3 loop timeout.
828 executorList = {Executor::ASYNC, Executor::SYNC, Executor::FENCED};
829 memoryTypeList = {MemoryType::ASHMEM};
830 } break;
831 }
832
833 for (const OutputType outputType : outputTypesList) {
834 for (const MeasureTiming measureTiming : measureTimingList) {
835 for (const Executor executor : executorList) {
836 for (const MemoryType memoryType : memoryTypeList) {
837 const TestConfig testConfig(executor, measureTiming, outputType, memoryType);
838 EvaluatePreparedModel(device, preparedModel, testModel, testConfig);
839 }
840 }
841 }
842 }
843 }
844
EvaluatePreparedCoupledModels(const sp<IDevice> & device,const sp<IPreparedModel> & preparedModel,const TestModel & testModel,const sp<IPreparedModel> & preparedCoupledModel,const TestModel & coupledModel)845 void EvaluatePreparedCoupledModels(const sp<IDevice>& device,
846 const sp<IPreparedModel>& preparedModel,
847 const TestModel& testModel,
848 const sp<IPreparedModel>& preparedCoupledModel,
849 const TestModel& coupledModel) {
850 const std::vector<OutputType> outputTypesList = {OutputType::FULLY_SPECIFIED};
851 const std::vector<MeasureTiming> measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
852 const std::vector<Executor> executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST,
853 Executor::FENCED};
854
855 for (const OutputType outputType : outputTypesList) {
856 for (const MeasureTiming measureTiming : measureTimingList) {
857 for (const Executor executor : executorList) {
858 const TestConfig testConfig(executor, measureTiming, outputType, MemoryType::ASHMEM,
859 /*reportSkipping=*/false);
860 bool baseSkipped = false;
861 EvaluatePreparedModel(device, preparedModel, testModel, testConfig, &baseSkipped);
862 bool coupledSkipped = false;
863 EvaluatePreparedModel(device, preparedCoupledModel, coupledModel, testConfig,
864 &coupledSkipped);
865 ASSERT_EQ(baseSkipped, coupledSkipped);
866 if (baseSkipped) {
867 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
868 "execute model that it does not support.";
869 std::cout << "[ ] Early termination of test because vendor service "
870 "cannot "
871 "execute model that it does not support."
872 << std::endl;
873 GTEST_SKIP();
874 }
875 }
876 }
877 }
878 }
879
Execute(const sp<IDevice> & device,const TestModel & testModel,TestKind testKind)880 void Execute(const sp<IDevice>& device, const TestModel& testModel, TestKind testKind) {
881 Model model = createModel(testModel);
882 if (testKind == TestKind::DYNAMIC_SHAPE) {
883 makeOutputDimensionsUnspecified(&model);
884 }
885
886 sp<IPreparedModel> preparedModel;
887 switch (testKind) {
888 case TestKind::GENERAL:
889 case TestKind::DYNAMIC_SHAPE:
890 case TestKind::MEMORY_DOMAIN:
891 case TestKind::FENCED_COMPUTE:
892 case TestKind::INTINITE_LOOP_TIMEOUT: {
893 createPreparedModel(device, model, &preparedModel);
894 if (preparedModel == nullptr) return;
895 EvaluatePreparedModel(device, preparedModel, testModel, testKind);
896 } break;
897 case TestKind::QUANTIZATION_COUPLING: {
898 ASSERT_TRUE(testModel.hasQuant8CoupledOperands());
899 createPreparedModel(device, model, &preparedModel,
900 /*reportSkipping*/ false);
901 TestModel signedQuantizedModel = convertQuant8AsymmOperandsToSigned(testModel);
902 sp<IPreparedModel> preparedCoupledModel;
903 createPreparedModel(device, createModel(signedQuantizedModel), &preparedCoupledModel,
904 /*reportSkipping*/ false);
905 // If we couldn't prepare a model with unsigned quantization, we must
906 // fail to prepare a model with signed quantization as well.
907 if (preparedModel == nullptr) {
908 ASSERT_EQ(preparedCoupledModel, nullptr);
909 // If we failed to prepare both of the models, we can safely skip
910 // the test.
911 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
912 "prepare model that it does not support.";
913 std::cout
914 << "[ ] Early termination of test because vendor service cannot "
915 "prepare model that it does not support."
916 << std::endl;
917 GTEST_SKIP();
918 }
919 ASSERT_NE(preparedCoupledModel, nullptr);
920 EvaluatePreparedCoupledModels(device, preparedModel, testModel, preparedCoupledModel,
921 signedQuantizedModel);
922 } break;
923 }
924 }
925
SetUp()926 void GeneratedTestBase::SetUp() {
927 testing::TestWithParam<GeneratedTestParam>::SetUp();
928 ASSERT_NE(kDevice, nullptr);
929 }
930
getNamedModels(const FilterFn & filter)931 std::vector<NamedModel> getNamedModels(const FilterFn& filter) {
932 return TestModelManager::get().getTestModels(filter);
933 }
934
getNamedModels(const FilterNameFn & filter)935 std::vector<NamedModel> getNamedModels(const FilterNameFn& filter) {
936 return TestModelManager::get().getTestModels(filter);
937 }
938
printGeneratedTest(const testing::TestParamInfo<GeneratedTestParam> & info)939 std::string printGeneratedTest(const testing::TestParamInfo<GeneratedTestParam>& info) {
940 const auto& [namedDevice, namedModel] = info.param;
941 return gtestCompliantName(getName(namedDevice) + "_" + getName(namedModel));
942 }
943
944 // Tag for the generated tests
945 class GeneratedTest : public GeneratedTestBase {};
946
947 // Tag for the dynamic output shape tests
948 class DynamicOutputShapeTest : public GeneratedTest {};
949
950 // Tag for the memory domain tests
951 class MemoryDomainTest : public GeneratedTest {};
952
953 // Tag for the fenced compute tests
954 class FencedComputeTest : public GeneratedTest {};
955
956 // Tag for the dynamic output shape tests
957 class QuantizationCouplingTest : public GeneratedTest {};
958
959 // Tag for the loop timeout tests
960 class InfiniteLoopTimeoutTest : public GeneratedTest {};
961
TEST_P(GeneratedTest,Test)962 TEST_P(GeneratedTest, Test) {
963 Execute(kDevice, kTestModel, TestKind::GENERAL);
964 }
965
TEST_P(DynamicOutputShapeTest,Test)966 TEST_P(DynamicOutputShapeTest, Test) {
967 Execute(kDevice, kTestModel, TestKind::DYNAMIC_SHAPE);
968 }
969
TEST_P(MemoryDomainTest,Test)970 TEST_P(MemoryDomainTest, Test) {
971 Execute(kDevice, kTestModel, TestKind::MEMORY_DOMAIN);
972 }
973
TEST_P(FencedComputeTest,Test)974 TEST_P(FencedComputeTest, Test) {
975 Execute(kDevice, kTestModel, TestKind::FENCED_COMPUTE);
976 }
977
TEST_P(QuantizationCouplingTest,Test)978 TEST_P(QuantizationCouplingTest, Test) {
979 Execute(kDevice, kTestModel, TestKind::QUANTIZATION_COUPLING);
980 }
981
TEST_P(InfiniteLoopTimeoutTest,Test)982 TEST_P(InfiniteLoopTimeoutTest, Test) {
983 Execute(kDevice, kTestModel, TestKind::INTINITE_LOOP_TIMEOUT);
984 }
985
986 INSTANTIATE_GENERATED_TEST(GeneratedTest,
__anon404ff7050902(const TestModel& testModel) 987 [](const TestModel& testModel) { return !testModel.expectFailure; });
988
__anon404ff7050a02(const TestModel& testModel) 989 INSTANTIATE_GENERATED_TEST(DynamicOutputShapeTest, [](const TestModel& testModel) {
990 return !testModel.expectFailure && !testModel.hasScalarOutputs();
991 });
992
993 INSTANTIATE_GENERATED_TEST(MemoryDomainTest,
__anon404ff7050b02(const TestModel& testModel) 994 [](const TestModel& testModel) { return !testModel.expectFailure; });
995
996 INSTANTIATE_GENERATED_TEST(FencedComputeTest,
__anon404ff7050c02(const TestModel& testModel) 997 [](const TestModel& testModel) { return !testModel.expectFailure; });
998
__anon404ff7050d02(const TestModel& testModel) 999 INSTANTIATE_GENERATED_TEST(QuantizationCouplingTest, [](const TestModel& testModel) {
1000 return !testModel.expectFailure && testModel.hasQuant8CoupledOperands() &&
1001 testModel.main.operations.size() == 1;
1002 });
1003
__anon404ff7050e02(const TestModel& testModel) 1004 INSTANTIATE_GENERATED_TEST(InfiniteLoopTimeoutTest, [](const TestModel& testModel) {
1005 return testModel.isInfiniteLoopTimeoutTest();
1006 });
1007
1008 } // namespace android::hardware::neuralnetworks::V1_3::vts::functional
1009