blob: e28605dca208752f14e759999419fef7d3f13495 [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.
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +000045 CHECK_EQ(testModel.referenced.size(), 0u); // Not supported in 1.0.
46 hidl_vec<Operand> operands(testModel.main.operands.size());
Xusong Wang8e8b70c2019-08-09 16:38:14 -070047 size_t constCopySize = 0, constRefSize = 0;
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +000048 for (uint32_t i = 0; i < testModel.main.operands.size(); i++) {
49 const auto& op = testModel.main.operands[i];
Xusong Wang8e8b70c2019-08-09 16:38:14 -070050
51 DataLocation loc = {};
52 if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) {
53 loc = {.poolIndex = 0,
54 .offset = static_cast<uint32_t>(constCopySize),
55 .length = static_cast<uint32_t>(op.data.size())};
56 constCopySize += op.data.alignedSize();
57 } else if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) {
58 loc = {.poolIndex = 0,
59 .offset = static_cast<uint32_t>(constRefSize),
60 .length = static_cast<uint32_t>(op.data.size())};
61 constRefSize += op.data.alignedSize();
62 }
63
64 operands[i] = {.type = static_cast<OperandType>(op.type),
65 .dimensions = op.dimensions,
66 .numberOfConsumers = op.numberOfConsumers,
67 .scale = op.scale,
68 .zeroPoint = op.zeroPoint,
69 .lifetime = static_cast<OperandLifeTime>(op.lifetime),
70 .location = loc};
71 }
72
73 // Model operations.
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +000074 hidl_vec<Operation> operations(testModel.main.operations.size());
75 std::transform(testModel.main.operations.begin(), testModel.main.operations.end(),
76 operations.begin(), [](const TestOperation& op) -> Operation {
Xusong Wang8e8b70c2019-08-09 16:38:14 -070077 return {.type = static_cast<OperationType>(op.type),
78 .inputs = op.inputs,
79 .outputs = op.outputs};
80 });
81
82 // Constant copies.
83 hidl_vec<uint8_t> operandValues(constCopySize);
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +000084 for (uint32_t i = 0; i < testModel.main.operands.size(); i++) {
85 const auto& op = testModel.main.operands[i];
Xusong Wang8e8b70c2019-08-09 16:38:14 -070086 if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) {
87 const uint8_t* begin = op.data.get<uint8_t>();
88 const uint8_t* end = begin + op.data.size();
89 std::copy(begin, end, operandValues.data() + operands[i].location.offset);
90 }
91 }
92
93 // Shared memory.
94 hidl_vec<hidl_memory> pools;
95 if (constRefSize > 0) {
96 hidl_vec_push_back(&pools, nn::allocateSharedMemory(constRefSize));
97 CHECK_NE(pools[0].size(), 0u);
98
99 // load data
100 sp<IMemory> mappedMemory = mapMemory(pools[0]);
101 CHECK(mappedMemory.get() != nullptr);
102 uint8_t* mappedPtr =
103 reinterpret_cast<uint8_t*>(static_cast<void*>(mappedMemory->getPointer()));
104 CHECK(mappedPtr != nullptr);
105
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000106 for (uint32_t i = 0; i < testModel.main.operands.size(); i++) {
107 const auto& op = testModel.main.operands[i];
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700108 if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) {
109 const uint8_t* begin = op.data.get<uint8_t>();
110 const uint8_t* end = begin + op.data.size();
111 std::copy(begin, end, mappedPtr + operands[i].location.offset);
112 }
113 }
114 }
115
116 return {.operands = std::move(operands),
117 .operations = std::move(operations),
Slava Shklyaev1f98e2e2020-01-31 15:14:24 +0000118 .inputIndexes = testModel.main.inputIndexes,
119 .outputIndexes = testModel.main.outputIndexes,
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700120 .operandValues = std::move(operandValues),
121 .pools = std::move(pools)};
122}
Xusong Wangd2933152019-03-12 14:40:32 -0700123
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700124// Top level driver for models and examples generated by test_generator.py
125// Test driver for those generated from ml/nn/runtime/test/spec
Michael Butlere16af0a2019-08-29 22:17:24 -0700126void Execute(const sp<IDevice>& device, const TestModel& testModel) {
127 const Model model = createModel(testModel);
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700128 const Request request = createRequest(testModel);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700129
Michael Butlere16af0a2019-08-29 22:17:24 -0700130 // Create IPreparedModel.
131 sp<IPreparedModel> preparedModel;
132 createPreparedModel(device, model, &preparedModel);
133 if (preparedModel == nullptr) return;
134
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700135 // Launch execution.
136 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
137 Return<ErrorStatus> executionLaunchStatus = preparedModel->execute(request, executionCallback);
138 ASSERT_TRUE(executionLaunchStatus.isOk());
139 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700140
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700141 // Retrieve execution status.
142 executionCallback->wait();
143 ASSERT_EQ(ErrorStatus::NONE, executionCallback->getStatus());
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000144
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700145 // Retrieve execution results.
146 const std::vector<TestBuffer> outputs = getOutputBuffers(request);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700147
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700148 // We want "close-enough" results.
149 checkResults(testModel, outputs);
Xusong Wang34058782019-01-18 17:28:26 -0800150}
151
Michael Butler7076f622019-08-29 11:08:25 -0700152void GeneratedTestBase::SetUp() {
153 testing::TestWithParam<GeneratedTestParam>::SetUp();
154 ASSERT_NE(kDevice, nullptr);
155}
156
157std::vector<NamedModel> getNamedModels(const FilterFn& filter) {
158 return TestModelManager::get().getTestModels(filter);
159}
160
161std::string printGeneratedTest(const testing::TestParamInfo<GeneratedTestParam>& info) {
162 const auto& [namedDevice, namedModel] = info.param;
163 return gtestCompliantName(getName(namedDevice) + "_" + getName(namedModel));
164}
165
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700166// Tag for the generated tests
Michael Butlere16af0a2019-08-29 22:17:24 -0700167class GeneratedTest : public GeneratedTestBase {};
Miao Wanga2d04c82018-02-05 17:26:54 -0800168
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700169TEST_P(GeneratedTest, Test) {
Michael Butlere16af0a2019-08-29 22:17:24 -0700170 Execute(kDevice, kTestModel);
Slava Shklyaev871be942018-09-12 14:52:02 +0100171}
172
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700173INSTANTIATE_GENERATED_TEST(GeneratedTest,
174 [](const TestModel& testModel) { return !testModel.expectFailure; });
175
Michael Butlerbbe5dad2019-08-26 23:55:47 -0700176} // namespace android::hardware::neuralnetworks::V1_0::vts::functional