blob: 14d300db7147410ac20a84bfefa3cc64f1253e00 [file] [log] [blame]
Slava Shklyaev1d6b4652019-05-14 14:15:14 +01001/*
2 * Copyright (C) 2017 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/IPreparedModel.h>
21#include <android/hardware/neuralnetworks/1.0/types.h>
22#include <android/hardware/neuralnetworks/1.1/IDevice.h>
23#include <android/hidl/allocator/1.0/IAllocator.h>
24#include <android/hidl/memory/1.0/IMemory.h>
25#include <hidlmemory/mapping.h>
26
Xusong Wang6aad0402019-08-09 16:41:16 -070027#include <gtest/gtest.h>
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010028#include <iostream>
29
30#include "1.0/Callbacks.h"
31#include "1.0/Utils.h"
32#include "MemoryUtils.h"
33#include "TestHarness.h"
Xusong Wang9e2b97b2019-08-23 16:10:54 -070034#include "VtsHalNeuralnetworks.h"
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010035
Michael Butlerbbe5dad2019-08-26 23:55:47 -070036namespace android::hardware::neuralnetworks::V1_1::vts::functional {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010037
Xusong Wang6aad0402019-08-09 16:41:16 -070038using namespace test_helper;
Michael Butlerbbe5dad2019-08-26 23:55:47 -070039using hidl::memory::V1_0::IMemory;
40using V1_0::DataLocation;
41using V1_0::ErrorStatus;
42using V1_0::IPreparedModel;
43using V1_0::Operand;
44using V1_0::OperandLifeTime;
45using V1_0::OperandType;
46using V1_0::Request;
47using V1_0::implementation::ExecutionCallback;
48using V1_0::implementation::PreparedModelCallback;
Xusong Wang6aad0402019-08-09 16:41:16 -070049
50Model createModel(const TestModel& testModel) {
51 // Model operands.
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +000052 CHECK_EQ(testModel.referenced.size(), 0u); // Not supported in 1.1.
53 hidl_vec<Operand> operands(testModel.main.operands.size());
Xusong Wang6aad0402019-08-09 16:41:16 -070054 size_t constCopySize = 0, constRefSize = 0;
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +000055 for (uint32_t i = 0; i < testModel.main.operands.size(); i++) {
56 const auto& op = testModel.main.operands[i];
Xusong Wang6aad0402019-08-09 16:41:16 -070057
58 DataLocation loc = {};
59 if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) {
60 loc = {.poolIndex = 0,
61 .offset = static_cast<uint32_t>(constCopySize),
62 .length = static_cast<uint32_t>(op.data.size())};
63 constCopySize += op.data.alignedSize();
64 } else if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) {
65 loc = {.poolIndex = 0,
66 .offset = static_cast<uint32_t>(constRefSize),
67 .length = static_cast<uint32_t>(op.data.size())};
68 constRefSize += op.data.alignedSize();
69 }
70
71 operands[i] = {.type = static_cast<OperandType>(op.type),
72 .dimensions = op.dimensions,
73 .numberOfConsumers = op.numberOfConsumers,
74 .scale = op.scale,
75 .zeroPoint = op.zeroPoint,
76 .lifetime = static_cast<OperandLifeTime>(op.lifetime),
77 .location = loc};
78 }
79
80 // Model operations.
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +000081 hidl_vec<Operation> operations(testModel.main.operations.size());
82 std::transform(testModel.main.operations.begin(), testModel.main.operations.end(),
83 operations.begin(), [](const TestOperation& op) -> Operation {
Xusong Wang6aad0402019-08-09 16:41:16 -070084 return {.type = static_cast<OperationType>(op.type),
85 .inputs = op.inputs,
86 .outputs = op.outputs};
87 });
88
89 // Constant copies.
90 hidl_vec<uint8_t> operandValues(constCopySize);
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +000091 for (uint32_t i = 0; i < testModel.main.operands.size(); i++) {
92 const auto& op = testModel.main.operands[i];
Xusong Wang6aad0402019-08-09 16:41:16 -070093 if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) {
94 const uint8_t* begin = op.data.get<uint8_t>();
95 const uint8_t* end = begin + op.data.size();
96 std::copy(begin, end, operandValues.data() + operands[i].location.offset);
97 }
98 }
99
100 // Shared memory.
101 hidl_vec<hidl_memory> pools;
102 if (constRefSize > 0) {
103 hidl_vec_push_back(&pools, nn::allocateSharedMemory(constRefSize));
104 CHECK_NE(pools[0].size(), 0u);
105
106 // load data
107 sp<IMemory> mappedMemory = mapMemory(pools[0]);
108 CHECK(mappedMemory.get() != nullptr);
109 uint8_t* mappedPtr =
110 reinterpret_cast<uint8_t*>(static_cast<void*>(mappedMemory->getPointer()));
111 CHECK(mappedPtr != nullptr);
112
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000113 for (uint32_t i = 0; i < testModel.main.operands.size(); i++) {
114 const auto& op = testModel.main.operands[i];
Xusong Wang6aad0402019-08-09 16:41:16 -0700115 if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) {
116 const uint8_t* begin = op.data.get<uint8_t>();
117 const uint8_t* end = begin + op.data.size();
118 std::copy(begin, end, mappedPtr + operands[i].location.offset);
119 }
120 }
121 }
122
123 return {.operands = std::move(operands),
124 .operations = std::move(operations),
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000125 .inputIndexes = testModel.main.inputIndexes,
126 .outputIndexes = testModel.main.outputIndexes,
Xusong Wang6aad0402019-08-09 16:41:16 -0700127 .operandValues = std::move(operandValues),
128 .pools = std::move(pools),
129 .relaxComputationFloat32toFloat16 = testModel.isRelaxed};
130}
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100131
132// Top level driver for models and examples generated by test_generator.py
133// Test driver for those generated from ml/nn/runtime/test/spec
Michael Butlere16af0a2019-08-29 22:17:24 -0700134void Execute(const sp<IDevice>& device, const TestModel& testModel) {
135 const Model model = createModel(testModel);
Xusong Wang41adc5b2020-02-25 11:43:10 -0800136
137 ExecutionContext context;
138 const Request request = context.createRequest(testModel);
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100139
Michael Butlere16af0a2019-08-29 22:17:24 -0700140 // Create IPreparedModel.
141 sp<IPreparedModel> preparedModel;
142 createPreparedModel(device, model, &preparedModel);
143 if (preparedModel == nullptr) return;
144
Xusong Wang6aad0402019-08-09 16:41:16 -0700145 // Launch execution.
146 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
147 Return<ErrorStatus> executionLaunchStatus = preparedModel->execute(request, executionCallback);
148 ASSERT_TRUE(executionLaunchStatus.isOk());
149 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100150
Xusong Wang6aad0402019-08-09 16:41:16 -0700151 // Retrieve execution status.
152 executionCallback->wait();
153 ASSERT_EQ(ErrorStatus::NONE, executionCallback->getStatus());
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100154
Xusong Wang6aad0402019-08-09 16:41:16 -0700155 // Retrieve execution results.
Xusong Wang41adc5b2020-02-25 11:43:10 -0800156 const std::vector<TestBuffer> outputs = context.getOutputBuffers(request);
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100157
Xusong Wang6aad0402019-08-09 16:41:16 -0700158 // We want "close-enough" results.
159 checkResults(testModel, outputs);
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100160}
161
Michael Butler7076f622019-08-29 11:08:25 -0700162void GeneratedTestBase::SetUp() {
163 testing::TestWithParam<GeneratedTestParam>::SetUp();
164 ASSERT_NE(kDevice, nullptr);
165}
166
167std::vector<NamedModel> getNamedModels(const FilterFn& filter) {
168 return TestModelManager::get().getTestModels(filter);
169}
170
171std::string printGeneratedTest(const testing::TestParamInfo<GeneratedTestParam>& info) {
172 const auto& [namedDevice, namedModel] = info.param;
173 return gtestCompliantName(getName(namedDevice) + "_" + getName(namedModel));
174}
175
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700176// Tag for the generated tests
Michael Butlere16af0a2019-08-29 22:17:24 -0700177class GeneratedTest : public GeneratedTestBase {};
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100178
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700179TEST_P(GeneratedTest, Test) {
Michael Butlere16af0a2019-08-29 22:17:24 -0700180 Execute(kDevice, kTestModel);
Slava Shklyaev1d6b4652019-05-14 14:15:14 +0100181}
182
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700183INSTANTIATE_GENERATED_TEST(GeneratedTest,
184 [](const TestModel& testModel) { return !testModel.expectFailure; });
185
Michael Butlerbbe5dad2019-08-26 23:55:47 -0700186} // namespace android::hardware::neuralnetworks::V1_1::vts::functional