blob: c1bf494328193a36d736b38f99565360db82909d [file] [log] [blame]
Slava Shklyaev73ee79d2019-05-14 14:15:14 +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>
30#include <android/hidl/allocator/1.0/IAllocator.h>
31#include <android/hidl/memory/1.0/IMemory.h>
32#include <hidlmemory/mapping.h>
33
Xusong Wangead950d2019-08-09 16:45:24 -070034#include <gtest/gtest.h>
35#include <algorithm>
Michael Butler648ada52019-07-25 17:22:11 -070036#include <chrono>
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010037#include <iostream>
Xusong Wangead950d2019-08-09 16:45:24 -070038#include <numeric>
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010039
40#include "1.0/Utils.h"
41#include "1.2/Callbacks.h"
42#include "ExecutionBurstController.h"
43#include "MemoryUtils.h"
44#include "TestHarness.h"
45#include "Utils.h"
Xusong Wangbcaa7822019-08-23 16:10:54 -070046#include "VtsHalNeuralnetworks.h"
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010047
Michael Butler62749b92019-08-26 23:55:47 -070048namespace android::hardware::neuralnetworks::V1_2::vts::functional {
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010049
Xusong Wangead950d2019-08-09 16:45:24 -070050using namespace test_helper;
Michael Butler62749b92019-08-26 23:55:47 -070051using hidl::memory::V1_0::IMemory;
52using implementation::ExecutionCallback;
53using implementation::PreparedModelCallback;
54using V1_0::DataLocation;
55using V1_0::ErrorStatus;
56using V1_0::OperandLifeTime;
57using V1_0::Request;
58using V1_1::ExecutionPreference;
Slava Shklyaev73ee79d2019-05-14 14:15:14 +010059using HidlToken = hidl_array<uint8_t, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
60
Lev Proleev0d4ba3f2019-10-02 17:32:06 +010061namespace {
62
63enum class Executor { ASYNC, SYNC, BURST };
64
Xusong Wangead950d2019-08-09 16:45:24 -070065enum class OutputType { FULLY_SPECIFIED, UNSPECIFIED, INSUFFICIENT };
66
Lev Proleev0d4ba3f2019-10-02 17:32:06 +010067struct TestConfig {
68 Executor executor;
69 MeasureTiming measureTiming;
70 OutputType outputType;
71};
72
73} // namespace
74
Xusong Wangead950d2019-08-09 16:45:24 -070075Model createModel(const TestModel& testModel) {
76 // Model operands.
77 hidl_vec<Operand> operands(testModel.operands.size());
78 size_t constCopySize = 0, constRefSize = 0;
79 for (uint32_t i = 0; i < testModel.operands.size(); i++) {
80 const auto& op = testModel.operands[i];
81
82 DataLocation loc = {};
83 if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) {
84 loc = {.poolIndex = 0,
85 .offset = static_cast<uint32_t>(constCopySize),
86 .length = static_cast<uint32_t>(op.data.size())};
87 constCopySize += op.data.alignedSize();
88 } else if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) {
89 loc = {.poolIndex = 0,
90 .offset = static_cast<uint32_t>(constRefSize),
91 .length = static_cast<uint32_t>(op.data.size())};
92 constRefSize += op.data.alignedSize();
93 }
94
95 Operand::ExtraParams extraParams;
96 if (op.type == TestOperandType::TENSOR_QUANT8_SYMM_PER_CHANNEL) {
97 extraParams.channelQuant(SymmPerChannelQuantParams{
98 .scales = op.channelQuant.scales, .channelDim = op.channelQuant.channelDim});
99 }
100
101 operands[i] = {.type = static_cast<OperandType>(op.type),
102 .dimensions = op.dimensions,
103 .numberOfConsumers = op.numberOfConsumers,
104 .scale = op.scale,
105 .zeroPoint = op.zeroPoint,
106 .lifetime = static_cast<OperandLifeTime>(op.lifetime),
107 .location = loc,
108 .extraParams = std::move(extraParams)};
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100109 }
Xusong Wangead950d2019-08-09 16:45:24 -0700110
111 // Model operations.
112 hidl_vec<Operation> operations(testModel.operations.size());
113 std::transform(testModel.operations.begin(), testModel.operations.end(), operations.begin(),
114 [](const TestOperation& op) -> Operation {
115 return {.type = static_cast<OperationType>(op.type),
116 .inputs = op.inputs,
117 .outputs = op.outputs};
118 });
119
120 // Constant copies.
121 hidl_vec<uint8_t> operandValues(constCopySize);
122 for (uint32_t i = 0; i < testModel.operands.size(); i++) {
123 const auto& op = testModel.operands[i];
124 if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) {
125 const uint8_t* begin = op.data.get<uint8_t>();
126 const uint8_t* end = begin + op.data.size();
127 std::copy(begin, end, operandValues.data() + operands[i].location.offset);
128 }
129 }
130
131 // Shared memory.
132 hidl_vec<hidl_memory> pools = {};
133 if (constRefSize > 0) {
134 hidl_vec_push_back(&pools, nn::allocateSharedMemory(constRefSize));
135 CHECK_NE(pools[0].size(), 0u);
136
137 // load data
138 sp<IMemory> mappedMemory = mapMemory(pools[0]);
139 CHECK(mappedMemory.get() != nullptr);
140 uint8_t* mappedPtr =
141 reinterpret_cast<uint8_t*>(static_cast<void*>(mappedMemory->getPointer()));
142 CHECK(mappedPtr != nullptr);
143
144 for (uint32_t i = 0; i < testModel.operands.size(); i++) {
145 const auto& op = testModel.operands[i];
146 if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) {
147 const uint8_t* begin = op.data.get<uint8_t>();
148 const uint8_t* end = begin + op.data.size();
149 std::copy(begin, end, mappedPtr + operands[i].location.offset);
150 }
151 }
152 }
153
154 return {.operands = std::move(operands),
155 .operations = std::move(operations),
156 .inputIndexes = testModel.inputIndexes,
157 .outputIndexes = testModel.outputIndexes,
158 .operandValues = std::move(operandValues),
159 .pools = std::move(pools),
160 .relaxComputationFloat32toFloat16 = testModel.isRelaxed};
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100161}
162
Xusong Wangead950d2019-08-09 16:45:24 -0700163static bool isOutputSizeGreaterThanOne(const TestModel& testModel, uint32_t index) {
164 const auto byteSize = testModel.operands[testModel.outputIndexes[index]].data.size();
165 return byteSize > 1u;
166}
167
168static void makeOutputInsufficientSize(uint32_t outputIndex, Request* request) {
169 auto& length = request->outputs[outputIndex].location.length;
170 ASSERT_GT(length, 1u);
171 length -= 1u;
172}
173
174static void makeOutputDimensionsUnspecified(Model* model) {
175 for (auto i : model->outputIndexes) {
176 auto& dims = model->operands[i].dimensions;
177 std::fill(dims.begin(), dims.end(), 0);
178 }
179}
180
181static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel,
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100182 const Request& request, MeasureTiming measure,
183 sp<ExecutionCallback>& callback) {
184 return preparedModel->execute_1_2(request, measure, callback);
185}
Xusong Wangead950d2019-08-09 16:45:24 -0700186static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel,
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100187 const Request& request, MeasureTiming measure,
188 hidl_vec<OutputShape>* outputShapes,
189 Timing* timing) {
190 ErrorStatus result;
191 Return<void> ret = preparedModel->executeSynchronously(
192 request, measure,
193 [&result, outputShapes, timing](ErrorStatus error, const hidl_vec<OutputShape>& shapes,
194 const Timing& time) {
195 result = error;
196 *outputShapes = shapes;
197 *timing = time;
198 });
199 if (!ret.isOk()) {
200 return ErrorStatus::GENERAL_FAILURE;
201 }
202 return result;
203}
204static std::shared_ptr<::android::nn::ExecutionBurstController> CreateBurst(
205 const sp<IPreparedModel>& preparedModel) {
Michael Butler648ada52019-07-25 17:22:11 -0700206 return android::nn::ExecutionBurstController::create(preparedModel,
207 std::chrono::microseconds{0});
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100208}
Xusong Wangead950d2019-08-09 16:45:24 -0700209
210void EvaluatePreparedModel(const sp<IPreparedModel>& preparedModel, const TestModel& testModel,
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100211 const TestConfig& testConfig) {
Xusong Wangead950d2019-08-09 16:45:24 -0700212 // If output0 does not have size larger than one byte, we can not test with insufficient buffer.
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100213 if (testConfig.outputType == OutputType::INSUFFICIENT &&
214 !isOutputSizeGreaterThanOne(testModel, 0)) {
Xusong Wangead950d2019-08-09 16:45:24 -0700215 return;
216 }
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100217
Xusong Wangead950d2019-08-09 16:45:24 -0700218 Request request = createRequest(testModel);
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100219 if (testConfig.outputType == OutputType::INSUFFICIENT) {
Xusong Wangead950d2019-08-09 16:45:24 -0700220 makeOutputInsufficientSize(/*outputIndex=*/0, &request);
221 }
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100222
Xusong Wangead950d2019-08-09 16:45:24 -0700223 ErrorStatus executionStatus;
224 hidl_vec<OutputShape> outputShapes;
225 Timing timing;
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100226 switch (testConfig.executor) {
Xusong Wangead950d2019-08-09 16:45:24 -0700227 case Executor::ASYNC: {
228 SCOPED_TRACE("asynchronous");
229
230 // launch execution
231 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100232 Return<ErrorStatus> executionLaunchStatus = ExecutePreparedModel(
233 preparedModel, request, testConfig.measureTiming, executionCallback);
Xusong Wangead950d2019-08-09 16:45:24 -0700234 ASSERT_TRUE(executionLaunchStatus.isOk());
235 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
236
237 // retrieve execution status
238 executionCallback->wait();
239 executionStatus = executionCallback->getStatus();
240 outputShapes = executionCallback->getOutputShapes();
241 timing = executionCallback->getTiming();
242
243 break;
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100244 }
Xusong Wangead950d2019-08-09 16:45:24 -0700245 case Executor::SYNC: {
246 SCOPED_TRACE("synchronous");
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100247
Xusong Wangead950d2019-08-09 16:45:24 -0700248 // execute
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100249 Return<ErrorStatus> executionReturnStatus = ExecutePreparedModel(
250 preparedModel, request, testConfig.measureTiming, &outputShapes, &timing);
Xusong Wangead950d2019-08-09 16:45:24 -0700251 ASSERT_TRUE(executionReturnStatus.isOk());
252 executionStatus = static_cast<ErrorStatus>(executionReturnStatus);
253
254 break;
255 }
256 case Executor::BURST: {
257 SCOPED_TRACE("burst");
258
259 // create burst
260 const std::shared_ptr<::android::nn::ExecutionBurstController> controller =
261 CreateBurst(preparedModel);
262 ASSERT_NE(nullptr, controller.get());
263
264 // create memory keys
265 std::vector<intptr_t> keys(request.pools.size());
266 for (size_t i = 0; i < keys.size(); ++i) {
267 keys[i] = reinterpret_cast<intptr_t>(&request.pools[i]);
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100268 }
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100269
Xusong Wangead950d2019-08-09 16:45:24 -0700270 // execute burst
Michael Butler648ada52019-07-25 17:22:11 -0700271 int n;
272 std::tie(n, outputShapes, timing, std::ignore) =
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100273 controller->compute(request, testConfig.measureTiming, keys);
Michael Butler648ada52019-07-25 17:22:11 -0700274 executionStatus = nn::convertResultCodeToErrorStatus(n);
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100275
Xusong Wangead950d2019-08-09 16:45:24 -0700276 break;
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100277 }
278 }
Xusong Wangead950d2019-08-09 16:45:24 -0700279
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100280 if (testConfig.outputType != OutputType::FULLY_SPECIFIED &&
Xusong Wangead950d2019-08-09 16:45:24 -0700281 executionStatus == ErrorStatus::GENERAL_FAILURE) {
282 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
283 "execute model that it does not support.";
284 std::cout << "[ ] Early termination of test because vendor service cannot "
285 "execute model that it does not support."
286 << std::endl;
287 GTEST_SKIP();
288 }
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100289 if (testConfig.measureTiming == MeasureTiming::NO) {
Xusong Wangead950d2019-08-09 16:45:24 -0700290 EXPECT_EQ(UINT64_MAX, timing.timeOnDevice);
291 EXPECT_EQ(UINT64_MAX, timing.timeInDriver);
292 } else {
293 if (timing.timeOnDevice != UINT64_MAX && timing.timeInDriver != UINT64_MAX) {
294 EXPECT_LE(timing.timeOnDevice, timing.timeInDriver);
295 }
296 }
297
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100298 switch (testConfig.outputType) {
Xusong Wangead950d2019-08-09 16:45:24 -0700299 case OutputType::FULLY_SPECIFIED:
300 // If the model output operands are fully specified, outputShapes must be either
301 // either empty, or have the same number of elements as the number of outputs.
302 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
303 ASSERT_TRUE(outputShapes.size() == 0 ||
304 outputShapes.size() == testModel.outputIndexes.size());
305 break;
306 case OutputType::UNSPECIFIED:
307 // If the model output operands are not fully specified, outputShapes must have
308 // the same number of elements as the number of outputs.
309 ASSERT_EQ(ErrorStatus::NONE, executionStatus);
310 ASSERT_EQ(outputShapes.size(), testModel.outputIndexes.size());
311 break;
312 case OutputType::INSUFFICIENT:
313 ASSERT_EQ(ErrorStatus::OUTPUT_INSUFFICIENT_SIZE, executionStatus);
314 ASSERT_EQ(outputShapes.size(), testModel.outputIndexes.size());
315 ASSERT_FALSE(outputShapes[0].isSufficient);
316 return;
317 }
318
319 // Go through all outputs, check returned output shapes.
320 for (uint32_t i = 0; i < outputShapes.size(); i++) {
321 EXPECT_TRUE(outputShapes[i].isSufficient);
322 const auto& expect = testModel.operands[testModel.outputIndexes[i]].dimensions;
323 const std::vector<uint32_t> actual = outputShapes[i].dimensions;
324 EXPECT_EQ(expect, actual);
325 }
326
327 // Retrieve execution results.
328 const std::vector<TestBuffer> outputs = getOutputBuffers(request);
329
330 // We want "close-enough" results.
331 checkResults(testModel, outputs);
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100332}
333
Xusong Wangead950d2019-08-09 16:45:24 -0700334void EvaluatePreparedModel(const sp<IPreparedModel>& preparedModel, const TestModel& testModel,
335 bool testDynamicOutputShape) {
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100336 std::initializer_list<OutputType> outputTypesList;
337 std::initializer_list<MeasureTiming> measureTimingList;
338 std::initializer_list<Executor> executorList;
339
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100340 if (testDynamicOutputShape) {
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100341 outputTypesList = {OutputType::UNSPECIFIED, OutputType::INSUFFICIENT};
342 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
343 executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST};
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100344 } else {
Lev Proleev0d4ba3f2019-10-02 17:32:06 +0100345 outputTypesList = {OutputType::FULLY_SPECIFIED};
346 measureTimingList = {MeasureTiming::NO, MeasureTiming::YES};
347 executorList = {Executor::ASYNC, Executor::SYNC, Executor::BURST};
348 }
349
350 for (const OutputType outputType : outputTypesList) {
351 for (const MeasureTiming measureTiming : measureTimingList) {
352 for (const Executor executor : executorList) {
353 const TestConfig testConfig = {.executor = executor,
354 .measureTiming = measureTiming,
355 .outputType = outputType};
356 EvaluatePreparedModel(preparedModel, testModel, testConfig);
357 }
358 }
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100359 }
360}
361
Michael Butler13b05162019-08-29 22:17:24 -0700362void Execute(const sp<IDevice>& device, const TestModel& testModel, bool testDynamicOutputShape) {
363 Model model = createModel(testModel);
364 if (testDynamicOutputShape) {
365 makeOutputDimensionsUnspecified(&model);
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100366 }
Michael Butler13b05162019-08-29 22:17:24 -0700367
368 sp<IPreparedModel> preparedModel;
369 createPreparedModel(device, model, &preparedModel);
370 if (preparedModel == nullptr) return;
371
372 EvaluatePreparedModel(preparedModel, testModel, testDynamicOutputShape);
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100373}
374
Michael Butler07633282019-08-29 11:08:25 -0700375void GeneratedTestBase::SetUp() {
376 testing::TestWithParam<GeneratedTestParam>::SetUp();
377 ASSERT_NE(kDevice, nullptr);
378}
379
380std::vector<NamedModel> getNamedModels(const FilterFn& filter) {
381 return TestModelManager::get().getTestModels(filter);
382}
383
384std::string printGeneratedTest(const testing::TestParamInfo<GeneratedTestParam>& info) {
385 const auto& [namedDevice, namedModel] = info.param;
386 return gtestCompliantName(getName(namedDevice) + "_" + getName(namedModel));
387}
388
Xusong Wangbcaa7822019-08-23 16:10:54 -0700389// Tag for the generated tests
Michael Butler13b05162019-08-29 22:17:24 -0700390class GeneratedTest : public GeneratedTestBase {};
Xusong Wangbcaa7822019-08-23 16:10:54 -0700391
392// Tag for the dynamic output shape tests
393class DynamicOutputShapeTest : public GeneratedTest {};
394
395TEST_P(GeneratedTest, Test) {
Michael Butler13b05162019-08-29 22:17:24 -0700396 Execute(kDevice, kTestModel, /*testDynamicOutputShape=*/false);
Slava Shklyaev73ee79d2019-05-14 14:15:14 +0100397}
398
Xusong Wangbcaa7822019-08-23 16:10:54 -0700399TEST_P(DynamicOutputShapeTest, Test) {
Michael Butler13b05162019-08-29 22:17:24 -0700400 Execute(kDevice, kTestModel, /*testDynamicOutputShape=*/true);
Xusong Wangbcaa7822019-08-23 16:10:54 -0700401}
402
403INSTANTIATE_GENERATED_TEST(GeneratedTest,
404 [](const TestModel& testModel) { return !testModel.expectFailure; });
405
406INSTANTIATE_GENERATED_TEST(DynamicOutputShapeTest,
407 [](const TestModel& testModel) { return !testModel.expectFailure; });
408
Michael Butler62749b92019-08-26 23:55:47 -0700409} // namespace android::hardware::neuralnetworks::V1_2::vts::functional