blob: 5f96539fc4735027d45becc2a2c8a80512589bcf [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
36namespace android {
37namespace hardware {
38namespace neuralnetworks {
Slava Shklyaev2bcfdc82019-07-17 15:50:57 +010039namespace V1_0 {
Xusong Wang9e2b97b2019-08-23 16:10:54 -070040namespace vts {
41namespace functional {
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010042
Xusong Wang8e8b70c2019-08-09 16:38:14 -070043using namespace test_helper;
Slava Shklyaev1d6b4652019-05-14 14:15:14 +010044using ::android::hardware::neuralnetworks::V1_0::ErrorStatus;
45using ::android::hardware::neuralnetworks::V1_0::IDevice;
46using ::android::hardware::neuralnetworks::V1_0::IPreparedModel;
47using ::android::hardware::neuralnetworks::V1_0::Model;
48using ::android::hardware::neuralnetworks::V1_0::Request;
49using ::android::hardware::neuralnetworks::V1_0::RequestArgument;
50using ::android::hardware::neuralnetworks::V1_0::implementation::ExecutionCallback;
51using ::android::hardware::neuralnetworks::V1_0::implementation::PreparedModelCallback;
52using ::android::hidl::memory::V1_0::IMemory;
Xusong Wang8e8b70c2019-08-09 16:38:14 -070053
54Model createModel(const TestModel& testModel) {
55 // Model operands.
56 hidl_vec<Operand> operands(testModel.operands.size());
57 size_t constCopySize = 0, constRefSize = 0;
58 for (uint32_t i = 0; i < testModel.operands.size(); i++) {
59 const auto& op = testModel.operands[i];
60
61 DataLocation loc = {};
62 if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) {
63 loc = {.poolIndex = 0,
64 .offset = static_cast<uint32_t>(constCopySize),
65 .length = static_cast<uint32_t>(op.data.size())};
66 constCopySize += op.data.alignedSize();
67 } else if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) {
68 loc = {.poolIndex = 0,
69 .offset = static_cast<uint32_t>(constRefSize),
70 .length = static_cast<uint32_t>(op.data.size())};
71 constRefSize += op.data.alignedSize();
72 }
73
74 operands[i] = {.type = static_cast<OperandType>(op.type),
75 .dimensions = op.dimensions,
76 .numberOfConsumers = op.numberOfConsumers,
77 .scale = op.scale,
78 .zeroPoint = op.zeroPoint,
79 .lifetime = static_cast<OperandLifeTime>(op.lifetime),
80 .location = loc};
81 }
82
83 // Model operations.
84 hidl_vec<Operation> operations(testModel.operations.size());
85 std::transform(testModel.operations.begin(), testModel.operations.end(), operations.begin(),
86 [](const TestOperation& op) -> Operation {
87 return {.type = static_cast<OperationType>(op.type),
88 .inputs = op.inputs,
89 .outputs = op.outputs};
90 });
91
92 // Constant copies.
93 hidl_vec<uint8_t> operandValues(constCopySize);
94 for (uint32_t i = 0; i < testModel.operands.size(); i++) {
95 const auto& op = testModel.operands[i];
96 if (op.lifetime == TestOperandLifeTime::CONSTANT_COPY) {
97 const uint8_t* begin = op.data.get<uint8_t>();
98 const uint8_t* end = begin + op.data.size();
99 std::copy(begin, end, operandValues.data() + operands[i].location.offset);
100 }
101 }
102
103 // Shared memory.
104 hidl_vec<hidl_memory> pools;
105 if (constRefSize > 0) {
106 hidl_vec_push_back(&pools, nn::allocateSharedMemory(constRefSize));
107 CHECK_NE(pools[0].size(), 0u);
108
109 // load data
110 sp<IMemory> mappedMemory = mapMemory(pools[0]);
111 CHECK(mappedMemory.get() != nullptr);
112 uint8_t* mappedPtr =
113 reinterpret_cast<uint8_t*>(static_cast<void*>(mappedMemory->getPointer()));
114 CHECK(mappedPtr != nullptr);
115
116 for (uint32_t i = 0; i < testModel.operands.size(); i++) {
117 const auto& op = testModel.operands[i];
118 if (op.lifetime == TestOperandLifeTime::CONSTANT_REFERENCE) {
119 const uint8_t* begin = op.data.get<uint8_t>();
120 const uint8_t* end = begin + op.data.size();
121 std::copy(begin, end, mappedPtr + operands[i].location.offset);
122 }
123 }
124 }
125
126 return {.operands = std::move(operands),
127 .operations = std::move(operations),
128 .inputIndexes = testModel.inputIndexes,
129 .outputIndexes = testModel.outputIndexes,
130 .operandValues = std::move(operandValues),
131 .pools = std::move(pools)};
132}
Xusong Wangd2933152019-03-12 14:40:32 -0700133
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700134// Top level driver for models and examples generated by test_generator.py
135// Test driver for those generated from ml/nn/runtime/test/spec
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700136void EvaluatePreparedModel(const sp<IPreparedModel>& preparedModel, const TestModel& testModel) {
137 const Request request = createRequest(testModel);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700138
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700139 // Launch execution.
140 sp<ExecutionCallback> executionCallback = new ExecutionCallback();
141 Return<ErrorStatus> executionLaunchStatus = preparedModel->execute(request, executionCallback);
142 ASSERT_TRUE(executionLaunchStatus.isOk());
143 EXPECT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(executionLaunchStatus));
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700144
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700145 // Retrieve execution status.
146 executionCallback->wait();
147 ASSERT_EQ(ErrorStatus::NONE, executionCallback->getStatus());
Michael K. Sandersefa4c812018-10-30 14:44:48 +0000148
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700149 // Retrieve execution results.
150 const std::vector<TestBuffer> outputs = getOutputBuffers(request);
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700151
Xusong Wang8e8b70c2019-08-09 16:38:14 -0700152 // We want "close-enough" results.
153 checkResults(testModel, outputs);
Xusong Wang34058782019-01-18 17:28:26 -0800154}
155
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700156// Tag for the generated tests
157class GeneratedTest : public GeneratedTestBase {
158 protected:
159 void Execute(const TestModel& testModel) {
160 Model model = createModel(testModel);
Miao Wanga2d04c82018-02-05 17:26:54 -0800161
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700162 // see if service can handle model
163 bool fullySupportsModel = false;
164 Return<void> supportedCall = device->getSupportedOperations(
165 model, [&fullySupportsModel](ErrorStatus status, const hidl_vec<bool>& supported) {
166 ASSERT_EQ(ErrorStatus::NONE, status);
167 ASSERT_NE(0ul, supported.size());
168 fullySupportsModel = std::all_of(supported.begin(), supported.end(),
169 [](bool valid) { return valid; });
170 });
171 ASSERT_TRUE(supportedCall.isOk());
Michael Butler4d5bb102018-02-26 15:24:46 -0800172
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700173 // launch prepare model
174 sp<PreparedModelCallback> preparedModelCallback = new PreparedModelCallback();
175 Return<ErrorStatus> prepareLaunchStatus =
176 device->prepareModel(model, preparedModelCallback);
177 ASSERT_TRUE(prepareLaunchStatus.isOk());
178 ASSERT_EQ(ErrorStatus::NONE, static_cast<ErrorStatus>(prepareLaunchStatus));
Miao Wanga2d04c82018-02-05 17:26:54 -0800179
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700180 // retrieve prepared model
181 preparedModelCallback->wait();
182 ErrorStatus prepareReturnStatus = preparedModelCallback->getStatus();
183 sp<IPreparedModel> preparedModel = preparedModelCallback->getPreparedModel();
Miao Wanga2d04c82018-02-05 17:26:54 -0800184
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700185 // early termination if vendor service cannot fully prepare model
186 if (!fullySupportsModel && prepareReturnStatus != ErrorStatus::NONE) {
187 ASSERT_EQ(nullptr, preparedModel.get());
188 LOG(INFO) << "NN VTS: Early termination of test because vendor service cannot "
189 "prepare model that it does not support.";
190 std::cout << "[ ] Early termination of test because vendor service cannot "
191 "prepare model that it does not support."
192 << std::endl;
193 GTEST_SKIP();
194 }
195 EXPECT_EQ(ErrorStatus::NONE, prepareReturnStatus);
196 ASSERT_NE(nullptr, preparedModel.get());
197
198 EvaluatePreparedModel(preparedModel, testModel);
Miao Wanga2d04c82018-02-05 17:26:54 -0800199 }
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700200};
Miao Wanga2d04c82018-02-05 17:26:54 -0800201
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700202TEST_P(GeneratedTest, Test) {
203 Execute(*mTestModel);
Slava Shklyaev871be942018-09-12 14:52:02 +0100204}
205
Xusong Wang9e2b97b2019-08-23 16:10:54 -0700206INSTANTIATE_GENERATED_TEST(GeneratedTest,
207 [](const TestModel& testModel) { return !testModel.expectFailure; });
208
209} // namespace functional
210} // namespace vts
Slava Shklyaev2bcfdc82019-07-17 15:50:57 +0100211} // namespace V1_0
I-Jui (Ray) Sung2c4e1362017-09-06 02:15:54 -0700212} // namespace neuralnetworks
213} // namespace hardware
214} // namespace android