blob: 4f9b6f9c15e95d33e5d1043d0dbc2b3b39f0f67c [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>
Xusong Wangcc47dff2019-10-23 10:35:07 -070032#include <android/hardware/neuralnetworks/1.3/IPreparedModelCallback.h>
Lev Proleev26d1bc82019-08-30 11:57:18 +010033#include <android/hardware/neuralnetworks/1.3/types.h>
Lev Proleev13fdfcd2019-08-30 11:35:34 +010034#include <android/hidl/allocator/1.0/IAllocator.h>
35#include <android/hidl/memory/1.0/IMemory.h>
36#include <hidlmemory/mapping.h>
37
38#include <gtest/gtest.h>
39#include <algorithm>
Michael Butler648ada52019-07-25 17:22:11 -070040#include <chrono>
Lev Proleev13fdfcd2019-08-30 11:35:34 +010041#include <iostream>
42#include <numeric>
43
44#include "1.0/Utils.h"
45#include "1.2/Callbacks.h"
Xusong Wangcc47dff2019-10-23 10:35:07 -070046#include "1.3/Callbacks.h"
Lev Proleev13fdfcd2019-08-30 11:35:34 +010047#include "ExecutionBurstController.h"
48#include "MemoryUtils.h"
49#include "TestHarness.h"
50#include "Utils.h"
51#include "VtsHalNeuralnetworks.h"
52
Lev Proleev26d1bc82019-08-30 11:57:18 +010053namespace android::hardware::neuralnetworks::V1_3::vts::functional {
Lev Proleev13fdfcd2019-08-30 11:35:34 +010054
55using namespace test_helper;
56using hidl::memory::V1_0::IMemory;
Xusong Wangcc47dff2019-10-23 10:35:07 -070057using implementation::PreparedModelCallback;
Lev Proleev13fdfcd2019-08-30 11:35:34 +010058using V1_0::DataLocation;
59using V1_0::ErrorStatus;
60using V1_0::OperandLifeTime;
61using V1_0::Request;
62using V1_1::ExecutionPreference;
Lev Proleev26d1bc82019-08-30 11:57:18 +010063using V1_2::Constant;
64using V1_2::IPreparedModel;
65using V1_2::MeasureTiming;
66using V1_2::OperationType;
67using V1_2::OutputShape;
68using V1_2::SymmPerChannelQuantParams;
69using V1_2::Timing;
70using V1_2::implementation::ExecutionCallback;
Lev Proleev13fdfcd2019-08-30 11:35:34 +010071using HidlToken = hidl_array<uint8_t, static_cast<uint32_t>(Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
72
73enum class OutputType { FULLY_SPECIFIED, UNSPECIFIED, INSUFFICIENT };
74
75Model 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)};
109 }
110
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};
161}
162
163static 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,
182 const Request& request, MeasureTiming measure,
183 sp<ExecutionCallback>& callback) {
184 return preparedModel->execute_1_2(request, measure, callback);
185}
186static Return<ErrorStatus> ExecutePreparedModel(const sp<IPreparedModel>& preparedModel,
187 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});
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100208}
209enum class Executor { ASYNC, SYNC, BURST };
210
211void EvaluatePreparedModel(const sp<IPreparedModel>& preparedModel, const TestModel& testModel,
212 Executor executor, MeasureTiming measure, OutputType outputType) {
213 // If output0 does not have size larger than one byte, we can not test with insufficient buffer.
214 if (outputType == OutputType::INSUFFICIENT && !isOutputSizeGreaterThanOne(testModel, 0)) {
215 return;
216 }
217
218 Request request = createRequest(testModel);
219 if (outputType == OutputType::INSUFFICIENT) {
220 makeOutputInsufficientSize(/*outputIndex=*/0, &request);
221 }
222
223 ErrorStatus executionStatus;
224 hidl_vec<OutputShape> outputShapes;
225 Timing timing;
226 switch (executor) {
227 case Executor::ASYNC: {
228 SCOPED_TRACE("asynchronous");
229
230 // launch execution
231 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
232 Return<ErrorStatus> executionLaunchStatus =
233 ExecutePreparedModel(preparedModel, request, measure, executionCallback);
234 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;
244 }
245 case Executor::SYNC: {
246 SCOPED_TRACE("synchronous");
247
248 // execute
249 Return<ErrorStatus> executionReturnStatus =
250 ExecutePreparedModel(preparedModel, request, measure, &outputShapes, &timing);
251 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]);
268 }
269
270 // execute burst
Michael Butler648ada52019-07-25 17:22:11 -0700271 int n;
272 std::tie(n, outputShapes, timing, std::ignore) =
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100273 controller->compute(request, measure, keys);
Michael Butler648ada52019-07-25 17:22:11 -0700274 executionStatus = nn::convertResultCodeToErrorStatus(n);
Lev Proleev13fdfcd2019-08-30 11:35:34 +0100275
276 break;
277 }
278 }
279
280 if (outputType != OutputType::FULLY_SPECIFIED &&
281 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 }
289 if (measure == MeasureTiming::NO) {
290 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
298 switch (outputType) {
299 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);
332}
333
334void EvaluatePreparedModel(const sp<IPreparedModel>& preparedModel, const TestModel& testModel,
335 bool testDynamicOutputShape) {
336 if (testDynamicOutputShape) {
337 EvaluatePreparedModel(preparedModel, testModel, Executor::ASYNC, MeasureTiming::NO,
338 OutputType::UNSPECIFIED);
339 EvaluatePreparedModel(preparedModel, testModel, Executor::SYNC, MeasureTiming::NO,
340 OutputType::UNSPECIFIED);
341 EvaluatePreparedModel(preparedModel, testModel, Executor::BURST, MeasureTiming::NO,
342 OutputType::UNSPECIFIED);
343 EvaluatePreparedModel(preparedModel, testModel, Executor::ASYNC, MeasureTiming::YES,
344 OutputType::UNSPECIFIED);
345 EvaluatePreparedModel(preparedModel, testModel, Executor::SYNC, MeasureTiming::YES,
346 OutputType::UNSPECIFIED);
347 EvaluatePreparedModel(preparedModel, testModel, Executor::BURST, MeasureTiming::YES,
348 OutputType::UNSPECIFIED);
349 EvaluatePreparedModel(preparedModel, testModel, Executor::ASYNC, MeasureTiming::NO,
350 OutputType::INSUFFICIENT);
351 EvaluatePreparedModel(preparedModel, testModel, Executor::SYNC, MeasureTiming::NO,
352 OutputType::INSUFFICIENT);
353 EvaluatePreparedModel(preparedModel, testModel, Executor::BURST, MeasureTiming::NO,
354 OutputType::INSUFFICIENT);
355 EvaluatePreparedModel(preparedModel, testModel, Executor::ASYNC, MeasureTiming::YES,
356 OutputType::INSUFFICIENT);
357 EvaluatePreparedModel(preparedModel, testModel, Executor::SYNC, MeasureTiming::YES,
358 OutputType::INSUFFICIENT);
359 EvaluatePreparedModel(preparedModel, testModel, Executor::BURST, MeasureTiming::YES,
360 OutputType::INSUFFICIENT);
361 } else {
362 EvaluatePreparedModel(preparedModel, testModel, Executor::ASYNC, MeasureTiming::NO,
363 OutputType::FULLY_SPECIFIED);
364 EvaluatePreparedModel(preparedModel, testModel, Executor::SYNC, MeasureTiming::NO,
365 OutputType::FULLY_SPECIFIED);
366 EvaluatePreparedModel(preparedModel, testModel, Executor::BURST, MeasureTiming::NO,
367 OutputType::FULLY_SPECIFIED);
368 EvaluatePreparedModel(preparedModel, testModel, Executor::ASYNC, MeasureTiming::YES,
369 OutputType::FULLY_SPECIFIED);
370 EvaluatePreparedModel(preparedModel, testModel, Executor::SYNC, MeasureTiming::YES,
371 OutputType::FULLY_SPECIFIED);
372 EvaluatePreparedModel(preparedModel, testModel, Executor::BURST, MeasureTiming::YES,
373 OutputType::FULLY_SPECIFIED);
374 }
375}
376
377void Execute(const sp<IDevice>& device, const TestModel& testModel, bool testDynamicOutputShape) {
378 Model model = createModel(testModel);
379 if (testDynamicOutputShape) {
380 makeOutputDimensionsUnspecified(&model);
381 }
382
383 sp<IPreparedModel> preparedModel;
384 createPreparedModel(device, model, &preparedModel);
385 if (preparedModel == nullptr) return;
386
387 EvaluatePreparedModel(preparedModel, testModel, testDynamicOutputShape);
388}
389
390void GeneratedTestBase::SetUp() {
391 testing::TestWithParam<GeneratedTestParam>::SetUp();
392 ASSERT_NE(kDevice, nullptr);
393}
394
395std::vector<NamedModel> getNamedModels(const FilterFn& filter) {
396 return TestModelManager::get().getTestModels(filter);
397}
398
399std::string printGeneratedTest(const testing::TestParamInfo<GeneratedTestParam>& info) {
400 const auto& [namedDevice, namedModel] = info.param;
401 return gtestCompliantName(getName(namedDevice) + "_" + getName(namedModel));
402}
403
404// Tag for the generated tests
405class GeneratedTest : public GeneratedTestBase {};
406
407// Tag for the dynamic output shape tests
408class DynamicOutputShapeTest : public GeneratedTest {};
409
410TEST_P(GeneratedTest, Test) {
411 Execute(kDevice, kTestModel, /*testDynamicOutputShape=*/false);
412}
413
414TEST_P(DynamicOutputShapeTest, Test) {
415 Execute(kDevice, kTestModel, /*testDynamicOutputShape=*/true);
416}
417
418INSTANTIATE_GENERATED_TEST(GeneratedTest,
419 [](const TestModel& testModel) { return !testModel.expectFailure; });
420
421INSTANTIATE_GENERATED_TEST(DynamicOutputShapeTest,
422 [](const TestModel& testModel) { return !testModel.expectFailure; });
423
Lev Proleev26d1bc82019-08-30 11:57:18 +0100424} // namespace android::hardware::neuralnetworks::V1_3::vts::functional