blob: 595ad85633004a6d89da8300907c615a5d0e0eab [file] [log] [blame]
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -07001/*
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
Xusong Wang34058782019-01-18 17:28:26 -080017#include "GeneratedTestHarness.h"
Xusong Wang8e8b70c2019-08-09 16:38:14 -070018
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010019#include "1.0/Callbacks.h"
20#include "1.0/Utils.h"
21#include "MemoryUtils.h"
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070022#include "TestHarness.h"
Xusong Wang9e2b97b2019-08-23 16:10:54 -070023#include "VtsHalNeuralnetworks.h"
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070024
25#include <android-base/logging.h>
Miao Wanga2d04c82018-02-05 17:26:54 -080026#include <android/hardware/neuralnetworks/1.0/IDevice.h>
Miao Wanga2d04c82018-02-05 17:26:54 -080027#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
Miao Wanga2d04c82018-02-05 17:26:54 -080028#include <android/hardware/neuralnetworks/1.0/types.h>
29#include <android/hidl/allocator/1.0/IAllocator.h>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070030#include <android/hidl/memory/1.0/IMemory.h>
31#include <hidlmemory/mapping.h>
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010032
Xusong Wang8e8b70c2019-08-09 16:38:14 -070033#include <gtest/gtest.h>
Michael Butler0897ab32017-10-04 02:38:42 -070034#include <iostream>
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -070035
Michael Butlerbbe5dad2019-08-26 23:55:47 -070036namespace android::hardware::neuralnetworks::V1_0::vts::functional {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010037
Xusong Wang8e8b70c2019-08-09 16:38:14 -070038using namespace test_helper;
Michael Butlerbbe5dad2019-08-26 23:55:47 -070039using hidl::memory::V1_0::IMemory;
40using implementation::ExecutionCallback;
41using implementation::PreparedModelCallback;
Xusong Wang8e8b70c2019-08-09 16:38:14 -070042
43Model createModel(const TestModel& testModel) {
44 // Model operands.
45 hidl_vec<Operand> operands(testModel.operands.size());
46 size_t constCopySize = 0, constRefSize = 0;
47 for (uint32_t i = 0; i < testModel.operands.size(); i++) {
48 const auto& op = testModel.operands[i];
49
50 DataLocation loc = {};
51 if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) {
52 loc = {.poolIndex = 0,
53 .offset = static_cast<uint32_t>(constCopySize),
54 .length = static_cast<uint32_t>(op.data.size())};
55 constCopySize += op.data.alignedSize();
56 } else if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) {
57 loc = {.poolIndex = 0,
58 .offset = static_cast<uint32_t>(constRefSize),
59 .length = static_cast<uint32_t>(op.data.size())};
60 constRefSize += op.data.alignedSize();
61 }
62
63 operands[i] = {.type = static_cast<OperandType>(op.type),
64 .dimensions = op.dimensions,
65 .numberOfConsumers = op.numberOfConsumers,
66 .scale = op.scale,
67 .zeroPoint = op.zeroPoint,
68 .lifetime = static_cast<OperandLifeTime>(op.lifetime),
69 .location = loc};
70 }
71
72 // Model operations.
73 hidl_vec<Operation> operations(testModel.operations.size());
74 std::transform(testModel.operations.begin(), testModel.operations.end(), operations.begin(),
75 [](const TestOperation& op) -> Operation {
76 return {.type = static_cast<OperationType>(op.type),
77 .inputs = op.inputs,
78 .outputs = op.outputs};
79 });
80
81 // Constant copies.
82 hidl_vec<uint8_t> operandValues(constCopySize);
83 for (uint32_t i = 0; i < testModel.operands.size(); i++) {
84 const auto& op = testModel.operands[i];
85 if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) {
86 const uint8_t* begin = op.data.get<uint8_t>();
87 const uint8_t* end = begin + op.data.size();
88 std::copy(begin, end, operandValues.data() + operands[i].location.offset);
89 }
90 }
91
92 // Shared memory.
93 hidl_vec<hidl_memory> pools;
94 if (constRefSize > 0) {
95 hidl_vec_push_back(&pools, nn::allocateSharedMemory(constRefSize));
96 CHECK_NE(pools[0].size(), 0u);
97
98 // load data
99 sp<IMemory> mappedMemory = mapMemory(pools[0]);
100 CHECK(mappedMemory.get() != nullptr);
101 uint8_t* mappedPtr =
102 reinterpret_cast<uint8_t*>(static_cast<void*>(mappedMemory->getPointer()));
103 CHECK(mappedPtr != nullptr);
104
105 for (uint32_t i = 0; i < testModel.operands.size(); i++) {
106 const auto& op = testModel.operands[i];
107 if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) {
108 const uint8_t* begin = op.data.get<uint8_t>();
109 const uint8_t* end = begin + op.data.size();
110 std::copy(begin, end, mappedPtr + operands[i].location.offset);
111 }
112 }
113 }
114
115 return {.operands = std::move(operands),
116 .operations = std::move(operations),
117 .inputIndexes = testModel.inputIndexes,
118 .outputIndexes = testModel.outputIndexes,
119 .operandValues = std::move(operandValues),
120 .pools = std::move(pools)};
121}
Xusong Wangd2933152019-03-12 14:40:32 -0700122
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700123// Top level driver for models and examples generated by test_generator.py
124// Test driver for those generated from ml/nn/runtime/test/spec
Michael Butlere16af0a2019-08-29 22:17:24 -0700125void Execute(const sp<IDevice>& device, const TestModel& testModel) {
126 const Model model = createModel(testModel);
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700127 const Request request = createRequest(testModel);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700128
Michael Butlere16af0a2019-08-29 22:17:24 -0700129 // Create IPreparedModel.
130 sp<IPreparedModel> preparedModel;
131 createPreparedModel(device, model, &preparedModel);
132 if (preparedModel == nullptr) return;
133
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700134 // Launch execution.
135 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
136 Return<ErrorStatus> executionLaunchStatus = preparedModel->execute(request, executionCallback);
137 ASSERT_TRUE(executionLaunchStatus.isOk());
138 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700139
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700140 // Retrieve execution status.
141 executionCallback->wait();
142 ASSERT_EQ(ErrorStatus::NONE, executionCallback->getStatus());
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000143
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700144 // Retrieve execution results.
145 const std::vector<TestBuffer> outputs = getOutputBuffers(request);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700146
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700147 // We want "close-enough" results.
148 checkResults(testModel, outputs);
Xusong Wang34058782019-01-18 17:28:26 -0800149}
150
Michael Butler7076f622019-08-29 11:08:25 -0700151void GeneratedTestBase::SetUp() {
152 testing::TestWithParam<GeneratedTestParam>::SetUp();
153 ASSERT_NE(kDevice, nullptr);
154}
155
156std::vector<NamedModel> getNamedModels(const FilterFn& filter) {
157 return TestModelManager::get().getTestModels(filter);
158}
159
160std::string printGeneratedTest(const testing::TestParamInfo<GeneratedTestParam>& info) {
161 const auto& [namedDevice, namedModel] = info.param;
162 return gtestCompliantName(getName(namedDevice) + "_" + getName(namedModel));
163}
164
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700165// Tag for the generated tests
Michael Butlere16af0a2019-08-29 22:17:24 -0700166class GeneratedTest : public GeneratedTestBase {};
Miao Wanga2d04c82018-02-05 17:26:54 -0800167
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700168TEST_P(GeneratedTest, Test) {
Michael Butlere16af0a2019-08-29 22:17:24 -0700169 Execute(kDevice, kTestModel);
Slava Shklyaev871be942018-09-12 14:52:02 +0100170}
171
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700172INSTANTIATE_GENERATED_TEST(GeneratedTest,
173 [](const TestModel& testModel) { return !testModel.expectFailure; });
174
Michael Butlerbbe5dad2019-08-26 23:55:47 -0700175} // namespace android::hardware::neuralnetworks::V1_0::vts::functional